LinkError when trying Hello World in Rust using WebAssembly

I am trying to run a welcome program made in Rust using WebAssembly, but when I try to load the program, I get an error.

I managed to run it, following some tutorials that I found, the problem is that they use Emscripten to create JavaScript and HTML to load the code, but there are tons and tons of templates and other things in this JavaScript and HTML. I got a little lost and instead wanted to try to get a really simple example of how I load myself.

I ran the following command to compile hello.wasm

echo 'fn main() { println!("Hello, Emscripten!"); }' > hello.rs
rustc --target=wasm32-unknown-emscripten hello.rs

To download hello.wasm, I took this example from Mozilla WebAssembly documents and tried to run it.

var importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};

fetch('hello.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, importObject)
).then(results => {
  // Do something with the compiled results!
});

WebAssembly.instantiate failure:

LinkError: Import #0 module="env" error: module is not an object or function

- , - , , , HTML JavaScript, , .

+4
1

, WASM. WASM , , . Emscripten JS-, WASM ( "" , Emscripten WASM).

Emscripten (JS ), .

, , :


WASM

- ( , ). / . , , . , , ..

print. - print - , . , ""? , - . : WASM print - !

, , . - . "" . WASM - ! .

- ( "" ) / . , extern crate Rust .cpp ++.

, WASM, . , .

? ! wasm-dis (), wasm : $ wasm-dis hello.wasm > hello.wast. , :

(import "env" "DYNAMICTOP_PTR" (global $import$0 i32))
(import "env" "STACKTOP" (global $import$1 i32))
(import "env" "STACK_MAX" (global $import$2 i32))
(import "env" "abort" (func $import$3 (param i32)))
...
(58 more)

, wast, , . , print!

(, , (import "env" "print" ...). , : . Emscripten .)

WASM ( Emscripten)

WASM WebAssembly.instantiate(). , importObject. / , WASM, WebAssembly.LinkError. .

WASM, hello.wasm, 62 . , ? , : Emscripten JS-! WASM, Emscripten, JS-, Emscripten!

?

, , ( ). , , ( ), ? , .

, Rust, C ++, , , . . , syscall. Syscalls . (, ), (, ).

AFAIK, WASM ( , ).

Emscripten

WASM :

  • WASM: WASM
  • : , ( )

Emscripten 1 , Emscripten. ?

! , , - wasm32-unknown-unknown Rust. LLVM WASM . WASM Emscripten. : JS- , , .

, hellorust.com. - .


ยน Emscripten WASM . asm.js, WASM.

+4

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


All Articles