Thread: Spidermonkey wrapper library callbacks

  1. #1
    Registered User
    Join Date
    Jul 2008

    Spidermonkey wrapper library callbacks


    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:
    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:
    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.

    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.

    native_function test_func = boost::bind(user_function, some, values, passed, along, _1, _2, _3, _4, _5);
    JSNative* real_func =<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:

        // 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:

    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.

  2. #2
    Registered User
    Join Date
    Jul 2008
    From the documentation:

    JS_ARGV_CALLEE(argv), which initially contains a reference to the JavaScript Function object being called.
    Using the macro JS_ARGV_CALLEE i can obtain a JSFunction* in the callback, which points to the same object returned by:

    JSFunction * JS_DefineFunction(JSContext *cx, JSObject *obj,
        const char *name, JSNative call, uintN nargs, uintN flags);
    And giving me the key i need for my unordered_map.

    This is only with normal functions, with class constructors or method i have yet to figure out a way, but the above will get me somewhere.

    If i have any more problems that probably can be solved by just RTFM, ill be sure to disturb you all again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  2. how to create an Import Library in Vs2008
    By khumayun in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2010, 10:49 AM
  3. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  4. Library Wrapper
    By cusavior in forum C Programming
    Replies: 3
    Last Post: 03-25-2008, 10:27 AM
  5. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM