Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolating execution via separate contexts #208

Open
mashdragon opened this issue Dec 30, 2023 · 4 comments
Open

Isolating execution via separate contexts #208

mashdragon opened this issue Dec 30, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@mashdragon
Copy link

Describe your feature request here.

It does not seem like there is a feature in PythonMonkey to run JavaScript scripts or evals in isolation after importing the module. This means that I have to keep track of the JavaScript state myself and perform any cleanup manually on objects that pollute the globalThis space.

It would be nice if there were a way to isolate execution so that separate scripts run in separate environments, just like how scripts in the web are isolated to their pages.

Maybe this would appear as a pm.context() function to get a new JavaScript execution context.

Code example

import pythonmonkey as pm

# Potential implementation of this context feature
context = pm.context()
context.eval("x = 4")

# A different context
context2 = pm.context()
context2.eval("x = 5")

print(context.eval("x")) # Prints 4
print(context2.eval("x")) # Prints 5
@wiwichips
Copy link
Collaborator

Hi @mashdragon, thanks for taking the time to check out PythonMonkey! This is definitely on our roadmap and something we will get to in the future

@hjdhnx
Copy link

hjdhnx commented Feb 29, 2024

I think it's a good idea, quickjs did it
see here

@wesgarland
Copy link
Collaborator

This is definitely an interesting idea.

It does raise some complex questions like - what happens when a JS variable is shared from one context to another via the underlying Python code? The GC requirements in PythonMonkey can get very interesting because we have to interoperate with both interpreters at the same time.

One of the questions that pops is -- do we care about complete isolation, meaning a new SpiderMonkey JSContext *cx, or is symbol-level isolation enough?

Providing a new global object and standard classes (Array, Object, etc) would be pretty easy, along with an isolated module context via ctx-module.

But this type of incomplete isolation might have interesting prototype-walking attacks, like [].constructor.prototype ... might be outside of the symbol isolation context because there is only one struct JSClass * per standard class in a JSContext *cx even when different instances of the standard classes are available in JavaScript.

I like the QuickJS interface. If we do this, we will also make it possible to emulate the NodeJS vm module calls somehow.

@wesgarland
Copy link
Collaborator

I think what we want is separate realms. We can call 'em contexts from the users' POV.

http://www.carolinecullen.com/spidermonkeynotes/notes/runtime.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

4 participants