ccxvii/mujs

JSON.parse from C

Closed this issue · 1 comments

Any idea why this won't work?

#include <stdio.h>
#include <mujs.h>

int main(int argc, char **argv)
{
        char * res;
        char line[256];
        js_State *J = js_newstate(NULL, NULL, JS_STRICT);
        jsB_initjson(J);
        js_getglobal(J, "JSON.parse");
        js_pushnull(J);
        js_pushstring(J, "{}");
        if (js_pcall(J, 1)) {
                fprintf(stderr, "an exception occurred in the javascript callba$
                js_pop(J, 1);
                return -1;
        }
        res = js_tostring(J, -1);
        printf("%s\n",res);
        js_freestate(J);
}

"JSON.parse" is not a global. That's an expression. You don't need to call jsB_initjson. The JSON object is already initialized when creating the state.

js_getglobal(J, "JSON");
js_getproperty(J, -1, "parse");