lua-api

Weak table and GC finalizer using C API

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 06:53:15
I am attempting to create a GC finalizer for a function value by storing it in a weak table using the C API. I started off by writing a prototype in pure Lua 5.2: local function myfinalizer() print 'Called finalizer' end function myfunc() print 'Called myfunc' end local sentinels = setmetatable({}, { __mode='k' }) sentinels[myfunc] = setmetatable({}, { __gc=myfinalizer }) myfunc() myfunc = nil collectgarbage 'collect' print 'Closing Lua' Resulting output: Called myfunc Called finalizer Closing Lua The prototype seems to be working as intended. Below is the C version: #include <stdlib.h>

How to execute an untrusted Lua file in its own environment from the C API

泄露秘密 提交于 2019-12-03 13:22:47
问题 I want to execute an untrusted .lua file in its own environment by calling lua_setfenv() so that it cannot affect any of my code. The documentation for that function though only explains how to call a function, not how to execute a file. Currently to run the file I use: int error = luaL_loadfile(mState, path.c_str()) || lua_pcall(mState, 0, 0, 0); Do I have to call the "dofile" lua function from the C API with lua_setfenv , or is there a more elegant way to do it? 回答1: See the discussion at

How to execute an untrusted Lua file in its own environment from the C API

廉价感情. 提交于 2019-12-03 03:26:31
I want to execute an untrusted .lua file in its own environment by calling lua_setfenv() so that it cannot affect any of my code. The documentation for that function though only explains how to call a function, not how to execute a file. Currently to run the file I use: int error = luaL_loadfile(mState, path.c_str()) || lua_pcall(mState, 0, 0, 0); Do I have to call the "dofile" lua function from the C API with lua_setfenv , or is there a more elegant way to do it? See the discussion at the Lua User's Wiki of sandboxing , and the more general topic of script security . There are a number of

Print stacktrace from C code with embedded lua

夙愿已清 提交于 2019-11-30 08:13:38
If I understand this correctly, Lua by default will call the debug library "debug.traceback" when an error occurs. However, when embedding Lua into C code like done in the example here: Simple Lua API Example We only have available the error message on the top of the stack. i.e. if (status) { /* If something went wrong, error message is at the top of */ /* the stack */ fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1)); /* I want to print a stacktrace here. How do I do that? */ exit(1); } How do I print the stack trace from C after the initial error? Nicol Bolas Lua by default

Calling lua functions from .lua's using handles?

空扰寡人 提交于 2019-11-30 05:32:41
I'm working on a small project trying to integrate lua with c++. My problem however is as follows: I have multiple lua scripts, lets call them s1.lua s2.lua and s3.lua. Each of these has the following functions: setVars() and executeResults(). Now I am able to to call a lua file through LuaL_dofile and immediately after use setVars() and/or executeResults(). The problem here however is that after I load s2.lua I can no longer call the functions of s1.lua. This would mean I have to redo the LuaL_dofile on s1.lua to regain access to the function and by doing so I lose access to the functions in

Print stacktrace from C code with embedded lua

让人想犯罪 __ 提交于 2019-11-29 11:12:03
问题 If I understand this correctly, Lua by default will call the debug library "debug.traceback" when an error occurs. However, when embedding Lua into C code like done in the example here: Simple Lua API Example We only have available the error message on the top of the stack. i.e. if (status) { /* If something went wrong, error message is at the top of */ /* the stack */ fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1)); /* I want to print a stacktrace here. How do I do that? */

Calling lua functions from .lua's using handles?

给你一囗甜甜゛ 提交于 2019-11-29 04:17:59
问题 I'm working on a small project trying to integrate lua with c++. My problem however is as follows: I have multiple lua scripts, lets call them s1.lua s2.lua and s3.lua. Each of these has the following functions: setVars() and executeResults(). Now I am able to to call a lua file through LuaL_dofile and immediately after use setVars() and/or executeResults(). The problem here however is that after I load s2.lua I can no longer call the functions of s1.lua. This would mean I have to redo the

How to create nested Lua tables using the C API

房东的猫 提交于 2019-11-28 18:03:13
I want to create a table like myTable = { [0] = { ["a"] = 4, ["b"] = 2 }, [1] = { ["a"] = 13, ["b"] = 37 } } using the C API? My current approach is lua_createtable(L, 0, 2); int c = lua_gettop(L); lua_pushstring(L, "a"); lua_pushnumber(L, 4); lua_settable(L, c); lua_pushstring(L, "b"); lua_pushnumber(L, 2); lua_settable(L, c); to create the inner tables in a loop. Before, this loop, I use lua_createtable(L, 2, 0); int outertable = lua_gettop(L); to create the outer table for 2 numeric slots. But how can I save the inner tables to the outer table? Here's a full and minimal program

How to create nested Lua tables using the C API

北慕城南 提交于 2019-11-27 11:00:33
问题 I want to create a table like myTable = { [0] = { ["a"] = 4, ["b"] = 2 }, [1] = { ["a"] = 13, ["b"] = 37 } } using the C API? My current approach is lua_createtable(L, 0, 2); int c = lua_gettop(L); lua_pushstring(L, "a"); lua_pushnumber(L, 4); lua_settable(L, c); lua_pushstring(L, "b"); lua_pushnumber(L, 2); lua_settable(L, c); to create the inner tables in a loop. Before, this loop, I use lua_createtable(L, 2, 0); int outertable = lua_gettop(L); to create the outer table for 2 numeric slots.