Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
7c62fbe3
Commit
7c62fbe3
authored
Mar 29, 2024
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
webassembly/proxy_js: Promote Python thenable to a Promise.
Signed-off-by:
Damien George
<
damien@micropython.org
>
parent
39975321
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
1 deletion
+55
-1
ports/webassembly/proxy_js.js
ports/webassembly/proxy_js.js
+16
-1
tests/ports/webassembly/fun_py_callback_js.mjs
tests/ports/webassembly/fun_py_callback_js.mjs
+31
-0
tests/ports/webassembly/fun_py_callback_js.mjs.exp
tests/ports/webassembly/fun_py_callback_js.mjs.exp
+8
-0
No files found.
ports/webassembly/proxy_js.js
View file @
7c62fbe3
...
...
@@ -89,7 +89,22 @@ function proxy_call_python(target, argumentsList) {
if
(
argumentsList
.
length
>
0
)
{
Module
.
_free
(
args
);
}
return
proxy_convert_mp_to_js_obj_jsside_with_free
(
value
);
const
ret
=
proxy_convert_mp_to_js_obj_jsside_with_free
(
value
);
if
(
ret
instanceof
PyProxyThenable
)
{
// In Python when an async function is called it creates the
// corresponding "generator", which must then be executed at
// the top level by an asyncio-like scheduler. In JavaScript
// the semantics for async functions is that they are started
// immediately (their non-async prefix code is executed immediately)
// and only if they await do they return a Promise to delay the
// execution of the remainder of the function.
//
// Emulate the JavaScript behaviour here by resolving the Python
// async function. We assume that the caller who gets this
// return is JavaScript.
return
Promise
.
resolve
(
ret
);
}
return
ret
;
}
function
proxy_convert_js_to_mp_obj_jsside
(
js_obj
,
out
)
{
...
...
tests/ports/webassembly/fun_py_callback_js.mjs
0 → 100644
View file @
7c62fbe3
// Test using Python functions as JS callbacks.
const
mp
=
await
(
await
import
(
process
.
argv
[
2
])).
loadMicroPython
();
globalThis
.
asyncTimeout
=
(
ms
)
=>
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
ms
));
mp
.
runPython
(
`
import js
def f0():
print("f0 run")
async def f1():
print("f1 run")
async def f2():
print("f2 start")
await js.asyncTimeout(0)
print("f2 end")
async def f3():
print("f3 start")
await f2()
print("f3 end")
js.setTimeout(f0, 0)
js.setTimeout(f1, 50)
js.setTimeout(f2, 100)
js.setTimeout(f3, 150)
`
);
tests/ports/webassembly/fun_py_callback_js.mjs.exp
0 → 100644
View file @
7c62fbe3
f0 run
f1 run
f2 start
f2 end
f3 start
f2 start
f2 end
f3 end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment