aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--src/ldblib.js25
2 files changed, 27 insertions, 2 deletions
diff --git a/README.md b/README.md
index 2e53fc2..46a31ae 100644
--- a/README.md
+++ b/README.md
@@ -90,14 +90,14 @@
- [x] debug.getlocal
- [x] debug.getmetatable
- [x] debug.getregistry
+ - [x] debug.getupvalue
- [x] debug.setmetatable
+ - [x] debug.setupvalue
- [x] debug.traceback
- [ ] debug.gethook
- - [ ] debug.getupvalue
- [ ] debug.getuservalue
- [ ] debug.sethook
- [ ] debug.setlocal
- - [ ] debug.setupvalue
- [ ] debug.setuservalue
- [ ] debug.upvalueid
- [ ] debug.upvaluejoin
diff --git a/src/ldblib.js b/src/ldblib.js
index 2edc507..d3e3c7c 100644
--- a/src/ldblib.js
+++ b/src/ldblib.js
@@ -178,6 +178,29 @@ const db_getlocal = function(L) {
};
/*
+** get (if 'get' is true) or set an upvalue from a closure
+*/
+const auxupvalue = function(L, get) {
+ let n = lauxlib.luaL_checkinteger(L, 2); /* upvalue index */
+ lauxlib.luaL_checktype(L, 1, lua.CT.LUA_TFUNCTION); /* closure */
+ let name = get ? lapi.lua_getupvalue(L, 1, n) : lapi.lua_setupvalue(L, 1, n);
+ if (name === null) return 0;
+ lapi.lua_pushstring(L, name);
+ lapi.lua_insert(L, -(get+1)); /* no-op if get is false */
+ return get + 1;
+};
+
+
+const db_getupvalue = function(L) {
+ return auxupvalue(L, 1);
+};
+
+const db_setupvalue = function(L) {
+ lauxlib.luaL_checkany(L, 3);
+ return auxupvalue(L, 0);
+};
+
+/*
** Check whether a given upvalue from a given closure exists and
** returns its index
*/
@@ -214,7 +237,9 @@ const dblib = {
"getlocal": db_getlocal,
"getmetatable": db_getmetatable,
"getregistry": db_getregistry,
+ "getupvalue": db_getupvalue,
"setmetatable": db_setmetatable,
+ "setupvalue": db_setupvalue,
"traceback": db_traceback,
"upvalueid": db_upvalueid
};