diff --git a/ckiwi/luacompat.h b/ckiwi/luacompat.h index 4615994..c0ece96 100644 --- a/ckiwi/luacompat.h +++ b/ckiwi/luacompat.h @@ -1,9 +1,15 @@ #ifndef LKIWI_LUACOMPAT_H_ #define LKIWI_LUACOMPAT_H_ +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus #include #include #include +#ifdef __cplusplus +} +#endif // __cplusplus #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 diff --git a/ckiwi/luakiwi-int.h b/ckiwi/luakiwi-int.h index bd7a49f..024cdb8 100644 --- a/ckiwi/luakiwi-int.h +++ b/ckiwi/luakiwi-int.h @@ -3,7 +3,7 @@ #include "luacompat.h" -#define ARR_COUNT(x) (sizeof(x) / sizeof((x)[0])) +#define ARR_COUNT(x) ((int)(sizeof(x) / sizeof((x)[0]))) #if defined(__GNUC__) && !defined(LKIWI_NO_BUILTIN) #define l_likely(x) (__builtin_expect(((x) != 0), 1)) diff --git a/ckiwi/luakiwi.c b/ckiwi/luakiwi.cpp similarity index 95% rename from ckiwi/luakiwi.c rename to ckiwi/luakiwi.cpp index a8240d5..513be00 100644 --- a/ckiwi/luakiwi.c +++ b/ckiwi/luakiwi.cpp @@ -68,7 +68,7 @@ static inline int is_udata_obj(lua_State* L, int type_id) { // get typename, copy the stack string to tidx, helpful when using // with buffers. -static const char* typename(lua_State* L, int idx, int tidx) { +static const char* lk_typename(lua_State* L, int idx, int tidx) { const char* ret = 0; if (lua_getmetatable(L, idx)) { lua_getfield(L, -1, "__name"); @@ -91,14 +91,14 @@ static int op_error(lua_State* L, const char* op, int lidx, int ridx) { int stridx = lua_gettop(L); luaL_buffinit(L, &buf); - lua_pushfstring(L, "invalid operand type for '%s' %s('", op, typename(L, lidx, stridx)); + lua_pushfstring(L, "invalid operand type for '%s' %s('", op, lk_typename(L, lidx, stridx)); luaL_addvalue(&buf); str = luaL_tolstring(L, lidx, &len); lua_replace(L, stridx); luaL_addlstring(&buf, str, len < 100 ? len : 100); - lua_pushfstring(L, "') and %s('", typename(L, ridx, stridx)); + lua_pushfstring(L, "') and %s('", lk_typename(L, ridx, stridx)); luaL_addvalue(&buf); str = luaL_tolstring(L, ridx, &len); @@ -150,11 +150,11 @@ static inline KiwiVarRef try_var(lua_State* L, int idx) { } static inline KiwiTerm* try_term(lua_State* L, int idx) { - return try_type(L, idx, TERM); + return (KiwiTerm*)try_type(L, idx, TERM); } static inline KiwiExpression* try_expr(lua_State* L, int idx) { - return try_type(L, idx, EXPR); + return (KiwiExpression*)try_type(L, idx, EXPR); } // method to test types for expression functions @@ -195,18 +195,18 @@ static inline KiwiVarRef get_var(lua_State* L, int idx) { } static inline KiwiTerm* get_term(lua_State* L, int idx) { - return check_arg(L, idx, TERM); + return (KiwiTerm*)check_arg(L, idx, TERM); } static inline KiwiExpression* get_expr(lua_State* L, int idx) { - return check_arg(L, idx, EXPR); + return (KiwiExpression*)check_arg(L, idx, EXPR); } static inline KiwiExpression* get_expr_opt(lua_State* L, int idx) { if (lua_isnoneornil(L, idx)) { return 0; } - return check_arg(L, idx, EXPR); + return (KiwiExpression*)check_arg(L, idx, EXPR); } static inline KiwiConstraintRef get_constraint(lua_State* L, int idx) { @@ -219,7 +219,7 @@ static inline KiwiSolver* get_solver(lua_State* L, int idx) { // note this expects the 2nd upvalue to have the variable weak table static inline KiwiVarRef var_new(lua_State* L, KiwiVarRef var) { - KiwiVarRef* varp = lua_newuserdata(L, sizeof(KiwiVarRef)); + KiwiVarRef* varp = (KiwiVarRef*)lua_newuserdata(L, sizeof(KiwiVarRef)); *varp = var; push_type(L, VAR); @@ -238,14 +238,14 @@ static inline KiwiVarRef var_new(lua_State* L, KiwiVarRef var) { } static inline KiwiTerm* term_new(lua_State* L) { - KiwiTerm* ret = lua_newuserdata(L, sizeof(KiwiTerm)); + KiwiTerm* ret = (KiwiTerm*)lua_newuserdata(L, sizeof(KiwiTerm)); push_type(L, TERM); lua_setmetatable(L, -2); return ret; } static inline KiwiExpression* expr_new(lua_State* L, int nterms) { - KiwiExpression* ret = lua_newuserdata( + KiwiExpression* ret = (KiwiExpression*)lua_newuserdata( L, sizeof(KiwiExpression) + sizeof(KiwiTerm) * (nterms > 1 ? nterms - 1 : 0) ); @@ -261,7 +261,7 @@ static inline KiwiConstraintRef constraint_new( enum KiwiRelOp op, double strength ) { - KiwiConstraintRef* ref = lua_newuserdata(L, sizeof(KiwiConstraintRef)); + KiwiConstraintRef* ref = (KiwiConstraintRef*)lua_newuserdata(L, sizeof(KiwiConstraintRef)); *ref = kiwi_constraint_new(lhs, rhs, op, strength); push_type(L, CONSTRAINT); @@ -284,7 +284,7 @@ static KiwiExpression* toexpr(lua_State* L, int idx, KiwiExpression* temp) { push_type(L, EXPR); if (lua_rawequal(L, -1, -2)) { - return ud; + return (KiwiExpression*)ud; } temp->constant = 0; @@ -398,10 +398,10 @@ static int lkiwi_var_m_add(lua_State* L) { return push_expr_pair(L, 0.0, &ta, &tb); } case TERM: - return push_expr_var_term(L, 0.0, var_a, arg_b); + return push_expr_var_term(L, 0.0, var_a, (const KiwiTerm*)arg_b); case EXPR: { const KiwiTerm t = {var_a, 1.0}; - return push_add_expr_term(L, arg_b, &t); + return push_add_expr_term(L, (const KiwiExpression*)arg_b, &t); } case NUMBER: { const KiwiTerm t = {var_a, 1.0}; @@ -605,7 +605,7 @@ static int lkiwi_term_m_add(lua_State* L) { int isnum_a; num = lua_tonumberx(L, 1, &isnum_a); if (isnum_a) { - return push_expr_one(L, num, arg_b); + return push_expr_one(L, num, (const KiwiTerm*)arg_b); } } @@ -613,13 +613,13 @@ static int lkiwi_term_m_add(lua_State* L) { if (term_a) { switch (type_id_b) { case TERM: - return push_expr_pair(L, 0.0, term_a, arg_b); + return push_expr_pair(L, 0.0, term_a, (const KiwiTerm*)arg_b); case VAR: { const KiwiTerm term_b = {*(KiwiVarRef*)arg_b, 1.0}; return push_expr_pair(L, 0.0, term_a, &term_b); } case EXPR: - return push_add_expr_term(L, arg_b, term_a); + return push_add_expr_term(L, (const KiwiExpression*)arg_b, term_a); case NUMBER: return push_expr_one(L, num, term_a); default: @@ -791,7 +791,7 @@ static int lkiwi_expr_m_add(lua_State* L) { int isnum_a; num = lua_tonumberx(L, 1, &isnum_a); if (isnum_a) { - const KiwiExpression* expr_b = arg_b; + const KiwiExpression* expr_b = (const KiwiExpression*)arg_b; return push_expr_constant(L, expr_b, num + expr_b->constant); } } @@ -800,9 +800,9 @@ static int lkiwi_expr_m_add(lua_State* L) { if (expr_a) { switch (type_id_b) { case EXPR: - return push_add_expr_expr(L, expr_a, arg_b); + return push_add_expr_expr(L, expr_a, (const KiwiExpression*)arg_b); case TERM: - return push_add_expr_term(L, expr_a, arg_b); + return push_add_expr_term(L, expr_a, (const KiwiTerm*)arg_b); case VAR: { const KiwiTerm term_b = {*(KiwiVarRef*)arg_b, 1.0}; return push_add_expr_term(L, expr_a, &term_b); @@ -964,7 +964,7 @@ static int lkiwi_constraint_violated(lua_State* L) { static int lkiwi_constraint_expression(lua_State* L) { KiwiConstraintRef c = get_constraint(L, 1); - const static int SZ = 12; + const static int SZ = 7; KiwiExpression* expr = expr_new(L, SZ); int n = kiwi_constraint_expression(c, expr, SZ); @@ -980,11 +980,12 @@ static int lkiwi_constraint_m_tostring(lua_State* L) { KiwiConstraintRef c = get_constraint(L, 1); const static int SZ = 16; - KiwiExpression* expr = stalloc(sizeof(KiwiExpression) + sizeof(KiwiTerm) * (SZ - 1)); + KiwiExpression* expr = + (KiwiExpression*)stalloc(sizeof(KiwiExpression) + sizeof(KiwiTerm) * (SZ - 1)); int n = kiwi_constraint_expression(c, expr, SZ); if (l_unlikely(n > SZ)) { - expr = malloc(sizeof(KiwiExpression) + sizeof(KiwiTerm) * (n - 1)); + expr = (KiwiExpression*)malloc(sizeof(KiwiExpression) + sizeof(KiwiTerm) * (n - 1)); if (!expr) { luaL_error(L, "out of memory"); } @@ -1088,7 +1089,7 @@ static int push_pair_constraint( enum KiwiRelOp op, double strength ) { - KiwiExpression* expr = stalloc(sizeof(KiwiExpression) + sizeof(KiwiTerm)); + KiwiExpression* expr = (KiwiExpression*)stalloc(sizeof(KiwiExpression) + sizeof(KiwiTerm)); expr->constant = constant; expr->term_count = 2; expr->terms[0].var = left; @@ -1478,7 +1479,7 @@ static int lkiwi_solver_new_meth(lua_State* L) { error_mask = luaL_optinteger(L, 1, 0); } - KiwiSolver** sp = lua_newuserdata(L, sizeof(KiwiSolver*)); + KiwiSolver** sp = (KiwiSolver**)lua_newuserdata(L, sizeof(KiwiSolver*)); *sp = kiwi_solver_new(error_mask); push_type(L, SOLVER); @@ -1620,7 +1621,13 @@ static void compat_init(lua_State* L, int context_absi) { static void compat_init(lua_State* L, int i) {} #endif /* Lua 5.1 */ -extern int luaopen_ckiwi(lua_State* L) { +#ifdef __cplusplus +extern "C" int luaopen_ckiwi(lua_State* L); +#else +extern luaopen_ckiwi(lua_State* L); +#endif + +int luaopen_ckiwi(lua_State* L) { luaL_checkversion(L); /* context table */