├── .gitignore ├── examples ├── canvas │ ├── .gitignore │ ├── example.wasm │ ├── index.html │ ├── Makefile │ ├── Cargo.toml │ └── src │ │ └── lib.rs ├── jquery │ ├── .gitignore │ ├── example.wasm │ ├── Makefile │ ├── index.html │ ├── Cargo.toml │ └── src │ │ └── lib.rs ├── timer │ ├── .gitignore │ ├── example.wasm │ ├── index.html │ ├── Makefile │ ├── Cargo.toml │ └── src │ │ └── lib.rs ├── helloworld │ ├── .gitignore │ ├── src │ │ └── lib.rs │ ├── index.html │ ├── example.wasm │ ├── Makefile │ └── Cargo.toml ├── typed_array │ ├── .gitignore │ ├── example.wasm │ ├── index.html │ ├── Makefile │ ├── Cargo.toml │ └── src │ │ └── lib.rs ├── button_click │ ├── .gitignore │ ├── example.wasm │ ├── index.html │ ├── Makefile │ ├── Cargo.toml │ └── src │ │ └── lib.rs ├── helloworld_async │ ├── .gitignore │ ├── index.html │ ├── example.wasm │ ├── Makefile │ ├── Cargo.toml │ └── src │ │ └── lib.rs └── helloworld_no_std │ ├── .gitignore │ ├── index.html │ ├── example.wasm │ ├── Makefile │ ├── Cargo.toml │ └── src │ └── lib.rs ├── README.md ├── examples_in_c ├── canvas │ ├── main.wasm │ ├── index.html │ ├── Makefile │ └── src │ │ └── main.c ├── jquery │ ├── main.wasm │ ├── src │ │ └── main.c │ ├── index.html │ └── Makefile └── helloworld │ ├── main.wasm │ ├── index.html │ ├── src │ └── main.c │ └── Makefile ├── Makefile ├── Cargo.toml ├── LICENSE-MIT ├── js_ffi.h ├── LICENSE-APACHE ├── js_ffi.js └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/canvas/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/jquery/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/timer/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/helloworld/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/typed_array/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/button_click/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/helloworld_async/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /examples/helloworld_no_std/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This library is obsolete, check out https://github.com/richardanaya/js-wasm 2 | -------------------------------------------------------------------------------- /examples/helloworld/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub fn main() -> usize { 3 | return 42; 4 | } 5 | -------------------------------------------------------------------------------- /examples/canvas/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/canvas/example.wasm -------------------------------------------------------------------------------- /examples/jquery/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/jquery/example.wasm -------------------------------------------------------------------------------- /examples/timer/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/timer/example.wasm -------------------------------------------------------------------------------- /examples/timer/index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /examples/helloworld/index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /examples_in_c/canvas/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples_in_c/canvas/main.wasm -------------------------------------------------------------------------------- /examples_in_c/jquery/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples_in_c/jquery/main.wasm -------------------------------------------------------------------------------- /examples/button_click/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/button_click/example.wasm -------------------------------------------------------------------------------- /examples/helloworld/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/helloworld/example.wasm -------------------------------------------------------------------------------- /examples/typed_array/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/typed_array/example.wasm -------------------------------------------------------------------------------- /examples/typed_array/index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /examples_in_c/helloworld/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples_in_c/helloworld/main.wasm -------------------------------------------------------------------------------- /examples/helloworld_async/index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /examples/helloworld_no_std/index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /examples/helloworld_async/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/helloworld_async/example.wasm -------------------------------------------------------------------------------- /examples/helloworld_no_std/example.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/js_ffi/HEAD/examples/helloworld_no_std/example.wasm -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | @cargo build --target wasm32-unknown-unknown 3 | lint: 4 | @cargo fmt 5 | serve: 6 | @python3 -m http.server 8080 -------------------------------------------------------------------------------- /examples/button_click/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /examples/canvas/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /examples_in_c/helloworld/index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /examples/jquery/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 -------------------------------------------------------------------------------- /examples/button_click/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 -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /examples/typed_array/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 -------------------------------------------------------------------------------- /examples/helloworld_no_std/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 -------------------------------------------------------------------------------- /examples_in_c/canvas/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /examples_in_c/helloworld/src/main.c: -------------------------------------------------------------------------------- 1 | #include "../../js_ffi.h" 2 | 3 | export int main() { 4 | int log = jsffiregister("window.alert"); 5 | jsfficall1(JS_UNDEFINED,log,TYPE_STRING,(JSValue)(int)&"Hello World!"); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /examples/timer/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 | serve: 5 | @python3 -m http.server 8080 6 | lint: 7 | @cargo fmt -------------------------------------------------------------------------------- /examples_in_c/jquery/src/main.c: -------------------------------------------------------------------------------- 1 | #include "../../js_ffi.h" 2 | 3 | export int main() { 4 | int log = jsffiregister("function(msg) { $(\"body\").html(msg); }"); 5 | jsfficall1(JS_UNDEFINED,log,TYPE_STRING,(JSValue)(int)&"Hello World!"); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /examples_in_c/jquery/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /examples/helloworld_async/Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | @RUSTFLAGS='-C link-arg=-s' cargo +nightly build --target wasm32-unknown-unknown --release 3 | @cp target/wasm32-unknown-unknown/release/example.wasm . 4 | serve: 5 | @python3 -m http.server 8080 6 | lint: 7 | @cargo fmt -------------------------------------------------------------------------------- /examples/jquery/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | -------------------------------------------------------------------------------- /examples_in_c/canvas/Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | clang -O3 --target=wasm32 -nostdlib -I./ -Wl,--no-entry -Wl,-allow-undefined -Wl,--export-dynamic -o main.wasm src/main.c 3 | serve: 4 | python3 -m http.server 8080 5 | lint: 6 | clang-lint -i src/main.c 7 | deps: 8 | sudo dnf install clang llvm lld 9 | -------------------------------------------------------------------------------- /examples_in_c/helloworld/Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | clang -O3 --target=wasm32 -nostdlib -I./ -Wl,--no-entry -Wl,-allow-undefined -Wl,--export-dynamic -o main.wasm src/main.c 3 | serve: 4 | python3 -m http.server 8080 5 | lint: 6 | clang-lint -i src/main.c 7 | deps: 8 | sudo dnf install clang llvm lld 9 | -------------------------------------------------------------------------------- /examples_in_c/jquery/Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | clang -O3 --target=wasm32 -nostdlib -I./ -Wl,--no-entry -Wl,-allow-undefined -Wl,--export-dynamic -o main.wasm src/main.c 3 | serve: 4 | python3 -m http.server 8080 5 | lint: 6 | clang-lint -i src/main.c 7 | deps: 8 | sudo dnf install clang llvm lld 9 | -------------------------------------------------------------------------------- /examples/helloworld/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | js_ffi = {path="../../"} 11 | malloc = "0.0.1" 12 | 13 | [lib] 14 | crate-type =["cdylib"] 15 | 16 | [profile.release] 17 | lto = true -------------------------------------------------------------------------------- /examples/helloworld_no_std/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | js_ffi = {path="../../"} 11 | wee_alloc = "0" 12 | 13 | [lib] 14 | crate-type =["cdylib"] 15 | 16 | [profile.release] 17 | lto = true -------------------------------------------------------------------------------- /examples/canvas/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | executor = "0.0.3" 11 | js_ffi = {path="../../"} 12 | 13 | 14 | [lib] 15 | crate-type =["cdylib"] 16 | 17 | [profile.release] 18 | lto = true 19 | opt-level = 's' -------------------------------------------------------------------------------- /examples/jquery/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | executor = "0.0.3" 11 | js_ffi = {path="../../"} 12 | 13 | 14 | [lib] 15 | crate-type =["cdylib"] 16 | 17 | [profile.release] 18 | lto = true 19 | opt-level = 's' -------------------------------------------------------------------------------- /examples/button_click/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | executor = "0.0.3" 11 | js_ffi = {path="../../"} 12 | 13 | 14 | [lib] 15 | crate-type =["cdylib"] 16 | 17 | [profile.release] 18 | lto = true 19 | opt-level = 's' -------------------------------------------------------------------------------- /examples/typed_array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | executor = "0.0.3" 11 | js_ffi = {path="../../"} 12 | 13 | 14 | [lib] 15 | crate-type =["cdylib"] 16 | 17 | [profile.release] 18 | lto = true 19 | opt-level = 's' -------------------------------------------------------------------------------- /examples/button_click/src/lib.rs: -------------------------------------------------------------------------------- 1 | use js_ffi::*; 2 | 3 | #[no_mangle] 4 | fn main() { 5 | let btn = register_function("document.querySelector").call_1(DOCUMENT, "#button").to_js_object(); 6 | register_function("Node.prototype.addEventListener").call_2( 7 | btn, 8 | "click", 9 | create_callback_0(|| { 10 | register_function("window.alert").invoke_1("I was clicked"); 11 | }), 12 | ); 13 | } 14 | -------------------------------------------------------------------------------- /examples/helloworld_async/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | executor = "0" 11 | js_ffi = {path="../../"} 12 | wee_alloc = "0" 13 | 14 | [lib] 15 | crate-type =["cdylib"] 16 | 17 | [profile.release] 18 | lto = true 19 | opt-level = 's' 20 | -------------------------------------------------------------------------------- /examples/timer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example" 3 | version = "0.1.0" 4 | authors = ["richard "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | once_cell = "1.2.0" 11 | spin = "0.5.2" 12 | executor = "0.0.4" 13 | js_ffi = {path="../../"} 14 | 15 | [lib] 16 | crate-type =["cdylib"] 17 | 18 | [profile.release] 19 | lto = true 20 | opt-level = 's' -------------------------------------------------------------------------------- /examples/jquery/src/lib.rs: -------------------------------------------------------------------------------- 1 | use js_ffi::*; 2 | 3 | #[no_mangle] 4 | fn main() { 5 | let jquery_handle = register_function("$"); 6 | let jquery_on_handle = register_function("jQuery.prototype.on"); 7 | let alert = register_function("(msg)=>window.alert(msg)"); 8 | 9 | let body = jquery_handle.invoke_1("body").to_js_object(); 10 | jquery_on_handle.call_2( 11 | &body, 12 | "click", 13 | create_callback_1(move |_event| { 14 | alert.invoke_1("I was clicked!"); 15 | }), 16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "js_ffi" 3 | version = "0.8.0" 4 | authors = ["Richard Anaya"] 5 | edition = "2018" 6 | description = "A FFI library for calling javascript" 7 | license = "MIT OR Apache-2.0" 8 | categories = ["wasm", "no-std"] 9 | repository = "https://github.com/richardanaya/js_ffi" 10 | readme="README.md" 11 | 12 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 13 | 14 | [dependencies] 15 | cstring = "0.0" 16 | spin = "0.5" 17 | 18 | [dependencies.lazy_static] 19 | version = "1.4.0" 20 | features = ["spin_no_std"] 21 | -------------------------------------------------------------------------------- /examples/helloworld_no_std/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![feature(alloc_error_handler)] 3 | 4 | use js_ffi::*; 5 | 6 | #[no_mangle] 7 | pub fn main() -> () { 8 | register_function("console.log").invoke_1("Hello World"); 9 | } 10 | 11 | extern crate wee_alloc; 12 | 13 | #[global_allocator] 14 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 15 | 16 | #[panic_handler] 17 | fn panic(_info: &core::panic::PanicInfo) -> ! { 18 | throw_error("A panic occurred"); 19 | loop {} 20 | } 21 | 22 | #[alloc_error_handler] 23 | fn oom(_: core::alloc::Layout) -> ! { 24 | throw_error("Ran out of memory"); 25 | loop {} 26 | } 27 | -------------------------------------------------------------------------------- /examples/canvas/src/lib.rs: -------------------------------------------------------------------------------- 1 | use js_ffi::*; 2 | 3 | #[no_mangle] 4 | fn main() { 5 | let screen = register_function("document.querySelector").call_1(&DOCUMENT, "#screen").to_js_object(); 6 | let ctx = register_function("HTMLCanvasElement.prototype.getContext").call_1(&screen, "2d").to_js_object(); 7 | 8 | let fill_style = register_function("function(color){ 9 | this.fillStyle = color; 10 | }"); 11 | let fill_rect = register_function("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 | -------------------------------------------------------------------------------- /examples/typed_array/src/lib.rs: -------------------------------------------------------------------------------- 1 | use js_ffi::*; 2 | 3 | #[no_mangle] 4 | pub fn main() -> () { 5 | let log = register_function("console.log"); 6 | log.invoke_1(&vec![1u8, 2, 3]); 7 | log.invoke_1(&vec![4u32, 8, 12]); 8 | log.invoke_1(&vec![-1i32, 1000, -1000]); 9 | log.invoke_1(&vec![3.14_f64, 0.0_f64, -1.0_f64]); 10 | log.invoke_1(&vec![1.2, 0.0, -2.333]); 11 | let a = vec![1.0,2.0]; 12 | // send array to js and send it back immediately 13 | let result = register_function("(data)=>data").invoke_1(&a).to_vec::(); 14 | log.invoke_1(result.len()); 15 | log.invoke_1(result[0]); 16 | log.invoke_1(result[1]); 17 | // make sure its same values 18 | assert_eq!(a,result); 19 | 20 | 21 | let b = vec![1u8,5,6]; 22 | // send array to js and send it back immediately 23 | let result2 = register_function("(data)=>data").invoke_1(&b).to_vec::(); 24 | // make sure its same values 25 | assert_eq!(b,result2); 26 | log.invoke_1(result2[1]); 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | 2 | Copyright 2020 Richard Anaya 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /examples_in_c/canvas/src/main.c: -------------------------------------------------------------------------------- 1 | #include "../../js_ffi.h" 2 | 3 | export int main() { 4 | int fn_querySelector = jsffiregister("document.querySelector"); 5 | int fn_getContext = jsffiregister("HTMLCanvasElement.prototype.getContext"); 6 | int fn_setFill = jsffiregister("function(color){ this.fillStyle = color; }"); 7 | int fn_fillRect = jsffiregister("CanvasRenderingContext2D.prototype.fillRect"); 8 | 9 | int screen = jsfficall1(DOCUMENT,fn_querySelector,TYPE_STRING,(JSValue)(int)&"#screen"); 10 | int ctx = jsfficall1(screen,fn_getContext,TYPE_STRING,(JSValue)(int)&"2d"); 11 | 12 | 13 | jsfficall1(ctx,fn_setFill,TYPE_STRING,(JSValue)(int)&"red"); 14 | jsfficall4(ctx,fn_fillRect,TYPE_NUM,0.0,TYPE_NUM,0.0,TYPE_NUM,50.0,TYPE_NUM,50.0); 15 | 16 | jsfficall1(ctx,fn_setFill,TYPE_STRING,(JSValue)(int)&"green"); 17 | jsfficall4(ctx,fn_fillRect,TYPE_NUM,15.0,TYPE_NUM,15.0,TYPE_NUM,50.0,TYPE_NUM,50.0); 18 | 19 | jsfficall1(ctx,fn_setFill,TYPE_STRING,(JSValue)(int)&"blue"); 20 | jsfficall4(ctx,fn_fillRect,TYPE_NUM,30.0,TYPE_NUM,30.0,TYPE_NUM,50.0,TYPE_NUM,50.0); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /examples/helloworld_async/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![feature(alloc_error_handler)] 3 | 4 | use js_ffi::*; 5 | 6 | #[no_mangle] 7 | pub fn main() -> () { 8 | executor::spawn(async { 9 | let console_log = register_function("console.log"); 10 | console_log.invoke_1("Hello"); 11 | sleep(1000).await; 12 | console_log.invoke_1("world!"); 13 | }); 14 | } 15 | 16 | fn sleep(millis: u32) -> impl core::future::Future { 17 | let set_timeout = register_function("window.setTimeout"); 18 | let (future, cb) = create_callback_future_0(); 19 | set_timeout.invoke_2(cb, millis); 20 | future 21 | } 22 | 23 | // memory allocator stuff 24 | 25 | extern crate wee_alloc; 26 | 27 | // Use `wee_alloc` as the global allocator. 28 | #[global_allocator] 29 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 30 | 31 | #[panic_handler] 32 | fn panic(_info: &core::panic::PanicInfo) -> ! { 33 | throw_error("A panic occurred"); 34 | loop {} 35 | } 36 | 37 | #[alloc_error_handler] 38 | fn oom(_: core::alloc::Layout) -> ! { 39 | throw_error("Ran out of memory"); 40 | loop {} 41 | } -------------------------------------------------------------------------------- /examples/timer/src/lib.rs: -------------------------------------------------------------------------------- 1 | use core::future::Future; 2 | use executor::Executor; 3 | use js_ffi::*; 4 | use once_cell::sync::Lazy; 5 | use spin::Mutex; 6 | 7 | static JS_API: Lazy> = Lazy::new(|| { 8 | let a = API { 9 | log_handle: register_function("console.log"), 10 | set_interval_handle: register_function("window.setInterval"), 11 | set_timeout_handle: register_function("window.setTimeout"), 12 | }; 13 | Mutex::new(a) 14 | }); 15 | 16 | #[no_mangle] 17 | pub fn main() -> () { 18 | JS_API.lock().window_set_interval( 19 | || { 20 | Executor::spawn(async { 21 | let api = JS_API.lock(); 22 | api.console_log("Tic"); 23 | api.window_set_timeout(500).await; 24 | api.console_log("Toc"); 25 | }); 26 | }, 27 | 1000, 28 | ); 29 | } 30 | 31 | struct API { 32 | log_handle: JSInvoker, 33 | set_timeout_handle: JSInvoker, 34 | set_interval_handle: JSInvoker, 35 | } 36 | 37 | impl API { 38 | fn console_log(&self, msg: &str) { 39 | self.log_handle.invoke_1(msg); 40 | } 41 | 42 | fn window_set_interval(&self, cb: impl FnMut() -> () + Send + 'static, millis: i32) { 43 | let cb = create_callback_0(cb); 44 | self.set_interval_handle.invoke_2(cb, millis); 45 | } 46 | 47 | fn window_set_timeout(&self, millis: i32) -> impl Future { 48 | let (future, cb) = create_callback_future_0(); 49 | self.set_timeout_handle.invoke_2(cb, millis); 50 | future 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /js_ffi.h: -------------------------------------------------------------------------------- 1 | #ifndef export 2 | #define export __attribute__((visibility("default"))) 3 | #endif 4 | 5 | #ifndef JS_FFI_H 6 | typedef double JSValue; 7 | extern void jsffirelease(JSValue); 8 | extern void jsffithrowerror(char*); 9 | extern int jsffiregister(char*); 10 | extern JSValue jsfficall0(JSValue,int); 11 | extern JSValue jsfficall1(JSValue,int,int,JSValue); 12 | extern JSValue jsfficall2(JSValue,int,int,JSValue,int,JSValue); 13 | extern JSValue jsfficall3(JSValue,int,int,JSValue,int,JSValue,int,JSValue); 14 | extern JSValue jsfficall4(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 15 | extern JSValue jsfficall5(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 16 | extern JSValue jsfficall6(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 17 | extern JSValue jsfficall7(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 18 | extern JSValue jsfficall8(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 19 | extern JSValue jsfficall9(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 20 | extern JSValue jsfficall10(JSValue,int,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue,int,JSValue); 21 | 22 | JSValue const JS_UNDEFINED = 0.0; 23 | JSValue const JS_NULL = 1.0; 24 | JSValue const JS_FALSE = 0.0; 25 | JSValue const JS_TRUE = 1.0; 26 | JSValue const CONSOLE = 2.0; 27 | JSValue const WINDOW = 3.0; 28 | JSValue const DOCUMENT = 4.0; 29 | 30 | int const TYPE_NOTHING = 0; 31 | int const TYPE_NUM = 1; 32 | int const TYPE_STRING = 2; 33 | int const TYPE_BOOL = 3; 34 | int const TYPE_FUNCTION = 4; 35 | int const TYPE_OBJ = 5; 36 | int const TYPE_UINT8_ARRAY = 6; 37 | int const TYPE_INT8_ARRAY = 7; 38 | int const TYPE_UINT8CLAMPED_ARRAY = 8; 39 | int const TYPE_INT16_ARRAY = 9; 40 | int const TYPE_UINT16_ARRAY = 10; 41 | int const TYPE_INT32_ARRAY = 11; 42 | int const TYPE_UINT32_ARRAY = 12; 43 | int const TYPE_F32_ARRAY = 13; 44 | int const TYPE_F64_ARRAY = 14; 45 | int const TYPE_BI64_ARRAY = 15; 46 | int const TYPE_BUI64_ARRAY = 16; 47 | int const TYPE_MEMORY = 17; 48 | #endif -------------------------------------------------------------------------------- /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. -------------------------------------------------------------------------------- /js_ffi.js: -------------------------------------------------------------------------------- 1 | var js_ffi = { 2 | run: function(cfg) { 3 | //allocator 4 | let allocations = [ 5 | undefined, 6 | null, 7 | console, 8 | self, 9 | typeof document != "undefined" ? document : null 10 | ]; 11 | let empty = []; 12 | function allocate(value) { 13 | const i = empty.length > 0 ? empty.pop() : allocations.length; 14 | allocations[i] = value; 15 | return i; 16 | } 17 | function allocator_release(handle) { 18 | if (handle > 4) { 19 | delete allocations[handle]; 20 | empty.push(handle); 21 | } 22 | } 23 | function allocator_get(handle) { 24 | if (handle < 0) { 25 | return; 26 | } 27 | const ret = allocations[handle]; 28 | if (handle !== 0 && !ret) { 29 | console.error(`Asked for ${handle} after it was released.`); 30 | } 31 | return ret; 32 | } 33 | 34 | let functions = [ 35 | // get property 36 | (o, p) => { 37 | return o[p]; 38 | }, 39 | // set property 40 | (o, p, v) => { 41 | o[p] = v; 42 | } 43 | ]; 44 | let mod = null; 45 | 46 | const TYPE_NOTHING = 0; 47 | const TYPE_NUM = 1; 48 | const TYPE_STRING = 2; 49 | const TYPE_BOOL = 3; 50 | const TYPE_FUNCTION = 4; 51 | const TYPE_OBJ = 5; 52 | const TYPE_UINT8_ARRAY = 6; 53 | const TYPE_INT8_ARRAY = 7; 54 | const TYPE_UINT8CLAMPED_ARRAY = 8; 55 | const TYPE_INT16_ARRAY = 9; 56 | const TYPE_UINT16_ARRAY = 10; 57 | const TYPE_INT32_ARRAY = 11; 58 | const TYPE_UINT32_ARRAY = 12; 59 | const TYPE_F32_ARRAY = 13; 60 | const TYPE_F64_ARRAY = 14; 61 | const TYPE_BI64_ARRAY = 15; 62 | const TYPE_BUI64_ARRAY = 16; 63 | const TYPE_MEMORY = 17; 64 | 65 | const utf8dec = new TextDecoder("utf-8"); 66 | const utf8enc = new TextEncoder("utf-8"); 67 | 68 | function createString(str) { 69 | let bytes = utf8enc.encode(str + String.fromCharCode(0)); 70 | let len = bytes.length; 71 | let start = mod.instance.exports.jsffimalloc(len); 72 | const memory = new Uint8Array(mod.instance.exports.memory.buffer); 73 | memory.set(bytes, start); 74 | return start; 75 | } 76 | 77 | function createTypedArray(r, t, size) { 78 | let start = mod.instance.exports.jsffimalloc(size * r.length + 4); 79 | let memory = new Uint32Array(mod.instance.exports.memory.buffer); 80 | memory[start / 4] = r.length; 81 | let data_start = (start + 4) / size; 82 | memory = new t(mod.instance.exports.memory.buffer); 83 | for (let i = 0; i < r.length; i++) { 84 | memory[data_start + i] = r[i]; 85 | } 86 | return start; 87 | } 88 | 89 | function getStringFromMemory(mem, start) { 90 | const data = new Uint8Array(mem); 91 | const str = []; 92 | let i = start; 93 | while (data[i] !== 0) { 94 | str.push(data[i]); 95 | i++; 96 | } 97 | return utf8dec.decode(new Uint8Array(str)); 98 | } 99 | 100 | function getTypedArrayFromMemory(t, mem, start, size) { 101 | const data32 = new Uint32Array(mem); 102 | const ptr = data32[start / 4]; 103 | const offset = data32[ptr / 4]; 104 | const length = data32[ptr / 4 + 1]; 105 | let b = mem.slice(offset, offset + length * size); 106 | let a = new t(b); 107 | return a; 108 | } 109 | 110 | function convertArgument(val_type, val) { 111 | if (val_type == TYPE_NUM) { 112 | } else if (val_type === TYPE_NOTHING) { 113 | val = undefined; 114 | } else if (val_type === TYPE_STRING) { 115 | val = getStringFromMemory(mod.instance.exports.memory.buffer, val); 116 | } else if (val_type === TYPE_BOOL) { 117 | val = val != 0; 118 | } else if (val_type === TYPE_OBJ) { 119 | val = allocator_get(val); 120 | } else if (val_type === TYPE_FUNCTION) { 121 | let id = val; 122 | val = function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) { 123 | let l = arguments.length; 124 | if (l === 0) { 125 | mod.instance.exports.jsfficallback( 126 | id, 127 | 0, 128 | 0, 129 | 0, 130 | 0, 131 | 0, 132 | 0, 133 | 0, 134 | 0, 135 | 0, 136 | 0 137 | ); 138 | } else if (l === 1) { 139 | mod.instance.exports.jsfficallback( 140 | id, 141 | convertResponse(a1), 142 | 0, 143 | 0, 144 | 0, 145 | 0, 146 | 0, 147 | 0, 148 | 0, 149 | 0, 150 | 0 151 | ); 152 | } else if (l === 2) { 153 | mod.instance.exports.jsfficallback( 154 | id, 155 | convertResponse(a1), 156 | convertResponse(a2), 157 | 0, 158 | 0, 159 | 0, 160 | 0, 161 | 0, 162 | 0, 163 | 0, 164 | 0 165 | ); 166 | } else if (l === 3) { 167 | mod.instance.exports.jsfficallback( 168 | id, 169 | convertResponse(a1), 170 | convertResponse(a2), 171 | convertResponse(a3), 172 | 0, 173 | 0, 174 | 0, 175 | 0, 176 | 0, 177 | 0, 178 | 0 179 | ); 180 | } else if (l === 4) { 181 | mod.instance.exports.jsfficallback( 182 | id, 183 | convertResponse(a1), 184 | convertResponse(a2), 185 | convertResponse(a3), 186 | convertResponse(a4), 187 | 0, 188 | 0, 189 | 0, 190 | 0, 191 | 0, 192 | 0 193 | ); 194 | } else if (l === 5) { 195 | mod.instance.exports.jsfficallback( 196 | id, 197 | convertResponse(a1), 198 | convertResponse(a2), 199 | convertResponse(a3), 200 | convertResponse(a4), 201 | convertResponse(a5), 202 | 0, 203 | 0, 204 | 0, 205 | 0, 206 | 0 207 | ); 208 | } else if (l === 6) { 209 | mod.instance.exports.jsfficallback( 210 | id, 211 | convertResponse(a1), 212 | convertResponse(a2), 213 | convertResponse(a3), 214 | convertResponse(a4), 215 | convertResponse(a5), 216 | convertResponse(a6), 217 | 0, 218 | 0, 219 | 0, 220 | 0 221 | ); 222 | } else if (l === 7) { 223 | mod.instance.exports.jsfficallback( 224 | id, 225 | convertResponse(a1), 226 | convertResponse(a2), 227 | convertResponse(a3), 228 | convertResponse(a4), 229 | convertResponse(a5), 230 | convertResponse(a6), 231 | convertResponse(a7), 232 | 0, 233 | 0, 234 | 0 235 | ); 236 | } else if (l === 8) { 237 | mod.instance.exports.jsfficallback( 238 | id, 239 | convertResponse(a1), 240 | convertResponse(a2), 241 | convertResponse(a3), 242 | convertResponse(a4), 243 | convertResponse(a5), 244 | convertResponse(a6), 245 | convertResponse(a7), 246 | convertResponse(a8), 247 | 0, 248 | 0 249 | ); 250 | } else if (l === 9) { 251 | mod.instance.exports.jsfficallback( 252 | id, 253 | convertResponse(a1), 254 | convertResponse(a2), 255 | convertResponse(a3), 256 | convertResponse(a4), 257 | convertResponse(a5), 258 | convertResponse(a6), 259 | convertResponse(a7), 260 | convertResponse(a8), 261 | convertResponse(a9), 262 | 0 263 | ); 264 | } else if (l === 10) { 265 | mod.instance.exports.jsfficallback( 266 | id, 267 | convertResponse(a1), 268 | convertResponse(a2), 269 | convertResponse(a3), 270 | convertResponse(a4), 271 | convertResponse(a5), 272 | convertResponse(a6), 273 | convertResponse(a7), 274 | convertResponse(a8), 275 | convertResponse(a9), 276 | convertResponse(a10) 277 | ); 278 | } 279 | }; 280 | } else if (val_type === TYPE_UINT8_ARRAY) { 281 | val = getTypedArrayFromMemory( 282 | Uint8Array, 283 | mod.instance.exports.memory.buffer, 284 | val, 285 | 1 286 | ); 287 | } else if (val_type === TYPE_INT8_ARRAY) { 288 | val = getTypedArrayFromMemory( 289 | Int8Array, 290 | mod.instance.exports.memory.buffer, 291 | val, 292 | 1 293 | ); 294 | } else if (val_type === TYPE_F32_ARRAY) { 295 | val = getTypedArrayFromMemory( 296 | Float32Array, 297 | mod.instance.exports.memory.buffer, 298 | val, 299 | 4 300 | ); 301 | } else if (val_type === TYPE_F64_ARRAY) { 302 | val = getTypedArrayFromMemory( 303 | Float64Array, 304 | mod.instance.exports.memory.buffer, 305 | val, 306 | 8 307 | ); 308 | } else if (val_type === TYPE_INT32_ARRAY) { 309 | val = getTypedArrayFromMemory( 310 | Int32Array, 311 | mod.instance.exports.memory.buffer, 312 | val, 313 | 4 314 | ); 315 | } else if (val_type === TYPE_UINT32_ARRAY) { 316 | val = getTypedArrayFromMemory( 317 | Uint32Array, 318 | mod.instance.exports.memory.buffer, 319 | val, 320 | 4 321 | ); 322 | } else if (val_type === TYPE_INT16_ARRAY) { 323 | val = getTypedArrayFromMemory( 324 | Int16Array, 325 | mod.instance.exports.memory.buffer, 326 | val, 327 | 2 328 | ); 329 | } else if (val_type === TYPE_UINT16_ARRAY) { 330 | val = getTypedArrayFromMemory( 331 | Uint16Array, 332 | mod.instance.exports.memory.buffer, 333 | val, 334 | 2 335 | ); 336 | } else if (val_type === TYPE_MEMORY) { 337 | val = mod.instance.exports.memory.buffer; 338 | } else { 339 | throw error("Unknown data type"); 340 | } 341 | return val; 342 | } 343 | 344 | function convertResponse(r) { 345 | const type = typeof r; 346 | if (type === "string") { 347 | return createString(r); 348 | } else if (type === "number") { 349 | return r; 350 | } else if (r === undefined) { 351 | return 0; 352 | } else if (r === null) { 353 | return 1; 354 | } else if (r === true) { 355 | return 1; 356 | } else if (r === false) { 357 | return 0; 358 | } else if (r.constructor === Float32Array) { 359 | return createTypedArray(r, Float32Array, 4); 360 | } else if (r.constructor === Uint8Array) { 361 | return createTypedArray(r, Uint8Array, 1); 362 | } else if (r.constructor === Int8Array) { 363 | return createTypedArray(r, Int8Array, 1); 364 | } else if (r.constructor === Float64Array) { 365 | return createTypedArray(r, Float64Array, 8); 366 | } else if (r.constructor === Int32Array) { 367 | return createTypedArray(r, Int32Array, 4); 368 | } else if (r.constructor === Uint32Array) { 369 | return createTypedArray(r, Uint32Array, 4); 370 | } else if (r.constructor === Int16Array) { 371 | return createTypedArray(r, Int16Array, 2); 372 | } else if (r.constructor === Uint16Array) { 373 | return createTypedArray(r, Uint16Array, 2); 374 | } 375 | return allocate(r); 376 | } 377 | if (typeof cfg === "string") { 378 | cfg = { path: cfg }; 379 | } 380 | let result = {}; 381 | fetch(cfg.path) 382 | .then(response => response.arrayBuffer()) 383 | .then(bytes => 384 | WebAssembly.instantiate( 385 | bytes, 386 | cfg.overrides || { 387 | env: Object.assign( 388 | { 389 | jsffithrowerror: function(e) { 390 | let err = getStringFromMemory( 391 | mod.instance.exports.memory.buffer, 392 | e 393 | ); 394 | console.error(err); 395 | throw new Error("Web assembly module exited unexpectedly."); 396 | }, 397 | jsffirelease: function(obj) { 398 | allocator_release(obj); 399 | }, 400 | jsffiregister: function(code) { 401 | code = getStringFromMemory( 402 | mod.instance.exports.memory.buffer, 403 | code 404 | ); 405 | let id = functions.length; 406 | functions.push(eval("(" + code + ")")); 407 | return id; 408 | }, 409 | jsfficall0: function(obj, f) { 410 | return convertResponse(functions[f].call(allocator_get(obj))); 411 | }, 412 | jsfficall1: function(obj, f, a1_type, a1) { 413 | return convertResponse( 414 | functions[f].call( 415 | allocator_get(obj), 416 | convertArgument(a1_type, a1) 417 | ) 418 | ); 419 | }, 420 | jsfficall2: function(obj, f, a1_type, a1, a2_type, a2) { 421 | return convertResponse( 422 | functions[f].call( 423 | allocator_get(obj), 424 | convertArgument(a1_type, a1), 425 | convertArgument(a2_type, a2) 426 | ) 427 | ); 428 | }, 429 | jsfficall3: function( 430 | obj, 431 | f, 432 | a1_type, 433 | a1, 434 | a2_type, 435 | a2, 436 | a3_type, 437 | a3 438 | ) { 439 | return convertResponse( 440 | functions[f].call( 441 | allocator_get(obj), 442 | convertArgument(a1_type, a1), 443 | convertArgument(a2_type, a2), 444 | convertArgument(a3_type, a3) 445 | ) 446 | ); 447 | }, 448 | jsfficall4: function( 449 | obj, 450 | f, 451 | a1_type, 452 | a1, 453 | a2_type, 454 | a2, 455 | a3_type, 456 | a3, 457 | a4_type, 458 | a4 459 | ) { 460 | return convertResponse( 461 | functions[f].call( 462 | allocator_get(obj), 463 | convertArgument(a1_type, a1), 464 | convertArgument(a2_type, a2), 465 | convertArgument(a3_type, a3), 466 | convertArgument(a4_type, a4) 467 | ) 468 | ); 469 | }, 470 | jsfficall5: function( 471 | obj, 472 | f, 473 | a1_type, 474 | a1, 475 | a2_type, 476 | a2, 477 | a3_type, 478 | a3, 479 | a4_type, 480 | a4, 481 | a5_type, 482 | a5 483 | ) { 484 | return convertResponse( 485 | functions[f].call( 486 | allocator_get(obj), 487 | convertArgument(a1_type, a1), 488 | convertArgument(a2_type, a2), 489 | convertArgument(a3_type, a3), 490 | convertArgument(a4_type, a4), 491 | convertArgument(a5_type, a5) 492 | ) 493 | ); 494 | }, 495 | jsfficall6: function( 496 | obj, 497 | f, 498 | a1_type, 499 | a1, 500 | a2_type, 501 | a2, 502 | a3_type, 503 | a3, 504 | a4_type, 505 | a4, 506 | a5_type, 507 | a5, 508 | a6_type, 509 | a6 510 | ) { 511 | return convertResponse( 512 | functions[f].call( 513 | allocator_get(obj), 514 | convertArgument(a1_type, a1), 515 | convertArgument(a2_type, a2), 516 | convertArgument(a3_type, a3), 517 | convertArgument(a4_type, a4), 518 | convertArgument(a5_type, a5), 519 | convertArgument(a6_type, a6) 520 | ) 521 | ); 522 | }, 523 | jsfficall7: function( 524 | obj, 525 | f, 526 | a1_type, 527 | a1, 528 | a2_type, 529 | a2, 530 | a3_type, 531 | a3, 532 | a4_type, 533 | a4, 534 | a5_type, 535 | a5, 536 | a6_type, 537 | a6, 538 | a7_type, 539 | a7 540 | ) { 541 | return convertResponse( 542 | functions[f].call( 543 | allocator_get(obj), 544 | convertArgument(a1_type, a1), 545 | convertArgument(a2_type, a2), 546 | convertArgument(a3_type, a3), 547 | convertArgument(a4_type, a4), 548 | convertArgument(a5_type, a5), 549 | convertArgument(a6_type, a6), 550 | convertArgument(a7_type, a7) 551 | ) 552 | ); 553 | }, 554 | jsfficall8: function( 555 | obj, 556 | f, 557 | a1_type, 558 | a1, 559 | a2_type, 560 | a2, 561 | a3_type, 562 | a3, 563 | a4_type, 564 | a4, 565 | a5_type, 566 | a5, 567 | a6_type, 568 | a6, 569 | a7_type, 570 | a7, 571 | a8_type, 572 | a8 573 | ) { 574 | return convertResponse( 575 | functions[f].call( 576 | allocator_get(obj), 577 | convertArgument(a1_type, a1), 578 | convertArgument(a2_type, a2), 579 | convertArgument(a3_type, a3), 580 | convertArgument(a4_type, a4), 581 | convertArgument(a5_type, a5), 582 | convertArgument(a6_type, a6), 583 | convertArgument(a7_type, a7), 584 | convertArgument(a8_type, a8) 585 | ) 586 | ); 587 | }, 588 | jsfficall9: function( 589 | obj, 590 | f, 591 | a1_type, 592 | a1, 593 | a2_type, 594 | a2, 595 | a3_type, 596 | a3, 597 | a4_type, 598 | a4, 599 | a5_type, 600 | a5, 601 | a6_type, 602 | a6, 603 | a7_type, 604 | a7, 605 | a8_type, 606 | a8, 607 | a9_type, 608 | a9 609 | ) { 610 | return convertResponse( 611 | functions[f].call( 612 | allocator_get(obj), 613 | convertArgument(a1_type, a1), 614 | convertArgument(a2_type, a2), 615 | convertArgument(a3_type, a3), 616 | convertArgument(a4_type, a4), 617 | convertArgument(a5_type, a5), 618 | convertArgument(a6_type, a6), 619 | convertArgument(a7_type, a7), 620 | convertArgument(a8_type, a8), 621 | convertArgument(a9_type, a9) 622 | ) 623 | ); 624 | }, 625 | jsfficall10: function( 626 | obj, 627 | f, 628 | a1_type, 629 | a1, 630 | a2_type, 631 | a2, 632 | a3_type, 633 | a3, 634 | a4_type, 635 | a4, 636 | a5_type, 637 | a5, 638 | a6_type, 639 | a6, 640 | a7_type, 641 | a7, 642 | a8_type, 643 | a8, 644 | a9_type, 645 | a9, 646 | a10_type, 647 | a10 648 | ) { 649 | return convertResponse( 650 | functions[f].call( 651 | allocator_get(obj), 652 | convertArgument(a1_type, a1), 653 | convertArgument(a2_type, a2), 654 | convertArgument(a3_type, a3), 655 | convertArgument(a4_type, a4), 656 | convertArgument(a5_type, a5), 657 | convertArgument(a6_type, a6), 658 | convertArgument(a7_type, a7), 659 | convertArgument(a8_type, a8), 660 | convertArgument(a9_type, a9), 661 | convertArgument(a10_type, a10) 662 | ) 663 | ); 664 | } 665 | }, 666 | cfg.imports || {} 667 | ) 668 | } 669 | ).then(module => { 670 | mod = module; 671 | result.instance = module.instance; 672 | if (cfg.onLoad) { 673 | cfg.onLoad(module); 674 | } 675 | if (cfg.entry !== null) { 676 | module.instance.exports[cfg.entry || "main"](); 677 | } 678 | }) 679 | ); 680 | return result; 681 | } 682 | }; 683 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | use alloc::boxed::Box; 3 | use alloc::sync::Arc; 4 | use core::{ 5 | future::Future, 6 | pin::Pin, 7 | task::{Context, Poll, Waker}, 8 | }; 9 | use spin::Mutex; 10 | extern crate alloc; 11 | #[macro_use] 12 | extern crate lazy_static; 13 | use alloc::vec::Vec; 14 | use cstring::{cstr, cstr_to_string}; 15 | 16 | pub type JSValue = f64; 17 | pub type JSType = i32; 18 | 19 | pub const FALSE: JSValue = 0.0; 20 | pub const TRUE: JSValue = 1.0; 21 | 22 | pub const TYPE_NOTHING: JSType = 0; 23 | pub const TYPE_NUMBER: JSType = 1; 24 | pub const TYPE_STRING: JSType = 2; 25 | pub const TYPE_BOOL: JSType = 3; 26 | pub const TYPE_FUNCTION: JSType = 4; 27 | pub const TYPE_OBJECT: JSType = 5; 28 | pub const TYPE_UINT8_ARRAY: JSType = 6; 29 | pub const TYPE_INT8_ARRAY: JSType = 7; 30 | //pub const TYPE_UINT8CLAMPED_ARRAY: JSType = 8; 31 | pub const TYPE_INT16_ARRAY: JSType = 9; 32 | pub const TYPE_UINT16_ARRAY: JSType = 10; 33 | pub const TYPE_INT32_ARRAY: JSType = 11; 34 | pub const TYPE_UINT32_ARRAY: JSType = 12; 35 | pub const TYPE_F32_ARRAY: JSType = 13; 36 | pub const TYPE_F64_ARRAY: JSType = 14; 37 | //pub const TYPE_BI64_ARRAY: JSType = 15; 38 | //pub const TYPE_BUI64_ARRAY: JSType = 16; 39 | pub const TYPE_MEMORY: JSType = 17; 40 | 41 | pub const UNDEFINED: JSObject = JSObject(0.0); 42 | pub const NULL: JSObject = JSObject(1.0); 43 | pub const CONSOLE: JSObject = JSObject(2.0); 44 | pub const WINDOW: JSObject = JSObject(3.0); 45 | pub const SELF: JSObject = JSObject(3.0); 46 | pub const DOCUMENT: JSObject = JSObject(4.0); 47 | 48 | #[derive(Clone, Copy)] 49 | pub struct JSInvoker(JSValue); 50 | 51 | pub trait ToJSValue { 52 | fn to_js_value(&self) -> JSValue; 53 | fn to_js_type(&self) -> JSType; 54 | } 55 | 56 | impl ToJSValue for &str { 57 | #[inline] 58 | fn to_js_value(&self) -> JSValue { 59 | cstr(self) as JSValue 60 | } 61 | 62 | #[inline] 63 | fn to_js_type(&self) -> JSType { 64 | TYPE_STRING 65 | } 66 | } 67 | 68 | impl ToJSValue for &alloc::string::String { 69 | #[inline] 70 | fn to_js_value(&self) -> JSValue { 71 | cstr(self) as JSValue 72 | } 73 | 74 | #[inline] 75 | fn to_js_type(&self) -> JSType { 76 | TYPE_STRING 77 | } 78 | } 79 | 80 | impl ToJSValue for usize { 81 | #[inline] 82 | fn to_js_value(&self) -> JSValue { 83 | *self as JSValue 84 | } 85 | 86 | #[inline] 87 | fn to_js_type(&self) -> JSType { 88 | TYPE_NUMBER 89 | } 90 | } 91 | 92 | impl ToJSValue for u8 { 93 | #[inline] 94 | fn to_js_value(&self) -> JSValue { 95 | *self as JSValue 96 | } 97 | 98 | #[inline] 99 | fn to_js_type(&self) -> JSType { 100 | TYPE_NUMBER 101 | } 102 | } 103 | 104 | impl ToJSValue for u16 { 105 | #[inline] 106 | fn to_js_value(&self) -> JSValue { 107 | *self as JSValue 108 | } 109 | 110 | #[inline] 111 | fn to_js_type(&self) -> JSType { 112 | TYPE_NUMBER 113 | } 114 | } 115 | 116 | impl ToJSValue for u32 { 117 | #[inline] 118 | fn to_js_value(&self) -> JSValue { 119 | *self as JSValue 120 | } 121 | 122 | #[inline] 123 | fn to_js_type(&self) -> JSType { 124 | TYPE_NUMBER 125 | } 126 | } 127 | 128 | impl ToJSValue for i8 { 129 | #[inline] 130 | fn to_js_value(&self) -> JSValue { 131 | *self as JSValue 132 | } 133 | 134 | #[inline] 135 | fn to_js_type(&self) -> JSType { 136 | TYPE_NUMBER 137 | } 138 | } 139 | 140 | impl ToJSValue for i16 { 141 | #[inline] 142 | fn to_js_value(&self) -> JSValue { 143 | *self as JSValue 144 | } 145 | 146 | #[inline] 147 | fn to_js_type(&self) -> JSType { 148 | TYPE_NUMBER 149 | } 150 | } 151 | 152 | impl ToJSValue for i32 { 153 | #[inline] 154 | fn to_js_value(&self) -> JSValue { 155 | *self as JSValue 156 | } 157 | 158 | #[inline] 159 | fn to_js_type(&self) -> JSType { 160 | TYPE_NUMBER 161 | } 162 | } 163 | 164 | impl ToJSValue for bool { 165 | #[inline] 166 | fn to_js_value(&self) -> JSValue { 167 | if *self { 168 | TRUE as JSValue 169 | } else { 170 | FALSE as JSValue 171 | } 172 | } 173 | 174 | #[inline] 175 | fn to_js_type(&self) -> JSType { 176 | TYPE_BOOL 177 | } 178 | } 179 | 180 | impl ToJSValue for f64 { 181 | #[inline] 182 | fn to_js_value(&self) -> JSValue { 183 | *self as JSValue 184 | } 185 | 186 | #[inline] 187 | fn to_js_type(&self) -> JSType { 188 | TYPE_NUMBER 189 | } 190 | } 191 | 192 | impl ToJSValue for f32 { 193 | #[inline] 194 | fn to_js_value(&self) -> JSValue { 195 | *self as JSValue 196 | } 197 | 198 | #[inline] 199 | fn to_js_type(&self) -> JSType { 200 | TYPE_NUMBER 201 | } 202 | } 203 | 204 | pub struct JSObject(pub JSValue); 205 | 206 | impl Drop for JSObject { 207 | fn drop(&mut self) { 208 | release_object(self) 209 | } 210 | } 211 | 212 | impl ToJSValue for JSObject { 213 | #[inline] 214 | fn to_js_value(&self) -> JSValue { 215 | self.0 216 | } 217 | 218 | #[inline] 219 | fn to_js_type(&self) -> JSType { 220 | TYPE_OBJECT 221 | } 222 | } 223 | 224 | impl ToJSValue for &JSObject { 225 | #[inline] 226 | fn to_js_value(&self) -> JSValue { 227 | self.0 228 | } 229 | 230 | #[inline] 231 | fn to_js_type(&self) -> JSType { 232 | TYPE_OBJECT 233 | } 234 | } 235 | 236 | #[derive(Clone, Copy)] 237 | pub struct JSFunction(JSValue); 238 | 239 | impl JSFunction { 240 | #[inline] 241 | pub fn from(v: T) -> JSFunction 242 | where 243 | T: Clone + Into, 244 | { 245 | JSFunction(v.clone().into() as JSValue) 246 | } 247 | } 248 | 249 | impl ToJSValue for JSFunction { 250 | #[inline] 251 | fn to_js_value(&self) -> JSValue { 252 | self.0 253 | } 254 | 255 | #[inline] 256 | fn to_js_type(&self) -> JSType { 257 | TYPE_FUNCTION 258 | } 259 | } 260 | 261 | #[derive(Clone, Copy)] 262 | pub struct WasmMemory; 263 | 264 | impl ToJSValue for WasmMemory { 265 | #[inline] 266 | fn to_js_value(&self) -> JSValue { 267 | 0.0 268 | } 269 | 270 | #[inline] 271 | fn to_js_type(&self) -> JSType { 272 | TYPE_MEMORY 273 | } 274 | } 275 | 276 | impl ToJSValue for &Vec { 277 | #[inline] 278 | fn to_js_value(&self) -> JSValue { 279 | self as *const _ as usize as JSValue 280 | } 281 | 282 | #[inline] 283 | fn to_js_type(&self) -> JSType { 284 | TYPE_F32_ARRAY 285 | } 286 | } 287 | 288 | impl ToJSValue for &Vec { 289 | #[inline] 290 | fn to_js_value(&self) -> JSValue { 291 | self as *const _ as usize as JSValue 292 | } 293 | 294 | #[inline] 295 | fn to_js_type(&self) -> JSType { 296 | TYPE_F64_ARRAY 297 | } 298 | } 299 | 300 | impl ToJSValue for &Vec { 301 | #[inline] 302 | fn to_js_value(&self) -> JSValue { 303 | self as *const _ as usize as JSValue 304 | } 305 | 306 | #[inline] 307 | fn to_js_type(&self) -> JSType { 308 | TYPE_UINT8_ARRAY 309 | } 310 | } 311 | 312 | impl ToJSValue for &Vec { 313 | #[inline] 314 | fn to_js_value(&self) -> JSValue { 315 | self as *const _ as usize as JSValue 316 | } 317 | 318 | #[inline] 319 | fn to_js_type(&self) -> JSType { 320 | TYPE_INT8_ARRAY 321 | } 322 | } 323 | 324 | impl ToJSValue for &Vec { 325 | #[inline] 326 | fn to_js_value(&self) -> JSValue { 327 | self as *const _ as usize as JSValue 328 | } 329 | 330 | #[inline] 331 | fn to_js_type(&self) -> JSType { 332 | TYPE_UINT16_ARRAY 333 | } 334 | } 335 | 336 | impl ToJSValue for &Vec { 337 | #[inline] 338 | fn to_js_value(&self) -> JSValue { 339 | self as *const _ as usize as JSValue 340 | } 341 | 342 | #[inline] 343 | fn to_js_type(&self) -> JSType { 344 | TYPE_INT16_ARRAY 345 | } 346 | } 347 | 348 | impl ToJSValue for &Vec { 349 | #[inline] 350 | fn to_js_value(&self) -> JSValue { 351 | self as *const _ as usize as JSValue 352 | } 353 | 354 | #[inline] 355 | fn to_js_type(&self) -> JSType { 356 | TYPE_UINT32_ARRAY 357 | } 358 | } 359 | 360 | impl ToJSValue for &Vec { 361 | #[inline] 362 | fn to_js_value(&self) -> JSValue { 363 | self as *const _ as usize as JSValue 364 | } 365 | 366 | #[inline] 367 | fn to_js_type(&self) -> JSType { 368 | TYPE_INT32_ARRAY 369 | } 370 | } 371 | 372 | impl ToJSValue for &Vec { 373 | #[inline] 374 | fn to_js_value(&self) -> JSValue { 375 | self as *const _ as usize as JSValue 376 | } 377 | 378 | #[inline] 379 | fn to_js_type(&self) -> JSType { 380 | TYPE_UINT32_ARRAY 381 | } 382 | } 383 | 384 | pub trait JSConvert { 385 | fn to_string(&self) -> alloc::string::String; 386 | fn to_js_object(&self) -> JSObject; 387 | fn to_bool(&self) -> bool; 388 | fn is_null(&self) -> bool; 389 | fn is_undefined(&self) -> bool; 390 | fn to_vec(&self) -> Vec 391 | where 392 | Q: Copy; 393 | } 394 | 395 | impl JSConvert for JSValue { 396 | #[inline] 397 | fn to_string(&self) -> alloc::string::String { 398 | cstr_to_string(*self as i32) 399 | } 400 | 401 | #[inline] 402 | fn to_js_object(&self) -> JSObject { 403 | JSObject(*self) 404 | } 405 | 406 | #[inline] 407 | fn to_bool(&self) -> bool { 408 | *self == TRUE 409 | } 410 | 411 | #[inline] 412 | fn is_null(&self) -> bool { 413 | *self == NULL.0 414 | } 415 | 416 | #[inline] 417 | fn is_undefined(&self) -> bool { 418 | *self == UNDEFINED.0 419 | } 420 | 421 | fn to_vec(&self) -> Vec 422 | where 423 | Q: Copy, 424 | { 425 | unsafe { 426 | let ptr = *self; 427 | let start = ptr as usize; 428 | let lptr = start as *const usize; 429 | let length = *lptr; 430 | let mut v = Vec::with_capacity(length); 431 | let mut data_ptr = ptr as usize + core::mem::size_of::(); 432 | for _ in 0..length { 433 | v.push(*(data_ptr as *const Q)); 434 | data_ptr += core::mem::size_of::(); 435 | } 436 | v 437 | } 438 | } 439 | } 440 | 441 | impl JSInvoker { 442 | #[inline] 443 | pub fn call_0(&self, obj: &JSObject) -> JSValue { 444 | unsafe { jsfficall0(obj.to_js_value(), self.0) } 445 | } 446 | 447 | #[inline] 448 | pub fn call_1(&self, obj: &JSObject, a1: impl ToJSValue) -> JSValue { 449 | unsafe { jsfficall1(obj.to_js_value(), self.0, a1.to_js_type(), a1.to_js_value()) } 450 | } 451 | 452 | #[inline] 453 | pub fn call_2(&self, obj: &JSObject, a1: impl ToJSValue, a2: impl ToJSValue) -> JSValue { 454 | unsafe { 455 | jsfficall2( 456 | obj.to_js_value(), 457 | self.0, 458 | a1.to_js_type(), 459 | a1.to_js_value(), 460 | a2.to_js_type(), 461 | a2.to_js_value(), 462 | ) 463 | } 464 | } 465 | 466 | #[inline] 467 | pub fn call_3( 468 | &self, 469 | obj: &JSObject, 470 | a1: impl ToJSValue, 471 | a2: impl ToJSValue, 472 | a3: impl ToJSValue, 473 | ) -> JSValue { 474 | unsafe { 475 | jsfficall3( 476 | obj.to_js_value(), 477 | self.0, 478 | a1.to_js_type(), 479 | a1.to_js_value(), 480 | a2.to_js_type(), 481 | a2.to_js_value(), 482 | a3.to_js_type(), 483 | a3.to_js_value(), 484 | ) 485 | } 486 | } 487 | 488 | #[inline] 489 | pub fn call_4( 490 | &self, 491 | obj: &JSObject, 492 | a1: impl ToJSValue, 493 | a2: impl ToJSValue, 494 | a3: impl ToJSValue, 495 | 496 | a4: impl ToJSValue, 497 | ) -> JSValue { 498 | unsafe { 499 | jsfficall4( 500 | obj.to_js_value(), 501 | self.0, 502 | a1.to_js_type(), 503 | a1.to_js_value(), 504 | a2.to_js_type(), 505 | a2.to_js_value(), 506 | a3.to_js_type(), 507 | a3.to_js_value(), 508 | a4.to_js_type(), 509 | a4.to_js_value(), 510 | ) 511 | } 512 | } 513 | 514 | #[inline] 515 | pub fn call_5( 516 | &self, 517 | obj: &JSObject, 518 | 519 | a1: impl ToJSValue, 520 | 521 | a2: impl ToJSValue, 522 | 523 | a3: impl ToJSValue, 524 | 525 | a4: impl ToJSValue, 526 | 527 | a5: impl ToJSValue, 528 | ) -> JSValue { 529 | unsafe { 530 | jsfficall5( 531 | obj.to_js_value(), 532 | self.0, 533 | a1.to_js_type(), 534 | a1.to_js_value(), 535 | a2.to_js_type(), 536 | a2.to_js_value(), 537 | a3.to_js_type(), 538 | a3.to_js_value(), 539 | a4.to_js_type(), 540 | a4.to_js_value(), 541 | a5.to_js_type(), 542 | a5.to_js_value(), 543 | ) 544 | } 545 | } 546 | 547 | #[inline] 548 | pub fn call_6( 549 | &self, 550 | obj: &JSObject, 551 | 552 | a1: impl ToJSValue, 553 | 554 | a2: impl ToJSValue, 555 | 556 | a3: impl ToJSValue, 557 | 558 | a4: impl ToJSValue, 559 | 560 | a5: impl ToJSValue, 561 | 562 | a6: impl ToJSValue, 563 | ) -> JSValue { 564 | unsafe { 565 | jsfficall6( 566 | obj.to_js_value(), 567 | self.0, 568 | a1.to_js_type(), 569 | a1.to_js_value(), 570 | a2.to_js_type(), 571 | a2.to_js_value(), 572 | a3.to_js_type(), 573 | a3.to_js_value(), 574 | a4.to_js_type(), 575 | a4.to_js_value(), 576 | a5.to_js_type(), 577 | a5.to_js_value(), 578 | a6.to_js_type(), 579 | a6.to_js_value(), 580 | ) 581 | } 582 | } 583 | 584 | #[inline] 585 | pub fn call_7( 586 | &self, 587 | obj: &JSObject, 588 | 589 | a1: impl ToJSValue, 590 | 591 | a2: impl ToJSValue, 592 | 593 | a3: impl ToJSValue, 594 | 595 | a4: impl ToJSValue, 596 | 597 | a5: impl ToJSValue, 598 | 599 | a6: impl ToJSValue, 600 | 601 | a7: impl ToJSValue, 602 | ) -> JSValue { 603 | unsafe { 604 | jsfficall7( 605 | obj.to_js_value(), 606 | self.0, 607 | a1.to_js_type(), 608 | a1.to_js_value(), 609 | a2.to_js_type(), 610 | a2.to_js_value(), 611 | a3.to_js_type(), 612 | a3.to_js_value(), 613 | a4.to_js_type(), 614 | a4.to_js_value(), 615 | a5.to_js_type(), 616 | a5.to_js_value(), 617 | a6.to_js_type(), 618 | a6.to_js_value(), 619 | a7.to_js_type(), 620 | a7.to_js_value(), 621 | ) 622 | } 623 | } 624 | 625 | #[inline] 626 | pub fn call_8( 627 | &self, 628 | obj: &JSObject, 629 | 630 | a1: impl ToJSValue, 631 | 632 | a2: impl ToJSValue, 633 | 634 | a3: impl ToJSValue, 635 | 636 | a4: impl ToJSValue, 637 | 638 | a5: impl ToJSValue, 639 | 640 | a6: impl ToJSValue, 641 | 642 | a7: impl ToJSValue, 643 | 644 | a8: impl ToJSValue, 645 | ) -> JSValue { 646 | unsafe { 647 | jsfficall8( 648 | obj.to_js_value(), 649 | self.0, 650 | a1.to_js_type(), 651 | a1.to_js_value(), 652 | a2.to_js_type(), 653 | a2.to_js_value(), 654 | a3.to_js_type(), 655 | a3.to_js_value(), 656 | a4.to_js_type(), 657 | a4.to_js_value(), 658 | a5.to_js_type(), 659 | a5.to_js_value(), 660 | a6.to_js_type(), 661 | a6.to_js_value(), 662 | a7.to_js_type(), 663 | a7.to_js_value(), 664 | a8.to_js_type(), 665 | a8.to_js_value(), 666 | ) 667 | } 668 | } 669 | 670 | #[inline] 671 | pub fn call_9( 672 | &self, 673 | obj: &JSObject, 674 | 675 | a1: impl ToJSValue, 676 | 677 | a2: impl ToJSValue, 678 | 679 | a3: impl ToJSValue, 680 | 681 | a4: impl ToJSValue, 682 | 683 | a5: impl ToJSValue, 684 | 685 | a6: impl ToJSValue, 686 | 687 | a7: impl ToJSValue, 688 | 689 | a8: impl ToJSValue, 690 | 691 | a9: impl ToJSValue, 692 | ) -> JSValue { 693 | unsafe { 694 | jsfficall9( 695 | obj.to_js_value(), 696 | self.0, 697 | a1.to_js_type(), 698 | a1.to_js_value(), 699 | a2.to_js_type(), 700 | a2.to_js_value(), 701 | a3.to_js_type(), 702 | a3.to_js_value(), 703 | a4.to_js_type(), 704 | a4.to_js_value(), 705 | a5.to_js_type(), 706 | a5.to_js_value(), 707 | a6.to_js_type(), 708 | a6.to_js_value(), 709 | a7.to_js_type(), 710 | a7.to_js_value(), 711 | a8.to_js_type(), 712 | a8.to_js_value(), 713 | a9.to_js_type(), 714 | a9.to_js_value(), 715 | ) 716 | } 717 | } 718 | 719 | #[inline] 720 | pub fn call_10( 721 | &self, 722 | obj: &JSObject, 723 | 724 | a1: impl ToJSValue, 725 | 726 | a2: impl ToJSValue, 727 | 728 | a3: impl ToJSValue, 729 | 730 | a4: impl ToJSValue, 731 | 732 | a5: impl ToJSValue, 733 | 734 | a6: impl ToJSValue, 735 | 736 | a7: impl ToJSValue, 737 | 738 | a8: impl ToJSValue, 739 | 740 | a9: impl ToJSValue, 741 | 742 | a10: impl ToJSValue, 743 | ) -> JSValue { 744 | unsafe { 745 | jsfficall10( 746 | obj.to_js_value(), 747 | self.0, 748 | a1.to_js_type(), 749 | a1.to_js_value(), 750 | a2.to_js_type(), 751 | a2.to_js_value(), 752 | a3.to_js_type(), 753 | a3.to_js_value(), 754 | a4.to_js_type(), 755 | a4.to_js_value(), 756 | a5.to_js_type(), 757 | a5.to_js_value(), 758 | a6.to_js_type(), 759 | a6.to_js_value(), 760 | a7.to_js_type(), 761 | a7.to_js_value(), 762 | a8.to_js_type(), 763 | a8.to_js_value(), 764 | a9.to_js_type(), 765 | a9.to_js_value(), 766 | a10.to_js_type(), 767 | a10.to_js_value(), 768 | ) 769 | } 770 | } 771 | 772 | #[inline] 773 | pub fn invoke_0(&self) -> JSValue { 774 | unsafe { jsfficall0(UNDEFINED.0, self.0) } 775 | } 776 | 777 | #[inline] 778 | pub fn invoke_1(&self, a1: impl ToJSValue) -> JSValue { 779 | unsafe { jsfficall1(UNDEFINED.0, self.0, a1.to_js_type(), a1.to_js_value()) } 780 | } 781 | 782 | #[inline] 783 | pub fn invoke_2(&self, a1: impl ToJSValue, a2: impl ToJSValue) -> JSValue { 784 | unsafe { 785 | jsfficall2( 786 | UNDEFINED.0, 787 | self.0, 788 | a1.to_js_type(), 789 | a1.to_js_value(), 790 | a2.to_js_type(), 791 | a2.to_js_value(), 792 | ) 793 | } 794 | } 795 | 796 | #[inline] 797 | pub fn invoke_3(&self, a1: impl ToJSValue, a2: impl ToJSValue, a3: impl ToJSValue) -> JSValue { 798 | unsafe { 799 | jsfficall3( 800 | UNDEFINED.0, 801 | self.0, 802 | a1.to_js_type(), 803 | a1.to_js_value(), 804 | a2.to_js_type(), 805 | a2.to_js_value(), 806 | a3.to_js_type(), 807 | a3.to_js_value(), 808 | ) 809 | } 810 | } 811 | 812 | #[inline] 813 | pub fn invoke_4( 814 | &self, 815 | 816 | a1: impl ToJSValue, 817 | 818 | a2: impl ToJSValue, 819 | 820 | a3: impl ToJSValue, 821 | 822 | a4: impl ToJSValue, 823 | ) -> JSValue { 824 | unsafe { 825 | jsfficall4( 826 | UNDEFINED.0, 827 | self.0, 828 | a1.to_js_type(), 829 | a1.to_js_value(), 830 | a2.to_js_type(), 831 | a2.to_js_value(), 832 | a3.to_js_type(), 833 | a3.to_js_value(), 834 | a4.to_js_type(), 835 | a4.to_js_value(), 836 | ) 837 | } 838 | } 839 | 840 | #[inline] 841 | pub fn invoke_5( 842 | &self, 843 | 844 | a1: impl ToJSValue, 845 | 846 | a2: impl ToJSValue, 847 | 848 | a3: impl ToJSValue, 849 | 850 | a4: impl ToJSValue, 851 | 852 | a5: impl ToJSValue, 853 | ) -> JSValue { 854 | unsafe { 855 | jsfficall5( 856 | UNDEFINED.0, 857 | self.0, 858 | a1.to_js_type(), 859 | a1.to_js_value(), 860 | a2.to_js_type(), 861 | a2.to_js_value(), 862 | a3.to_js_type(), 863 | a3.to_js_value(), 864 | a4.to_js_type(), 865 | a4.to_js_value(), 866 | a5.to_js_type(), 867 | a5.to_js_value(), 868 | ) 869 | } 870 | } 871 | 872 | #[inline] 873 | pub fn invoke_6( 874 | &self, 875 | 876 | a1: impl ToJSValue, 877 | 878 | a2: impl ToJSValue, 879 | 880 | a3: impl ToJSValue, 881 | 882 | a4: impl ToJSValue, 883 | 884 | a5: impl ToJSValue, 885 | 886 | a6: impl ToJSValue, 887 | ) -> JSValue { 888 | unsafe { 889 | jsfficall6( 890 | UNDEFINED.0, 891 | self.0, 892 | a1.to_js_type(), 893 | a1.to_js_value(), 894 | a2.to_js_type(), 895 | a2.to_js_value(), 896 | a3.to_js_type(), 897 | a3.to_js_value(), 898 | a4.to_js_type(), 899 | a4.to_js_value(), 900 | a5.to_js_type(), 901 | a5.to_js_value(), 902 | a6.to_js_type(), 903 | a6.to_js_value(), 904 | ) 905 | } 906 | } 907 | 908 | #[inline] 909 | pub fn invoke_7( 910 | &self, 911 | 912 | a1: impl ToJSValue, 913 | 914 | a2: impl ToJSValue, 915 | 916 | a3: impl ToJSValue, 917 | 918 | a4: impl ToJSValue, 919 | 920 | a5: impl ToJSValue, 921 | 922 | a6: impl ToJSValue, 923 | 924 | a7: impl ToJSValue, 925 | ) -> JSValue { 926 | unsafe { 927 | jsfficall7( 928 | UNDEFINED.0, 929 | self.0, 930 | a1.to_js_type(), 931 | a1.to_js_value(), 932 | a2.to_js_type(), 933 | a2.to_js_value(), 934 | a3.to_js_type(), 935 | a3.to_js_value(), 936 | a4.to_js_type(), 937 | a4.to_js_value(), 938 | a5.to_js_type(), 939 | a5.to_js_value(), 940 | a6.to_js_type(), 941 | a6.to_js_value(), 942 | a7.to_js_type(), 943 | a7.to_js_value(), 944 | ) 945 | } 946 | } 947 | 948 | #[inline] 949 | pub fn invoke_8( 950 | &self, 951 | 952 | a1: impl ToJSValue, 953 | 954 | a2: impl ToJSValue, 955 | 956 | a3: impl ToJSValue, 957 | 958 | a4: impl ToJSValue, 959 | 960 | a5: impl ToJSValue, 961 | 962 | a6: impl ToJSValue, 963 | 964 | a7: impl ToJSValue, 965 | 966 | a8: impl ToJSValue, 967 | ) -> JSValue { 968 | unsafe { 969 | jsfficall8( 970 | UNDEFINED.0, 971 | self.0, 972 | a1.to_js_type(), 973 | a1.to_js_value(), 974 | a2.to_js_type(), 975 | a2.to_js_value(), 976 | a3.to_js_type(), 977 | a3.to_js_value(), 978 | a4.to_js_type(), 979 | a4.to_js_value(), 980 | a5.to_js_type(), 981 | a5.to_js_value(), 982 | a6.to_js_type(), 983 | a6.to_js_value(), 984 | a7.to_js_type(), 985 | a7.to_js_value(), 986 | a8.to_js_type(), 987 | a8.to_js_value(), 988 | ) 989 | } 990 | } 991 | 992 | #[inline] 993 | pub fn invoke_9( 994 | &self, 995 | 996 | a1: impl ToJSValue, 997 | 998 | a2: impl ToJSValue, 999 | 1000 | a3: impl ToJSValue, 1001 | 1002 | a4: impl ToJSValue, 1003 | 1004 | a5: impl ToJSValue, 1005 | 1006 | a6: impl ToJSValue, 1007 | 1008 | a7: impl ToJSValue, 1009 | 1010 | a8: impl ToJSValue, 1011 | 1012 | a9: impl ToJSValue, 1013 | ) -> JSValue { 1014 | unsafe { 1015 | jsfficall9( 1016 | UNDEFINED.0, 1017 | self.0, 1018 | a1.to_js_type(), 1019 | a1.to_js_value(), 1020 | a2.to_js_type(), 1021 | a2.to_js_value(), 1022 | a3.to_js_type(), 1023 | a3.to_js_value(), 1024 | a4.to_js_type(), 1025 | a4.to_js_value(), 1026 | a5.to_js_type(), 1027 | a5.to_js_value(), 1028 | a6.to_js_type(), 1029 | a6.to_js_value(), 1030 | a7.to_js_type(), 1031 | a7.to_js_value(), 1032 | a8.to_js_type(), 1033 | a8.to_js_value(), 1034 | a9.to_js_type(), 1035 | a9.to_js_value(), 1036 | ) 1037 | } 1038 | } 1039 | 1040 | #[inline] 1041 | pub fn invoke_10( 1042 | &self, 1043 | 1044 | a1: impl ToJSValue, 1045 | 1046 | a2: impl ToJSValue, 1047 | 1048 | a3: impl ToJSValue, 1049 | 1050 | a4: impl ToJSValue, 1051 | 1052 | a5: impl ToJSValue, 1053 | 1054 | a6: impl ToJSValue, 1055 | 1056 | a7: impl ToJSValue, 1057 | 1058 | a8: impl ToJSValue, 1059 | 1060 | a9: impl ToJSValue, 1061 | 1062 | a10: impl ToJSValue, 1063 | ) -> JSValue { 1064 | unsafe { 1065 | jsfficall10( 1066 | UNDEFINED.0, 1067 | self.0, 1068 | a1.to_js_type(), 1069 | a1.to_js_value(), 1070 | a2.to_js_type(), 1071 | a2.to_js_value(), 1072 | a3.to_js_type(), 1073 | a3.to_js_value(), 1074 | a4.to_js_type(), 1075 | a4.to_js_value(), 1076 | a5.to_js_type(), 1077 | a5.to_js_value(), 1078 | a6.to_js_type(), 1079 | a6.to_js_value(), 1080 | a7.to_js_type(), 1081 | a7.to_js_value(), 1082 | a8.to_js_type(), 1083 | a8.to_js_value(), 1084 | a9.to_js_type(), 1085 | a9.to_js_value(), 1086 | a10.to_js_type(), 1087 | a10.to_js_value(), 1088 | ) 1089 | } 1090 | } 1091 | } 1092 | 1093 | type CStrPtr = i32; 1094 | 1095 | extern "C" { 1096 | fn jsffithrowerror(err: CStrPtr); 1097 | fn jsffirelease(obj: JSValue); 1098 | fn jsffiregister(code: CStrPtr) -> JSValue; 1099 | fn jsfficall0(obj: JSValue, function: JSValue) -> JSValue; 1100 | fn jsfficall1(obj: JSValue, function: JSValue, a1_type: JSType, a1: JSValue) -> JSValue; 1101 | fn jsfficall2( 1102 | obj: JSValue, 1103 | function: JSValue, 1104 | a1_type: JSType, 1105 | a1: JSValue, 1106 | a2_type: JSType, 1107 | a2: JSValue, 1108 | ) -> JSValue; 1109 | fn jsfficall3( 1110 | obj: JSValue, 1111 | function: JSValue, 1112 | a1_type: JSType, 1113 | a1: JSValue, 1114 | a2_type: JSType, 1115 | a2: JSValue, 1116 | a3_type: JSType, 1117 | a3: JSValue, 1118 | ) -> JSValue; 1119 | fn jsfficall4( 1120 | obj: JSValue, 1121 | function: JSValue, 1122 | a1_type: JSType, 1123 | a1: JSValue, 1124 | a2_type: JSType, 1125 | a2: JSValue, 1126 | a3_type: JSType, 1127 | a3: JSValue, 1128 | a4_type: JSType, 1129 | a4: JSValue, 1130 | ) -> JSValue; 1131 | fn jsfficall5( 1132 | obj: JSValue, 1133 | function: JSValue, 1134 | a1_type: JSType, 1135 | a1: JSValue, 1136 | a2_type: JSType, 1137 | a2: JSValue, 1138 | a3_type: JSType, 1139 | a3: JSValue, 1140 | a4_type: JSType, 1141 | a4: JSValue, 1142 | a5_type: JSType, 1143 | a5: JSValue, 1144 | ) -> JSValue; 1145 | fn jsfficall6( 1146 | obj: JSValue, 1147 | function: JSValue, 1148 | a1_type: JSType, 1149 | a1: JSValue, 1150 | a2_type: JSType, 1151 | a2: JSValue, 1152 | a3_type: JSType, 1153 | a3: JSValue, 1154 | a4_type: JSType, 1155 | a4: JSValue, 1156 | a5_type: JSType, 1157 | a5: JSValue, 1158 | a6_type: JSType, 1159 | a6: JSValue, 1160 | ) -> JSValue; 1161 | fn jsfficall7( 1162 | obj: JSValue, 1163 | function: JSValue, 1164 | a1_type: JSType, 1165 | a1: JSValue, 1166 | a2_type: JSType, 1167 | a2: JSValue, 1168 | a3_type: JSType, 1169 | a3: JSValue, 1170 | a4_type: JSType, 1171 | a4: JSValue, 1172 | a5_type: JSType, 1173 | a5: JSValue, 1174 | a6_type: JSType, 1175 | a6: JSValue, 1176 | a7_type: JSType, 1177 | a7: JSValue, 1178 | ) -> JSValue; 1179 | fn jsfficall8( 1180 | obj: JSValue, 1181 | function: JSValue, 1182 | a1_type: JSType, 1183 | a1: JSValue, 1184 | a2_type: JSType, 1185 | a2: JSValue, 1186 | a3_type: JSType, 1187 | a3: JSValue, 1188 | a4_type: JSType, 1189 | a4: JSValue, 1190 | a5_type: JSType, 1191 | a5: JSValue, 1192 | a6_type: JSType, 1193 | a6: JSValue, 1194 | a7_type: JSType, 1195 | a7: JSValue, 1196 | a8_type: JSType, 1197 | a8: JSValue, 1198 | ) -> JSValue; 1199 | fn jsfficall9( 1200 | obj: JSValue, 1201 | function: JSValue, 1202 | a1_type: JSType, 1203 | a1: JSValue, 1204 | a2_type: JSType, 1205 | a2: JSValue, 1206 | a3_type: JSType, 1207 | a3: JSValue, 1208 | a4_type: JSType, 1209 | a4: JSValue, 1210 | a5_type: JSType, 1211 | a5: JSValue, 1212 | a6_type: JSType, 1213 | a6: JSValue, 1214 | a7_type: JSType, 1215 | a7: JSValue, 1216 | a8_type: JSType, 1217 | a8: JSValue, 1218 | a9_type: JSType, 1219 | a9: JSValue, 1220 | ) -> JSValue; 1221 | fn jsfficall10( 1222 | obj: JSValue, 1223 | function: JSValue, 1224 | a1_type: JSType, 1225 | a1: JSValue, 1226 | a2_type: JSType, 1227 | a2: JSValue, 1228 | a3_type: JSType, 1229 | a3: JSValue, 1230 | a4_type: JSType, 1231 | a4: JSValue, 1232 | a5_type: JSType, 1233 | a5: JSValue, 1234 | a6_type: JSType, 1235 | a6: JSValue, 1236 | a7_type: JSType, 1237 | a7: JSValue, 1238 | a8_type: JSType, 1239 | a8: JSValue, 1240 | a9_type: JSType, 1241 | a9: JSValue, 1242 | a10_type: JSType, 1243 | a10: JSValue, 1244 | ) -> JSValue; 1245 | } 1246 | 1247 | pub fn release_object(obj: &JSObject) { 1248 | unsafe { jsffirelease(obj.0) } 1249 | } 1250 | 1251 | pub fn register_function(code: &str) -> JSInvoker { 1252 | unsafe { JSInvoker(jsffiregister(cstr(code))) } 1253 | } 1254 | 1255 | #[no_mangle] 1256 | fn jsfficallback( 1257 | id: u32, 1258 | a1: JSValue, 1259 | a2: JSValue, 1260 | a3: JSValue, 1261 | a4: JSValue, 1262 | a5: JSValue, 1263 | a6: JSValue, 1264 | a7: JSValue, 1265 | a8: JSValue, 1266 | a9: JSValue, 1267 | a10: JSValue, 1268 | ) -> () { 1269 | let h = get_callback(id); 1270 | let handler_ref = h.unwrap().clone(); 1271 | let mut handler = handler_ref.lock(); 1272 | match &mut *handler { 1273 | CallbackHandler::Callback0(c) => c(), 1274 | CallbackHandler::Callback1(c) => c(a1), 1275 | CallbackHandler::Callback2(c) => c(a1, a2), 1276 | CallbackHandler::Callback3(c) => c(a1, a2, a3), 1277 | CallbackHandler::Callback4(c) => c(a1, a2, a3, a4), 1278 | CallbackHandler::Callback5(c) => c(a1, a2, a3, a4, a5), 1279 | CallbackHandler::Callback6(c) => c(a1, a2, a3, a4, a5, a6), 1280 | CallbackHandler::Callback7(c) => c(a1, a2, a3, a4, a5, a6, a7), 1281 | CallbackHandler::Callback8(c) => c(a1, a2, a3, a4, a5, a6, a7, a8), 1282 | CallbackHandler::Callback9(c) => c(a1, a2, a3, a4, a5, a6, a7, a8, a9), 1283 | CallbackHandler::Callback10(c) => c(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10), 1284 | } 1285 | } 1286 | 1287 | #[no_mangle] 1288 | fn jsffimalloc(size: i32) -> *mut u8 { 1289 | let mut buf = Vec::with_capacity(size as usize); 1290 | let ptr = buf.as_mut_ptr(); 1291 | core::mem::forget(buf); 1292 | ptr 1293 | } 1294 | 1295 | enum CallbackHandler { 1296 | Callback0(Box () + Send + 'static>), 1297 | Callback1(Box () + Send + 'static>), 1298 | Callback2(Box () + Send + 'static>), 1299 | Callback3(Box () + Send + 'static>), 1300 | Callback4(Box () + Send + 'static>), 1301 | Callback5(Box () + Send + 'static>), 1302 | Callback6( 1303 | Box () + Send + 'static>, 1304 | ), 1305 | Callback7( 1306 | Box< 1307 | dyn FnMut(JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue) -> () 1308 | + Send 1309 | + 'static, 1310 | >, 1311 | ), 1312 | Callback8( 1313 | Box< 1314 | dyn FnMut(JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue) -> () 1315 | + Send 1316 | + 'static, 1317 | >, 1318 | ), 1319 | Callback9( 1320 | Box< 1321 | dyn FnMut( 1322 | JSValue, 1323 | JSValue, 1324 | JSValue, 1325 | JSValue, 1326 | JSValue, 1327 | JSValue, 1328 | JSValue, 1329 | JSValue, 1330 | JSValue, 1331 | ) -> () 1332 | + Send 1333 | + 'static, 1334 | >, 1335 | ), 1336 | Callback10( 1337 | Box< 1338 | dyn FnMut( 1339 | JSValue, 1340 | JSValue, 1341 | JSValue, 1342 | JSValue, 1343 | JSValue, 1344 | JSValue, 1345 | JSValue, 1346 | JSValue, 1347 | JSValue, 1348 | JSValue, 1349 | ) -> () 1350 | + Send 1351 | + 'static, 1352 | >, 1353 | ), 1354 | } 1355 | 1356 | type CallbackHandle = u32; 1357 | 1358 | struct CallbackManager { 1359 | cur_id: CallbackHandle, 1360 | keys: Vec, 1361 | handlers: Vec>>, 1362 | } 1363 | 1364 | lazy_static! { 1365 | static ref INSTANCE: Mutex = { 1366 | Mutex::new(CallbackManager { 1367 | cur_id: 0, 1368 | keys: Vec::new(), 1369 | handlers: Vec::new(), 1370 | }) 1371 | }; 1372 | } 1373 | 1374 | fn get_callbacks() -> &'static Mutex { 1375 | &INSTANCE 1376 | } 1377 | 1378 | fn get_callback(id: CallbackHandle) -> Option>> { 1379 | let cbs = get_callbacks().lock(); 1380 | let index = cbs.keys.iter().position(|&r| r == id); 1381 | if let Some(i) = index { 1382 | let handler_ref = cbs.handlers.get(i).unwrap().clone(); 1383 | core::mem::drop(cbs); 1384 | Some(handler_ref) 1385 | } else { 1386 | None 1387 | } 1388 | } 1389 | 1390 | pub fn remove_callback(cb: JSFunction) { 1391 | let mut cbs = get_callbacks().lock(); 1392 | let index = cbs.keys.iter().position(|&r| r == cb.0 as u32); 1393 | if let Some(i) = index { 1394 | cbs.keys.remove(i); 1395 | cbs.handlers.remove(i); 1396 | } 1397 | } 1398 | 1399 | fn create_callback(cb: CallbackHandler) -> JSFunction { 1400 | let mut h = get_callbacks().lock(); 1401 | h.cur_id += 1; 1402 | let id = h.cur_id; 1403 | h.keys.push(id); 1404 | h.handlers.push(Arc::new(Mutex::new(cb))); 1405 | return JSFunction::from(id); 1406 | } 1407 | 1408 | pub fn create_callback_0(cb: impl FnMut() -> () + Send + 'static) -> JSFunction { 1409 | create_callback(CallbackHandler::Callback0(Box::new(cb))) 1410 | } 1411 | 1412 | pub fn create_callback_1(cb: impl FnMut(JSValue) -> () + Send + 'static) -> JSFunction { 1413 | create_callback(CallbackHandler::Callback1(Box::new(cb))) 1414 | } 1415 | 1416 | pub fn create_callback_2(cb: impl FnMut(JSValue, JSValue) -> () + Send + 'static) -> JSFunction { 1417 | create_callback(CallbackHandler::Callback2(Box::new(cb))) 1418 | } 1419 | 1420 | pub fn create_callback_3( 1421 | cb: impl FnMut(JSValue, JSValue, JSValue) -> () + Send + 'static, 1422 | ) -> JSFunction { 1423 | create_callback(CallbackHandler::Callback3(Box::new(cb))) 1424 | } 1425 | 1426 | pub fn create_callback_4( 1427 | cb: impl FnMut(JSValue, JSValue, JSValue, JSValue) -> () + Send + 'static, 1428 | ) -> JSFunction { 1429 | create_callback(CallbackHandler::Callback4(Box::new(cb))) 1430 | } 1431 | 1432 | pub fn create_callback_5( 1433 | cb: impl FnMut(JSValue, JSValue, JSValue, JSValue, JSValue) -> () + Send + 'static, 1434 | ) -> JSFunction { 1435 | create_callback(CallbackHandler::Callback5(Box::new(cb))) 1436 | } 1437 | 1438 | pub fn create_callback_6( 1439 | cb: impl FnMut(JSValue, JSValue, JSValue, JSValue, JSValue, JSValue) -> () + Send + 'static, 1440 | ) -> JSFunction { 1441 | create_callback(CallbackHandler::Callback6(Box::new(cb))) 1442 | } 1443 | 1444 | pub fn create_callback_7( 1445 | cb: impl FnMut(JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue) -> () + Send + 'static, 1446 | ) -> JSFunction { 1447 | create_callback(CallbackHandler::Callback7(Box::new(cb))) 1448 | } 1449 | 1450 | pub fn create_callback_8( 1451 | cb: impl FnMut(JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue) -> () 1452 | + Send 1453 | + 'static, 1454 | ) -> JSFunction { 1455 | create_callback(CallbackHandler::Callback8(Box::new(cb))) 1456 | } 1457 | 1458 | pub fn create_callback_9( 1459 | cb: impl FnMut(JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue, JSValue) -> () 1460 | + Send 1461 | + 'static, 1462 | ) -> JSFunction { 1463 | create_callback(CallbackHandler::Callback9(Box::new(cb))) 1464 | } 1465 | 1466 | pub fn create_callback_10( 1467 | cb: impl FnMut( 1468 | JSValue, 1469 | JSValue, 1470 | JSValue, 1471 | JSValue, 1472 | JSValue, 1473 | JSValue, 1474 | JSValue, 1475 | JSValue, 1476 | JSValue, 1477 | JSValue, 1478 | ) -> () 1479 | + Send 1480 | + 'static, 1481 | ) -> JSFunction { 1482 | create_callback(CallbackHandler::Callback10(Box::new(cb))) 1483 | } 1484 | 1485 | struct CallbackFuture0 { 1486 | shared_state: Arc>, 1487 | } 1488 | 1489 | /// Shared state between the future and the waiting thread 1490 | struct SharedState0 { 1491 | completed: bool, 1492 | waker: Option, 1493 | result: (), 1494 | } 1495 | 1496 | impl Future for CallbackFuture0 { 1497 | type Output = (); 1498 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1499 | let mut shared_state = self.shared_state.lock(); 1500 | if shared_state.completed { 1501 | Poll::Ready(shared_state.result) 1502 | } else { 1503 | shared_state.waker = Some(cx.waker().clone()); 1504 | Poll::Pending 1505 | } 1506 | } 1507 | } 1508 | 1509 | impl CallbackFuture0 { 1510 | fn new() -> (Self, JSFunction) { 1511 | let shared_state = Arc::new(Mutex::new(SharedState0 { 1512 | completed: false, 1513 | waker: None, 1514 | result: (), 1515 | })); 1516 | 1517 | let thread_shared_state = shared_state.clone(); 1518 | let id = create_callback(CallbackHandler::Callback0(Box::new(move || { 1519 | let mut shared_state = thread_shared_state.lock(); 1520 | shared_state.completed = true; 1521 | shared_state.result = (); 1522 | if let Some(waker) = shared_state.waker.take() { 1523 | core::mem::drop(shared_state); 1524 | waker.wake() 1525 | } 1526 | }))); 1527 | (CallbackFuture0 { shared_state }, id) 1528 | } 1529 | } 1530 | 1531 | pub fn create_callback_future_0() -> (impl Future, JSFunction) { 1532 | CallbackFuture0::new() 1533 | } 1534 | 1535 | struct CallbackFuture1 { 1536 | shared_state: Arc>, 1537 | } 1538 | 1539 | /// Shared state between the future and the waiting thread 1540 | struct SharedState1 { 1541 | completed: bool, 1542 | waker: Option, 1543 | result: JSValue, 1544 | } 1545 | 1546 | impl Future for CallbackFuture1 { 1547 | type Output = JSValue; 1548 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1549 | let mut shared_state = self.shared_state.lock(); 1550 | if shared_state.completed { 1551 | Poll::Ready(shared_state.result) 1552 | } else { 1553 | shared_state.waker = Some(cx.waker().clone()); 1554 | Poll::Pending 1555 | } 1556 | } 1557 | } 1558 | 1559 | impl CallbackFuture1 { 1560 | fn new() -> (Self, JSFunction) { 1561 | let shared_state = Arc::new(Mutex::new(SharedState1 { 1562 | completed: false, 1563 | waker: None, 1564 | result: UNDEFINED.0, 1565 | })); 1566 | 1567 | let thread_shared_state = shared_state.clone(); 1568 | let id = create_callback(CallbackHandler::Callback1(Box::new(move |v: JSValue| { 1569 | let mut shared_state = thread_shared_state.lock(); 1570 | shared_state.completed = true; 1571 | shared_state.result = v; 1572 | if let Some(waker) = shared_state.waker.take() { 1573 | core::mem::drop(shared_state); 1574 | waker.wake() 1575 | } 1576 | }))); 1577 | (CallbackFuture1 { shared_state }, id) 1578 | } 1579 | } 1580 | 1581 | pub fn create_callback_future_1() -> (impl Future, JSFunction) { 1582 | CallbackFuture1::new() 1583 | } 1584 | 1585 | struct CallbackFuture2 { 1586 | shared_state: Arc>, 1587 | } 1588 | 1589 | /// Shared state between the future and the waiting thread 1590 | struct SharedState2 { 1591 | completed: bool, 1592 | waker: Option, 1593 | result: (JSValue, JSValue), 1594 | } 1595 | 1596 | impl Future for CallbackFuture2 { 1597 | type Output = (JSValue, JSValue); 1598 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1599 | let mut shared_state = self.shared_state.lock(); 1600 | if shared_state.completed { 1601 | Poll::Ready(shared_state.result) 1602 | } else { 1603 | shared_state.waker = Some(cx.waker().clone()); 1604 | Poll::Pending 1605 | } 1606 | } 1607 | } 1608 | 1609 | impl CallbackFuture2 { 1610 | fn new() -> (Self, JSFunction) { 1611 | let shared_state = Arc::new(Mutex::new(SharedState2 { 1612 | completed: false, 1613 | waker: None, 1614 | result: (UNDEFINED.0, UNDEFINED.0), 1615 | })); 1616 | 1617 | let thread_shared_state = shared_state.clone(); 1618 | let id = create_callback(CallbackHandler::Callback2(Box::new( 1619 | move |a1: JSValue, a2: JSValue| { 1620 | let mut shared_state = thread_shared_state.lock(); 1621 | shared_state.completed = true; 1622 | shared_state.result = (a1, a2); 1623 | if let Some(waker) = shared_state.waker.take() { 1624 | core::mem::drop(shared_state); 1625 | waker.wake() 1626 | } 1627 | }, 1628 | ))); 1629 | (CallbackFuture2 { shared_state }, id) 1630 | } 1631 | } 1632 | 1633 | pub fn create_callback_future_2() -> (impl Future, JSFunction) { 1634 | CallbackFuture2::new() 1635 | } 1636 | 1637 | struct CallbackFuture3 { 1638 | shared_state: Arc>, 1639 | } 1640 | 1641 | /// Shared state between the future and the waiting thread 1642 | struct SharedState3 { 1643 | completed: bool, 1644 | waker: Option, 1645 | result: (JSValue, JSValue, JSValue), 1646 | } 1647 | 1648 | impl Future for CallbackFuture3 { 1649 | type Output = (JSValue, JSValue, JSValue); 1650 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1651 | let mut shared_state = self.shared_state.lock(); 1652 | if shared_state.completed { 1653 | Poll::Ready(shared_state.result) 1654 | } else { 1655 | shared_state.waker = Some(cx.waker().clone()); 1656 | Poll::Pending 1657 | } 1658 | } 1659 | } 1660 | 1661 | impl CallbackFuture3 { 1662 | fn new() -> (Self, JSFunction) { 1663 | let shared_state = Arc::new(Mutex::new(SharedState3 { 1664 | completed: false, 1665 | waker: None, 1666 | result: (UNDEFINED.0, UNDEFINED.0, UNDEFINED.0), 1667 | })); 1668 | 1669 | let thread_shared_state = shared_state.clone(); 1670 | let id = create_callback(CallbackHandler::Callback3(Box::new( 1671 | move |a1: JSValue, a2: JSValue, a3: JSValue| { 1672 | let mut shared_state = thread_shared_state.lock(); 1673 | shared_state.completed = true; 1674 | shared_state.result = (a1, a2, a3); 1675 | if let Some(waker) = shared_state.waker.take() { 1676 | core::mem::drop(shared_state); 1677 | waker.wake() 1678 | } 1679 | }, 1680 | ))); 1681 | (CallbackFuture3 { shared_state }, id) 1682 | } 1683 | } 1684 | 1685 | pub fn create_callback_future_3() -> (impl Future, JSFunction) { 1686 | CallbackFuture3::new() 1687 | } 1688 | 1689 | struct CallbackFuture4 { 1690 | shared_state: Arc>, 1691 | } 1692 | 1693 | /// Shared state between the future and the waiting thread 1694 | struct SharedState4 { 1695 | completed: bool, 1696 | waker: Option, 1697 | result: (JSValue, JSValue, JSValue, JSValue), 1698 | } 1699 | 1700 | impl Future for CallbackFuture4 { 1701 | type Output = (JSValue, JSValue, JSValue, JSValue); 1702 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1703 | let mut shared_state = self.shared_state.lock(); 1704 | if shared_state.completed { 1705 | Poll::Ready(shared_state.result) 1706 | } else { 1707 | shared_state.waker = Some(cx.waker().clone()); 1708 | Poll::Pending 1709 | } 1710 | } 1711 | } 1712 | 1713 | impl CallbackFuture4 { 1714 | fn new() -> (Self, JSFunction) { 1715 | let shared_state = Arc::new(Mutex::new(SharedState4 { 1716 | completed: false, 1717 | waker: None, 1718 | result: (UNDEFINED.0, UNDEFINED.0, UNDEFINED.0, UNDEFINED.0), 1719 | })); 1720 | 1721 | let thread_shared_state = shared_state.clone(); 1722 | let id = create_callback(CallbackHandler::Callback4(Box::new( 1723 | move |a1: JSValue, a2: JSValue, a3: JSValue, a4: JSValue| { 1724 | let mut shared_state = thread_shared_state.lock(); 1725 | shared_state.completed = true; 1726 | shared_state.result = (a1, a2, a3, a4); 1727 | if let Some(waker) = shared_state.waker.take() { 1728 | core::mem::drop(shared_state); 1729 | waker.wake() 1730 | } 1731 | }, 1732 | ))); 1733 | (CallbackFuture4 { shared_state }, id) 1734 | } 1735 | } 1736 | 1737 | pub fn create_callback_future_4() -> (impl Future, JSFunction) { 1738 | CallbackFuture4::new() 1739 | } 1740 | 1741 | struct CallbackFuture5 { 1742 | shared_state: Arc>, 1743 | } 1744 | 1745 | /// Shared state between the future and the waiting thread 1746 | struct SharedState5 { 1747 | completed: bool, 1748 | waker: Option, 1749 | result: (JSValue, JSValue, JSValue, JSValue, JSValue), 1750 | } 1751 | 1752 | impl Future for CallbackFuture5 { 1753 | type Output = (JSValue, JSValue, JSValue, JSValue, JSValue); 1754 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1755 | let mut shared_state = self.shared_state.lock(); 1756 | if shared_state.completed { 1757 | Poll::Ready(shared_state.result) 1758 | } else { 1759 | shared_state.waker = Some(cx.waker().clone()); 1760 | Poll::Pending 1761 | } 1762 | } 1763 | } 1764 | 1765 | impl CallbackFuture5 { 1766 | fn new() -> (Self, JSFunction) { 1767 | let shared_state = Arc::new(Mutex::new(SharedState5 { 1768 | completed: false, 1769 | waker: None, 1770 | result: ( 1771 | UNDEFINED.0, 1772 | UNDEFINED.0, 1773 | UNDEFINED.0, 1774 | UNDEFINED.0, 1775 | UNDEFINED.0, 1776 | ), 1777 | })); 1778 | 1779 | let thread_shared_state = shared_state.clone(); 1780 | let id = create_callback(CallbackHandler::Callback5(Box::new( 1781 | move |a1: JSValue, a2: JSValue, a3: JSValue, a4: JSValue, a5: JSValue| { 1782 | let mut shared_state = thread_shared_state.lock(); 1783 | shared_state.completed = true; 1784 | shared_state.result = (a1, a2, a3, a4, a5); 1785 | if let Some(waker) = shared_state.waker.take() { 1786 | core::mem::drop(shared_state); 1787 | waker.wake() 1788 | } 1789 | }, 1790 | ))); 1791 | (CallbackFuture5 { shared_state }, id) 1792 | } 1793 | } 1794 | 1795 | pub fn create_callback_future_5() -> (impl Future, JSFunction) { 1796 | CallbackFuture5::new() 1797 | } 1798 | 1799 | struct CallbackFuture6 { 1800 | shared_state: Arc>, 1801 | } 1802 | 1803 | /// Shared state between the future and the waiting thread 1804 | struct SharedState6 { 1805 | completed: bool, 1806 | waker: Option, 1807 | result: (JSValue, JSValue, JSValue, JSValue, JSValue, JSValue), 1808 | } 1809 | 1810 | impl Future for CallbackFuture6 { 1811 | type Output = (JSValue, JSValue, JSValue, JSValue, JSValue, JSValue); 1812 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1813 | let mut shared_state = self.shared_state.lock(); 1814 | if shared_state.completed { 1815 | Poll::Ready(shared_state.result) 1816 | } else { 1817 | shared_state.waker = Some(cx.waker().clone()); 1818 | Poll::Pending 1819 | } 1820 | } 1821 | } 1822 | 1823 | impl CallbackFuture6 { 1824 | fn new() -> (Self, JSFunction) { 1825 | let shared_state = Arc::new(Mutex::new(SharedState6 { 1826 | completed: false, 1827 | waker: None, 1828 | result: ( 1829 | UNDEFINED.0, 1830 | UNDEFINED.0, 1831 | UNDEFINED.0, 1832 | UNDEFINED.0, 1833 | UNDEFINED.0, 1834 | UNDEFINED.0, 1835 | ), 1836 | })); 1837 | 1838 | let thread_shared_state = shared_state.clone(); 1839 | let id = create_callback(CallbackHandler::Callback6(Box::new( 1840 | move |a1: JSValue, a2: JSValue, a3: JSValue, a4: JSValue, a5: JSValue, a6: JSValue| { 1841 | let mut shared_state = thread_shared_state.lock(); 1842 | shared_state.completed = true; 1843 | shared_state.result = (a1, a2, a3, a4, a5, a6); 1844 | if let Some(waker) = shared_state.waker.take() { 1845 | core::mem::drop(shared_state); 1846 | waker.wake() 1847 | } 1848 | }, 1849 | ))); 1850 | (CallbackFuture6 { shared_state }, id) 1851 | } 1852 | } 1853 | 1854 | pub fn create_callback_future_6() -> (impl Future, JSFunction) { 1855 | CallbackFuture6::new() 1856 | } 1857 | 1858 | struct CallbackFuture7 { 1859 | shared_state: Arc>, 1860 | } 1861 | 1862 | /// Shared state between the future and the waiting thread 1863 | struct SharedState7 { 1864 | completed: bool, 1865 | waker: Option, 1866 | result: ( 1867 | JSValue, 1868 | JSValue, 1869 | JSValue, 1870 | JSValue, 1871 | JSValue, 1872 | JSValue, 1873 | JSValue, 1874 | ), 1875 | } 1876 | 1877 | impl Future for CallbackFuture7 { 1878 | type Output = ( 1879 | JSValue, 1880 | JSValue, 1881 | JSValue, 1882 | JSValue, 1883 | JSValue, 1884 | JSValue, 1885 | JSValue, 1886 | ); 1887 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1888 | let mut shared_state = self.shared_state.lock(); 1889 | if shared_state.completed { 1890 | Poll::Ready(shared_state.result) 1891 | } else { 1892 | shared_state.waker = Some(cx.waker().clone()); 1893 | Poll::Pending 1894 | } 1895 | } 1896 | } 1897 | 1898 | impl CallbackFuture7 { 1899 | fn new() -> (Self, JSFunction) { 1900 | let shared_state = Arc::new(Mutex::new(SharedState7 { 1901 | completed: false, 1902 | waker: None, 1903 | result: ( 1904 | UNDEFINED.0, 1905 | UNDEFINED.0, 1906 | UNDEFINED.0, 1907 | UNDEFINED.0, 1908 | UNDEFINED.0, 1909 | UNDEFINED.0, 1910 | UNDEFINED.0, 1911 | ), 1912 | })); 1913 | 1914 | let thread_shared_state = shared_state.clone(); 1915 | let id = create_callback(CallbackHandler::Callback7(Box::new( 1916 | move |a1: JSValue, 1917 | a2: JSValue, 1918 | a3: JSValue, 1919 | a4: JSValue, 1920 | a5: JSValue, 1921 | a6: JSValue, 1922 | a7: JSValue| { 1923 | let mut shared_state = thread_shared_state.lock(); 1924 | shared_state.completed = true; 1925 | shared_state.result = (a1, a2, a3, a4, a5, a6, a7); 1926 | if let Some(waker) = shared_state.waker.take() { 1927 | core::mem::drop(shared_state); 1928 | waker.wake() 1929 | } 1930 | }, 1931 | ))); 1932 | (CallbackFuture7 { shared_state }, id) 1933 | } 1934 | } 1935 | 1936 | pub fn create_callback_future_7() -> (impl Future, JSFunction) { 1937 | CallbackFuture7::new() 1938 | } 1939 | 1940 | struct CallbackFuture8 { 1941 | shared_state: Arc>, 1942 | } 1943 | 1944 | /// Shared state between the future and the waiting thread 1945 | struct SharedState8 { 1946 | completed: bool, 1947 | waker: Option, 1948 | result: ( 1949 | JSValue, 1950 | JSValue, 1951 | JSValue, 1952 | JSValue, 1953 | JSValue, 1954 | JSValue, 1955 | JSValue, 1956 | JSValue, 1957 | ), 1958 | } 1959 | 1960 | impl Future for CallbackFuture8 { 1961 | type Output = ( 1962 | JSValue, 1963 | JSValue, 1964 | JSValue, 1965 | JSValue, 1966 | JSValue, 1967 | JSValue, 1968 | JSValue, 1969 | JSValue, 1970 | ); 1971 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 1972 | let mut shared_state = self.shared_state.lock(); 1973 | if shared_state.completed { 1974 | Poll::Ready(shared_state.result) 1975 | } else { 1976 | shared_state.waker = Some(cx.waker().clone()); 1977 | Poll::Pending 1978 | } 1979 | } 1980 | } 1981 | 1982 | impl CallbackFuture8 { 1983 | fn new() -> (Self, JSFunction) { 1984 | let shared_state = Arc::new(Mutex::new(SharedState8 { 1985 | completed: false, 1986 | waker: None, 1987 | result: ( 1988 | UNDEFINED.0, 1989 | UNDEFINED.0, 1990 | UNDEFINED.0, 1991 | UNDEFINED.0, 1992 | UNDEFINED.0, 1993 | UNDEFINED.0, 1994 | UNDEFINED.0, 1995 | UNDEFINED.0, 1996 | ), 1997 | })); 1998 | 1999 | let thread_shared_state = shared_state.clone(); 2000 | let id = create_callback(CallbackHandler::Callback8(Box::new( 2001 | move |a1: JSValue, 2002 | a2: JSValue, 2003 | a3: JSValue, 2004 | a4: JSValue, 2005 | a5: JSValue, 2006 | a6: JSValue, 2007 | a7: JSValue, 2008 | a8: JSValue| { 2009 | let mut shared_state = thread_shared_state.lock(); 2010 | shared_state.completed = true; 2011 | shared_state.result = (a1, a2, a3, a4, a5, a6, a7, a8); 2012 | if let Some(waker) = shared_state.waker.take() { 2013 | core::mem::drop(shared_state); 2014 | waker.wake() 2015 | } 2016 | }, 2017 | ))); 2018 | (CallbackFuture8 { shared_state }, id) 2019 | } 2020 | } 2021 | 2022 | pub fn create_callback_future_8() -> (impl Future, JSFunction) { 2023 | CallbackFuture8::new() 2024 | } 2025 | 2026 | struct CallbackFuture9 { 2027 | shared_state: Arc>, 2028 | } 2029 | 2030 | /// Shared state between the future and the waiting thread 2031 | struct SharedState9 { 2032 | completed: bool, 2033 | waker: Option, 2034 | result: ( 2035 | JSValue, 2036 | JSValue, 2037 | JSValue, 2038 | JSValue, 2039 | JSValue, 2040 | JSValue, 2041 | JSValue, 2042 | JSValue, 2043 | JSValue, 2044 | ), 2045 | } 2046 | 2047 | impl Future for CallbackFuture9 { 2048 | type Output = ( 2049 | JSValue, 2050 | JSValue, 2051 | JSValue, 2052 | JSValue, 2053 | JSValue, 2054 | JSValue, 2055 | JSValue, 2056 | JSValue, 2057 | JSValue, 2058 | ); 2059 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 2060 | let mut shared_state = self.shared_state.lock(); 2061 | if shared_state.completed { 2062 | Poll::Ready(shared_state.result) 2063 | } else { 2064 | shared_state.waker = Some(cx.waker().clone()); 2065 | Poll::Pending 2066 | } 2067 | } 2068 | } 2069 | 2070 | impl CallbackFuture9 { 2071 | fn new() -> (Self, JSFunction) { 2072 | let shared_state = Arc::new(Mutex::new(SharedState9 { 2073 | completed: false, 2074 | waker: None, 2075 | result: ( 2076 | UNDEFINED.0, 2077 | UNDEFINED.0, 2078 | UNDEFINED.0, 2079 | UNDEFINED.0, 2080 | UNDEFINED.0, 2081 | UNDEFINED.0, 2082 | UNDEFINED.0, 2083 | UNDEFINED.0, 2084 | UNDEFINED.0, 2085 | ), 2086 | })); 2087 | 2088 | let thread_shared_state = shared_state.clone(); 2089 | let id = create_callback(CallbackHandler::Callback9(Box::new( 2090 | move |a1: JSValue, 2091 | a2: JSValue, 2092 | a3: JSValue, 2093 | a4: JSValue, 2094 | a5: JSValue, 2095 | a6: JSValue, 2096 | a7: JSValue, 2097 | a8: JSValue, 2098 | a9: JSValue| { 2099 | let mut shared_state = thread_shared_state.lock(); 2100 | shared_state.completed = true; 2101 | shared_state.result = (a1, a2, a3, a4, a5, a6, a7, a8, a9); 2102 | if let Some(waker) = shared_state.waker.take() { 2103 | core::mem::drop(shared_state); 2104 | waker.wake() 2105 | } 2106 | }, 2107 | ))); 2108 | (CallbackFuture9 { shared_state }, id) 2109 | } 2110 | } 2111 | 2112 | pub fn create_callback_future_9() -> (impl Future, JSFunction) { 2113 | CallbackFuture9::new() 2114 | } 2115 | 2116 | struct CallbackFuture10 { 2117 | shared_state: Arc>, 2118 | } 2119 | 2120 | /// Shared state between the future and the waiting thread 2121 | struct SharedState10 { 2122 | completed: bool, 2123 | waker: Option, 2124 | result: ( 2125 | JSValue, 2126 | JSValue, 2127 | JSValue, 2128 | JSValue, 2129 | JSValue, 2130 | JSValue, 2131 | JSValue, 2132 | JSValue, 2133 | JSValue, 2134 | JSValue, 2135 | ), 2136 | } 2137 | 2138 | impl Future for CallbackFuture10 { 2139 | type Output = ( 2140 | JSValue, 2141 | JSValue, 2142 | JSValue, 2143 | JSValue, 2144 | JSValue, 2145 | JSValue, 2146 | JSValue, 2147 | JSValue, 2148 | JSValue, 2149 | JSValue, 2150 | ); 2151 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 2152 | let mut shared_state = self.shared_state.lock(); 2153 | if shared_state.completed { 2154 | Poll::Ready(shared_state.result) 2155 | } else { 2156 | shared_state.waker = Some(cx.waker().clone()); 2157 | Poll::Pending 2158 | } 2159 | } 2160 | } 2161 | 2162 | impl CallbackFuture10 { 2163 | fn new() -> (Self, JSFunction) { 2164 | let shared_state = Arc::new(Mutex::new(SharedState10 { 2165 | completed: false, 2166 | waker: None, 2167 | result: ( 2168 | UNDEFINED.0, 2169 | UNDEFINED.0, 2170 | UNDEFINED.0, 2171 | UNDEFINED.0, 2172 | UNDEFINED.0, 2173 | UNDEFINED.0, 2174 | UNDEFINED.0, 2175 | UNDEFINED.0, 2176 | UNDEFINED.0, 2177 | UNDEFINED.0, 2178 | ), 2179 | })); 2180 | 2181 | let thread_shared_state = shared_state.clone(); 2182 | let id = create_callback(CallbackHandler::Callback10(Box::new( 2183 | move |a1: JSValue, 2184 | a2: JSValue, 2185 | a3: JSValue, 2186 | a4: JSValue, 2187 | a5: JSValue, 2188 | a6: JSValue, 2189 | a7: JSValue, 2190 | a8: JSValue, 2191 | a9: JSValue, 2192 | a10: JSValue| { 2193 | let mut shared_state = thread_shared_state.lock(); 2194 | shared_state.completed = true; 2195 | shared_state.result = (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); 2196 | if let Some(waker) = shared_state.waker.take() { 2197 | core::mem::drop(shared_state); 2198 | waker.wake() 2199 | } 2200 | }, 2201 | ))); 2202 | (CallbackFuture10 { shared_state }, id) 2203 | } 2204 | } 2205 | 2206 | pub fn create_callback_future_10() -> (impl Future, JSFunction) { 2207 | CallbackFuture10::new() 2208 | } 2209 | 2210 | pub fn throw_error(err: &str) { 2211 | unsafe { 2212 | jsffithrowerror(cstr(err)); 2213 | } 2214 | } 2215 | 2216 | pub fn get_property(o: impl ToJSValue, prop: &str) -> JSValue { 2217 | unsafe { 2218 | jsfficall2( 2219 | UNDEFINED.0, 2220 | 0.0, 2221 | o.to_js_type(), 2222 | o.to_js_value(), 2223 | TYPE_STRING, 2224 | cstr(prop) as JSValue, 2225 | ) 2226 | } 2227 | } 2228 | 2229 | pub fn set_property(o: impl ToJSValue, prop: &str, v: impl ToJSValue) { 2230 | unsafe { 2231 | jsfficall3( 2232 | UNDEFINED.0, 2233 | 1.0, 2234 | o.to_js_type(), 2235 | o.to_js_value(), 2236 | TYPE_STRING, 2237 | cstr(prop) as JSValue, 2238 | v.to_js_type(), 2239 | v.to_js_value(), 2240 | ); 2241 | } 2242 | } 2243 | --------------------------------------------------------------------------------