Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jsonnull/rust-wasm
A Rust and JavaScript utility suite for writing WebAssembly modules
https://github.com/jsonnull/rust-wasm
javascript rust wasm webassembly
Last synced: 3 months ago
JSON representation
A Rust and JavaScript utility suite for writing WebAssembly modules
- Host: GitHub
- URL: https://github.com/jsonnull/rust-wasm
- Owner: jsonnull
- License: mit
- Created: 2017-12-31T06:43:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-26T03:01:09.000Z (almost 7 years ago)
- Last Synced: 2024-09-16T23:10:33.749Z (3 months ago)
- Topics: javascript, rust, wasm, webassembly
- Language: JavaScript
- Homepage:
- Size: 323 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-wasm
[![Build Status](https://travis-ci.org/jsonnull/rust-wasm.svg?branch=master)](https://travis-ci.org/jsonnull/rust-wasm)
A Rust and JavaScript utility suite for writing WebAssembly modules.
* Write functions with near-automatic string handling and memory management
* Ergonomic memory management API if you want to avoid unnecessary allocations
* No additional build-time tools required—use Cargo and your JavaScript bundler**Note:** This is in early stages, expect frequent API changes for now.
---
**lib.rs**
```rust
extern crate rust_wasm_prelude;
use rust_wasm_prelude::*;pub use rust_wasm_prelude::exports::*;
#[no_mangle]
pub fn to_uppercase(ptr: JsString) -> JsString {
let mut s: String = js_string_input(ptr);
s = s.to_uppercase();
js_string_output(s)
}
```**index.js**
```js
import { Prelude, types } from '@rust-wasm/prelude'
import loadWasm from './lib.rs'const prelude = new Prelude()
loadWasm()
.then(module => module.instance.exports)
.then(exports => {
prelude.withExports(exports)const toUppercase = prelude.wrap(
types.string,
types.string,
exports.to_uppercase
)console.log('uppercase of `test` is', toUppercase('test'))
})
```## Installation
Add Rust dependency to your `Cargo.toml`
```toml
rust-wasm-prelude = "0.3.0"
rust-wasm-utils = "0.3.0"
```Install JavaScript dependency with yarn/npm
```bash
yarn add @rust-wasm/prelude @rust-wasm/utils
```## Examples
* [Basic](/examples/basic): A buildable demo project showing the API essentials
* [Memory Management](/examples/memory): The same as `basic`, but shows how to manage memory manually on the JS side## Acknowledgements
* [HelloRust](https://github.com/badboy/hellorust) for showing how string
passing functions are used by JavaScript and Rust