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

[email protected] Thu, 30 Apr 2020 21:03:31 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
On Thursday, 30 April 2020 19:26:01 UTC-7, James Stortz  wrote:
> Just try 'args[0].toString()`
> 
> like so
> 
> char* name = JS_EncodeString(cx, args[0].toString());
> 
> also a good idea to check for argument type like so
> 
> if(args[0].isString()) {
>   char* name = JS_EncodeString(cx, args[0].toString());
> }
> 
> full example like so
> 
>         bool stdout_log(JSContext *cx, unsigned argc, JS::Value *vp) {
> 		
> 		JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
> 		
> 		for(int i=0; i<args.length(); i++) {
> 			if(args[i].isString()){
> 			
> 				cout << JS_EncodeString(cx, args[i].toString()) <<endl;
> 			
> 			}
> 		}
> 
> 		args.rval().setNull();
> 		return true;
> 	};
> 
> On 4/30/20, [email protected] <[email protected]> wrote:
> > 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!
> > _______________________________________________
> > dev-tech-js-engine mailing list
> > [email protected]
> > https://lists.mozilla.org/listinfo/dev-tech-js-engine
> >

Thanks James for your advice. I tried that but my argument does not pass isString check.Further diagnosis  pointed to me that isNumber evaluation becomes true.So argument is coming as Number.This is even more crazier that passed argument is a String value.Although if I change number of arguments then count comes correct but data type is completely incorrect.Getting out of ideas here.