From 1282ea7e5af150b3c5ebff9ac0e1dbf2979e5931 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Sun, 11 Sep 2016 17:02:41 +0100 Subject: [PATCH] issue #101 #91 #103 --- include/lauxlib.h | 17 +++-- include/ltable.h | 1 + include/lua.h | 9 ++- llvmbinding/basics.lua | 22 +++---- src/lapi.c | 20 +++++- src/lauxlib.c | 105 +++++++++++++++++++----------- src/ltable.c | 30 +++++++-- src/ravi_llvmluaapi.cpp | 112 ++++++++++++++++---------------- vscode-debugger/src/ravidebug.c | 11 ++-- 9 files changed, 202 insertions(+), 125 deletions(-) diff --git a/include/lauxlib.h b/include/lauxlib.h index 5e034f5..cea24a0 100644 --- a/include/lauxlib.h +++ b/include/lauxlib.h @@ -35,6 +35,10 @@ LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); +/* Following is a Ravi extension - it is similar to luaL_tolstring() except + that the type information is provided using Ravi's extended typename + api function ravi_typename() */ +LUALIB_API const char *(raviL_tolstring) (lua_State *L, int idx, size_t *len); LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l); @@ -265,26 +269,27 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, converted to Lua string, hash code computed etc. Following implementations are taken from a post in Lua mailing list (http://lua-users.org/lists/lua-l/2010-11/msg00151.html) - They use lightuserdata instead of strings to speed - things up meta_key is the key assigned to the meta table of the userdata + IMPORTANT: Caller must ensure that supplied meta_key points to somewhere in + static storage as otherwise memory fault will occur. */ -LUALIB_API int raviU_newmetatable(lua_State *L, const char *meta_key); +LUALIB_API int raviL_newmetatable(lua_State *L, const void *meta_key, + const char *tname); /* meta_key is the key assigned to the meta table of the userdata */ -LUALIB_API void raviU_getmetatable(lua_State *L, const char *meta_key); +LUALIB_API void raviL_getmetatable(lua_State *L, const void *meta_key); /* arg_index is the position of userdata argument on the stack meta_key is the key assigned to the meta table of the userdata */ -LUALIB_API void *raviU_testudata(lua_State *L, int arg_index, const char *meta_key); +LUALIB_API void *raviL_testudata(lua_State *L, int arg_index, const void *meta_key); /* arg_index is the position of userdata argument on the stack meta_key is the key assigned to the meta table of the userdata */ -LUALIB_API void *raviU_checkudata(lua_State *L, int arg_index, const char *meta_key); +LUALIB_API void *raviL_checkudata(lua_State *L, int arg_index, const void *meta_key); #endif diff --git a/include/ltable.h b/include/ltable.h index 449b10a..df1bf84 100644 --- a/include/ltable.h +++ b/include/ltable.h @@ -135,6 +135,7 @@ LUAI_FUNC const TValue *raviH_slice_parent(lua_State *L, TValue *slice); } LUAI_FUNC void raviH_get_number_array_rawdata(lua_State *L, Table *t, lua_Number **startp, lua_Number **endp); +LUAI_FUNC void raviH_get_integer_array_rawdata(lua_State *L, Table *t, lua_Integer **startp, lua_Integer **endp); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); diff --git a/include/lua.h b/include/lua.h index e02ff9f..3067a74 100644 --- a/include/lua.h +++ b/include/lua.h @@ -179,6 +179,9 @@ LUA_API int (lua_isinteger) (lua_State *L, int idx); LUA_API int (lua_isuserdata) (lua_State *L, int idx); LUA_API int (lua_type) (lua_State *L, int idx); LUA_API const char *(lua_typename) (lua_State *L, int tp); +/* This is a Ravi extension - it is similar to lua_typename() except + that it provides more granular type information, and also uses + metamethod __name() if available for userdata and table types */ LUA_API const char * (ravi_typename) (lua_State *L, int idx); LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); @@ -500,9 +503,11 @@ LUA_API int ravi_is_integer_array(lua_State *L, int idx); /* Get the raw data associated with the number array at idx. * Note that Ravi arrays have an extra element at offset 0 - this - * function returns a pointer to &data[0] - bear in mind that + * function returns a pointer to &data[0]. The number of + * array elements is returned in len. */ -LUA_API lua_Number *ravi_get_number_array_rawdata(lua_State *L, int idx); +LUA_API lua_Number *ravi_get_number_array_rawdata(lua_State *L, int idx, size_t *len); +LUA_API lua_Integer *ravi_get_integer_array_rawdata(lua_State *L, int idx, size_t *len); /* API to set the output functions used by Lua / Ravi * This allows the default implementations to be overridden diff --git a/llvmbinding/basics.lua b/llvmbinding/basics.lua index 0330191..af628c2 100644 --- a/llvmbinding/basics.lua +++ b/llvmbinding/basics.lua @@ -4,7 +4,7 @@ assert(llvm) -- Get the LLVM context - right now this is the -- global context context = llvm.context() -assert(getmetatable(context).type == "LLVMcontext") +assert(ravitype(context) == "LLVMcontext") -- The bindings provide a number of predefined types that -- are Lua specific plus some standard C types such as 'int', @@ -12,7 +12,7 @@ assert(getmetatable(context).type == "LLVMcontext") types = context:types() print 'Listing types' for k,v in pairs(types) do - print('\t', k, getmetatable(v).type) + print('\t', k, ravitype(v)) llvm.dump(v) end @@ -21,11 +21,11 @@ end -- Just as an exercise create a struct type -- Initially we have an opaque struct gcobject = context:structtype("GCObject") -assert(getmetatable(gcobject).type == "LLVMstructtype") +assert(ravitype(gcobject) == "LLVMstructtype") -- Create pointer to GCObject gcobject_p = context:pointertype(gcobject) -assert(getmetatable(gcobject_p).type == "LLVMpointertype") +assert(ravitype(gcobject_p) == "LLVMpointertype") -- Add the members of the struct -- Following demonstrates the use of predefined Lua types @@ -43,12 +43,12 @@ llvm.dump(gcobject) -- At this stage the function will get a module and -- execution engine but no body myfunc = context:lua_CFunction("myfunc") -assert(getmetatable(myfunc).type == "LLVMmainfunction") +assert(ravitype(myfunc) == "LLVMmainfunction") -- Get a new IRBuilder intance -- this will be garbage collected by Lua irbuilder = context:irbuilder() -assert(getmetatable(irbuilder).type == "LLVMirbuilder") +assert(ravitype(irbuilder) == "LLVMirbuilder") -- Create a basic block block = context:basicblock("entry") @@ -62,10 +62,10 @@ irbuilder:setinsertpoint(block) -- Get printf decl ----------------------------------------------- printf = myfunc:extern("printf") -assert(getmetatable(printf).type == "LLVMconstant") +assert(ravitype(printf) == "LLVMconstant") luaL_checklstring = myfunc:extern("luaL_checklstring") -assert(getmetatable(luaL_checklstring).type == "LLVMconstant") +assert(ravitype(luaL_checklstring) == "LLVMconstant") hellostr = irbuilder:stringconstant("hello world!\n") irbuilder:call(printf, { hellostr }) @@ -74,11 +74,11 @@ irbuilder:call(printf, { hellostr }) -- Test calling a random function ------------------------------------------------- puts_type = context:functiontype(types.int, {types.pchar}, {vararg=false}) -assert(getmetatable(puts_type).type == "LLVMfunctiontype") +assert(ravitype(puts_type) == "LLVMfunctiontype") -- Declare extern puts() puts = myfunc:extern("puts", puts_type); -assert(getmetatable(puts).type == "LLVMconstant") +assert(ravitype(puts) == "LLVMconstant") -- Get the L parameter of myfunc L = myfunc:arg(1) @@ -92,7 +92,7 @@ irbuilder:call(puts, { str }) -- add CreateRet(0) inst = irbuilder:ret(context:intconstant(0)) -assert(getmetatable(inst).type == "LLVMinstruction") +assert(ravitype(inst) == "LLVMinstruction") -- **************************************************/ -- what did we get? diff --git a/src/lapi.c b/src/lapi.c index 5f9a9b7..2d4c209 100644 --- a/src/lapi.c +++ b/src/lapi.c @@ -277,7 +277,7 @@ LUA_API const char *ravi_typename(lua_State *L, int idx) { case LUA_TNUMINT: return "integer"; case ctb(LUA_TLNGSTR): return "string"; case ctb(LUA_TSHRSTR): return "string"; - case ctb(LUA_TUSERDATA): return "userdata"; + case ctb(LUA_TUSERDATA): return luaT_objtypename(L, o); case LUA_TLIGHTUSERDATA: return "lightuserdata"; case LUA_TLCF: return "lightCfunction"; case ctb(LUA_TCCL): return "Cclosure"; @@ -288,7 +288,7 @@ LUA_API const char *ravi_typename(lua_State *L, int idx) { switch (h->ravi_array.array_type) { case RAVI_TARRAYFLT: return "number[]"; case RAVI_TARRAYINT: return "integer[]"; - default: return "table"; + default: return luaT_objtypename(L, o); } } default: return "unknown"; @@ -850,11 +850,25 @@ LUA_API int ravi_is_integer_array(lua_State *L, int idx) { * Note that Ravi arrays have an extra element at offset 0 - this * function returns a pointer to &data[0] - bear in mind that */ -LUA_API lua_Number* ravi_get_number_array_rawdata(lua_State *L, int idx) { +LUA_API lua_Number* ravi_get_number_array_rawdata(lua_State *L, int idx, size_t *len) { StkId o = index2addr(L, idx); lua_assert(ttistable(o) && hvalue(o)->ravi_array.array_type == RAVI_TARRAYFLT); lua_Number *startp, *endp; raviH_get_number_array_rawdata(L, hvalue(o), &startp, &endp); + *len = (endp - startp); + return startp; +} + +/* Get the raw data associated with the number array at idx. +* Note that Ravi arrays have an extra element at offset 0 - this +* function returns a pointer to &data[0] - bear in mind that +*/ +LUA_API lua_Integer* ravi_get_integer_array_rawdata(lua_State *L, int idx, size_t *len) { + StkId o = index2addr(L, idx); + lua_assert(ttistable(o) && hvalue(o)->ravi_array.array_type == RAVI_TARRAYINT); + lua_Integer *startp, *endp; + raviH_get_integer_array_rawdata(L, hvalue(o), &startp, &endp); + *len = (endp - startp); return startp; } diff --git a/src/lauxlib.c b/src/lauxlib.c index b9427f5..29cba62 100644 --- a/src/lauxlib.c +++ b/src/lauxlib.c @@ -836,6 +836,35 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { return lua_tolstring(L, -1, len); } +LUALIB_API const char *raviL_tolstring(lua_State *L, int idx, size_t *len) { + if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ + switch (lua_type(L, idx)) { + case LUA_TNUMBER: { + if (lua_isinteger(L, idx)) + lua_pushfstring(L, "%I", lua_tointeger(L, idx)); + else + lua_pushfstring(L, "%f", lua_tonumber(L, idx)); + break; + } + case LUA_TSTRING: + lua_pushvalue(L, idx); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); + break; + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + default: + lua_pushfstring(L, "%s: %p", ravi_typename(L, idx), + lua_topointer(L, idx)); + break; + } + } + return lua_tolstring(L, -1, len); +} + + /* ** {====================================================== @@ -1033,60 +1062,62 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { ver, *v); } -// The normal Lua metatable functions in C use string -// keys - these are expensive as the key needs to be -// converted to Lua string, hash code computed etc. -// Following implementations are taken from a post in -// Lua mailing list (http://lua-users.org/lists/lua-l/2010-11/msg00151.html) -// They use lightuserdata instead of strings to speed -// things up -// meta_key is the key assigned to the meta -// table of the userdata -LUALIB_API int raviU_newmetatable(lua_State *L, const char *meta_key) { +/* The normal Lua metatable functions in C use string + keys - these are expensive as the key needs to be + converted to Lua string, hash code computed etc. + Following implementations are taken from a post in + Lua mailing list (http://lua-users.org/lists/lua-l/2010-11/msg00151.html) + They use lightuserdata instead of strings to speed + things up + meta_key is the key assigned to the meta + table of the userdata */ +LUALIB_API int raviL_newmetatable(lua_State *L, const void *meta_key, const char *tname) { lua_pushlightuserdata(L, (void *)meta_key); lua_rawget(L, LUA_REGISTRYINDEX); - if (!lua_isnil(L, -1)) {// name already in use? - return 0; // leave previous value on top, but return 0 + if (!lua_isnil(L, -1)) { /* name already in use? */ + return 0; /* leave previous value on top, but return 0 */ } - lua_pop(L, 1); // pop the nil value - lua_newtable(L); // create metatable - lua_pushlightuserdata(L, (void *)meta_key); // meta_key - lua_pushvalue(L, -2); // table - lua_rawset(L, LUA_REGISTRYINDEX); // assign table to meta_key in the registry + lua_pop(L, 1); /* pop the nil value */ + lua_createtable(L, 0, 2); /* create metatable */ + lua_pushstring(L, tname); + lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ + lua_pushlightuserdata(L, (void *)meta_key); /* meta_key */ + lua_pushvalue(L, -2); /* table */ + lua_rawset(L, LUA_REGISTRYINDEX); /* assign table to meta_key in the registry */ return 1; } -// meta_key is the key assigned to the meta table of the userdata -LUALIB_API void raviU_getmetatable(lua_State *L, const char *meta_key) { - lua_pushlightuserdata(L, (void *)meta_key); // meta_key - lua_rawget(L, LUA_REGISTRYINDEX); // obtain the value associated with - // meta_key from registry +/* meta_key is the key assigned to the meta table of the userdata */ +LUALIB_API void raviL_getmetatable(lua_State *L, const void *meta_key) { + lua_pushlightuserdata(L, (void *)meta_key); /* meta_key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* obtain the value associated with + meta_key from registry */ } -// arg_index is the position of userdata argument on the stack -// meta_key is the key assigned to the meta table of the userdata -LUALIB_API void *raviU_testudata(lua_State *L, int arg_index, - const char *meta_key) { +/* arg_index is the position of userdata argument on the stack + meta_key is the key assigned to the meta table of the userdata */ +LUALIB_API void *raviL_testudata(lua_State *L, int arg_index, + const void *meta_key) { void *p = lua_touserdata(L, arg_index); - if (p != NULL) { // value is a userdata? - if (lua_getmetatable(L, arg_index)) { // does it have a metatable? - lua_pushlightuserdata(L, (void *)meta_key); // meta_key + if (p != NULL) { /* value is a userdata? */ + if (lua_getmetatable(L, arg_index)) { /* does it have a metatable? */ + lua_pushlightuserdata(L, (void *)meta_key); /* meta_key */ lua_rawget( L, - LUA_REGISTRYINDEX); // get correct metatable associated with meta_key - if (!lua_rawequal(L, -1, -2)) // compare: does it have the correct mt? + LUA_REGISTRYINDEX); /* get correct metatable associated with meta_key */ + if (!lua_rawequal(L, -1, -2)) /* compare: does it have the correct mt? */ p = NULL; - lua_pop(L, 2); // remove both metatables + lua_pop(L, 2); /* remove both metatables */ } } return p; /* to avoid warnings */ } -// arg_index is the position of userdata argument on the stack -// meta_key is the key assigned to the meta table of the userdata -LUALIB_API void *raviU_checkudata(lua_State *L, int arg_index, - const char *meta_key) { - void *p = raviU_testudata(L, arg_index, meta_key); +/* arg_index is the position of userdata argument on the stack + meta_key is the key assigned to the meta table of the userdata */ +LUALIB_API void *raviL_checkudata(lua_State *L, int arg_index, + const void *meta_key) { + void *p = raviL_testudata(L, arg_index, meta_key); if (p == NULL) luaL_argerror(L, arg_index, meta_key); return p; diff --git a/src/ltable.c b/src/ltable.c index 31b7e26..e7ca55a 100644 --- a/src/ltable.c +++ b/src/ltable.c @@ -716,17 +716,27 @@ int raviH_getn(Table *t) { static int ravi_resize_array(lua_State *L, Table *t, unsigned int new_size, int initialize) { if (t->ravi_array.array_modifier) { + /* cannot resize */ return 0; } - /* NOTE - relies upon lua_Number and lua_Integer being the same size */ - lua_assert(sizeof(lua_Integer) == sizeof(lua_Number)); + int number_array = RAVI_TARRAYFLT == t->ravi_array.array_type; unsigned int size = new_size < t->ravi_array.size + 10 ? t->ravi_array.size + 10 : new_size; - t->ravi_array.data = (char *)luaM_reallocv( + if (number_array) { + t->ravi_array.data = (char *)luaM_reallocv( L, t->ravi_array.data, t->ravi_array.size, size, sizeof(lua_Number)); - if (initialize) { - lua_Number *data = (lua_Number *)t->ravi_array.data; - memset(&data[t->ravi_array.len], 0, size - t->ravi_array.size); + if (initialize) { + lua_Number *data = (lua_Number *)t->ravi_array.data; + memset(&data[t->ravi_array.len], 0, (size - t->ravi_array.size) * sizeof(lua_Number)); + } + } + else { + t->ravi_array.data = (char *)luaM_reallocv( + L, t->ravi_array.data, t->ravi_array.size, size, sizeof(lua_Integer)); + if (initialize) { + lua_Integer *data = (lua_Integer *)t->ravi_array.data; + memset(&data[t->ravi_array.len], 0, (size - t->ravi_array.size) * sizeof(lua_Integer)); + } } t->ravi_array.size = size; return 1; @@ -828,6 +838,14 @@ void raviH_get_number_array_rawdata(lua_State *L, Table *t, lua_Number **startp, *endp = data + t->ravi_array.len + 1; } +void raviH_get_integer_array_rawdata(lua_State *L, Table *t, lua_Integer **startp, lua_Integer **endp) { + (void)L; + lua_assert(t->ravi_array.array_type == RAVI_TARRAYINT); + lua_Integer *data = (lua_Integer *)t->ravi_array.data; + *startp = data; + *endp = data + t->ravi_array.len + 1; +} + static const char *key_orig_table = "Originaltable"; /* Create a slice of an existing array diff --git a/src/ravi_llvmluaapi.cpp b/src/ravi_llvmluaapi.cpp index a9c6af6..c1779ab 100644 --- a/src/ravi_llvmluaapi.cpp +++ b/src/ravi_llvmluaapi.cpp @@ -67,74 +67,74 @@ static const char *LLVM_instruction = "LLVMinstruction"; static const char *LLVM_phinode = "LLVMphinode"; #define test_LLVM_irbuilder(L, idx) \ - ((IRBuilderHolder *)raviU_testudata(L, idx, LLVM_irbuilder)) + ((IRBuilderHolder *)raviL_testudata(L, idx, LLVM_irbuilder)) #define check_LLVM_irbuilder(L, idx) \ - ((IRBuilderHolder *)raviU_checkudata(L, idx, LLVM_irbuilder)) + ((IRBuilderHolder *)raviL_checkudata(L, idx, LLVM_irbuilder)) #define test_LLVM_type(L, idx) \ - ((TypeHolder *)raviU_testudata(L, idx, LLVM_type)) + ((TypeHolder *)raviL_testudata(L, idx, LLVM_type)) #define check_LLVM_type(L, idx) \ - ((TypeHolder *)raviU_checkudata(L, idx, LLVM_type)) + ((TypeHolder *)raviL_checkudata(L, idx, LLVM_type)) #define test_LLVM_context(L, idx) \ - ((ContextHolder *)raviU_testudata(L, idx, LLVM_context)) + ((ContextHolder *)raviL_testudata(L, idx, LLVM_context)) #define check_LLVM_context(L, idx) \ - ((ContextHolder *)raviU_checkudata(L, idx, LLVM_context)) + ((ContextHolder *)raviL_checkudata(L, idx, LLVM_context)) #define test_LLVM_structtype(L, idx) \ - ((StructTypeHolder *)raviU_testudata(L, idx, LLVM_structtype)) + ((StructTypeHolder *)raviL_testudata(L, idx, LLVM_structtype)) #define check_LLVM_structtype(L, idx) \ - ((StructTypeHolder *)raviU_checkudata(L, idx, LLVM_structtype)) + ((StructTypeHolder *)raviL_checkudata(L, idx, LLVM_structtype)) #define test_LLVM_pointertype(L, idx) \ - ((PointerTypeHolder *)raviU_testudata(L, idx, LLVM_pointertype)) + ((PointerTypeHolder *)raviL_testudata(L, idx, LLVM_pointertype)) #define check_LLVM_pointertype(L, idx) \ - ((PointerTypeHolder *)raviU_checkudata(L, idx, LLVM_pointertype)) + ((PointerTypeHolder *)raviL_checkudata(L, idx, LLVM_pointertype)) #define test_LLVM_functiontype(L, idx) \ - ((FunctionTypeHolder *)raviU_testudata(L, idx, LLVM_functiontype)) + ((FunctionTypeHolder *)raviL_testudata(L, idx, LLVM_functiontype)) #define check_LLVM_functiontype(L, idx) \ - ((FunctionTypeHolder *)raviU_checkudata(L, idx, LLVM_functiontype)) + ((FunctionTypeHolder *)raviL_checkudata(L, idx, LLVM_functiontype)) #define test_LLVM_mainfunction(L, idx) \ - ((MainFunctionHolder *)raviU_testudata(L, idx, LLVM_mainfunction)) + ((MainFunctionHolder *)raviL_testudata(L, idx, LLVM_mainfunction)) #define check_LLVM_mainfunction(L, idx) \ - ((MainFunctionHolder *)raviU_checkudata(L, idx, LLVM_mainfunction)) + ((MainFunctionHolder *)raviL_checkudata(L, idx, LLVM_mainfunction)) #define test_LLVM_function(L, idx) \ - ((FunctionHolder *)raviU_testudata(L, idx, LLVM_function)) + ((FunctionHolder *)raviL_testudata(L, idx, LLVM_function)) #define check_LLVM_function(L, idx) \ - ((FunctionHolder *)raviU_checkudata(L, idx, LLVM_function)) + ((FunctionHolder *)raviL_checkudata(L, idx, LLVM_function)) #define test_LLVM_basicblock(L, idx) \ - ((BasicBlockHolder *)raviU_testudata(L, idx, LLVM_basicblock)) + ((BasicBlockHolder *)raviL_testudata(L, idx, LLVM_basicblock)) #define check_LLVM_basicblock(L, idx) \ - ((BasicBlockHolder *)raviU_checkudata(L, idx, LLVM_basicblock)) + ((BasicBlockHolder *)raviL_checkudata(L, idx, LLVM_basicblock)) #define test_LLVM_value(L, idx) \ - ((ValueHolder *)raviU_testudata(L, idx, LLVM_value)) + ((ValueHolder *)raviL_testudata(L, idx, LLVM_value)) #define check_LLVM_value(L, idx) \ - ((ValueHolder *)raviU_checkudata(L, idx, LLVM_value)) + ((ValueHolder *)raviL_checkudata(L, idx, LLVM_value)) #define test_LLVM_constant(L, idx) \ - ((ConstantHolder *)raviU_testudata(L, idx, LLVM_constant)) + ((ConstantHolder *)raviL_testudata(L, idx, LLVM_constant)) #define check_LLVM_constant(L, idx) \ - ((ConstantHolder *)raviU_checkudata(L, idx, LLVM_constant)) + ((ConstantHolder *)raviL_checkudata(L, idx, LLVM_constant)) #define test_LLVM_instruction(L, idx) \ - ((InstructionHolder *)raviU_testudata(L, idx, LLVM_instruction)) + ((InstructionHolder *)raviL_testudata(L, idx, LLVM_instruction)) #define check_LLVM_instruction(L, idx) \ - ((InstructionHolder *)raviU_checkudata(L, idx, LLVM_instruction)) + ((InstructionHolder *)raviL_checkudata(L, idx, LLVM_instruction)) #define test_LLVM_module(L, idx) \ - ((ModuleHolder *)raviU_testudata(L, idx, LLVM_module)) + ((ModuleHolder *)raviL_testudata(L, idx, LLVM_module)) #define check_LLVM_module(L, idx) \ - ((ModuleHolder *)raviU_checkudata(L, idx, LLVM_module)) + ((ModuleHolder *)raviL_checkudata(L, idx, LLVM_module)) #define test_LLVM_phinode(L, idx) \ - ((PhiNodeHolder *)raviU_testudata(L, idx, LLVM_phinode)) + ((PhiNodeHolder *)raviL_testudata(L, idx, LLVM_phinode)) #define check_LLVM_phinode(L, idx) \ - ((PhiNodeHolder *)raviU_checkudata(L, idx, LLVM_phinode)) + ((PhiNodeHolder *)raviL_checkudata(L, idx, LLVM_phinode)) struct ContextHolder { /* Each Ravi instance (Lua instance) has its own @@ -206,7 +206,7 @@ static int context_new_LLVM_irbuilder(lua_State *L) { IRBuilderHolder *builder = (IRBuilderHolder *)lua_newuserdata(L, sizeof(IRBuilderHolder)); builder->builder = new llvm::IRBuilder<>(context->jitState->context()); - raviU_getmetatable(L, LLVM_irbuilder); + raviL_getmetatable(L, LLVM_irbuilder); lua_setmetatable(L, -2); return 1; } @@ -223,28 +223,28 @@ static int collect_LLVM_irbuilder(lua_State *L) { static void alloc_LLVM_module(lua_State *L, llvm::Module *M) { ModuleHolder *mh = (ModuleHolder *)lua_newuserdata(L, sizeof(ModuleHolder)); - raviU_getmetatable(L, LLVM_module); + raviL_getmetatable(L, LLVM_module); lua_setmetatable(L, -2); mh->M = M; } static void alloc_LLVM_type(lua_State *L, llvm::Type *t) { TypeHolder *tt = (TypeHolder *)lua_newuserdata(L, sizeof(TypeHolder)); - raviU_getmetatable(L, LLVM_type); + raviL_getmetatable(L, LLVM_type); lua_setmetatable(L, -2); tt->type = t; } static void alloc_LLVM_context(lua_State *L, ravi::RaviJITState *jit) { ContextHolder *h = (ContextHolder *)lua_newuserdata(L, sizeof(ContextHolder)); - raviU_getmetatable(L, LLVM_context); + raviL_getmetatable(L, LLVM_context); lua_setmetatable(L, -2); h->jitState = jit; } static void alloc_LLVM_value(lua_State *L, llvm::Value *v) { ValueHolder *h = (ValueHolder *)lua_newuserdata(L, sizeof(ValueHolder)); - raviU_getmetatable(L, LLVM_value); + raviL_getmetatable(L, LLVM_value); lua_setmetatable(L, -2); h->value = v; } @@ -252,7 +252,7 @@ static void alloc_LLVM_value(lua_State *L, llvm::Value *v) { static void alloc_LLVM_structtype(lua_State *L, llvm::StructType *type) { StructTypeHolder *h = (StructTypeHolder *)lua_newuserdata(L, sizeof(StructTypeHolder)); - raviU_getmetatable(L, LLVM_structtype); + raviL_getmetatable(L, LLVM_structtype); lua_setmetatable(L, -2); h->type = type; } @@ -260,7 +260,7 @@ static void alloc_LLVM_structtype(lua_State *L, llvm::StructType *type) { static void alloc_LLVM_pointertype(lua_State *L, llvm::PointerType *type) { PointerTypeHolder *h = (PointerTypeHolder *)lua_newuserdata(L, sizeof(PointerTypeHolder)); - raviU_getmetatable(L, LLVM_pointertype); + raviL_getmetatable(L, LLVM_pointertype); lua_setmetatable(L, -2); h->type = type; } @@ -268,7 +268,7 @@ static void alloc_LLVM_pointertype(lua_State *L, llvm::PointerType *type) { static void alloc_LLVM_functiontype(lua_State *L, llvm::FunctionType *type) { FunctionTypeHolder *h = (FunctionTypeHolder *)lua_newuserdata(L, sizeof(FunctionTypeHolder)); - raviU_getmetatable(L, LLVM_functiontype); + raviL_getmetatable(L, LLVM_functiontype); lua_setmetatable(L, -2); h->type = type; } @@ -276,7 +276,7 @@ static void alloc_LLVM_functiontype(lua_State *L, llvm::FunctionType *type) { static void alloc_LLVM_function(lua_State *L, llvm::Function *f) { FunctionHolder *h = (FunctionHolder *)lua_newuserdata(L, sizeof(FunctionHolder)); - raviU_getmetatable(L, LLVM_function); + raviL_getmetatable(L, LLVM_function); lua_setmetatable(L, -2); h->function = f; } @@ -284,7 +284,7 @@ static void alloc_LLVM_function(lua_State *L, llvm::Function *f) { static void alloc_LLVM_constant(lua_State *L, llvm::Constant *f) { ConstantHolder *h = (ConstantHolder *)lua_newuserdata(L, sizeof(ConstantHolder)); - raviU_getmetatable(L, LLVM_constant); + raviL_getmetatable(L, LLVM_constant); lua_setmetatable(L, -2); h->constant = f; } @@ -292,14 +292,14 @@ static void alloc_LLVM_constant(lua_State *L, llvm::Constant *f) { static void alloc_LLVM_instruction(lua_State *L, llvm::Instruction *i) { InstructionHolder *h = (InstructionHolder *)lua_newuserdata(L, sizeof(InstructionHolder)); - raviU_getmetatable(L, LLVM_instruction); + raviL_getmetatable(L, LLVM_instruction); lua_setmetatable(L, -2); h->i = i; } static void alloc_LLVM_phinode(lua_State *L, llvm::PHINode *phi) { PhiNodeHolder *h = (PhiNodeHolder *)lua_newuserdata(L, sizeof(PhiNodeHolder)); - raviU_getmetatable(L, LLVM_phinode); + raviL_getmetatable(L, LLVM_phinode); lua_setmetatable(L, -2); h->phi = phi; } @@ -313,7 +313,7 @@ static MainFunctionHolder *alloc_LLVM_mainfunction(lua_State *L, h->func = nullptr; h->compiled_func = nullptr; h->arg1 = nullptr; - raviU_getmetatable(L, LLVM_mainfunction); + raviL_getmetatable(L, LLVM_mainfunction); lua_setmetatable(L, -2); auto module = std::make_shared(G(L)->ravi_state->jit); h->func = new ravi::RaviJITFunction(&h->compiled_func, module, type, @@ -335,7 +335,7 @@ static int collect_LLVM_mainfunction(lua_State *L) { static void alloc_LLVM_basicblock(lua_State *L, llvm::BasicBlock *b) { BasicBlockHolder *h = (BasicBlockHolder *)lua_newuserdata(L, sizeof(BasicBlockHolder)); - raviU_getmetatable(L, LLVM_basicblock); + raviL_getmetatable(L, LLVM_basicblock); lua_setmetatable(L, -2); h->b = b; } @@ -1316,7 +1316,7 @@ static const luaL_Reg irbuilder_methods[] = { {NULL, NULL}}; LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { - raviU_newmetatable(L, LLVM_context); + raviL_newmetatable(L, LLVM_context, LLVM_context); lua_pushstring(L, LLVM_context); lua_setfield(L, -2, "type"); lua_pushvalue(L, -1); /* push metatable */ @@ -1324,7 +1324,7 @@ LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { luaL_setfuncs(L, context_methods, 0); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_module); + raviL_newmetatable(L, LLVM_module, LLVM_module); lua_pushstring(L, LLVM_module); lua_setfield(L, -2, "type"); lua_pushvalue(L, -1); /* push metatable */ @@ -1332,7 +1332,7 @@ LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { luaL_setfuncs(L, module_methods, 0); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_irbuilder); + raviL_newmetatable(L, LLVM_irbuilder, LLVM_irbuilder); lua_pushstring(L, LLVM_irbuilder); lua_setfield(L, -2, "type"); lua_pushcfunction(L, collect_LLVM_irbuilder); @@ -1342,7 +1342,7 @@ LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { luaL_setfuncs(L, irbuilder_methods, 0); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_mainfunction); + raviL_newmetatable(L, LLVM_mainfunction, LLVM_mainfunction); lua_pushstring(L, LLVM_mainfunction); lua_setfield(L, -2, "type"); lua_pushcfunction(L, collect_LLVM_mainfunction); @@ -1352,7 +1352,7 @@ LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { luaL_setfuncs(L, main_function_methods, 0); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_function); + raviL_newmetatable(L, LLVM_function, LLVM_function); lua_pushstring(L, LLVM_function); lua_setfield(L, -2, "type"); lua_pushvalue(L, -1); /* push metatable */ @@ -1360,17 +1360,17 @@ LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { luaL_setfuncs(L, function_methods, 0); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_type); + raviL_newmetatable(L, LLVM_type, LLVM_type); lua_pushstring(L, LLVM_type); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_value); + raviL_newmetatable(L, LLVM_value, LLVM_value); lua_pushstring(L, LLVM_value); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_structtype); + raviL_newmetatable(L, LLVM_structtype, LLVM_structtype); lua_pushstring(L, LLVM_structtype); lua_setfield(L, -2, "type"); lua_pushvalue(L, -1); /* push metatable */ @@ -1378,32 +1378,32 @@ LUAMOD_API int raviopen_llvmluaapi(lua_State *L) { luaL_setfuncs(L, structtype_methods, 0); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_pointertype); + raviL_newmetatable(L, LLVM_pointertype, LLVM_pointertype); lua_pushstring(L, LLVM_pointertype); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_functiontype); + raviL_newmetatable(L, LLVM_functiontype, LLVM_functiontype); lua_pushstring(L, LLVM_functiontype); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_basicblock); + raviL_newmetatable(L, LLVM_basicblock, LLVM_basicblock); lua_pushstring(L, LLVM_basicblock); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_constant); + raviL_newmetatable(L, LLVM_constant, LLVM_constant); lua_pushstring(L, LLVM_constant); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_instruction); + raviL_newmetatable(L, LLVM_instruction, LLVM_instruction); lua_pushstring(L, LLVM_instruction); lua_setfield(L, -2, "type"); lua_pop(L, 1); - raviU_newmetatable(L, LLVM_phinode); + raviL_newmetatable(L, LLVM_phinode, LLVM_phinode); lua_pushstring(L, LLVM_phinode); lua_setfield(L, -2, "type"); lua_pushvalue(L, -1); /* push metatable */ diff --git a/vscode-debugger/src/ravidebug.c b/vscode-debugger/src/ravidebug.c index 15ae7b2..61b4bb9 100644 --- a/vscode-debugger/src/ravidebug.c +++ b/vscode-debugger/src/ravidebug.c @@ -486,14 +486,17 @@ static int count_table_entries(lua_State *L, int stack_index) { // Get information regarding a Lua table static void get_table_info(lua_State *L, int stack_idx, char *buf, size_t len) { int num = count_table_entries(L, stack_idx); + const char *typename = ravi_typename(L, stack_idx); const void *ptr = lua_topointer(L, stack_idx); - snprintf(buf, len, "%p (%d items)", ptr, num); + snprintf(buf, len, "%s %p (%d items)", typename, ptr, num); } // Get information regarding a Lua userdata value static void get_userdata(lua_State *L, int stack_idx, char *buf, size_t len) { - void *udata = lua_touserdata(L, stack_idx); - snprintf(buf, len, "%p", udata); + + const char *udata = raviL_tolstring(L, stack_idx, NULL); + vscode_json_stringify(udata, buf, len); + lua_pop(L, 1); /* remove result from raviL_tolstring() */ } // Get information regarding a Lua value @@ -545,7 +548,7 @@ static int get_value(lua_State *L, int stack_idx, char *buf, size_t len) { break; } case LUA_TTHREAD: { - snprintf(buf, len, "Thread %p", lua_topointer(L, stack_idx)); + snprintf(buf, len, "thread %p", lua_topointer(L, stack_idx)); break; } }