Lutz: get-cell function patch
Posted: Sat Jun 14, 2014 4:29 pm
I have prepared a brief patch for adding a way to construct a newLISP cell from a pointer to an existing cell. I would prefer to make it a reference to the original cell instead of it getting garbage collected when the symbol's scope ends, but I did not see any easy way to do this. Instead, I wrote a function which takes a cell's pointer from the output of (first (dump foo)) and creates a new duplicate cell.
Where is this useful? I came up with the idea for allowing the FOOP API to also wrap function callbacks for C libraries. Many times these include a "user data" void pointer. I figured a good usage of this would be to allow the passing of a persistent cell's pointer as the "user data" pointer so it could be passed as an argument to a regular lisp function. The missing part is this builtin function to convert the pointer back to a CELL so it can be passed to registered user callbacks as an object rather than as an opaque pointer like a regular C library callback.
Lutz, if this function is acceptable, then I would also be willing to write the documentation. But I'm not going to do that unless I can get this approved somehow. I do not know newLISP as well as you do obviously, but I would really like some way to access a FOOP object (or other cells) from the registered C API callback. Being able to convert a pointer back to a usable cell, either as a reference or a copy, is something I think would be very useful for allowing FOOP to better act as a wrapper for C APIs. I have briefly tested it and it appears to work, but I don't know the newLISP innards very well.
Thanks for reading.
Code: Select all
diff -ur a/nl-string.c b/nl-string.c
--- a/nl-string.c 2014-04-08 09:54:24.000000000 -0500
+++ b/nl-string.c 2014-06-14 11:07:48.972430323 -0500
@@ -1359,6 +1359,11 @@
return(stuffString((char *)getAddress(params)));
}
+CELL * p_getCELL(CELL *params)
+{
+return(copyCell((CELL *)getAddress(params)));
+}
+
CELL * p_getInteger(CELL * params)
{
return(stuffInteger(*(int *)getAddress(params)));
diff -ur a/primes.h b/primes.h
--- a/primes.h 2014-04-08 09:54:24.000000000 -0500
+++ b/primes.h 2014-06-14 10:49:06.035008299 -0500
@@ -245,6 +245,7 @@
{"bits", p_bits, 0},
{"get-float", p_getFloat, 0},
{"get-string", p_getString, 0},
+ {"get-cell", p_getCELL, 0},
{"get-int", p_getInteger, 0},
{"get-long", p_getLong, 0},
{"get-char", p_getChar, 0},
diff -ur a/protos.h b/protos.h
--- a/protos.h 2014-04-08 09:54:24.000000000 -0500
+++ b/protos.h 2014-06-14 10:56:08.637362480 -0500
@@ -252,6 +252,7 @@
CELL * p_getInteger(CELL * params);
CELL * p_getLong(CELL * params);
CELL * p_getString(CELL * params);
+CELL * p_getCELL(CELL *params);
CELL * p_getUrl(CELL * params);
CELL * p_getenv(CELL * params);
CELL * p_global(CELL * params);
Lutz, if this function is acceptable, then I would also be willing to write the documentation. But I'm not going to do that unless I can get this approved somehow. I do not know newLISP as well as you do obviously, but I would really like some way to access a FOOP object (or other cells) from the registered C API callback. Being able to convert a pointer back to a usable cell, either as a reference or a copy, is something I think would be very useful for allowing FOOP to better act as a wrapper for C APIs. I have briefly tested it and it appears to work, but I don't know the newLISP innards very well.
Thanks for reading.