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. 39.C语言操作数据库

    一.准备工作: sqlite3工具集:链接:https://pan.baidu.com/s/1mjufXZa 密码:2ui7 安装步骤: 打开如下文件夹,找到sqlite3.dll,并放入系统目录 2 ...

  2. BZOJ5332: [Sdoi2018]旧试题(莫比乌斯反演)

    时光匆匆,转眼间又是一年寒暑…… 这是小 Q 同学第二次参加省队选拔赛. 今年,小 Q 痛定思痛,不再冒险偷取试题,而是通过练习旧 试题提升个人实力.可是旧试题太多了,小 Q 没日没夜地做题,却看不到 ...

  3. [置顶] Docker学习总结(3)——Docker实战之入门以及Dockerfile(三)

    应用镜像 csphere/wordpress:4.2 # cd docker-training/wordpress/ # ls -a . license.txt wp-config-sample.ph ...

  4. 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法

    第三中类型.自己定义任何位置返回页面的方式,上边的类就是.m,大家能够贴过去使用.这个类是继承NavigationController的.用这个类初始化rootController就能够了.这里还有源 ...

  5. worktools-不同分辨率下图片移植

    1.下载需要移植的平台代码 1)查看手机需要的项目平台信息:adb shell getprop | gerp flavor     ----->mt6732_m561_p2_kangjia_cc ...

  6. android图片特效处理之光照效果

    这篇将讲到图片特效处理的光照效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理:图片上面的像素点按照给定圆心,按照圆半径的变化,像素点的RGB值分别加上相应的值作为当前点的RGB值. 例: ...

  7. codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)

    传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...

  8. selenium+python自动化处理时间控件

    尝试编写12306网站查询余票信息的自动化脚本时,碰到日期选择的问题,此处做一下记录:

  9. 三、Docker镜像的相关操作

    原文:三.Docker镜像的相关操作 一.查看本地镜像: docker images 二.使用某个镜像来运行容器: docker run -t -i xxxx(镜像名):xx.xx(版本,不带即最新) ...

  10. 怎样让索引仅仅能被一个SQL使用

    有个徒弟问我,要创建一个索引,去优化一个SQL,可是创建了索引之后其它 SQL 也要用 这个索引,其它SQL慢死了.要优化的SQL又快.遇到这样的问题咋搞? 一般遇到这样的问题还是非常少的.处理的方法 ...