Re: Is only one JSContext allowed per thread in JS 76?

Miles <[email protected]> Fri, 26 Jun 2020 05:17:07 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
On Friday, 26 June 2020 08:38:04 UTC+1, Miles  wrote:
> Hi,
> 
> A quick question if I may...
> Am I correct in thinking that only one JSContext is allowed per thread in JS 76 (even if it is the main thread)?
> I'm assuming that this must be the case as in js::NewContext pretty much the first thing it checks is
>  
>  MOZ_RELEASE_ASSERT(!TlsContext.get());
> 
> and this asserts on a debug build if I try to create a second context.
> 
> In the older releases you used to be able to have a single JSRuntime but multiple JSContexts on a single thread and we made use of that. Each 'script' had its own Context but they were all running concurrently on a single thread.
> My (no doubt incorrect) understanding was that back then Spidermonkey could be built 'threadsafe' so that was OK. I thought that threadsafe builds were the default in newer releases though.
> 
> Only being able to have a single Context per thread seems to be a backward step to me and it's going to be a massive problem for our embedding.
> What was the rationale behind requiring it and do I have any alternatives other than making each Context a separate thread?
> 
> Many thanks
> 
> Miles

As a follow up/supplemental question, if you can only have one JSContext on a thread, can a context have multiple global objects? From my understanding all the standard classes etc are made using a global object.
Does that mean that if I have two scripts that I want to keep completely separate I can create two different global objects in one context, initialise the standard classes for each global object and then compile and execute the scripts separately in the different globals?
Are the variables, classes etc completely separate and independent?
If so, I think that will allow me to do what I need.

Many thanks

Miles