Skip to content

Commit

Permalink
Merge branch 'accept-jsstrings'
Browse files Browse the repository at this point in the history
  • Loading branch information
daurnimator committed Mar 30, 2018
2 parents 2f2bd7f + 1371afa commit fb3b9c3
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 68 deletions.
8 changes: 7 additions & 1 deletion src/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ const to_luastring = function(str, cache) {
};

const from_userstring = function(str) {
if (!is_luastring(str)) throw new TypeError("expects an array of bytes");
if (!is_luastring(str)) {
if (typeof str === "string") {
str = to_luastring(str);
} else {
throw new TypeError("expects an array of bytes or javascript string");
}
}
return str;
};

Expand Down
6 changes: 2 additions & 4 deletions src/lauxlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,14 @@ const luaL_checktype = function(L, arg, t) {
tag_error(L, arg, t);
};

const luaL_checkstring = function(L, n) {
return luaL_checklstring(L, n, null);
};

const luaL_checklstring = function(L, arg) {
let s = lua_tolstring(L, arg);
if (s === null || s === undefined) tag_error(L, arg, LUA_TSTRING);
return s;
};

const luaL_checkstring = luaL_checklstring;

const luaL_optlstring = function(L, arg, def) {
if (lua_type(L, arg) <= 0) {
return def === null ? null : from_userstring(def);
Expand Down
16 changes: 8 additions & 8 deletions src/lbaselib.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const luaB_getmetatable = function(L) {
const luaB_setmetatable = function(L) {
let t = lua_type(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, t === LUA_TNIL || t === LUA_TTABLE, 2, to_luastring("nil or table expected", true));
luaL_argcheck(L, t === LUA_TNIL || t === LUA_TTABLE, 2, "nil or table expected");
if (luaL_getmetafield(L, 1, to_luastring("__metatable", true)) !== LUA_TNIL)
return luaL_error(L, to_luastring("cannot change a protected metatable"));
lua_settop(L, 2);
Expand All @@ -173,7 +173,7 @@ const luaB_rawequal = function(L) {

const luaB_rawlen = function(L) {
let t = lua_type(L, 1);
luaL_argcheck(L, t === LUA_TTABLE || t === LUA_TSTRING, 1, to_luastring("table or string expected", true));
luaL_argcheck(L, t === LUA_TTABLE || t === LUA_TSTRING, 1, "table or string expected");
lua_pushinteger(L, lua_rawlen(L, 1));
return 1;
};
Expand Down Expand Up @@ -201,14 +201,14 @@ const opts = [
"isrunning"
].map((e) => to_luastring(e));
const luaB_collectgarbage = function(L) {
luaL_checkoption(L, 1, to_luastring("collect"), opts);
luaL_checkoption(L, 1, "collect", opts);
luaL_optinteger(L, 2, 0);
luaL_error(L, to_luastring("lua_gc not implemented"));
};

const luaB_type = function(L) {
let t = lua_type(L, 1);
luaL_argcheck(L, t !== LUA_TNONE, 1, to_luastring("value expected", true));
luaL_argcheck(L, t !== LUA_TNONE, 1, "value expected");
lua_pushstring(L, lua_typename(L, t));
return 1;
};
Expand Down Expand Up @@ -294,7 +294,7 @@ const luaB_tonumber = function(L) {
let base = luaL_checkinteger(L, 2);
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
let s = lua_tostring(L, 1);
luaL_argcheck(L, 2 <= base && base <= 36, 2, to_luastring("base out of range", true));
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
let n = b_str2int(s, base);
if (n !== null) {
lua_pushinteger(L, n);
Expand Down Expand Up @@ -338,7 +338,7 @@ const luaB_select = function(L) {
let i = luaL_checkinteger(L, 1);
if (i < 0) i = n + i;
else if (i > n) i = n;
luaL_argcheck(L, 1 <= i, 1, to_luastring("index out of range", true));
luaL_argcheck(L, 1 <= i, 1, "index out of range");
return n - i;
}
};
Expand Down Expand Up @@ -425,14 +425,14 @@ const generic_reader = function(L, ud) {

const luaB_load = function(L) {
let s = lua_tostring(L, 1);
let mode = luaL_optstring(L, 3, to_luastring("bt", true));
let mode = luaL_optstring(L, 3, "bt");
let env = !lua_isnone(L, 4) ? 4 : 0; /* 'env' index or 0 if no 'env' */
let status;
if (s !== null) { /* loading a string? */
let chunkname = luaL_optstring(L, 2, s);
status = luaL_loadbufferx(L, s, s.length, chunkname, mode);
} else { /* loading from a reader function */
let chunkname = luaL_optstring(L, 2, to_luastring("=(load)", true));
let chunkname = luaL_optstring(L, 2, "=(load)");
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
status = lua_load(L, generic_reader, null, chunkname, mode);
Expand Down
2 changes: 1 addition & 1 deletion src/lcorolib.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const { to_luastring } = require("./fengaricore.js");

const getco = function(L) {
let co = lua_tothread(L, 1);
luaL_argcheck(L, co, 1, to_luastring("thread expected", true));
luaL_argcheck(L, co, 1, "thread expected");
return co;
};

Expand Down
16 changes: 8 additions & 8 deletions src/ldblib.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const db_getmetatable = function(L) {

const db_setmetatable = function(L) {
const t = lua_type(L, 2);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, to_luastring("nil or table expected", true));
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table expected");
lua_settop(L, 2);
lua_setmetatable(L, 1);
return 1; /* return 1st argument */
Expand Down Expand Up @@ -194,7 +194,7 @@ const db_getinfo = function(L) {
let thread = getthread(L);
let arg = thread.arg;
let L1 = thread.thread;
let options = luaL_optstring(L, arg + 2, to_luastring("flnStu", true));
let options = luaL_optstring(L, arg + 2, "flnStu");
checkstack(L, L1, 3);
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
options = lua_pushfstring(L, to_luastring(">%s"), options); /* add '>' to 'options' */
Expand All @@ -208,7 +208,7 @@ const db_getinfo = function(L) {
}

if (!lua_getinfo(L1, options, ar))
luaL_argerror(L, arg + 2, to_luastring("invalid option", true));
luaL_argerror(L, arg + 2, "invalid option");
lua_newtable(L); /* table to collect results */
if (luastring_indexOf(options, 83 /* 'S'.charCodeAt(0) */) > -1) {
settabss(L, to_luastring("source", true), ar.source);
Expand Down Expand Up @@ -250,7 +250,7 @@ const db_getlocal = function(L) {
} else { /* stack-level argument */
let level = luaL_checkinteger(L, arg + 1);
if (!lua_getstack(L1, level, ar)) /* out of range? */
return luaL_argerror(L, arg+1, to_luastring("level out of range", true));
return luaL_argerror(L, arg+1, "level out of range");
checkstack(L, L1, 1);
let name = lua_getlocal(L1, ar, nvar);
if (name) {
Expand All @@ -274,7 +274,7 @@ const db_setlocal = function(L) {
let level = luaL_checkinteger(L, arg + 1);
let nvar = luaL_checkinteger(L, arg + 2);
if (!lua_getstack(L1, level, ar)) /* out of range? */
return luaL_argerror(L, arg + 1, to_luastring("level out of range", true));
return luaL_argerror(L, arg + 1, "level out of range");
luaL_checkany(L, arg + 3);
lua_settop(L, arg + 3);
checkstack(L, L1, 1);
Expand Down Expand Up @@ -316,7 +316,7 @@ const db_setupvalue = function(L) {
const checkupval = function(L, argf, argnup) {
let nup = luaL_checkinteger(L, argnup); /* upvalue index */
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
luaL_argcheck(L, (lua_getupvalue(L, argf, nup) !== null), argnup, to_luastring("invalid upvalue index", true));
luaL_argcheck(L, (lua_getupvalue(L, argf, nup) !== null), argnup, "invalid upvalue index");
return nup;
};

Expand All @@ -329,8 +329,8 @@ const db_upvalueid = function(L) {
const db_upvaluejoin = function(L) {
let n1 = checkupval(L, 1, 2);
let n2 = checkupval(L, 3, 4);
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, to_luastring("Lua function expected", true));
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, to_luastring("Lua function expected", true));
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
lua_upvaluejoin(L, 1, n1, 3, n2);
return 0;
};
Expand Down
12 changes: 6 additions & 6 deletions src/lmathlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ const math_random = function(L) {
up = luaL_checkinteger(L, 2);
break;
}
default: return luaL_error(L, to_luastring("wrong number of arguments", true));
default: return luaL_error(L, "wrong number of arguments");
}

/* random integer in the interval [low, up] */
luaL_argcheck(L, low <= up, 1, to_luastring("interval is empty", true));
luaL_argcheck(L, low <= up, 1, "interval is empty");
luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
to_luastring("interval too large", true));
"interval too large");

r *= (up - low) + 1;
lua_pushinteger(L, Math.floor(r) + low);
Expand Down Expand Up @@ -213,7 +213,7 @@ const math_rad = function(L) {
const math_min = function(L) {
let n = lua_gettop(L); /* number of arguments */
let imin = 1; /* index of current minimum value */
luaL_argcheck(L, n >= 1, 1, to_luastring("value expected", true));
luaL_argcheck(L, n >= 1, 1, "value expected");
for (let i = 2; i <= n; i++){
if (lua_compare(L, i, imin, LUA_OPLT))
imin = i;
Expand All @@ -225,7 +225,7 @@ const math_min = function(L) {
const math_max = function(L) {
let n = lua_gettop(L); /* number of arguments */
let imax = 1; /* index of current minimum value */
luaL_argcheck(L, n >= 1, 1, to_luastring("value expected", true));
luaL_argcheck(L, n >= 1, 1, "value expected");
for (let i = 2; i <= n; i++){
if (lua_compare(L, imax, i, LUA_OPLT))
imax = i;
Expand All @@ -252,7 +252,7 @@ const math_fmod = function(L) {
let d = lua_tointeger(L, 2);
/* no special case needed for -1 in javascript */
if (d === 0) {
luaL_argerror(L, 2, to_luastring("zero", true));
luaL_argerror(L, 2, "zero");
} else
lua_pushinteger(L, (lua_tointeger(L, 1) % d)|0);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/loadlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ const ll_searchpath = function(L) {
L,
luaL_checkstring(L, 1),
luaL_checkstring(L, 2),
luaL_optstring(L, 3, to_luastring(".")),
luaL_optstring(L, 4, to_luastring(LUA_DIRSEP))
luaL_optstring(L, 3, "."),
luaL_optstring(L, 4, LUA_DIRSEP)
);
if (f !== null) return 1;
else { /* error message is on top of the stack */
Expand Down
4 changes: 2 additions & 2 deletions src/loslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const checkoption = function(L, conv, i, buff) {


const os_date = function(L) {
let s = luaL_optlstring(L, 1, to_luastring("%c"));
let s = luaL_optlstring(L, 1, "%c");
let t = luaL_opt(L, l_checktime, 2, new Date().getTime() / 1000) * 1000;
let stm = new Date(t);
let utc = false;
Expand Down Expand Up @@ -188,7 +188,7 @@ const os_time = function(L) {

const l_checktime = function(L, arg) {
let t = luaL_checkinteger(L, arg);
// luaL_argcheck(L, t, arg, to_luastring("time out-of-bounds"));
// luaL_argcheck(L, t, arg, "time out-of-bounds");
return t;
};

Expand Down
28 changes: 14 additions & 14 deletions src/lstrlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ const str_format = function(L) {
if (form.length <= 2 || form[2] === 0) { /* no modifiers? */
luaL_addvalue(b); /* keep entire string */
} else {
luaL_argcheck(L, s.length === strlen(s), arg, to_luastring("string contains zeros", true));
luaL_argcheck(L, s.length === strlen(s), arg, "string contains zeros");
if (luastring_indexOf(form, 46 /* '.'.charCodeAt(0) */) < 0 && s.length >= 100) {
/* no precision and string is too long to be formatted */
luaL_addvalue(b); /* keep entire string */
Expand Down Expand Up @@ -601,15 +601,15 @@ const str_pack = function(L) {
let n = luaL_checkinteger(L, arg);
if (size < SZINT) { /* need overflow check? */
let lim = 1 << (size * 8) - 1;
luaL_argcheck(L, -lim <= n && n < lim, arg, to_luastring("integer overflow", true));
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
}
packint(b, n, h.islittle, size, n < 0);
break;
}
case Kuint: { /* unsigned integers */
let n = luaL_checkinteger(L, arg);
if (size < SZINT)
luaL_argcheck(L, (n>>>0) < (1 << (size * NB)), arg, to_luastring("unsigned overflow", true));
luaL_argcheck(L, (n>>>0) < (1 << (size * NB)), arg, "unsigned overflow");
packint(b, n>>>0, h.islittle, size, false);
break;
}
Expand All @@ -625,7 +625,7 @@ const str_pack = function(L) {
case Kchar: { /* fixed-size string */
let s = luaL_checkstring(L, arg);
let len = s.length;
luaL_argcheck(L, len <= size, arg, to_luastring("string longer than given size", true));
luaL_argcheck(L, len <= size, arg, "string longer than given size");
luaL_addlstring(b, s, len); /* add string */
while (len++ < size) /* pad extra space */
luaL_addchar(b, LUAL_PACKPADBYTE);
Expand All @@ -636,7 +636,7 @@ const str_pack = function(L) {
let len = s.length;
luaL_argcheck(L,
size >= 4 /* sizeof(size_t) */ || len < (1 << (size * NB)),
arg, to_luastring("string length does not fit in given size", true));
arg, "string length does not fit in given size");
packint(b, len, h.islittle, size, 0); /* pack length */
luaL_addlstring(b, s, len);
totalsize += len;
Expand All @@ -645,7 +645,7 @@ const str_pack = function(L) {
case Kzstr: { /* zero-terminated string */
let s = luaL_checkstring(L, arg);
let len = s.length;
luaL_argcheck(L, luastring_indexOf(s, 0) < 0, arg, to_luastring("strings contains zeros", true));
luaL_argcheck(L, luastring_indexOf(s, 0) < 0, arg, "strings contains zeros");
luaL_addlstring(b, s, len);
luaL_addchar(b, 0); /* add zero at the end */
totalsize += len + 1;
Expand Down Expand Up @@ -703,7 +703,7 @@ const str_rep = function(L) {
let s = luaL_checkstring(L, 1);
let l = s.length;
let n = luaL_checkinteger(L, 2);
let sep = luaL_optstring(L, 3, to_luastring(""));
let sep = luaL_optstring(L, 3, "");
let lsep = sep.length;
if (n <= 0) lua_pushliteral(L, "");
else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
Expand Down Expand Up @@ -737,10 +737,10 @@ const str_byte = function(L) {
if (pose > l) pose = l;
if (posi > pose) return 0; /* empty interval; return no values */
if (pose - posi >= Number.MAX_SAFE_INTEGER) /* arithmetic overflow? */
return luaL_error(L, to_luastring("string slice too long", true));
return luaL_error(L, "string slice too long");

let n = (pose - posi) + 1;
luaL_checkstack(L, n, to_luastring("string slice too long", true));
luaL_checkstack(L, n, "string slice too long");
for (let i = 0; i < n; i++)
lua_pushinteger(L, s[posi + i - 1]);
return n;
Expand All @@ -759,12 +759,12 @@ const str_packsize = function(L) {
let size = details.size;
let ntoalign = details.ntoalign;
size += ntoalign; /* total space used by option */
luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, to_luastring("format result too large", true));
luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, "format result too large");
totalsize += size;
switch (opt) {
case Kstring: /* strings with length count */
case Kzstr: /* zero-terminated string */
luaL_argerror(L, 1, to_luastring("variable-length format", true));
luaL_argerror(L, 1, "variable-length format");
/* call never return, but to avoid warnings: *//* fall through */
default: break;
}
Expand Down Expand Up @@ -824,7 +824,7 @@ const str_unpack = function(L) {
let ld = data.length;
let pos = posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
let n = 0; /* number of results */
luaL_argcheck(L, pos <= ld && pos >= 0, 3, to_luastring("initial position out of string", true));
luaL_argcheck(L, pos <= ld && pos >= 0, 3, "initial position out of string");
while (fmt.off < fmt.s.length) {
let details = getdetails(h, pos, fmt);
let opt = details.opt;
Expand Down Expand Up @@ -854,7 +854,7 @@ const str_unpack = function(L) {
}
case Kstring: {
let len = unpackint(L, data.subarray(pos), h.islittle, size, 0);
luaL_argcheck(L, pos + len + size <= ld, 2, to_luastring("data string too short", true));
luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
lua_pushstring(L, data.subarray(pos + size, pos + size + len));
pos += len; /* skip string */
break;
Expand Down Expand Up @@ -1421,7 +1421,7 @@ const str_gsub = function(L) {
let ms = new MatchState(L);
let b = new luaL_Buffer();
luaL_argcheck(L, tr === LUA_TNUMBER || tr === LUA_TSTRING || tr === LUA_TFUNCTION || tr === LUA_TTABLE, 3,
to_luastring("string/function/table expected", true));
"string/function/table expected");
luaL_buffinit(L, b);
if (anchor) {
p = p.subarray(1); lp--; /* skip anchor character */
Expand Down
Loading

0 comments on commit fb3b9c3

Please sign in to comment.