summaryrefslogtreecommitdiff
path: root/src/lobject.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lobject.js')
-rw-r--r--src/lobject.js444
1 files changed, 265 insertions, 179 deletions
diff --git a/src/lobject.js b/src/lobject.js
index 1b490f3..41bd702 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -1,24 +1,76 @@
"use strict";
-const assert = require('assert');
-
-const defs = require('./defs.js');
-const ljstype = require('./ljstype.js');
+const {
+ LUA_OPADD,
+ LUA_OPBAND,
+ LUA_OPBNOT,
+ LUA_OPBOR,
+ LUA_OPBXOR,
+ LUA_OPDIV,
+ LUA_OPIDIV,
+ LUA_OPMOD,
+ LUA_OPMUL,
+ LUA_OPPOW,
+ LUA_OPSHL,
+ LUA_OPSHR,
+ LUA_OPSUB,
+ LUA_OPUNM,
+ constant_types: {
+ LUA_NUMTAGS,
+ LUA_TBOOLEAN,
+ LUA_TCCL,
+ LUA_TFUNCTION,
+ LUA_TLCF,
+ LUA_TLCL,
+ LUA_TLIGHTUSERDATA,
+ LUA_TLNGSTR,
+ LUA_TNIL,
+ LUA_TNUMBER,
+ LUA_TNUMFLT,
+ LUA_TNUMINT,
+ LUA_TSHRSTR,
+ LUA_TSTRING,
+ LUA_TTABLE,
+ LUA_TTHREAD,
+ LUA_TUSERDATA
+ },
+ from_userstring,
+ luastring_indexOf,
+ luastring_of,
+ to_jsstring,
+ to_luastring
+} = require('./defs.js');
+const {
+ lisdigit,
+ lisprint,
+ lisspace,
+ lisxdigit
+} = require('./ljstype.js');
const ldebug = require('./ldebug.js');
const ldo = require('./ldo.js');
-const lfunc = require('./lfunc.js');
const lstate = require('./lstate.js');
-const lstring = require('./lstring.js');
+const {
+ luaS_bless,
+ luaS_new
+} = require('./lstring.js');
const ltable = require('./ltable.js');
-const luaconf = require('./luaconf.js');
+const {
+ LUA_COMPAT_FLOATSTRING,
+ ldexp,
+ lua_getlocaledecpoint,
+ lua_integer2str,
+ lua_number2str
+} = require('./luaconf.js');
const lvm = require('./lvm.js');
-const llimits = require('./llimits.js');
+const {
+ MAX_INT,
+ luai_nummod,
+ lua_assert
+} = require("./llimits.js");
const ltm = require('./ltm.js');
-const CT = defs.constant_types;
-const char = defs.char;
-const LUA_TPROTO = CT.LUA_NUMTAGS;
-const LUA_TDEADKEY = CT.LUA_NUMTAGS+1;
+const LUA_TPROTO = LUA_NUMTAGS;
+const LUA_TDEADKEY = LUA_NUMTAGS+1;
class TValue {
@@ -46,71 +98,71 @@ class TValue {
}
ttisnumber() {
- return this.checktype(CT.LUA_TNUMBER);
+ return this.checktype(LUA_TNUMBER);
}
ttisfloat() {
- return this.checktag(CT.LUA_TNUMFLT);
+ return this.checktag(LUA_TNUMFLT);
}
ttisinteger() {
- return this.checktag(CT.LUA_TNUMINT);
+ return this.checktag(LUA_TNUMINT);
}
ttisnil() {
- return this.checktag(CT.LUA_TNIL);
+ return this.checktag(LUA_TNIL);
}
ttisboolean() {
- return this.checktag(CT.LUA_TBOOLEAN);
+ return this.checktag(LUA_TBOOLEAN);
}
ttislightuserdata() {
- return this.checktag(CT.LUA_TLIGHTUSERDATA);
+ return this.checktag(LUA_TLIGHTUSERDATA);
}
ttisstring() {
- return this.checktype(CT.LUA_TSTRING);
+ return this.checktype(LUA_TSTRING);
}
ttisshrstring() {
- return this.checktag(CT.LUA_TSHRSTR);
+ return this.checktag(LUA_TSHRSTR);
}
ttislngstring() {
- return this.checktag(CT.LUA_TLNGSTR);
+ return this.checktag(LUA_TLNGSTR);
}
ttistable() {
- return this.checktag(CT.LUA_TTABLE);
+ return this.checktag(LUA_TTABLE);
}
ttisfunction() {
- return this.checktype(CT.LUA_TFUNCTION);
+ return this.checktype(LUA_TFUNCTION);
}
ttisclosure() {
- return (this.type & 0x1F) === CT.LUA_TFUNCTION;
+ return (this.type & 0x1F) === LUA_TFUNCTION;
}
ttisCclosure() {
- return this.checktag(CT.LUA_TCCL);
+ return this.checktag(LUA_TCCL);
}
ttisLclosure() {
- return this.checktag(CT.LUA_TLCL);
+ return this.checktag(LUA_TLCL);
}
ttislcf() {
- return this.checktag(CT.LUA_TLCF);
+ return this.checktag(LUA_TLCF);
}
ttisfulluserdata() {
- return this.checktag(CT.LUA_TUSERDATA);
+ return this.checktag(LUA_TUSERDATA);
}
ttisthread() {
- return this.checktag(CT.LUA_TTHREAD);
+ return this.checktag(LUA_TTHREAD);
}
ttisdeadkey() {
@@ -122,72 +174,72 @@ class TValue {
}
setfltvalue(x) {
- this.type = CT.LUA_TNUMFLT;
+ this.type = LUA_TNUMFLT;
this.value = x;
}
chgfltvalue(x) {
- assert(this.type == CT.LUA_TNUMFLT);
+ lua_assert(this.type == LUA_TNUMFLT);
this.value = x;
}
setivalue(x) {
- this.type = CT.LUA_TNUMINT;
+ this.type = LUA_TNUMINT;
this.value = x;
}
chgivalue(x) {
- assert(this.type == CT.LUA_TNUMINT);
+ lua_assert(this.type == LUA_TNUMINT);
this.value = x;
}
setnilvalue() {
- this.type = CT.LUA_TNIL;
- this.value = void 0;
+ this.type = LUA_TNIL;
+ this.value = null;
}
setfvalue(x) {
- this.type = CT.LUA_TLCF;
+ this.type = LUA_TLCF;
this.value = x;
}
setpvalue(x) {
- this.type = CT.LUA_TLIGHTUSERDATA;
+ this.type = LUA_TLIGHTUSERDATA;
this.value = x;
}
setbvalue(x) {
- this.type = CT.LUA_TBOOLEAN;
+ this.type = LUA_TBOOLEAN;
this.value = x;
}
setsvalue(x) {
- this.type = CT.LUA_TLNGSTR; /* LUA_TSHRSTR? */
+ this.type = LUA_TLNGSTR; /* LUA_TSHRSTR? */
this.value = x;
}
setuvalue(x) {
- this.type = CT.LUA_TUSERDATA;
+ this.type = LUA_TUSERDATA;
this.value = x;
}
setthvalue(x) {
- this.type = CT.LUA_TTHREAD;
+ this.type = LUA_TTHREAD;
this.value = x;
}
setclLvalue(x) {
- this.type = CT.LUA_TLCL;
+ this.type = LUA_TLCL;
this.value = x;
}
setclCvalue(x) {
- this.type = CT.LUA_TCCL;
+ this.type = LUA_TCCL;
this.value = x;
}
sethvalue(x) {
- this.type = CT.LUA_TTABLE;
+ this.type = LUA_TTABLE;
this.value = x;
}
@@ -202,7 +254,7 @@ class TValue {
}
tsvalue() {
- assert(this.ttisstring());
+ lua_assert(this.ttisstring());
return this.value;
}
@@ -215,16 +267,15 @@ class TValue {
}
jsstring(from, to) {
- return defs.to_jsstring(this.svalue(), from, to);
+ return to_jsstring(this.svalue(), from, to, true);
}
-
}
const pushobj2s = function(L, tv) {
L.stack[L.top++] = new TValue(tv.type, tv.value);
};
const pushsvalue2s = function(L, ts) {
- L.stack[L.top++] = new TValue(CT.LUA_TLNGSTR, ts);
+ L.stack[L.top++] = new TValue(LUA_TLNGSTR, ts);
};
/* from stack to (same) stack */
const setobjs2s = function(L, newidx, oldidx) {
@@ -238,7 +289,7 @@ const setsvalue2s = function(L, newidx, ts) {
L.stack[newidx].setsvalue(ts);
};
-const luaO_nilobject = new TValue(CT.LUA_TNIL, null);
+const luaO_nilobject = new TValue(LUA_TNIL, null);
Object.freeze(luaO_nilobject);
module.exports.luaO_nilobject = luaO_nilobject;
@@ -249,7 +300,7 @@ class LClosure {
this.p = null;
this.nupvalues = n;
- this.upvals = new Array(n); /* list of upvalues as UpVals. initialised in luaF_initupvals */
+ this.upvals = new Array(n); /* list of upvalues. initialised in luaF_initupvals */
}
}
@@ -263,7 +314,7 @@ class CClosure {
this.nupvalues = n;
this.upvalue = new Array(n); /* list of upvalues as TValues */
while (n--) {
- this.upvalue[n] = new TValue(CT.LUA_TNIL, null);
+ this.upvalue[n] = new TValue(LUA_TNIL, null);
}
}
@@ -275,7 +326,7 @@ class Udata {
this.id = L.l_G.id_counter++;
this.metatable = null;
- this.uservalue = new TValue(CT.LUA_TNIL, null);
+ this.uservalue = new TValue(LUA_TNIL, null);
this.len = size;
this.data = Object.create(null); // ignores size argument
}
@@ -294,23 +345,26 @@ class LocVar {
}
}
-const RETS = defs.to_luastring("...");
-const PRE = defs.to_luastring("[string \"");
-const POS = defs.to_luastring("\"]");
+const RETS = to_luastring("...");
+const PRE = to_luastring("[string \"");
+const POS = to_luastring("\"]");
const luaO_chunkid = function(source, bufflen) {
let l = source.length;
let out;
- if (source[0] === char['=']) { /* 'literal' source */
- if (l < bufflen) /* small enough? */
- out = source.slice(1);
- else { /* truncate it */
- out = source.slice(1, bufflen+1);
+ if (source[0] === 61 /* ('=').charCodeAt(0) */) { /* 'literal' source */
+ if (l < bufflen) { /* small enough? */
+ out = new Uint8Array(l-1);
+ out.set(source.subarray(1));
+ } else { /* truncate it */
+ out = new Uint8Array(bufflen);
+ out.set(source.subarray(1, bufflen+1));
}
- } else if (source[0] === char['@']) { /* file name */
- if (l <= bufflen) /* small enough? */
- out = source.slice(1);
- else { /* add '...' before rest of name */
+ } else if (source[0] === 64 /* ('@').charCodeAt(0) */) { /* file name */
+ if (l <= bufflen) { /* small enough? */
+ out = new Uint8Array(l-1);
+ out.set(source.subarray(1));
+ } else { /* add '...' before rest of name */
out = new Uint8Array(bufflen);
out.set(RETS);
bufflen -= RETS.length;
@@ -318,7 +372,7 @@ const luaO_chunkid = function(source, bufflen) {
}
} else { /* string; format as [string "source"] */
out = new Uint8Array(bufflen);
- let nli = source.indexOf(char['\n']); /* find first new line (if any) */
+ let nli = luastring_indexOf(source, 10 /* ('\n').charCodeAt(0) */); /* find first new line (if any) */
out.set(PRE); /* add prefix */
let out_i = PRE.length;
bufflen -= PRE.length + RETS.length + POS.length; /* save space for prefix+suffix */
@@ -341,15 +395,15 @@ const luaO_chunkid = function(source, bufflen) {
};
const luaO_hexavalue = function(c) {
- if (ljstype.lisdigit(c)) return c - char['0'];
- else return (String.fromCharCode(c).toLowerCase().charCodeAt(0) - char['a']) + 10;
+ if (lisdigit(c)) return c - 48;
+ else return (c & 0xdf) - 55;
};
const UTF8BUFFSZ = 8;
const luaO_utf8esc = function(buff, x) {
let n = 1; /* number of bytes put in buffer (backwards) */
- assert(x <= 0x10FFFF);
+ lua_assert(x <= 0x10FFFF);
if (x < 0x80) /* ascii? */
buff[UTF8BUFFSZ - 1] = x;
else { /* need continuation bytes */
@@ -374,24 +428,24 @@ const MAXSIGDIG = 30;
*/
const lua_strx2number = function(s) {
let i = 0;
- let dot = char[luaconf.lua_getlocaledecpoint()];
+ let dot = lua_getlocaledecpoint();
let r = 0.0; /* result (accumulator) */
let sigdig = 0; /* number of significant digits */
let nosigdig = 0; /* number of non-significant digits */
let e = 0; /* exponent correction */
let neg; /* 1 if number is negative */
let hasdot = false; /* true after seen a dot */
- while (ljstype.lisspace(s[i])) i++; /* skip initial spaces */
- if ((neg = (s[i] === char['-']))) i++; /* check signal */
- else if (s[i] === char['+']) i++;
- if (!(s[i] === char['0'] && (s[i+1] === char['x'] || s[i+1] === char['X']))) /* check '0x' */
+ while (lisspace(s[i])) i++; /* skip initial spaces */
+ if ((neg = (s[i] === 45 /* ('-').charCodeAt(0) */))) i++; /* check signal */
+ else if (s[i] === 43 /* ('+').charCodeAt(0) */) i++;
+ if (!(s[i] === 48 /* ('0').charCodeAt(0) */ && (s[i+1] === 120 /* ('x').charCodeAt(0) */ || s[i+1] === 88 /* ('X').charCodeAt(0) */))) /* check '0x' */
return null; /* invalid format (no '0x') */
for (i += 2; ; i++) { /* skip '0x' and read numeral */
if (s[i] === dot) {
if (hasdot) break; /* second dot? stop loop */
else hasdot = true;
- } else if (ljstype.lisxdigit(s[i])) {
- if (sigdig === 0 && s[i] === char['0']) /* non-significant digit (zero)? */
+ } else if (lisxdigit(s[i])) {
+ if (sigdig === 0 && s[i] === 48 /* ('0').charCodeAt(0) */) /* non-significant digit (zero)? */
nosigdig++;
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
r = (r * 16) + luaO_hexavalue(s[i]);
@@ -403,29 +457,29 @@ const lua_strx2number = function(s) {
if (nosigdig + sigdig === 0) /* no digits? */
return null; /* invalid format */
e *= 4; /* each digit multiplies/divides value by 2^4 */
- if (s[i] === char['p'] || s[i] === char['P']) { /* exponent part? */
+ if (s[i] === 112 /* ('p').charCodeAt(0) */ || s[i] === 80 /* ('P').charCodeAt(0) */) { /* exponent part? */
let exp1 = 0; /* exponent value */
let neg1; /* exponent signal */
i++; /* skip 'p' */
- if ((neg1 = (s[i] === char['-']))) i++; /* signal */
- else if (s[i] === char['+']) i++;
- if (!ljstype.lisdigit(s[i]))
+ if ((neg1 = (s[i] === 45 /* ('-').charCodeAt(0) */))) i++; /* signal */
+ else if (s[i] === 43 /* ('+').charCodeAt(0) */) i++;
+ if (!lisdigit(s[i]))
return null; /* invalid; must have at least one digit */
- while (ljstype.lisdigit(s[i])) /* read exponent */
- exp1 = exp1 * 10 + s[i++] - char['0'];
+ while (lisdigit(s[i])) /* read exponent */
+ exp1 = exp1 * 10 + s[i++] - 48 /* ('0').charCodeAt(0) */;
if (neg1) exp1 = -exp1;
e += exp1;
}
if (neg) r = -r;
return {
- n: luaconf.ldexp(r, e),
+ n: ldexp(r, e),
i: i
};
};
const lua_str2number = function(s) {
try {
- s = defs.to_jsstring(s);
+ s = to_jsstring(s);
} catch (e) {
return null;
}
@@ -441,26 +495,46 @@ const lua_str2number = function(s) {
const l_str2dloc = function(s, mode) {
let result = mode === 'x' ? lua_strx2number(s) : lua_str2number(s); /* try to convert */
if (result === null) return null;
- while (ljstype.lisspace(s[result.i])) result.i++; /* skip trailing spaces */
+ while (lisspace(s[result.i])) result.i++; /* skip trailing spaces */
return (result.i === s.length || s[result.i] === 0) ? result : null; /* OK if no trailing characters */
};
+const SIGILS = [
+ 46 /* (".").charCodeAt(0) */,
+ 120 /* ("x").charCodeAt(0) */,
+ 88 /* ("X").charCodeAt(0) */,
+ 110 /* ("n").charCodeAt(0) */,
+ 78 /* ("N").charCodeAt(0) */
+];
+const modes = {
+ [ 46]: ".",
+ [120]: "x",
+ [ 88]: "x",
+ [110]: "n",
+ [ 78]: "n"
+};
const l_str2d = function(s) {
- let pidx = /[.xXnN]/g.exec(String.fromCharCode(...s));
- pidx = pidx ? pidx.index : null;
- let pmode = pidx ? s[pidx] : null;
- let mode = pmode ? String.fromCharCode(pmode).toLowerCase() : 0;
+ let l = s.length;
+ let pmode = 0;
+ for (let i=0; i<l; i++) {
+ let v = s[i];
+ if (SIGILS.indexOf(v) !== -1) {
+ pmode = v;
+ break;
+ }
+ }
+ let mode = modes[pmode];
if (mode === 'n') /* reject 'inf' and 'nan' */
return null;
let end = l_str2dloc(s, mode); /* try to convert */
- if (end === null) { /* failed? may be a different locale */
- // throw new Error("Locale not available to handle number"); // TODO
- }
+ // if (end === null) { /* failed? may be a different locale */
+ // throw new Error("Locale not available to handle number"); // TODO
+ // }
return end;
};
-const MAXBY10 = Math.floor(llimits.MAX_INT / 10);
-const MAXLASTD = llimits.MAX_INT % 10;
+const MAXBY10 = Math.floor(MAX_INT / 10);
+const MAXLASTD = MAX_INT % 10;
const l_str2int = function(s) {
let i = 0;
@@ -468,26 +542,25 @@ const l_str2int = function(s) {
let empty = true;
let neg;
- while (ljstype.lisspace(s[i])) i++; /* skip initial spaces */
- if ((neg = (s[i] === char['-']))) i++;
- else if (s[i] === char['+']) i++;
- if (s[i] === char['0'] && (s[i+1] === char['x'] || s[i+1] === char['X'])) { /* hex? */
+ while (lisspace(s[i])) i++; /* skip initial spaces */
+ if ((neg = (s[i] === 45 /* ('-').charCodeAt(0) */))) i++;
+ else if (s[i] === 43 /* ('+').charCodeAt(0) */) i++;
+ if (s[i] === 48 /* ('0').charCodeAt(0) */ && (s[i+1] === 120 /* ('x').charCodeAt(0) */ || s[i+1] === 88 /* ('X').charCodeAt(0) */)) { /* hex? */
i += 2; /* skip '0x' */
-
- for (; ljstype.lisxdigit(s[i]); i++) {
+ for (; i < s.length && lisxdigit(s[i]); i++) {
a = (a * 16 + luaO_hexavalue(s[i]))|0;
empty = false;
}
} else { /* decimal */
- for (; ljstype.lisdigit(s[i]); i++) {
- let d = s[i] - char['0'];
+ for (; i < s.length && lisdigit(s[i]); i++) {
+ let d = s[i] - 48 /* ('0').charCodeAt(0) */;
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
return null; /* do not accept it (as integer) */
a = (a * 10 + d)|0;
empty = false;
}
}
- while (ljstype.lisspace(s[i])) i++; /* skip trailing spaces */
+ while (i < s.length && lisspace(s[i])) i++; /* skip trailing spaces */
if (empty || (i !== s.length && s[i] !== 0)) return null; /* something wrong in the numeral */
else {
return {
@@ -512,25 +585,23 @@ const luaO_str2num = function(s, o) {
}
};
-/* this currently returns new TValue instead of modifying */
const luaO_tostring = function(L, obj) {
let buff;
if (obj.ttisinteger())
- buff = defs.to_luastring(luaconf.lua_integer2str(obj.value));
+ buff = to_luastring(lua_integer2str(obj.value));
else {
- let str = luaconf.lua_number2str(obj.value);
- // Assume no LUA_COMPAT_FLOATSTRING
- if (/^[-0123456789]+$/.test(str)) { /* looks like an int? */
- str += luaconf.lua_getlocaledecpoint() + '0'; /* adds '.0' to result */
+ let str = lua_number2str(obj.value);
+ if (!LUA_COMPAT_FLOATSTRING && /^[-0123456789]+$/.test(str)) { /* looks like an int? */
+ str += String.fromCharCode(lua_getlocaledecpoint()) + '0'; /* adds '.0' to result */
}
- buff = defs.to_luastring(str);
+ buff = to_luastring(str);
}
- obj.setsvalue(lstring.luaS_bless(L, buff));
+ obj.setsvalue(luaS_bless(L, buff));
};
const pushstr = function(L, str) {
ldo.luaD_inctop(L);
- setsvalue2s(L, L.top-1, lstring.luaS_new(L, str));
+ setsvalue2s(L, L.top-1, luaS_new(L, str));
};
const luaO_pushvfstring = function(L, fmt, argp) {
@@ -539,82 +610,97 @@ const luaO_pushvfstring = function(L, fmt, argp) {
let a = 0;
let e;
for (;;) {
- e = fmt.indexOf(char['%'], i);
+ e = luastring_indexOf(fmt, 37 /* ('%').charCodeAt(0) */, i);
if (e == -1) break;
pushstr(L, fmt.subarray(i, e));
switch(fmt[e+1]) {
- case char['s']: {
+ case 115 /* ('s').charCodeAt(0) */: {
let s = argp[a++];
- if (s === null) s = defs.to_luastring("(null)", true);
+ if (s === null) s = to_luastring("(null)", true);
else {
- s = defs.from_userstring(s);
+ s = from_userstring(s);
/* respect null terminator */
- let i = s.indexOf(0);
+ let i = luastring_indexOf(s, 0);
if (i !== -1)
s = s.subarray(0, i);
}
pushstr(L, s);
break;
}
- case char['c']: {
+ case 99 /* ('c').charCodeAt(0) */: {
let buff = argp[a++];
- if (ljstype.lisprint(buff))
- pushstr(L, defs.string_of(buff));
+ if (lisprint(buff))
+ pushstr(L, luastring_of(buff));
else
- luaO_pushfstring(L, defs.to_luastring("<\\%d>", true), buff);
+ luaO_pushfstring(L, to_luastring("<\\%d>", true), buff);
break;
}
- case char['d']:
- case char['I']:
+ case 100 /* ('d').charCodeAt(0) */:
+ case 73 /* ('I').charCodeAt(0) */:
ldo.luaD_inctop(L);
L.stack[L.top-1].setivalue(argp[a++]);
luaO_tostring(L, L.stack[L.top-1]);
break;
- case char['f']:
+ case 102 /* ('f').charCodeAt(0) */:
ldo.luaD_inctop(L);
L.stack[L.top-1].setfltvalue(argp[a++]);
luaO_tostring(L, L.stack[L.top-1]);
break;
- case char['p']: {
+ case 112 /* ('p').charCodeAt(0) */: {
let v = argp[a++];
if (v instanceof lstate.lua_State ||
v instanceof ltable.Table ||
v instanceof Udata ||
v instanceof LClosure ||
- v instanceof CClosure ||
- v instanceof lfunc.UpVal) {
- pushstr(L, defs.to_luastring("0x"+v.id.toString(16)));
- } else if (v === null) { /* handle null before checking for typeof == object */
- pushstr(L, defs.to_luastring("null"));
- } else if (typeof v === "function" || typeof v === "object") {
- let id = L.l_G.ids.get(v);
- if (!id) {
- id = L.l_G.id_counter++;
- L.l_G.ids.set(v, id);
- }
- pushstr(L, defs.to_luastring("0x"+id.toString(16)));
- } else if (v === void 0) {
- pushstr(L, defs.to_luastring("undefined"));
- } else if (typeof v === "number") { /* before check object as null is an object */
- pushstr(L, defs.to_luastring("Number("+v+")"));
- } else if (typeof v === "string") { /* before check object as null is an object */
- pushstr(L, defs.to_luastring("String("+JSON.stringify(v)+")"));
- } else if (typeof v === "boolean") { /* before check object as null is an object */
- pushstr(L, defs.to_luastring(v?"Boolean(true)":"Boolean(false)"));
+ v instanceof CClosure) {
+ pushstr(L, to_luastring("0x"+v.id.toString(16)));
} else {
- /* user provided object. no id available */
- pushstr(L, defs.to_luastring("<id NYI>"));
+ switch(typeof v) {
+ case "undefined":
+ pushstr(L, to_luastring("undefined"));
+ break;
+ case "number": /* before check object as null is an object */
+ pushstr(L, to_luastring("Number("+v+")"));
+ break;
+ case "string": /* before check object as null is an object */
+ pushstr(L, to_luastring("String("+JSON.stringify(v)+")"));
+ break;
+ case "boolean": /* before check object as null is an object */
+ pushstr(L, to_luastring(v?"Boolean(true)":"Boolean(false)"));
+ break;
+ case "object":
+ if (v === null) { /* null is special */
+ pushstr(L, to_luastring("null"));
+ break;
+ }
+ /* fall through */
+ case "function": {
+ let id = L.l_G.ids.get(v);
+ if (!id) {
+ id = L.l_G.id_counter++;
+ L.l_G.ids.set(v, id);
+ }
+ pushstr(L, to_luastring("0x"+id.toString(16)));
+ break;
+ }
+ default:
+ /* user provided object. no id available */
+ pushstr(L, to_luastring("<id NYI>"));
+ }
}
break;
}
- case char['U']:
- pushstr(L, defs.to_luastring(String.fromCodePoint(argp[a++])));
+ case 85 /* ('U').charCodeAt(0) */: {
+ let buff = new Uint8Array(UTF8BUFFSZ);
+ let l = luaO_utf8esc(buff, argp[a++]);
+ pushstr(L, buff.subarray(UTF8BUFFSZ - l));
break;
- case char['%']:
- pushstr(L, defs.to_luastring("%", true));
+ }
+ case 37 /* ('%').charCodeAt(0) */:
+ pushstr(L, to_luastring("%", true));
break;
default:
- ldebug.luaG_runerror(L, defs.to_luastring("invalid option '%%%c' to 'lua_pushfstring'"), fmt[e + 1]);
+ ldebug.luaG_runerror(L, to_luastring("invalid option '%%%c' to 'lua_pushfstring'"), fmt[e + 1]);
}
n += 2;
i = e + 2;
@@ -651,34 +737,34 @@ const luaO_int2fb = function(x) {
const intarith = function(L, op, v1, v2) {
switch (op) {
- case defs.LUA_OPADD: return (v1 + v2)|0;
- case defs.LUA_OPSUB: return (v1 - v2)|0;
- case defs.LUA_OPMUL: return Math.imul(v1, v2);
- case defs.LUA_OPMOD: return lvm.luaV_mod(L, v1, v2);
- case defs.LUA_OPIDIV: return lvm.luaV_div(L, v1, v2);
- case defs.LUA_OPBAND: return (v1 & v2);
- case defs.LUA_OPBOR: return (v1 | v2);
- case defs.LUA_OPBXOR: return (v1 ^ v2);
- case defs.LUA_OPSHL: return lvm.luaV_shiftl(v1, v2);
- case defs.LUA_OPSHR: return lvm.luaV_shiftl(v1, -v2);
- case defs.LUA_OPUNM: return (0 - v1)|0;
- case defs.LUA_OPBNOT: return (~0 ^ v1);
- default: assert(0);
+ case LUA_OPADD: return (v1 + v2)|0;
+ case LUA_OPSUB: return (v1 - v2)|0;
+ case LUA_OPMUL: return lvm.luaV_imul(v1, v2);
+ case LUA_OPMOD: return lvm.luaV_mod(L, v1, v2);
+ case LUA_OPIDIV: return lvm.luaV_div(L, v1, v2);
+ case LUA_OPBAND: return (v1 & v2);
+ case LUA_OPBOR: return (v1 | v2);
+ case LUA_OPBXOR: return (v1 ^ v2);
+ case LUA_OPSHL: return lvm.luaV_shiftl(v1, v2);
+ case LUA_OPSHR: return lvm.luaV_shiftl(v1, -v2);
+ case LUA_OPUNM: return (0 - v1)|0;
+ case LUA_OPBNOT: return (~0 ^ v1);
+ default: lua_assert(0);
}
};
const numarith = function(L, op, v1, v2) {
switch (op) {
- case defs.LUA_OPADD: return v1 + v2;
- case defs.LUA_OPSUB: return v1 - v2;
- case defs.LUA_OPMUL: return v1 * v2;
- case defs.LUA_OPDIV: return v1 / v2;
- case defs.LUA_OPPOW: return Math.pow(v1, v2);
- case defs.LUA_OPIDIV: return Math.floor(v1 / v2);
- case defs.LUA_OPUNM: return -v1;
- case defs.LUA_OPMOD: return llimits.luai_nummod(L, v1, v2);
- default: assert(0);
+ case LUA_OPADD: return v1 + v2;
+ case LUA_OPSUB: return v1 - v2;
+ case LUA_OPMUL: return v1 * v2;
+ case LUA_OPDIV: return v1 / v2;
+ case LUA_OPPOW: return Math.pow(v1, v2);
+ case LUA_OPIDIV: return Math.floor(v1 / v2);
+ case LUA_OPUNM: return -v1;
+ case LUA_OPMOD: return luai_nummod(L, v1, v2);
+ default: lua_assert(0);
}
};
@@ -686,9 +772,9 @@ const luaO_arith = function(L, op, p1, p2, p3) {
let res = (typeof p3 === "number") ? L.stack[p3] : p3; /* FIXME */
switch (op) {
- case defs.LUA_OPBAND: case defs.LUA_OPBOR: case defs.LUA_OPBXOR:
- case defs.LUA_OPSHL: case defs.LUA_OPSHR:
- case defs.LUA_OPBNOT: { /* operate only on integers */
+ case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
+ case LUA_OPSHL: case LUA_OPSHR:
+ case LUA_OPBNOT: { /* operate only on integers */
let i1, i2;
if ((i1 = lvm.tointeger(p1)) !== false && (i2 = lvm.tointeger(p2)) !== false) {
res.setivalue(intarith(L, op, i1, i2));
@@ -696,7 +782,7 @@ const luaO_arith = function(L, op, p1, p2, p3) {
}
else break; /* go to the end */
}
- case defs.LUA_OPDIV: case defs.LUA_OPPOW: { /* operate only on floats */
+ case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
let n1, n2;
if ((n1 = lvm.tonumber(p1)) !== false && (n2 = lvm.tonumber(p2)) !== false) {
res.setfltvalue(numarith(L, op, n1, n2));
@@ -718,8 +804,8 @@ const luaO_arith = function(L, op, p1, p2, p3) {
}
}
/* could not perform raw operation; try metamethod */
- assert(L !== null); /* should not fail when folding (compile time) */
- ltm.luaT_trybinTM(L, p1, p2, p3, (op - defs.LUA_OPADD) + ltm.TMS.TM_ADD);
+ lua_assert(L !== null); /* should not fail when folding (compile time) */
+ ltm.luaT_trybinTM(L, p1, p2, p3, (op - LUA_OPADD) + ltm.TMS.TM_ADD);
};