diff --git a/packages/codemode/lua/LICENSE.lua b/packages/codemode/lua/LICENSE.lua deleted file mode 100644 index a86026803a0..00000000000 --- a/packages/codemode/lua/LICENSE.lua +++ /dev/null @@ -1,34 +0,0 @@ -Lua License ------------ - -Lua is licensed under the terms of the MIT license reproduced below. -This means that Lua is free software and can be used for both academic -and commercial purposes at absolutely no cost. - -For details and rationale, see http://www.lua.org/license.html . - -=============================================================================== - -Copyright (C) 1994-2012 Lua.org, PUC-Rio. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=============================================================================== - -(end of COPYRIGHT) diff --git a/packages/codemode/lua/Makefile b/packages/codemode/lua/Makefile deleted file mode 100644 index 0c71c2f9122..00000000000 --- a/packages/codemode/lua/Makefile +++ /dev/null @@ -1,64 +0,0 @@ -# Standalone experiment; deliberately not part of the package build. -BUILD := .build -SRC := $(BUILD)/lua-5.1.5/src -SUITE := $(BUILD)/lua5.1-tests -CORE := $(SRC)/liblua.a -CLI := $(SRC)/lua -DEBUG := $(BUILD)/debug/lua-5.1.5/src -.DELETE_ON_ERROR: -.PHONY: all cli test test-c conformance conformance-c test-debug -all: test -test: - node --test test/*.test.mjs - -conformance: $(SUITE)/.prepared - node test/upstream.mjs $(SUITE) - -# Optional C comparison targets; never used by index.js or the JS tests. -cli: $(CLI) - -$(BUILD)/source.tgz: - mkdir -p $(BUILD) - curl -fsSL https://www.lua.org/ftp/lua-5.1.5.tar.gz -o $@ - -$(SRC)/value-ops.inc: $(BUILD)/source.tgz - echo '2640fc56a795f29d28ef15e13c34a47e223960b0240e8cb0a82d9b0738695333 $<' | shasum -a 256 -c - - tar xzf $< -C $(BUILD) - sed '/^#define runtime_check/,$$d' $(SRC)/lvm.c > $@ - -$(CORE): $(SRC)/value-ops.inc vm.c - cp vm.c $(SRC)/lvm.c - $(MAKE) -C $(SRC) CC="$(CC)" MYCFLAGS="-std=gnu99 -DLUA_USE_POSIX" liblua.a - -$(CLI): $(CORE) - $(MAKE) -C $(SRC) CC="$(CC)" MYCFLAGS="-std=gnu99 -DLUA_USE_POSIX" lua - -test-c: $(CLI) - $(CLI) test/vm.lua - -$(SUITE)/.prepared: Makefile - mkdir -p $(BUILD) - curl -fsSL https://www.lua.org/tests/lua5.1-tests.tar.gz -o $(BUILD)/tests.tgz - echo '49e4ca6561f82ea605908c5041ab5fad66ed9930fa0686675bd51b02767f18ad $(BUILD)/tests.tgz' | shasum -a 256 -c - - tar xzf $(BUILD)/tests.tgz -C $(BUILD) - # The 2006 suite expects a 4 GiB concatenation to overflow size_t. - # On 64-bit machines it allocates instead. Guard only that assertion block. - awk 'NR==1 {print "if string.dump(function() end):byte(9) == 4 then"} {print} NR==12 {print "else print(\"skipping 32-bit size overflow assertion\") end"}' $(SUITE)/big.lua > $(BUILD)/big.lua - mv $(BUILD)/big.lua $(SUITE)/big.lua - touch $@ - -conformance-c: $(CLI) $(SUITE)/.prepared - cd $(SUITE) && LUA_INIT= LUA_PATH='?;./?.lua' "$(abspath $(CLI))" all.lua - -# The internal API/allocator checks need a different ABI. Never link the -# test-only T library into the normal artifact. -$(DEBUG)/lua: vm.c $(SRC)/value-ops.inc $(SUITE)/.prepared - mkdir -p $(BUILD)/debug - tar xzf $(BUILD)/source.tgz -C $(BUILD)/debug - cp $(SRC)/value-ops.inc $(SUITE)/etc/ltests.[ch] $(DEBUG)/ - cp vm.c $(DEBUG)/lvm.c - $(MAKE) -C $(DEBUG) CC="$(CC)" MYCFLAGS='-std=gnu99 -DLUA_USE_POSIX -DLUA_USER_H=\"ltests.h\"' LUA_O='lua.o ltests.o' lua - -test-debug: $(DEBUG)/lua - $(DEBUG)/lua test/vm.lua - cd $(SUITE) && LUA_INIT= LUA_PATH='?;./?.lua' "$(abspath $(DEBUG)/lua)" -e 'dofile("api.lua"); dofile("closure.lua"); dofile("db.lua")' diff --git a/packages/codemode/lua/README.md b/packages/codemode/lua/README.md index 72961b43f9b..48045e14640 100644 --- a/packages/codemode/lua/README.md +++ b/packages/codemode/lua/README.md @@ -9,7 +9,8 @@ bun install --frozen-lockfile --filter @opencode/lua-experiment --ignore-scripts cd packages/codemode/lua node --input-type=module -e "import {runLua} from './index.js'; console.log(runLua('return 1, nil, false'))" # [ 1, null, false ] -make test +bun run test +bun run conformance ``` The result is an array, not just the first Lua value. `return` produces `[]`; `return nil` produces `[null]`. Numbers, booleans, and strings cross directly. Strings (including the source) use the byte-string convention: one JS code unit per byte, `0`–`255`. To load a UTF-8 file without changing its bytes, read it with Node's `latin1` encoding. Returning a table, function or thread throws a `TypeError` instead of leaking a VM object. Uncaught Lua errors throw; Lua `pcall` and `coroutine.resume` preserve error objects inside the VM. @@ -24,14 +25,10 @@ The result is an array, not just the first Lua value. `return` produces `[]`; `r ## Compatibility and tests -`make test` runs the JavaScript regression suite, including the original `test/vm.lua`. That fixture exercises 100,000 tail calls with captured locals, nil-bearing return lists, coroutine resumption, recursive metamethods, a 26,001-element constructor, `loadstring` and chunk/line errors. +`bun run test` runs the JavaScript regression suite with Node, including the original `test/vm.lua`. That fixture exercises 100,000 tail calls with captured locals, nil-bearing return lists, coroutine resumption, recursive metamethods, a 26,001-element constructor, `loadstring` and chunk/line errors. -`make conformance` downloads the checksum-pinned official Lua 5.1 tests and runs **a subset** on JavaScript: `vararg.lua`, `pm.lua`, `locals.lua` (checking its first result inside Lua), and `math.lua` in its upstream `_soft` mode (skips its file-I/O section). This is **not** full Lua 5.1.5 conformance. In particular: +`bun run conformance` reads the checksum-pinned [official Lua 5.1 test archive](https://www.lua.org/tests/) in memory. To run offline, pass a local copy: `bun run conformance /path/to/lua5.1-tests.tar.gz`. It runs `vararg.lua`, `pm.lua`, `locals.lua` (checking its first result inside Lua), and `math.lua` in upstream `_soft` mode. That mode returns at line 122, before the file I/O, NaN, coercion and random-number tests that follow. This is **not** full Lua 5.1.5 conformance. In particular: - No `io`, `os`, `package`/`require`, `debug`, C API, bytecode loading or `string.dump`. `load`/`loadstring` accept text; `print` writes to the host console. - GC belongs to JavaScript. `collectgarbage` is a compatibility stub; weak tables, finalizers and Lua allocator behavior aren't implemented. - Parser limits, stack limits, diagnostics, locale behavior and some library argument checks and numeric edge cases differ from PUC Lua. The interpreter has no instruction, time or allocation budget. Run **trusted** code only; this is not a sandbox. - -### Optional native comparison - -`vm.c` is the branch's earlier Lua 5.1.5 register-machine experiment. It is retained for developer comparisons only, and is never built or invoked by the JavaScript implementation. `make cli`, `make test-c`, `make conformance-c` and `make test-debug` are the separate C targets. They require a POSIX toolchain and download checksum-pinned Lua sources under `.build/`; see [LICENSE.lua](LICENSE.lua). The C suite skips a 32-bit-only overflow assertion on 64-bit hosts; its results are not JavaScript compatibility evidence. diff --git a/packages/codemode/lua/package.json b/packages/codemode/lua/package.json index be1e94b425d..1c9c965b057 100644 --- a/packages/codemode/lua/package.json +++ b/packages/codemode/lua/package.json @@ -3,7 +3,8 @@ "private": true, "type": "module", "scripts": { - "test": "node --test test/*.test.mjs" + "test": "node --test test/*.test.mjs", + "conformance": "bun test/upstream.mjs" }, "dependencies": { "luaparse": "0.3.1", diff --git a/packages/codemode/lua/test/upstream.mjs b/packages/codemode/lua/test/upstream.mjs index 02ab8c88f1e..cf492f1977a 100644 --- a/packages/codemode/lua/test/upstream.mjs +++ b/packages/codemode/lua/test/upstream.mjs @@ -1,12 +1,24 @@ import assert from "node:assert/strict" -import { readFileSync } from "node:fs" -import { join } from "node:path" +import { pathToFileURL } from "node:url" import { runLua } from "../index.js" +const response = await fetch( + process.argv[2] ? pathToFileURL(process.argv[2]) : "https://www.lua.org/tests/lua5.1-tests.tar.gz", +) +if (!response.ok) throw new Error(`Lua test suite: ${response.status} ${response.statusText}`) +const bytes = await response.arrayBuffer() +assert.equal( + Bun.SHA256.hash(bytes, "hex"), + "49e4ca6561f82ea605908c5041ab5fad66ed9930fa0686675bd51b02767f18ad", + "unexpected Lua test archive", +) +const names = ["vararg", "pm", "locals", "math"] +const files = await new Bun.Archive(bytes).files(`lua5.1-tests/{${names.join(",")}}.lua`) + // Run source unchanged in fresh globals. locals.lua also returns a closure; // check its first result in Lua rather than exporting that closure to JS. -for (const name of ["vararg", "pm", "locals", "math"]) { - const source = readFileSync(join(process.argv[2], `${name}.lua`), "latin1") +for (const name of names) { + const source = Buffer.from(await files.get(`lua5.1-tests/${name}.lua`).arrayBuffer()).toString("latin1") const prefix = name === "math" ? "_soft = true\n" : "" const result = runLua( name === "locals" ? `local function suite()\n${source}\nend\nreturn (suite())` : prefix + source, diff --git a/packages/codemode/lua/vm.c b/packages/codemode/lua/vm.c deleted file mode 100644 index 2807291526a..00000000000 --- a/packages/codemode/lua/vm.c +++ /dev/null @@ -1,212 +0,0 @@ -/* Lua 5.1 register machine. The generated include contains Lua's unchanged - * value/metamethod operations, but NOT its interpreter loop (see Makefile). - * Lua data structures and instruction encoding: Copyright (C) 1994-2012 - * Lua.org, PUC-Rio. See LICENSE.lua. */ -#include "value-ops.inc" - -static TValue *operand(lua_State *L, LClosure *fn, int rk) { - return ISK(rk) ? &fn->p->k[INDEXK(rk)] : &L->base[rk]; -} - -/* A conditional instruction owns the JMP word that follows it. */ -static const Instruction *branch(const Instruction *pc, int take) { - return pc + 1 + (take ? GETARG_sBx(*pc) : 0); -} - -static void replace_frame(lua_State *L) { - CallInfo *next = L->ci, *prev = next - 1; - ptrdiff_t size = L->top - next->func; - luaF_close(L, prev->base); - L->base = prev->base = prev->func + (next->base - next->func); - memmove(prev->func, next->func, size * sizeof(TValue)); - L->top = prev->top = prev->func + size; - prev->savedpc = L->savedpc; - prev->tailcalls++; - L->ci = prev; -} - -static const Instruction *capture(lua_State *L, LClosure *parent, Proto *p, - int dest, const Instruction *pc) { - Closure *fn = luaF_newLclosure(L, p->nups, parent->env); - fn->l.p = p; - for (int j = 0; j < p->nups; ++j) { - Instruction bind = *pc++; - int src = GETARG_B(bind); - fn->l.upvals[j] = GET_OPCODE(bind) == OP_MOVE - ? luaF_findupval(L, L->base + src) : parent->upvals[src]; - } - setclvalue(L, L->base + dest, fn); - luaC_checkGC(L); - return pc; -} - -static void varargs(lua_State *L, int dest, int count) { - int supplied = cast_int(L->base - L->ci->func) - 1 - ci_func(L->ci)->l.p->numparams; - if (count == LUA_MULTRET) { - count = supplied; - luaD_checkstack(L, count); - L->top = L->base + dest + count; - } - for (int j = 0; j < count; ++j) { - TValue *slot = L->base + dest + j; - if (j < supplied) { setobj(L, slot, L->base - supplied + j); } - else { setnilvalue(slot); } - } -} - -static void fill_list(lua_State *L, StkId start, int count, int page) { - if (!ttistable(start)) luaG_typeerror(L, start, "index"); - Table *table = hvalue(start); - if (!count) { - count = cast_int(L->top - start) - 1; - L->top = L->ci->top; - } - int offset = (page - 1) * LFIELDS_PER_FLUSH; - if (offset + count > table->sizearray) luaH_resizearray(L, table, offset + count); - for (int j = 1; j <= count; ++j) { - setobj2t(L, luaH_setnum(L, table, offset + j), start + j); - luaC_barriert(L, table, start + j); - } -} - -void luaV_execute(lua_State *L, int frames) { - const Instruction *pc = L->savedpc; - for (;;) { - Instruction i = *pc++; - if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && - (--L->hookcount == 0 || (L->hookmask & LUA_MASKLINE))) { - traceexec(L, pc); - if (L->status == LUA_YIELD) { L->savedpc = pc - 1; return; } - } - /* Publish the fault location before anything that can call back into Lua. - * A call or GC step may move the stack: don't cache registers across it. */ - L->savedpc = pc; - LClosure *fn = &ci_func(L->ci)->l; - OpCode op = GET_OPCODE(i); - int a = GETARG_A(i), b = GETARG_B(i), c = GETARG_C(i); - #define R(n) (L->base + (n)) - #define RK(n) operand(L, fn, (n)) - switch (op) { - case OP_MOVE: setobj(L, R(a), R(b)); break; - case OP_LOADK: setobj(L, R(a), fn->p->k + GETARG_Bx(i)); break; - case OP_LOADBOOL: setbvalue(R(a), b); pc += (c != 0); break; - case OP_LOADNIL: - for (int j = a; j <= b; ++j) { setnilvalue(R(j)); } - break; - case OP_GETUPVAL: setobj(L, R(a), fn->upvals[b]->v); break; - case OP_SETUPVAL: - setobj(L, fn->upvals[b]->v, R(a)); - luaC_barrier(L, fn->upvals[b], R(a)); - break; - case OP_GETGLOBAL: case OP_SETGLOBAL: { - TValue env; - TValue *key = fn->p->k + GETARG_Bx(i); - sethvalue(L, &env, fn->env); - if (op == OP_GETGLOBAL) luaV_gettable(L, &env, key, R(a)); - else luaV_settable(L, &env, key, R(a)); - break; - } - case OP_SELF: - setobj(L, R(a+1), R(b)); - /* fall through: the method lookup is a GETTABLE */ - case OP_GETTABLE: luaV_gettable(L, R(b), RK(c), R(a)); break; - case OP_SETTABLE: luaV_settable(L, R(a), RK(b), RK(c)); break; - case OP_NEWTABLE: - sethvalue(L, R(a), luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); - luaC_checkGC(L); - break; - case OP_SETLIST: - fill_list(L, R(a), b, c ? c : cast_int(*pc++)); - break; - case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_MOD: case OP_POW: - /* Lua orders these six instructions identically to their tag methods. */ - Arith(L, R(a), RK(b), RK(c), (TMS)(TM_ADD + (op - OP_ADD))); - break; - case OP_UNM: Arith(L, R(a), R(b), R(b), TM_UNM); break; - case OP_NOT: { int v = l_isfalse(R(b)); setbvalue(R(a), v); break; } - case OP_LEN: { - TValue *src = R(b); - if (ttisstring(src)) { setnvalue(R(a), tsvalue(src)->len); } - else if (ttistable(src)) { setnvalue(R(a), luaH_getn(hvalue(src))); } - else if (!call_binTM(L, src, luaO_nilobject, R(a), TM_LEN)) luaG_typeerror(L, src, "get length of"); - break; - } - case OP_CONCAT: - luaV_concat(L, c - b + 1, c); - setobj(L, R(a), R(b)); - luaC_checkGC(L); - break; - case OP_JMP: pc += GETARG_sBx(i); break; - case OP_EQ: case OP_LT: case OP_LE: { - TValue *x = RK(b), *y = RK(c); - int result = op == OP_EQ ? equalobj(L, x, y) : - op == OP_LT ? luaV_lessthan(L, x, y) : lessequal(L, x, y); - pc = branch(pc, result == a); - break; - } - case OP_TEST: case OP_TESTSET: { - TValue *src = R(op == OP_TEST ? a : b); - int take = l_isfalse(src) != c; - if (take && op == OP_TESTSET) { setobj(L, R(a), src); } - pc = branch(pc, take); - break; - } - case OP_CALL: case OP_TAILCALL: { - int results = op == OP_TAILCALL ? LUA_MULTRET : c - 1; - if (b) L->top = R(a+b); - int status = luaD_precall(L, R(a), results); - if (status == PCRYIELD) return; - if (status == PCRLUA) { - if (op == OP_TAILCALL) replace_frame(L); - else ++frames; - pc = L->savedpc; - } else if (results != LUA_MULTRET) L->top = L->ci->top; - break; - } - case OP_RETURN: { - if (b) L->top = R(a+b-1); - luaF_close(L, L->base); - int fixed = luaD_poscall(L, R(a)); - if (--frames == 0) return; - if (fixed) L->top = L->ci->top; - pc = L->savedpc; - break; - } - case OP_FORPREP: { - static const char *const names[] = { "initial value", "limit", "step" }; - for (int j = 0; j < 3; ++j) { - TValue *v = R(a+j); - if (!luaV_tonumber(v, v)) luaG_runerror(L, "'for' %s must be a number", names[j]); - } - setnvalue(R(a), nvalue(R(a)) - nvalue(R(a+2))); - pc += GETARG_sBx(i); - break; - } - case OP_FORLOOP: { - lua_Number step = nvalue(R(a+2)), limit = nvalue(R(a+1)); - lua_Number next = nvalue(R(a)) + step; - if (step > 0 ? next <= limit : next >= limit) { - setnvalue(R(a), next); - setnvalue(R(a+3), next); - pc += GETARG_sBx(i); - } - break; - } - case OP_TFORLOOP: { - for (int j = 0; j < 3; ++j) { setobj(L, R(a+3+j), R(a+j)); } - L->top = R(a+6); - luaD_call(L, R(a+3), c); - L->top = L->ci->top; - int more = !ttisnil(R(a+3)); - if (more) { setobj(L, R(a+2), R(a+3)); } - pc = branch(pc, more); - break; - } - case OP_CLOSE: luaF_close(L, R(a)); break; - case OP_CLOSURE: pc = capture(L, fn, fn->p->p[GETARG_Bx(i)], a, pc); break; - case OP_VARARG: varargs(L, a, b-1); break; - } - #undef R - #undef RK - } -}