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_c: Return undefined if dict lookup failed on JS side.
Instead of raising KeyError. These semantics match JavaScript behaviour and make it much more seamless to pass Python dicts through to JavaScript as though they were JavaScript {} objects. Signed-off-by: Damien George <[email protected]>
- Loading branch information
Showing
4 changed files
with
58 additions
and
2 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,34 @@ | ||
// Test passing a Python dict into JavaScript, how it behaves with undefined keys. | ||
// If JavaScript accesses a key that does not exist, `undefined` should be returned. | ||
// This is different to Python-side behaviour, where `KeyError` is raised. | ||
|
||
const mp = await (await import(process.argv[2])).loadMicroPython(); | ||
|
||
// Create a JavaScript function with default arguments. | ||
// When `value` is `undefined` it will receive its default. | ||
function withDefault({ value = "OK" } = {}) { | ||
console.log(value); | ||
} | ||
|
||
globalThis.withDefault = withDefault; | ||
|
||
// Call the function from JavaScript with various arguments. | ||
withDefault(); | ||
withDefault({}); | ||
withDefault({ value: null }); | ||
withDefault({ value: undefined }); | ||
withDefault({ value: () => {} }); | ||
|
||
console.log("===="); | ||
|
||
// Call the function from Python with the same arguments as above. | ||
// The results should be the same. | ||
mp.runPython(` | ||
import js | ||
js.withDefault() | ||
js.withDefault({}) | ||
js.withDefault({"value": None}) | ||
js.withDefault({"value": js.undefined}) | ||
js.withDefault({"value": (lambda: {})}) | ||
`); |
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,11 @@ | ||
OK | ||
OK | ||
null | ||
OK | ||
[Function: value] | ||
==== | ||
OK | ||
OK | ||
null | ||
OK | ||
[Function: obj] { _ref: 7 } |