How can you compile the Rust library for asm.js?

I have a Rust library with the following regular structure:

Cargo.toml
src
 |--lib.rs
.cargo
 |--config (specifies target=asmjs-unknown-emscripten)
target
 |......

When I do cargo build, I get a new directory under the target name asmjs-unknown-emscripten, but the .js files I expect do not exist.

As this user notes , you need to do something special to export functions to asm.js, in addition to being publicly available:

Basically, you have this template now:

#[link_args = "-s EXPORTED_FUNCTIONS=['_hello_world']"]
extern {}

fn main() {}

#[no_mangle]
pub extern fn hello_world(n: c_int) -> c_int {
    n + 1
}

Then you can use this in your javascript to access and call the function:

var hello_world = cwrap('hello_world', 'number', ['number']);

console.log(hello_world(41));

However, Rust complains about the directive #[link_args...]as deprecated. Is there any documentation that can explain how this works?

+4
1

! fable.

Rust - JavaScript, - args , cargo rustc --target asmjs-unknown-emscripten call-into-lib.rs.

, cargo rustc

cd lib1
cargo build --target asmjs-unknown-emscripten
rustc --target=asmjs-unknown-emscripten src\lib.rs
cd ..

cd lib2
cargo build --target asmjs-unknown-emscripten
rustc --target=asmjs-unknown-emscripten src\lib.rs --extern lib1=..\lib1\target\asmjs-unknown-emscripten\debug\liblib1.rlib
cd ..

cd lib3
cargo build --target asmjs-unknown-emscripten
rem rustc --target=asmjs-unknown-emscripten src\lib.rs --extern webplatform=..\lib3\target\asmjs-unknown-emscripten\debug\deps\libwebplatform-80d107ece17b262d.rlib
rem the line above fails with "error[E0460]: found possibly newer version of crate `libc` which `webplatform` depends on"
cd ..

cd app
cargo build --target asmjs-unknown-emscripten
cd ..

. so-41492672-rust-js-structure. , JavaScript .

, - . .

P.S. , rustc , -Z print-link-args.

+2

Source: https://habr.com/ru/post/1665921/


All Articles