Skip to content

feature: implemented the luaL_checkcdataptr C API for converting LuaJIT cdata pointer object on Lua stack to pointer. #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/lj_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------- */

Expand Down Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


/*
** ===============================================================
Expand Down