Re: migration to mozjs-52

Steve Fink <[email protected]> Wed, 10 Jul 2019 10:00:42 -0700
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
Though now that I suggested that approach, I'm having second thoughts. 
The "pin" portion of JS_AtomizeAndPinJSString will keep the string alive 
forever. And we seem to have a hole in our API -- I don't see any way to 
just atomize a JSString* without extracting its characters. (Externally, 
I mean. Internally, there js::AtomizeString.) I suppose it's not often 
needed? You'd normally be starting from a string literal or something.

Anyway, it's probably fine if you're not going to be using this on a 
large number of distinct strings. (If you use strings with the same 
contents, it will reuse the existing atom, so that's fine.) If you do 
need lots of different strings, then you should probably extract the 
chars and use JS_AtomizeString on them to avoid leaking. It looks like 
you can use AutoCheckCannotGC and JS_GetTwoByteFlatStringChars for this, 
then call JS_AtomizeUCString (see the description in jsapi.h). That 
would normally make me nervous, as I would expect JS_AtomizeUCString to 
be able to GC and thus mangle the string you're using. But it looks like 
we make it fail and return nullptr without GCing, so it's ok.

And we should implement JS_AtomizeString (or JS::AtomizeString) for 
future versions.

On 7/10/19 6:21 AM, Alex Nekrylau wrote:
> Thank you Steve, with your help I was able to make that part work!
>
>> On Jul 8, 2019, at 2:55 PM, Steve Fink <[email protected] 
>> <mailto:[email protected]>> wrote:
>>
>> On 7/2/19 1:00 PM, [email protected] wrote:
>>> 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());
>>
>> That's a generally dangerous thing to be doing, since you now have a 
>> pointer to GC-controlled data that could be freed at any time. If 
>> AutoStableStringChars exists, that's probably the safe way to go 
>> about it.
>>
>> But you should also be able to use the string "directly" through some 
>> horrendously ugly APIs. Call |interned = JS_AtomizeAndPinJSString(cx, 
>> prName.toString()), then |||INTERNED_STRING_TO_JSID(cx, interned) on 
>> that, to get a jsid you can use for JS_GetProperty (error-checking 
>> all the way).||
>>
>> ||https://searchfox.org/mozilla-central/source/js/src/gdb/tests/test-jsid.cpp#7-9||
>>
>> ||
>> ||
>>
>> ||||
>>
>