static void json_create_config(lua_State *l) { json_config_t *cfg; int i; cfg = lua_newuserdata(l, sizeof(*cfg)); /* Create GC method to clean up strbuf */ lua_newtable(l); lua_pushcfunction(l, json_destroy_config); lua_setfield(l, -2, "__gc"); lua_setmetatable(l, -2); cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT; cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO; cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE; cfg->encode_max_depth = DEFAULT_ENCODE_MAX_DEPTH; cfg->decode_max_depth = DEFAULT_DECODE_MAX_DEPTH; cfg->encode_invalid_numbers = DEFAULT_ENCODE_INVALID_NUMBERS; cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS; cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER; cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; #if DEFAULT_ENCODE_KEEP_BUFFER > 0 strbuf_init(&cfg->encode_buf, 0); #endif /* Decoding init */ /* Tag all characters as an error */ for (i = 0; i < 256; i++) cfg->ch2token[i] = T_ERROR; /* Set tokens that require no further processing */ cfg->ch2token['{'] = T_OBJ_BEGIN; cfg->ch2token['}'] = T_OBJ_END; cfg->ch2token['['] = T_ARR_BEGIN; cfg->ch2token[']'] = T_ARR_END; cfg->ch2token[','] = T_COMMA; cfg->ch2token[':'] = T_COLON; cfg->ch2token['\0'] = T_END; cfg->ch2token[' '] = T_WHITESPACE; cfg->ch2token['\t'] = T_WHITESPACE; cfg->ch2token['\n'] = T_WHITESPACE; cfg->ch2token['\r'] = T_WHITESPACE; /* Update characters that require further processing */ cfg->ch2token['f'] = T_UNKNOWN; /* false? */ cfg->ch2token['i'] = T_UNKNOWN; /* inf, ininity? */ cfg->ch2token['I'] = T_UNKNOWN; cfg->ch2token['n'] = T_UNKNOWN; /* null, nan? */ cfg->ch2token['N'] = T_UNKNOWN; cfg->ch2token['t'] = T_UNKNOWN; /* true? */ cfg->ch2token['"'] = T_UNKNOWN; /* string? */ cfg->ch2token['+'] = T_UNKNOWN; /* number? */ cfg->ch2token['-'] = T_UNKNOWN; for (i = 0; i < 10; i++) cfg->ch2token['0' + i] = T_UNKNOWN; /* Lookup table for parsing escape characters */ for (i = 0; i < 256; i++) cfg->escape2char[i] = 0; /* String error */ cfg->escape2char['"'] = '"'; cfg->escape2char['\\'] = '\\'; cfg->escape2char['/'] = '/'; cfg->escape2char['b'] = '\b'; cfg->escape2char['t'] = '\t'; cfg->escape2char['n'] = '\n'; cfg->escape2char['f'] = '\f'; cfg->escape2char['r'] = '\r'; cfg->escape2char['u'] = 'u'; /* Unicode parsing required */ }
void lua_core::def(lua_CFunction f, const char *name){ lua_pushcfunction(state, f); lua_setglobal(state, name); }
static int image_index(lua_State *L) { const char* membername = lua_tostring(L, -1); const dt_image_t * my_image=checkreadimage(L,-2); if(luaA_struct_has_member_name(L,dt_image_t,membername)) { const int result = luaA_struct_push_member_name(L, dt_image_t, my_image, membername); releasereadimage(L,my_image); return result; } switch(luaL_checkoption(L,-1,NULL,image_fields_name)) { case PATH: { sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select folder from images, film_rolls where " "images.film_id = film_rolls.id and images.id = ?1", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); if(sqlite3_step(stmt) == SQLITE_ROW) { lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0)); } else { sqlite3_finalize(stmt); releasereadimage(L,my_image); return luaL_error(L,"should never happen"); } sqlite3_finalize(stmt); break; } case DUP_INDEX: { // get duplicate suffix int version = 0; sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select count(id) from images where filename in " "(select filename from images where id = ?1) and film_id in " "(select film_id from images where id = ?1) and id < ?1", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); if(sqlite3_step(stmt) == SQLITE_ROW) version = sqlite3_column_int(stmt, 0); sqlite3_finalize(stmt); lua_pushinteger(L,version); break; } case IS_LDR: lua_pushboolean(L,dt_image_is_ldr(my_image)); break; case IS_HDR: lua_pushboolean(L,dt_image_is_hdr(my_image)); break; case IS_RAW: lua_pushboolean(L,dt_image_is_raw(my_image)); break; case RATING: { int score = my_image->flags & 0x7; if(score >6) score=5; if(score ==6) score=-1; lua_pushinteger(L,score); break; } case ID: lua_pushinteger(L,my_image->id); break; case FILM: luaA_push(L,dt_lua_film_t,&my_image->film_id); break; case CREATOR: { sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_CREATOR); if(sqlite3_step(stmt) != SQLITE_ROW) { lua_pushstring(L,""); } else { lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0)); } sqlite3_finalize(stmt); break; } case PUBLISHER: { sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_PUBLISHER); if(sqlite3_step(stmt) != SQLITE_ROW) { lua_pushstring(L,""); } else { lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0)); } sqlite3_finalize(stmt); break; } case TITLE: { sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_TITLE); if(sqlite3_step(stmt) != SQLITE_ROW) { lua_pushstring(L,""); } else { lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0)); } sqlite3_finalize(stmt); break; } case DESCRIPTION: { sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_DESCRIPTION); if(sqlite3_step(stmt) != SQLITE_ROW) { lua_pushstring(L,""); } else { lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0)); } sqlite3_finalize(stmt); break; } case RIGHTS: { sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),"select value from meta_data where id = ?1 and key = ?2", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, my_image->id); DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, DT_METADATA_XMP_DC_RIGHTS); if(sqlite3_step(stmt) != SQLITE_ROW) { lua_pushstring(L,""); } else { lua_pushstring(L,(char *)sqlite3_column_text(stmt, 0)); } sqlite3_finalize(stmt); break; } case GROUP_LEADER: { luaA_push(L,dt_lua_image_t,&(my_image->group_id)); break; } case APPLY_STYLE: { lua_pushcfunction(L,dt_lua_style_apply); break; } case CREATE_STYLE: { lua_pushcfunction(L,dt_lua_style_create_from_image); break; } default: releasereadimage(L,my_image); return luaL_error(L,"should never happen %s",lua_tostring(L,-1)); } releasereadimage(L,my_image); return 1; }
int dt_lua_init_tags(lua_State *L) { dt_lua_init_int_type(L, dt_lua_tag_t); lua_pushcfunction(L, tag_length); lua_pushcfunction(L, tag_index); dt_lua_type_register_number_const(L, dt_lua_tag_t); lua_pushcfunction(L, tag_name); dt_lua_type_register_const(L, dt_lua_tag_t, "name"); lua_pushcfunction(L, tag_delete); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const(L, dt_lua_tag_t, "delete"); lua_pushcfunction(L, dt_lua_tag_attach); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const(L, dt_lua_tag_t, "attach"); lua_pushcfunction(L, dt_lua_tag_detach); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const(L, dt_lua_tag_t, "detach"); lua_pushcfunction(L, tag_tostring); dt_lua_type_setmetafield(L,dt_lua_tag_t,"__tostring"); /* tags */ dt_lua_push_darktable_lib(L); luaA_Type type_id = dt_lua_init_singleton(L, "tag_table", NULL); lua_setfield(L, -2, "tags"); lua_pop(L, 1); lua_pushcfunction(L, tag_lib_length); lua_pushcfunction(L, tag_lib_index); dt_lua_type_register_number_const_type(L, type_id); lua_pushcfunction(L, tag_lib_create); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const_type(L, type_id, "create"); lua_pushcfunction(L, tag_lib_find); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const_type(L, type_id, "find"); lua_pushcfunction(L, tag_delete); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const_type(L, type_id, "delete"); lua_pushcfunction(L, dt_lua_tag_attach); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const_type(L, type_id, "attach"); lua_pushcfunction(L, dt_lua_tag_detach); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const_type(L, type_id, "detach"); lua_pushcfunction(L, dt_lua_tag_get_attached); lua_pushcclosure(L, dt_lua_type_member_common, 1); dt_lua_type_register_const_type(L, type_id, "get_tags"); return 0; }
/** * @param: the table (at stack 1) * @param: the cycle table (at stack 2) * @return: the table string */ static int l_pretty_repr_table(lua_State *L) { lua_pushvalue(L, 1); /* duplicate the table in stack 3 */ lua_rawget(L, 2); /* get from the cycle table 2 */ if (lua_isnil(L, -1)) { lua_pop(L, 1); /* save the table item into cycle table */ lua_pushfstring(L, "table_(%p)", lua_topointer(L, 1)); /* put in stack 3 */ lua_pushvalue(L, 1); /* push key in stack 4 */ lua_pushvalue(L, 3); /* push value in stack 5 */ lua_rawset(L, 2); /* adjust the cycle table and pop 4,5 */ /* save the level into cycle table */ lua_pushliteral(L, "level"); lua_rawget(L, 2); /* get the level into stack 4 */ int level = 1; if (!lua_isnil(L, -1)) { level += lua_tointeger(L, -1); } lua_pushliteral(L, "level"); lua_pushinteger(L, level); lua_rawset(L, 2); /* save the level and pop 5,6 */ /* luaL_Buffer START */ luaL_Buffer b; luaL_buffinit(L, &b); if (1) { luaL_addlstring(&b, "{\n", 2); } else { /* optional: suffix a pointer behind */ lua_pushfstring(L, "{ -- %p\n", lua_topointer(L, 1)); luaL_addvalue(&b); /* add the pointer value into buffer */ } /* iterate the table */ for (lua_pushnil(L); lua_next(L, 1) != 0; lua_pushvalue(L, 4)) { lua_replace(L, 3); /* backups the value in stack 3 */ lua_replace(L, 4); /* backups the key in stack 4 */ for (int i=level; i--; ) luaL_addchar(&b, '\t'); /* repr the key */ lua_pushcfunction(L, l_repr_key); lua_pushvalue(L, 4); lua_pushvalue(L, 2); lua_call(L, 2, 1); luaL_addvalue(&b); /* add the return value into buffer */ luaL_addlstring(&b, " = ", 3); /* repr the value */ lua_pushcfunction(L, l_pretty_repr_slice); lua_pushvalue(L, 3); lua_pushvalue(L, 2); lua_call(L, 2, 1); luaL_addvalue(&b); /* add the return value into buffer */ luaL_addlstring(&b, ",\n", 2); } --level; /* decrease level when table finished */ for (int i=level; i--; ) luaL_addchar(&b, '\t'); luaL_addchar(&b, '}'); luaL_pushresult(&b); /* luaL_Buffer END */ lua_pushliteral(L, "level"); lua_pushinteger(L, level); lua_rawset(L, 2); /* save the level and pop */ } else { /* just use the return value of gettable as the result */ } return 1; }
/* Map function * It assigns a function into the current module (or class) */ TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func) { lua_pushstring(L,name); lua_pushcfunction(L,func); lua_rawset(L,-3); }
static void registerCFunction(lua_State* L, const char* name, lua_CFunction function) { lua_pushcfunction(L, function); lua_setglobal(L, name); }
static int luaopen_hexchat(lua_State *L) { lua_newtable(L); luaL_setfuncs(L, api_hexchat, 0); lua_pushinteger(L, HEXCHAT_PRI_HIGHEST); lua_setfield(L, -2, "PRI_HIGHEST"); lua_pushinteger(L, HEXCHAT_PRI_HIGH); lua_setfield(L, -2, "PRI_HIGH"); lua_pushinteger(L, HEXCHAT_PRI_NORM); lua_setfield(L, -2, "PRI_NORM"); lua_pushinteger(L, HEXCHAT_PRI_LOW); lua_setfield(L, -2, "PRI_LOW"); lua_pushinteger(L, HEXCHAT_PRI_LOWEST); lua_setfield(L, -2, "PRI_LOWEST"); lua_pushinteger(L, HEXCHAT_EAT_NONE); lua_setfield(L, -2, "EAT_NONE"); lua_pushinteger(L, HEXCHAT_EAT_HEXCHAT); lua_setfield(L, -2, "EAT_HEXCHAT"); lua_pushinteger(L, HEXCHAT_EAT_PLUGIN); lua_setfield(L, -2, "EAT_PLUGIN"); lua_pushinteger(L, HEXCHAT_EAT_ALL); lua_setfield(L, -2, "EAT_ALL"); lua_newtable(L); lua_newtable(L); luaL_setfuncs(L, api_hexchat_prefs_meta, 0); lua_setmetatable(L, -2); lua_setfield(L, -2, "prefs"); lua_newtable(L); lua_newtable(L); luaL_setfuncs(L, api_hexchat_props_meta, 0); lua_setmetatable(L, -2); lua_setfield(L, -2, "props"); lua_newtable(L); lua_newtable(L); luaL_setfuncs(L, api_hexchat_pluginprefs_meta, 0); lua_setmetatable(L, -2); lua_setfield(L, -2, "pluginprefs"); luaL_newmetatable(L, "hook"); lua_newtable(L); luaL_setfuncs(L, api_hook_meta_index, 0); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, "context"); lua_newtable(L); lua_pushcfunction(L, api_hexchat_set_context); lua_setfield(L, -2, "set"); wrap_context(L, "find_context", api_hexchat_find_context); wrap_context(L, "print", api_hexchat_print); wrap_context(L, "emit_print", api_hexchat_emit_print); wrap_context(L, "emit_print_attrs", api_hexchat_emit_print_attrs); wrap_context(L, "command", api_hexchat_command); wrap_context(L, "nickcmp", api_hexchat_nickcmp); wrap_context(L, "get_info", api_hexchat_get_info); wrap_context(L, "iterate", api_hexchat_iterate); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, api_hexchat_context_meta_eq); lua_setfield(L, -2, "__eq"); lua_pop(L, 1); luaL_newmetatable(L, "attrs"); luaL_setfuncs(L, api_attrs_meta, 0); lua_pop(L, 1); luaL_newmetatable(L, "list"); luaL_setfuncs(L, api_list_meta, 0); lua_pop(L, 1); return 1; }
static inline void wrap_context(lua_State *L, char const *field, lua_CFunction func) { lua_pushcfunction(L, func); lua_pushcclosure(L, wrap_context_closure, 1); lua_setfield(L, -2, field); }
/** * @brief Mods player using the power of Lua. */ static void faction_modPlayerLua( int f, double mod, const char *source, int secondary ) { Faction *faction; lua_State *L; int errf; double old, delta; HookParam hparam[3]; faction = &faction_stack[f]; /* Make sure it's not static. */ if (faction_isFlag(faction, FACTION_STATIC)) return; L = faction->state; old = faction->player; if (L == NULL) faction->player += mod; else { #if DEBUGGING lua_pushcfunction(L, nlua_errTrace); errf = -6; #else /* DEBUGGING */ errf = 0; #endif /* DEBUGGING */ /* Set up the function: * faction_hit( current, amount, source, secondary ) */ lua_getglobal( L, "faction_hit" ); lua_pushnumber( L, faction->player ); lua_pushnumber( L, mod ); lua_pushstring( L, source ); lua_pushboolean( L, secondary ); /* Call function. */ if (lua_pcall( L, 4, 1, errf )) { /* An error occurred. */ WARN("Faction '%s': %s", faction->name, lua_tostring(L,-1)); #if DEBUGGING lua_pop( L, 2 ); #else /* DEBUGGING */ lua_pop( L, 1 ); #endif /* DEBUGGING */ return; } /* Parse return. */ if (!lua_isnumber( L, -1 )) WARN( "Lua script for faction '%s' did not return a number from 'faction_hit(...)'.", faction->name ); else faction->player = lua_tonumber( L, -1 ); #if DEBUGGING lua_pop( L, 2 ); #else /* DEBUGGING */ lua_pop( L, 1 ); #endif /* DEBUGGING */ } /* Sanitize just in case. */ faction_sanitizePlayer( faction ); /* Run hook if necessary. */ delta = faction->player - old; if (fabs(delta) > 1e-10) { hparam[0].type = HOOK_PARAM_FACTION; hparam[0].u.lf = f; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = delta; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "standing", hparam ); /* Tell space the faction changed. */ space_factionChange(); } }
/*@-globs -mods@*/ /* XXX hide rpmGlobalMacroContext mods for now. */ rpmlua rpmluaNew(void) { rpmlua lua = rpmluaGetPool(_rpmluaPool); lua_State *L = luaL_newstate(); /*@-readonlytrans -nullassign @*/ /*@observer@*/ /*@unchecked@*/ static const luaL_Reg lualibs[] = { #ifdef WITH_SYCK {"lsyck", luaopen_syck}, #endif /* WITH_SYCK */ /* local LUA libraries (RPM only) */ #ifdef WITH_LUA_INTERNAL {"posix", luaopen_posix_c}, {"rex_posix", luaopen_rex_posix}, {"rex_pcre", luaopen_rex_pcre}, {"uuid", luaopen_uuid}, #ifdef DYING /* XXX not currently internal */ {"wrs", luaopen_wrs}, #endif #ifdef USE_LUA_CRYPTO /* XXX external lua modules instead. */ {"crypto", luaopen_crypto}, {"lxp", luaopen_lxp}, #endif #ifdef USE_LUA_SOCKET /* XXX external lua modules instead. */ {"socket", luaopen_socket_core}, #endif {"local", luaopen_local}, #endif {"rpm", luaopen_rpm}, {NULL, NULL}, }; /*@=readonlytrans =nullassign @*/ /*@observer@*/ /*@unchecked@*/ const luaL_Reg *lib = lualibs; char *path_buf; char *path_next; char *path; lua->L = L; lua->pushsize = 0; lua->storeprint = 0; /* XXX TODO: use an rpmiob here. */ lua->printbufsize = 0; lua->printbufused = 0; lua->printbuf = NULL; luaL_openlibs(L); for (; lib->name; lib++) { luaL_requiref(L, lib->name, lib->func, 1); } { const char * _lua_path = rpmGetPath(rpmluaPath, NULL); if (_lua_path != NULL) { lua_pushliteral(L, "LUA_PATH"); lua_pushstring(L, _lua_path); _lua_path = _free(_lua_path); } } #if defined(LUA_GLOBALSINDEX) lua_rawset(L, LUA_GLOBALSINDEX); #else lua_pushglobaltable(L); #endif lua_pushliteral(L, "print"); lua_pushcfunction(L, rpm_print); #if defined(LUA_GLOBALSINDEX) lua_rawset(L, LUA_GLOBALSINDEX); #else lua_pushglobaltable(L); #endif rpmluaSetData(lua, "lua", lua); /* load all standard RPM Lua script files */ path_buf = xstrdup(rpmluaFiles); for (path = path_buf; path != NULL && *path != '\0'; path = path_next) { const char **av; struct stat st; int ac, i; /* locate start of next path element */ path_next = strchr(path, ':'); if (path_next != NULL && *path_next == ':') *path_next++ = '\0'; else path_next = path + strlen(path); /* glob-expand the path element */ ac = 0; av = NULL; if ((i = rpmGlob(path, &ac, &av)) != 0) continue; /* work-off each resulting file from the path element */ for (i = 0; i < ac; i++) { const char *fn = av[i]; if (fn[0] == '@' /* attention */) { fn++; #if defined(RPM_VENDOR_OPENPKG) /* stick-with-rpm-file-sanity-checking */ || \ !defined(POPT_ERROR_BADCONFIG) /* XXX POPT 1.15 retrofit */ if (!rpmSecuritySaneFile(fn)) #else if (!poptSaneFile(fn)) #endif { rpmlog(RPMLOG_WARNING, "existing RPM Lua script file \"%s\" considered INSECURE -- not loaded\n", fn); /*@innercontinue@*/ continue; } } if (Stat(fn, &st) != -1) (void)rpmluaRunScriptFile(lua, fn); av[i] = _free(av[i]); } av = _free(av); } path_buf = _free(path_buf); return ((rpmlua)rpmioLinkPoolItem((rpmioItem)lua, __FUNCTION__, __FILE__, __LINE__)); }
int script_func_exec(const char *func, const char *sig, ...) { int lev = 1; /* `func` is global function */ int err = 0; size_t tok_len = 0; const char *cur_mod = func, *mod; char module[MAX_MODULE_NAME_LENGTH]; lua_getglobal(L, "_G"); if (!lua_istable(L, -1)) { lua_pop(L, lev); LOG_ERROR("script", "%s", "Global table is NOT available."); return SCRIPT_RC_ERROR; } while ((mod = strchr(cur_mod, '.'))) { tok_len = mod - cur_mod; strncpy(module, cur_mod, tok_len); module[tok_len] = '\0'; cur_mod = mod + 1; lua_getfield(L, -1, module); ++lev; if (!lua_istable(L, -1)) { lua_pop(L, lev); LOG_ERROR("script", "`%s()` is NOT recognized as a Lua function.", func); return SCRIPT_RC_ERROR; } } lua_getfield(L, -1, cur_mod); /* function */ if (!lua_isfunction(L, -1)) { lua_pop(L, lev + 1); LOG_ERROR("script", "`%s()` is NOT recognized as a Lua function.", func); return SCRIPT_RC_ERROR; } va_list args; va_start(args, sig); int argc = 0; /* parse input arguments */ for (; sig && *sig && *sig != '>'; ++sig, ++argc) { switch (*sig) { case 'i': /* integer */ lua_pushnumber(L, va_arg(args, int)); break; case 's': /* string */ lua_pushstring(L, va_arg(args, char *)); break; case 'f': /* double */ lua_pushnumber(L, va_arg(args, double)); break; default: /* double */ LOG_ERROR("script", "invalid option (%c)", *sig); lua_pop(L, lev + argc); return SCRIPT_RC_ERROR; } } int handler = lua_gettop(L) - argc; lua_pushcfunction(L, script_traceback); lua_insert(L, handler); err = lua_pcall(L, argc, LUA_MULTRET, handler); lua_remove(L, handler); va_end(args); if (err) { return script_error(L); } lua_pop(L, lev); return SCRIPT_RC_OK; }
static void luastate_CreateGraphicsTable(lua_State* l) { lua_newtable(l); lua_pushstring(l, "getRendererName"); lua_pushcfunction(l, &luafuncs_getRendererName); lua_settable(l, -3); lua_pushstring(l, "setWindow"); lua_pushcfunction(l, &luafuncs_setWindow); lua_settable(l, -3); lua_pushstring(l, "loadImage"); lua_pushcfunction(l, &luafuncs_loadImage); lua_settable(l, -3); lua_pushstring(l, "loadImageAsync"); lua_pushcfunction(l, &luafuncs_loadImageAsync); lua_settable(l, -3); lua_pushstring(l, "getImageSize"); lua_pushcfunction(l, &luafuncs_getImageSize); lua_settable(l, -3); lua_pushstring(l, "getWindowSize"); lua_pushcfunction(l, &luafuncs_getWindowSize); lua_settable(l, -3); lua_pushstring(l, "isImageLoaded"); lua_pushcfunction(l, &luafuncs_isImageLoaded); lua_settable(l, -3); lua_pushstring(l, "drawImage"); lua_pushcfunction(l, &luafuncs_drawImage); lua_settable(l, -3); lua_pushstring(l, "drawRectangle"); lua_pushcfunction(l, &luafuncs_drawRectangle); lua_settable(l, -3); lua_pushstring(l, "getDisplayModes"); lua_pushcfunction(l, &luafuncs_getDisplayModes); lua_settable(l, -3); lua_pushstring(l, "getDesktopDisplayMode"); lua_pushcfunction(l, &luafuncs_getDesktopDisplayMode); lua_settable(l, -3); lua_pushstring(l, "unloadImage"); lua_pushcfunction(l, &luafuncs_unloadImage); lua_settable(l, -3); }
static lua_State* luastate_New(void) { lua_State* l = luaL_newstate(); lua_gc(l, LUA_GCSETPAUSE, 110); lua_gc(l, LUA_GCSETSTEPMUL, 300); // standard libs luaL_openlibs(l); luastate_RememberTracebackFunc(l); luastate_VoidDebug(l); luastate_AddBlitwizFuncs(l); // own dofile/loadfile/print lua_pushcfunction(l, &luafuncs_loadfile); lua_setglobal(l, "loadfile"); lua_pushcfunction(l, &luafuncs_dofile); lua_setglobal(l, "dofile"); lua_pushcfunction(l, &luafuncs_print); lua_setglobal(l, "print"); // obtain the blitwiz lib lua_getglobal(l, "blitwiz"); // blitwiz.setStep: lua_pushstring(l, "setStep"); lua_pushcfunction(l, &luafuncs_setstep); lua_settable(l, -3); // blitwiz namespaces lua_pushstring(l, "graphics"); luastate_CreateGraphicsTable(l); lua_settable(l, -3); lua_pushstring(l, "net"); luastate_CreateNetTable(l); lua_settable(l, -3); /*lua_pushstring(l, "sound"); luastate_CreateSoundTable(l); lua_settable(l, -3);*/ lua_pushstring(l, "callback"); lua_newtable(l); lua_pushstring(l, "event"); lua_newtable(l); lua_settable(l, -3); lua_settable(l, -3); lua_pushstring(l, "time"); luastate_CreateTimeTable(l); lua_settable(l, -3); lua_pushstring(l, "physics"); luastate_CreatePhysicsTable(l); lua_settable(l, -3); // we still have the module "blitwiz" on the stack here lua_pop(l, 1); // obtain math table lua_getglobal(l, "math"); // math namespace extensions lua_pushstring(l, "trandom"); lua_pushcfunction(l, &luafuncs_trandom); lua_settable(l, -3); // remove math table from stack lua_pop(l, 1); // obtain os table lua_getglobal(l, "os"); // os namespace extensions lua_pushstring(l, "exit"); lua_pushcfunction(l, &luafuncs_exit); lua_settable(l, -3); lua_pushstring(l, "chdir"); lua_pushcfunction(l, &luafuncs_chdir); lua_settable(l, -3); lua_pushstring(l, "openConsole"); lua_pushcfunction(l, &luafuncs_openConsole); lua_settable(l, -3); lua_pushstring(l, "ls"); lua_pushcfunction(l, &luafuncs_ls); lua_settable(l, -3); lua_pushstring(l, "isdir"); lua_pushcfunction(l, &luafuncs_isdir); lua_settable(l, -3); lua_pushstring(l, "getcwd"); lua_pushcfunction(l, &luafuncs_getcwd); lua_settable(l, -3); lua_pushstring(l, "exists"); lua_pushcfunction(l, &luafuncs_exists); lua_settable(l, -3); lua_pushstring(l, "sysname"); lua_pushcfunction(l, &luafuncs_sysname); lua_settable(l, -3); lua_pushstring(l, "sysversion"); lua_pushcfunction(l, &luafuncs_sysversion); lua_settable(l, -3); // throw table "os" off the stack lua_pop(l, 1); // get "string" table for custom string functions lua_getglobal(l, "string"); // set custom string functions lua_pushstring(l, "starts"); lua_pushcfunction(l, &luafuncs_startswith); lua_settable(l, -3); lua_pushstring(l, "ends"); lua_pushcfunction(l, &luafuncs_endswith); lua_settable(l, -3); lua_pushstring(l, "split"); lua_pushcfunction(l, &luafuncs_split); lua_settable(l, -3); // throw table "string" off the stack lua_pop(l, 1); char vstr[512]; char is64bit[] = " (64-bit binary)"; #ifndef _64BIT strcpy(is64bit, ""); #endif snprintf(vstr, sizeof(vstr), "Blitwizard %s based on Lua 5.2%s", VERSION, is64bit); lua_pushstring(l, vstr); lua_setglobal(l, "_VERSION"); return l; }
int luaopen_GradientMapLoader(lua_State* L) { LUA_SCRIPTCLASS_REGISTER(L, GradientMapLoader_Lua); lua_pushcfunction(L, LUA_SCRIPTCLASS_NEW_FUNCTION(GradientMapLoader_Lua)); return 1; }
int main (int argc, char **argv) { int i; lua_State *L=luaL_newstate(); luaL_openlibs(L); lua_newtable(L); for (i=0; i<argc; i++) { lua_pushnumber(L, i); lua_pushstring(L, argv[i]); lua_rawset(L, -3); } lua_setglobal(L, "arg"); #ifdef _EJA_PATH #define xstr(s) str(s) #define str(s) #s lua_pushstring(L, xstr(_EJA_PATH) ); lua_setglobal(L, "_eja_path"); #endif lua_pushcfunction(L, eja_pid); lua_setglobal(L, "ejaPid"); lua_pushcfunction(L, eja_fork); lua_setglobal(L, "ejaFork"); lua_pushcfunction(L, eja_fork_clean); lua_setglobal(L, "ejaForkClean"); lua_pushcfunction(L, eja_dir_create); lua_setglobal(L, "ejaDirCreate"); lua_pushcfunction(L, eja_dir_list); lua_setglobal(L, "ejaDirList"); lua_pushcfunction(L, eja_sleep); lua_setglobal(L, "ejaSleep"); lua_pushcfunction(L, eja_file_stat); lua_setglobal(L, "ejaFileStat"); lua_pushcfunction(L, eja_kill); lua_setglobal(L, "ejaKill"); lua_pushcfunction(L, eja_ioctl); lua_setglobal(L, "ejaIoctl"); eja_socket_define(L); lua_pushcfunction(L, eja_socket_open); lua_setglobal(L, "ejaSocketOpen"); lua_pushcfunction(L, eja_socket_close); lua_setglobal(L, "ejaSocketClose"); lua_pushcfunction(L, eja_socket_connect); lua_setglobal(L, "ejaSocketConnect"); lua_pushcfunction(L, eja_socket_bind); lua_setglobal(L, "ejaSocketBind"); lua_pushcfunction(L, eja_socket_listen); lua_setglobal(L, "ejaSocketListen"); lua_pushcfunction(L, eja_socket_accept); lua_setglobal(L, "ejaSocketAccept"); lua_pushcfunction(L, eja_socket_read); lua_setglobal(L, "ejaSocketRead"); lua_pushcfunction(L, eja_socket_write); lua_setglobal(L, "ejaSocketWrite"); lua_pushcfunction(L, eja_socket_option_set); lua_setglobal(L, "ejaSocketOptionSet"); lua_pushcfunction(L, eja_socket_get_addr_info); lua_setglobal(L, "ejaSocketGetAddrInfo"); lua_pushcfunction(L, eja_socket_receive); lua_setglobal(L, "ejaSocketReceive"); lua_pushcfunction(L, eja_socket_send); lua_setglobal(L, "ejaSocketSend"); luaL_loadbuffer(L,luaBuf,sizeof(luaBuf),"ejaLua"); lua_call(L,0,0); lua_close(L); }
int luaopen_ui(lua_State *L) { static const struct luaL_Reg font_lib[] = { { NULL, NULL } }; static const struct luaL_Reg image_lib[] = { { "Draw", lua_image_draw }, { "Height", lua_image_height }, { "Width", lua_image_width }, { NULL, NULL } }; static const struct luaL_Reg ui_lib[] = { { "DrawString", lua_ui_draw_string }, { "FillRectangle", lua_ui_fill_rectangle }, { "Font", lua_ui_font }, { "Height", lua_ui_height }, { "HideProgress", lua_ui_hide_progress }, { "Image", lua_ui_image }, { "SetColor", lua_ui_set_color }, { "SetFont", lua_ui_set_font }, { "ShowProgress", lua_ui_show_progress }, { "Width", lua_ui_width }, { NULL, NULL } }; state = L; /* Create the "ui" library */ luaL_newlib(L, ui_lib); /* Add constants to the UI object */ lua_pushnumber(L, UI_TEXT_LEFT); lua_setfield(L, -2, "TEXT_LEFT"); lua_pushnumber(L, UI_TEXT_CENTER); lua_setfield(L, -2, "TEXT_CENTER"); lua_pushnumber(L, UI_TEXT_RIGHT); lua_setfield(L, -2, "TEXT_RIGHT"); /* Setup meta data for the "Font" object */ luaL_newmetatable(L, FontTypeName); luaL_newlib(L, font_lib); lua_setfield(L, -2, "__index"); /* GC callback for the "Font" object */ lua_pushstring(L, "__gc"); lua_pushcfunction(L, lua_font_gc); lua_settable(L, -3); /* Remove the Font library object from the stack */ lua_pop(L, 1); /* Setup meta data for the "Image" object */ luaL_newmetatable(L, ImageTypeName); luaL_newlib(L, image_lib); lua_setfield(L, -2, "__index"); /* GC callback for the "Image" object */ lua_pushstring(L, "__gc"); lua_pushcfunction(L, lua_image_gc); lua_settable(L, -3); /* Remove the Image library object from the stack */ lua_pop(L, 1); return 1; }
void luaopen_xml( lua_State *L ) { lua_pushcfunction( L, vlclua_xml_create ); lua_setfield( L, -2, "xml" ); }
// Curse whoever had this stupid idea. Curse whoever thought it would be a good // idea not to include an emulated lua_cpcall() even more. static int mp_cpcall (lua_State *L, lua_CFunction func, void *ud) { lua_pushcfunction(L, func); // doesn't allocate in 5.2 (but does in 5.1) lua_pushlightuserdata(L, ud); return lua_pcall(L, 1, 0, 0); }
void ngx_http_lua_inject_string_api(lua_State *L) { lua_pushcfunction(L, ngx_http_lua_ngx_escape_uri); lua_setfield(L, -2, "escape_uri"); lua_pushcfunction(L, ngx_http_lua_ngx_unescape_uri); lua_setfield(L, -2, "unescape_uri"); lua_pushcfunction(L, ngx_http_lua_ngx_encode_args); lua_setfield(L, -2, "encode_args"); lua_pushcfunction(L, ngx_http_lua_ngx_decode_args); lua_setfield(L, -2, "decode_args"); lua_pushcfunction(L, ngx_http_lua_ngx_quote_sql_str); lua_setfield(L, -2, "quote_sql_str"); lua_pushcfunction(L, ngx_http_lua_ngx_decode_base64); lua_setfield(L, -2, "decode_base64"); lua_pushcfunction(L, ngx_http_lua_ngx_encode_base64); lua_setfield(L, -2, "encode_base64"); lua_pushcfunction(L, ngx_http_lua_ngx_md5_bin); lua_setfield(L, -2, "md5_bin"); lua_pushcfunction(L, ngx_http_lua_ngx_md5); lua_setfield(L, -2, "md5"); #if (NGX_HAVE_SHA1) lua_pushcfunction(L, ngx_http_lua_ngx_sha1_bin); lua_setfield(L, -2, "sha1_bin"); #endif lua_pushcfunction(L, ngx_http_lua_ngx_crc32_short); lua_setfield(L, -2, "crc32_short"); lua_pushcfunction(L, ngx_http_lua_ngx_crc32_long); lua_setfield(L, -2, "crc32_long"); #if (NGX_OPENSSL) lua_pushcfunction(L, ngx_http_lua_ngx_hmac_sha1); lua_setfield(L, -2, "hmac_sha1"); #endif }
ngx_int_t ngx_http_lua_log_by_chunk(lua_State *L, ngx_http_request_t *r) { ngx_int_t rc; u_char *err_msg; size_t len; #if (NGX_PCRE) ngx_pool_t *old_pool; #endif /* set Lua VM panic handler */ lua_atpanic(L, ngx_http_lua_atpanic); NGX_LUA_EXCEPTION_TRY { /* initialize nginx context in Lua VM, code chunk at stack top sp = 1 */ ngx_http_lua_log_by_lua_env(L, r); #if (NGX_PCRE) /* XXX: work-around to nginx regex subsystem */ old_pool = ngx_http_lua_pcre_malloc_init(r->pool); #endif lua_pushcfunction(L, ngx_http_lua_traceback); lua_insert(L, 1); /* put it under chunk and args */ /* protected call user code */ rc = lua_pcall(L, 0, 1, 1); lua_remove(L, 1); /* remove traceback function */ #if (NGX_PCRE) /* XXX: work-around to nginx regex subsystem */ ngx_http_lua_pcre_malloc_done(old_pool); #endif if (rc != 0) { /* error occured when running loaded code */ err_msg = (u_char *) lua_tolstring(L, -1, &len); if (err_msg == NULL) { err_msg = (u_char *) "unknown reason"; len = sizeof("unknown reason") - 1; } ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to run log_by_lua*: %*s", len, err_msg); lua_settop(L, 0); /* clear remaining elems on stack */ return NGX_ERROR; } } NGX_LUA_EXCEPTION_CATCH { dd("nginx execution restored"); return NGX_ERROR; } /* clear Lua stack */ lua_settop(L, 0); return NGX_OK; }
void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction) { lua_pushcfunction(Game::getInstance()->getScriptController()->_lua, cppFunction); lua_setglobal(Game::getInstance()->getScriptController()->_lua, luaFunction); }
static void auxopen (lua_State *L, const char *name, lua_CFunction f, lua_CFunction u) { lua_pushcfunction(L, u); lua_pushcclosure(L, f, 1); lua_setfield(L, -2, name); }
GtR* gtr_new(GtError *err) { GtR *gtr; char *seedstr = NULL; int had_err = 0; #ifndef WITHOUT_CAIRO GtStr *style_file = NULL; #endif gtr = gt_calloc(1, sizeof (GtR)); if ((seedstr = getenv("GT_SEED"))) { if (gt_parse_uint(>r->seed, seedstr) != 0) { gt_error_set(err, "invalid seed in GT_SEED environment variable: %s", seedstr); had_err = -1; } } else gtr->seed = 0; if (!had_err) { gtr->debugfp = gt_str_new(); gtr->testspacepeak = gt_str_new(); gtr->test_only = gt_str_new(); gtr->manoutdir = gt_str_new(); gtr->L = luaL_newstate(); if (!gtr->L) { gt_error_set(err, "out of memory (cannot create new lua state)"); had_err = -1; } } if (!had_err) { luaL_openlibs(gtr->L); /* open the standard libraries */ gt_lua_open_lib(gtr->L); /* open the GenomeTools library */ lua_pushcfunction(gtr->L, luaopen_lpeg); lua_pushstring(gtr->L, "lpeg"); lua_call(gtr->L, 1, 0); /* open LPeg library */ lua_pushcfunction(gtr->L, luaopen_md5_core); lua_pushstring(gtr->L, "md5"); lua_call(gtr->L, 1, 0); /* open MD5 library */ lua_pushcfunction(gtr->L, luaopen_lfs); lua_pushstring(gtr->L, "lfs"); lua_call(gtr->L, 1, 0); /* open Lua filesystem */ lua_pushcfunction(gtr->L, luaopen_des56); lua_pushstring(gtr->L, "des56"); lua_call(gtr->L, 1, 0); /* open DES56 library */ had_err = gt_lua_set_modules_path(gtr->L, err); } #ifndef WITHOUT_CAIRO if (!had_err) { lua_settop(gtr->L, 0); if (!(gtr->style = gt_style_new_with_state(gtr->L))) had_err = -1; } if (!had_err) { if (!(style_file = gt_get_gtdata_path(gt_error_get_progname(err), err))) had_err = -1; } if (!had_err) { gt_str_append_cstr(style_file, "/sketch/default.style"); if (gt_file_exists(gt_str_get(style_file))) { if (gt_style_load_file(gtr->style, gt_str_get(style_file), err)) had_err = -1; else gt_lua_put_style_in_registry(gtr->L, gtr->style); } } gt_str_delete(style_file); #endif if (had_err) { gt_free(gtr); return NULL; } return gtr; }
void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction, lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath) { ScriptController* sc = Game::getInstance()->getScriptController(); // If the type is an inner type, get the correct parent // table on the stack before creating the table for the class. if (!scopePath.empty()) { std::string tablename = name; // Strip off the scope path part of the name. lua_getglobal(sc->_lua, scopePath[0].c_str()); std::size_t index = tablename.find(scopePath[0]); if (index != std::string::npos) tablename = tablename.substr(index + scopePath[0].size()); for (unsigned int i = 1; i < scopePath.size(); i++) { lua_pushstring(sc->_lua, scopePath[i].c_str()); lua_gettable(sc->_lua, -2); index = tablename.find(scopePath[i]); if (index != std::string::npos) tablename = tablename.substr(index + scopePath[i].size()); } lua_pushstring(sc->_lua, tablename.c_str()); lua_newtable(sc->_lua); } else { // If the type is not an inner type, set it as a global table. lua_newtable(sc->_lua); lua_pushvalue(sc->_lua, -1); lua_setglobal(sc->_lua, name); } // Create the metatable and populate it with the member functions. lua_pushliteral(sc->_lua, "__metatable"); luaL_newmetatable(sc->_lua, name); if (members) luaL_setfuncs(sc->_lua, members, 0); lua_pushstring(sc->_lua, "__index"); lua_pushvalue(sc->_lua, -2); lua_settable(sc->_lua, -3); // Add the delete function if it was specified. if (deleteFunction) { lua_pushstring(sc->_lua, "__gc"); lua_pushcfunction(sc->_lua, deleteFunction); lua_settable(sc->_lua, -3); } // Set the metatable on the main table. lua_settable(sc->_lua, -3); // Populate the main table with the static functions. if (statics) luaL_setfuncs(sc->_lua, statics, 0); // Set the new function(s) for the class. if (newFunction) { lua_pushliteral(sc->_lua, "new"); lua_pushcfunction(sc->_lua, newFunction); lua_settable(sc->_lua, -3); } // Set the table we just created within the correct parent table. if (!scopePath.empty()) { lua_settable(sc->_lua, -3); // Pop all the parent tables off the stack. int size = (int)scopePath.size(); lua_pop(sc->_lua, size); } else { // Pop the main table off the stack. lua_pop(sc->_lua, 1); } }
static int bindAll(lua_State* L) { Application* application = static_cast<Application*>(lua_touserdata(L, 1)); lua_pop(L, 1); StackChecker checker(L, "bindAll", 0); setEnvironTable(L); { lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_touches); lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_eventClosures); lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_events); luaL_newweaktable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_b2); lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_timers); } static const luaL_Reg functionList[] = { {"isInstanceOf", instance_of}, {"getClass", get_class}, {"getBaseClass", get_base}, {NULL, NULL}, }; luaL_newmetatable(L, "Object"); luaL_register(L, NULL, functionList); lua_setglobal(L, "Object"); EventBinder eventBinder(L); EventDispatcherBinder eventDispatcherBinder(L); TimerBinder timerBinder(L); MatrixBinder matrixBinder(L); SpriteBinder spriteBinder(L); TextureBaseBinder textureBaseBinder(L); TextureBinder textureBinder(L); TexturePackBinder texturePackBinder(L); BitmapDataBinder bitmapDataBinder(L); BitmapBinder bitmapBinder(L); StageBinder stageBinder(L, application); FontBaseBinder fontBaseBinder(L); FontBinder fontBinder(L); TTFontBinder ttfontBinder(L); TextFieldBinder textFieldBinder(L); #ifndef TARGET_OS_TV AccelerometerBinder accelerometerBinder(L); #endif Box2DBinder2 box2DBinder2(L); DibBinder dibBinder(L); TileMapBinder tileMapBinder(L); ApplicationBinder applicationBinder(L); ShapeBinder shapeBinder(L); MovieClipBinder movieClipBinder(L); UrlLoaderBinder urlLoaderBinder(L); #ifndef TARGET_OS_TV GeolocationBinder geolocationBinder(L); GyroscopeBinder gyroscopeBinder(L); #endif AlertDialogBinder alertDialogBinder(L); TextInputDialogBinder textInputDialogBinder(L); MeshBinder meshBinder(L); AudioBinder audioBinder(L); RenderTargetBinder renderTargetBinder(L); ShaderBinder shaderBinder(L); Path2DBinder path2DBinder(L); PluginManager& pluginManager = PluginManager::instance(); for (size_t i = 0; i < pluginManager.plugins.size(); ++i) pluginManager.plugins[i].main(L, 0); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_Event); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_EnterFrameEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_MouseEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_TouchEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_TimerEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_KeyboardEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_CompleteEvent); #include "property.c.in" #include "texturepack.c.in" #include "sprite.c.in" #include "compatibility.c.in" lua_newtable(L); lua_pushinteger(L, GINPUT_KEY_BACK); lua_setfield(L, -2, "BACK"); lua_pushinteger(L, GINPUT_KEY_SEARCH); lua_setfield(L, -2, "SEARCH"); lua_pushinteger(L, GINPUT_KEY_MENU); lua_setfield(L, -2, "MENU"); lua_pushinteger(L, GINPUT_KEY_CENTER); lua_setfield(L, -2, "CENTER"); lua_pushinteger(L, GINPUT_KEY_SELECT); lua_setfield(L, -2, "SELECT"); lua_pushinteger(L, GINPUT_KEY_START); lua_setfield(L, -2, "START"); lua_pushinteger(L, GINPUT_KEY_L1); lua_setfield(L, -2, "L1"); lua_pushinteger(L, GINPUT_KEY_R1); lua_setfield(L, -2, "R1"); lua_pushinteger(L, GINPUT_KEY_LEFT); lua_setfield(L, -2, "LEFT"); lua_pushinteger(L, GINPUT_KEY_UP); lua_setfield(L, -2, "UP"); lua_pushinteger(L, GINPUT_KEY_RIGHT); lua_setfield(L, -2, "RIGHT"); lua_pushinteger(L, GINPUT_KEY_DOWN); lua_setfield(L, -2, "DOWN"); lua_pushinteger(L, GINPUT_KEY_A); lua_setfield(L, -2, "A"); lua_pushinteger(L, GINPUT_KEY_B); lua_setfield(L, -2, "B"); lua_pushinteger(L, GINPUT_KEY_C); lua_setfield(L, -2, "C"); lua_pushinteger(L, GINPUT_KEY_D); lua_setfield(L, -2, "D"); lua_pushinteger(L, GINPUT_KEY_E); lua_setfield(L, -2, "E"); lua_pushinteger(L, GINPUT_KEY_F); lua_setfield(L, -2, "F"); lua_pushinteger(L, GINPUT_KEY_G); lua_setfield(L, -2, "G"); lua_pushinteger(L, GINPUT_KEY_H); lua_setfield(L, -2, "H"); lua_pushinteger(L, GINPUT_KEY_I); lua_setfield(L, -2, "I"); lua_pushinteger(L, GINPUT_KEY_J); lua_setfield(L, -2, "J"); lua_pushinteger(L, GINPUT_KEY_K); lua_setfield(L, -2, "K"); lua_pushinteger(L, GINPUT_KEY_L); lua_setfield(L, -2, "L"); lua_pushinteger(L, GINPUT_KEY_M); lua_setfield(L, -2, "M"); lua_pushinteger(L, GINPUT_KEY_N); lua_setfield(L, -2, "N"); lua_pushinteger(L, GINPUT_KEY_O); lua_setfield(L, -2, "O"); lua_pushinteger(L, GINPUT_KEY_P); lua_setfield(L, -2, "P"); lua_pushinteger(L, GINPUT_KEY_Q); lua_setfield(L, -2, "Q"); lua_pushinteger(L, GINPUT_KEY_R); lua_setfield(L, -2, "R"); lua_pushinteger(L, GINPUT_KEY_S); lua_setfield(L, -2, "S"); lua_pushinteger(L, GINPUT_KEY_T); lua_setfield(L, -2, "T"); lua_pushinteger(L, GINPUT_KEY_U); lua_setfield(L, -2, "U"); lua_pushinteger(L, GINPUT_KEY_V); lua_setfield(L, -2, "V"); lua_pushinteger(L, GINPUT_KEY_W); lua_setfield(L, -2, "W"); lua_pushinteger(L, GINPUT_KEY_X); lua_setfield(L, -2, "X"); lua_pushinteger(L, GINPUT_KEY_Y); lua_setfield(L, -2, "Y"); lua_pushinteger(L, GINPUT_KEY_Z); lua_setfield(L, -2, "Z"); lua_pushinteger(L, GINPUT_KEY_0); lua_setfield(L, -2, "NUM_0"); lua_pushinteger(L, GINPUT_KEY_1); lua_setfield(L, -2, "NUM_1"); lua_pushinteger(L, GINPUT_KEY_2); lua_setfield(L, -2, "NUM_2"); lua_pushinteger(L, GINPUT_KEY_3); lua_setfield(L, -2, "NUM_3"); lua_pushinteger(L, GINPUT_KEY_4); lua_setfield(L, -2, "NUM_4"); lua_pushinteger(L, GINPUT_KEY_5); lua_setfield(L, -2, "NUM_5"); lua_pushinteger(L, GINPUT_KEY_6); lua_setfield(L, -2, "NUM_6"); lua_pushinteger(L, GINPUT_KEY_7); lua_setfield(L, -2, "NUM_7"); lua_pushinteger(L, GINPUT_KEY_8); lua_setfield(L, -2, "NUM_8"); lua_pushinteger(L, GINPUT_KEY_9); lua_setfield(L, -2, "NUM_9"); lua_pushinteger(L, GINPUT_KEY_SHIFT); lua_setfield(L, -2, "SHIFT"); lua_pushinteger(L, GINPUT_KEY_SPACE); lua_setfield(L, -2, "SPACE"); lua_pushinteger(L, GINPUT_KEY_BACKSPACE); lua_setfield(L, -2, "BACKSPACE"); lua_pushinteger(L, GINPUT_KEY_CTRL); lua_setfield(L, -2, "CTRL"); lua_pushinteger(L, GINPUT_KEY_ALT); lua_setfield(L, -2, "ALT"); lua_pushinteger(L, GINPUT_KEY_ESC); lua_setfield(L, -2, "ESC"); lua_pushinteger(L, GINPUT_KEY_TAB); lua_setfield(L, -2, "TAB"); lua_pushinteger(L, GINPUT_NO_BUTTON); lua_setfield(L, -2, "MOUSE_NONE"); lua_pushinteger(L, GINPUT_LEFT_BUTTON); lua_setfield(L, -2, "MOUSE_LEFT"); lua_pushinteger(L, GINPUT_RIGHT_BUTTON); lua_setfield(L, -2, "MOUSE_RIGHT"); lua_pushinteger(L, GINPUT_MIDDLE_BUTTON); lua_setfield(L, -2, "MOUSE_MIDDLE"); lua_setglobal(L, "KeyCode"); // correct clock function which is wrong in iphone lua_getglobal(L, "os"); lua_pushcfunction(L, os_timer); lua_setfield(L, -2, "timer"); lua_pop(L, 1); // register collectgarbagelater // lua_pushcfunction(L, ::collectgarbagelater); // lua_setglobal(L, "collectgarbagelater"); registerModules(L); return 0; }
static void metatable(lua_State * L) { lua_pushcfunction(L, PRIMER_ADAPT(&impl_token_index)); lua_setfield(L, -2, "__index"); lua_pushboolean(L, true); lua_setfield(L, -2, "__persist"); }
static void newfenv (lua_State *L, lua_CFunction cls) { lua_createtable(L, 0, 1); lua_pushcfunction(L, cls); lua_setfield(L, -2, "__close"); }
int luastate_CallFunctionInMainstate(const char* function, int args, int recursivetables, int allownil, char** error, int* functiondidnotexist) { // push error function lua_pushcfunction(scriptstate, &gettraceback); if (args > 0) { lua_insert(scriptstate, -(args+1)); } // look up table components of our function name (e.g. namespace.func()) int tablerecursion = 0; while (recursivetables && tablerecursion < 5) { unsigned int r = 0; int recursed = 0; while (r < strlen(function)) { if (function[r] == '.') { recursed = 1; // extract the component char* fp = malloc(r+1); if (!fp) { *error = NULL; // clean up stack again: lua_pop(scriptstate, 1); // error func lua_pop(scriptstate, args); if (recursivetables > 0) { // clean up recursive table left on stack lua_pop(scriptstate, 1); } return 0; } memcpy(fp, function, r); fp[r] = 0; lua_pushstring(scriptstate, fp); function += r+1; free(fp); // lookup if (tablerecursion == 0) { // lookup on global table lua_getglobal(scriptstate, lua_tostring(scriptstate, -1)); lua_insert(scriptstate, -2); lua_pop(scriptstate, 1); }else{ // lookup nested on previous table lua_gettable(scriptstate, -2); // dispose of previous table lua_insert(scriptstate, -2); lua_pop(scriptstate, 1); } break; } r++; } if (recursed) { tablerecursion++; }else{ break; } } // lookup function normally if there was no recursion lookup: if (tablerecursion <= 0) { lua_getglobal(scriptstate, function); }else{ // get the function from our recursive lookup lua_pushstring(scriptstate, function); lua_gettable(scriptstate, -2); // wipe out the table we got it from lua_insert(scriptstate,-2); lua_pop(scriptstate, 1); } // quit sanely if function is nil and we allowed this if (allownil && lua_type(scriptstate, -1) == LUA_TNIL) { // clean up stack again: lua_pop(scriptstate, 1); // error func lua_pop(scriptstate, args); if (recursivetables > 0) { // clean up recursive origin table left on stack lua_pop(scriptstate, 1); } // give back the info that it did not exist: if (functiondidnotexist) { *functiondidnotexist = 1; } return 1; } // function needs to be first, then arguments. -> correct order if (args > 0) { lua_insert(scriptstate, -(args+1)); } int previoustop = lua_gettop(scriptstate)-(args+2); // 2 = 1 (error func) + 1 (called func) // call function int i = lua_pcall(scriptstate, args, 0, -(args+2)); // process errors int returnvalue = 1; if (i != 0) { *error = NULL; if (i == LUA_ERRRUN || i == LUA_ERRERR) { const char* e = lua_tostring(scriptstate, -1); *error = NULL; if (e) { *error = strdup(e); } }else{ if (i == LUA_ERRMEM) { *error = strdup("Out of memory"); }else{ *error = strdup("Unknown error"); } } returnvalue = 0; } // clean up stack if (lua_gettop(scriptstate) > previoustop) { lua_pop(scriptstate, lua_gettop(scriptstate) - previoustop); } return returnvalue; }
static void luv_req_init(lua_State* L) { luaL_newmetatable (L, "uv_req"); lua_pushcfunction(L, luv_req_tostring); lua_setfield(L, -2, "__tostring"); lua_pop(L, 1); }