We write a function that converts a string to lowercase in WebAssembly, demonstrating how to set the input string from JavaScript.

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 data into WASM, we need to variables in C code, one is 'inStr' which get original input (for example 'Hello World'), another is 'outStr' which will transform to lower case string (for example: 'hello world').

C code:

void consoleLog (char* offset, int len);

char inStr[];
char outStr[]; char* getInStrOffset () {
return &inStr[];
} void toLowerCase() {
for (int i = ; i < ; i++ ) {
char c = inStr[i];
if (c > && c < ) {
c = c + ;
}
outStr[i] = c;
}
consoleLog(&outStr[], );
}

JS: Some code to get wasm instance.

var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(wasmModule, {
env: {
consoleLog (offset, len) {
const strBuf = new Uint8Array(mem.buffer, offset, len);
log(new TextDecoder().decode(strBuf));
}
}
});
const mem = wasmInstance.exports.memory;

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

const mem = wasmInstance.exports.memory;

function writeString (str, offset) {
const strBuf = new TextEncoder().encode(str);
const outBuf = new Uint8Array(mem.buffer, offset, strBuf.length); for (let i = 0; i < strBuf.length; i++) {
outBuf[i] = strBuf[i];
}
}
} writeString('Hello Web Assembly', wasmInstance.exports.getInStrOffset());

Because what 'wasmInstance.exports.getInStrOffset()' return us is the first char address of 'inStr', then we use for loop to write data into 'inStr'.

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

var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(wasmModule, {
env: {
consoleLog (offset, len) {
const strBuf = new Uint8Array(mem.buffer, offset, len);
log(new TextDecoder().decode(strBuf));
}
}
});
const mem = wasmInstance.exports.memory; function writeString (str, offset) {
const strBuf = new TextEncoder().encode(str);
const outBuf = new Uint8Array(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的更多相关文章

  1. [WASM] Read WebAssembly Memory from JavaScript

    We use an offset exporting function to get the address of a string in WebAssembly memory. We then cr ...

  2. WebAssembly是解决JavaScript 痼疾的银弹?

    写在前面 <没有银弹>是 Fred Brooks 在 1987 年所发表的一篇关于软件工程的经典论文.该论文的主要论点是,没有任何一项技术或方法可以能让软件工程的生产力在十年内提高十倍. ...

  3. (翻译) How variables are allocated memory in Javascript? | scope chain | lexicial scope

    总结: 阅读下面文章需要15分钟 提问者的问题是JavaScript中内存是怎么分配的,在介绍的过程作者涉及计到了JS中 Scope Chain和调用函数call生成lexicial environm ...

  4. [WASM] Access WebAssembly Memory Directly from JavaScript

    While JavaScript has a garbage-collected heap, WebAssembly has a linear memory space. Nevertheless u ...

  5. ASP.NET Core Blazor WebAssembly 之 .NET JavaScript互调

    Blazor WebAssembly可以在浏览器上跑C#代码,但是很多时候显然还是需要跟JavaScript打交道.比如操作dom,当然跟angular.vue一样不提倡直接操作dom:比如浏览器的后 ...

  6. WebAssembly让你的Javascript计算性能提升70%

    现在的JavaScript代码要进行性能优化,通常使用一些常规手段,如:延迟执行.预处理.setTimeout等异步方式避免处理主线程,高大上一点的会使用WebWorker.即使对于WebWorker ...

  7. WebAssembly完全入门——了解wasm的前世今身

    前言 接触WebAssembly之后,在google上看了很多资料.感觉对WebAssembly的使用.介绍.意义都说的比较模糊和笼统.感觉看了之后收获没有达到预期,要么是文章中的例子自己去实操不能成 ...

  8. [WASM] Set up wasm-bindgen for easy Rust/JavaScript Interoperability

    Interoperability between JavaScript and Rust is limited to numerics and accessing memory directly. S ...

  9. JavaScript与WebAssembly进行比较

    本文由云+社区发表 作者:QQ音乐前端团队 在识别和描述核心元素的过程中,我们分享了构建SessionStack时使用的一些经验法则,这是一个轻量级但健壮且高性能的JavaScript应用程序,以帮助 ...

随机推荐

  1. 洛谷P2251 质量检测

    题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... Am} ...

  2. 关于cook操作

    http://www.cnblogs.com/fishtreeyu/archive/2011/10/06/2200280.html

  3. POJ 1279 Art Gallery 半平面交/多边形求核

    http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...

  4. canvas:画布

    画布有默认宽度,如果要自己设置宽带要写在属性上 列: <canvas id = "myCanvas" width = "600" height = &qu ...

  5. flask使用第三方云通讯平台时,出现{'172001':'网络错误'}解决方法

    问题描述:flask使用第三方云通讯平台时,出现{'172001':'网络错误'} 解决方法: 在sms.py文件中添加如下代码 import ssl # 取消证书验证ssl._create_defa ...

  6. 抓包神器Fiddler之Https请求随心转

    随着AppleStore对APP的审核越来越严格,客户端请求服务端API的方式大多数都变更为了https,在更安全的同时又引起了另外一个问题——本地抓包开发调试的不便. 一般来说,我们在开发API的时 ...

  7. Hyperic

    https://my.oschina.net/hyperichq/blog/525590

  8. HDU 3072--Intelligence System【SCC缩点新构图 &amp;&amp; 求连通全部SCC的最小费用】

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. 1.20 Python基础知识 - python常用模块-1

    一.time和datetime 1.time模块 1)time.process_time() >>> import time >>> time.process_ti ...

  10. golang sync.WaitGroup

    //阻塞,直到WaitGroup中的所以过程完成. import ( "fmt" "sync" ) func wgProcess(wg *sync.WaitGr ...