2

Working with JavaScriptCore's C API and I have this C function pointer signature:

typedef void
(*JSObjectInitializeCallback) (JSContextRef ctx, JSObjectRef object);

And here is a problematic usage:

JSClassDefinition def = kJSClassDefinitionEmpty;
// This is that previously typed out signature being used
def.initialize = [](JSContextRef ctx, JSObjectRef obj){
  CAMLlocal2(init_context, init_obj);

  init_context = caml_alloc(sizeof(JSContextRef), Abstract_tag);
  init_obj = caml_alloc(sizeof(JSObjectRef), Abstract_tag);
  Store_field(init_context, 0, (value)ctx);
  Store_field(init_obj, 0, (value)obj);
  // Need to call this but can't because class_def is 
  // from the outside and can't be put in the capture list
  //caml_callback2(Field(class_def, 5), init_context, init_obj);
};

So I'm not sure what's the right way to get around this. I don't want to start defining C functions all over the place because I need to create these callbacks dynamically and I'm not sure how to do the global state solution correctly because that will introduce more issues of locking correctly, etc. I'm also open to using objective-C blocks, just the blocks, no objective-C. Saw some mentions of std::function but I don't understand C++ well enough to use that API.

Looking through the Apple Objective-C layer on top of JSC to see how they did it....

EDIT1:Found this link as a possible solution, http://p-nand-q.com/programming/cplusplus/using_member_functions_with_c_function_pointers .html but its pretty deep in C++ knowledge of which mine is limited.

EDIT2: I guess I need something to template magic generate the code for me?

  • If your question involves use of Obj-C, add that tag as well. – CinchBlue Jun 27 '16 at 20:20
  • If you can do something with a regular function, you can do it with a non-capturing lambda, and vice versa. OTOH capturing lambdas are not compatible with regular functions, so you cannot take one and send it to a place that expects a regular function. I have no idea what an ObjC block is, but I suspect they are exactly equivalent to either kind of lambdas. It seems the question would benefit from rephrasing in terms of regular functions, as lambdas (and ObjC blocks?) only muddle the water without contributing anything beyond syntatctic sugar. – n. m. could be an AI Jun 27 '16 at 20:30
  • @VermillionAzure it doesn't, blocks are separate from Objective-C. n.m I'm looking if anyone else has hit this roadblock and what solutions they came up with. –  Jun 27 '16 at 20:51
  • This is a pretty common issue when binding C++ to languages like python or lua. In the case of lua, what people do is use template libraries like `luabind` or `luabridge` which wrap more complicated C++ function calls and convert them into delegates matching the required typedef. There's any number of ways to actually implement that wrapping. One hacky way is to create some kind of global resource manager, and evaluate delegates of the simple function pointer type by accessing the global singleton and calling a more complex function. I don't know the details of JS binding, YMMV – Chris Beck Jun 27 '16 at 21:43

0 Answers0