static int lua_carray_len(lua_State *L) { structCArray *p = lua_checkcarray(L, 1); lua_pushinteger(L, p->size); return 1; }
static int os_execute (lua_State *L) { lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); return 1; }
void push_stat_field(lua_State *L, apr_finfo_t *info, apr_int32_t which, const char *path) { switch (which) { /* user/group name */ case APR_FINFO_USER: if (!push_username(L, info->pool, info->user)) lua_pushnil(L); break; case APR_FINFO_GROUP: push_groupname(L, info->pool, info->group); break; /* dates */ case APR_FINFO_CTIME: time_push(L, info->ctime); break; case APR_FINFO_MTIME: time_push(L, info->mtime); break; case APR_FINFO_ATIME: time_push(L, info->atime); break; /* numbers */ case APR_FINFO_SIZE: lua_pushinteger(L, (lua_Integer) info->size); break; case APR_FINFO_CSIZE: lua_pushinteger(L, (lua_Integer) info->csize); break; case APR_FINFO_INODE: lua_pushinteger(L, (lua_Integer) info->inode); break; case APR_FINFO_NLINK: lua_pushinteger(L, (lua_Integer) info->nlink); break; case APR_FINFO_DEV: lua_pushinteger(L, (lua_Integer) info->device); break; /* file path / name */ case APR_FINFO_PATH: if (path && 0 != strcmp(path, ".")) { char *filepath; apr_status_t status; status = apr_filepath_merge(&filepath, path, info->name, 0, info->pool); if (APR_SUCCESS == status) { lua_pushstring(L, filepath); break; } } /* fall through */ case APR_FINFO_NAME: lua_pushstring(L, info->name); break; /* type */ case APR_FINFO_TYPE: switch (info->filetype) { case APR_LNK: lua_pushliteral(L, "link"); break; case APR_REG: lua_pushliteral(L, "file"); break; case APR_PIPE: lua_pushliteral(L, "pipe"); break; case APR_SOCK: lua_pushliteral(L, "socket"); break; case APR_DIR: lua_pushliteral(L, "directory"); break; case APR_BLK: lua_pushliteral(L, "block device"); break; case APR_CHR: lua_pushliteral(L, "character device"); break; default: lua_pushliteral(L, "unknown"); break; } break; /* protection tables */ case APR_FINFO_UPROT | APR_FINFO_GPROT | APR_FINFO_WPROT: push_protection(L, info->protection); break; /* Error in Lua/APR :( */ default: assert(0); lua_pushnil(L); } }
static int luv_constants(lua_State* L) { lua_newtable(L); // File open bitwise flags O_* #ifdef O_RDONLY lua_pushinteger(L, O_RDONLY); lua_setfield(L, -2, "O_RDONLY"); #endif #ifdef O_WRONLY lua_pushinteger(L, O_WRONLY); lua_setfield(L, -2, "O_WRONLY"); #endif #ifdef O_RDWR lua_pushinteger(L, O_RDWR); lua_setfield(L, -2, "O_RDWR"); #endif #ifdef O_APPEND lua_pushinteger(L, O_APPEND); lua_setfield(L, -2, "O_APPEND"); #endif #ifdef O_CREAT lua_pushinteger(L, O_CREAT); lua_setfield(L, -2, "O_CREAT"); #endif #ifdef O_DSYNC lua_pushinteger(L, O_DSYNC); lua_setfield(L, -2, "O_DSYNC"); #endif #ifdef O_EXCL lua_pushinteger(L, O_EXCL); lua_setfield(L, -2, "O_EXCL"); #endif #ifdef O_EXLOCK lua_pushinteger(L, O_EXLOCK); lua_setfield(L, -2, "O_EXLOCK"); #endif #ifdef O_NOCTTY lua_pushinteger(L, O_NOCTTY); lua_setfield(L, -2, "O_NOCTTY"); #endif #ifdef O_NONBLOCK lua_pushinteger(L, O_NONBLOCK); lua_setfield(L, -2, "O_NONBLOCK"); #endif #ifdef O_RSYNC lua_pushinteger(L, O_RSYNC); lua_setfield(L, -2, "O_RSYNC"); #endif #ifdef O_SYNC lua_pushinteger(L, O_SYNC); lua_setfield(L, -2, "O_SYNC"); #endif #ifdef O_TRUNC lua_pushinteger(L, O_TRUNC); lua_setfield(L, -2, "O_TRUNC"); #endif // Socket types SOCK_* #ifdef SOCK_STREAM lua_pushinteger(L, SOCK_STREAM); lua_setfield(L, -2, "SOCK_STREAM"); #endif #ifdef SOCK_DGRAM lua_pushinteger(L, SOCK_DGRAM); lua_setfield(L, -2, "SOCK_DGRAM"); #endif #ifdef SOCK_SEQPACKET lua_pushinteger(L, SOCK_SEQPACKET); lua_setfield(L, -2, "SOCK_SEQPACKET"); #endif #ifdef SOCK_RAW lua_pushinteger(L, SOCK_RAW); lua_setfield(L, -2, "SOCK_RAW"); #endif #ifdef SOCK_RDM lua_pushinteger(L, SOCK_RDM); lua_setfield(L, -2, "SOCK_RDM"); #endif // AF_* #ifdef AF_UNIX lua_pushinteger(L, AF_UNIX); lua_setfield(L, -2, "AF_UNIX"); #endif #ifdef AF_INET lua_pushinteger(L, AF_INET); lua_setfield(L, -2, "AF_INET"); #endif #ifdef AF_INET6 lua_pushinteger(L, AF_INET6); lua_setfield(L, -2, "AF_INET6"); #endif #ifdef AF_IPX lua_pushinteger(L, AF_IPX); lua_setfield(L, -2, "AF_IPX"); #endif #ifdef AF_NETLINK lua_pushinteger(L, AF_NETLINK); lua_setfield(L, -2, "AF_NETLINK"); #endif #ifdef AF_X25 lua_pushinteger(L, AF_X25); lua_setfield(L, -2, "AF_X25"); #endif #ifdef AF_AX25 lua_pushinteger(L, AF_AX25); lua_setfield(L, -2, "AF_AX25"); #endif #ifdef AF_ATMPVC lua_pushinteger(L, AF_ATMPVC); lua_setfield(L, -2, "AF_ATMPVC"); #endif #ifdef AF_APPLETALK lua_pushinteger(L, AF_APPLETALK); lua_setfield(L, -2, "AF_APPLETALK"); #endif #ifdef AF_PACKET lua_pushinteger(L, AF_PACKET); lua_setfield(L, -2, "AF_PACKET"); #endif // AI_* #ifdef AI_ADDRCONFIG lua_pushinteger(L, AI_ADDRCONFIG); lua_setfield(L, -2, "AI_ADDRCONFIG"); #endif #ifdef AI_V4MAPPED lua_pushinteger(L, AI_V4MAPPED); lua_setfield(L, -2, "AI_V4MAPPED"); #endif #ifdef AI_ALL lua_pushinteger(L, AI_ALL); lua_setfield(L, -2, "AI_ALL"); #endif #ifdef AI_NUMERICHOST lua_pushinteger(L, AI_NUMERICHOST); lua_setfield(L, -2, "AI_NUMERICHOST"); #endif #ifdef AI_PASSIVE lua_pushinteger(L, AI_PASSIVE); lua_setfield(L, -2, "AI_PASSIVE"); #endif #ifdef AI_NUMERICSERV lua_pushinteger(L, AI_NUMERICSERV); lua_setfield(L, -2, "AI_NUMERICSERV"); #endif // Signals #ifdef SIGHUP lua_pushinteger(L, SIGHUP); lua_setfield(L, -2, "SIGHUP"); #endif #ifdef SIGINT lua_pushinteger(L, SIGINT); lua_setfield(L, -2, "SIGINT"); #endif #ifdef SIGQUIT lua_pushinteger(L, SIGQUIT); lua_setfield(L, -2, "SIGQUIT"); #endif #ifdef SIGILL lua_pushinteger(L, SIGILL); lua_setfield(L, -2, "SIGILL"); #endif #ifdef SIGTRAP lua_pushinteger(L, SIGTRAP); lua_setfield(L, -2, "SIGTRAP"); #endif #ifdef SIGABRT lua_pushinteger(L, SIGABRT); lua_setfield(L, -2, "SIGABRT"); #endif #ifdef SIGIOT lua_pushinteger(L, SIGIOT); lua_setfield(L, -2, "SIGIOT"); #endif #ifdef SIGBUS lua_pushinteger(L, SIGBUS); lua_setfield(L, -2, "SIGBUS"); #endif #ifdef SIGFPE lua_pushinteger(L, SIGFPE); lua_setfield(L, -2, "SIGFPE"); #endif #ifdef SIGKILL lua_pushinteger(L, SIGKILL); lua_setfield(L, -2, "SIGKILL"); #endif #ifdef SIGUSR1 lua_pushinteger(L, SIGUSR1); lua_setfield(L, -2, "SIGUSR1"); #endif #ifdef SIGSEGV lua_pushinteger(L, SIGSEGV); lua_setfield(L, -2, "SIGSEGV"); #endif #ifdef SIGUSR2 lua_pushinteger(L, SIGUSR2); lua_setfield(L, -2, "SIGUSR2"); #endif #ifdef SIGPIPE lua_pushinteger(L, SIGPIPE); lua_setfield(L, -2, "SIGPIPE"); #endif #ifdef SIGALRM lua_pushinteger(L, SIGALRM); lua_setfield(L, -2, "SIGALRM"); #endif #ifdef SIGTERM lua_pushinteger(L, SIGTERM); lua_setfield(L, -2, "SIGTERM"); #endif #ifdef SIGCHLD lua_pushinteger(L, SIGCHLD); lua_setfield(L, -2, "SIGCHLD"); #endif #ifdef SIGSTKFLT lua_pushinteger(L, SIGSTKFLT); lua_setfield(L, -2, "SIGSTKFLT"); #endif #ifdef SIGCONT lua_pushinteger(L, SIGCONT); lua_setfield(L, -2, "SIGCONT"); #endif #ifdef SIGSTOP lua_pushinteger(L, SIGSTOP); lua_setfield(L, -2, "SIGSTOP"); #endif #ifdef SIGTSTP lua_pushinteger(L, SIGTSTP); lua_setfield(L, -2, "SIGTSTP"); #endif #ifdef SIGBREAK lua_pushinteger(L, SIGBREAK); lua_setfield(L, -2, "SIGBREAK"); #endif #ifdef SIGTTIN lua_pushinteger(L, SIGTTIN); lua_setfield(L, -2, "SIGTTIN"); #endif #ifdef SIGTTOU lua_pushinteger(L, SIGTTOU); lua_setfield(L, -2, "SIGTTOU"); #endif #ifdef SIGURG lua_pushinteger(L, SIGURG); lua_setfield(L, -2, "SIGURG"); #endif #ifdef SIGXCPU lua_pushinteger(L, SIGXCPU); lua_setfield(L, -2, "SIGXCPU"); #endif #ifdef SIGXFSZ lua_pushinteger(L, SIGXFSZ); lua_setfield(L, -2, "SIGXFSZ"); #endif #ifdef SIGVTALRM lua_pushinteger(L, SIGVTALRM); lua_setfield(L, -2, "SIGVTALRM"); #endif #ifdef SIGPROF lua_pushinteger(L, SIGPROF); lua_setfield(L, -2, "SIGPROF"); #endif #ifdef SIGWINCH lua_pushinteger(L, SIGWINCH); lua_setfield(L, -2, "SIGWINCH"); #endif #ifdef SIGIO lua_pushinteger(L, SIGIO); lua_setfield(L, -2, "SIGIO"); #endif #ifdef SIGPOLL lua_pushinteger(L, SIGPOLL); lua_setfield(L, -2, "SIGPOLL"); #endif #ifdef SIGLOST lua_pushinteger(L, SIGLOST); lua_setfield(L, -2, "SIGLOST"); #endif #ifdef SIGPWR lua_pushinteger(L, SIGPWR); lua_setfield(L, -2, "SIGPWR"); #endif #ifdef SIGSYS lua_pushinteger(L, SIGSYS); lua_setfield(L, -2, "SIGSYS"); #endif return 1; }
/* ** err = ctx:set_options(options) */ static int l_set_options(lua_State *L) { SSL_CTX_set_options(getctx(L), luaL_checkint(L, 2)); lua_pushinteger(L, 0); return 1; }
static int lua_sleep(lua_State *L) { int sec = luaL_checkint(L, 1); int ret = sleep(sec); lua_pushinteger(L, ret); return 1; }
static void LuaIO_fs_callback(uv_fs_t* req) { LuaIO_fs_req_t* fs_req = container_of(req, LuaIO_fs_req_t, req); lua_State* L = fs_req->current_thread; int result = req->result; switch (req->fs_type) { case UV_FS_ACCESS: case UV_FS_CLOSE: case UV_FS_RENAME: case UV_FS_UNLINK: case UV_FS_RMDIR: case UV_FS_MKDIR: case UV_FS_FTRUNCATE: case UV_FS_FSYNC: case UV_FS_FDATASYNC: case UV_FS_LINK: case UV_FS_SYMLINK: case UV_FS_CHMOD: case UV_FS_FCHMOD: case UV_FS_CHOWN: case UV_FS_FCHOWN: case UV_FS_UTIME: case UV_FS_FUTIME: case UV_FS_OPEN: case UV_FS_SENDFILE: lua_pushinteger(L, result); LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 1); return; case UV_FS_READ: if (result > 0) { fs_req->read_buffer->write_pos += result; } lua_pushinteger(L, result); LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 1); return; case UV_FS_WRITE: if (result < 0) { lua_pushinteger(L, result); } else { lua_pushinteger(L, fs_req->bytes); } luaL_unref(L, LUA_REGISTRYINDEX, fs_req->write_data_ref); LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 1); return; case UV_FS_STAT: case UV_FS_LSTAT: case UV_FS_FSTAT: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { LuaIO_fs_create_stat(L, &req->statbuf); lua_pushinteger(L, 0); } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; case UV_FS_MKDTEMP: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { lua_pushstring(L, req->path); lua_pushinteger(L, 0); } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; case UV_FS_READLINK: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { lua_pushstring(L, (char*)req->ptr); lua_pushinteger(L, 0); } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; case UV_FS_SCANDIR: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { int ret; lua_createtable(L, 0, 0); for (int i = 1; ; i++) { uv_dirent_t ent; ret = uv_fs_scandir_next(req, &ent); if (ret) break; lua_pushstring(L, ent.name); lua_rawseti(L, -2, i); } if (ret && ret != UV_EOF) { lua_pop(L, 1); lua_pushnil(L); lua_pushinteger(L, ret); } else { lua_pushinteger(L, 0); } } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; default: luaL_error(L, "fs_native module error: unknown fs type(%d)", req->fs_type); } }
inline int32 CLuaBattlefield::getEntrance(lua_State* L) { DSP_DEBUG_BREAK_IF(m_PLuaBattlefield == nullptr); lua_pushinteger(L, m_PLuaBattlefield->getEntrance()); return 1; }
inline int32 CLuaBattlefield::getTimeInside(lua_State* L) { DSP_DEBUG_BREAK_IF(m_PLuaBattlefield == nullptr); uint32 duration = std::chrono::duration_cast<std::chrono::seconds>(m_PLuaBattlefield->getWinTime() - m_PLuaBattlefield->getStartTime()).count(); lua_pushinteger(L, duration); return 1; }
inline int32 CLuaBattlefield::getFastestTime(lua_State* L) { DSP_DEBUG_BREAK_IF(m_PLuaBattlefield == nullptr); lua_pushinteger(L, m_PLuaBattlefield->m_FastestTime); return 1; }
inline int32 CLuaBattlefield::setAsFastest(lua_State* L) { DSP_DEBUG_BREAK_IF(m_PLuaBattlefield == nullptr); lua_pushinteger(L, 0); return 1; }
static int make_lua_call(lua_State *L, mrp_funcbridge_t *fb, int f) { #define ARG_MAX 256 int ret; int i, n, m, b, e; const char *s; char t; mrp_funcbridge_value_t args[ARG_MAX]; mrp_funcbridge_value_t *a, r; e = lua_gettop(L); f = (f < 0) ? e + f + 1 : f; b = f + 1; n = e - b + 1; switch (fb->type) { case MRP_C_FUNCTION: m = strlen(fb->c.signature); if (n >= ARG_MAX - 1 || n > m) return luaL_error(L, "too many arguments"); if (n < m) return luaL_error(L, "too few arguments"); for (i = b, s = fb->c.signature, a= args; i <= e; i++, s++, a++){ switch (*s) { case MRP_FUNCBRIDGE_STRING: a->string = luaL_checklstring(L, i, NULL); break; case MRP_FUNCBRIDGE_INTEGER: a->integer = luaL_checkinteger(L, i); break; case MRP_FUNCBRIDGE_FLOATING: a->floating = luaL_checknumber(L, i); break; case MRP_FUNCBRIDGE_OBJECT: a->pointer = mrp_lua_check_object(L, NULL, i); break; default: return luaL_error(L, "argument %d has unsupported type '%c'", (i - b) + 1, i); } } memset(a, 0, sizeof(*a)); if (!fb->c.func(L, fb->c.data, fb->c.signature, args, &t, &r)) return luaL_error(L, "c function invocation failed"); switch (t) { case MRP_FUNCBRIDGE_NO_DATA: ret = 0; break; case MRP_FUNCBRIDGE_STRING: ret = 1; lua_pushstring(L, r.string); break; case MRP_FUNCBRIDGE_INTEGER: ret = 1; lua_pushinteger(L, r.integer); break; case MRP_FUNCBRIDGE_FLOATING: ret = 1; lua_pushnumber(L, r.floating); break; default: ret = 0; lua_pushnil(L); } break; case MRP_LUA_FUNCTION: lua_rawgeti(L, f, 1); luaL_checktype(L, -1, LUA_TFUNCTION); lua_replace(L, f); lua_pcall(L, n, 1, 0); ret = 1; break; default: return luaL_error(L, "internal error"); } return ret; #undef ARG_MAX }
bool mrp_funcbridge_call_from_c(lua_State *L, mrp_funcbridge_t *fb, const char *signature, mrp_funcbridge_value_t *args, char *ret_type, mrp_funcbridge_value_t *ret_value) { char t; int i; int sp; mrp_funcbridge_value_t *a; int sts; bool success; if (!fb) success = false; else { switch (fb->type) { case MRP_C_FUNCTION: if (!strcmp(signature, fb->c.signature)) success = fb->c.func(L, fb->c.data, signature, args, ret_type, ret_value); else { *ret_type = MRP_FUNCBRIDGE_STRING; ret_value->string = mrp_strdup("mismatching signature " "@ C invocation"); success = false; } break; case MRP_LUA_FUNCTION: sp = lua_gettop(L); mrp_funcbridge_push(L, fb); lua_rawgeti(L, -1, 1); luaL_checktype(L, -1, LUA_TFUNCTION); for (i = 0; (t = signature[i]); i++) { a = args + i; switch (t) { case MRP_FUNCBRIDGE_STRING: lua_pushstring(L, a->string); break; case MRP_FUNCBRIDGE_INTEGER: lua_pushinteger(L, a->integer); break; case MRP_FUNCBRIDGE_FLOATING: lua_pushnumber(L, a->floating); break; case MRP_FUNCBRIDGE_BOOLEAN: lua_pushboolean(L, a->boolean); break; case MRP_FUNCBRIDGE_OBJECT: mrp_lua_push_object(L, a->pointer); break; default: success = false; goto done; } } sts = lua_pcall(L, i, 1, 0); MRP_ASSERT(!sts || (sts && lua_type(L, -1) == LUA_TSTRING), "lua pcall did not return error string when failed"); switch (lua_type(L, -1)) { case LUA_TSTRING: *ret_type = MRP_FUNCBRIDGE_STRING; ret_value->string = mrp_strdup(lua_tolstring(L, -1, NULL)); break; case LUA_TNUMBER: *ret_type = MRP_FUNCBRIDGE_FLOATING; ret_value->floating = lua_tonumber(L, -1); break; case LUA_TBOOLEAN: *ret_type = MRP_FUNCBRIDGE_BOOLEAN; ret_value->boolean = lua_toboolean(L, -1); break; default: *ret_type = MRP_FUNCBRIDGE_NO_DATA; memset(ret_value, 0, sizeof(*ret_value)); break; } success = !sts; done: lua_settop(L, sp); break; default: success = false; break; } } return success; }
static int lua_carray_new(lua_State *L) { const char *type = luaL_optstring(L, 1, "double"); int size; bool istable = lua_istable(L, 2); if (istable) { #if LUA_VERSION_NUM == 502 size = lua_rawlen(L, 2); #else size = lua_objlen(L, 2); #endif } else { size = luaL_optinteger(L, 2, 1); } structCArray *ud = (structCArray *)lua_newuserdata(L, sizeof(structCArray)); ud->size = size; ud->type = type[0]; ud->own = 1; int i; unsigned char *p_uchar; char *p_char; short *p_short; int *p_int; unsigned int *p_uint; float *p_float; double *p_double; switch (ud->type) { case 'b': p_uchar = new unsigned char[size]; ud->ptr = p_uchar; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); // Lua stack: table, userdata, index (top) lua_gettable(L, -3); p_uchar[i] = lua_tointeger(L, -1); lua_pop(L, 1); } } break; case 'c': p_char = new char[size]; ud->ptr = p_char; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); lua_gettable(L, -3); p_char[i] = lua_tointeger(L, -2); lua_pop(L, 1); } } break; case 's': p_short = new short[size]; ud->ptr = p_short; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); lua_gettable(L, -3); p_short[i] = lua_tointeger(L, -2); lua_pop(L, 1); } } break; case 'i': p_int = new int[size]; ud->ptr = p_int; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); lua_gettable(L, -3); p_int[i] = lua_tointeger(L, -2); lua_pop(L, 1); } } break; case 'u': p_uint = new unsigned int[size]; ud->ptr = p_uint; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); lua_gettable(L, -3); p_uint[i] = lua_tointeger(L, -2); lua_pop(L, 1); } } break; case 'f': p_float = new float[size]; ud->ptr = p_float; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); lua_gettable(L, -3); p_float[i] = lua_tonumber(L, -1); lua_pop(L, 1); } } break; case 'd': p_double = new double[size]; ud->ptr = p_double; if (istable) { for (i = 0; i < size; i++) { lua_pushinteger(L, i+1); lua_gettable(L, -3); p_double[i] = lua_tonumber(L, -1); lua_pop(L, 1); } } break; default: ud->ptr = new char[size]; } luaL_getmetatable(L, "carray_mt"); lua_setmetatable(L, -2); return 1; }
static int color_getBackground(lua_State *L) { lua_pushinteger(L, color_background); return 1; }
int heka_read_message(lua_State *lua, lsb_heka_message *m) { int n = lua_gettop(lua); if (n < 1 || n > 3) { return luaL_error(lua, "read_message() incorrect number of arguments"); } size_t field_len; const char *field = luaL_checklstring(lua, 1, &field_len); int fi = (int)luaL_optinteger(lua, 2, 0); luaL_argcheck(lua, fi >= 0, 2, "field index must be >= 0"); int ai = (int)luaL_optinteger(lua, 3, 0); luaL_argcheck(lua, ai >= 0, 3, "array index must be >= 0"); if (strcmp(field, LSB_UUID) == 0) { if (m->uuid.s) { lua_pushlstring(lua, m->uuid.s, m->uuid.len); } else { lua_pushnil(lua); } } else if (strcmp(field, LSB_TIMESTAMP) == 0) { lua_pushnumber(lua, (lua_Number)m->timestamp); } else if (strcmp(field, LSB_TYPE) == 0) { if (m->type.s) { lua_pushlstring(lua, m->type.s, m->type.len); } else { lua_pushnil(lua); } } else if (strcmp(field, LSB_LOGGER) == 0) { if (m->logger.s) { lua_pushlstring(lua, m->logger.s, m->logger.len); } else { lua_pushnil(lua); } } else if (strcmp(field, LSB_SEVERITY) == 0) { lua_pushinteger(lua, m->severity); } else if (strcmp(field, LSB_PAYLOAD) == 0) { if (m->payload.s) { lua_pushlstring(lua, m->payload.s, m->payload.len); } else { lua_pushnil(lua); } } else if (strcmp(field, LSB_ENV_VERSION) == 0) { if (m->env_version.s) { lua_pushlstring(lua, m->env_version.s, m->env_version.len); } else { lua_pushnil(lua); } } else if (strcmp(field, LSB_PID) == 0) { lua_pushinteger(lua, m->pid); } else if (strcmp(field, LSB_HOSTNAME) == 0) { if (m->hostname.s) { lua_pushlstring(lua, m->hostname.s, m->hostname.len); } else { lua_pushnil(lua); } } else if (strcmp(field, "raw") == 0) { lua_pushlstring(lua, m->raw.s, m->raw.len); } else if (strcmp(field, "framed") == 0) { { char header[14] = "\x1e\x00\x08"; // up to 10 varint bytes and a \x1f int hlen = lsb_pb_output_varint(header + 3, m->raw.len) + 1; header[1] = (char)hlen; header[hlen + 2] = '\x1f'; luaL_Buffer b; luaL_buffinit(lua, &b); luaL_addlstring(&b, header, hlen + 3); luaL_addlstring(&b, m->raw.s, m->raw.len); luaL_pushresult(&b); } } else if (strcmp(field, "size") == 0) { lua_pushnumber(lua, (lua_Number)m->raw.len); } else { if (field_len >= 8 && memcmp(field, LSB_FIELDS "[", 7) == 0 && field[field_len - 1] == ']') { lsb_read_value v; lsb_const_string f = { .s = field + 7, .len = field_len - 8 }; lsb_read_heka_field(m, &f, fi, ai, &v); switch (v.type) { case LSB_READ_STRING: lua_pushlstring(lua, v.u.s.s, v.u.s.len); break; case LSB_READ_NUMERIC: lua_pushnumber(lua, v.u.d); break; case LSB_READ_BOOL: lua_pushboolean(lua, v.u.d ? 1 : 0); break; default: lua_pushnil(lua); break; } } else { lua_pushnil(lua); } }
static int lua_usleep(lua_State *L) { int usec = luaL_checkint(L, 1); int ret = usleep((useconds_t) usec); lua_pushinteger(L, ret); return 1; }
static int ts_lua_transform_handler(TSCont contp, ts_lua_transform_ctx *transform_ctx) { TSVConn output_conn; TSVIO input_vio; TSIOBufferReader input_reader; TSIOBufferBlock blk; int64_t towrite, blk_len, upstream_done, avail, left; const char *start; const char *res; size_t res_len; int ret, eos; lua_State *L; L = transform_ctx->hctx->lua; output_conn = TSTransformOutputVConnGet(contp); input_vio = TSVConnWriteVIOGet(contp); input_reader = TSVIOReaderGet(input_vio); if (!transform_ctx->output_buffer) { transform_ctx->output_buffer = TSIOBufferCreate(); transform_ctx->output_reader = TSIOBufferReaderAlloc(transform_ctx->output_buffer); transform_ctx->output_vio = TSVConnWrite(output_conn, contp, transform_ctx->output_reader, INT64_MAX); } if (!TSVIOBufferGet(input_vio)) { TSVIONBytesSet(transform_ctx->output_vio, transform_ctx->total); TSVIOReenable(transform_ctx->output_vio); return 1; } if (transform_ctx->eos) { return 1; } left = towrite = TSVIONTodoGet(input_vio); upstream_done = TSVIONDoneGet(input_vio); avail = TSIOBufferReaderAvail(input_reader); eos = 0; if (left <= avail) eos = 1; if (towrite > avail) towrite = avail; blk = TSIOBufferReaderStart(input_reader); do { start = TSIOBufferBlockReadStart(blk, input_reader, &blk_len); lua_pushlightuserdata(L, transform_ctx); lua_rawget(L, LUA_GLOBALSINDEX); /* push function */ if (towrite > blk_len) { lua_pushlstring(L, start, (size_t)blk_len); towrite -= blk_len; } else { lua_pushlstring(L, start, (size_t)towrite); towrite = 0; } if (!towrite && eos) { lua_pushinteger(L, 1); /* second param, not finish */ } else { lua_pushinteger(L, 0); /* second param, not finish */ } if (lua_pcall(L, 2, 2, 0)) { fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(L, -1)); } ret = lua_tointeger(L, -1); /* 0 is not finished, 1 is finished */ res = lua_tolstring(L, -2, &res_len); if (res && res_len) { TSIOBufferWrite(transform_ctx->output_buffer, res, res_len); transform_ctx->total += res_len; } lua_pop(L, 2); if (ret || eos) { // EOS eos = 1; break; } blk = TSIOBufferBlockNext(blk); } while (blk && towrite > 0); TSIOBufferReaderConsume(input_reader, avail); TSVIONDoneSet(input_vio, upstream_done + avail); if (eos) { transform_ctx->eos = 1; TSVIONBytesSet(transform_ctx->output_vio, transform_ctx->total); TSVIOReenable(transform_ctx->output_vio); TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio); } else { TSVIOReenable(transform_ctx->output_vio); TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio); } return 1; }
TOLUA_API int tolua_remove_ccobject_by_refid(lua_State* L, int refid) { void* ptr = NULL; const char* type = NULL; void** ud = NULL; if (refid == 0) return -1; // get ptr from tolua_refid_ptr_mapping lua_pushstring(L, TOLUA_REFID_PTR_MAPPING); lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */ lua_pushinteger(L, refid); /* stack: refid_ptr refid */ lua_rawget(L, -2); /* stack: refid_ptr ptr */ ptr = lua_touserdata(L, -1); lua_pop(L, 1); /* stack: refid_ptr */ if (ptr == NULL) { lua_pop(L, 1); // Lua stack has closed, C++ object not in Lua. // printf("[LUA ERROR] remove CCObject with NULL ptr, refid: %d\n", refid); return -2; } // remove ptr from tolua_refid_ptr_mapping lua_pushinteger(L, refid); /* stack: refid_ptr refid */ lua_pushnil(L); /* stack: refid_ptr refid nil */ lua_rawset(L, -3); /* delete refid_ptr[refid], stack: refid_ptr */ lua_pop(L, 1); /* stack: - */ // get type from tolua_refid_type_mapping lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING); lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */ lua_pushinteger(L, refid); /* stack: refid_type refid */ lua_rawget(L, -2); /* stack: refid_type type */ if (lua_isnil(L, -1)) { lua_pop(L, 2); printf("[LUA ERROR] remove CCObject with NULL type, refid: %d, ptr: %x\n", refid, (int)ptr); return -1; } type = lua_tostring(L, -1); lua_pop(L, 1); /* stack: refid_type */ // remove type from tolua_refid_type_mapping lua_pushinteger(L, refid); /* stack: refid_type refid */ lua_pushnil(L); /* stack: refid_type refid nil */ lua_rawset(L, -3); /* delete refid_type[refid], stack: refid_type */ lua_pop(L, 1); /* stack: - */ // get ubox luaL_getmetatable(L, type); /* stack: mt */ lua_pushstring(L, "tolua_ubox"); /* stack: mt key */ lua_rawget(L, -2); /* stack: mt ubox */ if (lua_isnil(L, -1)) { // use global ubox lua_pop(L, 1); /* stack: mt */ lua_pushstring(L, "tolua_ubox"); /* stack: mt key */ lua_rawget(L, LUA_REGISTRYINDEX); /* stack: mt ubox */ }; lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */ lua_rawget(L,-2); /* stack: mt ubox ud */ if (lua_isnil(L, -1)) { // Lua object has released (GC), C++ object not in ubox. //printf("[LUA ERROR] remove CCObject with NULL ubox, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type); lua_pop(L, 3); return -3; } ud = (void**)lua_touserdata(L, -1); lua_pop(L, 1); /* stack: mt ubox */ if (ud == NULL) { printf("[LUA ERROR] remove CCObject with NULL userdata, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type); lua_pop(L, 2); return -1; } // clean userdata *ud = NULL; lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */ lua_pushnil(L); /* stack: mt ubox ptr nil */ lua_rawset(L, -3); /* ubox[ptr] = nil, stack: mt ubox */ lua_pop(L, 2); //printf("[LUA] remove CCObject, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type); return 0; }
int EdgeShape::GetType(lua_State* L) { lua_pushinteger(L, b2Shape::e_edge); return 1; }
// 1 string data // 2 result document table // return boolean succ (false -> request id, error document) // number request_id // document first // string cursor_id // integer startfrom static int op_reply(lua_State *L) { size_t data_len = 0; const char * data = luaL_checklstring(L,1,&data_len); struct { // int32_t length; // total message size, including this int32_t request_id; // identifier for this message int32_t response_id; // requestID from the original request // (used in reponses from db) int32_t opcode; // request type int32_t flags; int32_t cursor_id[2]; int32_t starting; int32_t number; } const *reply = (const void *)data; if (data_len < sizeof(*reply)) { lua_pushboolean(L, 0); return 1; } int id = little_endian(reply->response_id); int flags = little_endian(reply->flags); if (flags & REPLY_QUERYFAILURE) { lua_pushboolean(L,0); lua_pushinteger(L, id); lua_pushlightuserdata(L, (void *)(reply+1)); return 3; } int starting_from = little_endian(reply->starting); int number = little_endian(reply->number); int sz = (int)data_len - sizeof(*reply); const uint8_t * doc = (const uint8_t *)(reply+1); if (lua_istable(L,2)) { int i = 1; while (sz > 4) { lua_pushlightuserdata(L, (void *)doc); lua_rawseti(L, 2, i); int32_t doc_len = get_length((document)doc); doc += doc_len; sz -= doc_len; ++i; } if (i != number + 1) { lua_pushboolean(L,0); lua_pushinteger(L, id); return 2; } int c = lua_rawlen(L, 2); for (;i<=c;i++) { lua_pushnil(L); lua_rawseti(L, 2, i); } } else { if (sz >= 4) { sz -= get_length((document)doc); } } if (sz != 0) { return luaL_error(L, "Invalid result bson document"); } lua_pushboolean(L,1); lua_pushinteger(L, id); if (number == 0) lua_pushnil(L); else lua_pushlightuserdata(L, (void *)(reply+1)); if (reply->cursor_id[0] == 0 && reply->cursor_id[1]==0) { // closed cursor lua_pushnil(L); } else { lua_pushlstring(L, (const char *)(reply->cursor_id), 8); } lua_pushinteger(L, starting_from); return 5; }
int EdgeShape::GetChildCount(lua_State* L) { lua_pushinteger(L, 1); return 1; }
/* ** err = ctx:set_verify_depth(depth) */ static int l_set_verify_depth(lua_State *L) { SSL_CTX_set_verify_depth(getctx(L), luaL_checkint(L, 2)); lua_pushinteger(L, 0); return 1; }
static int luv_write_queue_size(lua_State* L) { uv_tcp_t* handle = luv_check_tcp(L, 1); lua_pushinteger(L, handle->write_queue_size); return 1; }
static void setfield (lua_State *L, const char *key, int value) { lua_pushinteger(L, value); lua_setfield(L, -2, key); }
static int gprs_start( lua_State* L ) { uint32_t len; int timeout = luaL_checkinteger(L, 1); const char *APN[10]; char *APN_USED; char Param[BUFFER_SZ]; uint8_t i = 0; uint8_t j = 5; uint8_t result = 1; unsigned char cont = lua_gettop(L) - 1; APN[0] = lua_tolstring( L, 2, &len ); APN[1] = lua_tolstring( L, 3, &len ); APN[2] = lua_tolstring( L, 4, &len ); APN[3] = lua_tolstring( L, 5, &len ); APN[4] = lua_tolstring( L, 6, &len ); APN[5] = lua_tolstring( L, 7, &len ); APN[6] = lua_tolstring( L, 8, &len ); APN[7] = lua_tolstring( L, 9, &len ); APN[8] = lua_tolstring( L, 10, &len ); APN[9] = lua_tolstring( L, 11, &len ); c_memset(Param, 0 , BUFFER_SZ); if( APN[0] != NULL && APN[1] != NULL && APN[2] != NULL && APN[3] != NULL ) { result = 1; gprs_clear(L); milisec(1500); tcpip_getIpStatus(L); len = gprs_Attach(L); if( len == 1 ) { result = 2; for(i = 0; i < 5; i++) { gprs_clear(L); //tcpip_getIpStatus(L); if( APN[9] != NULL ) { APN_USED = (char*)APN[9]; c_sprintf(Param, CGDCONT, APN[9]); for(j = 0; j < 5; j++) { len = gprs_Activate(L,Param); if( len == 1 ) goto end_act; milisec(1000); } } if( j>= 5 ) { for(j = 0; j < cont; j++) { if( APN[j] != NULL ) { APN_USED = (char*)APN[j]; c_sprintf(Param, CGDCONT, APN[j]); len = gprs_Activate(L,Param); if( len == 1 ) goto end_act; milisec(1000); } } } } end_act: if( tcpip_getIpStatus(L) == 1 ) { tcpip_getIp(L); tcpip_getIpStatus(L); result = 0; lua_pushinteger(L, result); lua_pushstring(L,APN_USED); return 2; } } } lua_pushinteger(L, result); return 1; }
static void dns_cb(struct dns_ctx *ctx, void *result, void *data) { int r = dns_status(ctx); dns_lookup_ctx_t *dlc = data; struct dns_parse p; struct dns_rr rr; unsigned nrr = 0; unsigned char dn[DNS_MAXDN]; const unsigned char *pkt, *cur, *end; lua_State *L; if(!dlc->active) goto cleanup; if(!result) goto cleanup; L = dlc->ci->coro_state; pkt = result; end = pkt + r; cur = dns_payload(pkt); dns_getdn(pkt, &cur, end, dn, sizeof(dn)); dns_initparse(&p, NULL, pkt, cur, end); p.dnsp_qcls = 0; p.dnsp_qtyp = 0; while(dns_nextrr(&p, &rr) > 0) { const char *fieldname = NULL; char buff[DNS_MAXDN], *txt_str, *c; int totalsize; const unsigned char *pkt = p.dnsp_pkt; const unsigned char *end = p.dnsp_end; const unsigned char *dptr = rr.dnsrr_dptr; const unsigned char *dend = rr.dnsrr_dend; unsigned char *dn = rr.dnsrr_dn; const unsigned char *tmp; memset(buff, 0, sizeof(buff)); if (!dns_dnequal(dn, rr.dnsrr_dn)) continue; if ((dlc->query_ctype == DNS_C_ANY || dlc->query_ctype == rr.dnsrr_cls) && (dlc->query_rtype == DNS_T_ANY || dlc->query_rtype == rr.dnsrr_typ)) { lua_newtable(L); lua_pushinteger(L, rr.dnsrr_ttl); lua_setfield(L, -2, "ttl"); switch(rr.dnsrr_typ) { case DNS_T_A: if(rr.dnsrr_dsz == 4) { snprintf(buff, sizeof(buff), "%d.%d.%d.%d", dptr[0], dptr[1], dptr[2], dptr[3]); lua_pushstring(L, buff); lua_setfield(L, -2, "a"); } break; case DNS_T_AAAA: if(rr.dnsrr_dsz == 16) { inet_ntop(AF_INET6, dptr, buff, 16); lua_pushstring(L, buff); lua_setfield(L, -2, "aaaa"); } break; case DNS_T_TXT: totalsize = 0; for(tmp = dptr; tmp < dend; totalsize += *tmp, tmp += *tmp + 1) if(tmp + *tmp + 1 > dend) break; /* worst case: every character escaped + '\0' */ txt_str = alloca(totalsize * 3 + 1); if(!txt_str) break; c = txt_str; for(tmp = dptr; tmp < dend; tmp += *tmp + 1) c = encode_txt(c, tmp+1, *tmp); lua_pushstring(L, txt_str); lua_setfield(L, -2, "txt"); break; case DNS_T_MX: lua_pushinteger(L, dns_get16(dptr)); lua_setfield(L, -2, "preference"); tmp = dptr + 2; if(dns_getdn(pkt, &tmp, end, dn, DNS_MAXDN) <= 0 || tmp != dend) break; dns_dntop(dn, buff + strlen(buff), sizeof(buff) - strlen(buff)); lua_pushstring(L, buff); lua_setfield(L, -2, "mx"); break; case DNS_T_CNAME: if(!fieldname) fieldname = "cname"; case DNS_T_PTR: if(!fieldname) fieldname = "ptr"; case DNS_T_NS: if(!fieldname) fieldname = "ns"; case DNS_T_MB: if(!fieldname) fieldname = "mb"; case DNS_T_MD: if(!fieldname) fieldname = "md"; case DNS_T_MF: if(!fieldname) fieldname = "mf"; case DNS_T_MG: if(!fieldname) fieldname = "mg"; case DNS_T_MR: if(!fieldname) fieldname = "mr"; if(dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN) <= 0) break; dns_dntop(dn, buff, sizeof(buff)); lua_pushstring(L, buff); lua_setfield(L, -2, fieldname); break; default: break; } ++nrr; } else if (rr.dnsrr_typ == DNS_T_CNAME && !nrr) { if (dns_getdn(pkt, &rr.dnsrr_dptr, end, p.dnsp_dnbuf, sizeof(p.dnsp_dnbuf)) <= 0 || rr.dnsrr_dptr != rr.dnsrr_dend) { break; } } } cleanup: if(result) free(result); if(dlc->active) dlc->ci->lmc->resume(dlc->ci, nrr); lookup_ctx_release(dlc); }
static int color_getDefault(lua_State *L) { lua_pushinteger(L, color_default); return 1; }
bool EmbeddedGamePanel::loadLua() { // Create state m_L = luaL_newstate(); lua_atpanic(m_L, _l_panic); // Save a pointer to ourselves in the registry lua_pushliteral(m_L, "wxWindow"); lua_pushlightuserdata(m_L, reinterpret_cast<wxWindow*>(this)); lua_settable(m_L, LUA_REGISTRYINDEX); // Open default libraries, and override appropriate bits luaL_openlibs(m_L); luaT_execute(m_L, "print = ...", _l_print); // Set _MAP_EDITOR to true, to allow scripts to notice that they are // running inside this component, rather than standalone. luaT_execute(m_L, "_MAP_EDITOR = true"); // Load CorsixTH.lua and perform other initialisation needed by it lua_settop(m_L, 0); lua_pushcfunction(m_L, CorsixTH_lua_stacktrace); lua_pushcfunction(m_L, CorsixTH_lua_main_no_eval); lua_checkstack(m_L, wxTheApp->argc); for(int i = 0; i < wxTheApp->argc; ++ i) lua_pushstring(m_L, wxTheApp->argv[i]); if(lua_pcall(m_L, wxTheApp->argc, 1, 1)) { if(m_pPrintTarget) { m_pPrintTarget->AppendText(L"Error initialising Lua: "); m_pPrintTarget->AppendText(lua_tostring(m_L, -1)); m_pPrintTarget->AppendText(L"\n"); } return false; } // NB: CorsixTH_lua_main_no_eval will have loaded CorsixTH.lua and left it // as the top value on the stack, but will not have executed it. // The stack will hence have two things on it: the stacktrace function, // and the loaded CorsixTH.lua // Overwrite what CorsixTH_lua_main_no_eval registered for require("sdl") // with our own function that uses wxWidgets to do what SDL would have. luaT_execute(m_L, "package.preload.sdl = ...", _l_open_sdl); // Replace the Surface:endFrame() function with our own lua_getglobal(m_L, "require"); lua_pushliteral(m_L, "TH"); lua_call(m_L, 1, 1); lua_getfield(m_L, -1, "surface"); lua_getfield(m_L, -1, "endFrame"); lua_pushcclosure(m_L, _l_end_frame, 1); lua_setfield(m_L, -2, "endFrame"); lua_pop(m_L, 2); // Perform extra initialisation if(m_fnExtraLuaInit) { if(lua_cpcall(m_L, m_fnExtraLuaInit, m_pExtraLuaInitArg) != 0) lua_pop(m_L, 1); } // Execute CorsixTH.lua in a coroutine lua_getglobal(m_L, "coroutine"); lua_getfield(m_L, -1, "create"); lua_replace(m_L, -2); lua_insert(m_L, -2); lua_call(m_L, 1, 1); lua_State *L = lua_tothread(m_L, -1); if(lua_resume(L, 0) != LUA_YIELD) { if(m_pPrintTarget) { // Push debug.traceback onto m_L lua_getglobal(m_L, "debug"); lua_getfield(m_L, -1, "traceback"); lua_replace(m_L, -2); // Push the thread onto m_L lua_pushvalue(m_L, -2); // Push tostring(errmsg) onto m_L lua_getglobal(m_L, "tostring"); lua_xmove(L, m_L, 1); lua_call(m_L, 1, 1); // Push constant 1 onto m_L lua_pushinteger(m_L, 1); // Call debug.traceback(thread, tostring(err), 1) lua_call(m_L, 3, 1); // Display resulting string and pop it m_pPrintTarget->AppendText(L"Error initialising Lua: "); m_pPrintTarget->AppendText(lua_tostring(m_L, -1)); m_pPrintTarget->AppendText(L"\n"); lua_pop(m_L, 1); } return false; } lua_settop(L, 1); m_Lthread = L; // The stack of the Lua states is now as follows: // m_L: stacktrace function, m_Lthread <top // m_Lthread: event dispatch coroutine <top return true; }
/* ** Traversal function for 'ipairs' for tables with metamethods */ static int ipairsaux (lua_State *L) { lua_Integer i = luaL_checkinteger(L, 2) + 1; lua_pushinteger(L, i); return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; }