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

James Stortz <[email protected]> Thu, 30 Apr 2020 22:25:50 -0400
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <CAE0WHd-8a6frdO6g2MxNbMQYj6Vx_=sgp-aB7Bay++x_C+W6pg@mail.gmail.com>
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
>