Hello,
I am using GCC 4.5 (on Linux) with Boost 1.43.0 and C++0X to create a C++0X "worthy" wrapper library for the SpiderMonkey Java/ECMAscript engine.
However, i am running in to a problem.
See here a function pointer typedef:
Code:
typedef JSBool (*JSNative)(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
I use the JSNative type to make my C functions (very limiting for C++) known and callable for Java/ECMAScript.
Whenever the user of my wrapper wants a function or class method "registered" in the Java/ECMAScript engine, i give them this type to do it with:
Code:
typedef boost::function<JSBool (JSContext*, JSObject*, uintN, jsval*, jsval*)> native_function;
This gives users the abillity to have class methods registered and much more, instead just normal functions. The above type wil change, (The JSContext and JSObject structs wil to change to some class of my own wrapper library, that is the second reason i am covering up the engine's type).
A native_function gets registered in a unordered_map as value. The key is the problem. When the following function gets called (this function gets passed as the callback to the engine API, therefore its responsible for all JSNative related callbacks), it is tasked with looking up the appropriate native_function (and in the future also convert the engine's structs to my classes) and call it.
Code:
JSBool delegate_native_function (JSContext*, JSObject*, uintN, jsval*, jsval*);
I cannot send any variables along of my own to this callback. Nor am i able to calculate the desired native_function using the parameters (not enough information).
What i have tried.
Code:
native_function test_func = boost::bind(user_function, some, values, passed, along, _1, _2, _3, _4, _5);
JSNative* real_func = test_func.target<JSNative>();
(*real_func)(NULL, NULL, 0, NULL, NULL);
// Segfaults on the dereferencing of real_func. target returned NULL.
My current idea is to somehow generate (during compile time) a type used as a key in the unordered_map (this type would get passed to the delegate_native_function using templates). But unfortunately i was unsuccessful at this. I tried the following:
Code:
// Two different functions:
auto a = boost::bind(g_with_long_name, _1);
auto b = boost::bind(test2, _1);
std::cout << "main(): typeid(...): " << typeid(a).name() << std::endl;
std::cout << "main(): typeid(...): " << typeid(b).name() << std::endl;
if (typeid(a) == typeid(b))
std::cout << "Oh no..." << std::endl;
Which prints out:
Code:
main(): typeid(...): N5boost3_bi6bind_tIiPFiiENS0_5list1INS_3argILi1EEEEEEE
main(): typeid(...): N5boost3_bi6bind_tIiPFiiENS0_5list1INS_3argILi1EEEEEEE
Oh no...
This does not provide an unique enough key. So if i am able to get an unique compile time type key, im set. Unless any of you have a better idea.
https://developer.mozilla.org/en/Spi...rence/JSNative