Merge remote-tracking branch 'origin/ljtest' into ljtest2
This commit is contained in:
commit
54b9c86d9d
15 files changed
+17421
-6
No files matched your search
@@ -379,6 +379,16 @@ endif
|
||||
# I wish I could make dependencies out of rc files :(
|
||||
$(objdir)/win32/Srb2win.res : \
|
||||
win32/afxres.h win32/resource.h
|
||||
clean:
|
||||
$(REMOVE) *~ *.flc
|
||||
$(REMOVE) $(OBJDIR)/*.o
|
||||
ifndef NOBLUAJIT
|
||||
$(REMOVE) $(OBJDIR)/buildvm* $(OBJDIR)/minilua* $(OBJDIR)/lj_vm* $(ALL_HDRGEN)
|
||||
|
||||
cleanjit:
|
||||
$(REMOVE) $(LJVMCORE_O) $(ALL_GEN)
|
||||
$(REMOVE) $(OBJDIR)/buildvm* $(OBJDIR)/minilua* $(OBJDIR)/lj_vm* $(ALL_HDRGEN)
|
||||
endif
|
||||
|
||||
# dependency recipe template
|
||||
# 1: source file suffix
|
||||
|
||||
@@ -0,0 +1,309 @@
|
||||
----------------------------------------------------------------------------
|
||||
-- LuaJIT profiler.
|
||||
--
|
||||
-- Copyright (C) 2005-2023 Mike Pall. All rights reserved.
|
||||
-- Released under the MIT license. See Copyright Notice in luajit.h
|
||||
----------------------------------------------------------------------------
|
||||
--
|
||||
-- This module is a simple command line interface to the built-in
|
||||
-- low-overhead profiler of LuaJIT.
|
||||
--
|
||||
-- The lower-level API of the profiler is accessible via the "jit.profile"
|
||||
-- module or the luaJIT_profile_* C API.
|
||||
--
|
||||
-- Example usage:
|
||||
--
|
||||
-- luajit -jp myapp.lua
|
||||
-- luajit -jp=s myapp.lua
|
||||
-- luajit -jp=-s myapp.lua
|
||||
-- luajit -jp=vl myapp.lua
|
||||
-- luajit -jp=G,profile.txt myapp.lua
|
||||
--
|
||||
-- The following dump features are available:
|
||||
--
|
||||
-- f Stack dump: function name, otherwise module:line. Default mode.
|
||||
-- F Stack dump: ditto, but always prepend module.
|
||||
-- l Stack dump: module:line.
|
||||
-- <number> stack dump depth (callee < caller). Default: 1.
|
||||
-- -<number> Inverse stack dump depth (caller > callee).
|
||||
-- s Split stack dump after first stack level. Implies abs(depth) >= 2.
|
||||
-- p Show full path for module names.
|
||||
-- v Show VM states. Can be combined with stack dumps, e.g. vf or fv.
|
||||
-- z Show zones. Can be combined with stack dumps, e.g. zf or fz.
|
||||
-- r Show raw sample counts. Default: show percentages.
|
||||
-- a Annotate excerpts from source code files.
|
||||
-- A Annotate complete source code files.
|
||||
-- G Produce raw output suitable for graphical tools (e.g. flame graphs).
|
||||
-- m<number> Minimum sample percentage to be shown. Default: 3.
|
||||
-- i<number> Sampling interval in milliseconds. Default: 10.
|
||||
--
|
||||
----------------------------------------------------------------------------
|
||||
|
||||
-- Cache some library functions and objects.
|
||||
local jit = require("jit")
|
||||
local profile = require("jit.profile")
|
||||
local vmdef = require("jit.vmdef")
|
||||
local math = math
|
||||
local pairs, ipairs, tonumber, floor = pairs, ipairs, tonumber, math.floor
|
||||
local sort, format = table.sort, string.format
|
||||
local stdout = io.stdout
|
||||
local zone -- Load jit.zone module on demand.
|
||||
|
||||
-- Output file handle.
|
||||
local out
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local prof_ud
|
||||
local prof_states, prof_split, prof_min, prof_raw, prof_fmt, prof_depth
|
||||
local prof_ann, prof_count1, prof_count2, prof_samples
|
||||
|
||||
local map_vmmode = {
|
||||
N = "Compiled",
|
||||
I = "Interpreted",
|
||||
C = "C code",
|
||||
G = "Garbage Collector",
|
||||
J = "JIT Compiler",
|
||||
}
|
||||
|
||||
-- Profiler callback.
|
||||
local function prof_cb(th, samples, vmmode)
|
||||
prof_samples = prof_samples + samples
|
||||
local key_stack, key_stack2, key_state
|
||||
-- Collect keys for sample.
|
||||
if prof_states then
|
||||
if prof_states == "v" then
|
||||
key_state = map_vmmode[vmmode] or vmmode
|
||||
else
|
||||
key_state = zone:get() or "(none)"
|
||||
end
|
||||
end
|
||||
if prof_fmt then
|
||||
key_stack = profile.dumpstack(th, prof_fmt, prof_depth)
|
||||
key_stack = key_stack:gsub("%[builtin#(%d+)%]", function(x)
|
||||
return vmdef.ffnames[tonumber(x)]
|
||||
end)
|
||||
if prof_split == 2 then
|
||||
local k1, k2 = key_stack:match("(.-) [<>] (.*)")
|
||||
if k2 then key_stack, key_stack2 = k1, k2 end
|
||||
elseif prof_split == 3 then
|
||||
key_stack2 = profile.dumpstack(th, "l", 1)
|
||||
end
|
||||
end
|
||||
-- Order keys.
|
||||
local k1, k2
|
||||
if prof_split == 1 then
|
||||
if key_state then
|
||||
k1 = key_state
|
||||
if key_stack then k2 = key_stack end
|
||||
end
|
||||
elseif key_stack then
|
||||
k1 = key_stack
|
||||
if key_stack2 then k2 = key_stack2 elseif key_state then k2 = key_state end
|
||||
end
|
||||
-- Coalesce samples in one or two levels.
|
||||
if k1 then
|
||||
local t1 = prof_count1
|
||||
t1[k1] = (t1[k1] or 0) + samples
|
||||
if k2 then
|
||||
local t2 = prof_count2
|
||||
local t3 = t2[k1]
|
||||
if not t3 then t3 = {}; t2[k1] = t3 end
|
||||
t3[k2] = (t3[k2] or 0) + samples
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- Show top N list.
|
||||
local function prof_top(count1, count2, samples, indent)
|
||||
local t, n = {}, 0
|
||||
for k in pairs(count1) do
|
||||
n = n + 1
|
||||
t[n] = k
|
||||
end
|
||||
sort(t, function(a, b) return count1[a] > count1[b] end)
|
||||
for i=1,n do
|
||||
local k = t[i]
|
||||
local v = count1[k]
|
||||
local pct = floor(v*100/samples + 0.5)
|
||||
if pct < prof_min then break end
|
||||
if not prof_raw then
|
||||
out:write(format("%s%2d%% %s\n", indent, pct, k))
|
||||
elseif prof_raw == "r" then
|
||||
out:write(format("%s%5d %s\n", indent, v, k))
|
||||
else
|
||||
out:write(format("%s %d\n", k, v))
|
||||
end
|
||||
if count2 then
|
||||
local r = count2[k]
|
||||
if r then
|
||||
prof_top(r, nil, v, (prof_split == 3 or prof_split == 1) and " -- " or
|
||||
(prof_depth < 0 and " -> " or " <- "))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Annotate source code
|
||||
local function prof_annotate(count1, samples)
|
||||
local files = {}
|
||||
local ms = 0
|
||||
for k, v in pairs(count1) do
|
||||
local pct = floor(v*100/samples + 0.5)
|
||||
ms = math.max(ms, v)
|
||||
if pct >= prof_min then
|
||||
local file, line = k:match("^(.*):(%d+)$")
|
||||
if not file then file = k; line = 0 end
|
||||
local fl = files[file]
|
||||
if not fl then fl = {}; files[file] = fl; files[#files+1] = file end
|
||||
line = tonumber(line)
|
||||
fl[line] = prof_raw and v or pct
|
||||
end
|
||||
end
|
||||
sort(files)
|
||||
local fmtv, fmtn = " %3d%% | %s\n", " | %s\n"
|
||||
if prof_raw then
|
||||
local n = math.max(5, math.ceil(math.log10(ms)))
|
||||
fmtv = "%"..n.."d | %s\n"
|
||||
fmtn = (" "):rep(n).." | %s\n"
|
||||
end
|
||||
local ann = prof_ann
|
||||
for _, file in ipairs(files) do
|
||||
local f0 = file:byte()
|
||||
if f0 == 40 or f0 == 91 then
|
||||
out:write(format("\n====== %s ======\n[Cannot annotate non-file]\n", file))
|
||||
break
|
||||
end
|
||||
local fp, err = io.open(file)
|
||||
if not fp then
|
||||
out:write(format("====== ERROR: %s: %s\n", file, err))
|
||||
break
|
||||
end
|
||||
out:write(format("\n====== %s ======\n", file))
|
||||
local fl = files[file]
|
||||
local n, show = 1, false
|
||||
if ann ~= 0 then
|
||||
for i=1,ann do
|
||||
if fl[i] then show = true; out:write("@@ 1 @@\n"); break end
|
||||
end
|
||||
end
|
||||
for line in fp:lines() do
|
||||
if line:byte() == 27 then
|
||||
out:write("[Cannot annotate bytecode file]\n")
|
||||
break
|
||||
end
|
||||
local v = fl[n]
|
||||
if ann ~= 0 then
|
||||
local v2 = fl[n+ann]
|
||||
if show then
|
||||
if v2 then show = n+ann elseif v then show = n
|
||||
elseif show+ann < n then show = false end
|
||||
elseif v2 then
|
||||
show = n+ann
|
||||
out:write(format("@@ %d @@\n", n))
|
||||
end
|
||||
if not show then goto next end
|
||||
end
|
||||
if v then
|
||||
out:write(format(fmtv, v, line))
|
||||
else
|
||||
out:write(format(fmtn, line))
|
||||
end
|
||||
::next::
|
||||
n = n + 1
|
||||
end
|
||||
fp:close()
|
||||
end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- Finish profiling and dump result.
|
||||
local function prof_finish()
|
||||
if prof_ud then
|
||||
profile.stop()
|
||||
local samples = prof_samples
|
||||
if samples == 0 then
|
||||
if prof_raw ~= true then out:write("[No samples collected]\n") end
|
||||
elseif prof_ann then
|
||||
prof_annotate(prof_count1, samples)
|
||||
else
|
||||
prof_top(prof_count1, prof_count2, samples, "")
|
||||
end
|
||||
prof_count1 = nil
|
||||
prof_count2 = nil
|
||||
prof_ud = nil
|
||||
if out ~= stdout then out:close() end
|
||||
end
|
||||
end
|
||||
|
||||
-- Start profiling.
|
||||
local function prof_start(mode)
|
||||
local interval = ""
|
||||
mode = mode:gsub("i%d*", function(s) interval = s; return "" end)
|
||||
prof_min = 3
|
||||
mode = mode:gsub("m(%d+)", function(s) prof_min = tonumber(s); return "" end)
|
||||
prof_depth = 1
|
||||
mode = mode:gsub("%-?%d+", function(s) prof_depth = tonumber(s); return "" end)
|
||||
local m = {}
|
||||
for c in mode:gmatch(".") do m[c] = c end
|
||||
prof_states = m.z or m.v
|
||||
if prof_states == "z" then zone = require("jit.zone") end
|
||||
local scope = m.l or m.f or m.F or (prof_states and "" or "f")
|
||||
local flags = (m.p or "")
|
||||
prof_raw = m.r
|
||||
if m.s then
|
||||
prof_split = 2
|
||||
if prof_depth == -1 or m["-"] then prof_depth = -2
|
||||
elseif prof_depth == 1 then prof_depth = 2 end
|
||||
elseif mode:find("[fF].*l") then
|
||||
scope = "l"
|
||||
prof_split = 3
|
||||
else
|
||||
prof_split = (scope == "" or mode:find("[zv].*[lfF]")) and 1 or 0
|
||||
end
|
||||
prof_ann = m.A and 0 or (m.a and 3)
|
||||
if prof_ann then
|
||||
scope = "l"
|
||||
prof_fmt = "pl"
|
||||
prof_split = 0
|
||||
prof_depth = 1
|
||||
elseif m.G and scope ~= "" then
|
||||
prof_fmt = flags..scope.."Z;"
|
||||
prof_depth = -100
|
||||
prof_raw = true
|
||||
prof_min = 0
|
||||
elseif scope == "" then
|
||||
prof_fmt = false
|
||||
else
|
||||
local sc = prof_split == 3 and m.f or m.F or scope
|
||||
prof_fmt = flags..sc..(prof_depth >= 0 and "Z < " or "Z > ")
|
||||
end
|
||||
prof_count1 = {}
|
||||
prof_count2 = {}
|
||||
prof_samples = 0
|
||||
profile.start(scope:lower()..interval, prof_cb)
|
||||
prof_ud = newproxy(true)
|
||||
getmetatable(prof_ud).__gc = prof_finish
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local function start(mode, outfile)
|
||||
if not outfile then outfile = os.getenv("LUAJIT_PROFILEFILE") end
|
||||
if outfile then
|
||||
out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
|
||||
else
|
||||
out = stdout
|
||||
end
|
||||
prof_start(mode or "f")
|
||||
end
|
||||
|
||||
-- Public module functions.
|
||||
return {
|
||||
start = start, -- For -j command line option.
|
||||
stop = prof_finish
|
||||
}
|
||||
|
||||
@@ -0,0 +1,709 @@
|
||||
/*
|
||||
** Base and coroutine library.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
**
|
||||
** Major portions taken verbatim or adapted from the Lua interpreter.
|
||||
** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define lib_base_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#include "lj_obj.h"
|
||||
#include "lj_gc.h"
|
||||
#include "lj_err.h"
|
||||
#include "lj_debug.h"
|
||||
#include "lj_buf.h"
|
||||
#include "lj_str.h"
|
||||
#include "lj_tab.h"
|
||||
#include "lj_meta.h"
|
||||
#include "lj_state.h"
|
||||
#include "lj_frame.h"
|
||||
#if LJ_HASFFI
|
||||
#include "lj_ctype.h"
|
||||
#include "lj_cconv.h"
|
||||
#endif
|
||||
#include "lj_bc.h"
|
||||
#include "lj_ff.h"
|
||||
#include "lj_dispatch.h"
|
||||
#include "lj_char.h"
|
||||
#include "lj_strscan.h"
|
||||
#include "lj_strfmt.h"
|
||||
#include "lj_lib.h"
|
||||
|
||||
/* -- Base library: checks ------------------------------------------------ */
|
||||
|
||||
#define LJLIB_MODULE_base
|
||||
|
||||
LJLIB_ASM(assert) LJLIB_REC(.)
|
||||
{
|
||||
lj_lib_checkany(L, 1);
|
||||
if (L->top == L->base+1)
|
||||
lj_err_caller(L, LJ_ERR_ASSERT);
|
||||
else if (tvisstr(L->base+1) || tvisnumber(L->base+1))
|
||||
lj_err_callermsg(L, strdata(lj_lib_checkstr(L, 2)));
|
||||
else
|
||||
lj_err_run(L);
|
||||
return FFH_UNREACHABLE;
|
||||
}
|
||||
|
||||
/* ORDER LJ_T */
|
||||
LJLIB_PUSH("nil")
|
||||
LJLIB_PUSH("boolean")
|
||||
LJLIB_PUSH(top-1) /* boolean */
|
||||
LJLIB_PUSH("userdata")
|
||||
LJLIB_PUSH("string")
|
||||
LJLIB_PUSH("upval")
|
||||
LJLIB_PUSH("thread")
|
||||
LJLIB_PUSH("proto")
|
||||
LJLIB_PUSH("function")
|
||||
LJLIB_PUSH("trace")
|
||||
LJLIB_PUSH("cdata")
|
||||
LJLIB_PUSH("table")
|
||||
LJLIB_PUSH(top-9) /* userdata */
|
||||
LJLIB_PUSH("number")
|
||||
LJLIB_ASM_(type) LJLIB_REC(.)
|
||||
/* Recycle the lj_lib_checkany(L, 1) from assert. */
|
||||
|
||||
/* -- Base library: iterators --------------------------------------------- */
|
||||
|
||||
/* This solves a circular dependency problem -- change FF_next_N as needed. */
|
||||
LJ_STATIC_ASSERT((int)FF_next == FF_next_N);
|
||||
|
||||
LJLIB_ASM(next) LJLIB_REC(.)
|
||||
{
|
||||
lj_lib_checktab(L, 1);
|
||||
lj_err_msg(L, LJ_ERR_NEXTIDX);
|
||||
return FFH_UNREACHABLE;
|
||||
}
|
||||
|
||||
#if LJ_52 || LJ_HASFFI
|
||||
static int ffh_pairs(lua_State *L, MMS mm)
|
||||
{
|
||||
TValue *o = lj_lib_checkany(L, 1);
|
||||
cTValue *mo = lj_meta_lookup(L, o, mm);
|
||||
if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) {
|
||||
L->top = o+1; /* Only keep one argument. */
|
||||
copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */
|
||||
return FFH_TAILCALL;
|
||||
} else {
|
||||
if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE);
|
||||
if (LJ_FR2) { copyTV(L, o-1, o); o--; }
|
||||
setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1)));
|
||||
if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0);
|
||||
return FFH_RES(3);
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE)
|
||||
#endif
|
||||
|
||||
LJLIB_PUSH(lastcl)
|
||||
LJLIB_ASM(pairs) LJLIB_REC(xpairs 0)
|
||||
{
|
||||
return ffh_pairs(L, MM_pairs);
|
||||
}
|
||||
|
||||
LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.)
|
||||
{
|
||||
lj_lib_checktab(L, 1);
|
||||
lj_lib_checkint(L, 2);
|
||||
return FFH_UNREACHABLE;
|
||||
}
|
||||
|
||||
LJLIB_PUSH(lastcl)
|
||||
LJLIB_ASM(ipairs) LJLIB_REC(xpairs 1)
|
||||
{
|
||||
return ffh_pairs(L, MM_ipairs);
|
||||
}
|
||||
|
||||
/* -- Base library: getters and setters ----------------------------------- */
|
||||
|
||||
LJLIB_ASM_(getmetatable) LJLIB_REC(.)
|
||||
/* Recycle the lj_lib_checkany(L, 1) from assert. */
|
||||
|
||||
LJLIB_ASM(setmetatable) LJLIB_REC(.)
|
||||
{
|
||||
GCtab *t = lj_lib_checktab(L, 1);
|
||||
GCtab *mt = lj_lib_checktabornil(L, 2);
|
||||
if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable)))
|
||||
lj_err_caller(L, LJ_ERR_PROTMT);
|
||||
setgcref(t->metatable, obj2gco(mt));
|
||||
if (mt) { lj_gc_objbarriert(L, t, mt); }
|
||||
settabV(L, L->base-1-LJ_FR2, t);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
|
||||
LJLIB_CF(getfenv) LJLIB_REC(.)
|
||||
{
|
||||
GCfunc *fn;
|
||||
cTValue *o = L->base;
|
||||
if (!(o < L->top && tvisfunc(o))) {
|
||||
int level = lj_lib_optint(L, 1, 1);
|
||||
o = lj_debug_frame(L, level, &level);
|
||||
if (o == NULL)
|
||||
lj_err_arg(L, 1, LJ_ERR_INVLVL);
|
||||
if (LJ_FR2) o--;
|
||||
}
|
||||
fn = &gcval(o)->fn;
|
||||
settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_CF(setfenv)
|
||||
{
|
||||
GCfunc *fn;
|
||||
GCtab *t = lj_lib_checktab(L, 2);
|
||||
cTValue *o = L->base;
|
||||
if (!(o < L->top && tvisfunc(o))) {
|
||||
int level = lj_lib_checkint(L, 1);
|
||||
if (level == 0) {
|
||||
/* NOBARRIER: A thread (i.e. L) is never black. */
|
||||
setgcref(L->env, obj2gco(t));
|
||||
return 0;
|
||||
}
|
||||
o = lj_debug_frame(L, level, &level);
|
||||
if (o == NULL)
|
||||
lj_err_arg(L, 1, LJ_ERR_INVLVL);
|
||||
if (LJ_FR2) o--;
|
||||
}
|
||||
fn = &gcval(o)->fn;
|
||||
if (!isluafunc(fn))
|
||||
lj_err_caller(L, LJ_ERR_SETFENV);
|
||||
setgcref(fn->l.env, obj2gco(t));
|
||||
lj_gc_objbarrier(L, obj2gco(fn), t);
|
||||
setfuncV(L, L->top++, fn);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_ASM(rawget) LJLIB_REC(.)
|
||||
{
|
||||
lj_lib_checktab(L, 1);
|
||||
lj_lib_checkany(L, 2);
|
||||
return FFH_UNREACHABLE;
|
||||
}
|
||||
|
||||
LJLIB_CF(rawset) LJLIB_REC(.)
|
||||
{
|
||||
lj_lib_checktab(L, 1);
|
||||
lj_lib_checkany(L, 2);
|
||||
L->top = 1+lj_lib_checkany(L, 3);
|
||||
lua_rawset(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_CF(rawequal) LJLIB_REC(.)
|
||||
{
|
||||
cTValue *o1 = lj_lib_checkany(L, 1);
|
||||
cTValue *o2 = lj_lib_checkany(L, 2);
|
||||
setboolV(L->top-1, lj_obj_equal(o1, o2));
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if LJ_52
|
||||
LJLIB_CF(rawlen) LJLIB_REC(.)
|
||||
{
|
||||
cTValue *o = L->base;
|
||||
int32_t len;
|
||||
if (L->top > o && tvisstr(o))
|
||||
len = (int32_t)strV(o)->len;
|
||||
else
|
||||
len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1));
|
||||
setintV(L->top-1, len);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
LJLIB_CF(unpack)
|
||||
{
|
||||
GCtab *t = lj_lib_checktab(L, 1);
|
||||
int32_t n, i = lj_lib_optint(L, 2, 1);
|
||||
int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ?
|
||||
lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t);
|
||||
uint32_t nu;
|
||||
if (i > e) return 0;
|
||||
nu = (uint32_t)e - (uint32_t)i;
|
||||
n = (int32_t)(nu+1);
|
||||
if (nu >= LUAI_MAXCSTACK || !lua_checkstack(L, n))
|
||||
lj_err_caller(L, LJ_ERR_UNPACK);
|
||||
do {
|
||||
cTValue *tv = lj_tab_getint(t, i);
|
||||
if (tv) {
|
||||
copyTV(L, L->top++, tv);
|
||||
} else {
|
||||
setnilV(L->top++);
|
||||
}
|
||||
} while (i++ < e);
|
||||
return n;
|
||||
}
|
||||
|
||||
LJLIB_CF(select) LJLIB_REC(.)
|
||||
{
|
||||
int32_t n = (int32_t)(L->top - L->base);
|
||||
if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') {
|
||||
setintV(L->top-1, n-1);
|
||||
return 1;
|
||||
} else {
|
||||
int32_t i = lj_lib_checkint(L, 1);
|
||||
if (i < 0) i = n + i; else if (i > n) i = n;
|
||||
if (i < 1)
|
||||
lj_err_arg(L, 1, LJ_ERR_IDXRNG);
|
||||
return n - i;
|
||||
}
|
||||
}
|
||||
|
||||
/* -- Base library: conversions ------------------------------------------- */
|
||||
|
||||
LJLIB_ASM(tonumber) LJLIB_REC(.)
|
||||
{
|
||||
int32_t base = lj_lib_optint(L, 2, 10);
|
||||
if (base == 10) {
|
||||
TValue *o = lj_lib_checkany(L, 1);
|
||||
if (lj_strscan_numberobj(o)) {
|
||||
copyTV(L, L->base-1-LJ_FR2, o);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
#if LJ_HASFFI
|
||||
if (tviscdata(o)) {
|
||||
CTState *cts = ctype_cts(L);
|
||||
CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid);
|
||||
if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
|
||||
if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
|
||||
if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) &&
|
||||
ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) {
|
||||
int32_t i;
|
||||
lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0);
|
||||
setintV(L->base-1-LJ_FR2, i);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE),
|
||||
(uint8_t *)&(L->base-1-LJ_FR2)->n, o, 0);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
const char *p = strdata(lj_lib_checkstr(L, 1));
|
||||
char *ep;
|
||||
unsigned int neg = 0;
|
||||
unsigned long ul;
|
||||
if (base < 2 || base > 36)
|
||||
lj_err_arg(L, 2, LJ_ERR_BASERNG);
|
||||
while (lj_char_isspace((unsigned char)(*p))) p++;
|
||||
if (*p == '-') { p++; neg = 1; } else if (*p == '+') { p++; }
|
||||
if (lj_char_isalnum((unsigned char)(*p))) {
|
||||
ul = strtoul(p, &ep, base);
|
||||
if (p != ep) {
|
||||
while (lj_char_isspace((unsigned char)(*ep))) ep++;
|
||||
if (*ep == '\0') {
|
||||
if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u+neg)) {
|
||||
if (neg) ul = ~ul+1u;
|
||||
setintV(L->base-1-LJ_FR2, (int32_t)ul);
|
||||
} else {
|
||||
lua_Number n = (lua_Number)ul;
|
||||
if (neg) n = -n;
|
||||
setnumV(L->base-1-LJ_FR2, n);
|
||||
}
|
||||
return FFH_RES(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
setnilV(L->base-1-LJ_FR2);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
|
||||
LJLIB_ASM(tostring) LJLIB_REC(.)
|
||||
{
|
||||
TValue *o = lj_lib_checkany(L, 1);
|
||||
cTValue *mo;
|
||||
L->top = o+1; /* Only keep one argument. */
|
||||
if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
|
||||
copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */
|
||||
return FFH_TAILCALL;
|
||||
}
|
||||
lj_gc_check(L);
|
||||
setstrV(L, L->base-1-LJ_FR2, lj_strfmt_obj(L, L->base));
|
||||
return FFH_RES(1);
|
||||
}
|
||||
|
||||
/* -- Base library: throw and catch errors -------------------------------- */
|
||||
|
||||
LJLIB_CF(error)
|
||||
{
|
||||
int32_t level = lj_lib_optint(L, 2, 1);
|
||||
lua_settop(L, 1);
|
||||
if (lua_isstring(L, 1) && level > 0) {
|
||||
luaL_where(L, level);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
LJLIB_ASM(pcall) LJLIB_REC(.)
|
||||
{
|
||||
lj_lib_checkany(L, 1);
|
||||
lj_lib_checkfunc(L, 2); /* For xpcall only. */
|
||||
return FFH_UNREACHABLE;
|
||||
}
|
||||
LJLIB_ASM_(xpcall) LJLIB_REC(.)
|
||||
|
||||
/* -- Base library: load Lua code ----------------------------------------- */
|
||||
|
||||
#if !LJ_SRB2LIB
|
||||
static int load_aux(lua_State *L, int status, int envarg)
|
||||
{
|
||||
if (status == LUA_OK) {
|
||||
/*
|
||||
** Set environment table for top-level function.
|
||||
** Don't do this for non-native bytecode, which returns a prototype.
|
||||
*/
|
||||
if (tvistab(L->base+envarg-1) && tvisfunc(L->top-1)) {
|
||||
GCfunc *fn = funcV(L->top-1);
|
||||
GCtab *t = tabV(L->base+envarg-1);
|
||||
setgcref(fn->c.env, obj2gco(t));
|
||||
lj_gc_objbarrier(L, fn, t);
|
||||
}
|
||||
return 1;
|
||||
} else {
|
||||
setnilV(L->top-2);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
LJLIB_CF(loadfile)
|
||||
{
|
||||
GCstr *fname = lj_lib_optstr(L, 1);
|
||||
GCstr *mode = lj_lib_optstr(L, 2);
|
||||
int status;
|
||||
lua_settop(L, 3); /* Ensure env arg exists. */
|
||||
status = luaL_loadfilex(L, fname ? strdata(fname) : NULL,
|
||||
mode ? strdata(mode) : NULL);
|
||||
return load_aux(L, status, 3);
|
||||
}
|
||||
|
||||
static const char *reader_func(lua_State *L, void *ud, size_t *size)
|
||||
{
|
||||
UNUSED(ud);
|
||||
luaL_checkstack(L, 2, "too many nested functions");
|
||||
copyTV(L, L->top++, L->base);
|
||||
lua_call(L, 0, 1); /* Call user-supplied function. */
|
||||
L->top--;
|
||||
if (tvisnil(L->top)) {
|
||||
*size = 0;
|
||||
return NULL;
|
||||
} else if (tvisstr(L->top) || tvisnumber(L->top)) {
|
||||
copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */
|
||||
return lua_tolstring(L, 5, size);
|
||||
} else {
|
||||
lj_err_caller(L, LJ_ERR_RDRSTR);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
LJLIB_CF(load)
|
||||
{
|
||||
GCstr *name = lj_lib_optstr(L, 2);
|
||||
GCstr *mode = lj_lib_optstr(L, 3);
|
||||
int status;
|
||||
if (L->base < L->top &&
|
||||
(tvisstr(L->base) || tvisnumber(L->base) || tvisbuf(L->base))) {
|
||||
const char *s;
|
||||
MSize len;
|
||||
if (tvisbuf(L->base)) {
|
||||
SBufExt *sbx = bufV(L->base);
|
||||
s = sbx->r;
|
||||
len = sbufxlen(sbx);
|
||||
if (!name) name = &G(L)->strempty; /* Buffers are not NUL-terminated. */
|
||||
} else {
|
||||
GCstr *str = lj_lib_checkstr(L, 1);
|
||||
s = strdata(str);
|
||||
len = str->len;
|
||||
}
|
||||
lua_settop(L, 4); /* Ensure env arg exists. */
|
||||
status = luaL_loadbufferx(L, s, len, name ? strdata(name) : s,
|
||||
mode ? strdata(mode) : NULL);
|
||||
} else {
|
||||
lj_lib_checkfunc(L, 1);
|
||||
lua_settop(L, 5); /* Reserve a slot for the string from the reader. */
|
||||
status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)",
|
||||
mode ? strdata(mode) : NULL);
|
||||
}
|
||||
return load_aux(L, status, 4);
|
||||
}
|
||||
|
||||
LJLIB_CF(loadstring)
|
||||
{
|
||||
return lj_cf_load(L);
|
||||
}
|
||||
|
||||
LJLIB_CF(dofile)
|
||||
{
|
||||
GCstr *fname = lj_lib_optstr(L, 1);
|
||||
setnilV(L->top);
|
||||
L->top = L->base+1;
|
||||
if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != LUA_OK)
|
||||
lua_error(L);
|
||||
lua_call(L, 0, LUA_MULTRET);
|
||||
return (int)(L->top - L->base) - 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* -- Base library: GC control -------------------------------------------- */
|
||||
|
||||
LJLIB_CF(gcinfo)
|
||||
{
|
||||
setintV(L->top++, (int32_t)(G(L)->gc.total >> 10));
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_CF(collectgarbage)
|
||||
{
|
||||
int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */
|
||||
"\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul\1\377\11isrunning");
|
||||
int32_t data = lj_lib_optint(L, 2, 0);
|
||||
if (opt == LUA_GCCOUNT) {
|
||||
#if LJ_INTONLY
|
||||
setintV(L->top, G(L)->gc.total/1024);
|
||||
#else
|
||||
setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0);
|
||||
#endif
|
||||
} else {
|
||||
int res = lua_gc(L, opt, data);
|
||||
if (opt == LUA_GCSTEP || opt == LUA_GCISRUNNING)
|
||||
setboolV(L->top, res);
|
||||
else
|
||||
setintV(L->top, res);
|
||||
}
|
||||
L->top++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* -- Base library: miscellaneous functions ------------------------------- */
|
||||
|
||||
LJLIB_PUSH(top-2) /* Upvalue holds weak table. */
|
||||
LJLIB_CF(newproxy)
|
||||
{
|
||||
lua_settop(L, 1);
|
||||
lua_newuserdata(L, 0);
|
||||
if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */
|
||||
return 1;
|
||||
} else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */
|
||||
lua_newtable(L);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */
|
||||
} else { /* newproxy(proxy): inherit metatable. */
|
||||
int validproxy = 0;
|
||||
if (lua_getmetatable(L, 1)) {
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
validproxy = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
if (!validproxy)
|
||||
lj_err_arg(L, 1, LJ_ERR_NOPROXY);
|
||||
lua_getmetatable(L, 1);
|
||||
}
|
||||
lua_setmetatable(L, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_PUSH("tostring")
|
||||
LJLIB_CF(print)
|
||||
{
|
||||
ptrdiff_t i, nargs = L->top - L->base;
|
||||
cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));
|
||||
int shortcut;
|
||||
if (tv && !tvisnil(tv)) {
|
||||
copyTV(L, L->top++, tv);
|
||||
} else {
|
||||
setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1)));
|
||||
lua_gettable(L, LUA_GLOBALSINDEX);
|
||||
tv = L->top-1;
|
||||
}
|
||||
shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring) &&
|
||||
!gcrefu(basemt_it(G(L), LJ_TNUMX));
|
||||
for (i = 0; i < nargs; i++) {
|
||||
cTValue *o = &L->base[i];
|
||||
const char *str;
|
||||
size_t size;
|
||||
MSize len;
|
||||
if (shortcut && (str = lj_strfmt_wstrnum(L, o, &len)) != NULL) {
|
||||
size = len;
|
||||
} else {
|
||||
copyTV(L, L->top+1, o);
|
||||
copyTV(L, L->top, L->top-1);
|
||||
L->top += 2;
|
||||
lua_call(L, 1, 1);
|
||||
str = lua_tolstring(L, -1, &size);
|
||||
if (!str)
|
||||
lj_err_caller(L, LJ_ERR_PRTOSTR);
|
||||
L->top--;
|
||||
}
|
||||
if (i)
|
||||
putchar('\t');
|
||||
fwrite(str, 1, size, stdout);
|
||||
}
|
||||
putchar('\n');
|
||||
return 0;
|
||||
}
|
||||
|
||||
LJLIB_PUSH(top-3)
|
||||
LJLIB_SET(_VERSION)
|
||||
|
||||
#include "lj_libdef.h"
|
||||
|
||||
/* -- Coroutine library --------------------------------------------------- */
|
||||
|
||||
#define LJLIB_MODULE_coroutine
|
||||
|
||||
LJLIB_CF(coroutine_status)
|
||||
{
|
||||
const char *s;
|
||||
lua_State *co;
|
||||
if (!(L->top > L->base && tvisthread(L->base)))
|
||||
lj_err_arg(L, 1, LJ_ERR_NOCORO);
|
||||
co = threadV(L->base);
|
||||
if (co == L) s = "running";
|
||||
else if (co->status == LUA_YIELD) s = "suspended";
|
||||
else if (co->status != LUA_OK) s = "dead";
|
||||
else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal";
|
||||
else if (co->top == co->base) s = "dead";
|
||||
else s = "suspended";
|
||||
lua_pushstring(L, s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_CF(coroutine_running)
|
||||
{
|
||||
#if LJ_52
|
||||
int ismain = lua_pushthread(L);
|
||||
setboolV(L->top++, ismain);
|
||||
return 2;
|
||||
#else
|
||||
if (lua_pushthread(L))
|
||||
setnilV(L->top++);
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
LJLIB_CF(coroutine_isyieldable)
|
||||
{
|
||||
setboolV(L->top++, cframe_canyield(L->cframe));
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_CF(coroutine_create)
|
||||
{
|
||||
lua_State *L1;
|
||||
if (!(L->base < L->top && tvisfunc(L->base)))
|
||||
lj_err_argt(L, 1, LUA_TFUNCTION);
|
||||
L1 = lua_newthread(L);
|
||||
setfuncV(L, L1->top++, funcV(L->base));
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_ASM(coroutine_yield)
|
||||
{
|
||||
lj_err_caller(L, LJ_ERR_CYIELD);
|
||||
return FFH_UNREACHABLE;
|
||||
}
|
||||
|
||||
static int ffh_resume(lua_State *L, lua_State *co, int wrap)
|
||||
{
|
||||
if (co->cframe != NULL || co->status > LUA_YIELD ||
|
||||
(co->status == LUA_OK && co->top == co->base)) {
|
||||
ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
|
||||
if (wrap) lj_err_caller(L, em);
|
||||
setboolV(L->base-1-LJ_FR2, 0);
|
||||
setstrV(L, L->base-LJ_FR2, lj_err_str(L, em));
|
||||
return FFH_RES(2);
|
||||
}
|
||||
if (lj_state_cpgrowstack(co, (MSize)(L->top - L->base)) != LUA_OK) {
|
||||
cTValue *msg = --co->top;
|
||||
lj_err_callermsg(L, strVdata(msg));
|
||||
}
|
||||
return FFH_RETRY;
|
||||
}
|
||||
|
||||
LJLIB_ASM(coroutine_resume)
|
||||
{
|
||||
if (!(L->top > L->base && tvisthread(L->base)))
|
||||
lj_err_arg(L, 1, LJ_ERR_NOCORO);
|
||||
return ffh_resume(L, threadV(L->base), 0);
|
||||
}
|
||||
|
||||
LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)
|
||||
{
|
||||
return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);
|
||||
}
|
||||
|
||||
/* Inline declarations. */
|
||||
LJ_ASMF void lj_ff_coroutine_wrap_aux(void);
|
||||
#if !(LJ_TARGET_MIPS && defined(ljamalg_c))
|
||||
LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
|
||||
lua_State *co);
|
||||
#endif
|
||||
|
||||
/* Error handler, called from assembler VM. */
|
||||
void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)
|
||||
{
|
||||
co->top--; copyTV(L, L->top, co->top); L->top++;
|
||||
if (tvisstr(L->top-1))
|
||||
lj_err_callermsg(L, strVdata(L->top-1));
|
||||
else
|
||||
lj_err_run(L);
|
||||
}
|
||||
|
||||
/* Forward declaration. */
|
||||
static void setpc_wrap_aux(lua_State *L, GCfunc *fn);
|
||||
|
||||
LJLIB_CF(coroutine_wrap)
|
||||
{
|
||||
GCfunc *fn;
|
||||
lj_cf_coroutine_create(L);
|
||||
fn = lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1);
|
||||
setpc_wrap_aux(L, fn);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include "lj_libdef.h"
|
||||
|
||||
/* Fix the PC of wrap_aux. Really ugly workaround. */
|
||||
static void setpc_wrap_aux(lua_State *L, GCfunc *fn)
|
||||
{
|
||||
setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
static void newproxy_weaktable(lua_State *L)
|
||||
{
|
||||
/* NOBARRIER: The table is new (marked white). */
|
||||
GCtab *t = lj_tab_new(L, 0, 1);
|
||||
settabV(L, L->top++, t);
|
||||
setgcref(t->metatable, obj2gco(t));
|
||||
setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
|
||||
lj_str_newlit(L, "kv"));
|
||||
t->nomm = (uint8_t)(~(1u<<MM_mode));
|
||||
}
|
||||
|
||||
LUALIB_API int luaopen_base(lua_State *L)
|
||||
{
|
||||
/* NOBARRIER: Table and value are the same. */
|
||||
GCtab *env = tabref(L->env);
|
||||
settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);
|
||||
lua_pushliteral(L, LUA_VERSION); /* top-3. */
|
||||
newproxy_weaktable(L); /* top-2. */
|
||||
LJ_LIB_REG(L, "_G", base);
|
||||
LJ_LIB_REG(L, LUA_COLIBNAME, coroutine);
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
** Bit manipulation library.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
*/
|
||||
|
||||
#define lib_bit_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#include "lj_obj.h"
|
||||
#include "lj_err.h"
|
||||
#include "lj_buf.h"
|
||||
#include "lj_strscan.h"
|
||||
#include "lj_strfmt.h"
|
||||
#if LJ_HASFFI
|
||||
#include "lj_ctype.h"
|
||||
#include "lj_cdata.h"
|
||||
#include "lj_cconv.h"
|
||||
#include "lj_carith.h"
|
||||
#endif
|
||||
#include "lj_ff.h"
|
||||
#include "lj_lib.h"
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
#define LJLIB_MODULE_bit
|
||||
|
||||
#if LJ_HASFFI
|
||||
static int bit_result64(lua_State *L, CTypeID id, uint64_t x)
|
||||
{
|
||||
GCcdata *cd = lj_cdata_new_(L, id, 8);
|
||||
*(uint64_t *)cdataptr(cd) = x;
|
||||
setcdataV(L, L->base-1-LJ_FR2, cd);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
#else
|
||||
static int32_t bit_checkbit(lua_State *L, int narg)
|
||||
{
|
||||
TValue *o = L->base + narg-1;
|
||||
if (!(o < L->top && lj_strscan_numberobj(o)))
|
||||
lj_err_argt(L, narg, LUA_TNUMBER);
|
||||
if (LJ_LIKELY(tvisint(o))) {
|
||||
return intV(o);
|
||||
} else {
|
||||
int32_t i = lj_num2bit(numV(o));
|
||||
if (LJ_DUALNUM) setintV(o, i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit)
|
||||
{
|
||||
#if LJ_HASFFI
|
||||
CTypeID id = 0;
|
||||
setintV(L->base-1-LJ_FR2, (int32_t)lj_carith_check64(L, 1, &id));
|
||||
return FFH_RES(1);
|
||||
#else
|
||||
lj_lib_checknumber(L, 1);
|
||||
return FFH_RETRY;
|
||||
#endif
|
||||
}
|
||||
|
||||
LJLIB_ASM(bit_bnot) LJLIB_REC(bit_unary IR_BNOT)
|
||||
{
|
||||
#if LJ_HASFFI
|
||||
CTypeID id = 0;
|
||||
uint64_t x = lj_carith_check64(L, 1, &id);
|
||||
return id ? bit_result64(L, id, ~x) : FFH_RETRY;
|
||||
#else
|
||||
lj_lib_checknumber(L, 1);
|
||||
return FFH_RETRY;
|
||||
#endif
|
||||
}
|
||||
|
||||
LJLIB_ASM(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP)
|
||||
{
|
||||
#if LJ_HASFFI
|
||||
CTypeID id = 0;
|
||||
uint64_t x = lj_carith_check64(L, 1, &id);
|
||||
return id ? bit_result64(L, id, lj_bswap64(x)) : FFH_RETRY;
|
||||
#else
|
||||
lj_lib_checknumber(L, 1);
|
||||
return FFH_RETRY;
|
||||
#endif
|
||||
}
|
||||
|
||||
LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL)
|
||||
{
|
||||
#if LJ_HASFFI
|
||||
CTypeID id = 0, id2 = 0;
|
||||
uint64_t x = lj_carith_check64(L, 1, &id);
|
||||
int32_t sh = (int32_t)lj_carith_check64(L, 2, &id2);
|
||||
if (id) {
|
||||
x = lj_carith_shift64(x, sh, curr_func(L)->c.ffid - (int)FF_bit_lshift);
|
||||
return bit_result64(L, id, x);
|
||||
}
|
||||
setintV(L->base+1, sh);
|
||||
return FFH_RETRY;
|
||||
#else
|
||||
lj_lib_checknumber(L, 1);
|
||||
bit_checkbit(L, 2);
|
||||
return FFH_RETRY;
|
||||
#endif
|
||||
}
|
||||
LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR)
|
||||
LJLIB_ASM_(bit_arshift) LJLIB_REC(bit_shift IR_BSAR)
|
||||
LJLIB_ASM_(bit_rol) LJLIB_REC(bit_shift IR_BROL)
|
||||
LJLIB_ASM_(bit_ror) LJLIB_REC(bit_shift IR_BROR)
|
||||
|
||||
LJLIB_ASM(bit_band) LJLIB_REC(bit_nary IR_BAND)
|
||||
{
|
||||
#if LJ_HASFFI
|
||||
CTypeID id = 0;
|
||||
TValue *o = L->base, *top = L->top;
|
||||
int i = 0;
|
||||
do { lj_carith_check64(L, ++i, &id); } while (++o < top);
|
||||
if (id) {
|
||||
CTState *cts = ctype_cts(L);
|
||||
CType *ct = ctype_get(cts, id);
|
||||
int op = curr_func(L)->c.ffid - (int)FF_bit_bor;
|
||||
uint64_t x, y = op >= 0 ? 0 : ~(uint64_t)0;
|
||||
o = L->base;
|
||||
do {
|
||||
lj_cconv_ct_tv(cts, ct, (uint8_t *)&x, o, 0);
|
||||
if (op < 0) y &= x; else if (op == 0) y |= x; else y ^= x;
|
||||
} while (++o < top);
|
||||
return bit_result64(L, id, y);
|
||||
}
|
||||
return FFH_RETRY;
|
||||
#else
|
||||
int i = 0;
|
||||
do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);
|
||||
return FFH_RETRY;
|
||||
#endif
|
||||
}
|
||||
LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR)
|
||||
LJLIB_ASM_(bit_bxor) LJLIB_REC(bit_nary IR_BXOR)
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
LJLIB_CF(bit_tohex) LJLIB_REC(.)
|
||||
{
|
||||
#if LJ_HASFFI
|
||||
CTypeID id = 0, id2 = 0;
|
||||
uint64_t b = lj_carith_check64(L, 1, &id);
|
||||
int32_t n = L->base+1>=L->top ? (id ? 16 : 8) :
|
||||
(int32_t)lj_carith_check64(L, 2, &id2);
|
||||
#else
|
||||
uint32_t b = (uint32_t)bit_checkbit(L, 1);
|
||||
int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2);
|
||||
#endif
|
||||
SBuf *sb = lj_buf_tmp_(L);
|
||||
SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);
|
||||
if (n < 0) { n = (int32_t)(~(uint32_t)n+1u); sf |= STRFMT_F_UPPER; }
|
||||
if ((uint32_t)n > 254) n = 254;
|
||||
sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC);
|
||||
#if LJ_HASFFI
|
||||
if (n < 16) b &= ((uint64_t)1 << 4*n)-1;
|
||||
#else
|
||||
if (n < 8) b &= (1u << 4*n)-1;
|
||||
#endif
|
||||
sb = lj_strfmt_putfxint(sb, sf, b);
|
||||
setstrV(L, L->top-1, lj_buf_str(L, sb));
|
||||
lj_gc_check(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
#include "lj_libdef.h"
|
||||
|
||||
LUALIB_API int luaopen_bit(lua_State *L)
|
||||
{
|
||||
LJ_LIB_REG(L, LUA_BITLIBNAME, bit);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,331 @@
|
||||
/*
|
||||
** Table library.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
**
|
||||
** Major portions taken verbatim or adapted from the Lua interpreter.
|
||||
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lib_table_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#include "lj_obj.h"
|
||||
#include "lj_gc.h"
|
||||
#include "lj_err.h"
|
||||
#include "lj_buf.h"
|
||||
#include "lj_tab.h"
|
||||
#include "lj_ff.h"
|
||||
#include "lj_lib.h"
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
#define LJLIB_MODULE_table
|
||||
|
||||
LJLIB_LUA(table_foreachi) /*
|
||||
function(t, f)
|
||||
CHECK_tab(t)
|
||||
CHECK_func(f)
|
||||
for i=1,#t do
|
||||
local r = f(i, t[i])
|
||||
if r ~= nil then return r end
|
||||
end
|
||||
end
|
||||
*/
|
||||
|
||||
LJLIB_LUA(table_foreach) /*
|
||||
function(t, f)
|
||||
CHECK_tab(t)
|
||||
CHECK_func(f)
|
||||
for k, v in PAIRS(t) do
|
||||
local r = f(k, v)
|
||||
if r ~= nil then return r end
|
||||
end
|
||||
end
|
||||
*/
|
||||
|
||||
LJLIB_LUA(table_getn) /*
|
||||
function(t)
|
||||
CHECK_tab(t)
|
||||
return #t
|
||||
end
|
||||
*/
|
||||
|
||||
LJLIB_CF(table_maxn)
|
||||
{
|
||||
GCtab *t = lj_lib_checktab(L, 1);
|
||||
TValue *array = tvref(t->array);
|
||||
Node *node;
|
||||
lua_Number m = 0;
|
||||
ptrdiff_t i;
|
||||
for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--)
|
||||
if (!tvisnil(&array[i])) {
|
||||
m = (lua_Number)(int32_t)i;
|
||||
break;
|
||||
}
|
||||
node = noderef(t->node);
|
||||
for (i = (ptrdiff_t)t->hmask; i >= 0; i--)
|
||||
if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) {
|
||||
lua_Number n = numberVnum(&node[i].key);
|
||||
if (n > m) m = n;
|
||||
}
|
||||
#if LJ_INTONLY
|
||||
setintV(L->top-1, lj_num2int(m));
|
||||
#else
|
||||
setnumV(L->top-1, m);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_CF(table_insert) LJLIB_REC(.)
|
||||
{
|
||||
GCtab *t = lj_lib_checktab(L, 1);
|
||||
int32_t n, i = (int32_t)lj_tab_len(t) + 1;
|
||||
int nargs = (int)((char *)L->top - (char *)L->base);
|
||||
if (nargs != 2*sizeof(TValue)) {
|
||||
if (nargs != 3*sizeof(TValue))
|
||||
lj_err_caller(L, LJ_ERR_TABINS);
|
||||
/* NOBARRIER: This just moves existing elements around. */
|
||||
for (n = lj_lib_checkint(L, 2); i > n; i--) {
|
||||
/* The set may invalidate the get pointer, so need to do it first! */
|
||||
TValue *dst = lj_tab_setint(L, t, i);
|
||||
cTValue *src = lj_tab_getint(t, i-1);
|
||||
if (src) {
|
||||
copyTV(L, dst, src);
|
||||
} else {
|
||||
setnilV(dst);
|
||||
}
|
||||
}
|
||||
i = n;
|
||||
}
|
||||
{
|
||||
TValue *dst = lj_tab_setint(L, t, i);
|
||||
copyTV(L, dst, L->top-1); /* Set new value. */
|
||||
lj_gc_barriert(L, t, dst);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
LJLIB_LUA(table_remove) /*
|
||||
function(t, pos)
|
||||
CHECK_tab(t)
|
||||
local len = #t
|
||||
if pos == nil then
|
||||
if len ~= 0 then
|
||||
local old = t[len]
|
||||
t[len] = nil
|
||||
return old
|
||||
end
|
||||
else
|
||||
CHECK_int(pos)
|
||||
if pos >= 1 and pos <= len then
|
||||
local old = t[pos]
|
||||
for i=pos+1,len do
|
||||
t[i-1] = t[i]
|
||||
end
|
||||
t[len] = nil
|
||||
return old
|
||||
end
|
||||
end
|
||||
end
|
||||
*/
|
||||
|
||||
LJLIB_LUA(table_move) /*
|
||||
function(a1, f, e, t, a2)
|
||||
CHECK_tab(a1)
|
||||
CHECK_int(f)
|
||||
CHECK_int(e)
|
||||
CHECK_int(t)
|
||||
if a2 == nil then a2 = a1 end
|
||||
CHECK_tab(a2)
|
||||
if e >= f then
|
||||
local d = t - f
|
||||
if t > e or t <= f or a2 ~= a1 then
|
||||
for i=f,e do a2[i+d] = a1[i] end
|
||||
else
|
||||
for i=e,f,-1 do a2[i+d] = a1[i] end
|
||||
end
|
||||
end
|
||||
return a2
|
||||
end
|
||||
*/
|
||||
|
||||
LJLIB_CF(table_concat) LJLIB_REC(.)
|
||||
{
|
||||
GCtab *t = lj_lib_checktab(L, 1);
|
||||
GCstr *sep = lj_lib_optstr(L, 2);
|
||||
int32_t i = lj_lib_optint(L, 3, 1);
|
||||
int32_t e = (L->base+3 < L->top && !tvisnil(L->base+3)) ?
|
||||
lj_lib_checkint(L, 4) : (int32_t)lj_tab_len(t);
|
||||
SBuf *sb = lj_buf_tmp_(L);
|
||||
SBuf *sbx = lj_buf_puttab(sb, t, sep, i, e);
|
||||
if (LJ_UNLIKELY(!sbx)) { /* Error: bad element type. */
|
||||
int32_t idx = (int32_t)(intptr_t)sb->w;
|
||||
cTValue *o = lj_tab_getint(t, idx);
|
||||
lj_err_callerv(L, LJ_ERR_TABCAT,
|
||||
lj_obj_itypename[o ? itypemap(o) : ~LJ_TNIL], idx);
|
||||
}
|
||||
setstrV(L, L->top-1, lj_buf_str(L, sbx));
|
||||
lj_gc_check(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
static void set2(lua_State *L, int i, int j)
|
||||
{
|
||||
lua_rawseti(L, 1, i);
|
||||
lua_rawseti(L, 1, j);
|
||||
}
|
||||
|
||||
static int sort_comp(lua_State *L, int a, int b)
|
||||
{
|
||||
if (!lua_isnil(L, 2)) { /* function? */
|
||||
int res;
|
||||
lua_pushvalue(L, 2);
|
||||
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
||||
lua_call(L, 2, 1);
|
||||
res = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
} else { /* a < b? */
|
||||
return lua_lessthan(L, a, b);
|
||||
}
|
||||
}
|
||||
|
||||
static void auxsort(lua_State *L, int l, int u)
|
||||
{
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
lua_rawgeti(L, 1, l);
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
|
||||
set2(L, l, u); /* swap a[l] - a[u] */
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
lua_rawgeti(L, 1, i);
|
||||
lua_rawgeti(L, 1, l);
|
||||
if (sort_comp(L, -2, -1)) { /* a[i]<a[l]? */
|
||||
set2(L, i, l);
|
||||
} else {
|
||||
lua_pop(L, 1); /* remove a[l] */
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
|
||||
set2(L, i, u);
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
lua_rawgeti(L, 1, i); /* Pivot */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_rawgeti(L, 1, u-1);
|
||||
set2(L, i, u-1);
|
||||
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
|
||||
i = l; j = u-1;
|
||||
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
||||
/* repeat ++i until a[i] >= P */
|
||||
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||
if (i>=u) lj_err_caller(L, LJ_ERR_TABSORT);
|
||||
lua_pop(L, 1); /* remove a[i] */
|
||||
}
|
||||
/* repeat --j until a[j] <= P */
|
||||
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||
if (j<=l) lj_err_caller(L, LJ_ERR_TABSORT);
|
||||
lua_pop(L, 1); /* remove a[j] */
|
||||
}
|
||||
if (j<i) {
|
||||
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
|
||||
break;
|
||||
}
|
||||
set2(L, i, j);
|
||||
}
|
||||
lua_rawgeti(L, 1, u-1);
|
||||
lua_rawgeti(L, 1, i);
|
||||
set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
||||
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
|
||||
if (i-l < u-i) {
|
||||
j=l; i=i-1; l=i+2;
|
||||
} else {
|
||||
j=i+1; i=u; u=j-2;
|
||||
}
|
||||
auxsort(L, j, i); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
LJLIB_CF(table_sort)
|
||||
{
|
||||
GCtab *t = lj_lib_checktab(L, 1);
|
||||
int32_t n = (int32_t)lj_tab_len(t);
|
||||
lua_settop(L, 2);
|
||||
if (!tvisnil(L->base+1))
|
||||
lj_lib_checkfunc(L, 2);
|
||||
auxsort(L, 1, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if LJ_52
|
||||
LJLIB_PUSH("n")
|
||||
LJLIB_CF(table_pack)
|
||||
{
|
||||
TValue *array, *base = L->base;
|
||||
MSize i, n = (uint32_t)(L->top - base);
|
||||
GCtab *t = lj_tab_new(L, n ? n+1 : 0, 1);
|
||||
/* NOBARRIER: The table is new (marked white). */
|
||||
setintV(lj_tab_setstr(L, t, strV(lj_lib_upvalue(L, 1))), (int32_t)n);
|
||||
for (array = tvref(t->array) + 1, i = 0; i < n; i++)
|
||||
copyTV(L, &array[i], &base[i]);
|
||||
settabV(L, base, t);
|
||||
L->top = base+1;
|
||||
lj_gc_check(L);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
LJLIB_NOREG LJLIB_CF(table_new) LJLIB_REC(.)
|
||||
{
|
||||
int32_t a = lj_lib_checkint(L, 1);
|
||||
int32_t h = lj_lib_checkint(L, 2);
|
||||
lua_createtable(L, a, h);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LJLIB_NOREG LJLIB_CF(table_clear) LJLIB_REC(.)
|
||||
{
|
||||
lj_tab_clear(lj_lib_checktab(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int luaopen_table_new(lua_State *L)
|
||||
{
|
||||
return lj_lib_postreg(L, lj_cf_table_new, FF_table_new, "new");
|
||||
}
|
||||
|
||||
static int luaopen_table_clear(lua_State *L)
|
||||
{
|
||||
return lj_lib_postreg(L, lj_cf_table_clear, FF_table_clear, "clear");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
#include "lj_libdef.h"
|
||||
|
||||
LUALIB_API int luaopen_table(lua_State *L)
|
||||
{
|
||||
LJ_LIB_REG(L, LUA_TABLIBNAME, table);
|
||||
#if LJ_52
|
||||
lua_getglobal(L, "unpack");
|
||||
lua_setfield(L, -2, "unpack");
|
||||
#endif
|
||||
lj_lib_prereg(L, LUA_TABLIBNAME ".new", luaopen_table_new, tabV(L->top-1));
|
||||
lj_lib_prereg(L, LUA_TABLIBNAME ".clear", luaopen_table_clear, tabV(L->top-1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,456 @@
|
||||
/*
|
||||
** Bytecode reader.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
*/
|
||||
|
||||
#define lj_bcread_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lj_obj.h"
|
||||
#include "lj_gc.h"
|
||||
#include "lj_err.h"
|
||||
#include "lj_buf.h"
|
||||
#include "lj_str.h"
|
||||
#include "lj_tab.h"
|
||||
#include "lj_bc.h"
|
||||
#if LJ_HASFFI
|
||||
#include "lj_ctype.h"
|
||||
#include "lj_cdata.h"
|
||||
#include "lualib.h"
|
||||
#endif
|
||||
#include "lj_lex.h"
|
||||
#include "lj_bcdump.h"
|
||||
#include "lj_state.h"
|
||||
#include "lj_strfmt.h"
|
||||
|
||||
/* Reuse some lexer fields for our own purposes. */
|
||||
#define bcread_flags(ls) ls->level
|
||||
#define bcread_swap(ls) \
|
||||
((bcread_flags(ls) & BCDUMP_F_BE) != LJ_BE*BCDUMP_F_BE)
|
||||
#define bcread_oldtop(L, ls) restorestack(L, ls->lastline)
|
||||
#define bcread_savetop(L, ls, top) \
|
||||
ls->lastline = (BCLine)savestack(L, (top))
|
||||
|
||||
/* -- Input buffer handling ----------------------------------------------- */
|
||||
|
||||
/* Throw reader error. */
|
||||
static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em)
|
||||
{
|
||||
lua_State *L = ls->L;
|
||||
const char *name = ls->chunkarg;
|
||||
if (*name == BCDUMP_HEAD1) name = "(binary)";
|
||||
else if (*name == '@' || *name == '=') name++;
|
||||
lj_strfmt_pushf(L, "%s: %s", name, err2msg(em));
|
||||
lj_err_throw(L, LUA_ERRSYNTAX);
|
||||
}
|
||||
|
||||
/* Refill buffer. */
|
||||
static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
|
||||
{
|
||||
lj_assertLS(len != 0, "empty refill");
|
||||
if (len > LJ_MAX_BUF || ls->c < 0)
|
||||
bcread_error(ls, LJ_ERR_BCBAD);
|
||||
do {
|
||||
const char *buf;
|
||||
size_t sz;
|
||||
char *p = ls->sb.b;
|
||||
MSize n = (MSize)(ls->pe - ls->p);
|
||||
if (n) { /* Copy remainder to buffer. */
|
||||
if (sbuflen(&ls->sb)) { /* Move down in buffer. */
|
||||
lj_assertLS(ls->pe == ls->sb.w, "bad buffer pointer");
|
||||
if (ls->p != p) memmove(p, ls->p, n);
|
||||
} else { /* Copy from buffer provided by reader. */
|
||||
p = lj_buf_need(&ls->sb, len);
|
||||
memcpy(p, ls->p, n);
|
||||
}
|
||||
ls->p = p;
|
||||
ls->pe = p + n;
|
||||
}
|
||||
ls->sb.w = p + n;
|
||||
buf = ls->rfunc(ls->L, ls->rdata, &sz); /* Get more data from reader. */
|
||||
if (buf == NULL || sz == 0) { /* EOF? */
|
||||
if (need) bcread_error(ls, LJ_ERR_BCBAD);
|
||||
ls->c = -1; /* Only bad if we get called again. */
|
||||
break;
|
||||
}
|
||||
if (sz >= LJ_MAX_BUF - n) lj_err_mem(ls->L);
|
||||
if (n) { /* Append to buffer. */
|
||||
n += (MSize)sz;
|
||||
p = lj_buf_need(&ls->sb, n < len ? len : n);
|
||||
memcpy(ls->sb.w, buf, sz);
|
||||
ls->sb.w = p + n;
|
||||
ls->p = p;
|
||||
ls->pe = p + n;
|
||||
} else { /* Return buffer provided by reader. */
|
||||
ls->p = buf;
|
||||
ls->pe = buf + sz;
|
||||
}
|
||||
} while ((MSize)(ls->pe - ls->p) < len);
|
||||
}
|
||||
|
||||
/* Need a certain number of bytes. */
|
||||
static LJ_AINLINE void bcread_need(LexState *ls, MSize len)
|
||||
{
|
||||
if (LJ_UNLIKELY((MSize)(ls->pe - ls->p) < len))
|
||||
bcread_fill(ls, len, 1);
|
||||
}
|
||||
|
||||
/* Want to read up to a certain number of bytes, but may need less. */
|
||||
static LJ_AINLINE void bcread_want(LexState *ls, MSize len)
|
||||
{
|
||||
if (LJ_UNLIKELY((MSize)(ls->pe - ls->p) < len))
|
||||
bcread_fill(ls, len, 0);
|
||||
}
|
||||
|
||||
/* Return memory block from buffer. */
|
||||
static LJ_AINLINE uint8_t *bcread_mem(LexState *ls, MSize len)
|
||||
{
|
||||
uint8_t *p = (uint8_t *)ls->p;
|
||||
ls->p += len;
|
||||
lj_assertLS(ls->p <= ls->pe, "buffer read overflow");
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Copy memory block from buffer. */
|
||||
static void bcread_block(LexState *ls, void *q, MSize len)
|
||||
{
|
||||
memcpy(q, bcread_mem(ls, len), len);
|
||||
}
|
||||
|
||||
/* Read byte from buffer. */
|
||||
static LJ_AINLINE uint32_t bcread_byte(LexState *ls)
|
||||
{
|
||||
lj_assertLS(ls->p < ls->pe, "buffer read overflow");
|
||||
return (uint32_t)(uint8_t)*ls->p++;
|
||||
}
|
||||
|
||||
/* Read ULEB128 value from buffer. */
|
||||
static LJ_AINLINE uint32_t bcread_uleb128(LexState *ls)
|
||||
{
|
||||
uint32_t v = lj_buf_ruleb128(&ls->p);
|
||||
lj_assertLS(ls->p <= ls->pe, "buffer read overflow");
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Read top 32 bits of 33 bit ULEB128 value from buffer. */
|
||||
static uint32_t bcread_uleb128_33(LexState *ls)
|
||||
{
|
||||
const uint8_t *p = (const uint8_t *)ls->p;
|
||||
uint32_t v = (*p++ >> 1);
|
||||
if (LJ_UNLIKELY(v >= 0x40)) {
|
||||
int sh = -1;
|
||||
v &= 0x3f;
|
||||
do {
|
||||
v |= ((*p & 0x7f) << (sh += 7));
|
||||
} while (*p++ >= 0x80);
|
||||
}
|
||||
ls->p = (char *)p;
|
||||
lj_assertLS(ls->p <= ls->pe, "buffer read overflow");
|
||||
return v;
|
||||
}
|
||||
|
||||
/* -- Bytecode reader ----------------------------------------------------- */
|
||||
|
||||
/* Read debug info of a prototype. */
|
||||
static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg)
|
||||
{
|
||||
void *lineinfo = (void *)proto_lineinfo(pt);
|
||||
bcread_block(ls, lineinfo, sizedbg);
|
||||
/* Swap lineinfo if the endianess differs. */
|
||||
if (bcread_swap(ls) && pt->numline >= 256) {
|
||||
MSize i, n = pt->sizebc-1;
|
||||
if (pt->numline < 65536) {
|
||||
uint16_t *p = (uint16_t *)lineinfo;
|
||||
for (i = 0; i < n; i++) p[i] = (uint16_t)((p[i] >> 8)|(p[i] << 8));
|
||||
} else {
|
||||
uint32_t *p = (uint32_t *)lineinfo;
|
||||
for (i = 0; i < n; i++) p[i] = lj_bswap(p[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Find pointer to varinfo. */
|
||||
static const void *bcread_varinfo(GCproto *pt)
|
||||
{
|
||||
const uint8_t *p = proto_uvinfo(pt);
|
||||
MSize n = pt->sizeuv;
|
||||
if (n) while (*p++ || --n) ;
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Read a single constant key/value of a template table. */
|
||||
static void bcread_ktabk(LexState *ls, TValue *o)
|
||||
{
|
||||
MSize tp = bcread_uleb128(ls);
|
||||
if (tp >= BCDUMP_KTAB_STR) {
|
||||
MSize len = tp - BCDUMP_KTAB_STR;
|
||||
const char *p = (const char *)bcread_mem(ls, len);
|
||||
setstrV(ls->L, o, lj_str_new(ls->L, p, len));
|
||||
} else if (tp == BCDUMP_KTAB_INT) {
|
||||
setintV(o, (int32_t)bcread_uleb128(ls));
|
||||
} else if (tp == BCDUMP_KTAB_NUM) {
|
||||
o->u32.lo = bcread_uleb128(ls);
|
||||
o->u32.hi = bcread_uleb128(ls);
|
||||
} else {
|
||||
lj_assertLS(tp <= BCDUMP_KTAB_TRUE, "bad constant type %d", tp);
|
||||
setpriV(o, ~tp);
|
||||
}
|
||||
}
|
||||
|
||||
/* Read a template table. */
|
||||
static GCtab *bcread_ktab(LexState *ls)
|
||||
{
|
||||
MSize narray = bcread_uleb128(ls);
|
||||
MSize nhash = bcread_uleb128(ls);
|
||||
GCtab *t = lj_tab_new(ls->L, narray, hsize2hbits(nhash));
|
||||
if (narray) { /* Read array entries. */
|
||||
MSize i;
|
||||
TValue *o = tvref(t->array);
|
||||
for (i = 0; i < narray; i++, o++)
|
||||
bcread_ktabk(ls, o);
|
||||
}
|
||||
if (nhash) { /* Read hash entries. */
|
||||
MSize i;
|
||||
for (i = 0; i < nhash; i++) {
|
||||
TValue key;
|
||||
bcread_ktabk(ls, &key);
|
||||
lj_assertLS(!tvisnil(&key), "nil key");
|
||||
bcread_ktabk(ls, lj_tab_set(ls->L, t, &key));
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
/* Read GC constants of a prototype. */
|
||||
static void bcread_kgc(LexState *ls, GCproto *pt, MSize sizekgc)
|
||||
{
|
||||
MSize i;
|
||||
GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
|
||||
for (i = 0; i < sizekgc; i++, kr++) {
|
||||
MSize tp = bcread_uleb128(ls);
|
||||
if (tp >= BCDUMP_KGC_STR) {
|
||||
MSize len = tp - BCDUMP_KGC_STR;
|
||||
const char *p = (const char *)bcread_mem(ls, len);
|
||||
setgcref(*kr, obj2gco(lj_str_new(ls->L, p, len)));
|
||||
} else if (tp == BCDUMP_KGC_TAB) {
|
||||
setgcref(*kr, obj2gco(bcread_ktab(ls)));
|
||||
#if LJ_HASFFI
|
||||
} else if (tp != BCDUMP_KGC_CHILD) {
|
||||
CTypeID id = tp == BCDUMP_KGC_COMPLEX ? CTID_COMPLEX_DOUBLE :
|
||||
tp == BCDUMP_KGC_I64 ? CTID_INT64 : CTID_UINT64;
|
||||
CTSize sz = tp == BCDUMP_KGC_COMPLEX ? 16 : 8;
|
||||
GCcdata *cd = lj_cdata_new_(ls->L, id, sz);
|
||||
TValue *p = (TValue *)cdataptr(cd);
|
||||
setgcref(*kr, obj2gco(cd));
|
||||
p[0].u32.lo = bcread_uleb128(ls);
|
||||
p[0].u32.hi = bcread_uleb128(ls);
|
||||
if (tp == BCDUMP_KGC_COMPLEX) {
|
||||
p[1].u32.lo = bcread_uleb128(ls);
|
||||
p[1].u32.hi = bcread_uleb128(ls);
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
lua_State *L = ls->L;
|
||||
lj_assertLS(tp == BCDUMP_KGC_CHILD, "bad constant type %d", tp);
|
||||
if (L->top <= bcread_oldtop(L, ls)) /* Stack underflow? */
|
||||
bcread_error(ls, LJ_ERR_BCBAD);
|
||||
L->top--;
|
||||
setgcref(*kr, obj2gco(protoV(L->top)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Read number constants of a prototype. */
|
||||
static void bcread_knum(LexState *ls, GCproto *pt, MSize sizekn)
|
||||
{
|
||||
MSize i;
|
||||
TValue *o = mref(pt->k, TValue);
|
||||
for (i = 0; i < sizekn; i++, o++) {
|
||||
int isnum = (ls->p[0] & 1);
|
||||
uint32_t lo = bcread_uleb128_33(ls);
|
||||
if (isnum) {
|
||||
o->u32.lo = lo;
|
||||
o->u32.hi = bcread_uleb128(ls);
|
||||
} else {
|
||||
setintV(o, lo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Read bytecode instructions. */
|
||||
static void bcread_bytecode(LexState *ls, GCproto *pt, MSize sizebc)
|
||||
{
|
||||
BCIns *bc = proto_bc(pt);
|
||||
BCIns op;
|
||||
if (ls->fr2 != LJ_FR2) op = BC_NOT; /* Mark non-native prototype. */
|
||||
else if ((pt->flags & PROTO_VARARG)) op = BC_FUNCV;
|
||||
else op = BC_FUNCF;
|
||||
bc[0] = BCINS_AD(op, pt->framesize, 0);
|
||||
bcread_block(ls, bc+1, (sizebc-1)*(MSize)sizeof(BCIns));
|
||||
/* Swap bytecode instructions if the endianess differs. */
|
||||
if (bcread_swap(ls)) {
|
||||
MSize i;
|
||||
for (i = 1; i < sizebc; i++) bc[i] = lj_bswap(bc[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Read upvalue refs. */
|
||||
static void bcread_uv(LexState *ls, GCproto *pt, MSize sizeuv)
|
||||
{
|
||||
if (sizeuv) {
|
||||
uint16_t *uv = proto_uv(pt);
|
||||
bcread_block(ls, uv, sizeuv*2);
|
||||
/* Swap upvalue refs if the endianess differs. */
|
||||
if (bcread_swap(ls)) {
|
||||
MSize i;
|
||||
for (i = 0; i < sizeuv; i++)
|
||||
uv[i] = (uint16_t)((uv[i] >> 8)|(uv[i] << 8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Read a prototype. */
|
||||
GCproto *lj_bcread_proto(LexState *ls)
|
||||
{
|
||||
GCproto *pt;
|
||||
MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept;
|
||||
MSize ofsk, ofsuv, ofsdbg;
|
||||
MSize sizedbg = 0;
|
||||
BCLine firstline = 0, numline = 0;
|
||||
|
||||
/* Read prototype header. */
|
||||
flags = bcread_byte(ls);
|
||||
numparams = bcread_byte(ls);
|
||||
framesize = bcread_byte(ls);
|
||||
sizeuv = bcread_byte(ls);
|
||||
sizekgc = bcread_uleb128(ls);
|
||||
sizekn = bcread_uleb128(ls);
|
||||
sizebc = bcread_uleb128(ls) + 1;
|
||||
if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) {
|
||||
sizedbg = bcread_uleb128(ls);
|
||||
if (sizedbg) {
|
||||
firstline = bcread_uleb128(ls);
|
||||
numline = bcread_uleb128(ls);
|
||||
}
|
||||
}
|
||||
|
||||
/* Calculate total size of prototype including all colocated arrays. */
|
||||
sizept = (MSize)sizeof(GCproto) +
|
||||
sizebc*(MSize)sizeof(BCIns) +
|
||||
sizekgc*(MSize)sizeof(GCRef);
|
||||
sizept = (sizept + (MSize)sizeof(TValue)-1) & ~((MSize)sizeof(TValue)-1);
|
||||
ofsk = sizept; sizept += sizekn*(MSize)sizeof(TValue);
|
||||
ofsuv = sizept; sizept += ((sizeuv+1)&~1)*2;
|
||||
ofsdbg = sizept; sizept += sizedbg;
|
||||
|
||||
/* Allocate prototype object and initialize its fields. */
|
||||
pt = (GCproto *)lj_mem_newgco(ls->L, (MSize)sizept);
|
||||
pt->gct = ~LJ_TPROTO;
|
||||
pt->numparams = (uint8_t)numparams;
|
||||
pt->framesize = (uint8_t)framesize;
|
||||
pt->sizebc = sizebc;
|
||||
setmref(pt->k, (char *)pt + ofsk);
|
||||
setmref(pt->uv, (char *)pt + ofsuv);
|
||||
pt->sizekgc = 0; /* Set to zero until fully initialized. */
|
||||
pt->sizekn = sizekn;
|
||||
pt->sizept = sizept;
|
||||
pt->sizeuv = (uint8_t)sizeuv;
|
||||
pt->flags = (uint8_t)flags;
|
||||
pt->trace = 0;
|
||||
setgcref(pt->chunkname, obj2gco(ls->chunkname));
|
||||
|
||||
/* Close potentially uninitialized gap between bc and kgc. */
|
||||
*(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(sizekgc+1)) = 0;
|
||||
|
||||
/* Read bytecode instructions and upvalue refs. */
|
||||
bcread_bytecode(ls, pt, sizebc);
|
||||
bcread_uv(ls, pt, sizeuv);
|
||||
|
||||
/* Read constants. */
|
||||
bcread_kgc(ls, pt, sizekgc);
|
||||
pt->sizekgc = sizekgc;
|
||||
bcread_knum(ls, pt, sizekn);
|
||||
|
||||
/* Read and initialize debug info. */
|
||||
pt->firstline = firstline;
|
||||
pt->numline = numline;
|
||||
if (sizedbg) {
|
||||
MSize sizeli = (sizebc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);
|
||||
setmref(pt->lineinfo, (char *)pt + ofsdbg);
|
||||
setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli);
|
||||
bcread_dbg(ls, pt, sizedbg);
|
||||
setmref(pt->varinfo, bcread_varinfo(pt));
|
||||
} else {
|
||||
setmref(pt->lineinfo, NULL);
|
||||
setmref(pt->uvinfo, NULL);
|
||||
setmref(pt->varinfo, NULL);
|
||||
}
|
||||
return pt;
|
||||
}
|
||||
|
||||
/* Read and check header of bytecode dump. */
|
||||
static int bcread_header(LexState *ls)
|
||||
{
|
||||
uint32_t flags;
|
||||
bcread_want(ls, 3+5+5);
|
||||
if (bcread_byte(ls) != BCDUMP_HEAD2 ||
|
||||
bcread_byte(ls) != BCDUMP_HEAD3 ||
|
||||
bcread_byte(ls) != BCDUMP_VERSION) return 0;
|
||||
bcread_flags(ls) = flags = bcread_uleb128(ls);
|
||||
if ((flags & ~(BCDUMP_F_KNOWN)) != 0) return 0;
|
||||
if ((flags & BCDUMP_F_FR2) != (uint32_t)ls->fr2*BCDUMP_F_FR2) return 0;
|
||||
if ((flags & BCDUMP_F_FFI)) {
|
||||
#if LJ_HASFFI
|
||||
lua_State *L = ls->L;
|
||||
ctype_loadffi(L);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
if ((flags & BCDUMP_F_STRIP)) {
|
||||
ls->chunkname = lj_str_newz(ls->L, *ls->chunkarg == BCDUMP_HEAD1 ? "=?" : ls->chunkarg);
|
||||
} else {
|
||||
MSize len = bcread_uleb128(ls);
|
||||
bcread_need(ls, len);
|
||||
ls->chunkname = lj_str_new(ls->L, (const char *)bcread_mem(ls, len), len);
|
||||
}
|
||||
return 1; /* Ok. */
|
||||
}
|
||||
|
||||
/* Read a bytecode dump. */
|
||||
GCproto *lj_bcread(LexState *ls)
|
||||
{
|
||||
lua_State *L = ls->L;
|
||||
lj_assertLS(ls->c == BCDUMP_HEAD1, "bad bytecode header");
|
||||
bcread_savetop(L, ls, L->top);
|
||||
lj_buf_reset(&ls->sb);
|
||||
/* Check for a valid bytecode dump header. */
|
||||
if (!bcread_header(ls))
|
||||
bcread_error(ls, LJ_ERR_BCFMT);
|
||||
for (;;) { /* Process all prototypes in the bytecode dump. */
|
||||
GCproto *pt;
|
||||
MSize len;
|
||||
const char *startp;
|
||||
/* Read length. */
|
||||
if (ls->p < ls->pe && ls->p[0] == 0) { /* Shortcut EOF. */
|
||||
ls->p++;
|
||||
break;
|
||||
}
|
||||
bcread_want(ls, 5);
|
||||
len = bcread_uleb128(ls);
|
||||
if (!len) break; /* EOF */
|
||||
bcread_need(ls, len);
|
||||
startp = ls->p;
|
||||
pt = lj_bcread_proto(ls);
|
||||
if (ls->p != startp + len)
|
||||
bcread_error(ls, LJ_ERR_BCBAD);
|
||||
setprotoV(L, L->top, pt);
|
||||
incr_top(L);
|
||||
}
|
||||
if ((ls->pe != ls->p && !ls->endmark) || L->top-1 != bcread_oldtop(L, ls))
|
||||
bcread_error(ls, LJ_ERR_BCBAD);
|
||||
/* Pop off last prototype. */
|
||||
L->top--;
|
||||
return protoV(L->top);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,692 @@
|
||||
/*
|
||||
** Table handling.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
**
|
||||
** Major portions taken verbatim or adapted from the Lua interpreter.
|
||||
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lj_tab_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lj_obj.h"
|
||||
#include "lj_gc.h"
|
||||
#include "lj_err.h"
|
||||
#include "lj_tab.h"
|
||||
|
||||
/* -- Object hashing ------------------------------------------------------ */
|
||||
|
||||
/* Hash an arbitrary key and return its anchor position in the hash table. */
|
||||
static Node *hashkey(const GCtab *t, cTValue *key)
|
||||
{
|
||||
lj_assertX(!tvisint(key), "attempt to hash integer");
|
||||
if (tvisstr(key))
|
||||
return hashstr(t, strV(key));
|
||||
else if (tvisnum(key))
|
||||
return hashnum(t, key);
|
||||
else if (tvisbool(key))
|
||||
return hashmask(t, boolV(key));
|
||||
else
|
||||
return hashgcref(t, key->gcr);
|
||||
/* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */
|
||||
}
|
||||
|
||||
/* -- Table creation and destruction -------------------------------------- */
|
||||
|
||||
/* Create new hash part for table. */
|
||||
static LJ_AINLINE void newhpart(lua_State *L, GCtab *t, uint32_t hbits)
|
||||
{
|
||||
uint32_t hsize;
|
||||
Node *node;
|
||||
lj_assertL(hbits != 0, "zero hash size");
|
||||
if (hbits > LJ_MAX_HBITS)
|
||||
lj_err_msg(L, LJ_ERR_TABOV);
|
||||
hsize = 1u << hbits;
|
||||
node = lj_mem_newvec(L, hsize, Node);
|
||||
setmref(t->node, node);
|
||||
setfreetop(t, node, &node[hsize]);
|
||||
t->hmask = hsize-1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Q: Why all of these copies of t->hmask, t->node etc. to local variables?
|
||||
** A: Because alias analysis for C is _really_ tough.
|
||||
** Even state-of-the-art C compilers won't produce good code without this.
|
||||
*/
|
||||
|
||||
/* Clear hash part of table. */
|
||||
static LJ_AINLINE void clearhpart(GCtab *t)
|
||||
{
|
||||
uint32_t i, hmask = t->hmask;
|
||||
Node *node = noderef(t->node);
|
||||
lj_assertX(t->hmask != 0, "empty hash part");
|
||||
for (i = 0; i <= hmask; i++) {
|
||||
Node *n = &node[i];
|
||||
setmref(n->next, NULL);
|
||||
setnilV(&n->key);
|
||||
setnilV(&n->val);
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear array part of table. */
|
||||
static LJ_AINLINE void clearapart(GCtab *t)
|
||||
{
|
||||
uint32_t i, asize = t->asize;
|
||||
TValue *array = tvref(t->array);
|
||||
for (i = 0; i < asize; i++)
|
||||
setnilV(&array[i]);
|
||||
}
|
||||
|
||||
/* Create a new table. Note: the slots are not initialized (yet). */
|
||||
static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
|
||||
{
|
||||
GCtab *t;
|
||||
/* First try to colocate the array part. */
|
||||
if (LJ_MAX_COLOSIZE != 0 && asize > 0 && asize <= LJ_MAX_COLOSIZE) {
|
||||
Node *nilnode;
|
||||
lj_assertL((sizeof(GCtab) & 7) == 0, "bad GCtab size");
|
||||
t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));
|
||||
t->gct = ~LJ_TTAB;
|
||||
t->nomm = (uint8_t)~0;
|
||||
t->colo = (int8_t)asize;
|
||||
setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));
|
||||
setgcrefnull(t->metatable);
|
||||
t->asize = asize;
|
||||
t->hmask = 0;
|
||||
nilnode = &G(L)->nilnode;
|
||||
setmref(t->node, nilnode);
|
||||
#if LJ_GC64
|
||||
setmref(t->freetop, nilnode);
|
||||
#endif
|
||||
} else { /* Otherwise separately allocate the array part. */
|
||||
Node *nilnode;
|
||||
t = lj_mem_newobj(L, GCtab);
|
||||
t->gct = ~LJ_TTAB;
|
||||
t->nomm = (uint8_t)~0;
|
||||
t->colo = 0;
|
||||
setmref(t->array, NULL);
|
||||
setgcrefnull(t->metatable);
|
||||
t->asize = 0; /* In case the array allocation fails. */
|
||||
t->hmask = 0;
|
||||
nilnode = &G(L)->nilnode;
|
||||
setmref(t->node, nilnode);
|
||||
#if LJ_GC64
|
||||
setmref(t->freetop, nilnode);
|
||||
#endif
|
||||
if (asize > 0) {
|
||||
if (asize > LJ_MAX_ASIZE)
|
||||
lj_err_msg(L, LJ_ERR_TABOV);
|
||||
setmref(t->array, lj_mem_newvec(L, asize, TValue));
|
||||
t->asize = asize;
|
||||
}
|
||||
}
|
||||
if (hbits)
|
||||
newhpart(L, t, hbits);
|
||||
return t;
|
||||
}
|
||||
|
||||
/* Create a new table.
|
||||
**
|
||||
** IMPORTANT NOTE: The API differs from lua_createtable()!
|
||||
**
|
||||
** The array size is non-inclusive. E.g. asize=128 creates array slots
|
||||
** for 0..127, but not for 128. If you need slots 1..128, pass asize=129
|
||||
** (slot 0 is wasted in this case).
|
||||
**
|
||||
** The hash size is given in hash bits. hbits=0 means no hash part.
|
||||
** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on.
|
||||
*/
|
||||
GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits)
|
||||
{
|
||||
GCtab *t = newtab(L, asize, hbits);
|
||||
clearapart(t);
|
||||
if (t->hmask > 0) clearhpart(t);
|
||||
return t;
|
||||
}
|
||||
|
||||
/* The API of this function conforms to lua_createtable(). */
|
||||
GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h)
|
||||
{
|
||||
return lj_tab_new(L, (uint32_t)(a > 0 ? a+1 : 0), hsize2hbits(h));
|
||||
}
|
||||
|
||||
#if LJ_HASJIT
|
||||
GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize)
|
||||
{
|
||||
GCtab *t = newtab(L, ahsize & 0xffffff, ahsize >> 24);
|
||||
clearapart(t);
|
||||
if (t->hmask > 0) clearhpart(t);
|
||||
return t;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Duplicate a table. */
|
||||
GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt)
|
||||
{
|
||||
GCtab *t;
|
||||
uint32_t asize, hmask;
|
||||
t = newtab(L, kt->asize, kt->hmask > 0 ? lj_fls(kt->hmask)+1 : 0);
|
||||
lj_assertL(kt->asize == t->asize && kt->hmask == t->hmask,
|
||||
"mismatched size of table and template");
|
||||
t->nomm = 0; /* Keys with metamethod names may be present. */
|
||||
asize = kt->asize;
|
||||
if (asize > 0) {
|
||||
TValue *array = tvref(t->array);
|
||||
TValue *karray = tvref(kt->array);
|
||||
if (asize < 64) { /* An inlined loop beats memcpy for < 512 bytes. */
|
||||
uint32_t i;
|
||||
for (i = 0; i < asize; i++)
|
||||
copyTV(L, &array[i], &karray[i]);
|
||||
} else {
|
||||
memcpy(array, karray, asize*sizeof(TValue));
|
||||
}
|
||||
}
|
||||
hmask = kt->hmask;
|
||||
if (hmask > 0) {
|
||||
uint32_t i;
|
||||
Node *node = noderef(t->node);
|
||||
Node *knode = noderef(kt->node);
|
||||
ptrdiff_t d = (char *)node - (char *)knode;
|
||||
setfreetop(t, node, (Node *)((char *)getfreetop(kt, knode) + d));
|
||||
for (i = 0; i <= hmask; i++) {
|
||||
Node *kn = &knode[i];
|
||||
Node *n = &node[i];
|
||||
Node *next = nextnode(kn);
|
||||
/* Don't use copyTV here, since it asserts on a copy of a dead key. */
|
||||
n->val = kn->val; n->key = kn->key;
|
||||
setmref(n->next, next == NULL? next : (Node *)((char *)next + d));
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
/* Clear a table. */
|
||||
void LJ_FASTCALL lj_tab_clear(GCtab *t)
|
||||
{
|
||||
clearapart(t);
|
||||
if (t->hmask > 0) {
|
||||
Node *node = noderef(t->node);
|
||||
setfreetop(t, node, &node[t->hmask+1]);
|
||||
clearhpart(t);
|
||||
}
|
||||
}
|
||||
|
||||
/* Free a table. */
|
||||
void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t)
|
||||
{
|
||||
if (t->hmask > 0)
|
||||
lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node);
|
||||
if (t->asize > 0 && LJ_MAX_COLOSIZE != 0 && t->colo <= 0)
|
||||
lj_mem_freevec(g, tvref(t->array), t->asize, TValue);
|
||||
if (LJ_MAX_COLOSIZE != 0 && t->colo)
|
||||
lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f));
|
||||
else
|
||||
lj_mem_freet(g, t);
|
||||
}
|
||||
|
||||
/* -- Table resizing ------------------------------------------------------ */
|
||||
|
||||
/* Resize a table to fit the new array/hash part sizes. */
|
||||
void lj_tab_resize(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits)
|
||||
{
|
||||
Node *oldnode = noderef(t->node);
|
||||
uint32_t oldasize = t->asize;
|
||||
uint32_t oldhmask = t->hmask;
|
||||
if (asize > oldasize) { /* Array part grows? */
|
||||
TValue *array;
|
||||
uint32_t i;
|
||||
if (asize > LJ_MAX_ASIZE)
|
||||
lj_err_msg(L, LJ_ERR_TABOV);
|
||||
if (LJ_MAX_COLOSIZE != 0 && t->colo > 0) {
|
||||
/* A colocated array must be separated and copied. */
|
||||
TValue *oarray = tvref(t->array);
|
||||
array = lj_mem_newvec(L, asize, TValue);
|
||||
t->colo = (int8_t)(t->colo | 0x80); /* Mark as separated (colo < 0). */
|
||||
for (i = 0; i < oldasize; i++)
|
||||
copyTV(L, &array[i], &oarray[i]);
|
||||
} else {
|
||||
array = (TValue *)lj_mem_realloc(L, tvref(t->array),
|
||||
oldasize*sizeof(TValue), asize*sizeof(TValue));
|
||||
}
|
||||
setmref(t->array, array);
|
||||
t->asize = asize;
|
||||
for (i = oldasize; i < asize; i++) /* Clear newly allocated slots. */
|
||||
setnilV(&array[i]);
|
||||
}
|
||||
/* Create new (empty) hash part. */
|
||||
if (hbits) {
|
||||
newhpart(L, t, hbits);
|
||||
clearhpart(t);
|
||||
} else {
|
||||
global_State *g = G(L);
|
||||
setmref(t->node, &g->nilnode);
|
||||
#if LJ_GC64
|
||||
setmref(t->freetop, &g->nilnode);
|
||||
#endif
|
||||
t->hmask = 0;
|
||||
}
|
||||
if (asize < oldasize) { /* Array part shrinks? */
|
||||
TValue *array = tvref(t->array);
|
||||
uint32_t i;
|
||||
t->asize = asize; /* Note: This 'shrinks' even colocated arrays. */
|
||||
for (i = asize; i < oldasize; i++) /* Reinsert old array values. */
|
||||
if (!tvisnil(&array[i]))
|
||||
copyTV(L, lj_tab_setinth(L, t, (int32_t)i), &array[i]);
|
||||
/* Physically shrink only separated arrays. */
|
||||
if (LJ_MAX_COLOSIZE != 0 && t->colo <= 0)
|
||||
setmref(t->array, lj_mem_realloc(L, array,
|
||||
oldasize*sizeof(TValue), asize*sizeof(TValue)));
|
||||
}
|
||||
if (oldhmask > 0) { /* Reinsert pairs from old hash part. */
|
||||
global_State *g;
|
||||
uint32_t i;
|
||||
for (i = 0; i <= oldhmask; i++) {
|
||||
Node *n = &oldnode[i];
|
||||
if (!tvisnil(&n->val))
|
||||
copyTV(L, lj_tab_set(L, t, &n->key), &n->val);
|
||||
}
|
||||
g = G(L);
|
||||
lj_mem_freevec(g, oldnode, oldhmask+1, Node);
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t countint(cTValue *key, uint32_t *bins)
|
||||
{
|
||||
lj_assertX(!tvisint(key), "bad integer key");
|
||||
if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) {
|
||||
bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t countarray(const GCtab *t, uint32_t *bins)
|
||||
{
|
||||
uint32_t na, b, i;
|
||||
if (t->asize == 0) return 0;
|
||||
for (na = i = b = 0; b < LJ_MAX_ABITS; b++) {
|
||||
uint32_t n, top = 2u << b;
|
||||
TValue *array;
|
||||
if (top >= t->asize) {
|
||||
top = t->asize-1;
|
||||
if (i > top)
|
||||
break;
|
||||
}
|
||||
array = tvref(t->array);
|
||||
for (n = 0; i <= top; i++)
|
||||
if (!tvisnil(&array[i]))
|
||||
n++;
|
||||
bins[b] += n;
|
||||
na += n;
|
||||
}
|
||||
return na;
|
||||
}
|
||||
|
||||
static uint32_t counthash(const GCtab *t, uint32_t *bins, uint32_t *narray)
|
||||
{
|
||||
uint32_t total, na, i, hmask = t->hmask;
|
||||
Node *node = noderef(t->node);
|
||||
for (total = na = 0, i = 0; i <= hmask; i++) {
|
||||
Node *n = &node[i];
|
||||
if (!tvisnil(&n->val)) {
|
||||
na += countint(&n->key, bins);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
*narray += na;
|
||||
return total;
|
||||
}
|
||||
|
||||
static uint32_t bestasize(uint32_t bins[], uint32_t *narray)
|
||||
{
|
||||
uint32_t b, sum, na = 0, sz = 0, nn = *narray;
|
||||
for (b = 0, sum = 0; 2*nn > (1u<<b) && sum != nn; b++)
|
||||
if (bins[b] > 0 && 2*(sum += bins[b]) > (1u<<b)) {
|
||||
sz = (2u<<b)+1;
|
||||
na = sum;
|
||||
}
|
||||
*narray = sz;
|
||||
return na;
|
||||
}
|
||||
|
||||
static void rehashtab(lua_State *L, GCtab *t, cTValue *ek)
|
||||
{
|
||||
uint32_t bins[LJ_MAX_ABITS];
|
||||
uint32_t total, asize, na, i;
|
||||
for (i = 0; i < LJ_MAX_ABITS; i++) bins[i] = 0;
|
||||
asize = countarray(t, bins);
|
||||
#if 0
|
||||
total = 1 + asize;
|
||||
total += counthash(t, bins, &asize);
|
||||
#else
|
||||
/* more closely matches vanilla lua when abusing undefined behavior (frictionfix.....) */
|
||||
total = counthash(t, bins, &asize);
|
||||
total += 1 + asize;
|
||||
#endif
|
||||
asize += countint(ek, bins);
|
||||
na = bestasize(bins, &asize);
|
||||
total -= na;
|
||||
lj_tab_resize(L, t, asize, hsize2hbits(total));
|
||||
}
|
||||
|
||||
void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize)
|
||||
{
|
||||
lj_tab_resize(L, t, nasize+1, t->hmask > 0 ? lj_fls(t->hmask)+1 : 0);
|
||||
}
|
||||
|
||||
/* -- Table getters ------------------------------------------------------- */
|
||||
|
||||
cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key)
|
||||
{
|
||||
TValue k;
|
||||
Node *n;
|
||||
k.n = (lua_Number)key;
|
||||
n = hashnum(t, &k);
|
||||
do {
|
||||
if (tvisnum(&n->key) && n->key.n == k.n)
|
||||
return &n->val;
|
||||
} while ((n = nextnode(n)));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cTValue *lj_tab_getstr(GCtab *t, const GCstr *key)
|
||||
{
|
||||
Node *n = hashstr(t, key);
|
||||
do {
|
||||
if (tvisstr(&n->key) && strV(&n->key) == key)
|
||||
return &n->val;
|
||||
} while ((n = nextnode(n)));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key)
|
||||
{
|
||||
if (tvisstr(key)) {
|
||||
cTValue *tv = lj_tab_getstr(t, strV(key));
|
||||
if (tv)
|
||||
return tv;
|
||||
} else if (tvisint(key)) {
|
||||
cTValue *tv = lj_tab_getint(t, intV(key));
|
||||
if (tv)
|
||||
return tv;
|
||||
} else if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if (nk == (lua_Number)k) {
|
||||
cTValue *tv = lj_tab_getint(t, k);
|
||||
if (tv)
|
||||
return tv;
|
||||
} else {
|
||||
goto genlookup; /* Else use the generic lookup. */
|
||||
}
|
||||
} else if (!tvisnil(key)) {
|
||||
Node *n;
|
||||
genlookup:
|
||||
n = hashkey(t, key);
|
||||
do {
|
||||
if (lj_obj_equal(&n->key, key))
|
||||
return &n->val;
|
||||
} while ((n = nextnode(n)));
|
||||
}
|
||||
return niltv(L);
|
||||
}
|
||||
|
||||
/* -- Table setters ------------------------------------------------------- */
|
||||
|
||||
/* Insert new key. Use Brent's variation to optimize the chain length. */
|
||||
TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key)
|
||||
{
|
||||
Node *n = hashkey(t, key);
|
||||
if (!tvisnil(&n->val) || t->hmask == 0) {
|
||||
Node *nodebase = noderef(t->node);
|
||||
Node *collide, *freenode = getfreetop(t, nodebase);
|
||||
lj_assertL(freenode >= nodebase && freenode <= nodebase+t->hmask+1,
|
||||
"bad freenode");
|
||||
do {
|
||||
if (freenode == nodebase) { /* No free node found? */
|
||||
rehashtab(L, t, key); /* Rehash table. */
|
||||
return lj_tab_set(L, t, key); /* Retry key insertion. */
|
||||
}
|
||||
} while (!tvisnil(&(--freenode)->key));
|
||||
setfreetop(t, nodebase, freenode);
|
||||
lj_assertL(freenode != &G(L)->nilnode, "store to fallback hash");
|
||||
collide = hashkey(t, &n->key);
|
||||
if (collide != n) { /* Colliding node not the main node? */
|
||||
while (noderef(collide->next) != n) /* Find predecessor. */
|
||||
collide = nextnode(collide);
|
||||
setmref(collide->next, freenode); /* Relink chain. */
|
||||
/* Copy colliding node into free node and free main node. */
|
||||
freenode->val = n->val;
|
||||
freenode->key = n->key;
|
||||
freenode->next = n->next;
|
||||
setmref(n->next, NULL);
|
||||
setnilV(&n->val);
|
||||
/* Rechain pseudo-resurrected string keys with colliding hashes. */
|
||||
while (nextnode(freenode)) {
|
||||
Node *nn = nextnode(freenode);
|
||||
if (!tvisnil(&nn->val) && hashkey(t, &nn->key) == n) {
|
||||
freenode->next = nn->next;
|
||||
nn->next = n->next;
|
||||
setmref(n->next, nn);
|
||||
/*
|
||||
** Rechaining a resurrected string key creates a new dilemma:
|
||||
** Another string key may have originally been resurrected via
|
||||
** _any_ of the previous nodes as a chain anchor. Including
|
||||
** a node that had to be moved, which makes them unreachable.
|
||||
** It's not feasible to check for all previous nodes, so rechain
|
||||
** any string key that's currently in a non-main positions.
|
||||
*/
|
||||
while ((nn = nextnode(freenode))) {
|
||||
if (!tvisnil(&nn->val)) {
|
||||
Node *mn = hashkey(t, &nn->key);
|
||||
if (mn != freenode && mn != nn) {
|
||||
freenode->next = nn->next;
|
||||
nn->next = mn->next;
|
||||
setmref(mn->next, nn);
|
||||
} else {
|
||||
freenode = nn;
|
||||
}
|
||||
} else {
|
||||
freenode = nn;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
freenode = nn;
|
||||
}
|
||||
}
|
||||
} else { /* Otherwise use free node. */
|
||||
setmrefr(freenode->next, n->next); /* Insert into chain. */
|
||||
setmref(n->next, freenode);
|
||||
n = freenode;
|
||||
}
|
||||
}
|
||||
n->key.u64 = key->u64;
|
||||
if (LJ_UNLIKELY(tvismzero(&n->key)))
|
||||
n->key.u64 = 0;
|
||||
lj_gc_anybarriert(L, t);
|
||||
lj_assertL(tvisnil(&n->val), "new hash slot is not empty");
|
||||
return &n->val;
|
||||
}
|
||||
|
||||
TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key)
|
||||
{
|
||||
TValue k;
|
||||
Node *n;
|
||||
k.n = (lua_Number)key;
|
||||
n = hashnum(t, &k);
|
||||
do {
|
||||
if (tvisnum(&n->key) && n->key.n == k.n)
|
||||
return &n->val;
|
||||
} while ((n = nextnode(n)));
|
||||
return lj_tab_newkey(L, t, &k);
|
||||
}
|
||||
|
||||
TValue *lj_tab_setstr(lua_State *L, GCtab *t, const GCstr *key)
|
||||
{
|
||||
TValue k;
|
||||
Node *n = hashstr(t, key);
|
||||
do {
|
||||
if (tvisstr(&n->key) && strV(&n->key) == key)
|
||||
return &n->val;
|
||||
} while ((n = nextnode(n)));
|
||||
setstrV(L, &k, key);
|
||||
return lj_tab_newkey(L, t, &k);
|
||||
}
|
||||
|
||||
TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)
|
||||
{
|
||||
Node *n;
|
||||
t->nomm = 0; /* Invalidate negative metamethod cache. */
|
||||
if (tvisstr(key)) {
|
||||
return lj_tab_setstr(L, t, strV(key));
|
||||
} else if (tvisint(key)) {
|
||||
return lj_tab_setint(L, t, intV(key));
|
||||
} else if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if (nk == (lua_Number)k)
|
||||
return lj_tab_setint(L, t, k);
|
||||
if (tvisnan(key))
|
||||
lj_err_msg(L, LJ_ERR_NANIDX);
|
||||
/* Else use the generic lookup. */
|
||||
} else if (tvisnil(key)) {
|
||||
lj_err_msg(L, LJ_ERR_NILIDX);
|
||||
}
|
||||
n = hashkey(t, key);
|
||||
do {
|
||||
if (lj_obj_equal(&n->key, key))
|
||||
return &n->val;
|
||||
} while ((n = nextnode(n)));
|
||||
return lj_tab_newkey(L, t, key);
|
||||
}
|
||||
|
||||
/* -- Table traversal ----------------------------------------------------- */
|
||||
|
||||
/* Table traversal indexes:
|
||||
**
|
||||
** Array key index: [0 .. t->asize-1]
|
||||
** Hash key index: [t->asize .. t->asize+t->hmask]
|
||||
** Invalid key: ~0
|
||||
*/
|
||||
|
||||
/* Get the successor traversal index of a key. */
|
||||
uint32_t LJ_FASTCALL lj_tab_keyindex(GCtab *t, cTValue *key)
|
||||
{
|
||||
TValue tmp;
|
||||
if (tvisint(key)) {
|
||||
int32_t k = intV(key);
|
||||
if ((uint32_t)k < t->asize)
|
||||
return (uint32_t)k + 1;
|
||||
setnumV(&tmp, (lua_Number)k);
|
||||
key = &tmp;
|
||||
} else if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if ((uint32_t)k < t->asize && nk == (lua_Number)k)
|
||||
return (uint32_t)k + 1;
|
||||
}
|
||||
if (!tvisnil(key)) {
|
||||
Node *n = hashkey(t, key);
|
||||
do {
|
||||
if (lj_obj_equal(&n->key, key))
|
||||
return t->asize + (uint32_t)((n+1) - noderef(t->node));
|
||||
} while ((n = nextnode(n)));
|
||||
if (key->u32.hi == LJ_KEYINDEX) /* Despecialized ITERN while running. */
|
||||
return key->u32.lo;
|
||||
return ~0u; /* Invalid key to next. */
|
||||
}
|
||||
return 0; /* A nil key starts the traversal. */
|
||||
}
|
||||
|
||||
/* Get the next key/value pair of a table traversal. */
|
||||
int lj_tab_next(GCtab *t, cTValue *key, TValue *o)
|
||||
{
|
||||
uint32_t idx = lj_tab_keyindex(t, key); /* Find successor index of key. */
|
||||
/* First traverse the array part. */
|
||||
for (; idx < t->asize; idx++) {
|
||||
cTValue *a = arrayslot(t, idx);
|
||||
if (LJ_LIKELY(!tvisnil(a))) {
|
||||
setintV(o, idx);
|
||||
o[1] = *a;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
idx -= t->asize;
|
||||
/* Then traverse the hash part. */
|
||||
for (; idx <= t->hmask; idx++) {
|
||||
Node *n = &noderef(t->node)[idx];
|
||||
if (!tvisnil(&n->val)) {
|
||||
o[0] = n->key;
|
||||
o[1] = n->val;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return (int32_t)idx < 0 ? -1 : 0; /* Invalid key or end of traversal. */
|
||||
}
|
||||
|
||||
/* -- Table length calculation -------------------------------------------- */
|
||||
|
||||
/* Compute table length. Slow path with mixed array/hash lookups. */
|
||||
LJ_NOINLINE static MSize tab_len_slow(GCtab *t, size_t hi)
|
||||
{
|
||||
cTValue *tv;
|
||||
size_t lo = hi;
|
||||
hi++;
|
||||
/* Widening search for an upper bound. */
|
||||
while ((tv = lj_tab_getint(t, (int32_t)hi)) && !tvisnil(tv)) {
|
||||
lo = hi;
|
||||
hi += hi;
|
||||
if (hi > (size_t)(0x7fffffff - 2)) { /* Punt and do a linear search. */
|
||||
lo = 1;
|
||||
while ((tv = lj_tab_getint(t, (int32_t)lo)) && !tvisnil(tv)) lo++;
|
||||
return (MSize)(lo - 1);
|
||||
}
|
||||
}
|
||||
/* Binary search to find a non-nil to nil transition. */
|
||||
while (hi - lo > 1) {
|
||||
size_t mid = (lo+hi) >> 1;
|
||||
cTValue *tvb = lj_tab_getint(t, (int32_t)mid);
|
||||
if (tvb && !tvisnil(tvb)) lo = mid; else hi = mid;
|
||||
}
|
||||
return (MSize)lo;
|
||||
}
|
||||
|
||||
/* Compute table length. Fast path. */
|
||||
MSize LJ_FASTCALL lj_tab_len(GCtab *t)
|
||||
{
|
||||
size_t hi = (size_t)t->asize;
|
||||
if (hi) hi--;
|
||||
/* In a growing array the last array element is very likely nil. */
|
||||
if (hi > 0 && LJ_LIKELY(tvisnil(arrayslot(t, hi)))) {
|
||||
/* Binary search to find a non-nil to nil transition in the array. */
|
||||
size_t lo = 0;
|
||||
while (hi - lo > 1) {
|
||||
size_t mid = (lo+hi) >> 1;
|
||||
if (tvisnil(arrayslot(t, mid))) hi = mid; else lo = mid;
|
||||
}
|
||||
return (MSize)lo;
|
||||
}
|
||||
/* Without a hash part, there's an implicit nil after the last element. */
|
||||
return t->hmask ? tab_len_slow(t, hi) : (MSize)hi;
|
||||
}
|
||||
|
||||
#if LJ_HASJIT
|
||||
/* Verify hinted table length or compute it. */
|
||||
MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint)
|
||||
{
|
||||
size_t asize = (size_t)t->asize;
|
||||
cTValue *tv = arrayslot(t, hint);
|
||||
if (LJ_LIKELY(hint+1 < asize)) {
|
||||
if (LJ_LIKELY(!tvisnil(tv) && tvisnil(tv+1))) return (MSize)hint;
|
||||
} else if (hint+1 <= asize && LJ_LIKELY(t->hmask == 0) && !tvisnil(tv)) {
|
||||
return (MSize)hint;
|
||||
}
|
||||
return lj_tab_len(t);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
** Math helper functions for assembler VM.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
*/
|
||||
|
||||
#define lj_vmmath_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lj_obj.h"
|
||||
#include "lj_ir.h"
|
||||
#include "lj_vm.h"
|
||||
|
||||
/* -- Wrapper functions --------------------------------------------------- */
|
||||
|
||||
#if LJ_TARGET_X86 && __ELF__ && __PIC__
|
||||
/* Wrapper functions to deal with the ELF/x86 PIC disaster. */
|
||||
LJ_FUNCA double lj_wrap_log(double x) { return log(x); }
|
||||
LJ_FUNCA double lj_wrap_log10(double x) { return log10(x); }
|
||||
LJ_FUNCA double lj_wrap_exp(double x) { return exp(x); }
|
||||
LJ_FUNCA double lj_wrap_sin(double x) { return sin(x); }
|
||||
LJ_FUNCA double lj_wrap_cos(double x) { return cos(x); }
|
||||
LJ_FUNCA double lj_wrap_tan(double x) { return tan(x); }
|
||||
LJ_FUNCA double lj_wrap_asin(double x) { return asin(x); }
|
||||
LJ_FUNCA double lj_wrap_acos(double x) { return acos(x); }
|
||||
LJ_FUNCA double lj_wrap_atan(double x) { return atan(x); }
|
||||
LJ_FUNCA double lj_wrap_sinh(double x) { return sinh(x); }
|
||||
LJ_FUNCA double lj_wrap_cosh(double x) { return cosh(x); }
|
||||
LJ_FUNCA double lj_wrap_tanh(double x) { return tanh(x); }
|
||||
LJ_FUNCA double lj_wrap_atan2(double x, double y) { return atan2(x, y); }
|
||||
LJ_FUNCA double lj_wrap_pow(double x, double y) { return pow(x, y); }
|
||||
LJ_FUNCA double lj_wrap_fmod(double x, double y) { return fmod(x, y); }
|
||||
#endif
|
||||
|
||||
/* -- Helper functions ---------------------------------------------------- */
|
||||
|
||||
/* Required to prevent the C compiler from applying FMA optimizations.
|
||||
**
|
||||
** Yes, there's -ffp-contract and the FP_CONTRACT pragma ... in theory.
|
||||
** But the current state of C compilers is a mess in this regard.
|
||||
** Also, this function is not performance sensitive at all.
|
||||
*/
|
||||
LJ_NOINLINE static double lj_vm_floormul(double x, double y)
|
||||
{
|
||||
return lj_vm_floor(x / y) * y;
|
||||
}
|
||||
|
||||
double lj_vm_foldarith(double x, double y, int op)
|
||||
{
|
||||
switch (op) {
|
||||
case IR_ADD - IR_ADD: return x+y; break;
|
||||
case IR_SUB - IR_ADD: return x-y; break;
|
||||
case IR_MUL - IR_ADD: return x*y; break;
|
||||
case IR_DIV - IR_ADD: return x/y; break;
|
||||
case IR_MOD - IR_ADD: return x-lj_vm_floormul(x, y); break;
|
||||
case IR_POW - IR_ADD: return pow(x, y); break;
|
||||
case IR_NEG - IR_ADD: return -x; break;
|
||||
case IR_ABS - IR_ADD: return fabs(x); break;
|
||||
#if LJ_HASJIT
|
||||
case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;
|
||||
case IR_MIN - IR_ADD: return x < y ? x : y; break;
|
||||
case IR_MAX - IR_ADD: return x > y ? x : y; break;
|
||||
#endif
|
||||
default: return x;
|
||||
}
|
||||
}
|
||||
|
||||
#if LJ_INTONLY
|
||||
int32_t lj_vm_foldarith_int(int32_t x, int32_t y, int op)
|
||||
{
|
||||
switch (op) {
|
||||
case IR_ADD - IR_ADD: return x+y; break;
|
||||
case IR_SUB - IR_ADD: return x-y; break;
|
||||
case IR_MUL - IR_ADD: return x*y; break;
|
||||
case IR_DIV - IR_ADD: return x/y; break;
|
||||
case IR_MOD - IR_ADD: return x%y; break;
|
||||
case IR_POW - IR_ADD: return pow(x, y); break;
|
||||
case IR_NEG - IR_ADD: return -x; break;
|
||||
case IR_ABS - IR_ADD: return abs(x); break;
|
||||
#if LJ_HASJIT
|
||||
case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;
|
||||
case IR_MIN - IR_ADD: return x < y ? x : y; break;
|
||||
case IR_MAX - IR_ADD: return x > y ? x : y; break;
|
||||
#endif
|
||||
default: return x;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t lj_vm_foldbitwise(uint32_t x, uint32_t y, int op)
|
||||
#else
|
||||
int64_t lj_vm_foldbitwise(int64_t x, int64_t y, int op)
|
||||
#endif
|
||||
{
|
||||
switch (op) {
|
||||
case IR_BAND - IR_BAND: return x&y; break;
|
||||
case IR_BOR - IR_BAND: return x|y; break;
|
||||
case IR_BXOR - IR_BAND: return x^y; break;
|
||||
case IR_BSHL - IR_BAND: return x<<y; break;
|
||||
case IR_BSHR - IR_BAND: return x>>y; break;
|
||||
default: return ~x; /* IR_BNOT */
|
||||
}
|
||||
}
|
||||
|
||||
/* -- Helper functions for generated machine code ------------------------- */
|
||||
|
||||
#if (LJ_HASJIT && !(LJ_TARGET_ARM || LJ_TARGET_ARM64 || LJ_TARGET_PPC)) || LJ_TARGET_MIPS
|
||||
int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
|
||||
{
|
||||
/* This must be checked before using this function. */
|
||||
lj_assertX(b != 0, "modulo with zero divisor");
|
||||
#if LJ_INTONLY
|
||||
return a % b;
|
||||
#else
|
||||
uint32_t y, ua, ub;
|
||||
ua = a < 0 ? ~(uint32_t)a+1u : (uint32_t)a;
|
||||
ub = b < 0 ? ~(uint32_t)b+1u : (uint32_t)b;
|
||||
y = ua % ub;
|
||||
if (y != 0 && (a^b) < 0) y = y - ub;
|
||||
if (((int32_t)y^b) < 0) y = ~y+1u;
|
||||
return (int32_t)y;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if LJ_HASJIT
|
||||
|
||||
#ifdef LUAJIT_NO_LOG2
|
||||
double lj_vm_log2(double a)
|
||||
{
|
||||
return log(a) * 1.4426950408889634074;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Computes fpm(x) for extended math functions. */
|
||||
double lj_vm_foldfpm(double x, int fpm)
|
||||
{
|
||||
switch (fpm) {
|
||||
case IRFPM_FLOOR: return lj_vm_floor(x);
|
||||
case IRFPM_CEIL: return lj_vm_ceil(x);
|
||||
case IRFPM_TRUNC: return lj_vm_trunc(x);
|
||||
case IRFPM_SQRT: return sqrt(x);
|
||||
case IRFPM_LOG: return log(x);
|
||||
case IRFPM_LOG2: return lj_vm_log2(x);
|
||||
default: lj_assertX(0, "bad fpm %d", fpm);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if LJ_HASFFI
|
||||
int lj_vm_errno(void)
|
||||
{
|
||||
return errno;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
** Configuration header.
|
||||
** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
|
||||
*/
|
||||
|
||||
#ifndef luaconf_h
|
||||
#define luaconf_h
|
||||
|
||||
#ifndef WINVER
|
||||
#define WINVER 0x0501
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
|
||||
/* Default path for loading Lua and C modules with require(). */
|
||||
#if defined(_WIN32)
|
||||
/*
|
||||
** In Windows, any exclamation mark ('!') in the path is replaced by the
|
||||
** path of the directory of the executable file of the current process.
|
||||
*/
|
||||
#define LUA_LDIR "!\\lua\\"
|
||||
#define LUA_CDIR "!\\"
|
||||
#define LUA_PATH_DEFAULT \
|
||||
".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;"
|
||||
#define LUA_CPATH_DEFAULT \
|
||||
".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
|
||||
#else
|
||||
/*
|
||||
** Note to distribution maintainers: do NOT patch the following lines!
|
||||
** Please read ../doc/install.html#distro and pass PREFIX=/usr instead.
|
||||
*/
|
||||
#ifndef LUA_MULTILIB
|
||||
#define LUA_MULTILIB "lib"
|
||||
#endif
|
||||
#ifndef LUA_LMULTILIB
|
||||
#define LUA_LMULTILIB "lib"
|
||||
#endif
|
||||
#define LUA_LROOT "/usr/local"
|
||||
#define LUA_LUADIR "/lua/5.1/"
|
||||
|
||||
#ifdef LUA_ROOT
|
||||
#define LUA_JROOT LUA_ROOT
|
||||
#define LUA_RLDIR LUA_ROOT "/share" LUA_LUADIR
|
||||
#define LUA_RCDIR LUA_ROOT "/" LUA_MULTILIB LUA_LUADIR
|
||||
#define LUA_RLPATH ";" LUA_RLDIR "?.lua;" LUA_RLDIR "?/init.lua"
|
||||
#define LUA_RCPATH ";" LUA_RCDIR "?.so"
|
||||
#else
|
||||
#define LUA_JROOT LUA_LROOT
|
||||
#define LUA_RLPATH
|
||||
#define LUA_RCPATH
|
||||
#endif
|
||||
|
||||
#ifndef LUA_LJDIR
|
||||
#define LUA_LJDIR LUA_JROOT "/share/luajit-2.1"
|
||||
#endif
|
||||
|
||||
#define LUA_JPATH ";" LUA_LJDIR "/?.lua"
|
||||
#define LUA_LLDIR LUA_LROOT "/share" LUA_LUADIR
|
||||
#define LUA_LCDIR LUA_LROOT "/" LUA_LMULTILIB LUA_LUADIR
|
||||
#define LUA_LLPATH ";" LUA_LLDIR "?.lua;" LUA_LLDIR "?/init.lua"
|
||||
#define LUA_LCPATH1 ";" LUA_LCDIR "?.so"
|
||||
#define LUA_LCPATH2 ";" LUA_LCDIR "loadall.so"
|
||||
|
||||
#define LUA_PATH_DEFAULT "./?.lua" LUA_JPATH LUA_LLPATH LUA_RLPATH
|
||||
#define LUA_CPATH_DEFAULT "./?.so" LUA_LCPATH1 LUA_RCPATH LUA_LCPATH2
|
||||
#endif
|
||||
|
||||
/* Environment variable names for path overrides and initialization code. */
|
||||
#define LUA_PATH "LUA_PATH"
|
||||
#define LUA_CPATH "LUA_CPATH"
|
||||
#define LUA_INIT "LUA_INIT"
|
||||
|
||||
/* Special file system characters. */
|
||||
#if defined(_WIN32)
|
||||
#define LUA_DIRSEP "\\"
|
||||
#else
|
||||
#define LUA_DIRSEP "/"
|
||||
#endif
|
||||
#define LUA_PATHSEP ";"
|
||||
#define LUA_PATH_MARK "?"
|
||||
#define LUA_EXECDIR "!"
|
||||
#define LUA_IGMARK "-"
|
||||
#define LUA_PATH_CONFIG \
|
||||
LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \
|
||||
LUA_EXECDIR "\n" LUA_IGMARK "\n"
|
||||
|
||||
/* Quoting in error messages. */
|
||||
#define LUA_QL(x) "'" x "'"
|
||||
#define LUA_QS LUA_QL("%s")
|
||||
|
||||
/* Various tunables. */
|
||||
#define LUAI_MAXSTACK 65500 /* Max. # of stack slots for a thread (<64K). */
|
||||
#define LUAI_MAXCSTACK 8000 /* Max. # of stack slots for a C func (<10K). */
|
||||
#define LUAI_GCPAUSE 200 /* Pause GC until memory is at 200%. */
|
||||
#define LUAI_GCMUL 200 /* Run GC at 200% of allocation speed. */
|
||||
#define LUA_MAXCAPTURES 32 /* Max. pattern captures. */
|
||||
|
||||
/* Configuration for the frontend (the luajit executable). */
|
||||
#if defined(luajit_c)
|
||||
#define LUA_PROGNAME "luajit" /* Fallback frontend name. */
|
||||
#define LUA_PROMPT "> " /* Interactive prompt. */
|
||||
#define LUA_PROMPT2 ">> " /* Continuation prompt. */
|
||||
#define LUA_MAXINPUT 512 /* Max. input line length. */
|
||||
#endif
|
||||
|
||||
/* Note: changing the following defines breaks the Lua 5.1 ABI. */
|
||||
#ifdef LUAJIT_INTONLY
|
||||
#include <stdint.h>
|
||||
#define LUA_INTEGER int32_t
|
||||
#else
|
||||
#define LUA_INTEGER ptrdiff_t
|
||||
#endif
|
||||
#define LUA_IDSIZE 60 /* Size of lua_Debug.short_src. */
|
||||
/*
|
||||
** Size of lauxlib and io.* on-stack buffers. Weird workaround to avoid using
|
||||
** unreasonable amounts of stack space, but still retain ABI compatibility.
|
||||
** Blame Lua for depending on BUFSIZ in the ABI, blame **** for wrecking it.
|
||||
*/
|
||||
#define LUAL_BUFFERSIZE (BUFSIZ > 16384 ? 8192 : BUFSIZ)
|
||||
|
||||
/* The following defines are here only for compatibility with luaconf.h
|
||||
** from the standard Lua distribution. They must not be changed for LuaJIT.
|
||||
*/
|
||||
#define LUA_NUMBER_DOUBLE
|
||||
#define LUA_NUMBER double
|
||||
#define LUAI_UACNUMBER double
|
||||
#define LUA_NUMBER_SCAN "%lf"
|
||||
#define LUA_NUMBER_FMT "%.14g"
|
||||
#define lua_number2str(s, n) sprintf((s), LUA_NUMBER_FMT, (n))
|
||||
#define LUAI_MAXNUMBER2STR 32
|
||||
#define LUA_INTFRMLEN "l"
|
||||
#define LUA_INTFRM_T long
|
||||
|
||||
/* Linkage of public API functions. */
|
||||
#if defined(LUA_BUILD_AS_DLL)
|
||||
#if defined(LUA_CORE) || defined(LUA_LIB)
|
||||
#define LUA_API __declspec(dllexport)
|
||||
#else
|
||||
#define LUA_API __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
#define LUALIB_API LUA_API
|
||||
|
||||
/* Compatibility support for assertions. */
|
||||
#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK)
|
||||
#include <assert.h>
|
||||
#endif
|
||||
#ifdef LUA_USE_ASSERT
|
||||
#define lua_assert(x) assert(x)
|
||||
#endif
|
||||
#ifdef LUA_USE_APICHECK
|
||||
#define luai_apicheck(L, o) { (void)L; assert(o); }
|
||||
#else
|
||||
#define luai_apicheck(L, o) { (void)L; }
|
||||
#endif
|
||||
|
||||
/******************************************************************************
|
||||
* bluajit configuration *
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* [bluajit] Allow != as well as the traditional Lua ~= for inequality comparison.
|
||||
*/
|
||||
#ifndef BLUAJIT_BANG_EQUALS_TOKEN
|
||||
#define BLUAJIT_BANG_EQUALS_TOKEN 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* [bluajit] Make `then` after `if <expr>` optional.
|
||||
*/
|
||||
#ifndef BLUAJIT_OPTIONAL_THEN_TOKEN
|
||||
#define BLUAJIT_OPTIONAL_THEN_TOKEN 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* [bluajit] Make `do` after `for <expr>` and `while <expr>` optional.
|
||||
*/
|
||||
#ifndef BLUAJIT_OPTIONAL_DO_TOKEN
|
||||
#define BLUAJIT_OPTIONAL_DO_TOKEN 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* [bluajit] Make `do` and `end` create an anonymous function body, much like
|
||||
* `function() end`
|
||||
*/
|
||||
#ifndef BLUAJIT_DO_END_ANONYMOUS_FUNCTIONS
|
||||
#define BLUAJIT_DO_END_ANONYMOUS_FUNCTIONS 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* [bluajit] Enable multilevel loop breaks with `break n`.
|
||||
*/
|
||||
#ifndef BLUAJIT_MULTILEVEL_BREAK
|
||||
#define BLUAJIT_MULTILEVEL_BREAK 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* [bluajit] Add `continue` and `continue n` statements.
|
||||
*/
|
||||
#ifndef BLUAJIT_CONTINUE_STATEMENT
|
||||
#define BLUAJIT_CONTINUE_STATEMENT 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -42,6 +42,123 @@ boolean luaL_checkboolean(lua_State *L, int narg) {
|
||||
return lua_toboolean(L, narg);
|
||||
}
|
||||
|
||||
static int ref_open, ref_iowrite, ref_fwrite; // original versions of io functions
|
||||
|
||||
#define FILELIMIT 1024*1024 // Size limit for reading/writing files
|
||||
|
||||
static const char *whitelist[] = { // Allow scripters to write files of these types to SRB2's folder
|
||||
".txt",
|
||||
".sav2",
|
||||
".cfg",
|
||||
".png",
|
||||
".bmp"
|
||||
};
|
||||
|
||||
static int StartsWith(const char *a, const char *b) // this is wolfs being lazy yet again
|
||||
{
|
||||
if(strncmp(a, b, strlen(b)) == 0) return 1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Wrapper for opening files
|
||||
static int lib_open(lua_State *L)
|
||||
{
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
int pass = 0;
|
||||
size_t i;
|
||||
int length = strlen(filename);
|
||||
char *splitter, *forward, *backward;
|
||||
char *destFilename;
|
||||
|
||||
for (i = 0; i < (sizeof (whitelist) / sizeof(const char *)); i++)
|
||||
{
|
||||
if (!stricmp(&filename[length - strlen(whitelist[i])], whitelist[i]))
|
||||
{
|
||||
pass = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (strstr(filename, "..") || strchr(filename, ':') || StartsWith(filename, "\\")
|
||||
|| StartsWith(filename, "/") || !pass)
|
||||
{
|
||||
return luaL_error(L,"access denied to %s", filename);
|
||||
}
|
||||
|
||||
destFilename = va("%s"PATHSEP"luafiles"PATHSEP"%s", srb2home, filename);
|
||||
|
||||
// Make directories as needed
|
||||
splitter = destFilename;
|
||||
|
||||
forward = strchr(splitter, '/');
|
||||
backward = strchr(splitter, '\\');
|
||||
while ((splitter = (forward && backward) ? min(forward, backward) : (forward ?: backward)))
|
||||
{
|
||||
*splitter = 0;
|
||||
I_mkdir(destFilename, 0755);
|
||||
*splitter = '/';
|
||||
splitter++;
|
||||
|
||||
forward = strchr(splitter, '/');
|
||||
backward = strchr(splitter, '\\');
|
||||
}
|
||||
|
||||
// replace the filename string
|
||||
lua_pushstring(L, destFilename);
|
||||
lua_replace(L, 1);
|
||||
// now call the real io.open
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, ref_open);
|
||||
lua_insert(L, 1);
|
||||
lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
// Wrapper for writing to files
|
||||
static int lib_write(lua_State *L, int arg)
|
||||
{
|
||||
int top = lua_gettop(L);
|
||||
boolean error = true;
|
||||
size_t len, size = 0;
|
||||
int ogref = ref_iowrite;
|
||||
|
||||
if (arg != 1) {
|
||||
ogref = ref_fwrite;
|
||||
FILE *fp = *(FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
|
||||
if (fp == NULL) goto call; // real file:write will throw
|
||||
size = ftell(fp);
|
||||
}
|
||||
|
||||
// check the size of each argument. if there's too much data,
|
||||
// cut off the stack and print an error
|
||||
for (; arg <= top; arg++) {
|
||||
if (lua_tolstring(L, arg, &len) == NULL) goto call;
|
||||
if ((size += len) > FILELIMIT) {
|
||||
// oops! too many bytes!
|
||||
lua_settop(L, arg-1);
|
||||
goto call;
|
||||
}
|
||||
}
|
||||
error = false;
|
||||
|
||||
call:
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, ogref);
|
||||
lua_insert(L, 1);
|
||||
lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
|
||||
|
||||
if (error)
|
||||
return luaL_error(L,"write limit bypassed in file. Changes have been discarded.");
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
static int lib_write_f(lua_State *L)
|
||||
{
|
||||
return lib_write(L, 2);
|
||||
}
|
||||
|
||||
static int lib_write_io(lua_State *L)
|
||||
{
|
||||
return lib_write(L, 1);
|
||||
}
|
||||
|
||||
// String concatination
|
||||
static int lib_concat(lua_State *L)
|
||||
{
|
||||
@@ -3348,6 +3465,28 @@ int LUA_BaseLib(lua_State *L)
|
||||
lua_setfield(L,-2,"__add"); // ... store it as mathematical addition
|
||||
lua_pop(L, 2); // pop metatable and dummy string
|
||||
|
||||
// replaces an existing function with another,
|
||||
// saving the original as a reference
|
||||
#define REPLACE(name, func, ref) \
|
||||
lua_pushliteral(L, name); \
|
||||
lua_rawget(L, -2); \
|
||||
ref = luaL_ref(L, LUA_REGISTRYINDEX); \
|
||||
lua_pushliteral(L, name); \
|
||||
lua_pushcclosure(L, &func, 0); \
|
||||
lua_rawset(L, -3);
|
||||
|
||||
// replace io.open
|
||||
lua_getglobal(L, "io");
|
||||
REPLACE("open", lib_open, ref_open)
|
||||
REPLACE("write", lib_write_io, ref_iowrite)
|
||||
lua_pop(L, 1);
|
||||
|
||||
// replace file:write
|
||||
luaL_getmetatable(L, LUA_FILEHANDLE);
|
||||
REPLACE("write", lib_write_f, ref_fwrite)
|
||||
lua_pop(L, 1);
|
||||
#undef REPLACE
|
||||
|
||||
lua_newtable(L);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS);
|
||||
|
||||
|
||||
+17
-6
@@ -324,6 +324,8 @@ static int lib_cvRegisterVar(lua_State *L)
|
||||
|
||||
const char* category = NULL;
|
||||
const char* menu_name = NULL;
|
||||
boolean hasfunc = false;
|
||||
int funcref = 0;
|
||||
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 1); // Clear out all other possible arguments, leaving only the first one.
|
||||
@@ -409,12 +411,11 @@ static int lib_cvRegisterVar(lua_State *L)
|
||||
} else if (i == 5 || (k && fasticmp(k, "func"))) {
|
||||
if (!lua_isfunction(L, 4))
|
||||
TYPEERROR("func", LUA_TFUNCTION)
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "CV_OnChange");
|
||||
I_Assert(lua_istable(L, 5));
|
||||
lua_pushvalue(L, 4);
|
||||
lua_setfield(L, 5, cvar->name);
|
||||
lua_pop(L, 1);
|
||||
cvar->func = Lua_OnChange;
|
||||
if (hasfunc)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, funcref); // real clever, huh?
|
||||
funcref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
hasfunc = true;
|
||||
continue;
|
||||
}
|
||||
else if (((i == 5 && !(cvar->flags & CV_CALL))
|
||||
|| (cvar->flags & CV_CALL && i == 6))
|
||||
@@ -434,6 +435,16 @@ static int lib_cvRegisterVar(lua_State *L)
|
||||
#undef TYPEERROR
|
||||
|
||||
// stack: cvar table, cvar userdata
|
||||
if (hasfunc) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "CV_OnChange");
|
||||
I_Assert(lua_istable(L, 3));
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
|
||||
lua_setfield(L, 3, cvar->name);
|
||||
lua_pop(L, 1);
|
||||
cvar->func = Lua_OnChange;
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, funcref);
|
||||
}
|
||||
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "CV_Vars");
|
||||
I_Assert(lua_istable(L, 3));
|
||||
|
||||
|
||||
Reference in new issue
Block a user