wasm-bindgen-lite 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +27 -0
- package/Cargo.toml +23 -0
- package/LICENSE +21 -0
- package/README.md +313 -0
- package/bin/wasm-bindgen-lite.js +101 -0
- package/package.json +59 -0
- package/scripts/build.js +91 -0
- package/scripts/test-examples.sh +25 -0
- package/scripts/test.js +48 -0
- package/src/cli/build.js +83 -0
- package/src/cli/config.js +211 -0
- package/src/cli/emit.js +423 -0
- package/src/cli/index.js +79 -0
- package/src/cli/pkg.js +52 -0
- package/src/js/browser-inline.js +19 -0
- package/src/js/browser.js +30 -0
- package/src/js/core.js +57 -0
- package/src/js/node-inline.js +19 -0
- package/src/js/node.js +26 -0
- package/src/js/util.js +14 -0
- package/src/lib.rs +72 -0
package/src/js/util.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export async function instantiateWithFallback(
|
|
2
|
+
trySimdBytes,
|
|
3
|
+
baseBytes,
|
|
4
|
+
imports
|
|
5
|
+
) {
|
|
6
|
+
try {
|
|
7
|
+
const { instance } = await WebAssembly.instantiate(trySimdBytes, imports)
|
|
8
|
+
return { instance, backend: 'wasm-simd' }
|
|
9
|
+
} catch (e) {
|
|
10
|
+
// If SIMD fails (not supported), try baseline
|
|
11
|
+
const { instance } = await WebAssembly.instantiate(baseBytes, imports)
|
|
12
|
+
return { instance, backend: 'wasm' }
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
use std::alloc::{alloc, dealloc, Layout};
|
|
2
|
+
use std::mem;
|
|
3
|
+
|
|
4
|
+
#[no_mangle]
|
|
5
|
+
/// # Safety
|
|
6
|
+
/// This function is unsafe because it allocates memory using the global allocator and returns a raw pointer.
|
|
7
|
+
/// The caller must ensure that the memory is eventually deallocated using `free_bytes` with the same length.
|
|
8
|
+
pub unsafe extern "C" fn alloc_bytes(len: usize) -> *mut u8 {
|
|
9
|
+
let layout = Layout::from_size_align(len, mem::align_of::<u8>()).unwrap();
|
|
10
|
+
alloc(layout)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
#[no_mangle]
|
|
14
|
+
/// # Safety
|
|
15
|
+
/// This function is unsafe because it deallocates memory using a raw pointer.
|
|
16
|
+
/// The caller must ensure that `ptr` was previously allocated by `alloc_bytes` and that `len` is the same as when it was allocated.
|
|
17
|
+
pub unsafe extern "C" fn free_bytes(ptr: *mut u8, len: usize) {
|
|
18
|
+
let layout = Layout::from_size_align(len, mem::align_of::<u8>()).unwrap();
|
|
19
|
+
dealloc(ptr, layout);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// A simple example function that "processes" bytes.
|
|
23
|
+
/// In a real app, this might be SIMD-accelerated base64, crypto, etc.
|
|
24
|
+
///
|
|
25
|
+
/// # Safety
|
|
26
|
+
/// This function is unsafe because it reads from and writes to raw pointers.
|
|
27
|
+
/// The caller must ensure that:
|
|
28
|
+
/// - `in_ptr` points to at least `in_len` bytes of valid memory.
|
|
29
|
+
/// - `out_ptr` points to at least `in_len` bytes of valid memory.
|
|
30
|
+
/// - The memory ranges do not overlap, or if they do, the behavior is acceptable.
|
|
31
|
+
#[no_mangle]
|
|
32
|
+
pub unsafe extern "C" fn process_bytes(
|
|
33
|
+
in_ptr: *const u8,
|
|
34
|
+
in_len: usize,
|
|
35
|
+
out_ptr: *mut u8,
|
|
36
|
+
_out_len: usize,
|
|
37
|
+
) -> isize {
|
|
38
|
+
let input = std::slice::from_raw_parts(in_ptr, in_len);
|
|
39
|
+
let output = std::slice::from_raw_parts_mut(out_ptr, in_len);
|
|
40
|
+
|
|
41
|
+
// Just a simple transformation for demonstration
|
|
42
|
+
for i in 0..in_len {
|
|
43
|
+
output[i] = input[i].wrapping_add(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
in_len as isize
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#[cfg(test)]
|
|
50
|
+
mod tests {
|
|
51
|
+
use super::*;
|
|
52
|
+
|
|
53
|
+
#[test]
|
|
54
|
+
fn test_process_bytes() {
|
|
55
|
+
unsafe {
|
|
56
|
+
let input = b"hello";
|
|
57
|
+
let in_len = input.len();
|
|
58
|
+
let in_ptr = alloc_bytes(in_len);
|
|
59
|
+
std::ptr::copy_nonoverlapping(input.as_ptr(), in_ptr, in_len);
|
|
60
|
+
|
|
61
|
+
let out_ptr = alloc_bytes(in_len);
|
|
62
|
+
let written = process_bytes(in_ptr, in_len, out_ptr, in_len);
|
|
63
|
+
|
|
64
|
+
assert_eq!(written, in_len as isize);
|
|
65
|
+
let output = std::slice::from_raw_parts(out_ptr, in_len);
|
|
66
|
+
assert_eq!(output, b"ifmmp"); // h+1, e+1, l+1, l+1, o+1
|
|
67
|
+
|
|
68
|
+
free_bytes(in_ptr, in_len);
|
|
69
|
+
free_bytes(out_ptr, in_len);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|