Passing pointers in ByteArrays

Tony Garnock-Jones tonyg at lshift.net
Sat Oct 16 09:45:08 PDT 2004


One way is to construct the ByteArray on the Slate side, and pass it in 
to the primitive. Then the primitive fills the ByteArray:


/* Expects: 4-byte, populated ByteArray for handle, containing the 
result of dlopen; n-byte, populated string for symname, containing the 
symbol to look up; and 4-byte, unpopulated ByteArray for ptr, which will 
contain the resulting function pointer if the lookup succeeds. */

Bool lookupDynamicLibraryPrimitive(struct ByteArray /*in*/ *handle,
                                    struct ByteArray /*in*/ *symname,
                                    struct ByteArray /*inout*/ *ptr)
{
   void *h;
   primitive_fn_t fn;
   char *symbol;

   assert(BYTEARRAY_LEN(handle) >= sizeof(h));
   assert(BYTEARRAY_LEN(ptr) >= sizeof(fn));

   symbol = safe_string(symname, "");  /* this is one of my own utility 
functions */
   if (symbol == NULL)
     return False;

   memcpy(&h, handle->elements, sizeof(h));
   fn = (primitive_fn_t) dlsym(h, symbol);
   free(symbol);

   if (fn == NULL) {
     return False;
   } else {
     memcpy(ptr->elements, &fn, sizeof(fn));
     return True;
   }
}



Paul Dufresne wrote:
> Ok, since I'll try the Pupeno's way of doing
> Slate. By asking when you don't know.
> 
> I'd like to pass a C pointer (void *) to Slate.
> How do I transform the pointer in a ByteArray?
> 
> If someone other than Brian or Lee knows, please
> tell me (I feel bad asking such supposedly simple
> questions to them).
> 
> 
> 




More information about the Slate mailing list