Skip to content

ports/webassembly/proxy_c.c: Allow python bytes() to be proxied to/from JS. #17044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

bwhitman
Copy link

@bwhitman bwhitman commented Mar 30, 2025

Summary

This allows python bytes() objects to be passed to JS. Example:

 function set_audio_samples(samples) {
     var res_ptr = amy_module._malloc(2 * 256 * 2); // 2 channels, 256 frames, int16s
     amy_module.HEAPU8.set(samples, res_ptr);
     amy_module.ccall('amy_set_input_buffer', null, ["number"], [res_ptr]);
     amy_module._free(res_ptr);
 }
...
 await mp.registerJsModule('amy_set_input_buffer', set_audio_samples);

In MP:

buffer = bytes(1024)
# do something to fill in the bytes...
amy_set_input_buffer(buffer)

It also works the other way (JS returning bytes() to MP):

 function get_audio_samples() {
     var res_ptr = amy_module._malloc(2 * 256 * 2); // 2 channels, 256 frames, int16s
     amy_module.ccall('amy_get_input_buffer', null, ["number"], [res_ptr]);
     var view = amy_module.HEAPU8.subarray(res_ptr , res_ptr + 1024);
     amy_module._free(res_ptr);
     return view
 }
 await mp.registerJsModule('amy_get_input_buffer', get_audio_samples);
>>> b = amy_get_input_buffer()
>>> type(t)
<class 'bytes'>

Previously, we would have to manually construct a js.UInt8Array.new(1024) and fill it in one by one.

Testing

Tested on webassembly port.

@bwhitman bwhitman changed the title ports/webassembly/proxy_c.c: allow python bytes() to be proxied to JS ports/webassembly/proxy_c.c: Allow python bytes() to be proxied to/from JS. Mar 30, 2025
@dpgeorge
Copy link
Member

Thanks for the contribution, this is definitely a useful feature and makes it much more efficient to pass bytes between Python and JS.

But, because Uint8Array is a mutable object, it no longer allows you to share such an object between Python and JS (instead this PR would make a copy when passing it across the divide).

At the moment you can do something like this:

const mp = await (await import(process.argv[2])).loadMicroPython();

globalThis.array = new Uint8Array(4);
globalThis.array[0] = 1;
console.log(globalThis.array);

mp.runPython(`
import js
print(js.array[0])  # access JS Uint8Array
js.array[0] = 2  # modify JS Uint8Array
`);

console.log(globalThis.array);

Running this, it outputs:

$ node test.mjs `pwd`/build-pyscript/micropython.mjs
Uint8Array(4) [ 1, 0, 0, 0 ]
1
Uint8Array(4) [ 2, 0, 0, 0 ]

I don't think such sharing of mutable Uint8Array will be possible with this PR.


Maybe instead it could be part of jsffi.to_js() where the user explicitly converts the value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy