Skip to content

Commit

Permalink
Fix Luau VM and invoke handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RealEthanPlayzDev committed Jun 2, 2024
1 parent 9ba8f54 commit dff22da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
16 changes: 5 additions & 11 deletions snippets/Full.luau
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ local cfns = {
["luaopen_utf8"] = FUNC_LIST[732],
["lua_isthreadreset"] = FUNC_LIST[645],
["luau_load"] = FUNC_LIST[824],
["setThrew"] = FUNC_LIST[1136],
["stackSave"] = FUNC_LIST[1145],
["stackRestore"] = FUNC_LIST[1146],
}

local NOW_TIME = os.time() * 1000
Expand Down Expand Up @@ -258,9 +261,6 @@ FUNC_LIST[11] = stub("_gmtime_js", 0)
run_init_code()
memory_at_0 = MEMORY_LIST[0]

local zeroIntPtr = cfns.malloc(4)
rt.store.i32(memory_at_0, zeroIntPtr, 0)

local function luau_compile(src: string, optLevel: number?, dbgLevel: number?, covLevel: number?)
--// Source string
local srcPtr = cstr(src)
Expand Down Expand Up @@ -305,24 +305,18 @@ local function luau_run(bc: string, chunkName: string?, safeEnv: boolean?)
if (safeEnv) then cfns.luaL_sandbox(L) end

--// Load bytecode
local ldResPtr = cfns.luau_load(L, cnPtr, bcPtr, #bc, 0)
local ldRes = rt.load.i32(memory_at_0, ldResPtr)
local ldRes = cfns.luau_load(L, cnPtr, bcPtr, #bc, 0)

--// Cleanup unused stuff
cfns.free(bcPtr)
cfns.free(cnPtr)
cfns.free(ldResPtr)

--// Check if load succeded
if (ldRes ~= 0) then cfns.lua_close(L) end
assert(ldRes == 0, "failed to load luau bytecode")

--// Call function
local clResPtr = cfns.lua_pcall(L, zeroIntPtr, zeroIntPtr, zeroIntPtr)
local clRes = rt.load.i32(memory_at_0, clResPtr)

--// Cleanup unused stuff
cfns.free(clResPtr)
local clRes = cfns.lua_pcall(L, 0, 0, 0)

return clRes == 0
end
Expand Down
14 changes: 9 additions & 5 deletions snippets/LCUtils.luau
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ local function stub(name, ret)
end
end

--// TODO: Properly handle exceptions
local function invokeHandler(index)
return function(...)
local args = table.pack(...)
local result = table.pack(pcall(function() return TABLE_LIST[0].data[index](table.unpack(args)) end))
local success = table.remove(result, 1)
assert(success, "exception occured in c")
return table.unpack(result)
local fnIndex = table.remove(args, 1)
local sp = cfns.stackSave()
local e, stacktrace = "N/A", "N/A"
local success = xpcall(function() return TABLE_LIST[0].data[fnIndex](table.unpack(args)) end, function(a) e = a; stacktrace = debug.traceback() end)
if (not success) then
cfns.stackRestore(sp)
assert(typeof(e) ~= "string", `{e}\n\ninvoke stacktrace:\n{stacktrace}`)
cfns.setThrew(1, 0)
end
end
end
16 changes: 5 additions & 11 deletions snippets/VM.luau
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ local cfns = {
["luaopen_utf8"] = FUNC_LIST[463],
["lua_isthreadreset"] = FUNC_LIST[376],
["luau_load"] = FUNC_LIST[555],
["setThrew"] = FUNC_LIST[636],
["stackSave"] = FUNC_LIST[645],
["stackRestore"] = FUNC_LIST[646],
}

local NOW_TIME = os.time() * 1000
Expand Down Expand Up @@ -257,9 +260,6 @@ FUNC_LIST[11] = stub("_gmtime_js", 0)
run_init_code()
memory_at_0 = MEMORY_LIST[0]

local zeroIntPtr = cfns.malloc(4)
rt.store.i32(memory_at_0, zeroIntPtr, 0)

local function luau_run(bc: string, chunkName: string?, safeEnv: boolean?)
--// Bytecode
local bcPtr = cstr(bc)
Expand All @@ -273,24 +273,18 @@ local function luau_run(bc: string, chunkName: string?, safeEnv: boolean?)
if (safeEnv) then cfns.luaL_sandbox(L) end

--// Load bytecode
local ldResPtr = cfns.luau_load(L, cnPtr, bcPtr, #bc, 0)
local ldRes = rt.load.i32(memory_at_0, ldResPtr)
local ldRes = cfns.luau_load(L, cnPtr, bcPtr, #bc, 0)

--// Cleanup unused stuff
cfns.free(bcPtr)
cfns.free(cnPtr)
cfns.free(ldResPtr)

--// Check if load succeded
if (ldRes ~= 0) then cfns.lua_close(L) end
assert(ldRes == 0, "failed to load luau bytecode")

--// Call function
local clResPtr = cfns.lua_pcall(L, zeroIntPtr, zeroIntPtr, zeroIntPtr)
local clRes = rt.load.i32(memory_at_0, clResPtr)

--// Cleanup unused stuff
cfns.free(clResPtr)
local clRes = cfns.lua_pcall(L, 0, 0, 0)

return clRes == 0
end
Expand Down

0 comments on commit dff22da

Please sign in to comment.