From c4b23126f0cb803ec944c5d7877bbf669f582374 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 21 Mar 2017 14:05:14 +0100 Subject: Userdata are ArrayBuffers, new lightuserdata subtype for js objects --- src/lapi.js | 190 ++++++++++++++++++++++++++++++--------------------------- src/lobject.js | 8 +++ src/lstrlib.js | 3 +- src/lua.js | 3 + 4 files changed, 114 insertions(+), 90 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 5872545..991e982 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -281,9 +281,13 @@ const lua_pushboolean = function(L, b) { }; const lua_pushlightuserdata = function(L, p) { - assert(typeof p === "object"); + L.stack[L.top++] = new TValue(CT.LUA_TLIGHTUSERDATA_PTR, p); - L.stack[L.top++] = new TValue(CT.LUA_TLIGHTUSERDATA, p); + assert(L.top <= L.ci.top, "stack overflow"); +}; + +const lua_pushobject = function(L, p) { + L.stack[L.top++] = new TValue(CT.LUA_TLIGHTUSERDATA_OBJ, p); assert(L.top <= L.ci.top, "stack overflow"); }; @@ -428,8 +432,8 @@ const lua_createtable = function(L, narray, nrec) { assert(L.top <= L.ci.top, "stack overflow"); }; -const lua_newuserdata = function(L, u) { - L.stack[L.top++] = new lobject.TValue(CT.LUA_TUSERDATA, u); +const lua_newuserdata = function(L, size) { + L.stack[L.top++] = new lobject.TValue(CT.LUA_TUSERDATA, new ArrayBuffer(size)); assert(L.top <= L.ci.top, "stack overflow"); @@ -698,6 +702,11 @@ const lua_isstring = function(L, idx) { return o.ttisstring() || o.ttisnumber(); }; +const lua_isuserdata = function(L, idx) { + let o = index2addr(L, idx); + return o.ttisfulluserdata(o) || o.ttislightuserdata(); +}; + const lua_rawequal = function(L, index1, index2) { let o1 = index2addr(L, index1); let o2 = index2addr(L, index2); @@ -863,88 +872,91 @@ const lua_getextraspace = function () { return null; }; -module.exports.index2addr = index2addr; -module.exports.index2addr_ = index2addr_; -module.exports.lua_absindex = lua_absindex; -module.exports.lua_atpanic = lua_atpanic; -module.exports.lua_call = lua_call; -module.exports.lua_callk = lua_callk; -module.exports.lua_checkstack = lua_checkstack; -module.exports.lua_compare = lua_compare; -module.exports.lua_compare_ = lua_compare_; -module.exports.lua_concat = lua_concat; -module.exports.lua_copy = lua_copy; -module.exports.lua_createtable = lua_createtable; -module.exports.lua_dump = lua_dump; -module.exports.lua_error = lua_error; -module.exports.lua_gc = lua_gc; -module.exports.lua_getallocf = lua_getallocf; -module.exports.lua_getextraspace = lua_getextraspace; -module.exports.lua_getfield = lua_getfield; -module.exports.lua_getglobal = lua_getglobal; -module.exports.lua_geti = lua_geti; -module.exports.lua_getmetatable = lua_getmetatable; -module.exports.lua_gettable = lua_gettable; -module.exports.lua_gettop = lua_gettop; -module.exports.lua_insert = lua_insert; -module.exports.lua_isinteger = lua_isinteger; -module.exports.lua_isnil = lua_isnil; -module.exports.lua_isnone = lua_isnone; -module.exports.lua_isnoneornil = lua_isnoneornil; -module.exports.lua_isnumber = lua_isnumber; -module.exports.lua_isstring = lua_isstring; -module.exports.lua_istable = lua_istable; -module.exports.lua_len = lua_len; -module.exports.lua_load = lua_load; -module.exports.lua_newtable = lua_newtable; -module.exports.lua_newuserdata = lua_newuserdata; -module.exports.lua_next = lua_next; -module.exports.lua_pcall = lua_pcall; -module.exports.lua_pcallk = lua_pcallk; -module.exports.lua_pop = lua_pop; -module.exports.lua_pushboolean = lua_pushboolean; -module.exports.lua_pushcclosure = lua_pushcclosure; -module.exports.lua_pushcfunction = lua_pushcfunction; -module.exports.lua_pushglobaltable = lua_pushglobaltable; -module.exports.lua_pushinteger = lua_pushinteger; -module.exports.lua_pushjsclosure = lua_pushjsclosure; -module.exports.lua_pushjsfunction = lua_pushjsfunction; -module.exports.lua_pushliteral = lua_pushliteral; -module.exports.lua_pushlstring = lua_pushlstring; -module.exports.lua_pushnil = lua_pushnil; -module.exports.lua_pushnumber = lua_pushnumber; -module.exports.lua_pushstring = lua_pushstring; -module.exports.lua_pushthread = lua_pushthread; -module.exports.lua_pushtvalue = lua_pushtvalue; -module.exports.lua_pushvalue = lua_pushvalue; -module.exports.lua_rawequal = lua_rawequal; -module.exports.lua_rawget = lua_rawget; -module.exports.lua_rawgeti = lua_rawgeti; -module.exports.lua_rawlen = lua_rawlen; -module.exports.lua_rawset = lua_rawset; -module.exports.lua_remove = lua_remove; -module.exports.lua_replace = lua_replace; -module.exports.lua_rotate = lua_rotate; -module.exports.lua_setfield = lua_setfield; -module.exports.lua_setglobal = lua_setglobal; -module.exports.lua_seti = lua_seti; -module.exports.lua_setmetatable = lua_setmetatable; -module.exports.lua_settable = lua_settable; -module.exports.lua_settop = lua_settop; -module.exports.lua_setupvalue = lua_setupvalue; -module.exports.lua_status = lua_status; -module.exports.lua_stringtonumber = lua_stringtonumber; -module.exports.lua_toboolean = lua_toboolean; -module.exports.lua_todataview = lua_todataview; -module.exports.lua_tointeger = lua_tointeger; -module.exports.lua_tointegerx = lua_tointegerx; -module.exports.lua_tolstring = lua_tolstring; -module.exports.lua_tonumber = lua_tonumber; -module.exports.lua_topointer = lua_topointer; -module.exports.lua_tostring = lua_tostring; -module.exports.lua_tothread = lua_tothread; -module.exports.lua_touserdata = lua_touserdata; -module.exports.lua_type = lua_type; -module.exports.lua_typename = lua_typename; -module.exports.lua_version = lua_version; -module.exports.lua_xmove = lua_xmove; \ No newline at end of file +module.exports.index2addr = index2addr; +module.exports.index2addr_ = index2addr_; +module.exports.lua_absindex = lua_absindex; +module.exports.lua_atpanic = lua_atpanic; +module.exports.lua_call = lua_call; +module.exports.lua_callk = lua_callk; +module.exports.lua_checkstack = lua_checkstack; +module.exports.lua_compare = lua_compare; +module.exports.lua_compare_ = lua_compare_; +module.exports.lua_concat = lua_concat; +module.exports.lua_copy = lua_copy; +module.exports.lua_createtable = lua_createtable; +module.exports.lua_dump = lua_dump; +module.exports.lua_error = lua_error; +module.exports.lua_gc = lua_gc; +module.exports.lua_getallocf = lua_getallocf; +module.exports.lua_getextraspace = lua_getextraspace; +module.exports.lua_getfield = lua_getfield; +module.exports.lua_getglobal = lua_getglobal; +module.exports.lua_geti = lua_geti; +module.exports.lua_getmetatable = lua_getmetatable; +module.exports.lua_gettable = lua_gettable; +module.exports.lua_gettop = lua_gettop; +module.exports.lua_insert = lua_insert; +module.exports.lua_isinteger = lua_isinteger; +module.exports.lua_isnil = lua_isnil; +module.exports.lua_isnone = lua_isnone; +module.exports.lua_isnoneornil = lua_isnoneornil; +module.exports.lua_isnumber = lua_isnumber; +module.exports.lua_isstring = lua_isstring; +module.exports.lua_istable = lua_istable; +module.exports.lua_isuserdata = lua_isuserdata; +module.exports.lua_len = lua_len; +module.exports.lua_load = lua_load; +module.exports.lua_newtable = lua_newtable; +module.exports.lua_newuserdata = lua_newuserdata; +module.exports.lua_next = lua_next; +module.exports.lua_pcall = lua_pcall; +module.exports.lua_pcallk = lua_pcallk; +module.exports.lua_pop = lua_pop; +module.exports.lua_pushboolean = lua_pushboolean; +module.exports.lua_pushcclosure = lua_pushcclosure; +module.exports.lua_pushcfunction = lua_pushcfunction; +module.exports.lua_pushglobaltable = lua_pushglobaltable; +module.exports.lua_pushinteger = lua_pushinteger; +module.exports.lua_pushjsclosure = lua_pushjsclosure; +module.exports.lua_pushjsfunction = lua_pushjsfunction; +module.exports.lua_pushlightuserdata = lua_pushlightuserdata; +module.exports.lua_pushliteral = lua_pushliteral; +module.exports.lua_pushlstring = lua_pushlstring; +module.exports.lua_pushnil = lua_pushnil; +module.exports.lua_pushnumber = lua_pushnumber; +module.exports.lua_pushobject = lua_pushobject; +module.exports.lua_pushstring = lua_pushstring; +module.exports.lua_pushthread = lua_pushthread; +module.exports.lua_pushtvalue = lua_pushtvalue; +module.exports.lua_pushvalue = lua_pushvalue; +module.exports.lua_rawequal = lua_rawequal; +module.exports.lua_rawget = lua_rawget; +module.exports.lua_rawgeti = lua_rawgeti; +module.exports.lua_rawlen = lua_rawlen; +module.exports.lua_rawset = lua_rawset; +module.exports.lua_remove = lua_remove; +module.exports.lua_replace = lua_replace; +module.exports.lua_rotate = lua_rotate; +module.exports.lua_setfield = lua_setfield; +module.exports.lua_setglobal = lua_setglobal; +module.exports.lua_seti = lua_seti; +module.exports.lua_setmetatable = lua_setmetatable; +module.exports.lua_settable = lua_settable; +module.exports.lua_settop = lua_settop; +module.exports.lua_setupvalue = lua_setupvalue; +module.exports.lua_status = lua_status; +module.exports.lua_stringtonumber = lua_stringtonumber; +module.exports.lua_toboolean = lua_toboolean; +module.exports.lua_todataview = lua_todataview; +module.exports.lua_tointeger = lua_tointeger; +module.exports.lua_tointegerx = lua_tointegerx; +module.exports.lua_tolstring = lua_tolstring; +module.exports.lua_tonumber = lua_tonumber; +module.exports.lua_topointer = lua_topointer; +module.exports.lua_tostring = lua_tostring; +module.exports.lua_tothread = lua_tothread; +module.exports.lua_touserdata = lua_touserdata; +module.exports.lua_type = lua_type; +module.exports.lua_typename = lua_typename; +module.exports.lua_version = lua_version; +module.exports.lua_xmove = lua_xmove; \ No newline at end of file diff --git a/src/lobject.js b/src/lobject.js index e1c3e97..dfe6337 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -57,6 +57,14 @@ class TValue { ttislightuserdata() { return this.checktag(CT.LUA_TLIGHTUSERDATA); } + + ttisobject() { + return this.checktag(CT.LUA_TLIGHTUSERDATA_OBJ); + } + + ttisptr() { + return this.checktag(CT.LUA_TLIGHTUSERDATA_PTR); + } ttisstring() { return this.checktype(CT.LUA_TSTRING); diff --git a/src/lstrlib.js b/src/lstrlib.js index cd9278d..95fb375 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -1252,7 +1252,8 @@ const str_gmatch = function(L) { let ls = s.length; let lp = p.length; lapi.lua_settop(L, 2); /* keep them on closure to avoid being collected */ - let gm = lapi.lua_newuserdata(L, new GMatchState()); + let gm = new GMatchState(); + lapi.lua_pushobject(L, gm); prepstate(gm.ms, L, s, ls, p, lp); gm.src = 0; gm.p = 0; diff --git a/src/lua.js b/src/lua.js index be6cc1e..d7bfa01 100644 --- a/src/lua.js +++ b/src/lua.js @@ -60,6 +60,9 @@ const constant_types = { constant_types.LUA_TSHRSTR = constant_types.LUA_TSTRING | (0 << 4); /* short strings */ constant_types.LUA_TLNGSTR = constant_types.LUA_TSTRING | (1 << 4); /* long strings */ +constant_types.LUA_TLIGHTUSERDATA_PTR = constant_types.LUA_TLIGHTUSERDATA | (0 << 4); /* short strings */ +constant_types.LUA_TLIGHTUSERDATA_OBJ = constant_types.LUA_TLIGHTUSERDATA | (1 << 4); /* long strings */ + constant_types.LUA_TNUMFLT = constant_types.LUA_TNUMBER | (0 << 4); /* float numbers */ constant_types.LUA_TNUMINT = constant_types.LUA_TNUMBER | (1 << 4); /* integer numbers */ -- cgit v1.2.3-70-g09d2