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

Commit

Permalink
Revert any chunk name arg changes
Browse files Browse the repository at this point in the history
It breaks Luau.Run (and Luau.RunBytecode)
  • Loading branch information
RadiatedExodus committed Jan 23, 2023
1 parent 49fbaef commit 4298616
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
16 changes: 8 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ extern "C" char* LuauCompile(const char* src)
return luau_compile(src, strlen(src), nullptr, &BytecodeSize);
}

extern "C" int LuauRunUsingCustomState(lua_State* L, const char* bytecode, const char* chunkname)
extern "C" int LuauRunUsingCustomState(lua_State* L, const char* bytecode)
{
std::string BytecodeStdString = bytecode;
int LoadResult = luau_load(L, chunkname, bytecode, BytecodeStdString.size(), 0);
int LoadResult = luau_load(L, "LuauInLuau_Chunk", bytecode, BytecodeStdString.size(), 0);
if (LoadResult == 1) { return 1; }
return lua_pcall(L, 0, 0, 0);
}

extern "C" int LuauRun(const char* bytecode, const char* chunkname)
extern "C" int LuauRun(const char* bytecode)
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
int Result = LuauRunUsingCustomState(L, bytecode, chunkname);
int Result = LuauRunUsingCustomState(L, bytecode);
lua_close(L);
return Result;
}

extern "C" int LuauRunWithSafeEnv(const char* bytecode, const char* chunkname)
extern "C" int LuauRunWithSafeEnv(const char* bytecode)
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaL_sandbox(L);
int Result = LuauRunUsingCustomState(L, bytecode, chunkname);
int Result = LuauRunUsingCustomState(L, bytecode);
lua_close(L);
return Result;
}

int main()
{
const char* Bytecode = LuauCompile("print(\"hello world\")");
LuauRun(Bytecode, "LuauInLuau_Chunk");
LuauRunWithSafeEnv(Bytecode, "LuauInLuau_Chunk");
LuauRun(Bytecode);
LuauRunWithSafeEnv(Bytecode);
return 0;
}
28 changes: 8 additions & 20 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, chunkname: string?, safeenv: boolean?)
local function LuauRun(src: string, safeenv: boolean?)
--// Compile bytecode
local CompileSuccess, Result, ResultSize = LuauCompile(src)
assert(CompileSuccess, Result)
Expand All @@ -136,11 +136,6 @@ local function LuauRun(src: string, chunkname: 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 @@ -149,33 +144,27 @@ local function LuauRun(src: string, chunkname: string?, safeenv: boolean?)
end

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

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

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

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

local function LuauRunBytecode(bytecode: string, chunkname: string?, safeenv: boolean?)
local function LuauRunBytecode(bytecode: 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 @@ -184,12 +173,11 @@ local function LuauRunBytecode(bytecode: string, chunkname: string?, safeenv: bo
end

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

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

Expand Down

0 comments on commit 4298616

Please sign in to comment.