diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-02-18 16:07:47 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-20 08:56:33 +0100 |
commit | b62bcdfa67d6b0359bf45930ab392953d69eb399 (patch) | |
tree | ca4cc55bce206e8924f4099e1b19b6eab30234ed /src/lapi.js | |
parent | 6316cab500cdaa944c6d2ef886138e7e9da0cc7c (diff) | |
download | fengari-b62bcdfa67d6b0359bf45930ab392953d69eb399.tar.gz fengari-b62bcdfa67d6b0359bf45930ab392953d69eb399.tar.bz2 fengari-b62bcdfa67d6b0359bf45930ab392953d69eb399.zip |
setmetatable, getmetatable
Diffstat (limited to 'src/lapi.js')
-rw-r--r-- | src/lapi.js | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/lapi.js b/src/lapi.js index ece02e0..e1e548d 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -260,6 +260,33 @@ const lua_setglobal = function(L, name) { auxsetstr(L, L.l_G.l_registry.value.array[lua.LUA_RIDX_GLOBALS - 1], name); }; +const lua_setmetatable = function(L, objindex) { + assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack"); + let mt; + let obj = index2addr(L, objindex); + if (L.stack[L.top - 1].ttisnil()) + mt = null; + else { + assert(L.stack[L.top - 1].ttistable(), "table expected"); + mt = L.stack[L.top - 1]; + } + + switch (obj.ttnov()) { + case CT.LUA_TUSERDATA: + case CT.LUA_TTABLE: { + obj.metatable = mt; + break; + } + default: { + L.l_G.mt[obj.ttnov()] = mt; + break; + } + } + + L.top--; + return true; +}; + const lua_settable = function(L, idx) { assert(2 < L.top - L.ci.funcOff, "not enough elements in the stack"); @@ -562,4 +589,6 @@ module.exports.lua_pushglobaltable = lua_pushglobaltable; module.exports.lua_setfield = lua_setfield; module.exports.lua_getfield = lua_getfield; module.exports.lua_getglobal = lua_getglobal; -module.exports.lua_getmetatable = lua_getmetatable;
\ No newline at end of file +module.exports.lua_getmetatable = lua_getmetatable; +module.exports.lua_setmetatable = lua_setmetatable; +module.exports.lua_settop = lua_settop;
\ No newline at end of file |