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

Commit

Permalink
Sync to 0.579
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiatedExodus committed Jun 8, 2023
1 parent 970fd37 commit eab3942
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# LuauInLuau
Inspired by @Rerumu's [LuauInLuau](https://gist.github.com/Rerumu/ecaf1de2f2b31d0fa91b9bac8e1e15d8), I've created this repository to translate Luau to Luau.

## BROKEN SINCE 0.579
Something seems to have changed (needing gl apis and ``__wasm_call_ctors`` jumped from 10/11 to 350, along with every function, the changes are REALLY large), the resulting wasm binary now requires multiple environment functions that I cannot write at the moment, there is a 0.579 pre-release but keep in mind that it does NOT work. Previous releases are unaffected. Feel free to make a pr to fix the issues, but as of June 8 2023, I cannot fix this due to my lack of understanding.

## Features
- Running Luau code (with or without safe env)
- Compiling Luau code into bytecode
Expand All @@ -13,10 +16,10 @@ You can get it from releases or compile using the following instruction: (they w
4. Compile this project into WebAssembly (``sh build.sh``, make sure cmake, clang/gcc, and llvm is installed)
5. Use Wasynth to translate the WebAssembly to Luau (``wasm2luau LuauInLuau.wasm > LuauInLuau.luau``)
6. Copy over the contents of ``ExtraCodeSnippet.txt`` (at the ``snippets`` folder) and comment out the old returned function generated by Wasynth, _you might need to change some things if theres a breaking change update_.
7. (ONLY NEEDED IF RUNNING VANILLA LUAU) Copy the contents of ``Vector3Table.txt`` (at the ``snippets`` folder) to the top of the script, no changes should be required, you need this to have the script work at vanilla Luau (like the luau repl for example) as Wasynth uses Roblox's Vector3 datatype (the snippet checks for the existence of the V3 global and if it doesn't exist then it recreates it + overriding type and typeof for getting the V3 datatype). Alternatively compile Wasynth with ``default-features = false``, since it will include the Vector3 table implementation directly without any need of user interference. (Thanks @Rerumu for the Wasynth compile tip)
7. (ONLY NEEDED IF RUNNING VANILLA LUAU) Copy the contents of ``Vector3Table.txt`` (at the ``snippets`` folder) to the top of the script, no changes should be required, you need this to have the script work at vanilla Luau (like the luau repl for example) as Wasynth uses Roblox's Vector3 datatype (the snippet checks for the existence of the V3 global and if it doesn't exist then it recreates it + overriding type and typeof for getting the V3 datatype). Alternatively compile Wasynth with ``default-features = false``, since it will include the Vector3 table implementation directly without any need of user interference. (Thanks [@Rerumu](https://github.com/Rerumu) for the Wasynth compile tip)

## How it works and a bit of a motivation background
I've started working on this around December 2022, where I found @Rerumu's LuauInLuau gist which caught my interest since I've been looking for a Luau compiler and interpreter written in Luau, the way it works was pretty simple, you compile C++ code into WebAssembly and use @Rerumu's [Wasynth](https://github.com/Rerumu/Wasynth) to translate WebAssembly to Luau, @Rerumu and a few others in their server had helped me with this process. I was then able to produce a working LuauInLuau for version 0.556.
I've started working on this around December 2022, where I found @Rerumu's LuauInLuau gist which caught my interest since I've been looking for a Luau compiler and interpreter written in Luau, the way it works was pretty simple, you compile C++ code into WebAssembly and use [@Rerumu](https://github.com/Rerumu)'s [Wasynth](https://github.com/Rerumu/Wasynth) to translate WebAssembly to Luau, [@Rerumu](https://github.com/Rerumu) and a few others in their server had helped me with this process. I was then able to produce a working LuauInLuau for version 0.556.

Now skipping to January 14 2023, I've come back to this little project under one goal, to be able to compile Luau code and have the bytecode string in Luau, I could have just directly used ``luau_compile`` but when I first started trying things, I was too stupid to understand how Wasynth even handles loading and storing string and integers.

Expand All @@ -30,7 +33,7 @@ My solution? Make a C++ program to wrap around those functions! Yes, that is why
- [Fiu](https://github.com/TheGreatSageEqualToHeaven/Fiu) - Used their Luau bytecode version verification code

## Acknowledgements
- @Roblox and Luau contributors
- [@Roblox](https://github.com/Roblox) and Luau contributors
- Emscripten contributors and creators
- @Rerumu
- @TheGreatSageEqualToHeaven
- [@Rerumu](https://github.com/Rerumu)
- [@TheGreatSageEqualToHeaven](https://github.com/TheGreatSageEqualToHeaven)
2 changes: 1 addition & 1 deletion emsdk
2 changes: 1 addition & 1 deletion luau
Submodule luau updated from 734589 to 63679f
39 changes: 23 additions & 16 deletions snippets/ExtraCodeSnippet.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
--// Named function dictionary
local NamedFunctionList = {
--// Luau
["luau_compile"] = FUNC_LIST[374];
["luaL_newstate"] = FUNC_LIST[586];
["luaL_openlibs"] = FUNC_LIST[584];
["lua_close"] = FUNC_LIST[652];
["luaL_sandbox"] = FUNC_LIST[585];
["lua_tolstring"] = FUNC_LIST[399];
["luau_compile"] = FUNC_LIST[753];
["luaL_newstate"] = FUNC_LIST[1083];
["luaL_openlibs"] = FUNC_LIST[1080];
["lua_close"] = FUNC_LIST[1150];
["luaL_sandbox"] = FUNC_LIST[1082];
["lua_tolstring"] = FUNC_LIST[784];

--// Standard Library
["dlmalloc"] = FUNC_LIST[1257];
["dlfree"] = FUNC_LIST[1258];
["dlmalloc"] = FUNC_LIST[3450];
["dlfree"] = FUNC_LIST[3451];

--// WebAssembly
["__wasm_call_ctors"] = FUNC_LIST[11];
["__wasm_call_ctors"] = FUNC_LIST[350];

--// LuauInLuau
["LuauRunUsingCustomState"] = FUNC_LIST[12];
["LuauRunUsingCustomState"] = FUNC_LIST[352];

--// Macros
["lua_tostring"] = function(L, i) return FUNC_LIST[399](L, i, 0) end;
["lua_tostring"] = function(L, i) return FUNC_LIST[784](L, i, 0) end;
}

--// Pre-init environment function setup
Expand Down Expand Up @@ -47,17 +47,17 @@ FUNC_LIST[0] = function(except, _info, _dtor)
end

--// emscripten_memcpy_big
FUNC_LIST[2] = function(destination, source, size)
FUNC_LIST[281] = function(destination, source, size)
rt.allocator.copy(memory_at_0, destination, source, size)
end

--// emscripten_date_now
FUNC_LIST[3] = function()
FUNC_LIST[285] = function()
return NOW_TIME + os.clock() * 1000
end

--// fd_write
FUNC_LIST[7] = function(fd, list, count, ret_pointer)
FUNC_LIST[268] = function(fd, list, count, ret_pointer)
local file = FILE_MAP[fd]
if not file then return 8 end
local total = 0
Expand All @@ -74,7 +74,7 @@ FUNC_LIST[7] = function(fd, list, count, ret_pointer)
end

--// abort
FUNC_LIST[10] = function(reason)
FUNC_LIST[312] = function(reason)
assert(false, "Aborted("..reason..")")
end

Expand All @@ -88,7 +88,14 @@ local function lua_try_catch(index, ...)
return TABLE_LIST[0].data[index](...)
end

for _, v in ipairs { 1,4,5,6,8,9 } do
local DoNotPatchTargets = { 0, 268, 281, 285, 312 }
local PatchTargets = {}
for i = 0, 349 do
if table.find(DoNotPatchTargets, i) then continue end
table.insert(PatchTargets, i)
end

for _, v in ipairs(PatchTargets) do
FUNC_LIST[v] = lua_try_catch
end

Expand Down

0 comments on commit eab3942

Please sign in to comment.