From 011c821b3a94c0d2fa8892696178c76aed4c2349 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 11 May 2017 15:41:21 +1000 Subject: src/lapi.js: Add extensions 'lua_toproxy' and 'lua_isproxy' --- src/lapi.js | 33 +++++++++++++++++++++++++++++++++ src/lua.js | 2 ++ 2 files changed, 35 insertions(+) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index e033f69..a5cfb8d 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -723,6 +723,37 @@ const lua_topointer = function(L, idx) { } }; + +/* A proxy is a function that the same lua value to the given lua state. */ + +/* Having a weakmap of created proxies was only way I could think of to provide an 'isproxy' function */ +const seen = new WeakMap(); + +/* is the passed object a proxy? is it from the given state? (if passed) */ +const lua_isproxy = function(p, L) { + let G = seen.get(p); + if (!G) + return false; + return (L === null) || (L.l_G === G); +}; + +/* Use 'create_proxy' helper function so that 'L' is not in scope */ +const create_proxy = function(G, type, value) { + let proxy = function(L) { + assert(L instanceof lstate.lua_State && G === L.l_G, "must be from same global state"); + L.stack[L.top++] = new TValue(type, value); + }; + seen.set(proxy, G); + return proxy; +}; + +const lua_toproxy = function(L, idx) { + let tv = index2addr(L, idx); + /* pass broken down tv incase it is an upvalue index */ + return create_proxy(L.l_G, tv.type, tv.value); +}; + + const lua_compare = function(L, index1, index2, op) { let o1 = index2addr(L, index1); let o2 = index2addr(L, index2); @@ -1068,6 +1099,7 @@ 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_isproxy = lua_isproxy; module.exports.lua_isstring = lua_isstring; module.exports.lua_istable = lua_istable; module.exports.lua_isthread = lua_isthread; @@ -1127,6 +1159,7 @@ module.exports.lua_toljsstring = lua_toljsstring; module.exports.lua_tolstring = lua_tolstring; module.exports.lua_tonumber = lua_tonumber; module.exports.lua_topointer = lua_topointer; +module.exports.lua_toproxy = lua_toproxy; module.exports.lua_tostring = lua_tostring; module.exports.lua_tothread = lua_tothread; module.exports.lua_touserdata = lua_touserdata; diff --git a/src/lua.js b/src/lua.js index 793b39a..ae93e58 100644 --- a/src/lua.js +++ b/src/lua.js @@ -130,6 +130,7 @@ module.exports.lua_isnil = lapi.lua_isnil; module.exports.lua_isnone = lapi.lua_isnone; module.exports.lua_isnoneornil = lapi.lua_isnoneornil; module.exports.lua_isnumber = lapi.lua_isnumber; +module.exports.lua_isproxy = lapi.lua_isproxy; module.exports.lua_isstring = lapi.lua_isstring; module.exports.lua_istable = lapi.lua_istable; module.exports.lua_isthread = lapi.lua_isthread; @@ -195,6 +196,7 @@ module.exports.lua_toljsstring = lapi.lua_toljsstring; module.exports.lua_tolstring = lapi.lua_tolstring; module.exports.lua_tonumber = lapi.lua_tonumber; module.exports.lua_topointer = lapi.lua_topointer; +module.exports.lua_toproxy = lapi.lua_toproxy; module.exports.lua_tostring = lapi.lua_tostring; module.exports.lua_tothread = lapi.lua_tothread; module.exports.lua_touserdata = lapi.lua_touserdata; -- cgit v1.2.3-70-g09d2