mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
feat(codemode): add standalone Lua 5.1 interpreter experiment
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
This is the checkable support matrix for CodeMode's confined JavaScript interpreter. It tracks the language and
|
||||
standard-library surface that programs can use today, plus concrete gaps that may be implemented later.
|
||||
|
||||
There is also a [standalone Lua 5.1 interpreter experiment](lua/README.md). It is not selectable in Code Mode and does not change the JavaScript support described here.
|
||||
|
||||
- `[x]` means the feature is implemented at the scope described here.
|
||||
- `[ ]` means a concrete compatibility gap remains.
|
||||
- Checked items do not promise complete ECMAScript edge-case parity; known differences are stated explicitly.
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/.build/
|
||||
@@ -0,0 +1,34 @@
|
||||
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)
|
||||
@@ -0,0 +1,57 @@
|
||||
# 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 conformance test-debug
|
||||
all: $(CORE)
|
||||
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: $(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: $(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")'
|
||||
@@ -0,0 +1,26 @@
|
||||
# Lua interpreter experiment
|
||||
|
||||
Standalone implementation of the **Lua 5.1** instruction set, targeting **5.1.5**. It is not a JavaScript translation and does not use an installed Lua interpreter. No Code Mode entrypoint imports it.
|
||||
|
||||
`vm.c` owns the fetch/decode/execute loop for all 38 opcodes. Lua-to-Lua calls iterate over register frames rather than recursing in C. Tail calls replace the caller's frame after closing captured registers; yields return control to the coroutine runtime. Calls, varargs and returns preserve Lua's multiple-result convention, including trailing nils.
|
||||
|
||||
The front end and runtime are **reused, not rewritten**: the build compiles the MIT-licensed Lua 5.1.5 lexer, parser, bytecode generator, tables, GC, coroutine machinery, C API and libraries. It extracts the value/coercion/metamethod helpers from upstream `lvm.c`, then builds our executor **instead of** the upstream instruction loop. Both source archives are checksum-pinned in the Makefile. See [LICENSE.lua](LICENSE.lua).
|
||||
|
||||
## Try it
|
||||
|
||||
From `packages/codemode/lua`, on a POSIX system with a C99 compiler, Make, curl, tar and shasum:
|
||||
|
||||
```sh
|
||||
make # .build/lua-5.1.5/src/liblua.a (no libraries opened automatically)
|
||||
make cli # developer CLI with the standard libraries opened
|
||||
.build/lua-5.1.5/src/lua -e 'local function f(...) return ... end; print(f(1,nil,3))'
|
||||
make test # targeted VM regressions
|
||||
make conformance # upstream language/standard-library suite
|
||||
make test-debug # allocator, C API, coroutine and hook tests with upstream test instrumentation
|
||||
```
|
||||
|
||||
The first build downloads source; subsequent runs use `.build/`. The conformance target uses the 2006 official 5.1 suite. On a 64-bit host it skips the assertion that concatenating 4 GiB overflows `size_t`; the rest of `big.lua` still runs. Other platform/optional skips are reported by the suite (locales, dynamic C modules, internal test library). `test-debug` enables the internal test library separately. Its `code.lua` compiler-shape assertions are not a 5.1.5 compatibility gate: the unmodified 5.1.5 reference compiler also fails the expected instruction sequence at line 45.
|
||||
|
||||
## Boundary
|
||||
|
||||
This is an interpreter experiment, **not a sandbox**. The developer CLI is for trusted scripts: it has normal Lua file/process APIs and accepts binary chunks. No resource budget, host-value bridge, tool bindings, or Code Mode language switch is implemented. Lua 5.2+ syntax and semantics are outside this version target.
|
||||
@@ -0,0 +1,38 @@
|
||||
-- A tail call must close captured registers before reusing their frame.
|
||||
local saved = {}
|
||||
local function descend(n, ...)
|
||||
if n == 0 then return ... end
|
||||
if n % 1000 == 0 then saved[#saved+1] = function() return n end end
|
||||
return descend(n-1, ...)
|
||||
end
|
||||
local function pack(...) return {n = select('#', ...), ...} end
|
||||
local results = pack(descend(100000, nil, 'middle', nil))
|
||||
assert(results.n == 3 and results[2] == 'middle')
|
||||
assert(saved[1]() == 100000 and saved[#saved]() == 1000)
|
||||
|
||||
-- Resume a yielded C call with open results across a Lua tail call.
|
||||
local thread = coroutine.create(function(...)
|
||||
return descend(10, coroutine.yield(...))
|
||||
end)
|
||||
local ok, first = coroutine.resume(thread, 7)
|
||||
assert(ok and first == 7)
|
||||
results = pack(coroutine.resume(thread, false, nil, 9))
|
||||
assert(results.n == 4 and results[1] and results[2] == false and results[4] == 9)
|
||||
|
||||
-- Metamethods can call Lua and grow the stack in the middle of an opcode.
|
||||
local function grow(n)
|
||||
if n == 0 then collectgarbage(); return 0 end
|
||||
return 1 + grow(n-1)
|
||||
end
|
||||
local obj = setmetatable({}, {__index = function(_, key) return grow(300) + key end})
|
||||
assert(obj[4] == 304)
|
||||
|
||||
-- Exercise the extra page word for a list wider than the SETLIST C field.
|
||||
local wide = assert(loadstring('return {' .. string.rep('1,', 26000) .. '42}'))()
|
||||
assert(#wide == 26001 and wide[26001] == 42)
|
||||
|
||||
local f, syntax = loadstring('local =')
|
||||
assert(not f and type(syntax) == 'string')
|
||||
local ok, err = pcall(assert(loadstring('local x\nreturn x.missing', '@bad.lua')))
|
||||
assert(not ok and err:match('bad.lua:2:'))
|
||||
print('VM regression tests passed')
|
||||
@@ -0,0 +1,212 @@
|
||||
/* 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user