[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cinvoke-svn] r50 - in trunk/cinvoke: . bindings/lua
From: |
will |
Subject: |
[cinvoke-svn] r50 - in trunk/cinvoke: . bindings/lua |
Date: |
26 Jun 2006 23:32:28 -0400 |
Author: will
Date: 2006-06-26 23:32:28 -0400 (Mon, 26 Jun 2006)
New Revision: 50
Modified:
trunk/cinvoke/TODO.txt
trunk/cinvoke/bindings/lua/cinvoke_lua.c
trunk/cinvoke/bindings/lua/test.lua
Log:
some lua fixes
Modified: trunk/cinvoke/TODO.txt
===================================================================
--- trunk/cinvoke/TODO.txt 2006-06-26 04:18:29 UTC (rev 49)
+++ trunk/cinvoke/TODO.txt 2006-06-27 03:32:28 UTC (rev 50)
@@ -1,3 +1,4 @@
+test lua structs/callbacks
finish amd64 port
ppc port
java binding
Modified: trunk/cinvoke/bindings/lua/cinvoke_lua.c
===================================================================
--- trunk/cinvoke/bindings/lua/cinvoke_lua.c 2006-06-26 04:18:29 UTC (rev 49)
+++ trunk/cinvoke/bindings/lua/cinvoke_lua.c 2006-06-27 03:32:28 UTC (rev 50)
@@ -43,6 +43,11 @@
expected(l, "number", index);
return lua_tonumber(l, index);
}
+int toboolean(lua_State *l, int index) {
+ if (lua_type(l, index) != LUA_TBOOLEAN)
+ expected(l, "boolean", index);
+ return lua_toboolean(l, index);
+}
struct LibStruct {
CInvContext *ctx;
@@ -228,7 +233,7 @@
numargs = lua_gettop(l);
if ((numargs % 2) != 0) {
lua_pushstring(l,
- "usage: cstructure.new(type, name, [type, name], ...");
+ "usage: cstructure.new(type, name[, type, name ...]");
goto error;
}
@@ -559,6 +564,11 @@
struct StrStruct *st;
int members;
+ if (lua_isnil(l, argindex)) {
+ lua_pushstring(l, "structs cannot be nil");
+ lua_error(l);
+ }
+
lua_getfield(l, typeindex, "ud");
st = touserdata(l, -1);
lua_pop(l, 1);
@@ -603,37 +613,41 @@
if (isarray(l, typeindex)) {
char **ptr = malloc(sizeof(char *));
if (ptr) {
- int arrlen = lua_objlen(l, argindex);
- int elsize = get_arrelement_size(l, typeindex);
- *ptr = malloc(arrlen * elsize);
- if (*ptr) {
- int i;
- char *tmp = *ptr;
+ if (lua_isnil(l, argindex))
+ *ptr = NULL;
+ else {
+ int arrlen = lua_objlen(l, argindex);
+ int elsize = get_arrelement_size(l, typeindex);
+ *ptr = malloc(arrlen * elsize);
+ if (*ptr) {
+ int i;
+ char *tmp = *ptr;
- for (i = 1; i <= arrlen; i++) {
- lua_pushinteger(l, i);
- lua_gettable(l, argindex);
+ for (i = 1; i <= arrlen; i++) {
+ lua_pushinteger(l, i);
+ lua_gettable(l, argindex);
- if (lua_isnil(l, -1)) {
- lua_pop(l, 1);
- break;
- }
+ if (lua_isnil(l, -1)) {
+ lua_pop(l, 1);
+ break;
+ }
- if (isstruct(l, typeindex))
- marshal_struct(l, tmp,
typeindex, lua_gettop(l));
- else if (isstring(l, typeindex))
- marshal_string(l, tmp,
lua_gettop(l));
- else
- marshal_basic(l, tmp,
typeindex, lua_gettop(l));
+ if (isstruct(l, typeindex))
+ marshal_struct(l, tmp,
typeindex, lua_gettop(l));
+ else if (isstring(l, typeindex))
+ marshal_string(l, tmp,
lua_gettop(l));
+ else
+ marshal_basic(l, tmp,
typeindex, lua_gettop(l));
- lua_pop(l, 1);
+ lua_pop(l, 1);
- tmp += elsize;
- }
+ tmp += elsize;
+ }
- ret = ptr;
- } else
- free(ptr);
+ ret = ptr;
+ } else
+ free(ptr);
+ }
}
} else {
ret = malloc(get_arrelement_size(l, typeindex));
@@ -747,7 +761,7 @@
lua_remove(l, members);
}
void unmarshal_array(lua_State *l, int typeindex, void *value, int outindex) {
- char *cptr = (char *)value;
+ char *cptr = *(char **)value;
int arrlen = lua_objlen(l, outindex), i;
int elsize = get_arrelement_size(l, typeindex);
@@ -1220,16 +1234,24 @@
}
int _cinv_string_to_chararray(lua_State *l) {
- size_t len, i;
+ size_t len, i, end;
+ int includenil = 1;
const char *s;
- if (lua_gettop(l) != 1) {
- lua_pushstring(l, "usage: cinv.string_to_chararray(string)");
+ if (lua_gettop(l) < 1 || lua_gettop(l) > 2) {
+ lua_pushstring(l,
+ "usage: cinv.string_to_chararray(string[,
includenil])");
lua_error(l);
}
+ if (lua_gettop(l) == 2)
+ includenil = toboolean(l, 2);
+
s = tolstring(l, 1, &len);
lua_newtable(l);
- for (i = 0; i <= len; i++) {
+ end = len + 1;
+ if (!includenil)
+ end = len;
+ for (i = 0; i < len; i++) {
lua_pushinteger(l, i + 1);
lua_pushlstring(l, s + i, 1);
lua_settable(l, 1);
@@ -1240,12 +1262,19 @@
int _cinv_chararray_to_string(lua_State *l) {
size_t len, i;
- if (lua_gettop(l) != 1) {
- lua_pushstring(l, "usage: cinv.chararray_to_string(carray)");
+ if (lua_gettop(l) < 1 || lua_gettop(l) > 2) {
+ lua_pushstring(l, "usage: cinv.chararray_to_string(carray[,
len])");
lua_error(l);
}
- len = lua_objlen(l, 1);
-
+ if (lua_gettop(l) == 2) {
+ len = lua_tointeger(l, 2);
+ if (len < 0) {
+ lua_pushstring(l, "invalid length");
+ lua_error(l);
+ }
+ } else {
+ len = lua_objlen(l, 1);
+ }
if (len == 0) {
lua_pushlstring(l, "", len);
} else {
@@ -1326,7 +1355,7 @@
lua_pushstring(l, "");
lua_settable(l, index);
}
- unmarshal_array(l, 2, value, index);
+ unmarshal_array(l, 2, &value, index);
}
return 1;
}
Modified: trunk/cinvoke/bindings/lua/test.lua
===================================================================
--- trunk/cinvoke/bindings/lua/test.lua 2006-06-26 04:18:29 UTC (rev 49)
+++ trunk/cinvoke/bindings/lua/test.lua 2006-06-27 03:32:28 UTC (rev 50)
@@ -3,7 +3,12 @@
libc = clibrary.new("libc.so.6")
getpass = libc:get_function(Cstring, "getpass", Cstring)
+strcpy = libc:get_function(Cptr, "strcpy", cinv.array(Cchar), Cstring)
pass = getpass("Enter a password: ")
+print("You entered " .. pass)
-print("You entered " .. pass)
+buf = { "a", "b", "c" }
+strcpy(buf, "ef")
+
+print(cinv.chararray_to_string(buf, 2))
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [cinvoke-svn] r50 - in trunk/cinvoke: . bindings/lua,
will <=