tbodt / v8py

Write Python APIs, then call them from JavaScript using the V8 engine.
GNU Lesser General Public License v3.0
440 stars 28 forks source link

Enable WebAssembly on Python 3 #31

Closed turbolent closed 3 years ago

turbolent commented 5 years ago
codecov-io commented 5 years ago

Codecov Report

Merging #31 into master will increase coverage by 0.62%. The diff coverage is 90%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
+ Coverage      81%   81.63%   +0.62%     
==========================================
  Files          16       16              
  Lines        1353     1361       +8     
==========================================
+ Hits         1096     1111      +15     
+ Misses        257      250       -7
Impacted Files Coverage Δ
v8py/convert.cpp 88.81% <100%> (+3.81%) :arrow_up:
v8py/jsobject.cpp 89.78% <85.71%> (+0.35%) :arrow_up:
v8py/polyfill.h 100% <0%> (+16.66%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 3615a48...df7e3a9. Read the comment docs.

tbodt commented 5 years ago

Neat! I'll take a look tomorrow.

tbodt commented 5 years ago

Why not use bytearray instead of bytes? That works with both python 2 and python 3, and it's mutable so the data can actually be shared.

turbolent commented 5 years ago

Initially I just tried to pass bytes as such into V8 to instantiate a WebAssembly module (https://github.com/tbodt/v8py/pull/31/commits/ebd08f99a271e50414b23aeb91047b639e900860). I also added the dual, to convert typed arrays back from V8 to Python bytes so a WebAssembly module's memory can be accessed from Python.

However, I then realized that the WebAssembly module's memory might want to be mutated, e.g. to pass data into the module, using TypedArray.set. So instead, I removed the implicit conversion from V8 typed arrays to Python bytes in https://github.com/tbodt/v8py/pull/31/commits/de255482a7ef36cc6a765463cd4e585f645c35dd) and added the to_bytes method to still be able to do the conversion manually.

I guess to avoid the possible name-class for the to_bytes method, it could be exposed as a function from the v8py module (like new).

I agree that making bytearray work would be an even better solution, but I don't quite see how to implement that. Ideally, a slicing assignment to the bytearray in Python , e.g. arr[1:3], would translate to a TypedArray.set call.

I looked at both the C API for buffers and for byte arrays, but I'm not sure how too implement it.

I guess __setitem__ could be implemented for js_object, and implemented as a memcpy to ArrayBuffer.GetContents.Data?

tbodt commented 5 years ago

The V8 API supports externalized array buffers, so no need to do all that. You can just create an array buffer backed by memory owned by a python bytearray. It should also be possible to create a python bytearray or memoryview or similar backed by an array buffer.