├── .gitignore
├── examples
├── canvas
│ ├── example.wasm
│ ├── Makefile
│ ├── Cargo.toml
│ ├── index.html
│ ├── src
│ │ └── lib.rs
│ └── Cargo.lock
├── helloworld
│ ├── example.wasm
│ ├── src
│ │ └── lib.rs
│ ├── index.html
│ ├── Makefile
│ ├── Cargo.toml
│ └── Cargo.lock
└── web_console
│ ├── example.wasm
│ ├── index.html
│ ├── Makefile
│ ├── src
│ └── lib.rs
│ ├── Cargo.toml
│ └── Cargo.lock
├── .npmignore
├── package.json
├── Makefile
├── LICENSE-MIT
├── README.md
├── wasm-module.min.js
├── LICENSE-APACHE
└── wasm-module.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | target
3 |
--------------------------------------------------------------------------------
/examples/canvas/example.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richardanaya/wasm-module/HEAD/examples/canvas/example.wasm
--------------------------------------------------------------------------------
/examples/helloworld/example.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richardanaya/wasm-module/HEAD/examples/helloworld/example.wasm
--------------------------------------------------------------------------------
/examples/web_console/example.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richardanaya/wasm-module/HEAD/examples/web_console/example.wasm
--------------------------------------------------------------------------------
/examples/helloworld/src/lib.rs:
--------------------------------------------------------------------------------
1 | use js_ffi::*;
2 |
3 | #[no_mangle]
4 | pub fn main() -> () {
5 | js!(console.log).invoke_1("Hello World");
6 | }
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | examples
2 | examples_old
3 | node_modules
4 | src
5 | tools
6 | webidl
7 | .gitignore
8 | .npmignore
9 | Makefile
10 | package*
11 |
--------------------------------------------------------------------------------
/examples/helloworld/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/examples/web_console/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/examples/canvas/Makefile:
--------------------------------------------------------------------------------
1 | build:
2 | @RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
3 | @cp target/wasm32-unknown-unknown/release/example.wasm .
4 | lint:
5 | @cargo fmt
6 | serve:
7 | python3 -m http.server 8080
--------------------------------------------------------------------------------
/examples/helloworld/Makefile:
--------------------------------------------------------------------------------
1 | build:
2 | @RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
3 | @cp target/wasm32-unknown-unknown/release/example.wasm .
4 | lint:
5 | @cargo fmt
6 | serve:
7 | python3 -m http.server 8080
--------------------------------------------------------------------------------
/examples/web_console/Makefile:
--------------------------------------------------------------------------------
1 | build:
2 | @RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
3 | @cp target/wasm32-unknown-unknown/release/example.wasm .
4 | lint:
5 | @cargo fmt
6 | serve:
7 | python3 -m http.server 8080
--------------------------------------------------------------------------------
/examples/canvas/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "example"
3 | version = "0.1.0"
4 | authors = ["Richard "]
5 | edition = "2018"
6 |
7 | [lib]
8 | crate-type =["cdylib"]
9 |
10 | [dependencies]
11 | js_ffi = "0.6" # for talking to javascript
--------------------------------------------------------------------------------
/examples/canvas/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/examples/web_console/src/lib.rs:
--------------------------------------------------------------------------------
1 | #[no_mangle]
2 | pub fn main() -> () {
3 | let console = globals::get::().lock();
4 | console.log("You wont see this");
5 | console.clear();
6 | console.time(None);
7 | console.log("Hello world!");
8 | console.time_end(None);
9 | }
10 |
--------------------------------------------------------------------------------
/examples/web_console/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "example"
3 | version = "0.1.0"
4 | authors = ["Richard "]
5 | edition = "2018"
6 |
7 | [lib]
8 | crate-type =["cdylib"]
9 |
10 | [dependencies]
11 | web_console = "0.0.0"
12 | globals = "0.1.3" # optional: for easily getting global singletons that implement Default trait
--------------------------------------------------------------------------------
/examples/helloworld/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "example"
3 | version = "0.1.0"
4 | authors = ["Richard "]
5 | edition = "2018"
6 |
7 | [lib]
8 | crate-type =["cdylib"]
9 |
10 | [dependencies]
11 | js_ffi = "0.6" # for talking to javascript
12 | globals = "0.1.3" # optional: for easily getting global singletons that implement Default trait
--------------------------------------------------------------------------------
/examples/canvas/src/lib.rs:
--------------------------------------------------------------------------------
1 | use js_ffi::*;
2 |
3 | #[no_mangle]
4 | fn main() {
5 | let screen = js!(document.querySelector).call_1(DOCUMENT, "#screen");
6 | let ctx = js!(HTMLCanvasElement.prototype.getContext).call_1(screen, "2d");
7 |
8 | let fill_style = js!(function(color){
9 | this.fillStyle = color;
10 | });
11 | let fill_rect = js!(CanvasRenderingContext2D.prototype.fillRect);
12 |
13 | fill_style.call_1(ctx, "red");
14 | fill_rect.call_4(ctx, 0.0, 0.0, 50.0, 50.0);
15 |
16 | fill_style.call_1(ctx, "green");
17 | fill_rect.call_4(ctx, 15.0, 15.0, 50.0, 50.0);
18 |
19 | fill_style.call_1(ctx, "blue");
20 | fill_rect.call_4(ctx, 30.0, 30.0, 50.0, 50.0);
21 | }
22 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "wasm-module",
3 | "version": "0.1.1",
4 | "description": "A web component for making web assembly modules that can interact with browser easily",
5 | "main": "wasm-module.js",
6 | "directories": {
7 | "example": "examples"
8 | },
9 | "scripts": {
10 | "test": "echo \"Error: no test specified\" && exit 1"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "git+https://github.com/richardanaya/wasm-module.git"
15 | },
16 | "keywords": [
17 | "wasm",
18 | "webidl"
19 | ],
20 | "author": "Richard Anaya",
21 | "license": "MIT OR Apache2.0",
22 | "bugs": {
23 | "url": "https://github.com/richardanaya/wasm-module/issues"
24 | },
25 | "homepage": "https://github.com/richardanaya/wasm-module#readme",
26 | "dependencies": {
27 | "babel-minify": "^0.5.1"
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | everything: setup generate_webidl lint build minify examples
2 | build:
3 | ./node_modules/.bin/rollup src/wasm-module.js --file wasm-module.js --format umd --name webidlLoader
4 | .PHONY: examples
5 | examples:
6 | cd examples/helloworld && make
7 | cd examples/alert && make
8 | cd examples/canvas && make
9 | cd examples/events && make
10 | cd examples/x-clock && make
11 | setup:
12 | npm install
13 | generate_webidl:
14 | node tools/generate_webidl.js Console.webidl Window.webidl Document.webidl HTMLCanvasElement.webidl CanvasRenderingContext2D.webidl EventTarget.webidl KeyboardEvent.webidl MouseEvent.webidl Element.webidl HTMLInputElement.webidl WindowOrWorkerGlobalScope.webidl
15 | lint:
16 | ./node_modules/.bin/prettier --write src/wasm-module.js src/webidl.js tools/generate_webidl.js
17 | minify:
18 | ./node_modules/.bin/babel-minify wasm-module.js -o wasm-module.min.js
19 | publish:
20 | npm publish
21 | serve:
22 | @python3 -m http.server 8080
--------------------------------------------------------------------------------
/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | Copyright (c) 2019 Richard Anaya
2 |
3 | Permission is hereby granted, free of charge, to any
4 | person obtaining a copy of this software and associated
5 | documentation files (the "Software"), to deal in the
6 | Software without restriction, including without
7 | limitation the rights to use, copy, modify, merge,
8 | publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software
10 | is furnished to do so, subject to the following
11 | conditions:
12 |
13 | The above copyright notice and this permission notice
14 | shall be included in all copies or substantial portions
15 | of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 | DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # wasm-module
2 |
3 | A custom HTML element `` that loads your web assembly module and dynamically exposes access to javascript to your web assembly module with no special setup.
4 |
5 | ## Features
6 | - [x] interact with browser imperatively and with no code generation using [js_ffi](https://github.com/richardanaya/js_ffi)
7 | - [x] [standard libraries](https://github.com/richardanaya/wasm-module#standard-web-libraries) that expose new functionality so you don't having to know javascript
8 | - [x] usable for Rust, C, or [any other language that compiles to WASM](https://github.com/appcypher/awesome-wasm-langs)
9 | - [ ] run your wasm module on a separate thread in a web worker
10 |
11 | # Hello World
12 |
13 | See demo [here](https://richardanaya.github.io/wasm-module/examples/helloworld/)
14 |
15 | ```rust
16 | use js_ffi::*;
17 |
18 | #[no_mangle]
19 | pub fn main() -> () {
20 | js!(console.log).invoke_1("Hello World");
21 | }
22 | ```
23 | ```toml
24 | [dependencies]
25 | js_ffi = "0.6" # for talking to javascript
26 | ```
27 | ```makefile
28 | # cli commands for building web assembly
29 | build:
30 | @RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
31 | @cp target/wasm32-unknown-unknown/release/helloworld.wasm .
32 | lint:
33 | @cargo fmt
34 | serve:
35 | python3 -m http.server 8080
36 | ```
37 | ```html
38 |
39 |
40 |
41 | ```
42 | # Drawing
43 |
44 | See demo [here](https://richardanaya.github.io/wasm-module/examples/canvas/)
45 |
46 | ```html
47 |
48 |
49 |
50 |
51 | ```
52 | ```rust
53 | use js_ffi::*;
54 |
55 | #[no_mangle]
56 | fn main() {
57 | let screen = js!(document.querySelector).call_1(DOCUMENT, "#screen");
58 | let ctx = js!(document.querySelector).call_1(screen, "#screen");
59 |
60 | let fill_style = js!(function(color){
61 | this.fillStyle = color;
62 | });
63 | let fill_rect = js!(CanvasRenderingContext2D.prototype.fillRect);
64 |
65 | fill_style.call_1(ctx, "red");
66 | fill_rect.call_4(ctx, 0.0, 0.0, 50.0, 50.0);
67 |
68 | fill_style.call_1(ctx, "green");
69 | fill_rect.call_4(ctx, 15.0, 15.0, 50.0, 50.0);
70 |
71 | fill_style.call_1(ctx, "blue");
72 | fill_rect.call_4(ctx, 30.0, 30.0, 50.0, 50.0);
73 | }
74 | ```
75 |
76 | # Standard Web Libraries
77 |
78 | A collection of libraries exist that expose javascript functionality so you don't have to implement it yourself. Just add them to your project and go!
79 |
80 | * [web_console](https://github.com/richardanaya/web_console)
81 | * [web_random](https://github.com/richardanaya/web_random)
82 | * [web_timer](https://github.com/richardanaya/web_timer)
83 |
84 | # Don't like Rust?
85 |
86 | There's nothing Rust specific about this web component. `js_ffi` has a [technology agnostic interface](https://github.com/richardanaya/js_ffi/#dont-like-rust) that can't be used by many web assembly languages.
87 |
88 | # Want small web assembly modules?
89 | You can drastically reduce the size of your web assembly modules by:
90 |
91 | * be sure to make your library`#![no_std]`
92 | * utilize the `alloc` crate for standard data structues
93 | * ONLY use dependent libraries that are `#![no_std]`
94 | * use a custom allocator like [`wee_alloc`](https://github.com/rustwasm/wee_alloc) instead of the bulky standard jemalloc
95 | * compile in release with flag to strip symbols: `RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release`
96 |
97 | # License
98 |
99 | This project is licensed under either of
100 |
101 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
102 | http://www.apache.org/licenses/LICENSE-2.0)
103 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or
104 | http://opensource.org/licenses/MIT)
105 |
106 | at your option.
107 |
108 | ### Contribution
109 |
110 | Unless you explicitly state otherwise, any contribution intentionally submitted
111 | for inclusion in `wasm-module` by you, as defined in the Apache-2.0 license, shall be
112 | dual licensed as above, without any additional terms or conditions.
113 |
--------------------------------------------------------------------------------
/examples/web_console/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | [[package]]
4 | name = "callback"
5 | version = "0.0.5"
6 | source = "registry+https://github.com/rust-lang/crates.io-index"
7 | dependencies = [
8 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
9 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
10 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
11 | "wasm_common 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
12 | ]
13 |
14 | [[package]]
15 | name = "cstring"
16 | version = "0.0.4"
17 | source = "registry+https://github.com/rust-lang/crates.io-index"
18 | dependencies = [
19 | "cty 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
20 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
21 | ]
22 |
23 | [[package]]
24 | name = "cty"
25 | version = "0.2.0"
26 | source = "registry+https://github.com/rust-lang/crates.io-index"
27 |
28 | [[package]]
29 | name = "example"
30 | version = "0.1.0"
31 | dependencies = [
32 | "globals 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
33 | "web_console 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
34 | ]
35 |
36 | [[package]]
37 | name = "globals"
38 | version = "0.1.3"
39 | source = "registry+https://github.com/rust-lang/crates.io-index"
40 | dependencies = [
41 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
42 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
43 | ]
44 |
45 | [[package]]
46 | name = "js_ffi"
47 | version = "0.0.13"
48 | source = "registry+https://github.com/rust-lang/crates.io-index"
49 | dependencies = [
50 | "callback 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
51 | "cstring 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
52 | "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
53 | "web_common 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
54 | ]
55 |
56 | [[package]]
57 | name = "lazy_static"
58 | version = "1.4.0"
59 | source = "registry+https://github.com/rust-lang/crates.io-index"
60 | dependencies = [
61 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
62 | ]
63 |
64 | [[package]]
65 | name = "memchr"
66 | version = "2.2.1"
67 | source = "registry+https://github.com/rust-lang/crates.io-index"
68 |
69 | [[package]]
70 | name = "spin"
71 | version = "0.5.2"
72 | source = "registry+https://github.com/rust-lang/crates.io-index"
73 |
74 | [[package]]
75 | name = "unreachable"
76 | version = "1.0.0"
77 | source = "registry+https://github.com/rust-lang/crates.io-index"
78 | dependencies = [
79 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
80 | ]
81 |
82 | [[package]]
83 | name = "void"
84 | version = "1.0.2"
85 | source = "registry+https://github.com/rust-lang/crates.io-index"
86 |
87 | [[package]]
88 | name = "wasm_common"
89 | version = "0.0.1"
90 | source = "registry+https://github.com/rust-lang/crates.io-index"
91 |
92 | [[package]]
93 | name = "web_common"
94 | version = "0.0.1"
95 | source = "registry+https://github.com/rust-lang/crates.io-index"
96 |
97 | [[package]]
98 | name = "web_console"
99 | version = "0.0.0"
100 | source = "registry+https://github.com/rust-lang/crates.io-index"
101 | dependencies = [
102 | "js_ffi 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
103 | ]
104 |
105 | [metadata]
106 | "checksum callback 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f4cb91be39183e89a537181974c197d510fa7c52713bcf331ce05fb59c5cec06"
107 | "checksum cstring 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f59de94c81f8ba8b78c997112fa1efdff7d52829e026ce2671bf6ad9b738a29f"
108 | "checksum cty 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "151a4ee37fd024fa6a134a4b506977504fa5dd378f6b7c797818ee41c7b2c23e"
109 | "checksum globals 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d8a382a9faa4ecba7968d794d80d8639687c5c6378771b900f189e7d37c2ef38"
110 | "checksum js_ffi 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "05d590d52834ae84f14dcbbe7cb0e4acfbed7b9def907ed2585bf084e915bd4a"
111 | "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
112 | "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
113 | "checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
114 | "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
115 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
116 | "checksum wasm_common 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "339fe4bc97d1e2a89dd7c2e39079fce2fc746e35a9935ed4edaa8b8b3184bc82"
117 | "checksum web_common 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e30600102354b117d215f08b8086651c7fdacc5581e2864d75e1dee5a204e2b"
118 | "checksum web_console 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "54472e39fe4426ee1233670c62c72342f520c625b5f5497ac354e8e78bec5186"
119 |
--------------------------------------------------------------------------------
/examples/canvas/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | [[package]]
4 | name = "anystring"
5 | version = "0.0.2"
6 | source = "registry+https://github.com/rust-lang/crates.io-index"
7 | dependencies = [
8 | "anystring_macro 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
9 | "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)",
10 | ]
11 |
12 | [[package]]
13 | name = "anystring_macro"
14 | version = "0.0.1"
15 | source = "registry+https://github.com/rust-lang/crates.io-index"
16 | dependencies = [
17 | "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)",
18 | ]
19 |
20 | [[package]]
21 | name = "cstring"
22 | version = "0.0.4"
23 | source = "registry+https://github.com/rust-lang/crates.io-index"
24 | dependencies = [
25 | "cty 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
26 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
27 | ]
28 |
29 | [[package]]
30 | name = "cty"
31 | version = "0.2.0"
32 | source = "registry+https://github.com/rust-lang/crates.io-index"
33 |
34 | [[package]]
35 | name = "example"
36 | version = "0.1.0"
37 | dependencies = [
38 | "js_ffi 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
39 | ]
40 |
41 | [[package]]
42 | name = "js_ffi"
43 | version = "0.6.2"
44 | source = "registry+https://github.com/rust-lang/crates.io-index"
45 | dependencies = [
46 | "anystring 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
47 | "cstring 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
48 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
49 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
50 | ]
51 |
52 | [[package]]
53 | name = "lazy_static"
54 | version = "1.4.0"
55 | source = "registry+https://github.com/rust-lang/crates.io-index"
56 | dependencies = [
57 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
58 | ]
59 |
60 | [[package]]
61 | name = "memchr"
62 | version = "2.2.1"
63 | source = "registry+https://github.com/rust-lang/crates.io-index"
64 |
65 | [[package]]
66 | name = "proc-macro-hack"
67 | version = "0.5.11"
68 | source = "registry+https://github.com/rust-lang/crates.io-index"
69 | dependencies = [
70 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
71 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
72 | "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
73 | ]
74 |
75 | [[package]]
76 | name = "proc-macro2"
77 | version = "1.0.6"
78 | source = "registry+https://github.com/rust-lang/crates.io-index"
79 | dependencies = [
80 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
81 | ]
82 |
83 | [[package]]
84 | name = "quote"
85 | version = "1.0.2"
86 | source = "registry+https://github.com/rust-lang/crates.io-index"
87 | dependencies = [
88 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
89 | ]
90 |
91 | [[package]]
92 | name = "spin"
93 | version = "0.5.2"
94 | source = "registry+https://github.com/rust-lang/crates.io-index"
95 |
96 | [[package]]
97 | name = "syn"
98 | version = "1.0.8"
99 | source = "registry+https://github.com/rust-lang/crates.io-index"
100 | dependencies = [
101 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
102 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
103 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
104 | ]
105 |
106 | [[package]]
107 | name = "unicode-xid"
108 | version = "0.2.0"
109 | source = "registry+https://github.com/rust-lang/crates.io-index"
110 |
111 | [metadata]
112 | "checksum anystring 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "273fdc712cfa78d37e882d7ead8985d33a9f671460ea17754bfec15c20bc0fa3"
113 | "checksum anystring_macro 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "017b7ab0dd69075acb48c5a697748033218fdc40f2f10d3ad2e52600075f2bd0"
114 | "checksum cstring 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f59de94c81f8ba8b78c997112fa1efdff7d52829e026ce2671bf6ad9b738a29f"
115 | "checksum cty 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "151a4ee37fd024fa6a134a4b506977504fa5dd378f6b7c797818ee41c7b2c23e"
116 | "checksum js_ffi 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2b56549124205ed183cbef5734d4e2e588ad5925e4354d9c2cb57d16646604"
117 | "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
118 | "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
119 | "checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5"
120 | "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
121 | "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
122 | "checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
123 | "checksum syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "661641ea2aa15845cddeb97dad000d22070bb5c1fb456b96c1cba883ec691e92"
124 | "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
125 |
--------------------------------------------------------------------------------
/examples/helloworld/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | [[package]]
4 | name = "anystring"
5 | version = "0.0.2"
6 | source = "registry+https://github.com/rust-lang/crates.io-index"
7 | dependencies = [
8 | "anystring_macro 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
9 | "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)",
10 | ]
11 |
12 | [[package]]
13 | name = "anystring_macro"
14 | version = "0.0.1"
15 | source = "registry+https://github.com/rust-lang/crates.io-index"
16 | dependencies = [
17 | "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)",
18 | ]
19 |
20 | [[package]]
21 | name = "cstring"
22 | version = "0.0.4"
23 | source = "registry+https://github.com/rust-lang/crates.io-index"
24 | dependencies = [
25 | "cty 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
26 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
27 | ]
28 |
29 | [[package]]
30 | name = "cty"
31 | version = "0.2.0"
32 | source = "registry+https://github.com/rust-lang/crates.io-index"
33 |
34 | [[package]]
35 | name = "example"
36 | version = "0.1.0"
37 | dependencies = [
38 | "globals 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
39 | "js_ffi 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
40 | ]
41 |
42 | [[package]]
43 | name = "globals"
44 | version = "0.1.3"
45 | source = "registry+https://github.com/rust-lang/crates.io-index"
46 | dependencies = [
47 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
48 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
49 | ]
50 |
51 | [[package]]
52 | name = "js_ffi"
53 | version = "0.6.2"
54 | source = "registry+https://github.com/rust-lang/crates.io-index"
55 | dependencies = [
56 | "anystring 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
57 | "cstring 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
58 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
59 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
60 | ]
61 |
62 | [[package]]
63 | name = "lazy_static"
64 | version = "1.4.0"
65 | source = "registry+https://github.com/rust-lang/crates.io-index"
66 | dependencies = [
67 | "spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
68 | ]
69 |
70 | [[package]]
71 | name = "memchr"
72 | version = "2.2.1"
73 | source = "registry+https://github.com/rust-lang/crates.io-index"
74 |
75 | [[package]]
76 | name = "proc-macro-hack"
77 | version = "0.5.11"
78 | source = "registry+https://github.com/rust-lang/crates.io-index"
79 | dependencies = [
80 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
81 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
82 | "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
83 | ]
84 |
85 | [[package]]
86 | name = "proc-macro2"
87 | version = "1.0.6"
88 | source = "registry+https://github.com/rust-lang/crates.io-index"
89 | dependencies = [
90 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
91 | ]
92 |
93 | [[package]]
94 | name = "quote"
95 | version = "1.0.2"
96 | source = "registry+https://github.com/rust-lang/crates.io-index"
97 | dependencies = [
98 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
99 | ]
100 |
101 | [[package]]
102 | name = "spin"
103 | version = "0.5.2"
104 | source = "registry+https://github.com/rust-lang/crates.io-index"
105 |
106 | [[package]]
107 | name = "syn"
108 | version = "1.0.8"
109 | source = "registry+https://github.com/rust-lang/crates.io-index"
110 | dependencies = [
111 | "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
112 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
113 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
114 | ]
115 |
116 | [[package]]
117 | name = "unicode-xid"
118 | version = "0.2.0"
119 | source = "registry+https://github.com/rust-lang/crates.io-index"
120 |
121 | [metadata]
122 | "checksum anystring 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "273fdc712cfa78d37e882d7ead8985d33a9f671460ea17754bfec15c20bc0fa3"
123 | "checksum anystring_macro 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "017b7ab0dd69075acb48c5a697748033218fdc40f2f10d3ad2e52600075f2bd0"
124 | "checksum cstring 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f59de94c81f8ba8b78c997112fa1efdff7d52829e026ce2671bf6ad9b738a29f"
125 | "checksum cty 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "151a4ee37fd024fa6a134a4b506977504fa5dd378f6b7c797818ee41c7b2c23e"
126 | "checksum globals 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d8a382a9faa4ecba7968d794d80d8639687c5c6378771b900f189e7d37c2ef38"
127 | "checksum js_ffi 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2b56549124205ed183cbef5734d4e2e588ad5925e4354d9c2cb57d16646604"
128 | "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
129 | "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
130 | "checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5"
131 | "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
132 | "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
133 | "checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
134 | "checksum syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "661641ea2aa15845cddeb97dad000d22070bb5c1fb456b96c1cba883ec691e92"
135 | "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
136 |
--------------------------------------------------------------------------------
/wasm-module.min.js:
--------------------------------------------------------------------------------
1 | var js_ffi={run:function(cfg){let allocations=[undefined,null,console,window,document];let empty=[];function allocate(value){const i=empty.length>0?empty.pop():allocations.length;allocations[i]=value;return i}function allocator_release(handle){if(handle>4){delete allocations[handle];empty.push(handle)}}function allocator_get(handle){if(handle<0){return}const ret=allocations[handle];if(handle!==0&&!ret){console.error(`Asked for ${handle} after it was released.`)}return ret}let functions=[(o,p)=>{return o[p]},(o,p,v)=>{o[p]=v}];let mod=null;const TYPE_NOTHING=0;const TYPE_NUM=1;const TYPE_STRING=2;const TYPE_BOOL=3;const TYPE_FUNCTION=4;const TYPE_OBJ=5;const TYPE_UINT8_ARRAY=6;const TYPE_INT8_ARRAY=7;const TYPE_UINT8CLAMPED_ARRAY=8;const TYPE_INT16_ARRAY=9;const TYPE_UINT16_ARRAY=10;const TYPE_INT32_ARRAY=11;const TYPE_UINT32_ARRAY=12;const TYPE_F32_ARRAY=13;const TYPE_F64_ARRAY=14;const TYPE_BI64_ARRAY=15;const TYPE_BUI64_ARRAY=16;const TYPE_MEMORY=17;const utf8dec=new TextDecoder("utf-8");const utf8enc=new TextEncoder("utf-8");function createString(str){let bytes=utf8enc.encode(str+String.fromCharCode(0));let len=bytes.length;let start=mod.instance.exports.jsffimalloc(len);const memory=new Uint8Array(mod.instance.exports.memory.buffer);memory.set(bytes,start);return start}function createTypedArray(r,t,size){let start=mod.instance.exports.jsffimalloc(size*r.length+4);let memory=new Uint32Array(mod.instance.exports.memory.buffer);memory[start/4]=r.length;let data_start=(start+4)/size;memory=new t(mod.instance.exports.memory.buffer);for(let i=0;iresponse.arrayBuffer()).then(bytes=>WebAssembly.instantiate(bytes,{env:{jsffithrowerror:function(e){let err=getStringFromMemory(mod.instance.exports.memory.buffer,e);console.error(err);throw new Error("Web assembly module exited unexpectedly.")},jsffirelease:function(obj){allocator_release(obj)},jsffiregister:function(code){code=getStringFromMemory(mod.instance.exports.memory.buffer,code);let id=functions.length;functions.push(eval("("+code+")"));return id},jsfficall0:function(obj,f){return convertResponse(functions[f].call(allocator_get(obj)))},jsfficall1:function(obj,f,a1_type,a1){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1)))},jsfficall2:function(obj,f,a1_type,a1,a2_type,a2){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2)))},jsfficall3:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3)))},jsfficall4:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4)))},jsfficall5:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4,a5_type,a5){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4),convertArgument(a5_type,a5)))},jsfficall6:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4,a5_type,a5,a6_type,a6){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4),convertArgument(a5_type,a5),convertArgument(a6_type,a6)))},jsfficall7:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4,a5_type,a5,a6_type,a6,a7_type,a7){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4),convertArgument(a5_type,a5),convertArgument(a6_type,a6),convertArgument(a7_type,a7)))},jsfficall8:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4,a5_type,a5,a6_type,a6,a7_type,a7,a8_type,a8){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4),convertArgument(a5_type,a5),convertArgument(a6_type,a6),convertArgument(a7_type,a7),convertArgument(a8_type,a8)))},jsfficall9:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4,a5_type,a5,a6_type,a6,a7_type,a7,a8_type,a8,a9_type,a9){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4),convertArgument(a5_type,a5),convertArgument(a6_type,a6),convertArgument(a7_type,a7),convertArgument(a8_type,a8),convertArgument(a9_type,a9)))},jsfficall10:function(obj,f,a1_type,a1,a2_type,a2,a3_type,a3,a4_type,a4,a5_type,a5,a6_type,a6,a7_type,a7,a8_type,a8,a9_type,a9,a10_type,a10){return convertResponse(functions[f].call(allocator_get(obj),convertArgument(a1_type,a1),convertArgument(a2_type,a2),convertArgument(a3_type,a3),convertArgument(a4_type,a4),convertArgument(a5_type,a5),convertArgument(a6_type,a6),convertArgument(a7_type,a7),convertArgument(a8_type,a8),convertArgument(a9_type,a9),convertArgument(a10_type,a10)))}}}).then(module=>{mod=module;if(cfg.onLoad){cfg.onLoad(module)}module.instance.exports.main()}))}};class WasmModule extends HTMLElement{connectedCallback(){let wasmSrc=this.getAttribute("src");if(!wasmSrc){console.error("no 'src' attribute specified for wasm-module");return}let _this=this;js_ffi.run({path:wasmSrc,onLoad:m=>{_this.module=m}})}}window.customElements.define("wasm-module",WasmModule);
2 |
--------------------------------------------------------------------------------
/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright (c) 2019 Richard Anaya
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------
/wasm-module.js:
--------------------------------------------------------------------------------
1 | var js_ffi = {
2 | run: function(cfg) {
3 | //allocator
4 | let allocations = [undefined, null, console, window, document];
5 | let empty = [];
6 | function allocate(value) {
7 | const i = empty.length > 0 ? empty.pop() : allocations.length;
8 | allocations[i] = value;
9 | return i;
10 | }
11 | function allocator_release(handle) {
12 | if (handle > 4) {
13 | delete allocations[handle];
14 | empty.push(handle);
15 | }
16 | }
17 | function allocator_get(handle) {
18 | if (handle < 0) {
19 | return;
20 | }
21 | const ret = allocations[handle];
22 | if (handle !== 0 && !ret) {
23 | console.error(`Asked for ${handle} after it was released.`);
24 | }
25 | return ret;
26 | }
27 |
28 | let functions = [
29 | // get property
30 | (o,p) => {
31 | return o[p];
32 | },
33 | // set property
34 | (o,p,v) => {
35 | o[p] = v;
36 | }
37 | ];
38 | let mod = null;
39 |
40 | const TYPE_NOTHING = 0;
41 | const TYPE_NUM = 1;
42 | const TYPE_STRING = 2;
43 | const TYPE_BOOL = 3;
44 | const TYPE_FUNCTION = 4;
45 | const TYPE_OBJ = 5;
46 | const TYPE_UINT8_ARRAY = 6;
47 | const TYPE_INT8_ARRAY = 7;
48 | const TYPE_UINT8CLAMPED_ARRAY = 8;
49 | const TYPE_INT16_ARRAY = 9;
50 | const TYPE_UINT16_ARRAY = 10;
51 | const TYPE_INT32_ARRAY = 11;
52 | const TYPE_UINT32_ARRAY = 12;
53 | const TYPE_F32_ARRAY = 13;
54 | const TYPE_F64_ARRAY = 14;
55 | const TYPE_BI64_ARRAY = 15;
56 | const TYPE_BUI64_ARRAY = 16;
57 | const TYPE_MEMORY = 17;
58 |
59 | const utf8dec = new TextDecoder("utf-8");
60 | const utf8enc = new TextEncoder("utf-8");
61 |
62 | function createString(str) {
63 | let bytes = utf8enc.encode(str + String.fromCharCode(0));
64 | let len = bytes.length;
65 | let start = mod.instance.exports.jsffimalloc(len);
66 | const memory = new Uint8Array(mod.instance.exports.memory.buffer);
67 | memory.set(bytes, start);
68 | return start;
69 | }
70 |
71 | function createTypedArray(r,t,size){
72 | let start = mod.instance.exports.jsffimalloc(size*r.length+4);
73 | let memory = new Uint32Array(mod.instance.exports.memory.buffer);
74 | memory[start/4] = r.length;
75 | let data_start = (start+4)/size;
76 | memory = new t(mod.instance.exports.memory.buffer);
77 | for(let i=0;i response.arrayBuffer())
376 | .then(bytes =>
377 | WebAssembly.instantiate(bytes, {
378 | env: {
379 | jsffithrowerror: function(e) {
380 | let err = getStringFromMemory(
381 | mod.instance.exports.memory.buffer,
382 | e
383 | );
384 | console.error(err);
385 | throw new Error("Web assembly module exited unexpectedly.");
386 | },
387 | jsffirelease: function(obj) {
388 | allocator_release(obj);
389 | },
390 | jsffiregister: function(code) {
391 | code = getStringFromMemory(
392 | mod.instance.exports.memory.buffer,
393 | code
394 | );
395 | let id = functions.length;
396 | functions.push(eval("("+code+")"));
397 | return id;
398 | },
399 | jsfficall0: function(obj, f) {
400 | return convertResponse(functions[f].call(allocator_get(obj)));
401 | },
402 | jsfficall1: function(obj, f, a1_type, a1) {
403 | return convertResponse(
404 | functions[f].call(
405 | allocator_get(obj),
406 | convertArgument(a1_type, a1)
407 | )
408 | );
409 | },
410 | jsfficall2: function(obj, f, a1_type, a1, a2_type, a2) {
411 | return convertResponse(
412 | functions[f].call(
413 | allocator_get(obj),
414 | convertArgument(a1_type, a1),
415 | convertArgument(a2_type, a2)
416 | )
417 | );
418 | },
419 | jsfficall3: function(
420 | obj,
421 | f,
422 | a1_type,
423 | a1,
424 | a2_type,
425 | a2,
426 | a3_type,
427 | a3
428 | ) {
429 | return convertResponse(
430 | functions[f].call(
431 | allocator_get(obj),
432 | convertArgument(a1_type, a1),
433 | convertArgument(a2_type, a2),
434 | convertArgument(a3_type, a3)
435 | )
436 | );
437 | },
438 | jsfficall4: function(
439 | obj,
440 | f,
441 | a1_type,
442 | a1,
443 | a2_type,
444 | a2,
445 | a3_type,
446 | a3,
447 | a4_type,
448 | a4
449 | ) {
450 | return convertResponse(
451 | functions[f].call(
452 | allocator_get(obj),
453 | convertArgument(a1_type, a1),
454 | convertArgument(a2_type, a2),
455 | convertArgument(a3_type, a3),
456 | convertArgument(a4_type, a4)
457 | )
458 | );
459 | },
460 | jsfficall5: function(
461 | obj,
462 | f,
463 | a1_type,
464 | a1,
465 | a2_type,
466 | a2,
467 | a3_type,
468 | a3,
469 | a4_type,
470 | a4,
471 | a5_type,
472 | a5
473 | ) {
474 | return convertResponse(
475 | functions[f].call(
476 | allocator_get(obj),
477 | convertArgument(a1_type, a1),
478 | convertArgument(a2_type, a2),
479 | convertArgument(a3_type, a3),
480 | convertArgument(a4_type, a4),
481 | convertArgument(a5_type, a5)
482 | )
483 | );
484 | },
485 | jsfficall6: function(
486 | obj,
487 | f,
488 | a1_type,
489 | a1,
490 | a2_type,
491 | a2,
492 | a3_type,
493 | a3,
494 | a4_type,
495 | a4,
496 | a5_type,
497 | a5,
498 | a6_type,
499 | a6
500 | ) {
501 | return convertResponse(
502 | functions[f].call(
503 | allocator_get(obj),
504 | convertArgument(a1_type, a1),
505 | convertArgument(a2_type, a2),
506 | convertArgument(a3_type, a3),
507 | convertArgument(a4_type, a4),
508 | convertArgument(a5_type, a5),
509 | convertArgument(a6_type, a6)
510 | )
511 | );
512 | },
513 | jsfficall7: function(
514 | obj,
515 | f,
516 | a1_type,
517 | a1,
518 | a2_type,
519 | a2,
520 | a3_type,
521 | a3,
522 | a4_type,
523 | a4,
524 | a5_type,
525 | a5,
526 | a6_type,
527 | a6,
528 | a7_type,
529 | a7
530 | ) {
531 | return convertResponse(
532 | functions[f].call(
533 | allocator_get(obj),
534 | convertArgument(a1_type, a1),
535 | convertArgument(a2_type, a2),
536 | convertArgument(a3_type, a3),
537 | convertArgument(a4_type, a4),
538 | convertArgument(a5_type, a5),
539 | convertArgument(a6_type, a6),
540 | convertArgument(a7_type, a7)
541 | )
542 | );
543 | },
544 | jsfficall8: function(
545 | obj,
546 | f,
547 | a1_type,
548 | a1,
549 | a2_type,
550 | a2,
551 | a3_type,
552 | a3,
553 | a4_type,
554 | a4,
555 | a5_type,
556 | a5,
557 | a6_type,
558 | a6,
559 | a7_type,
560 | a7,
561 | a8_type,
562 | a8
563 | ) {
564 | return convertResponse(
565 | functions[f].call(
566 | allocator_get(obj),
567 | convertArgument(a1_type, a1),
568 | convertArgument(a2_type, a2),
569 | convertArgument(a3_type, a3),
570 | convertArgument(a4_type, a4),
571 | convertArgument(a5_type, a5),
572 | convertArgument(a6_type, a6),
573 | convertArgument(a7_type, a7),
574 | convertArgument(a8_type, a8)
575 | )
576 | );
577 | },
578 | jsfficall9: function(
579 | obj,
580 | f,
581 | a1_type,
582 | a1,
583 | a2_type,
584 | a2,
585 | a3_type,
586 | a3,
587 | a4_type,
588 | a4,
589 | a5_type,
590 | a5,
591 | a6_type,
592 | a6,
593 | a7_type,
594 | a7,
595 | a8_type,
596 | a8,
597 | a9_type,
598 | a9
599 | ) {
600 | return convertResponse(
601 | functions[f].call(
602 | allocator_get(obj),
603 | convertArgument(a1_type, a1),
604 | convertArgument(a2_type, a2),
605 | convertArgument(a3_type, a3),
606 | convertArgument(a4_type, a4),
607 | convertArgument(a5_type, a5),
608 | convertArgument(a6_type, a6),
609 | convertArgument(a7_type, a7),
610 | convertArgument(a8_type, a8),
611 | convertArgument(a9_type, a9)
612 | )
613 | );
614 | },
615 | jsfficall10: function(
616 | obj,
617 | f,
618 | a1_type,
619 | a1,
620 | a2_type,
621 | a2,
622 | a3_type,
623 | a3,
624 | a4_type,
625 | a4,
626 | a5_type,
627 | a5,
628 | a6_type,
629 | a6,
630 | a7_type,
631 | a7,
632 | a8_type,
633 | a8,
634 | a9_type,
635 | a9,
636 | a10_type,
637 | a10
638 | ) {
639 | return convertResponse(
640 | functions[f].call(
641 | allocator_get(obj),
642 | convertArgument(a1_type, a1),
643 | convertArgument(a2_type, a2),
644 | convertArgument(a3_type, a3),
645 | convertArgument(a4_type, a4),
646 | convertArgument(a5_type, a5),
647 | convertArgument(a6_type, a6),
648 | convertArgument(a7_type, a7),
649 | convertArgument(a8_type, a8),
650 | convertArgument(a9_type, a9),
651 | convertArgument(a10_type, a10)
652 | )
653 | );
654 | }
655 | }
656 | }).then(module => {
657 | mod = module;
658 | if (cfg.onLoad) {
659 | cfg.onLoad(module);
660 | }
661 | module.instance.exports.main();
662 | })
663 | );
664 | }
665 | };
666 |
667 | class WasmModule extends HTMLElement {
668 | connectedCallback() {
669 | let wasmSrc = this.getAttribute("src");
670 | if (!wasmSrc) {
671 | console.error("no 'src' attribute specified for wasm-module");
672 | return;
673 | }
674 | let _this = this;
675 | js_ffi.run({
676 | path: wasmSrc,
677 | onLoad: m => {
678 | _this.module = m;
679 | }
680 | });
681 | }
682 | }
683 | window.customElements.define("wasm-module", WasmModule);
684 |
--------------------------------------------------------------------------------