Skip to content

webassembly: support converting buffer protocol objects to Uint8Array in jsffi.to_js()#19142

Open
dpgeorge wants to merge 2 commits into
micropython:masterfrom
dpgeorge:webassembly-jsffi-to-js-buffer-protocl
Open

webassembly: support converting buffer protocol objects to Uint8Array in jsffi.to_js()#19142
dpgeorge wants to merge 2 commits into
micropython:masterfrom
dpgeorge:webassembly-jsffi-to-js-buffer-protocl

Conversation

@dpgeorge
Copy link
Copy Markdown
Member

Summary

This adds support for explicit conversion from bytes/bytearray/memoryview/etc to JavaScript Uint8Array. Eg:

import js, jsffi

js.console.log(jsffi.to_js(b'1234'))

That now works and prints Uint8Array[4] [ 49, 50, 51, 52] in the JS console. The array contains a copy of the data (ie not a reference).

This matches Pyodide behaviour, eg pyodide.ffi.to_js(b'1234') returns the same Uint8Array.

Also includes a clean up of the conversion function to make it more efficient.

Testing

Added a test that runs under CI.

Generative AI

I did not use generative AI tools when creating this PR.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.46%. Comparing base (8a56be6) to head (b1af39e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19142   +/-   ##
=======================================
  Coverage   98.46%   98.46%           
=======================================
  Files         176      176           
  Lines       22811    22811           
=======================================
  Hits        22460    22460           
  Misses        351      351           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

If the Python object is a known type (tuple, list, dict) then the caller
always wants to follow up with a call to get the associated data for that
object.  So simplify things to just a single call that gets both the type
and object data (if the type is known).

Signed-off-by: Damien George <damien@micropython.org>
This adds support for explicit conversion from bytes/bytearray/memoryview
(and other objects with the buffer protocol) to JavaScript `Uint8Array`.
For example:

    import js, jsffi

    js.console.log(jsffi.to_js(b'1234'))

That now works and prints `Uint8Array[4] [ 49, 50, 51, 52]` in the JS
console. The array contains a copy of the data (ie not a reference).

This matches Pyodide behaviour, eg `pyodide.ffi.to_js(b'1234')` returns the
same `Uint8Array`.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the webassembly-jsffi-to-js-buffer-protocl branch from 353ae4a to b1af39e Compare April 23, 2026 13:34
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.

1 participant