forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webassembly: Reuse PyProxy objects when they are the same Python object.
This commit makes it so that PyProxy objects are reused (on the JavaScript side) when they correspond to an existing Python object that is the same object. For example, proxying the same Python function to JavaScript, the same PyProxy instance is now used. This means that if `foo` is a Python function then accessing it on the JavaScript side such as `api.globals().get("foo")` has the property that: api.globals().get("foo") === api.globals().get("foo") Prior to this commit the above was not true because new PyProxy instances were created each time `foo` was accessed. Signed-off-by: Damien George <[email protected]>
- Loading branch information
Showing
5 changed files
with
129 additions
and
27 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
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,26 @@ | ||
// Test identity of PyProxy when they are the same Python object. | ||
|
||
const mp = await (await import(process.argv[2])).loadMicroPython(); | ||
|
||
mp.runPython(` | ||
l = [] | ||
`); | ||
|
||
const l1 = mp.globals.get("l"); | ||
const l2 = mp.globals.get("l"); | ||
console.log(l1, l2); | ||
console.log(l1 === l2); | ||
|
||
globalThis.eventTarget = new EventTarget(); | ||
globalThis.event = new Event("event"); | ||
|
||
mp.runPython(` | ||
import js | ||
def callback(ev): | ||
print("callback", ev) | ||
js.eventTarget.addEventListener("event", callback) | ||
js.eventTarget.dispatchEvent(js.event) | ||
js.eventTarget.removeEventListener("event", callback) | ||
js.eventTarget.dispatchEvent(js.event) | ||
`); |
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,3 @@ | ||
PyProxy { _ref: 3 } PyProxy { _ref: 3 } | ||
true | ||
callback <JsProxy 7> |