cinvoke-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cinvoke-svn] r43 - in trunk/cinvoke: . bindings/lua


From: will
Subject: [cinvoke-svn] r43 - in trunk/cinvoke: . bindings/lua
Date: 25 Jun 2006 01:03:58 -0400

Author: will
Date: 2006-06-25 01:03:57 -0400 (Sun, 25 Jun 2006)
New Revision: 43

Modified:
   trunk/cinvoke/TODO
   trunk/cinvoke/bindings/lua/cinvoke_lua.c
Log:
more lua stuff


Modified: trunk/cinvoke/TODO
===================================================================
--- trunk/cinvoke/TODO  2006-06-24 18:36:30 UTC (rev 42)
+++ trunk/cinvoke/TODO  2006-06-25 05:03:57 UTC (rev 43)
@@ -1,5 +1,4 @@
-lua callbacks
-investigate tied variables, integrate with lua
+lua callbacks, tied variables
 amd64 port
 ppc port
 java binding

Modified: trunk/cinvoke/bindings/lua/cinvoke_lua.c
===================================================================
--- trunk/cinvoke/bindings/lua/cinvoke_lua.c    2006-06-24 18:36:30 UTC (rev 42)
+++ trunk/cinvoke/bindings/lua/cinvoke_lua.c    2006-06-25 05:03:57 UTC (rev 43)
@@ -3,47 +3,110 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <time.h>
 
+// XXX add callbacks to type system
+
 struct LibStruct {
        CInvContext *ctx;
        CInvLibrary *lib;
+       cinv_callconv_t cc;
 };
 
+void expected(lua_State *l, const char *exp, int index) {
+       char err[256];
+       snprintf(err, sizeof(err), "expected %s, got %s",
+               exp, lua_typename(l, index));
+       lua_pushstring(l, err);
+       lua_error(l);
+}
+
+const char *tolstring(lua_State *l, int index, size_t *len) {
+       if (lua_type(l, index) != LUA_TSTRING)
+               expected(l, "string", index);
+       return lua_tolstring(l, index, len);
+}
+const char *tostring(lua_State *l, int index) {
+       return tolstring(l, index, NULL);
+}
+void *touserdata(lua_State *l, int index) {
+       if (lua_type(l, index) != LUA_TUSERDATA)
+               expected(l, "userdata", index);
+       return lua_touserdata(l, index);
+}
+lua_Integer tointeger(lua_State *l, int index) {
+       if (lua_type(l, index) != LUA_TNUMBER)
+               expected(l, "integer", index);
+       return lua_tointeger(l, index);
+}
+lua_Number tonumber(lua_State *l, int index) {
+       if (lua_type(l, index) != LUA_TNUMBER)
+               expected(l, "number", index);
+       return lua_tonumber(l, index);
+}
+
 int _clibrary_new(lua_State *l) {
        CInvContext *ctx;
        CInvLibrary *lib;
+       cinv_callconv_t cc = CINV_CC_DEFAULT;
        struct LibStruct *st;
 
-       if (lua_gettop(l) != 1) {
-               lua_pushstring(l, "usage: clibrary.new(libname)");
+       if (lua_gettop(l) < 1 || lua_gettop(l) > 2) {
+               lua_pushstring(l, "usage: clibrary.new(libname[, 
callingconv])");
                lua_error(l);
        }
 
+       if (lua_gettop(l) == 2) {
+               const char *cs = tostring(l, 2);
+               if (!strcmp("cdecl", cs))
+                       cc = CINV_CC_CDECL;
+               else if (!strcmp("stdcall", cs))
+                       cc = CINV_CC_STDCALL;
+               else if (!strcmp("fastcall", cs))
+                       cc = CINV_CC_FASTCALL;
+               else if (strcmp("default", cs)) {
+                       lua_pushstring(l, "unknown calling convention");
+                       lua_error(l);
+               }
+       }
+
        ctx = cinv_context_create();
        if (ctx == NULL) {
                lua_pushstring(l, "out of memory");
                lua_error(l);
        }
        
-       lib = cinv_library_create(ctx, lua_tostring(l, 1));
+       lib = cinv_library_create(ctx, tostring(l, 1));
        if (lib == NULL) {
                lua_pushstring(l, cinv_context_get_errormsg(ctx));
                cinv_context_delete(ctx);
                lua_error(l);
        }
        
+       lua_newtable(l);
+
+       lua_newtable(l);
+       lua_getglobal(l, "clibrary");
+       lua_setfield(l, -2, "__index");
+       lua_setmetatable(l, -2);
+       
        st = lua_newuserdata(l, sizeof(struct LibStruct)); // return value
        st->ctx = ctx;
        st->lib = lib;
+       st->cc = cc;
        
        lua_newtable(l);
        lua_getglobal(l, "clibrary");
-       lua_setfield(l, -2, "__index");
-       lua_getglobal(l, "clibrary");
        lua_getfield(l, -1, "dispose");
        lua_setfield(l, -3, "__gc");
        lua_pop(l, 1);
        lua_setmetatable(l, -2);
+
+       lua_setfield(l, -2, "ud");
+
+       lua_newtable(l);
+       lua_setfield(l, -2, "ties");
+
        return 1;
 }
 int _clibrary_dispose(lua_State *l) {
@@ -54,12 +117,17 @@
                lua_error(l);
        }
 
