Re: migration to mozjs-52

[email protected] Tue, 2 Jul 2019 13:00:43 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
On Tuesday, July 2, 2019 at 2:52:08 PM UTC-4, Jason Orendorff wrote:
> I don't have mozjs-52 handy, but I can give you a few pointers.
> 
> You don't need a replacement for JS_NewScriptObject, JS_DestroyScript,
> JS_AddNamedRoot, or JS_RemoveRoot. They are all related to GC-safety and
> you shouldn't need anything more complicated than JS::Rooted for that.
> 
> Instead of separately calling JS_CompileScript and JS_ExecuteScript, you
> should call JS::Evaluate (in js/public/CompilationAndEvaluation.h) if it
> exists in mozjs-52. If not, I think it was called JS_EvaluateScript.
> 
> If you need a CompileOptions object, use JS::OwnedCompileOptions, defined
> in js/public/CompileOptions.h, and make sure to call init() on it before
> using it.
> 
> -j
> 
> On Tue, Jul 2, 2019 at 8:35 AM <[email protected]> wrote:
> 
> > Greetings!
> >
> > I've been working on some updates to OpenVXI and decided to update all the
> > libraries first.
> > Looks like I was able to figure out most of it, but still have some
> > questions (also I should mention that this is my first big c/c++ endeavor)
> >
> > I have this type of thing going on and I'm not sure what to replace it
> > with as all/most of these functions seem to be obsolete but JSAPI User
> > guide still has them in there.
> >
> > bool jsScriptRes = JS_CompileUCScript (context,
> >                                    tmpscript, tmpscriptlen,
> >                                   options, &jsScript);
> >   if ( ! jsScriptRes )
> >     rc = VXIjsi_RESULT_SYNTAX_ERROR;
> >   else {
> >     JSObject *jsScriptObj = JS_NewScriptObject (context, jsScript);
> >     if (( ! jsScriptObj ) ||
> >         ( ! JS_AddNamedRoot (context, &jsScriptObj, SCRIPT_OBJECT_NAME) ))
> > {
> >       JS_DestroyScript (context, jsScript);
> >       rc = VXIjsi_RESULT_OUT_OF_MEMORY;
> >     } else {
> >
> >       JS::Rooted <JS::Value> val(context, JS::UndefinedValue());
> >       JS::Rooted <JSObject*> robj(context, currentScope->GetJsobj( ));
> >
> >       if ( JS_ExecuteScript (context, jsScript,
> >                              &val) ) {
> >         if ( retval )
> >           rc = retval->Set (val);
> >       } else if ( exception ) {
> >         rc = VXIjsi_RESULT_SCRIPT_EXCEPTION;
> >       } else if ( numBranches > maxBranches ) {
> >         rc = VXIjsi_RESULT_SECURITY_VIOLATION;
> >       } else {
> >         rc = VXIjsi_RESULT_NON_FATAL_ERROR;
> >       }
> >
> >       if ( ! JS_RemoveRoot (context, &jsScriptObj) )
> >         rc = VXIjsi_RESULT_FATAL_ERROR;
> >     }
> >   }
> > _______________________________________________
> > dev-tech-js-engine mailing list
> > [email protected]
> > https://lists.mozilla.org/listinfo/dev-tech-js-engine
> >

thank you so much, Jason!

so I basically replaced that with: 

JS::CompileOptions options(context);
options.setFileAndLine(NULL, 1);

JS::Rooted <JSScript*> jsScript(context);
JS::Rooted <JS::Value> val(context, JS::UndefinedValue());

if ( JS::Evaluate(context, options, tmpscript, tmpscriptlen, &val) ) {
      if ( retval )
        rc = retval->Set (val);
} ....

gcc is not screaming at me about this part anymore :)

I also figured JS_DestroyIdArray is deprecated in 52 (the doc hasn't been updated yet apparently).. do I still need to use it to release the pointer assigned by JS_Enumerate (context, obj, &props)?

the other thing is, I've been trying to figure out how to make this expression work without JS_GetStringChars function to use it with JS_GetUCProperty later:

name = JS_GetStringChars(prName.toString());