cinvoke-svn
[Top][All Lists]
Advanced

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

[cinvoke-svn] r39 - in trunk/cinvoke: . bindings/lua lib test


From: will
Subject: [cinvoke-svn] r39 - in trunk/cinvoke: . bindings/lua lib test
Date: 23 Jun 2006 23:27:53 -0400

Author: will
Date: 2006-06-23 23:27:53 -0400 (Fri, 23 Jun 2006)
New Revision: 39

Added:
   trunk/cinvoke/TODO
Modified:
   trunk/cinvoke/Makefile
   trunk/cinvoke/bindings/lua/cinvoke_lua.c
   trunk/cinvoke/lib/cinvoke.h
   trunk/cinvoke/lib/structure.c
   trunk/cinvoke/test/Makefile
   trunk/cinvoke/test/runtests.c
Log:
changed struct api a bit, more lua binding implementation,
return of the TODO file


Modified: trunk/cinvoke/Makefile
===================================================================
--- trunk/cinvoke/Makefile      2006-06-23 04:39:48 UTC (rev 38)
+++ trunk/cinvoke/Makefile      2006-06-24 03:27:53 UTC (rev 39)
@@ -7,7 +7,7 @@
        cd lib && $(MAKE) BUILDARCH=-DARCH_GCC_X86_UNIX
        cd test && $(MAKE) BUILDARCH=-DARCH_GCC_X86_UNIX
 
-install: all
+install:
        install lib/cinvoke.h lib/cinvoke-arch.h $(PREFIX)/include
        install lib/arch/gcc_x86_unix.h $(PREFIX)/include/cinvoke-archspec.h
        install lib/libcinvoke.a $(PREFIX)/lib

Added: trunk/cinvoke/TODO
===================================================================
--- trunk/cinvoke/TODO                          (rev 0)
+++ trunk/cinvoke/TODO  2006-06-24 03:27:53 UTC (rev 39)
@@ -0,0 +1,6 @@
+lua callbacks
+investigate tied variables, integrate with lua
+amd64 port
+ppc port
+java binding
+website

Modified: trunk/cinvoke/bindings/lua/cinvoke_lua.c
===================================================================
--- trunk/cinvoke/bindings/lua/cinvoke_lua.c    2006-06-23 04:39:48 UTC (rev 38)
+++ trunk/cinvoke/bindings/lua/cinvoke_lua.c    2006-06-24 03:27:53 UTC (rev 39)
@@ -66,24 +66,6 @@
        return 0;
 }
 
-// dont need explicit dispose methods for structures, callbacks or functions
-struct StrStruct {
-       CInvContext *ctx;
-       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;
-       }
-       if (st->ctx) {
-               cinv_context_delete(st->ctx);
-               st->ctx = NULL;
-       }
-       return 0;
-}
-
 int isvoid(lua_State *l, int index) {
        int ret;
        lua_getfield(l, index, "family");
@@ -114,6 +96,24 @@
        return ret;
 }
 
+// dont need explicit dispose methods for structures, callbacks or functions
+struct StrStruct {
+       CInvContext *ctx;
+       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;
+       }
+       if (st->ctx) {
+               cinv_context_delete(st->ctx);
+               st->ctx = NULL;
+       }
+       return 0;
+}
+
 int _cstructure_new(lua_State *l) {
        struct StrStruct *ptr;
        CInvContext *ctx;
@@ -399,9 +399,6 @@
        }
        return ret;
 }
-void marshal_struct(lua_State *l, void *ret, int typeindex, int argindex) {
-       // XXX
-}
 void marshal_string(lua_State *l, void *ret, int argindex) {
        if (lua_isnil(l, argindex))
                *(const char **)ret = NULL;
@@ -452,6 +449,49 @@
                lua_error(l);
        }
 }
