aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2010-11-26 13:28:46 +0100
committerMike Pall <mike>2010-11-26 13:28:46 +0100
commit2fd129295555300ac01b17679a6ea4edee2173d2 (patch)
tree15ab739536315c9616560b57ab3850751b005467 /src
parent6290d6f5d08cc73ebd73d4363996f4f258ac7dcb (diff)
downloadluajit-2fd129295555300ac01b17679a6ea4edee2173d2.tar.gz
luajit-2fd129295555300ac01b17679a6ea4edee2173d2.tar.bz2
luajit-2fd129295555300ac01b17679a6ea4edee2173d2.zip
FFI: Add cdata object type.
Diffstat (limited to 'src')
-rw-r--r--src/lib_base.c3
-rw-r--r--src/lj_api.c6
-rw-r--r--src/lj_gc.c1
-rw-r--r--src/lj_ir.h3
-rw-r--r--src/lj_obj.c4
-rw-r--r--src/lj_obj.h38
6 files changed, 45 insertions, 10 deletions
diff --git a/src/lib_base.c b/src/lib_base.c
index c7ebafb6..af738008 100644
--- a/src/lib_base.c
+++ b/src/lib_base.c
@@ -55,8 +55,9 @@ LJLIB_PUSH("thread")
55LJLIB_PUSH("proto") 55LJLIB_PUSH("proto")
56LJLIB_PUSH("function") 56LJLIB_PUSH("function")
57LJLIB_PUSH("trace") 57LJLIB_PUSH("trace")
58LJLIB_PUSH("cdata")
58LJLIB_PUSH("table") 59LJLIB_PUSH("table")
59LJLIB_PUSH(top-8) /* userdata */ 60LJLIB_PUSH(top-9) /* userdata */
60LJLIB_PUSH("number") 61LJLIB_PUSH("number")
61LJLIB_ASM_(type) LJLIB_REC(.) 62LJLIB_ASM_(type) LJLIB_REC(.)
62/* Recycle the lj_lib_checkany(L, 1) from assert. */ 63/* Recycle the lj_lib_checkany(L, 1) from assert. */
diff --git a/src/lj_api.c b/src/lj_api.c
index 827ab42f..b93df344 100644
--- a/src/lj_api.c
+++ b/src/lj_api.c
@@ -197,9 +197,9 @@ LUA_API int lua_type(lua_State *L, int idx)
197 } else { /* Magic internal/external tag conversion. ORDER LJ_T */ 197 } else { /* Magic internal/external tag conversion. ORDER LJ_T */
198 uint32_t t = ~itype(o); 198 uint32_t t = ~itype(o);
199#if LJ_64 199#if LJ_64
200 int tt = (int)((U64x(7506,98042110) >> 4*t) & 15u); 200 int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);
201#else 201#else
202 int tt = (int)(((t < 8 ? 0x98042110 : 0x7506) >> 4*(t&7)) & 15u); 202 int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);
203#endif 203#endif
204 lua_assert(tt != LUA_TNIL || tvisnil(o)); 204 lua_assert(tt != LUA_TNIL || tvisnil(o));
205 return tt; 205 return tt;
@@ -525,6 +525,8 @@ LUA_API const void *lua_topointer(lua_State *L, int idx)
525 return uddata(udataV(o)); 525 return uddata(udataV(o));
526 else if (tvislightud(o)) 526 else if (tvislightud(o))
527 return lightudV(o); 527 return lightudV(o);
528 else if (tviscdata(o))
529 return cdataptr(cdataV(o));
528 else if (tvisgcv(o)) 530 else if (tvisgcv(o))
529 return gcV(o); 531 return gcV(o);
530 else 532 else
diff --git a/src/lj_gc.c b/src/lj_gc.c
index 75871a2d..6d47722b 100644
--- a/src/lj_gc.c
+++ b/src/lj_gc.c
@@ -373,6 +373,7 @@ static const GCFreeFunc gc_freefunc[] = {
373#else 373#else
374 (GCFreeFunc)0, 374 (GCFreeFunc)0,
375#endif 375#endif
376 (GCFreeFunc)0, /* Placeholder for C data. */
376 (GCFreeFunc)lj_tab_free, 377 (GCFreeFunc)lj_tab_free,
377 (GCFreeFunc)lj_udata_free 378 (GCFreeFunc)lj_udata_free
378}; 379};
diff --git a/src/lj_ir.h b/src/lj_ir.h
index 8ad5d004..d24d2980 100644
--- a/src/lj_ir.h
+++ b/src/lj_ir.h
@@ -316,6 +316,7 @@ typedef enum {
316 IRT_PROTO, 316 IRT_PROTO,
317 IRT_FUNC, 317 IRT_FUNC,
318 IRT_9, /* Unused (map of LJ_TTRACE). */ 318 IRT_9, /* Unused (map of LJ_TTRACE). */
319 IRT_CDATA,
319 IRT_TAB, 320 IRT_TAB,
320 IRT_UDATA, 321 IRT_UDATA,
321 /* ... until here. */ 322 /* ... until here. */
@@ -329,7 +330,7 @@ typedef enum {
329 IRT_U8, 330 IRT_U8,
330 IRT_I16, 331 IRT_I16,
331 IRT_U16, 332 IRT_U16,
332 /* There is room for 14 more types. */ 333 /* There is room for 13 more types. */
333 334
334 /* Additional flags. */ 335 /* Additional flags. */
335 IRT_MARK = 0x20, /* Marker for misc. purposes. */ 336 IRT_MARK = 0x20, /* Marker for misc. purposes. */
diff --git a/src/lj_obj.c b/src/lj_obj.c
index 3d7e9fcb..5dbed7a8 100644
--- a/src/lj_obj.c
+++ b/src/lj_obj.c
@@ -11,12 +11,12 @@
11/* Object type names. */ 11/* Object type names. */
12LJ_DATADEF const char *const lj_obj_typename[] = { /* ORDER LUA_T */ 12LJ_DATADEF const char *const lj_obj_typename[] = { /* ORDER LUA_T */
13 "no value", "nil", "boolean", "userdata", "number", "string", 13 "no value", "nil", "boolean", "userdata", "number", "string",
14 "table", "function", "userdata", "thread", "proto" 14 "table", "function", "userdata", "thread", "proto", "cdata"
15}; 15};
16 16
17LJ_DATADEF const char *const lj_obj_itypename[] = { /* ORDER LJ_T */ 17LJ_DATADEF const char *const lj_obj_itypename[] = { /* ORDER LJ_T */
18 "nil", "boolean", "boolean", "userdata", "string", "upval", "thread", 18 "nil", "boolean", "boolean", "userdata", "string", "upval", "thread",
19 "proto", "function", "trace", "table", "userdata", "number" 19 "proto", "function", "trace", "cdata", "table", "userdata", "number"
20}; 20};
21 21
22/* Compare two objects without calling metamethods. */ 22/* Compare two objects without calling metamethods. */
diff --git a/src/lj_obj.h b/src/lj_obj.h
index cbb25675..83e30b6e 100644
--- a/src/lj_obj.h
+++ b/src/lj_obj.h
@@ -165,6 +165,7 @@ typedef const TValue cTValue;
165/* More external and GCobj tags for internal objects. */ 165/* More external and GCobj tags for internal objects. */
166#define LAST_TT LUA_TTHREAD 166#define LAST_TT LUA_TTHREAD
167#define LUA_TPROTO (LAST_TT+1) 167#define LUA_TPROTO (LAST_TT+1)
168#define LUA_TCDATA (LAST_TT+2)
168 169
169/* Internal object tags. 170/* Internal object tags.
170** 171**
@@ -196,10 +197,11 @@ typedef const TValue cTValue;
196#define LJ_TPROTO (~7u) 197#define LJ_TPROTO (~7u)
197#define LJ_TFUNC (~8u) 198#define LJ_TFUNC (~8u)
198#define LJ_TTRACE (~9u) 199#define LJ_TTRACE (~9u)
199#define LJ_TTAB (~10u) 200#define LJ_TCDATA (~10u)
200#define LJ_TUDATA (~11u) 201#define LJ_TTAB (~11u)
202#define LJ_TUDATA (~12u)
201/* This is just the canonical number type used in some places. */ 203/* This is just the canonical number type used in some places. */
202#define LJ_TNUMX (~12u) 204#define LJ_TNUMX (~13u)
203 205
204#if LJ_64 206#if LJ_64
205#define LJ_TISNUM 0xfffeffffu 207#define LJ_TISNUM 0xfffeffffu
@@ -251,6 +253,28 @@ enum {
251#define uddata(u) ((void *)((u)+1)) 253#define uddata(u) ((void *)((u)+1))
252#define sizeudata(u) (sizeof(struct GCudata)+(u)->len) 254#define sizeudata(u) (sizeof(struct GCudata)+(u)->len)
253 255
256/* -- C data object ------------------------------------------------------- */
257
258/* C data object. Payload follows. */
259typedef struct GCcdata {
260 GCHeader;
261 uint16_t typeid; /* C type ID. */
262} GCcdata;
263
264/* Prepended to variable-sized or realigned C data objects. */
265typedef struct GCcdataVar {
266 uint16_t offset; /* Offset to allocated memory (relative to GCcdata). */
267 uint16_t extra; /* Extra space allocated (incl. GCcdata + GCcdatav). */
268 MSize len; /* Size of payload. */
269} GCcdataVar;
270
271#define cdataptr(cd) ((void *)((cd)+1))
272#define cdataisv(cd) ((cd)->marked & 0x80)
273#define cdatav(cd) ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar)))
274#define cdatavlen(cd) check_exp(cdataisv(cd), cdatav(cd)->len)
275#define sizecdatav(cd) (cdatavlen(cd) + cdatav(cd)->extra)
276#define memcdatav(cd) ((void *)((char *)(cd) - cdatav(cd)->offset))
277
254/* -- Prototype object ---------------------------------------------------- */ 278/* -- Prototype object ---------------------------------------------------- */
255 279
256#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) 280#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
@@ -498,6 +522,7 @@ typedef struct global_State {
498 BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */ 522 BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */
499 GCRef jit_L; /* Current JIT code lua_State or NULL. */ 523 GCRef jit_L; /* Current JIT code lua_State or NULL. */
500 MRef jit_base; /* Current JIT code L->base. */ 524 MRef jit_base; /* Current JIT code L->base. */
525 MRef ctype_state; /* Pointer to C type state. */
501 GCRef gcroot[GCROOT_MAX]; /* GC roots. */ 526 GCRef gcroot[GCROOT_MAX]; /* GC roots. */
502} global_State; 527} global_State;
503 528
@@ -582,6 +607,7 @@ typedef union GCobj {
582 lua_State th; 607 lua_State th;
583 GCproto pt; 608 GCproto pt;
584 GCfunc fn; 609 GCfunc fn;
610 GCcdata cd;
585 GCtab tab; 611 GCtab tab;
586 GCudata ud; 612 GCudata ud;
587} GCobj; 613} GCobj;
@@ -592,6 +618,7 @@ typedef union GCobj {
592#define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th) 618#define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th)
593#define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt) 619#define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt)
594#define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn) 620#define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn)
621#define gco2cd(o) check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd)
595#define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab) 622#define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab)
596#define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud) 623#define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud)
597 624
@@ -619,6 +646,7 @@ typedef union GCobj {
619#define tvisfunc(o) (itype(o) == LJ_TFUNC) 646#define tvisfunc(o) (itype(o) == LJ_TFUNC)
620#define tvisthread(o) (itype(o) == LJ_TTHREAD) 647#define tvisthread(o) (itype(o) == LJ_TTHREAD)
621#define tvisproto(o) (itype(o) == LJ_TPROTO) 648#define tvisproto(o) (itype(o) == LJ_TPROTO)
649#define tviscdata(o) (itype(o) == LJ_TCDATA)
622#define tvistab(o) (itype(o) == LJ_TTAB) 650#define tvistab(o) (itype(o) == LJ_TTAB)
623#define tvisudata(o) (itype(o) == LJ_TUDATA) 651#define tvisudata(o) (itype(o) == LJ_TUDATA)
624#define tvisnum(o) (itype(o) <= LJ_TISNUM) 652#define tvisnum(o) (itype(o) <= LJ_TISNUM)
@@ -657,6 +685,7 @@ typedef union GCobj {
657#define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn) 685#define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn)
658#define threadV(o) check_exp(tvisthread(o), &gcval(o)->th) 686#define threadV(o) check_exp(tvisthread(o), &gcval(o)->th)
659#define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt) 687#define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt)
688#define cdataV(o) check_exp(tviscdata(o), &gcval(o)->cd)
660#define tabV(o) check_exp(tvistab(o), &gcval(o)->tab) 689#define tabV(o) check_exp(tvistab(o), &gcval(o)->tab)
661#define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud) 690#define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud)
662#define numV(o) check_exp(tvisnum(o), (o)->n) 691#define numV(o) check_exp(tvisnum(o), (o)->n)
@@ -703,6 +732,7 @@ define_setV(setstrV, GCstr, LJ_TSTR)
703define_setV(setthreadV, lua_State, LJ_TTHREAD) 732define_setV(setthreadV, lua_State, LJ_TTHREAD)
704define_setV(setprotoV, GCproto, LJ_TPROTO) 733define_setV(setprotoV, GCproto, LJ_TPROTO)
705define_setV(setfuncV, GCfunc, LJ_TFUNC) 734define_setV(setfuncV, GCfunc, LJ_TFUNC)
735define_setV(setcdataV, GCcdata, LJ_TCDATA)
706define_setV(settabV, GCtab, LJ_TTAB) 736define_setV(settabV, GCtab, LJ_TTAB)
707define_setV(setudataV, GCudata, LJ_TUDATA) 737define_setV(setudataV, GCudata, LJ_TUDATA)
708 738
@@ -734,7 +764,7 @@ static LJ_AINLINE int32_t lj_num2bit(lua_Number n)
734/* -- Miscellaneous object handling --------------------------------------- */ 764/* -- Miscellaneous object handling --------------------------------------- */
735 765
736/* Names and maps for internal and external object tags. */ 766/* Names and maps for internal and external object tags. */
737LJ_DATA const char *const lj_obj_typename[1+LUA_TPROTO+1]; 767LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1];
738LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1]; 768LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1];
739 769
740#define typename(o) (lj_obj_itypename[itypemap(o)]) 770#define typename(o) (lj_obj_itypename[itypemap(o)])