forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webassembly/proxy_js: Allow a Python proxy of a function to be undone.
This optimises the case where a Python function is, for example, stored to a JavaScript attribute and then later retrieved from Python. The Python function no longer needs to be a proxy with double proxying needed for the call from Python -> JavaScript -> Python. Signed-off-by: Damien George <[email protected]>
- Loading branch information
Showing
4 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Test proxying of functions between Python and JavaScript. | ||
|
||
const mp = await (await import(process.argv[2])).loadMicroPython(); | ||
|
||
// Create JS functions living on the JS and Py sides. | ||
globalThis.jsFunAdd = (x, y) => x + y; | ||
mp.globals.set("js_fun_sub", (x, y) => x - y); | ||
|
||
console.log("== JavaScript side =="); | ||
|
||
// JS function living on the JS side, should be a function. | ||
console.log(globalThis.jsFunAdd); | ||
console.log(globalThis.jsFunAdd(1, 2)); | ||
|
||
// JS function living on the Py side, should be a function. | ||
console.log(mp.globals.get("js_fun_sub")); | ||
console.log(mp.globals.get("js_fun_sub")(1, 2)); | ||
|
||
mp.runPython(` | ||
import js | ||
print("== Python side ==") | ||
py_fun_mul = lambda x, y: x * y | ||
js.pyFunDiv = lambda x, y: x / y | ||
# JS function living on the JS side, should be a JsProxy. | ||
print(type(js.jsFunAdd)) | ||
print(js.jsFunAdd(1, 2)) | ||
# JS function living on the Py side, should be a JsProxy. | ||
print(type(js_fun_sub)) | ||
print(js_fun_sub(1, 2)) | ||
# Py function living on the Py side, should be a function. | ||
print(type(py_fun_mul)) | ||
print(py_fun_mul(2, 3)) | ||
# Py function living on the JS side, should be a function. | ||
print(type(js.pyFunDiv)) | ||
print(js.pyFunDiv(6, 2)) | ||
`); | ||
|
||
console.log("== JavaScript side =="); | ||
|
||
// Py function living on the Py side, should be a proxy function. | ||
console.log(mp.globals.get("py_fun_mul")); | ||
console.log(mp.globals.get("py_fun_mul")(2, 3)); | ||
|
||
// Py function living on the JS side, should be a proxy function. | ||
console.log(globalThis.pyFunDiv); | ||
console.log(globalThis.pyFunDiv(6, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
== JavaScript side == | ||
[Function (anonymous)] | ||
3 | ||
[Function (anonymous)] | ||
-1 | ||
== Python side == | ||
<class 'JsProxy'> | ||
3 | ||
<class 'JsProxy'> | ||
-1 | ||
<class 'function'> | ||
6 | ||
<class 'function'> | ||
3.0 | ||
== JavaScript side == | ||
[Function: obj] { _ref: 4 } | ||
6 | ||
[Function: obj] { _ref: 3 } | ||
3 |