-       st = lua_touserdata(l, 1);
-       if (st->lib) {
-               cinv_library_delete(st->ctx, st->lib);
-               st->lib = NULL;
+       if (lua_type(l, 1) == LUA_TUSERDATA)
+               st = touserdata(l, 1);
+       else {
+               lua_getfield(l, 1, "ud");
+               st = touserdata(l, -1);
        }
        if (st->ctx) {
+               if (st->lib) {
+                       cinv_library_delete(st->ctx, st->lib);
+                       st->lib = NULL;
+               }
                cinv_context_delete(st->ctx);
                st->ctx = NULL;
        }
@@ -69,7 +137,7 @@
 int isvoid(lua_State *l, int index) {
        int ret;
        lua_getfield(l, index, "family");
-       ret = !strcmp("void", lua_tostring(l, -1));
+       ret = !strcmp("void", tostring(l, -1));
        lua_pop(l, 1);
        return ret;
 }
@@ -84,17 +152,24 @@
 int isstruct(lua_State *l, int index) {
        int ret;
        lua_getfield(l, index, "family");
-       ret = !strcmp("struct", lua_tostring(l, -1));
+       ret = !strcmp("struct", tostring(l, -1));
        lua_pop(l, 1);
        return ret;
 }
 int isstring(lua_State *l, int index) {
        int ret;
        lua_getfield(l, index, "family");
-       ret = !strcmp("string", lua_tostring(l, -1));
+       ret = !strcmp("string", tostring(l, -1));
        lua_pop(l, 1);
        return ret;
 }
+int isbasic(lua_State *l, int index) {
+       int ret;
+       lua_getfield(l, index, "family");
+       ret = !strcmp("basic", tostring(l, -1));
+       lua_pop(l, 1);
+       return ret;
+}
 
 // dont need explicit dispose methods for structures, callbacks or functions
 struct StrStruct {
@@ -102,12 +177,12 @@
        CInvStructure *st;
 };
 int _cstructure_gc(lua_State *l) {
-       struct StrStruct *st = lua_touserdata(l, 1);
-       if (st->st) {
-               cinv_structure_delete(st->ctx, st->st);
-               st->st = NULL;
-       }
+       struct StrStruct *st = touserdata(l, 1);
        if (st->ctx) {
+               if (st->st) {
+                       cinv_structure_delete(st->ctx, st->st);
+                       st->st = NULL;
+               }
                cinv_context_delete(st->ctx);
                st->ctx = NULL;
        }
@@ -150,22 +225,22 @@
                }
 
                lua_getfield(l, i, "family");
-               family = lua_tostring(l, -1);
+               family = tostring(l, -1);
                if (!strcmp(family, "void")) {
                        lua_pushstring(l, "void is not a type");
                        goto error;
                } else if (!strcmp(family, "string")) {
                        if (!cinv_structure_addmember_value(ctx, st,
-                               lua_tostring(l, i + 1), CINV_T_PTR)) {
+                               tostring(l, i + 1), CINV_T_PTR)) {
                                lua_pushstring(l, 
cinv_context_get_errormsg(ctx));
                                goto error;
                        }
                } else if (!strcmp(family, "struct")) {
                        struct StrStruct *mem;
                        lua_getfield(l, i, "ud");
-                       mem = lua_touserdata(l, -1);
+                       mem = touserdata(l, -1);
                        if (!cinv_structure_addmember_struct(ctx, st,
-                               lua_tostring(l, i + 1), mem->st)) {
+                               tostring(l, i + 1), mem->st)) {
                                lua_pushstring(l, 
cinv_context_get_errormsg(ctx));
                                goto error;
                        }
