Question

I am embeding SpiderMonkey (mozila javascript) interpreter/engine in my C/C++ application. i programatically loading a javascript script and want to test (from my C/C++ code) if a certain function exist/defined in this script.

is there a way to do that ? maybe a way to list all functions in a given script ?

Tnx, Vertilka

Was it helpful?

Solution

As far as I know, this, by default, refers to the global scope. So you can:

(function() {
    var globals = this;
    for (var i in globals) {
        if (typeof globals[i] == 'function') {
            // a function named i is defined.
        }
    }
})();

OTHER TIPS

In C, with SpiderMonkey, you can programatically test for a certain function:

    JSObject *callable = NULL;
    JSObject *global;
    JSBool jb;
    jsval v = { 0 };

    jb = JS_GetProperty(context, global, "function_name", &v);
    g_assert_cmpint(jb, ==, JS_TRUE);
    g_assert(JSVAL_IS_OBJECT(v));
    g_assert(!JSVAL_IS_NULL(v));
    g_assert(!JSVAL_IS_VOID(v));

    callable = JSVAL_TO_OBJECT(v);
    g_assert(callable);
    g_assert(JS_ObjectIsFunction(context, callable));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top