Last active
July 26, 2025 12:24
-
-
Save jLn0n/105e985b6402ccaf7c1f65b66f4415fd to your computer and use it in GitHub Desktop.
problem and potential solution to bundled darklua code not working for luau analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --!optimize 2 | |
| local __DARKLUA__ | |
| __DARKLUA__ = { | |
| cache = {}, | |
| load = function(m) | |
| if not __DARKLUA__.cache[m] then | |
| __DARKLUA__.cache[m] = { | |
| c = __DARKLUA__[m](), | |
| } | |
| end | |
| return __DARKLUA__.cache[m].c | |
| end, | |
| } | |
| do | |
| function __DARKLUA__.a() | |
| return {hi1 = "hi"} | |
| end | |
| function __DARKLUA__.b() | |
| return {hi2 = "hi"} | |
| end | |
| function __DARKLUA__.c() | |
| return {hi3 = "hi"} | |
| end | |
| function __DARKLUA__.d() | |
| return {hi4 = "hi"} | |
| end | |
| function __DARKLUA__.e() | |
| return {hi5 = "hi"} | |
| end | |
| end | |
| local module_a = __DARKLUA__.load("a") | |
| local module_b = __DARKLUA__.load("b") | |
| local module_c = __DARKLUA__.load("c") | |
| return nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --!optimize 2 | |
| local __DARKLUA_cache__ = {} | |
| local __DARKLUA__ = setmetatable({}, { | |
| __index = function(self, m) | |
| if not __DARKLUA_cache__[m] then | |
| local module = rawget(self, m)() | |
| __DARKLUA_cache__[m] = function() | |
| return module | |
| end | |
| end | |
| return __DARKLUA_cache__[m] or rawget(self, m) -- hacks luau analysis to return the correct type | |
| end | |
| }) | |
| do | |
| __DARKLUA__.a = function() | |
| return {hi1 = "hi"} | |
| end | |
| __DARKLUA__.b = function() | |
| return {hi2 = "hi"} | |
| end | |
| __DARKLUA__["c"] = function() | |
| return {hi3 = "hi"} | |
| end | |
| __DARKLUA__["d"] = function() | |
| return {hi4 = "hi"} | |
| end | |
| __DARKLUA__["e"] = function() | |
| return {hi5 = "hi"} | |
| end | |
| end | |
| local module_a = __DARKLUA__["a"]() | |
| local module_b = __DARKLUA__["b"]() | |
| local module_c = __DARKLUA__["c"]() | |
| return nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --!optimize 2 | |
| --// LuauCeption - Compiler Variant | |
| --// Build from Luau 0.683 (0ce993fe6cf9850a144eaa398f6c0e2dc2f955b1) | |
| local STUB_WARN = false | |
| type LuauCompileOptions__DARKLUA_TYPE_a = {OptimizationLevel: number, DebugLevel: number, TypeInfoLevel: number, CoverageLevel: number, VectorLibName: string?, VectorLibConstructor: string?, VectorType: string?, MutableGlobals: {string}?, DisabledBuiltins: {string}?} | |
| local __DARKLUA_cache__ = {} | |
| local __LUAUCEPTION__ = setmetatable({}, { | |
| __index = function(self, m) | |
| if not __DARKLUA_cache__[m] then | |
| local module = rawget(self, m)() | |
| __DARKLUA_cache__[m] = function() | |
| return module | |
| end | |
| end | |
| return __DARKLUA_cache__[m] or rawget(self, m) -- hacks luau analysis to return the correct type | |
| end | |
| }) | |
| do | |
| function __LUAUCEPTION__.a() | |
| return function() | |
| local rt, memory_at_0, cfns, INDIRECT_FUNCTIONS | |
| local module = {} | |
| function module.stub(name: string, ret: any) | |
| return function(...) | |
| if (STUB_WARN) then | |
| print(`[WARN]: stub function '{name}' called`) | |
| end | |
| return ret | |
| end | |
| end | |
| function module.insertCFunction(func: (...any) -> (...any)): number | |
| local ptr = #INDIRECT_FUNCTIONS.data + 1 | |
| INDIRECT_FUNCTIONS.data[ptr] = func | |
| return ptr | |
| end | |
| function module.cstr(value: string): number | |
| local str_len = #value | |
| local str_ptr = cfns.malloc(str_len + 1) | |
| rt.store.string(memory_at_0, str_ptr, value) | |
| rt.store.i32_n8(memory_at_0, str_ptr + str_len, 0) | |
| return str_ptr | |
| end | |
| function module.cstr_array(array: {string}): number | |
| local array_len = #array | |
| local array_ptr = cfns.malloc((array_len + 1) * 4) | |
| for idx = 0, (array_len - 1)do | |
| local str_val = array[idx] | |
| local str_len = #str_val | |
| local str_ptr = cfns.malloc(str_len + 1) | |
| rt.store.string(memory_at_0, str_ptr, str_val) | |
| rt.store.i32_n8(memory_at_0, str_ptr + str_len, 0) | |
| rt.store.i32(memory_at_0, array_ptr + (idx * 4), str_ptr) | |
| end | |
| rt.store.i32(memory_at_0, array_ptr + (array_len * 4), 0) | |
| return array_ptr | |
| end | |
| function module.free_array(array_ptr: number) | |
| local position = 0 | |
| while true do | |
| local str_ptr = rt.load.i32(array_ptr + position) | |
| if str_ptr == 0 then | |
| break | |
| end | |
| cfns.free(str_ptr) | |
| position += 4 | |
| end | |
| cfns.free(array_ptr) | |
| end | |
| function module.newCException(excPtr: number) | |
| local CException = {} | |
| CException.ptr = excPtr | |
| function CException._UndecoratedName(self) | |
| local name_ptr = rt.load.i32(memory_at_0, self.ptr + 4) | |
| return rt.load.string(memory_at_0, name_ptr, cfns.strlen(name_ptr)) | |
| end | |
| function CException.vfptr(self) | |
| return rt.load.i32(memory_at_0, self.ptr) | |
| end | |
| return CException | |
| end | |
| return module, function(_rt: any, _memory_at_0: any, _cfns: any, _INDIRECT_FUNCTIONS: any) | |
| rt, memory_at_0, cfns, INDIRECT_FUNCTIONS = _rt, _memory_at_0, _cfns, _INDIRECT_FUNCTIONS | |
| end | |
| end | |
| end | |
| function __LUAUCEPTION__.b() | |
| return function(rt: any, memory_at_0: any, cfns: any, INDIRECT_FUNCTIONS: any, utils: typeof(__LUAUCEPTION__['a']()())) | |
| local module = {} | |
| function module.luau_setflag(name: string, state: boolean) | |
| local flagNamePtr = utils.cstr(name) | |
| cfns.set_flag(flagNamePtr, if(state)then 1 else 0) | |
| cfns.free(flagNamePtr) | |
| return | |
| end | |
| function module.luau_setallflags(state: boolean) | |
| cfns.set_all_flags(if(state)then 1 else 0) | |
| return | |
| end | |
| function module.luau_resetflags() | |
| cfns.reset_flags() | |
| return | |
| end | |
| return module | |
| end | |
| end | |
| function __LUAUCEPTION__.c() | |
| local task = task or require('@lune/task') | |
| return function(utils: typeof(__LUAUCEPTION__['a']()())) | |
| local rt, memory_at_0, cfns, INDIRECT_FUNCTIONS | |
| local polyfill = {} | |
| local START_CLOCK, NOW_TIME = os.clock(), os.time() * 1000 | |
| local FILE_MAP = {{}, {}} | |
| local LONGJMP_THROW_SIGN = newproxy(false) | |
| local __TIMERS = {} | |
| local __EXIT_STATUS | |
| local __ABORT_WASM = false | |
| local __NO_EXIT_RUNTIME = true | |
| local __RUNTIME_KEEPALIVE_COUNTER = 0 | |
| local function fd_flush(file: {string}) | |
| local final = table.concat(file) | |
| local last = 1 | |
| for index, data in string.gmatch(final, '()([^\n]*)\n')do | |
| last = index + #data | |
| print(data) | |
| end | |
| table.clear(file) | |
| file[1] = string.sub(final, last + 1) | |
| end | |
| local function now_clock(): number | |
| return os.clock() - START_CLOCK | |
| end | |
| local function invokeHandler(index: number) | |
| return function(fnIndex: number, ...) | |
| local stack_ptr = cfns.emscripten_stack_get_current() | |
| local err, stacktrace = 'N/A', 'N/A' | |
| local success = xpcall(INDIRECT_FUNCTIONS.data[fnIndex], function(_e) | |
| err, stacktrace = _e, debug.traceback() | |
| end, ...) | |
| if success then | |
| return | |
| end | |
| cfns._emscripten_stack_restore(stack_ptr) | |
| if err ~= LONGJMP_THROW_SIGN then | |
| error(`{err}\n\ninvoke stacktrace:\n{stacktrace}`, 2) | |
| end | |
| cfns.setThrew(1, 0) | |
| end | |
| end | |
| polyfill.invoke_vii = invokeHandler(1) | |
| polyfill._tzset_js = utils.stub('_tzset_js', 0) | |
| function polyfill.__cxa_throw(ptr, type, destructor) | |
| local info = utils.newCException(type) | |
| assert(false, `exception occured in c: {info:_UndecoratedName()} ({info:vfptr()})`) | |
| end | |
| function polyfill._abort_js(reason: any) | |
| assert(false, `Aborted({reason})`) | |
| end | |
| function polyfill.emscripten_date_now() | |
| return NOW_TIME + now_clock() * 1000 | |
| end | |
| function polyfill._emscripten_throw_longjmp() | |
| return error(LONGJMP_THROW_SIGN, 0) | |
| end | |
| function polyfill._emscripten_runtime_keepalive_clear() | |
| __NO_EXIT_RUNTIME, __RUNTIME_KEEPALIVE_COUNTER = false, 0 | |
| end | |
| function polyfill.emscripten_resize_heap(_requestedSize: number) | |
| assert(false, 'OOM') | |
| end | |
| function polyfill._setitimer_js(which, timeoutMs) | |
| if (not task) then | |
| print([[program is using _setitimer_js but task library not found, timers will NOT work.]]) | |
| return 0 | |
| end | |
| if (__TIMERS[which]) then | |
| task.cancel(__TIMERS[which].thread) | |
| __TIMERS[which] = nil | |
| end | |
| if (not timeoutMs or timeoutMs == 0) then | |
| return 0 | |
| end | |
| local thread = task.delay(function() | |
| __TIMERS[which] = nil | |
| if (__ABORT_WASM) then | |
| return | |
| end | |
| local success, err = pcall(function() | |
| cfns._emscripten_timeout(which, now_clock()) | |
| if (__NO_EXIT_RUNTIME or __RUNTIME_KEEPALIVE_COUNTER > 0) then | |
| return | |
| end | |
| local exit_success, exit_err = pcall(polyfill.proc_exit, __EXIT_STATUS) | |
| if exit_success then | |
| return | |
| end | |
| if (string.match(exit_err, 'ExitStatus:') ~= nil or exit_err == 'unwind') then | |
| return __EXIT_STATUS | |
| end | |
| assert(false, exit_err) | |
| end) | |
| if (not success) then | |
| if (string.match(err, 'ExitStatus:') ~= nil or err == 'unwind') then | |
| return | |
| end | |
| assert(false, err) | |
| end | |
| end, timeoutMs / 1000) | |
| __TIMERS[which] = { | |
| thread = thread, | |
| timeoutMs = timeoutMs, | |
| } | |
| return 0 | |
| end | |
| function polyfill._localtime_js(timer, buf) | |
| local tdata = os.date('*t', rt.i64.into_u64(timer)) | |
| rt.store.i32(memory_at_0, buf + 0, tdata.sec) | |
| rt.store.i32(memory_at_0, buf + 4, tdata.min) | |
| rt.store.i32(memory_at_0, buf + 8, tdata.hour) | |
| rt.store.i32(memory_at_0, buf + 12, tdata.day) | |
| rt.store.i32(memory_at_0, buf + 16, tdata.month - 1) | |
| rt.store.i32(memory_at_0, buf + 20, tdata.year - 1900) | |
| rt.store.i32(memory_at_0, buf + 24, tdata.wday) | |
| rt.store.i32(memory_at_0, buf + 28, tdata.yday) | |
| rt.store.i32(memory_at_0, buf + 32, if tdata.isdst then 1 else 0) | |
| end | |
| function polyfill._gmtime_js(timer, buf) | |
| local tdata = os.date('!*t', rt.i64.into_u64(timer)) | |
| rt.store.i32(memory_at_0, buf + 0, tdata.sec) | |
| rt.store.i32(memory_at_0, buf + 4, tdata.min) | |
| rt.store.i32(memory_at_0, buf + 8, tdata.hour) | |
| rt.store.i32(memory_at_0, buf + 12, tdata.day) | |
| rt.store.i32(memory_at_0, buf + 16, tdata.month - 1) | |
| rt.store.i32(memory_at_0, buf + 20, tdata.year - 1900) | |
| rt.store.i32(memory_at_0, buf + 24, tdata.wday) | |
| rt.store.i32(memory_at_0, buf + 28, tdata.yday) | |
| rt.store.i32(memory_at_0, buf + 32, if tdata.isdst then 1 else 0) | |
| end | |
| function polyfill.clock_time_get(clock_id: number, ignored_precision: boolean, ptime: number) | |
| if not (clock_id >= 0 and clock_id <= 3) then | |
| return 28 | |
| end | |
| local time_now | |
| if clock_id == 0 then | |
| time_now = polyfill.emscripten_date_now() | |
| else | |
| time_now = now_clock() | |
| end | |
| local nsec = math.round(time_now * 1000 * 1000) | |
| rt.store.i64(memory_at_0, ptime, rt.i64.from_u64(nsec)) | |
| return 0 | |
| end | |
| function polyfill.fd_write(fd, list, count, ret_pointer) | |
| local file = FILE_MAP[fd] | |
| if not file then | |
| return 8 | |
| end | |
| local total = 0 | |
| for pointer = list, list + (count - 1) * 8, 8 do | |
| local start = rt.load.i32(memory_at_0, pointer) | |
| local len = rt.load.i32(memory_at_0, pointer + 4) | |
| local read = rt.load.string(memory_at_0, start, len) | |
| table.insert(file, read) | |
| total = total + #read | |
| end | |
| rt.store.i32(memory_at_0, ret_pointer, total) | |
| fd_flush(file) | |
| return 0 | |
| end | |
| function polyfill.proc_exit(code: number) | |
| __EXIT_STATUS = code | |
| if (not (__NO_EXIT_RUNTIME or __RUNTIME_KEEPALIVE_COUNTER > 0)) then | |
| __ABORT_WASM = true | |
| end | |
| assert(false, `ExitStatus: Program exited with code ${code}`) | |
| end | |
| return polyfill, function(_rt: any, _memory_at_0: any, _cfns: any, _INDIRECT_FUNCTIONS: any) | |
| rt, memory_at_0, cfns, INDIRECT_FUNCTIONS = _rt, _memory_at_0, _cfns, _INDIRECT_FUNCTIONS | |
| end | |
| end | |
| end | |
| function __LUAUCEPTION__.d() | |
| local runtime_initialize = require('@self/runtime') | |
| local polyfill_create = __LUAUCEPTION__['c']() | |
| local utils_create = __LUAUCEPTION__['a']() | |
| return function() | |
| local utils, utils_ready = utils_create() | |
| local polyfill, polyfill_ready = polyfill_create(utils) | |
| local common_imports = {func_list = polyfill} | |
| local RuntimeInstance = runtime_initialize({ | |
| env = common_imports, | |
| wasi_snapshot_preview1 = common_imports, | |
| }) | |
| local rt, memory_at_0, cfns, INDIRECT_FUNCTIONS = RuntimeInstance.rt, RuntimeInstance.memory_list.memory, RuntimeInstance.func_list, RuntimeInstance.table_list.__indirect_function_table | |
| polyfill_ready(rt, memory_at_0, cfns, INDIRECT_FUNCTIONS) | |
| utils_ready(rt, memory_at_0, cfns, INDIRECT_FUNCTIONS) | |
| return rt, memory_at_0, cfns, INDIRECT_FUNCTIONS, utils, polyfill | |
| end | |
| end | |
| function __LUAUCEPTION__.e() | |
| return function(rt: any, memory_at_0: any, cfns: any, INDIRECT_FUNCTIONS: any, utils: typeof(__LUAUCEPTION__['a']()())) | |
| local module = {} | |
| local function construct_luau_CompileOptions(options: LuauCompileOptions__DARKLUA_TYPE_a): (number,() -> ()) | |
| local compileOptions_ptr = cfns.malloc(52) | |
| local __freed = false | |
| local vectorLibName_ptr = if options.VectorLibName then utils.cstr(options.VectorLibName)else 0 | |
| local vectorLibConstructor_ptr = if options.VectorLibConstructor then utils.cstr(options.VectorLibConstructor)else 0 | |
| local vectorType_ptr = if options.VectorType then utils.cstr(options.VectorType)else 0 | |
| local mutableGlobals_ptr = if options.MutableGlobals then utils.cstr_array(options.MutableGlobals)else 0 | |
| local disabledBuiltins_ptr = if options.DisabledBuiltins then utils.cstr_array(options.DisabledBuiltins)else 0 | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 0, options.OptimizationLevel) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 4, options.DebugLevel) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 8, options.TypeInfoLevel) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 12, options.CoverageLevel) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 16, vectorLibName_ptr) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 20, vectorLibConstructor_ptr) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 24, vectorType_ptr) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 28, mutableGlobals_ptr) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 32, 0) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 36, 0) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 40, 0) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 44, 0) | |
| rt.store.i32(memory_at_0, compileOptions_ptr + 48, disabledBuiltins_ptr) | |
| return compileOptions_ptr, function() | |
| if __freed then | |
| error('Already freed!') | |
| end | |
| __freed = true | |
| cfns.free(compileOptions_ptr) | |
| if vectorLibName_ptr ~= 0 then | |
| cfns.free(vectorLibName_ptr) | |
| end | |
| if vectorLibConstructor_ptr ~= 0 then | |
| cfns.free(vectorLibConstructor_ptr) | |
| end | |
| if vectorType_ptr ~= 0 then | |
| cfns.free(vectorType_ptr) | |
| end | |
| if disabledBuiltins_ptr ~= 0 then | |
| cfns.free(disabledBuiltins_ptr) | |
| end | |
| if mutableGlobals_ptr ~= 0 then | |
| cfns.free(mutableGlobals_ptr) | |
| end | |
| end | |
| end | |
| function module.luau_compile(source: string, options: LuauCompileOptions__DARKLUA_TYPE_a): (string,number) | |
| local luau_CompileOptions_ptr, compileOptions_free = construct_luau_CompileOptions(options) | |
| local source_ptr = utils.cstr(source) | |
| local bytecodeSize_ptr = cfns.malloc(8) | |
| local bytecode_ptr = cfns.luau_compile(source_ptr, #source, luau_CompileOptions_ptr, bytecodeSize_ptr) | |
| local bytecodeSize = rt.load.i32(memory_at_0, bytecodeSize_ptr) | |
| local bytecode = rt.load.string(memory_at_0, bytecode_ptr, bytecodeSize) | |
| compileOptions_free() | |
| cfns.free(source_ptr) | |
| cfns.free(bytecode_ptr) | |
| cfns.free(bytecodeSize_ptr) | |
| return bytecode, bytecodeSize | |
| end | |
| return module | |
| end | |
| end | |
| end | |
| local api_flags_create = __LUAUCEPTION__['b']() | |
| local runtime_create = __LUAUCEPTION__['d']() | |
| local compiler_common_init = __LUAUCEPTION__['e']() | |
| export type LuauCompileOptions = LuauCompileOptions__DARKLUA_TYPE_a | |
| local rt, memory_at_0, cfns, INDIRECT_FUNCTIONS, utils = runtime_create() | |
| local api_flags = api_flags_create(rt, memory_at_0, cfns, INDIRECT_FUNCTIONS, utils) | |
| local compiler_common = compiler_common_init(rt, memory_at_0, cfns, INDIRECT_FUNCTIONS, utils) | |
| return { | |
| luau_compile = compiler_common.luau_compile, | |
| luau_setflag = api_flags.luau_setflag, | |
| luau_setallflags = api_flags.luau_setallflags, | |
| luau_resetflags = api_flags.luau_resetflags, | |
| wasm = { | |
| rt = rt, | |
| cfns = cfns, | |
| indirect_function_table = INDIRECT_FUNCTIONS, | |
| memory = memory_at_0, | |
| insertCFunction = utils.insertCFunction, | |
| cstr = utils.cstr, | |
| }, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment