diff --git a/luprex/.gitignore b/luprex/.gitignore
index 38610511..21435866 100644
--- a/luprex/.gitignore
+++ b/luprex/.gitignore
@@ -2,6 +2,9 @@
*.o
*.dll
*.exe
+inc/**
+lib/**
+.vscode/**
luajit/src/lj_bcdef.h
luajit/src/lj_ffdef.h
luajit/src/lj_folddef.h
diff --git a/luprex/build.bat b/luprex/build.bat
new file mode 100644
index 00000000..1f0c079b
--- /dev/null
+++ b/luprex/build.bat
@@ -0,0 +1 @@
+gcc -g -o main main.c -Iinc -Llib lib/libluajit.a
diff --git a/luprex/inspect.lua b/luprex/inspect.lua
new file mode 100644
index 00000000..e2e3806e
--- /dev/null
+++ b/luprex/inspect.lua
@@ -0,0 +1,334 @@
+local inspect ={
+ _VERSION = 'inspect.lua 3.1.0',
+ _URL = 'http://github.com/kikito/inspect.lua',
+ _DESCRIPTION = 'human-readable representations of tables',
+ _LICENSE = [[
+ MIT LICENSE
+
+ Copyright (c) 2013 Enrique GarcĂa Cota
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ ]]
+}
+
+local tostring = tostring
+
+inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
+inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})
+
+local function rawpairs(t)
+ return next, t, nil
+end
+
+-- Apostrophizes the string if it has quotes, but not aphostrophes
+-- Otherwise, it returns a regular quoted string
+local function smartQuote(str)
+ if str:match('"') and not str:match("'") then
+ return "'" .. str .. "'"
+ end
+ return '"' .. str:gsub('"', '\\"') .. '"'
+end
+
+-- \a => '\\a', \0 => '\\0', 31 => '\31'
+local shortControlCharEscapes = {
+ ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
+ ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v"
+}
+local longControlCharEscapes = {} -- \a => nil, \0 => \000, 31 => \031
+for i=0, 31 do
+ local ch = string.char(i)
+ if not shortControlCharEscapes[ch] then
+ shortControlCharEscapes[ch] = "\\"..i
+ longControlCharEscapes[ch] = string.format("\\%03d", i)
+ end
+end
+
+local function escape(str)
+ return (str:gsub("\\", "\\\\")
+ :gsub("(%c)%f[0-9]", longControlCharEscapes)
+ :gsub("%c", shortControlCharEscapes))
+end
+
+local function isIdentifier(str)
+ return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
+end
+
+local function isSequenceKey(k, sequenceLength)
+ return type(k) == 'number'
+ and 1 <= k
+ and k <= sequenceLength
+ and math.floor(k) == k
+end
+
+local defaultTypeOrders = {
+ ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
+ ['function'] = 5, ['userdata'] = 6, ['thread'] = 7
+}
+
+local function sortKeys(a, b)
+ local ta, tb = type(a), type(b)
+
+ -- strings and numbers are sorted numerically/alphabetically
+ if ta == tb and (ta == 'string' or ta == 'number') then return a < b end
+
+ local dta, dtb = defaultTypeOrders[ta], defaultTypeOrders[tb]
+ -- Two default types are compared according to the defaultTypeOrders table
+ if dta and dtb then return defaultTypeOrders[ta] < defaultTypeOrders[tb]
+ elseif dta then return true -- default types before custom ones
+ elseif dtb then return false -- custom types after default ones
+ end
+
+ -- custom types are sorted out alphabetically
+ return ta < tb
+end
+
+-- For implementation reasons, the behavior of rawlen & # is "undefined" when
+-- tables aren't pure sequences. So we implement our own # operator.
+local function getSequenceLength(t)
+ local len = 1
+ local v = rawget(t,len)
+ while v ~= nil do
+ len = len + 1
+ v = rawget(t,len)
+ end
+ return len - 1
+end
+
+local function getNonSequentialKeys(t)
+ local keys, keysLength = {}, 0
+ local sequenceLength = getSequenceLength(t)
+ for k,_ in rawpairs(t) do
+ if not isSequenceKey(k, sequenceLength) then
+ keysLength = keysLength + 1
+ keys[keysLength] = k
+ end
+ end
+ table.sort(keys, sortKeys)
+ return keys, keysLength, sequenceLength
+end
+
+local function countTableAppearances(t, tableAppearances)
+ tableAppearances = tableAppearances or {}
+
+ if type(t) == 'table' then
+ if not tableAppearances[t] then
+ tableAppearances[t] = 1
+ for k,v in rawpairs(t) do
+ countTableAppearances(k, tableAppearances)
+ countTableAppearances(v, tableAppearances)
+ end
+ countTableAppearances(getmetatable(t), tableAppearances)
+ else
+ tableAppearances[t] = tableAppearances[t] + 1
+ end
+ end
+
+ return tableAppearances
+end
+
+local copySequence = function(s)
+ local copy, len = {}, #s
+ for i=1, len do copy[i] = s[i] end
+ return copy, len
+end
+
+local function makePath(path, ...)
+ local keys = {...}
+ local newPath, len = copySequence(path)
+ for i=1, #keys do
+ newPath[len + i] = keys[i]
+ end
+ return newPath
+end
+
+local function processRecursive(process, item, path, visited)
+ if item == nil then return nil end
+ if visited[item] then return visited[item] end
+
+ local processed = process(item, path)
+ if type(processed) == 'table' then
+ local processedCopy = {}
+ visited[item] = processedCopy
+ local processedKey
+
+ for k,v in rawpairs(processed) do
+ processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
+ if processedKey ~= nil then
+ processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
+ end
+ end
+
+ local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited)
+ if type(mt) ~= 'table' then mt = nil end -- ignore not nil/table __metatable field
+ setmetatable(processedCopy, mt)
+ processed = processedCopy
+ end
+ return processed
+end
+
+
+
+-------------------------------------------------------------------
+
+local Inspector = {}
+local Inspector_mt = {__index = Inspector}
+
+function Inspector:puts(...)
+ local args = {...}
+ local buffer = self.buffer
+ local len = #buffer
+ for i=1, #args do
+ len = len + 1
+ buffer[len] = args[i]
+ end
+end
+
+function Inspector:down(f)
+ self.level = self.level + 1
+ f()
+ self.level = self.level - 1
+end
+
+function Inspector:tabify()
+ self:puts(self.newline, string.rep(self.indent, self.level))
+end
+
+function Inspector:alreadyVisited(v)
+ return self.ids[v] ~= nil
+end
+
+function Inspector:getId(v)
+ local id = self.ids[v]
+ if not id then
+ local tv = type(v)
+ id = (self.maxIds[tv] or 0) + 1
+ self.maxIds[tv] = id
+ self.ids[v] = id
+ end
+ return tostring(id)
+end
+
+function Inspector:putKey(k)
+ if isIdentifier(k) then return self:puts(k) end
+ self:puts("[")
+ self:putValue(k)
+ self:puts("]")
+end
+
+function Inspector:putTable(t)
+ if t == inspect.KEY or t == inspect.METATABLE then
+ self:puts(tostring(t))
+ elseif self:alreadyVisited(t) then
+ self:puts('
')
+ elseif self.level >= self.depth then
+ self:puts('{...}')
+ else
+ if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
+
+ local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t)
+ local mt = getmetatable(t)
+
+ self:puts('{')
+ self:down(function()
+ local count = 0
+ for i=1, sequenceLength do
+ if count > 0 then self:puts(',') end
+ self:puts(' ')
+ self:putValue(t[i])
+ count = count + 1
+ end
+
+ for i=1, nonSequentialKeysLength do
+ local k = nonSequentialKeys[i]
+ if count > 0 then self:puts(',') end
+ self:tabify()
+ self:putKey(k)
+ self:puts(' = ')
+ self:putValue(t[k])
+ count = count + 1
+ end
+
+ if type(mt) == 'table' then
+ if count > 0 then self:puts(',') end
+ self:tabify()
+ self:puts(' = ')
+ self:putValue(mt)
+ end
+ end)
+
+ if nonSequentialKeysLength > 0 or type(mt) == 'table' then -- result is multi-lined. Justify closing }
+ self:tabify()
+ elseif sequenceLength > 0 then -- array tables have one extra space before closing }
+ self:puts(' ')
+ end
+
+ self:puts('}')
+ end
+end
+
+function Inspector:putValue(v)
+ local tv = type(v)
+
+ if tv == 'string' then
+ self:puts(smartQuote(escape(v)))
+ elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
+ tv == 'cdata' or tv == 'ctype' then
+ self:puts(tostring(v))
+ elseif tv == 'table' then
+ self:putTable(v)
+ else
+ self:puts('<', tv, ' ', self:getId(v), '>')
+ end
+end
+
+-------------------------------------------------------------------
+
+function inspect.inspect(root, options)
+ options = options or {}
+
+ local depth = options.depth or math.huge
+ local newline = options.newline or '\n'
+ local indent = options.indent or ' '
+ local process = options.process
+
+ if process then
+ root = processRecursive(process, root, {}, {})
+ end
+
+ local inspector = setmetatable({
+ depth = depth,
+ level = 0,
+ buffer = {},
+ ids = {},
+ maxIds = {},
+ newline = newline,
+ indent = indent,
+ tableAppearances = countTableAppearances(root)
+ }, Inspector_mt)
+
+ inspector:putValue(root)
+
+ return table.concat(inspector.buffer)
+end
+
+setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end })
+
+return inspect
+
diff --git a/luprex/luajit/README b/luprex/luajit/README
index 1858819a..d7a4064e 100644
--- a/luprex/luajit/README
+++ b/luprex/luajit/README
@@ -1,4 +1,11 @@
-This is a patched copy of LuaJIT 2.1.0-beta3
+Josh's notes:
+
+Each time you modify LuaJIT, you need to manually rebuild it:
+
+ prelua-build.bat
+
+This also copies the library file to the target locations.
+
-----------------------------
README for LuaJIT 2.1.0-beta3
diff --git a/luprex/luajit/prelua-build.bat b/luprex/luajit/prelua-build.bat
new file mode 100644
index 00000000..2264b582
--- /dev/null
+++ b/luprex/luajit/prelua-build.bat
@@ -0,0 +1,8 @@
+cd src
+make libluajit.a
+cp libluajit.a ../../lib
+cp lua.h ../../inc
+cp luaconf.h ../../inc
+cp lauxlib.h ../../inc
+cp lualib.h ../../inc
+cp lauxlib.h ../../inc
diff --git a/luprex/luajit/src/Makefile b/luprex/luajit/src/Makefile
index f56465d1..402fbd7f 100644
--- a/luprex/luajit/src/Makefile
+++ b/luprex/luajit/src/Makefile
@@ -36,7 +36,8 @@ CC= $(DEFAULT_CC)
# to slow down the C part by not omitting it. Debugging, tracebacks and
# unwinding are not affected -- the assembler part has frame unwind
# information and GCC emits it where needed (x64) or with -g (see CCDEBUG).
-CCOPT= -O2 -fomit-frame-pointer
+#CCOPT= -O2 -fomit-frame-pointer
+CCOPT=
# Use this if you want to generate a smaller binary (but it's slower):
#CCOPT= -Os -fomit-frame-pointer
# Note: it's no longer recommended to use -O3 with GCC 4.x.
@@ -56,7 +57,7 @@ CCOPT_mips=
#
CCDEBUG=
# Uncomment the next line to generate debug information:
-#CCDEBUG= -g
+CCDEBUG= -g
#
CCWARN= -Wall
# Uncomment the next line to enable more warnings:
diff --git a/luprex/main.c b/luprex/main.c
new file mode 100644
index 00000000..751750de
--- /dev/null
+++ b/luprex/main.c
@@ -0,0 +1,233 @@
+/*
+** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc.
+** Copyright (C) 2005-2017 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
+*/
+
+#include
+#include
+#include
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+#include "luajit.h"
+
+#include
+
+// Add another error status.
+#define LUA_PARTIAL (LUA_ERRERR + 10)
+#define LUA_EOF (LUA_ERRERR + 11)
+
+static lua_State *globalL = NULL;
+
+static void lstop(lua_State *L, lua_Debug *ar)
+{
+ (void)ar; /* unused arg. */
+ lua_sethook(L, NULL, 0, 0);
+ /* Avoid luaL_error -- a C hook doesn't add an extra frame. */
+ luaL_where(L, 0);
+ lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
+ lua_error(L);
+}
+
+static void laction(int i)
+{
+ signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
+ terminate process (default action) */
+ lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
+}
+
+static void l_message(const char *msg)
+{
+ fputs(msg, stderr);
+ fputc('\n', stderr);
+ fflush(stderr);
+}
+
+static int report(lua_State *L, int status)
+{
+ if (status && !lua_isnil(L, -1))
+ {
+ const char *msg = lua_tostring(L, -1);
+ if (msg == NULL)
+ msg = "(error object is not a string)";
+ l_message(msg);
+ lua_pop(L, 1);
+ }
+ return status;
+}
+
+static int traceback(lua_State *L)
+{
+ if (!lua_isstring(L, 1))
+ { /* Non-string error object? Try metamethod. */
+ if (lua_isnoneornil(L, 1) ||
+ !luaL_callmeta(L, 1, "__tostring") ||
+ !lua_isstring(L, -1))
+ return 1; /* Return non-string error object. */
+ lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
+ }
+ luaL_traceback(L, L, lua_tostring(L, 1), 1);
+ return 1;
+}
+
+static int docall(lua_State *L, int narg, int clear)
+{
+ int status;
+ int base = lua_gettop(L) - narg; /* function index */
+ lua_pushcfunction(L, traceback); /* push traceback function */
+ lua_insert(L, base); /* put it under chunk and args */
+ signal(SIGINT, laction);
+ status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
+ signal(SIGINT, SIG_DFL);
+ lua_remove(L, base); /* remove traceback function */
+ /* force a complete garbage collection in case of errors */
+ if (status != LUA_OK)
+ lua_gc(L, LUA_GCCOLLECT, 0);
+ return status;
+}
+
+// Print a prompt, read a line of text, and push it on the stack.
+// If it's the first line, translate "=" to "return"
+//
+static int pushline(lua_State *L, int firstline)
+{
+ const int MAXINPUT = 1000;
+ char buf[MAXINPUT];
+ fputs(firstline ? "> " : ">> ", stdout);
+ fflush(stdout);
+ if (fgets(buf, MAXINPUT, stdin))
+ {
+ size_t len = strlen(buf);
+ if (len > 0 && buf[len - 1] == '\n')
+ buf[len - 1] = '\0';
+ if (firstline && buf[0] == '=')
+ lua_pushfstring(L, "return %s", buf + 1);
+ else
+ lua_pushstring(L, buf);
+ return 1;
+ }
+ return 0;
+}
+
+// Same as lua_loadbuffer, except: if the form passed in
+// is a partial form, returns LUA_PARTIAL.
+//
+static int loadbuffer_partial(lua_State *L, const char *s, int len, const char *fn)
+{
+ const char *eof = "''";
+ int status = luaL_loadbuffer(L, s, len, fn);
+ if (status != LUA_ERRSYNTAX)
+ {
+ return status;
+ }
+ size_t lmsg;
+ const char *msg = lua_tolstring(L, -1, &lmsg);
+ const char *tp = msg + lmsg - (sizeof(eof) - 1);
+ if (strstr(msg, eof) == tp)
+ {
+ return LUA_PARTIAL;
+ }
+ else
+ {
+ return status;
+ }
+}
+
+// Read an entire form. On success, returns a closure. On EOF,
+// returns just the LUA_EOF status code. On failure, returns an
+// error message and a status code.
+static int read_and_load(lua_State *L)
+{
+ int status;
+ lua_settop(L, 0);
+ if (!pushline(L, 1))
+ return LUA_EOF; /* no input */
+ for (;;)
+ { /* repeat until gets a complete line */
+ status = loadbuffer_partial(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
+ if (status != LUA_PARTIAL)
+ break;
+ lua_pop(L, 1); /* pop the error message */
+ if (!pushline(L, 0)) /* no more input? */
+ return LUA_EOF;
+ lua_pushliteral(L, "\n"); /* add a new line... */
+ lua_insert(L, -2); /* ...between the two lines */
+ lua_concat(L, 3); /* join them */
+ }
+ lua_remove(L, 1); /* remove line */
+ return status;
+}
+
+static void dotty(lua_State *L)
+{
+ while (1)
+ {
+ int status = read_and_load(L);
+ if (status == LUA_EOF) break;
+ if (status == LUA_OK) status = docall(L, 0, 0);
+ report(L, status);
+ if (status == LUA_OK && lua_gettop(L) > 0)
+ { /* any result to print? */
+ lua_getglobal(L, "print");
+ lua_insert(L, 1);
+ if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0)
+ {
+ l_message(
+ lua_pushfstring(L, "error calling 'print' (%s)",
+ lua_tostring(L, -1)));
+ }
+ }
+ }
+ lua_settop(L, 0); /* clear stack */
+ fputs("\n", stdout);
+ fflush(stdout);
+}
+
+static int loadmain(lua_State *L)
+{
+ int status = luaL_loadfilex(L, "main.lua", NULL);
+ if (status == LUA_OK)
+ {
+ status = docall(L, 0, 0);
+ }
+ report(L, status);
+ return status;
+}
+
+static int pmain(lua_State *L)
+{
+ globalL = L;
+
+ LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */
+
+ lua_gc(L, LUA_GCSTOP, 0);
+ luaL_openlibs(L);
+ lua_gc(L, LUA_GCRESTART, -1);
+
+ int status = loadmain(L);
+ if (status != LUA_OK) {
+ return status;
+ }
+
+ dotty(L);
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int status;
+ lua_State *L = lua_open();
+ if (L == NULL)
+ {
+ l_message("cannot create state: not enough memory");
+ return -1;
+ }
+ status = lua_cpcall(L, pmain, NULL);
+ report(L, status);
+ lua_close(L);
+ return status ? -1 : 0;
+}
diff --git a/luprex/main.lua b/luprex/main.lua
new file mode 100644
index 00000000..1f061adc
--- /dev/null
+++ b/luprex/main.lua
@@ -0,0 +1,101 @@
+
+inspect = require("inspect")
+
+
+-- If t is a table, return it. If not, return a blank table.
+function forcetable(t)
+ if type(t) == "table" then
+ return t
+ else
+ return {}
+ end
+end
+
+-- Clear the table out. Deletes all keys, removes any metatable.
+-- TODO: this doesn't work on tables that have __metatable metamethod.
+function rawclear(t)
+ for k in pairs(t) do
+ rawset(t, k, nil)
+ end
+ setmetatable(t, nil)
+ return t
+end
+
+-- given a class database, removes all the functions that have
+-- been inserted into it, but keeps the class and action tables.
+function reset_class_db(db)
+ for name,maybeclass in pairs(db) do
+ local class = forcetable(maybeclass)
+ local action = forcetable(rawget(class, "action"))
+ rawclear(class)
+ rawclear(action)
+ rawset(db, name, class)
+ class.action = action
+ class.__index = class
+ class.__class = name
+ end
+end
+
+-- Given a class_db, makes an accessor that fetches classes
+-- from that class db.
+function make_class_accessor(db)
+ return function(name)
+ local class = rawget(db, name)
+ if type(class) ~= "table" then
+ class = {}
+ rawset(db, name, class)
+ class.action = {}
+ class.__index = class
+ class.__class = name
+ end
+ return class
+ end
+end
+
+-- given a global_db, makes an accessor that fetches named
+-- tables from that global_db.
+function make_global_accessor(db)
+ return function(name)
+ local global = rawget(db, name)
+ if type(global) ~= "table" then
+ global = {}
+ rawset(db, name, global)
+ end
+ return global
+ end
+end
+
+-- The sandbox list - a list of the builtin functions that gets imported
+-- into world models. This is a very weak form of sandboxing, a world
+-- model can still easily use functions that are not on this list. If you
+-- want stronger sandboxing, you need to not link in certain builtins at all.
+--
+-- Omitted from this list: "debug", "dofile", "getfenv", "io", "jit",
+-- "load", "loadfile", "loadstring", "module", "newproxy", "os", "package",
+-- "pcall", "require", "xpcall"
+--
+local sandbox_list = { "assert", "bit", "error", "getmetatable", "inspect",
+ "ipairs", "math", "next", "pairs", "print", "rawequal", "rawget", "rawset",
+ "select", "setmetatable", "tonumber", "tostring", "type", "unpack" }
+
+-- Make a world model.
+--
+function make_world_model()
+ genv = {}
+ genv._G = genv
+ meta = {}
+ meta.class_db = {}
+ meta.global_db = {}
+ meta.source_db = {}
+ meta.tangible_db = {}
+ meta.__newindex = function() error("Global environment is read-only") end
+ -- meta.__metatable = false
+ genv.class = make_class_accessor(meta.class_db)
+ genv.global = make_global_accessor(meta.global_db)
+ for i,name in ipairs(sandbox_list) do
+ genv[name] = _G[name]
+ end
+ setmetatable(genv, meta)
+ return genv
+end
+