From d11b7632b43f9c38a86682d808956e3159885110 Mon Sep 17 00:00:00 2001 From: Datong Sun Date: Fri, 13 Sep 2019 15:04:57 -0700 Subject: [PATCH 1/2] feature: implemented the luaL_checkcdataptr C API for converting LuaJIT cdata pointer object on Lua stack to pointer. --- src/lj_api.c | 19 +++++++++++++++++++ src/lua.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/lj_api.c b/src/lj_api.c index 9c4864d7f..3dedab1e5 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -25,6 +25,9 @@ #include "lj_vm.h" #include "lj_strscan.h" #include "lj_strfmt.h" +#include "lj_ctype.h" +#include "lj_lib.h" +#include "lj_cdata.h" /* -- Common helper functions --------------------------------------------- */ @@ -1299,3 +1302,19 @@ LUA_API void *lua_getexdata(lua_State *L) { return L->exdata; } + +LUA_API void *luaL_checkcdataptr(lua_State *L, int idx) +{ + if (idx < 0) idx = lua_gettop(L) + idx + 1; + + GCcdata *cd = lj_lib_checkcdata(L, idx); + + if (cd->ctypeid != CTID_P_VOID && + cd->ctypeid != CTID_P_CVOID && + cd->ctypeid != CTID_P_CCHAR) + { + lj_err_argtype(L, idx, "cdata pointer"); + } + + return cdata_getptr(cdataptr(cd), CTSIZE_PTR); +} diff --git a/src/lua.h b/src/lua.h index 9dcafd690..535d3fe4f 100644 --- a/src/lua.h +++ b/src/lua.h @@ -248,6 +248,8 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); LUA_API void lua_setexdata(lua_State *L, void *exdata); LUA_API void *lua_getexdata(lua_State *L); +LUA_API void *luaL_checkcdataptr(lua_State *L, int idx); + /* ** =============================================================== From b88268f3e05ceac37b87e5a657c97a85d5ea163d Mon Sep 17 00:00:00 2001 From: Datong Sun Date: Fri, 13 Sep 2019 15:20:08 -0700 Subject: [PATCH 2/2] readme: added the new luaL_checkcdataptr C API documentation. --- README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 597e876e0..97bf4eca2 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,15 @@ Table of Contents * [Name](#name) * [Description](#description) - * [New API](#new-api) + * [New Lua API](#new-lua-api) * [table.isempty](#tableisempty) * [table.isarray](#tableisarray) * [table.nkeys](#tablenkeys) * [thread.exdata](#threadexdata) * [table.clone](#tableclone) * [jit.prngstate](#jitprngstate) + * [New C API](#new-c-api) + * [luaL\_checkcdataptr](#lual_checkcdataptr) * [New JIT parameter defaults](#new-jit-parameter-defaults) * [String table hashing optimization](#string-table-hashing-optimization) * [New command-line option `-bL`](#new-command-line-option--bl) @@ -31,7 +33,7 @@ since we still synchronize any upstream changes all the time. We introduce our own changes which will never merge or haven't yet merged into the upstream LuaJIT (https://github.com/LuaJIT/LuaJIT), which are as follows -## New API +## New Lua API ### table.isempty @@ -161,6 +163,22 @@ the current PRNG state number used in the JIT compiler. [Back to TOC](#table-of-contents) +## New C API + +### luaL\_checkcdataptr + +```C +void *luaL_checkcdataptr(lua_State *L, int index); +``` + +This API allows unboxing of a cdata pointer from the Lua stack at `index`. +Pseudo indexes such as `-1` are also supported. + +If the object at given `index` is not a `cdata` or is not a pointer type +`cdata`, an error will be thrown. + +[Back to TOC](#table-of-contents) + ## New JIT parameter defaults We use more appressive JIT compiler parameters as the default to help