@@ -173,9 +248,9 @@
                } else if (!strcmp(family, "basic")) {
                        lua_Integer id;
                        lua_getfield(l, i, "id");
-                       id = lua_tointeger(l, -1);
+                       id = tointeger(l, -1);
                        if (!cinv_structure_addmember_value(ctx, st,
-                               lua_tostring(l, i + 1), (cinv_type_t)id)) {
+                               tostring(l, i + 1), (cinv_type_t)id)) {
                                lua_pushstring(l, 
cinv_context_get_errormsg(ctx));
                                goto error;
                        }
@@ -230,13 +305,14 @@
        return 0;
 }
 
-char getcode(lua_State *l, const char *family, int tblindex) {
-       lua_getfield(l, tblindex, "array");
-       if (!lua_isnil(l, -1)) {
-               lua_pop(l, 1);
+char getcode(lua_State *l, const char *family, int tblindex, int callback) {
+       if (isarray(l, tblindex)) {
+               if (callback) {
+                       lua_pushstring(l, "callbacks cannot accept arrays as 
arguments");
+                       return (char)-1;
+               }
                return 'p';
        }
-       lua_pop(l, 1);
 
        if (!strcmp(family, "struct")) {
                lua_pushstring(l,
@@ -246,7 +322,7 @@
                return 'p';
        } else if (!strcmp(family, "basic")) {
                lua_getfield(l, tblindex, "charid");
-               lua_Integer charid = lua_tointeger(l, -1);
+               lua_Integer charid = tointeger(l, -1);
                lua_pop(l, 1);
                return (char)charid;
        } else {
@@ -255,7 +331,8 @@
        }
 }
 
-CInvFunction *parsefunction(lua_State *l, CInvContext *ctx, int startarg) {
+CInvFunction *parsefunction(lua_State *l, CInvContext *ctx, int startarg,
+       int callback, cinv_callconv_t cc) {
        CInvFunction *ret;
        char retfmt[2];
        char *parmfmt;
@@ -277,13 +354,17 @@
                lua_pushstring(l, "returning arrays not supported");
                lua_error(l);
        }
+       if (callback && isstring(l, startarg)) {
+               lua_pushstring(l, "callbacks cannot return strings");
+               lua_error(l);
+       }
 
        lua_getfield(l, startarg, "family");
-       family = lua_tostring(l, -1);
+       family = tostring(l, -1);
        if (!strcmp("void", family))
                retfmt[0] = '\0';
        else {
-               retfmt[0] = getcode(l, family, startarg);
+               retfmt[0] = getcode(l, family, startarg, callback);
                if (retfmt[0] == (char)-1)
                        lua_error(l);
                retfmt[1] = '\0';
@@ -305,7 +386,7 @@
                        lua_pushstring(l, "invalid type");
                        lua_error(l);
                }
-               family = lua_tostring(l, -1);
+               family = tostring(l, -1);
                if (!strcmp("void", family)) {
                        // only put up with void if it is the first and only 
parameter
                        if (i == numparms && i == (startarg + 2)) {
@@ -316,7 +397,7 @@
                                lua_error(l);
                        }
                } else {
-                       parmfmt[i - (startarg + 2)] = getcode(l, family, i);
+                       parmfmt[i - (startarg + 2)] = getcode(l, family, i, 
callback);
                        if (parmfmt[i - (startarg + 2)] == (char)-1) {
                                free(parmfmt);
                                lua_error(l);
@@ -325,7 +406,7 @@
        }
        parmfmt[i - (startarg + 2)] = '\0';
 
-       ret = cinv_function_create(ctx, CINV_CC_DEFAULT, retfmt, parmfmt);
+       ret = cinv_function_create(ctx, cc, retfmt, parmfmt);
        if (!ret) {
                free(parmfmt);
                lua_pushstring(l, cinv_context_get_errormsg(ctx));
@@ -336,30 +417,10 @@
        return ret;
 }
 
-int _ccallback_new(lua_State *l) {
-       // XXX
-       return 0;
-}
-
-struct FunStruct {
-       void *ep;
-       CInvFunction *func;
-       CInvContext *ctx;
-};
-
-int _function_gc(lua_State *l) {
-       struct FunStruct *fs = lua_touserdata(l, 1);
-       if (fs->func) {
-               cinv_function_delete(fs->ctx, fs->func);
-               fs->func = NULL;
-       }
-       return 0;
-}
-
 int get_arrelement_size(lua_State *l, int type) {
        int ret;
        lua_getfield(l, type, "size");
-       ret = lua_tointeger(l, -1);
+       ret = tointeger(l, -1);
        lua_pop(l, 1);
        return ret;
 }
@@ -368,7 +429,7 @@
        const char *pstr;
        if (lua_isnil(l, index))
                return 0;
-       pstr = lua_tostring(l, index);
+       pstr = tostring(l, index);
        if (strlen(pstr) == 0) return 0;
        if (strlen(pstr) == 1 ||
                strncmp("0x", pstr, 2)) {
@@ -403,14 +464,14 @@
        if (lua_isnil(l, argindex))
                *(const char **)ret = NULL;
        else
-               *(const char **)ret = lua_tostring(l, argindex);
+               *(const char **)ret = tostring(l, argindex);
 }
 void marshal_basic(lua_State *l, void *ret, int typeindex, int argindex) {
        cinv_type_t id;
        int isnil;
 
        lua_getfield(l, typeindex, "id");
-       id = (cinv_type_t)lua_tointeger(l, -1);
+       id = (cinv_type_t)tointeger(l, -1);
        lua_pop(l, 1);
 
        isnil = lua_isnil(l, argindex);
@@ -421,25 +482,25 @@
 
        switch (id) {
        case CINV_T_CHAR:
-               *(char*)ret = lua_tostring(l, argindex)[0];
+               *(char*)ret = tostring(l, argindex)[0];
                break;
        case CINV_T_SHORT:
-               *(short*)ret = (short)lua_tointeger(l, argindex);
+               *(short*)ret = (short)tointeger(l, argindex);
                break;
        case CINV_T_INT:
-               *(int*)ret = (int)lua_tointeger(l, argindex);
+               *(int*)ret = (int)tointeger(l, argindex);
                break;
        case CINV_T_LONG:
-               *(long*)ret = (long)lua_tonumber(l, argindex);
+               *(long*)ret = (long)tonumber(l, argindex);
                break;
        case CINV_T_EXTRALONG:
-               *(long long*)ret = (long long)lua_tonumber(l, argindex);
+               *(long long*)ret = (long long)tonumber(l, argindex);
                break;
        case CINV_T_FLOAT:
-               *(float*)ret = (float)lua_tonumber(l, argindex);
+               *(float*)ret = (float)tonumber(l, argindex);
                break;
        case CINV_T_DOUBLE:
-               *(double*)ret = (double)lua_tonumber(l, argindex);
+               *(double*)ret = (double)tonumber(l, argindex);
                break;
        case CINV_T_PTR:
                *(void**)ret = get_ptr_val(l, argindex);
@@ -454,7 +515,7 @@
        int members;
 
        lua_getfield(l, typeindex, "ud");
-       st = lua_touserdata(l, -1);
+       st = touserdata(l, -1);
        lua_pop(l, 1);
 
        lua_getfield(l, typeindex, "members");
@@ -462,7 +523,7 @@
 
        lua_pushnil(l);
        while (lua_next(l, members) != 0) {
-               const char *key = lua_tostring(l, -2);
+               const char *key = tostring(l, -2);
                int type = lua_gettop(l);
                void *val;
 
@@ -558,13 +619,13 @@
        int famindex;
        lua_getfield(l, typeindex, "family");
        famindex = lua_gettop(l);
-       if (!strcmp("basic", lua_tostring(l, famindex))) {
+       if (!strcmp("basic", tostring(l, famindex))) {
                char ptrbuf[16];
                char strbuf[2];
                int idindex;
                lua_getfield(l, typeindex, "id");
                idindex = lua_gettop(l);
-               switch ((cinv_type_t)lua_tointeger(l, idindex)) {
+               switch ((cinv_type_t)tointeger(l, idindex)) {
                case CINV_T_CHAR:
                        strbuf[0] = *(char *)value;
                        strbuf[1] = '\0';
@@ -610,7 +671,7 @@
        ret = lua_gettop(l);
 
        lua_getfield(l, typeindex, "ud");
-       st = lua_touserdata(l, -1);
+       st = touserdata(l, -1);
        lua_pop(l, 1);
 
        lua_getfield(l, typeindex, "members");
@@ -618,7 +679,7 @@
 
        lua_pushnil(l);
        while (lua_next(l, members) != 0) {
-               const char *key = lua_tostring(l, -2);
+               const char *key = tostring(l, -2);
                int type = lua_gettop(l);
                
                void *val = cinv_structure_instance_getvalue(st->ctx, st->st,
@@ -658,6 +719,172 @@
        }
 }
 
+struct CBStruct {
+       CInvCallback *cb;
+       CInvFunction *func;
+       CInvContext *ctx;
+       lua_Integer key;
+       lua_State *l;
+};
+
+int _ccallback_gc(lua_State *l) {
+       struct CBStruct *cb = touserdata(l, 1);
+
+       lua_pushinteger(l, cb->key);
+       lua_pushnil(l);
+       lua_settable(l, LUA_GLOBALSINDEX);
+       
+       if (cb->cb) {
+               cinv_callback_delete(cb->ctx, cb->cb);
+               cb->cb = NULL;
+       }
+       if (cb->func) {
+               cinv_function_delete(cb->ctx, cb->func);
+               cb->func = NULL;
+       }
+
+       return 0;
+}
+
+void _ccallback_invoked(CInvFunction *f, void *parameters[],
+       void *returnout, void *userdata) {
+       struct CBStruct *cb = userdata;
+       int usertable, pindex, index, retindex;
+       int numargs, i;
+
+       lua_pushinteger(cb->l, cb->key);
+       lua_gettable(cb->l, LUA_GLOBALSINDEX);
+       usertable = lua_gettop(cb->l);
+       
+       if (lua_isnil(cb->l, usertable)) {
+               lua_pushstring(cb->l,
+                       "C callback being called for an object which has been 
collected");
+               lua_error(cb->l);
+       }
+       lua_getfield(cb->l, usertable, "cbfunc");
+
+       lua_getfield(cb->l, usertable, "params");
+       pindex = lua_gettop(cb->l);
+       numargs = lua_objlen(cb->l, pindex);
+
+       for (i = 0; i < numargs; i++) {
+               lua_pushinteger(cb->l, i + 1);
+               lua_gettable(cb->l, pindex);
+               index = lua_gettop(cb->l);
+
+               unmarshal_retval(cb->l, index, parameters[i]);
+
+               lua_remove(cb->l, index);
+       }
+       lua_remove(cb->l, pindex);
+
+       lua_call(cb->l, numargs, 1);
+
+       retindex = lua_gettop(cb->l);
+       lua_getfield(cb->l, usertable, "return");
+       index = lua_gettop(cb->l);
+       marshal_basic(cb->l, returnout, index, retindex);
+       lua_remove(cb->l, index);
+
+       lua_pop(cb->l, 2); // return value and usertable
+}
+
+int _clibrary_new_callback(lua_State *l) {
+       struct CBStruct *cbs;
+       struct LibStruct *lib;
+       CInvFunction *func;
+       CInvContext *ctx;
+       CInvCallback *cb;
+       int i;
+       int retval;
+       int numargs = lua_gettop(l);
+       if (numargs < 3) {
+               lua_pushstring(l, "usage: clibrary:new_callback(rettype, 
cbfunc, ...)");
+               lua_error(l);
+       }
+
+       lua_getfield(l, 1, "ud");
+       lib = touserdata(l, -1);
+       lua_pop(l, 1);
+
+       func = parsefunction(l, lib->ctx, 2, 1, lib->cc);
+
+       lua_newtable(l);
+       retval = lua_gettop(l);
+
+       cbs = lua_newuserdata(l, sizeof(struct CBStruct));
+       cbs->func = func;
+       cbs->ctx = ctx;
+       cbs->l = l;
+       
+       cb = cinv_callback_create(ctx, func, cbs, _ccallback_invoked);
+       if (!cb) {
+               lua_pushstring(l, cinv_context_get_errormsg(ctx));
+               cinv_function_delete(lib->ctx, func);
+               lua_error(l);
+       }
+       cbs->cb = cb;
+       cbs->key = time(NULL);
+
+       while (1) {
+               lua_pushinteger(l, cbs->key);
+               lua_gettable(l, LUA_GLOBALSINDEX);
+               if (!lua_isnil(l, -1)) {
+                       lua_pop(l, 1);
+                       cbs->key++;
+                       continue;
+               }
+               lua_pop(l, 1);
+               lua_pushinteger(l, cbs->key);
+               lua_pushvalue(l, retval);
+               lua_settable(l, LUA_GLOBALSINDEX);
+               break;
+       }
+       
+       lua_newtable(l);
+       lua_pushcfunction(l, _ccallback_gc);
+       lua_setfield(l, -2, "__gc");
+       lua_setmetatable(l, -2);
+       lua_setfield(l, -2, "ud");
+
+       lua_pushvalue(l, 1);
+       lua_setfield(l, -2, "return");
+       
+       lua_newtable(l);
+       for (i = 4; i <= numargs; i++) {
+               lua_pushinteger(l, i - 3);
+               lua_pushvalue(l, i);
+               if (isvoid(l, lua_gettop(l)))
+                       lua_pop(l, 2);
+               else
+                       lua_settable(l, -3);
+       }
+       lua_setfield(l, -2, "params");
+
+       lua_pushvalue(l, 3);
+       lua_setfield(l, -2, "cbfunc");
+
+       lua_pushvalue(l, 1);
+       lua_setfield(l, -2, "lib");
+
+       return 1;
+}
+
+struct FunStruct {
+       void *ep;
+       CInvFunction *func;
+       CInvContext *ctx;
+};
+
+int _function_gc(lua_State *l) {
+       struct FunStruct *fs = touserdata(l, 1);
+       if (fs->func) {
+               cinv_function_delete(fs->ctx, fs->func);
+               fs->func = NULL;
+       }
+       return 0;
+}
+
 int _function_call(lua_State *l) {
        struct FunStruct *fs;
        void *returnval = NULL;
@@ -667,7 +894,7 @@
        int index, pindex, i;
 
        lua_getfield(l, 1, "ud");
-       fs = lua_touserdata(l, -1);
+       fs = touserdata(l, -1);
        lua_pop(l, 1);
 
        lua_getfield(l, 1, "return");
@@ -741,11 +968,13 @@
                lua_error(l);
        }
 
-       lib = lua_touserdata(l, 1);
+       lua_getfield(l, 1, "ud");
+       lib = touserdata(l, -1);
+       lua_pop(l, 1);
 
-       func = parsefunction(l, lib->ctx, 2);
+       func = parsefunction(l, lib->ctx, 2, 0, lib->cc);
 
-       ep = cinv_library_load_entrypoint(lib->ctx, lib->lib, lua_tostring(l, 
3));
+       ep = cinv_library_load_entrypoint(lib->ctx, lib->lib, tostring(l, 3));
        if (!ep) {
                lua_pushstring(l, cinv_context_get_errormsg(lib->ctx));
                cinv_function_delete(lib->ctx, func);
@@ -790,6 +1019,74 @@
        return 1;
 }
 
+int _clibrary_tie_init(lua_State *l) {
+       struct LibStruct *lib;
+       void *ep;
+
+       if (lua_gettop(l) != 3) {
+               lua_pushstring(l, "usage: clibrary:tie_init(type, name)");
+               lua_error(l);
+       }
+
+       lua_getfield(l, 1, "ud");
+       lib = touserdata(l, -1);
+       lua_pop(l, 1);
+
+       if (isvoid(l, 2)) {
+               lua_pushstring(l, "void is not a type");
+               lua_error(l);
+       }
+       if (isarray(l, 2)) {
+               lua_pushstring(l, "tied variables cannot be arrays");
+               lua_error(l);
+       }
+       if (!isbasic(l, 2)) {
+               lua_pushstring(l, "tied variables cannot be strings or 
structs");
+               lua_error(l);
+       }
+
+       lua_getfield(l, 1, "ties");
+       lua_getfield(l, -1, tostring(l, 3));
+       if (!lua_isnil(l, -1)) {
+               lua_pushstring(l, "a tied variable with that name already 
exists");
+               lua_error(l);
+       }
+       lua_pop(l, 1);
+
+       ep = cinv_library_load_entrypoint(lib->ctx, lib->lib, tostring(l, 3));
+       if (!ep) {
+               lua_pushstring(l, cinv_context_get_errormsg(lib->ctx));
+               lua_error(l);
+       }
+       
+       lua_newtable(l);
+       lua_pushvalue(l, 2);
+       lua_setfield(l, -2, "type");
+       lua_pushlightuserdata(l, ep);
+       lua_setfield(l, -2, "ep");
+
+       lua_setfield(l, -2, lua_tostring(l, 3));
+       lua_pop(l, 1);
+
+       return 0;
+}
+int _clibrary_tie_get(lua_State *l) {
+       if (lua_gettop(l) != 2) {
+               lua_pushstring(l, "usage: clibrary:tie_get(name)");
+               lua_error(l);
+       }
+       // XXX
+       return 1;
+}
+int _clibrary_tie_set(lua_State *l) {
+       if (lua_gettop(l) != 3) {
+               lua_pushstring(l, "usage: clibrary:tie_set(name, value)");
+               lua_error(l);
+       }
+       // XXX
+       return 0;
+}
+
 void declbasic(lua_State *l, const char *g,
        cinv_type_t id, char charid, int size) {
        lua_newtable(l);
@@ -816,7 +1113,7 @@
                lua_pushstring(l, "usage: cinv.string_to_chararray(string)");
                lua_error(l);
        }
-       s = lua_tolstring(l, 1, &len);
+       s = tolstring(l, 1, &len);
        
        lua_newtable(l);
        for (i = 0; i <= len; i++) {
@@ -852,7 +1149,7 @@
                                lua_pop(l, 1);
                                break;
                        }
-                       ret[i] = lua_tostring(l, -1)[0];
+                       ret[i] = tostring(l, -1)[0];
                        lua_pop(l, 1);
                }
 
@@ -872,8 +1169,8 @@
                lua_error(l);
        }
        ptr = get_ptr_val(l, 1);
-       if (!lua_isnil(l, 2)) {
-               len = lua_tointeger(l, 2);
+       if (!lua_gettop(l) == 2) {
+               len = tointeger(l, 2);
                if (len < 0) {
                        lua_pushstring(l, "invalid length parameter");
                        lua_error(l);
@@ -899,7 +1196,7 @@
        if (value == NULL)
                lua_pushnil(l);
        else {
-               len = lua_tointeger(l, 3);
+               len = tointeger(l, 3);
                // create table of desired len and call unmarshal_array
                lua_newtable(l);
                index = lua_gettop(l);
@@ -953,7 +1250,7 @@
                lua_error(l);
        }
 
-       num = lua_tointeger(l, 2);
+       num = tointeger(l, 2);
        ptr = get_ptr_val(l, 1);
 
        ptr += num;
@@ -976,7 +1273,7 @@
        }
        lua_pop(l, 1);
        lua_getfield(l, 1, "family");
-       if (!strcmp(lua_tostring(l, -1), "void")) {
+       if (!strcmp(tostring(l, -1), "void")) {
                lua_pushstring(l, "void is not a type");
                lua_error(l);
        }
@@ -1037,14 +1334,14 @@
        lua_newtable(l);
        declfunc(l, "new", _clibrary_new);
        declfunc(l, "get_function", _clibrary_get_function);
+       declfunc(l, "new_callback", _clibrary_new_callback);
        declfunc(l, "dispose", _clibrary_dispose);
+       declfunc(l, "tie_init", _clibrary_tie_init);
+       declfunc(l, "tie_get", _clibrary_tie_get);
+       declfunc(l, "tie_set", _clibrary_tie_set);
        lua_setglobal(l, "clibrary");
 
        lua_newtable(l);
-       declfunc(l, "new", _ccallback_new);
-       lua_setglobal(l, "ccallback");
-       
-       lua_newtable(l);
        declfunc(l, "new", _cstructure_new);
        lua_setglobal(l, "cstructure");
        





reply via email to

[Prev in Thread] Current Thread [Next in Thread]