Add a VirtualMachine class to the WASM library#267
Add a VirtualMachine class to the WASM library#267windelbouwman merged 37 commits intoRustPython:masterfrom
Conversation
|
Sorry for the delay. Will get back to this later today. |
|
In working on this, I came across this comment in |
|
Hmm, that part changed a lot since I worked on it. @cthulahoops ? ^ |
|
Closures from Python to JS! To test it out, build and serve the demo page locally, then in the console enter: const vm = rp.vmStore.init("main");
vm.addToScope("fetch", (url, handler) => {
fetch(url)
.then(r => r.text())
.then(text => {
handler([text]);
});
});
vm.exec(`
a = {}
def handler(text):
a['result'] = text
fetch('https://httpbin.org/get', handler)
`);
// Wait a bit, then
vm.eval("a['result']");Do you think there's a better way to handle args/kwargs? Maybe like for JS closures where it's the this variable, where you'd have to do |
|
There are a few kinks to work out with borrow errors, LMK also if you can think of any better patterns than what's in |
|
Oh, and also up for discussion are ways of storing closure handles to prevent memory leaks. |
|
Re: "Printed already" In single mode the compiler inserts a PRINT_EXPR instruction into the code, so the repl doesn't need to do the print. Demo in Cpython: Implemented here: https://github.com/RustPython/RustPython/blob/master/vm/src/compile.rs#L117 |
|
@cthulahoops thanks! |
|
I'm having trouble solving the problem of Define a python closure and pass it to JS. The rust closure holds either a reference to a VM (probably not, cause it wouldn't be Call a JS function from Python. Currently, the VM store is borrowed, as is the VM. In that JS function, call static STORED_VMS: Rc<RefCell<HashMap<String, Rc<RefCell<StoredVirtualMachine>>>>> = Rc::default();I guess I was just hashing stuff out in this comment, cause I'm going to try that now. |
|
Wait, no, because it would still be borrowed while the vm was calling the injected JS function |
|
Alright, I've been thinking about this for a while and I think the only way to do this is to use unsafe code and a raw pointer, so I'll probably work on that when I get home today. |
|
HaHahahHA! I've figured out how to manage |
Codecov Report
@@ Coverage Diff @@
## master #267 +/- ##
==========================================
- Coverage 50.08% 48.32% -1.77%
==========================================
Files 68 69 +1
Lines 13924 14349 +425
Branches 3465 3582 +117
==========================================
- Hits 6974 6934 -40
- Misses 5096 5559 +463
- Partials 1854 1856 +2
Continue to review full report at Codecov.
|
Like variables carried across executions
|
I just realized that the fetch builtin might be a little out of the scope of this PR, so I'm going to take it out and make a separate PR for it later. |
|
@shinglyu, could you review this again? |
|
I'm merging this PR since it has grown to old and large, but in general I believe it is good. In future it would be better to split pull requests into smaller chunks. |
|
Thank you! I'll keep that in mind. |
This first commit is just to set up the framework; I haven't actually added any useful methods yet. Discussed in and resolves #262.