cinvoke-svn
[Top][All Lists]
Advanced

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

[cinvoke-svn] r36 - trunk/cinvoke/bindings/lua


From: will
Subject: [cinvoke-svn] r36 - trunk/cinvoke/bindings/lua
Date: 21 Jun 2006 01:18:24 -0400

Author: will
Date: 2006-06-21 01:18:24 -0400 (Wed, 21 Jun 2006)
New Revision: 36

Modified:
   trunk/cinvoke/bindings/lua/cinvoke_lua.c
Log:
some progress on lua binding


Modified: trunk/cinvoke/bindings/lua/cinvoke_lua.c
===================================================================
--- trunk/cinvoke/bindings/lua/cinvoke_lua.c    2006-06-19 02:41:45 UTC (rev 35)
+++ trunk/cinvoke/bindings/lua/cinvoke_lua.c    2006-06-21 05:18:24 UTC (rev 36)
@@ -4,7 +4,8 @@
 #include <stdio.h>
 #include <string.h>
 
-void declbasic(lua_State *l, const char *g, cinv_type_t id, char charid) {
+void declbasic(lua_State *l, const char *g,
+       cinv_type_t id, char charid, int size) {
        lua_newtable(l);
        lua_pushstring(l, "basic");
        lua_setfield(l, -2, "family");
@@ -12,6 +13,8 @@
        lua_setfield(l, -2, "id");
        lua_pushinteger(l, (lua_Integer)charid);
        lua_setfield(l, -2, "charid");
+       lua_pushinteger(l, (lua_Integer)size);
+       lua_setfield(l, -2, "size");
        lua_setglobal(l, g);
 }
 
@@ -22,17 +25,16 @@
 
 int _cinv_chararray_to_string(lua_State *l) {
        size_t len, i = 0;
-       char *ret;
        if (lua_gettop(l) != 0) {
                lua_pushstring(l, "usage: cinv.chararray_to_string(carray)");
                lua_error(l);
        }
        len = lua_objlen(l, 1);
 
-       if (len == 0)
-               ret = "";
-       else {
-               ret = malloc(len + 1);
+       if (len == 0) {
+               lua_pushlstring(l, "", len);
+       } else {
+               char *ret = malloc(len + 1);
                if (!ret) {
                        lua_pushstring(l, "out of memory");
                        lua_error(l);
@@ -48,52 +50,42 @@
                }
 
                ret[len] = '\0';
+               lua_pushlstring(l, ret, len);
+               free(ret);
        }
        
-       lua_pushlstring(l, ret, len);
        return 1;
 }
-int _cinv_out(lua_State *l) {
+int _cinv_array(lua_State *l) {
        if (lua_gettop(l) != 1) {
-               lua_pushstring(l, "usage: cinv.out(ptype)");
+               lua_pushstring(l, "usage: cinv.array(arrtype)");
                lua_error(l);
        }
        
-       lua_getfield(l, 1, "family");
-       if (!strcmp(lua_tostring(l, -1), "void")) {
-               lua_pushstring(l, "void is not a type");
+       lua_getfield(l, 1, "array");
+       if (!lua_isnil(l, -1)) {
+               lua_pushstring(l, "arrays of arrays not supported");
                lua_error(l);
        }
        lua_pop(l, 1);
-       
-       lua_newtable(l);
-       lua_pushnil(l);
-       while (lua_next(l, 1) != 0) {
-               lua_settable(l, -3);
-       }
-
-       lua_pushstring(l, "yes");
-       lua_setfield(l, -2, "out");
-
-       return 1;
-}
-int _cinv_array(lua_State *l) {
-       if (lua_gettop(l) != 1) {
-               lua_pushstring(l, "usage: cinv.array(arrtype)");
-               lua_error(l);
-       }
-       
        lua_getfield(l, 1, "family");
        if (!strcmp(lua_tostring(l, -1), "void")) {
                lua_pushstring(l, "void is not a type");
                lua_error(l);
        }
+       if (!strcmp(lua_tostring(l, -1), "struct")) {
+               lua_pushstring(l, "arrays of structs not supported");
+               lua_error(l);
+       }
        lua_pop(l, 1);
        
        lua_newtable(l);
        lua_pushnil(l);
        while (lua_next(l, 1) != 0) {
-               lua_settable(l, -3);
+               lua_pushvalue(l, -2);
+               lua_pushvalue(l, -2);
+               lua_settable(l, -5);
+               lua_pop(l, 1);
        }
 
        lua_pushstring(l, "yes");
@@ -210,12 +202,11 @@
 
        for (i = 1; i <= numargs; i += 2) {
                const char *family;
+               int isarray = 0;
 
-               lua_getfield(l, i, "out");
-               if (!lua_isnil(l, -1)) {
-                       lua_pushstring(l, "out modifier not valid for struct 
members");
-                       goto error;
-               }
+               lua_getfield(l, i, "array");
+               if (!lua_isnil(l, -1))
+                       isarray = 1;
                lua_pop(l, 1);
 
                lua_getfield(l, i, "family");
@@ -223,7 +214,7 @@
                if (!strcmp(family, "void")) {
                        lua_pushstring(l, "void is not a type");
                        goto error;
-               } else if (!strcmp(family, "string")) {
+               } else if (isarray || !strcmp(family, "string")) {
                        if (!cinv_structure_addmember_value(ctx, st,
                                lua_tostring(l, i + 1), CINV_T_PTR)) {
                                lua_pushstring(l, 
cinv_context_get_errormsg(ctx));
@@ -293,13 +284,6 @@
 }
 
 char getcode(lua_State *l, const char *family, int tblindex) {
-       lua_getfield(l, tblindex, "out");
-       if (!lua_isnil(l, -1)) {
-               lua_pop(l, 1);
-               return 'p';
-       }
-       lua_pop(l, 1);
-
        lua_getfield(l, tblindex, "array");
        if (!lua_isnil(l, -1)) {
                lua_pop(l, 1);
@@ -332,9 +316,9 @@
        int numparms = lua_gettop(l) - startarg - 1;
        const char *family;
 
-       lua_getfield(l, startarg, "out");
+       lua_getfield(l, startarg, "array");
        if (!lua_isnil(l, -1)) {
-               lua_pushstring(l, "out modifier not valid for return type");
+               lua_pushstring(l, "returning arrays not supported");
                lua_error(l);
        }
        lua_pop(l, 1);
@@ -408,11 +392,128 @@
        return 0;
 }
 
-int _function_call(lua_State *l) {
+int isvoid(lua_State *l, int tblindex) {
+       int ret;
+       lua_getfield(l, tblindex, "family");
+       ret = !strcmp("void", lua_tostring(l, -1));
+       lua_pop(l, 1);
+       return ret;
+}
+
+void *allocreturn(lua_State *l, int typeindex) {
+       int sz, isarray = 0;
+       void *ret;
+       lua_getfield(l, typeindex, "array");
+       if (!lua_isnil(l, -1))
+               isarray = 1;
+       lua_pop(l, 1);
+
+       lua_getfield(l, typeindex, "family");
+       if (isarray || !strcmp(lua_tostring(l, -1), "string")) {
+               sz = sizeof(void*);
+       } else { // basic
+               lua_getfield(l, typeindex, "size");
+               sz = lua_tointeger(l, -1);
+               lua_pop(l, 1);
+       }
+       lua_pop(l, 1);
+
+       ret = malloc(sz);
+       if (!ret) {
+               lua_pushstring(l, "out of memory");
+               lua_error(l);
+       }
+       return ret;
+}
+void *marshal(lua_State *l, int typeindex, int argindex) {
        // XXX
-       return 0;
+       return NULL;
 }
+void unmarshal_retval(lua_State *l, int typeindex, void *value) {
+       // XXX can be basic, string, or struct
+       // push completed value on stack
+}
+void unmarshal_array(lua_State *l, int typeindex, void *value, int outindex) {
+       // XXX
+}
+void unmarshal_struct(lua_State *l, int typeindex, void *value, int outindex) {
+       // XXX
+}
 
+int _function_call(lua_State *l) {
+       struct FunStruct *fs;
+       void *returnval = NULL;
+       void **parameters = NULL;
+       int numargs = 0;
+       int numreturn = 0;
+       int index, pindex, i;
+
+       lua_getfield(l, 1, "ud");
+       fs = lua_touserdata(l, -1);
+
+       lua_getfield(l, 1, "return");
+       index = lua_gettop(l);
+       if (!isvoid(l, index))
+               returnval = allocreturn(l, index);
+       lua_remove(l, index);
+
+       lua_getfield(l, 1, "params");
+       pindex = lua_gettop(l);
+       numargs = lua_objlen(l, pindex);
+       if (numargs > 0) {
+               parameters = malloc(numargs * sizeof(void*));
+               for (i = 0; i < numargs; i++) {
+                       lua_pushinteger(l, i + 1);
+                       lua_gettable(l, pindex);
+
+                       index = lua_gettop(l);
+                       
+                       parameters[i] = marshal(l, index, i + 2);
+
+                       lua_remove(l, index);
+               }
+       }
+       lua_remove(l, pindex);
+
+       cinv_function_invoke(fs->ctx, fs->func, fs->ep,
+               returnval, parameters);
+       
+       lua_getfield(l, 1, "params");
+       pindex = lua_gettop(l);
+       for (i = 0; i < numargs; i++) {
+               lua_pushinteger(l, i + 1);
+               lua_gettable(l, pindex);
+
+               index = lua_gettop(l);
+               lua_getfield(l, index, "array");
+               if (!lua_isnil(l, -1))
+                       unmarshal_array(l, index, parameters[i], i + 2);
+               lua_pop(l, 1);
+               lua_getfield(l, index, "family");
+               if (!strcmp("struct", lua_tostring(l, -1)))
+                       unmarshal_struct(l, index, parameters[i], i + 2);
+               lua_pop(l, 1);
+
+               lua_remove(l, index);
+       }
+       lua_remove(l, pindex);
+
+       if (returnval != NULL) {
+               lua_getfield(l, 1, "return");
+               index = lua_gettop(l);
+               unmarshal_retval(l, index, returnval);
+               numreturn++;
+               lua_remove(l, index);
+       }
+
+       for (i = 0; i < numargs; i++)
+               free(parameters[i]);
+       free(parameters);
+       free(returnval);
+
+       return numreturn;
+}
+
 int _clibrary_get_function(lua_State *l) {
        struct FunStruct *fs;
        struct LibStruct *lib;
@@ -460,7 +561,10 @@
        for (i = 4; i <= numargs; i++) {
                lua_pushinteger(l, i - 3);
                lua_pushvalue(l, i);
-               lua_settable(l, -3);
+               if (isvoid(l, lua_gettop(l)))
+                       lua_pop(l, 2);
+               else
+                       lua_settable(l, -3);
        }
        lua_setfield(l, -2, "params");
 
@@ -472,17 +576,17 @@
 }
 
 int luaopen_cinvoke_lua(lua_State *l) {
-       declbasic(l, "Cchar", CINV_T_CHAR, 'c');
-       declbasic(l, "Cshort", CINV_T_SHORT, 's');
-       declbasic(l, "Cint", CINV_T_INT, 'i');
-       declbasic(l, "Clong", CINV_T_LONG, 'l');
-       declbasic(l, "Clonglong", CINV_T_EXTRALONG, 'e');
-       declbasic(l, "Cfloat", CINV_T_FLOAT, 'f');
-       declbasic(l, "Cdouble", CINV_T_DOUBLE, 'd');
-       declbasic(l, "Cptr", CINV_T_PTR, 'p');
-       declbasic(l, "Cint16", CINV_T_2BYTE, '2');
-       declbasic(l, "Cint32", CINV_T_4BYTE, '4');
-       declbasic(l, "Cint64", CINV_T_8BYTE, '8');
+       declbasic(l, "Cchar", CINV_T_CHAR, 'c', 1);
+       declbasic(l, "Cshort", CINV_T_SHORT, 's', sizeof(short));
+       declbasic(l, "Cint", CINV_T_INT, 'i', sizeof(int));
+       declbasic(l, "Clong", CINV_T_LONG, 'l', sizeof(long));
+       declbasic(l, "Clonglong", CINV_T_EXTRALONG, 'e', sizeof(long long));
+       declbasic(l, "Cfloat", CINV_T_FLOAT, 'f', sizeof(float));
+       declbasic(l, "Cdouble", CINV_T_DOUBLE, 'd', sizeof(double));
+       declbasic(l, "Cptr", CINV_T_PTR, 'p', sizeof(void*));
+       declbasic(l, "Cint16", CINV_T_2BYTE, '2', 2);
+       declbasic(l, "Cint32", CINV_T_4BYTE, '4', 4);
+       declbasic(l, "Cint64", CINV_T_8BYTE, '8', 8);
 
        lua_newtable(l);
        lua_pushstring(l, "string");
@@ -495,7 +599,6 @@
        lua_setglobal(l, "Cvoid");
 
        lua_newtable(l);
-       declfunc(l, "out", _cinv_out);
        declfunc(l, "array", _cinv_array);
        declfunc(l, "chararray_to_string", _cinv_chararray_to_string);
        lua_setglobal(l, "cinv");





reply via email to

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