-
Notifications
You must be signed in to change notification settings - Fork 12
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
Use vm.runInContext
#9
Comments
Moved from n-riesco/ijavascript#117 (comment) : @Bamieh At the very beginning of IJavascript, I experimented with [] instanceof Array // true
vm.runInContext("[] instanceof Array", vm.createContext({Array})); // false Another difficulty with this idea of recreating the context is that the state of previous context would be lost. The way I see this issue is that IJavascript is an enriched REPL for When I use IJavascript as a REPL, I prefer to use When I use IJavascript to run a a script, then the normal rules for Picking up @rgbkrk 's suggestion and your idea, I could implement |
Moved from n-riesco/ijavascript#117 (comment) : @n-riesco why would you pass Array to the new context 🤔 , it gives false because the Array you passed is pointing to a different location in memory than the one created by To solve the previous context issue, we just track added objects on the global (observers or manually) and propagate them in the new context. Implementing a restart is not a good idea imo, since it might break other code blocks depending on previous code. If the code is to be run in an isolated scope, anyone can write this:
#blocks_are_the_new_iffe 😆 |
I was doing something like this: vm.runInContext("[] instanceof Array", vm.createContext(global));
What would be the criteria to decide what was into the context?
🤔
Observers are gone. Perhaps a Proxy would do the trick. Potentially, this may break for very large-memory objects (but let's ignore this for the time being). Preserving the global object only solves part of the problem:
🤔 (in the cases that it breaks, users should re-run the necessary code;
yes, this is useful, when the user doesn't need to carry definitions across notebooks cells. |
Effectively, you shouldn't pass
Thank you! This is exactly what I want out of the ijavascript kernel.
Me too, that's what I've been doing as well. |
What would be the criteria to decide what was into the context?The window/global object is a circular reference to the context object, but it is not the object itself. What needs to be inside the context is what node puts on the context, just logging the global object will show you what is needed:
Observers are gone. Perhaps a Proxy would do the trick.yea, i meant observers as a concept regardless of the implementation, using proxies, a library, or manual looping. Potentially, this may break for very large-memory objects (but let's ignore this for the time being).The passed context objects are passed by reference, so you are just passing reference points of those objects already in memory. The way I see this issue is that IJavascript is an enriched REPL for Node.js and it aims at behaving the same way Node.js's REPL does.It will still behave like a REPL regardless if we are running each individual snippet in a new vm or the same one (since we are sharing the context between them). For the end user, The current behavior of |
Without a solution for preserving the event loop, callbacks and the corresponding contexts, wouldn't this approach still require that users re-run previous code?
We'd also need to preserve all the modules available in a REPL session? Is there a way to determine this programmatically?
This wouldn't work. We'd need to clone the context before running the execution request, because the execution request may modify the context before encountering a
My feeling is that this is a project in itself that would require constant maintenance to keep up with changes in Node's REPL.
If, as you think, this is an unexpected behaviour for users, the place to fix it is https://github.com/nodejs/node . Personally, I think the |
passing the module and require in the context does the job.
Although this is problematic for any REPL, node people agreed to not fix this issue.
we can leave the behavior as is for now then |
I'm not sure I understand, I've tried this but it fails:
I remember reading about it, but now I couldn't find a link. Do you have a link to that decision?
OK, but if you make any progress on this subject, please, don't hesitate to report back here. |
That would be:
|
@rgbkrk There's no need for
Anyway, I don't think the lack of pre-required modules in a REPL session would be a show-stopper (it's just more convenient). |
I'll try to hack a little with it on the weekend see if i can come up with something simple. Here is a link to the node discussion nodejs/node#8441 |
That seems like a need for passing > var context = vm.createContext({require, child_process})
undefined
> vm.runInContext('child_process', context)
{ ChildProcess:
{ [Function: ChildProcess]
super_:
{ [Function: EventEmitter]
EventEmitter: [Circular],
usingDomains: true,
defaultMaxListeners: [Getter/Setter],
init: [Function],
listenerCount: [Function] } },
fork: [Function],
_forkChild: [Function],
exec: [Function],
execFile: [Function],
spawn: [Function],
spawnSync: [Function: spawnSync],
execFileSync: [Function: execFileSync],
execSync: [Function: execSync] } |
OMG I finally read the rest of the above more thoroughly. I didn't realize that built-in modules got "auto-required" in some sense. This whole time I've been working at the node repl I've been |
Since |
@Bamieh As an initial test, I'd use a notebook like this: // In[1]:
let x = 0;
const $x = $$.display("x");
setInterval(() => $x.text(`x: ${x}`), 1000);
console.log("The display below should show 'x: 0' until next cell is run");
// In[2]:
let x = 2
console.log("Now, the display in the cell above should show 'x: 2'"); |
For
issue, I think this is due to using ES6 So, what I understand is for a given code :
Re-running the cells will override
on the same context, for some reason(which I don't know why the implementation is like this, but probably, for big data operation Python code don't want to lose the previous result.) In fact, this is very easy to solve when the kernel pass the entire JavaScript source-code to the evaluation process, to add
then, this constant value or any other internal constant values are safely to be isolated from the override of re-running evaluation. |
I must comment this issue which a code with
on / from the second time evaluations should be flagged as Bug. This should not be happened. It is obvious, the JypyterNotebook document is static including the JavaScript source-code, and when the source is static and identical, evaluation result should be also identical. Running process for the evaluation for the second time differs to the first time is a phenomenon that should never happen. |
Assuming you meant the use of brackets
Why shouldn't it happen? Let's imagine this example. Someone writes a notebook and by mistake they type the same cell twice. If this cell defines a variable using
I do this is because IJavascript was originally written in ES5 and it's backwards-compatible down to node v0.10. Also, please, note that the project uses the linter ESLint to prevent the shadowing of identifiers.
This statement isn't correct. hydrogen, nteract, the Jupyter notebook and JupyterLab treat notebooks as REPL sessions. As far as I'm aware, only nbconvert and nbviewer treat a Jupyter notebook as an static document. |
Moved discussion from n-riesco/ijavascript#117 (comment) :
I would solve this issue like this:
with
If the code errors out, parse the error to check for this pattern:
if this pattern matches, create a new context to be used across the current session, and run the code again, any error created afterwards is propagated to the user.
I have not looked into the ijavascript code yet, but I am hoping to get some feedback on this before I do so.
The text was updated successfully, but these errors were encountered: