Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial
wasm-bindgen
compatibility
- Loading branch information
Showing
13 changed files
with
242 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[workspace] | ||
members = ["canvas", "drag", "echo", "gamepad", "hasher", "minimal", "todomvc", "webgl"] | ||
members = ["canvas", "drag", "echo", "gamepad", "hasher", "minimal", "todomvc", "webgl", "wasm-bindgen-minimal"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "wasm-bindgen-minimal" | ||
version = "0.1.0" | ||
authors = ["Jan Bujak <j@exia.io>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
wasm-bindgen = "0.2.40" | ||
stdweb = { path = "../..", features = ["wasm-bindgen"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
|
||
set -ex | ||
|
||
wasm-pack build --target web | ||
python3 -m http.server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<html> | ||
<head> | ||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> | ||
</head> | ||
<body> | ||
<!-- Note the usage of `type=module` here as this is an ES6 module --> | ||
<script type="module"> | ||
// Use ES module import syntax to import functionality from the module | ||
// that we have compiled. | ||
// | ||
// Note that the `default` import is an initialization function which | ||
// will "boot" the module and make it ready to use. Currently browsers | ||
// don't support natively imported WebAssembly as an ES module, but | ||
// eventually the manual initialization won't be required! | ||
import { default as init } from './pkg/wasm_bindgen_minimal.js'; | ||
|
||
async function run() { | ||
// First up we need to actually load the wasm file, so we use the | ||
// default export to inform it where the wasm file is located on the | ||
// server, and then we wait on the returned promise to wait for the | ||
// wasm to be loaded. | ||
// | ||
// Note that instead of a string here you can also pass in an instance | ||
// of `WebAssembly.Module` which allows you to compile your own module. | ||
// Also note that the promise, when resolved, yields the wasm module's | ||
// exports which is the same as importing the `*_bg` module in other | ||
// modes | ||
// await init('./pkg/wasm_bindgen_minimal_bg.wasm'); | ||
|
||
const url = await fetch('http://localhost:8000/pkg/wasm_bindgen_minimal_bg.wasm'); | ||
const body = await url.arrayBuffer(); | ||
const module = await WebAssembly.compile(body); | ||
await init(module); | ||
} | ||
|
||
run(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[macro_use] | ||
extern crate stdweb; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen(start)] | ||
pub fn main() -> Result<(), JsValue> { | ||
let message = "Hello, 世界!"; | ||
js! { | ||
alert( @{message} ); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] mod emscripten; | ||
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] pub use self::emscripten::*; | ||
|
||
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] mod wasm; | ||
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] pub use self::wasm::*; | ||
#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "wasm-bindgen")))] mod wasm; | ||
#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "wasm-bindgen")))] pub use self::wasm::*; | ||
|
||
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "wasm-bindgen"))] mod wasm_bindgen; | ||
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "wasm-bindgen"))] pub use self::wasm_bindgen::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::mem; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use webcore::initialization::initialize as initialize_crate; | ||
|
||
pub fn event_loop() { | ||
} | ||
|
||
fn alloc( size: usize ) -> *mut u8 { | ||
let mut buffer = Vec::with_capacity( size ); | ||
let ptr = buffer.as_mut_ptr(); | ||
mem::forget( buffer ); | ||
ptr | ||
} | ||
|
||
fn free( ptr: *mut u8, capacity: usize ) { | ||
unsafe { | ||
let _ = Vec::from_raw_parts( ptr, 0, capacity ); | ||
} | ||
} | ||
|
||
pub unsafe fn dealloc( ptr: *mut u8, capacity: usize ) { | ||
free( ptr, capacity ) | ||
} | ||
|
||
struct Module( Option< JsValue > ); | ||
unsafe impl Sync for Module {} | ||
|
||
static mut MODULE: Module = Module( None ); | ||
|
||
pub fn initialize() { | ||
#[wasm_bindgen(inline_js = r#"export function wasm_bindgen_initialize( memory, alloc, free ) { | ||
var Module = {}; | ||
Module.web_malloc = alloc; | ||
Module.web_free = free; | ||
Module.web_table = null; | ||
function define_heap( target ) { | ||
Object.defineProperty( target, "HEAP8", { | ||
get: function() { return new Int8Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAP16", { | ||
get: function() { return new Int16Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAP32", { | ||
get: function() { return new Int32Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAPU8", { | ||
get: function() { return new Uint8Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAPU16", { | ||
get: function() { return new Uint16Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAPU32", { | ||
get: function() { return new Uint32Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAPF32", { | ||
get: function() { return new Float32Array( memory.buffer ); } | ||
}); | ||
Object.defineProperty( target, "HEAPF64", { | ||
get: function() { return new Float64Array( memory.buffer ); } | ||
}); | ||
} | ||
if( typeof global !== "undefined" ) { | ||
define_heap( global ); | ||
} | ||
if( typeof window !== "undefined" ) { | ||
define_heap( window ); | ||
} | ||
return Module; | ||
}"#)] | ||
extern "C" { | ||
fn wasm_bindgen_initialize( | ||
memory: JsValue, | ||
alloc: &dyn Fn( usize ) -> *mut u8, | ||
free: &dyn Fn( *mut u8, usize ) | ||
) -> JsValue; | ||
} | ||
|
||
let memory = wasm_bindgen::memory(); | ||
unsafe { | ||
let module = wasm_bindgen_initialize( memory, &alloc, &free ); | ||
MODULE = Module( Some( module ) ); | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn get_module() -> JsValue { | ||
initialize_crate(); | ||
|
||
unsafe { | ||
MODULE.0.as_ref().unwrap().clone() | ||
} | ||
} | ||
|
||
pub mod exports {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters