Files
micropython/tests/ports/webassembly/fun_call.mjs
Damien George 01a11ea45e webassembly/objjsproxy: Support arbitrary number of args with kwargs.
When calling from Python into JavaScript and passing along keyword
arguments, the FFI bindings currently only support at most 1 positional
argument. For example:

    import js
    js.func(1, b=2, c=3)

This commit fixes that by supporting arbitrary number of positional
arguments, in combination with keyword arguments.  So now the following
works:

    import js
    js.func(1, 2, c=3, d=4)

Tests are added for these new, supported cases.

Signed-off-by: Damien George <damien@micropython.org>
2025-10-06 12:35:20 +11:00

45 lines
773 B
JavaScript

// Test calling JavaScript functions from Python.
const mp = await (await import(process.argv[2])).loadMicroPython();
globalThis.f = (a, b, c, d, e) => {
console.log(a, b, c, d, e);
};
mp.runPython(`
import js
js.f()
js.f(1)
js.f(1, 2)
js.f(1, 2, 3)
js.f(1, 2, 3, 4)
js.f(1, 2, 3, 4, 5)
js.f(1, 2, 3, 4, 5, 6)
`);
globalThis.g = (...args) => {
console.log(args);
};
mp.runPython(`
import js
js.g()
js.g(a=1)
js.g(a=1, b=2)
js.g(a=1, b=2, c=3)
js.g(a=1, b=2, c=3, d=4)
js.g(a=1, b=2, c=3, d=4, e=5)
js.g(1)
js.g(1, b=2)
js.g(1, b=2, c=3)
js.g(1, b=2, c=3, d=4)
js.g(1, b=2, c=3, d=4, e=5)
js.g(1, 2)
js.g(1, 2, c=3)
js.g(1, 2, c=3, d=4)
js.g(1, 2, c=3, d=4, e=5)
js.g(1, 2, 3)
js.g(1, 2, 3, d=4)
js.g(1, 2, 3, d=4, e=5)
js.g(1, 2, 3, 4)
js.g(1, 2, 3, 4, e=5)
`);