[WASM] Write to WebAssembly Memory from JavaScript

Source: Internet
Author: User

We Write a function that converts a string to lowercase in WebAssembly, demonstrating how to set the input string from Jav Ascript.

WASM Fiddle:https://wasdk.github.io/wasmfiddle/?h1s69

Demo Repo:https://github.com/guybedford/wasm-intro

We want to create a funcrtion ' toLowerCase ', which enable JS-to-write in Memory.

To write the data into WASM, we need to variables in C code, one was ' inStr ' which get original input (for example ' Hello world '), another is ' outstr ' which would transform to lower case string (for example: ' Hello World ').

C Code:

voidConsoleLog (Char* Offset,intlen);Charinstr[ -];Charoutstr[ -];Char*Getinstroffset () {return&instr[0];}voidtoLowerCase () { for(inti =0; I < -; i++ ) {    Charc =Instr[i]; if(C > -&& C < the) {C= C + +; } Outstr[i]=C; } consolelog (&outstr[0], -);}

Js:some code to get wasm instance.

var New Webassembly.module (wasmcode); var New webassembly.instance (wasmmodule, {  env: {    consolelog (offset, len) {      new  Uint8array (Mem.buffer, offset, len);      Log (new  Textdecoder (). Decode (STRBUF));    }   = WasmInstance.exports.memory;

Now we need to write data from JS to WASM memory, the "the" to does it is just to put data into ' inStr ':

Const MEM = wasmInstance.exports.memory; function writestring (str, offset) {  new  Textencoder (). Encode (str);   New Uint8array (Mem.buffer, offset, strbuf.length);          for (Let i = 0; i < strbuf.length; i++) {        = strbuf[i];}}  } WriteString (' Hello Web Assembly ', WasmInstance.exports.getInStrOffset ());

Because What ' wasmInstance.exports.getInStrOffset () ' Return us was the first char address of ' inStr ', then we use for loop To write data into ' inStr '.

After writting the data and then we can call ' toLowerCase ' to see the result:

varWasmmodule =NewWebassembly.module (wasmcode);varWasminstance =Newwebassembly.instance (Wasmmodule, {env: {ConsoleLog (offset, len) {const STRBUF=NewUint8array (Mem.buffer, offset, Len); Log (NewTextdecoder (). Decode (STRBUF)); }}); const MEM=wasmInstance.exports.memory;functionwritestring (str, offset) {const STRBUF=NewTextencoder (). Encode (str); Const OUTBUF=NewUint8array (Mem.buffer, offset, strbuf.length);  for(Let i = 0; i < strbuf.length; i++) {Outbuf[i]=Strbuf[i]; }}}writestring (' Hello Web Assembly ', WasmInstance.exports.getInStrOffset ()); WasmInstance.exports.toLowerCase ();

[WASM] Write to WebAssembly Memory from JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.