Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

Commit

Permalink
ChunkName argument impl
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiatedExodus committed Jan 23, 2023
1 parent 64e3adc commit 49fbaef
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions snippets/ExtraCodeSnippet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ local function LuauCompile(src: string)
return ValidLuauBytecode, Bytecode, BytecodeSize
end

local function LuauRun(src: string, safeenv: boolean?)
local function LuauRun(src: string, chunkname: string?, safeenv: boolean?)
--// Compile bytecode
local CompileSuccess, Result, ResultSize = LuauCompile(src)
assert(CompileSuccess, Result)
Expand All @@ -136,6 +136,11 @@ local function LuauRun(src: string, safeenv: boolean?)
local BytecodePtr = NamedFunctionList.dlmalloc(ResultSize)
rt.store.string(memory_at_0, BytecodePtr, Result)

--// Load chunk name string into memory
local ChunkName = chunkname or "LuauInLuau_Chunk"
local ChunkNamePtr = NamedFunctionList.dlmalloc(#ChunkName)
rt.store.string(memory_at_0, BytecodePtr, ChunkName)

--// lua_State setup
local L = NamedFunctionList.luaL_newstate()
NamedFunctionList.luaL_openlibs(L)
Expand All @@ -144,27 +149,33 @@ local function LuauRun(src: string, safeenv: boolean?)
end

--// Load and run bytecode
local Result = NamedFunctionList.LuauRunUsingCustomState(L, BytecodePtr)
local Result = NamedFunctionList.LuauRunUsingCustomState(L, BytecodePtr, ChunkNamePtr)

--// Cleanup and return
NamedFunctionList.lua_close(L)
NamedFunctionList.dlfree(BytecodePtr)
NamedFunctionList.dlfree(ChunkNamePtr)
return ResultCodeToBoolean(Result)
end

local function LuauRunWithSafeEnv(src: string)
return LuauRun(src, true)
local function LuauRunWithSafeEnv(src: string, chunkname: string?)
return LuauRun(src, chunkname, true)
end

local function LuauRunWithoutSafeEnv(src: string)
return LuauRun(src, false)
local function LuauRunWithoutSafeEnv(src: string, chunkname: string?)
return LuauRun(src, chunkname, false)
end

local function LuauRunBytecode(bytecode: string, safeenv: boolean?)
local function LuauRunBytecode(bytecode: string, chunkname: string?, safeenv: boolean?)
--// Load bytecode string into memory
local BytecodePtr = NamedFunctionList.dlmalloc(#bytecode)
rt.store.string(memory_at_0, BytecodePtr, bytecode)

--// Load chunk name string into memory
local ChunkName = chunkname or "LuauInLuau_Chunk"
local ChunkNamePtr = NamedFunctionList.dlmalloc(#ChunkName)
rt.store.string(memory_at_0, BytecodePtr, ChunkName)

--// lua_State setup
local L = NamedFunctionList.luaL_newstate()
NamedFunctionList.luaL_openlibs(L)
Expand All @@ -173,11 +184,12 @@ local function LuauRunBytecode(bytecode: string, safeenv: boolean?)
end

--// Load bytecode into lua_State and run bytecode
local Result = NamedFunctionList.LuauRunUsingCustomState(L, BytecodePtr)
local Result = NamedFunctionList.LuauRunUsingCustomState(L, BytecodePtr, ChunkNamePtr)

--// Cleanup and return
NamedFunctionList.lua_close(L)
NamedFunctionList.dlfree(BytecodePtr)
NamedFunctionList.dlfree(ChunkNamePtr)
return ResultCodeToBoolean(Result)
end

Expand Down

0 comments on commit 49fbaef

Please sign in to comment.