JS::CallArgs - Arguments are not being parsed in C++

[email protected] Thu, 30 Apr 2020 14:37:18 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
I am using Spidermonkey 52 and below is my sample implementation.



static JSClassOps global_ops = {
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    nullptr,
    JS_GlobalObjectTraceHook
};

//Creating Global Context Object
JSContext * createContext() {
    JSContext *cx = JS_NewContext(8L * 1024 * 1024,8192);
    if (!cx)
    return NULL;

    return cx;
}


static JSClass * basicGlobalClass() {

    static JSClass c = {
        "global",
        JSCLASS_GLOBAL_FLAGS,
        &global_ops
    };
  return &c;
}

JSClass * getGlobalClass() {
  return basicGlobalClass();
}
//Creating Global Object and initiating basic initializers
void createGlobal(JSPrincipals *principals)
{
    JS::CompartmentOptions options;
    options.behaviors().setVersion(JSVERSION_LATEST);

    JS::RootedObject globalTemp(cx);

    globalTemp = JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook, options);

    JSAutoCompartment ac(cx, globalTemp);
    if (!JS_InitStandardClasses(cx, globalTemp)) {
        globalTemp = nullptr;
    }

    globalHandle = globalTemp;

    global = globalTemp;
}

static bool
doit(JSContext *cx, unsigned argc, JS::Value *vp)
{
  JS::CallArgs args = CallArgsFromVp(argc, vp);
    JSString *temp = JS::ToString(cx, args[0]);
    char* name = JS_EncodeString(cx, temp); //This value comes as undefined
}

int                                     
testApp_init()
{
    JS_Init();
    cx = createContext();
    if (cx == NULL)
        return 0;
    if (!JS::InitSelfHostedCode(cx))
        return 0;
    createGlobal(NULL);
    if (!global)
        return 0;
    
    JSAutoRequest ar(cx); 

    JS::RootedValue rval(cx);
    retval = rval;

    JS_EnterCompartment(cx, global);

    static JSFunctionSpec my_functions[] = {
      JS_FN("doit", doit, 1, 0),
      /* etc... */
      JS_FS_END
    };
    bool ok = JS_DefineFunctions(cx, global, my_functions);
    return 1;
}

Now when I call method  "var testDo = doit("Testinggggg");"  from my javascript, number of arguments in my C++ function doit reaches as 1 but value of the argument comes as undefined. 
Any idea what I am doing wrong? Thanks in advance!