All Projects â†’ DanRuta â†’ wasm-arrays

DanRuta / wasm-arrays

Licence: MIT License
A couple of helper functions to make WebAssembly array parameters easy to use

Programming Languages

javascript
184084 projects - #8 most used programming language
C++
36643 projects - #6 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to wasm-arrays

block-aligner
SIMD-accelerated library for computing global and X-drop affine gap penalty sequence-to-sequence or sequence-to-profile alignments using an adaptive block-based algorithm.
Stars: ✭ 58 (+75.76%)
Mutual labels:  webassembly, wasm
x-img-diff-js
🎨 JavaScript library which compares 2 images and detect structural difference information using OpenCV(WebAssembly)
Stars: ✭ 43 (+30.3%)
Mutual labels:  webassembly, wasm
rustwasmc
Tool for building Rust functions for Node.js. Combine the performance of Rust, safety and portability of WebAssembly, and ease of use of JavaScript.
Stars: ✭ 97 (+193.94%)
Mutual labels:  webassembly, wasm
pywasm3
Python bindings for Wasm3, the fastest WebAssembly interpreter
Stars: ✭ 34 (+3.03%)
Mutual labels:  webassembly, wasm
holyc
An easy to use C++ to WASM compiler (Highly-experimental)
Stars: ✭ 33 (+0%)
Mutual labels:  webassembly, wasm
cabasa
Haxe Framework for WebAssembly
Stars: ✭ 30 (-9.09%)
Mutual labels:  webassembly, wasm
swam
WebAssembly engine in Scala
Stars: ✭ 38 (+15.15%)
Mutual labels:  webassembly, wasm
gearoenix
Cross-platform C++ 3D game engine.
Stars: ✭ 33 (+0%)
Mutual labels:  webassembly, wasm
rustexp
A Rust regular expression editor and tester that runs entirely within the browser!
Stars: ✭ 59 (+78.79%)
Mutual labels:  webassembly, wasm
wapc-rust
Rust-based WebAssembly Host Runtime for waPC-compliant modules
Stars: ✭ 75 (+127.27%)
Mutual labels:  webassembly, wasm
node-wasi
WASI for Node.js
Stars: ✭ 64 (+93.94%)
Mutual labels:  webassembly, wasm
learn-wasm
🎲 Learning WebAssembly
Stars: ✭ 57 (+72.73%)
Mutual labels:  webassembly, wasm
image-hub
Image Hub is a sample application for exploring WebAssembly modules used as Envoy filters.
Stars: ✭ 56 (+69.7%)
Mutual labels:  webassembly, wasm
golang-wasm
Golang-WASM provides a simple idiomatic, and comprehensive API and bindings for working with WebAssembly for Go and JavaScript developers
Stars: ✭ 57 (+72.73%)
Mutual labels:  webassembly, wasm
koder
QR/bar code scanner for the Browser
Stars: ✭ 73 (+121.21%)
Mutual labels:  webassembly, wasm
warpy
WebAssembly interpreter in RPython
Stars: ✭ 54 (+63.64%)
Mutual labels:  webassembly, wasm
vmrp
mrp emulator, virtual machine, mrp模拟器
Stars: ✭ 126 (+281.82%)
Mutual labels:  webassembly, wasm
TypeScriptXX
🧷 Stay safe! Type-safe scripting for C++ using TypeScriptToLua and CMake with auto-generated declarations.
Stars: ✭ 33 (+0%)
Mutual labels:  webassembly, wasm
go-wasm-examples
Some small examples of using Go and WebAssembly
Stars: ✭ 22 (-33.33%)
Mutual labels:  webassembly, wasm
idris-codegen-wasm
WebAssembly Code Generation Backend for Idris Compiler
Stars: ✭ 79 (+139.39%)
Mutual labels:  webassembly, wasm

wasm-arrays

Build Status Coverage Status

NPM

A couple of helper functions to make WebAssembly array parameters easy to use.

Read more

Medium article going through the reasoning and implementation:

https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97

Importing

Browser

Add <script src="https://github.com/dist/wasm-arrays.min.js"></script> and use ccallArrays or cwrapArrays See the demo.html file included to see a few examples.

NOTE: You will need to serve the html file through a server. There is one already included, so you can run node server, then navigate to localhost:1337 to view.

Nodejs

const {ccallArrays, cwrapArrays} = require("./wasm-arrays.js")

You can see the test.js file to see more nodejs examples.

Usage

There are a few examples included in the demo.html, test.js and emscripten.cpp files, for their respective use cases

You can also see some more in-depth, real usage examples in my jsNet framework (https://github.com/DanRuta/jsNet), where this has been expanded to also pass maps and volumes.

The included functions aim to mimick the WebAssembly ccall and cwrap functions. They therefore work the same way.

If you need to pass an array parameter, you need to just add the parameter to the list of parameters.

In the C++ code, this will be turned into two parameters:

  1. Array pointer
  2. Array size

Example:

JavaScript

const result = ccallArrays("addNums", "number", ["array"], [[1,2,3,4,5,6,7]])

C++

EMSCRIPTEN_KEEPALIVE
int addNums (float *buf, int bufSize) {

    int total = 0;

    for (int i=0; i<bufSize; i++) {
        total+= buf[i];
    }

    return total;
}

To return an array from WebAssembly, you need to specify the return parameter as "array". You also need to specify the size of the returned array.

Example:

JavaScript

const res = ccallArrays("doubleValues", "array", ["array"], [[1,2,3,4,5]], {heapIn: "HEAP8", heapOut: "HEAP8", returnArraySize: 5})
console.log(res) // [2,4,6,8,10]

C++

EMSCRIPTEN_KEEPALIVE
int8_t* doubleValues (int8_t *buf, int bufSize) {

    int8_t values[bufSize];

    for (int i=0; i<bufSize; i++) {
        values[i] = buf[i] * 2;
    }

    auto arrayPtr = &values[0];
    return arrayPtr;
}

Heaps

You can also specify the heaps used for the arrays going in and out. The following table matches the heaps to the C++ types, for quick reference:

Heap C++
HEAP8 int8_t
HEAPU8 uint8_t
HEAP16 int16_t
HEAPU16 uint16_t
HEAP32 int32_t
HEAPU32 uint32_t
HEAPF32 float
HEAPF64 double
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].