{
#if LUA_VERSION_NUM >= 502
lua_getglobal(L, "_SANDBOX");
- lua_setupvalue(L, -2, 1);
+ lua_setupvalue(L, -(2 + argc), 1);
#endif
return lua_pcall(L, argc, LUA_MULTRET, 0);
}
/* Check result. */
if (l_sandboxcall(engine->L, 1) != 0) {
+ fprintf(stderr, "%s\n", lua_tostring(engine->L, -1));
+ lua_pop(engine->L, 1);
return kr_error(EINVAL);
}
/* Evaluate */
if (ret != 0) {
- fprintf(stderr, "[system] error %s\n", lua_tostring(engine->L, -1));
+ fprintf(stderr, "%s\n", lua_tostring(engine->L, -1));
lua_pop(engine->L, 1);
return kr_error(EINVAL);
}
})
-- Make sandboxed environment
-function make_sandbox(defined)
+local function make_sandbox(defined)
local __protected = { modules = true, cache = true, net = true }
return setmetatable({}, {
__index = defined,
})
end
+-- Compatibility sandbox
if setfenv then -- Lua 5.1 and less
_G = make_sandbox(getfenv(0))
setfenv(0, _G)
_SANDBOX = make_sandbox(_ENV)
end
+-- Interactive command evaluation
+function eval_cmd(line)
+ -- Compatibility sandbox code loading
+ local function load_code(code)
+ if getfenv then -- Lua 5.1
+ return loadstring(code)
+ else -- Lua 5.2+
+ return load(code, nil, 't', _ENV)
+ end
+ end
+ local status, err, chunk
+ chunk, err = load_code('table_print('..line..')')
+ if err then
+ chunk, err = load_code(line)
+ end
+ if not err then
+ chunk()
+ end
+ if err then
+ print(err)
+ end
+end
+
-- Pretty printing
function table_print (tt, indent, done)
done = done or {}
else
io.write(tostring(tt) .. "\n")
end
-end
-
--- Interactive command evaluation
-function eval_cmd(line)
- local chunk, err = loadstring('table_print('..line..')')
- if err then
- chunk, err = loadstring(line)
- end
- if not err then
- status, err = pcall(chunk)
- end
- if err then
- print(err)
- end
end
\ No newline at end of file