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

James Stortz <[email protected]> Fri, 1 May 2020 01:39:55 -0400
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <CAE0WHd8Y4ks2ncQqQrLry+0agV8vsq8bWv6xXsgOWM9OHbgVbQ@mail.gmail.com>
It seems to me like JS_InitStandardClasses didn't make it's way into
your runtime scope. I'll share a working example for me. As far as I
can tell, the only difference is you are using EnterCompartment, after
outsourcing your global scope creation, whereas and I am using
AutoCompartment and InitStandardClasses right before defining the JS
function.

If InitStandardClasses isn't applied, that's the only reason I can
think of why something would return undefined but show the correct arg
count. Hope that helps. Try InitStandardClasses after entering the
compartment?


    bool Init(runtime_t *rt) {

                if (!(rt->cx = JS_NewContext(8L * 1024 * 1024)))
                        return 1;
                if (!JS::InitSelfHostedCode(rt->cx))
                        return 1;

                {
                        JSAutoRequest ar(rt->cx);

                        JS::CompartmentOptions options;
                        if(!(rt->gl = JS::PersistentRooted<JSObject*>
                                (rt->cx , JS_NewGlobalObject(rt->cx,
&global_class, nullptr,
JS::FireOnNewGlobalHook, options))
                        ))
                                return 1;

                        {
                                JSAutoCompartment ac(rt->cx, rt->gl);
                                JS_InitStandardClasses(rt->cx,
rt->gl); //!!! <-- try this!

                                unsigned flags = JSPROP_READONLY |
JSPROP_PERMANENT;

                                if (!JS_DefineFunction(rt->cx, rt->gl,
"log", &stdout_log, 1, flags))
                                        return 1;

                                //...

                        }
                }
                return 0;
        };

On 5/1/20, [email protected] <[email protected]> wrote:
> Kannan
>
> I tried with RootedString as well.Also JS_EncodeStringToUTF8 does not
> allow(compile error) assignment to UniqueChars type but it did allow
> assignment to char * but in that case value still came as undefined. I am
> surprised how your example allow assignment . My compiler is clang.
>
> JS::CallArgs args = CallArgsFromVp(argc, argv);
>     for(int i=0; i<args.length(); i++) {
>         JS::RootedString str(cx, JS::ToString(cx, args[i]));
>         if (!str) {
>           return false;
>         }
>        char *bytes = JS_EncodeStringToUTF8(cx, str);;
>         if (!bytes) {
>           return false;
>         }
>         print_error("DEBUG: doit value : %s\n",bytes); //Output -> DEBUG:
> doit value : undefined
>     }
> _______________________________________________
> dev-tech-js-engine mailing list
> [email protected]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>