+void marshal_struct(lua_State *l, void *ret, int typeindex, int argindex) {
+       struct StrStruct *st;
+       int members;
+
+       lua_getfield(l, typeindex, "ud");
+       st = lua_touserdata(l, -1);
+       lua_pop(l, 1);
+
+       lua_getfield(l, typeindex, "members");
+       members = lua_gettop(l);
+
+       lua_pushnil(l);
+       while (lua_next(l, members) != 0) {
+               const char *key = lua_tostring(l, -2);
+               int type = lua_gettop(l);
+               void *val;
+
+               val = malloc(get_arrelement_size(l, type));
+               if (!val) {
+                       lua_pushstring(l, "out of memory");
+                       lua_error(l);
+               }
+
+               lua_getfield(l, argindex, key);
+               if (isstruct(l, type))
+                       marshal_struct(l, val, type, lua_gettop(l));
+               if (isstring(l, type))
+                       marshal_string(l, val, lua_gettop(l));
+               else
+                       marshal_basic(l, val, type, lua_gettop(l));
+
+               if (!cinv_structure_instance_setvalue(st->ctx, st->st,
+                       ret, key, val)) {
+                       free(val);
+                       lua_pushstring(l, cinv_context_get_errormsg(st->ctx));
+                       lua_error(l);
+               }
+               free(val);
+               lua_pop(l, 2); // pop the value and field
+       }
+
+       lua_pop(l, 1);
+}
 void *marshal_param(lua_State *l, int typeindex, int argindex) {
        void *ret = NULL;
        if (isarray(l, typeindex)) {
@@ -560,9 +600,42 @@
        lua_remove(l, famindex);
 }
 void unmarshal_struct(lua_State *l, int typeindex, void *instance) {
+       struct StrStruct *st;
+       int members, ret;
+
        lua_newtable(l);
+       ret = lua_gettop(l);
 
-       // XXX set values
+       lua_getfield(l, typeindex, "ud");
+       st = lua_touserdata(l, -1);
+       lua_pop(l, 1);
+
+       lua_getfield(l, typeindex, "members");
+       members = lua_gettop(l);
+
+       lua_pushnil(l);
+       while (lua_next(l, members) != 0) {
+               const char *key = lua_tostring(l, -2);
+               int type = lua_gettop(l);
+               
+               void *val = cinv_structure_instance_getvalue(st->ctx, st->st,
+                       instance, key);
+               if (val == NULL) {
+                       lua_pushstring(l, cinv_context_get_errormsg(st->ctx));
+                       lua_error(l);
+               }
+
+               if (isstruct(l, type))
+                       unmarshal_struct(l, type, val);
+               else
+                       unmarshal_retval(l, type, val);
+
+               lua_setfield(l, ret, key);
+
+               lua_pop(l, 1); // pop the value
+       }
+
+       lua_remove(l, members);
 }
 void unmarshal_array(lua_State *l, int typeindex, void *value, int outindex) {
        char *cptr = (char *)value;

Modified: trunk/cinvoke/lib/cinvoke.h
===================================================================
--- trunk/cinvoke/lib/cinvoke.h 2006-06-23 04:39:48 UTC (rev 38)
+++ trunk/cinvoke/lib/cinvoke.h 2006-06-24 03:27:53 UTC (rev 39)
@@ -315,18 +315,23 @@
 */
 cinv_status_t cinv_structure_finish(CInvContext *context,
        CInvStructure *structure);
+/** Returns the size, in bytes, of a structure.
+* \param[in] context A C/Invoke context.
+* \param[in] structure The structure description.
+* \return A standard C/Invoke status code.
+*/
+cinv_status_t cinv_structure_get_size(CInvContext *context,
+       CInvStructure *structure, int *size_out);
 /** Allocates memory for a structure instance.
 * \param[in] context A C/Invoke context.
 * \param[in] structure The structure description to allocate memory for.
+* \param[out] size_out The number of bytes required to hold the structure
+* in memory.
 * \return A pointer to memory which can hold the given structure.
 */
 void *cinv_structure_create_instance(CInvContext *context,
        CInvStructure *structure);
-/** Sets the value of a member inside of a structure.  Note that the
-* semantics for setting the value of an embedded structure member
-* may be confusing; you should allocate a new structure instance
-* pointer with cinv_structure_create_instance, then pass a pointer
-* to this pointer as the value_ptr parameter.
+/** Sets the value of a member inside of a structure.  
 * \param[in] context A C/Invoke context.
 * \param[in] structure The structure description corresponding to
 * the given instance.
@@ -334,43 +339,31 @@
 * value of.
 * \param[in] name The name of the member to set.
 * \param[in] value_ptr A pointer to the value to set.  If the value
-* being set is a pointer, you should pass a pointer to a pointer.
+* being set is a pointer, you should pass a pointer to a pointer. If
+* the value being set is an embedded structure, create a structure
+* instance with cinv_structure_create_instance and pass the instance
+* pointer.
 * \return A standard C/Invoke status code.
 */
 cinv_status_t cinv_structure_instance_setvalue(CInvContext *context,
        CInvStructure *structure, void *instance, const char *name,
        void *value_ptr);
-/** Gets the value of a member inside of a structure.  Note that the
-* semantics for gettings the value of an embedded structure member
-* may be confusing; you should allocate a new structure instance
-* pointer with cinv_structure_create_instance, then pass a pointer
-* to this pointer as the value_out parameter.
+/** Gets a pointer to the value of a member inside of a structure.  
 * \param[in] context A C/Invoke context.
 * \param[in] structure The structure description corresponding to
 * the given instance.
 * \param[in] instance The structure instance to get the member
 * value from.
 * \param[in] name The name of the member to get.
-* \param[out] value_out A pointer to memory which will hold the
-* returned value.  If the value being retrieved is a pointer, you
-* should pass a pointer to a buffer big enough to hold a pointer. In
-* other words, this is \b wrong:
-* \code
-* void *ptr;
-* cinv_structure_instance_getvalue(ctx, strct, inst, "my_ptr_member",
-*      ptr);
-* \endcode
-* This is correct:
-* \code
-* void *ptr;
-* cinv_structure_instance_getvalue(ctx, strct, inst, "my_ptr_member",
-*      &ptr);
-* \endcode
-* \return A standard C/Invoke status code.
+* \return A pointer to the value, or NULL if an error occurred.
+* If the value being returned is a pointer type, this will be a
+* pointer to that pointer. If the value being returned is an
+* embedded structure, this will be an instance pointer.
+* In all cases, the pointer being returned is internal to the
+* structure instance and does not need to be deallocated separately.
 */
-cinv_status_t cinv_structure_instance_getvalue(CInvContext *context,
-       CInvStructure *structure, void *instance, const char *name,
-       void *value_out);
+void *cinv_structure_instance_getvalue(CInvContext *context,
+       CInvStructure *structure, void *instance, const char *name);
 /** Frees the memory associated with a structure instance.
 * \param[in] context A C/Invoke context.
 * \param[in] instance The structure instance to delete.

Modified: trunk/cinvoke/lib/structure.c
===================================================================
--- trunk/cinvoke/lib/structure.c       2006-06-23 04:39:48 UTC (rev 38)
+++ trunk/cinvoke/lib/structure.c       2006-06-24 03:27:53 UTC (rev 39)
@@ -170,6 +170,18 @@
        context_clear_error(context);
        return CINV_SUCCESS;
 }
+cinv_status_t cinv_structure_get_size(CInvContext *context,
+       CInvStructure *structure, int *size_out) {
+       if (!structure->finished) {
+               context_set_error(context, CINV_E_INVAL,
+                       "the structure is not finished", 0);
+               return CINV_ERROR;
+
+       }
+       *size_out = structure->nextoffset;
+       context_clear_error(context);
+       return CINV_SUCCESS;
+}
 void *cinv_structure_create_instance(CInvContext *context,
        CInvStructure *structure) {
        void *r;
@@ -224,33 +236,24 @@
 
        ptr += offset;
        
-       if (is_struct)
-               memcpy(ptr, *(void **)value_ptr, sz);
-       else
-               memcpy(ptr, value_ptr, sz);
+       memcpy(ptr, value_ptr, sz);
 
        context_clear_error(context);
        return CINV_SUCCESS;
 }
-cinv_status_t cinv_structure_instance_getvalue(CInvContext *context,
-       CInvStructure *structure, void *instance, const char *name,
-       void *value_out) {
+void *cinv_structure_instance_getvalue(CInvContext *context,
+       CInvStructure *structure, void *instance, const char *name) {
        int sz, offset, is_struct;
        char *ptr = instance;
 
        offset = get_member(context, structure, name, &sz, &is_struct);
        if (offset == -1)
-               return CINV_ERROR;
+               return NULL;
 
        ptr += offset;
        
-       if (is_struct)
-               memcpy(*(void **)value_out, ptr, sz);
-       else
-               memcpy(value_out, ptr, sz);
-
        context_clear_error(context);
-       return CINV_SUCCESS;
+       return ptr;
 }
 cinv_status_t cinv_structure_delete_instance(CInvContext *context,
        void *instance) {

Modified: trunk/cinvoke/test/Makefile
===================================================================
--- trunk/cinvoke/test/Makefile 2006-06-23 04:39:48 UTC (rev 38)
+++ trunk/cinvoke/test/Makefile 2006-06-24 03:27:53 UTC (rev 39)
@@ -6,5 +6,5 @@
 lib.so: lib.c
        gcc -g -shared -fPIC lib.c -o lib.so -Wall -Werror
 
-runtests: runtests.c lib.so
+runtests: runtests.c lib.so ../lib/libcinvoke.a
        gcc -g -DCINVOKE_BUILD $(BUILDARCH) -o runtests runtests.c -Wall 
-Werror -L../lib -I../lib `sh ../tools/libdl.sh` -lcinvoke -lm

Modified: trunk/cinvoke/test/runtests.c
===================================================================
--- trunk/cinvoke/test/runtests.c       2006-06-23 04:39:48 UTC (rev 38)
+++ trunk/cinvoke/test/runtests.c       2006-06-24 03:27:53 UTC (rev 39)
@@ -189,7 +189,7 @@
                cinv_structure_instance_setvalue(ctx, ts, tsinst, "d1", &d1);
                cinv_structure_instance_setvalue(ctx, ts, tsinst, "c2", &c2);
 
-               cinv_structure_instance_setvalue(ctx, ts2, inst, "ts1", 
&tsinst);
+               cinv_structure_instance_setvalue(ctx, ts2, inst, "ts1", tsinst);
                cinv_structure_delete_instance(ctx, tsinst);
        }
 
@@ -235,20 +235,18 @@
        inst = &rts2;
        
        {
-               char c1;
-               int i1;
-               short s1;
-               void *tsinst = cinv_structure_create_instance(ctx, ts);
-               double d1;
-               char c2;
+               char c1 = 
+                       *(char *)cinv_structure_instance_getvalue(ctx, ts2, 
inst, "c1");
+               int i1 = 
+                       *(int *)cinv_structure_instance_getvalue(ctx, ts2, 
inst, "i1");
+               short s1 =
+                       *(short *)cinv_structure_instance_getvalue(ctx, ts2, 
inst, "s1");
+               void *tsinst = cinv_structure_instance_getvalue(ctx, ts2, inst, 
"ts1");
+               double d1 =
+                       *(double *)cinv_structure_instance_getvalue(ctx, ts, 
tsinst, "d1");
+               char c2 =
+                       *(char *)cinv_structure_instance_getvalue(ctx, ts, 
tsinst, "c2");
 
-               cinv_structure_instance_getvalue(ctx, ts2, inst, "c1", &c1);
-               cinv_structure_instance_getvalue(ctx, ts2, inst, "i1", &i1);
-               cinv_structure_instance_getvalue(ctx, ts2, inst, "s1", &s1);
-               cinv_structure_instance_getvalue(ctx, ts2, inst, "ts1", 
&tsinst);
-               cinv_structure_instance_getvalue(ctx, ts, tsinst, "d1", &d1);
-               cinv_structure_instance_getvalue(ctx, ts, tsinst, "c2", &c2);
-
                if (c1 != 'A' ||
                        i1 != 55 ||
                        s1 != 44 ||





reply via email to

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