[WASM] Write to WebAssembly Memory from JavaScript
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的更多相关文章
- [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 ...
- WebAssembly是解决JavaScript 痼疾的银弹?
写在前面 <没有银弹>是 Fred Brooks 在 1987 年所发表的一篇关于软件工程的经典论文.该论文的主要论点是,没有任何一项技术或方法可以能让软件工程的生产力在十年内提高十倍. ...
- (翻译) How variables are allocated memory in Javascript? | scope chain | lexicial scope
总结: 阅读下面文章需要15分钟 提问者的问题是JavaScript中内存是怎么分配的,在介绍的过程作者涉及计到了JS中 Scope Chain和调用函数call生成lexicial environm ...
- [WASM] Access WebAssembly Memory Directly from JavaScript
While JavaScript has a garbage-collected heap, WebAssembly has a linear memory space. Nevertheless u ...
- ASP.NET Core Blazor WebAssembly 之 .NET JavaScript互调
Blazor WebAssembly可以在浏览器上跑C#代码,但是很多时候显然还是需要跟JavaScript打交道.比如操作dom,当然跟angular.vue一样不提倡直接操作dom:比如浏览器的后 ...
- WebAssembly让你的Javascript计算性能提升70%
现在的JavaScript代码要进行性能优化,通常使用一些常规手段,如:延迟执行.预处理.setTimeout等异步方式避免处理主线程,高大上一点的会使用WebWorker.即使对于WebWorker ...
- WebAssembly完全入门——了解wasm的前世今身
前言 接触WebAssembly之后,在google上看了很多资料.感觉对WebAssembly的使用.介绍.意义都说的比较模糊和笼统.感觉看了之后收获没有达到预期,要么是文章中的例子自己去实操不能成 ...
- [WASM] Set up wasm-bindgen for easy Rust/JavaScript Interoperability
Interoperability between JavaScript and Rust is limited to numerics and accessing memory directly. S ...
- JavaScript与WebAssembly进行比较
本文由云+社区发表 作者:QQ音乐前端团队 在识别和描述核心元素的过程中,我们分享了构建SessionStack时使用的一些经验法则,这是一个轻量级但健壮且高性能的JavaScript应用程序,以帮助 ...
随机推荐
- 5.brackets 快捷键 有大用
转自:https://blog.csdn.net/u012011360/article/details/41209223 ctrl+b 当选中一个文本时,会出现相同的文本,被高亮显示 按ctrl+b ...
- POJ 1991 DP
题意: 思路: 考虑DP 先把事件按照地点顺序排个序 f[i][j][0]表示从i到j还没有去过 现在在i f[i][j][1]表示从i到j还没有去过 现在在j 那么方程就呼之欲出了 f[i][j][ ...
- RGB 颜色空间转 HSI 颜色空间的matlab程序实现
RGB 颜色空间转 HSI 颜色空间的matlab程序实现 2014.10.20之前的内容有误,这里依据wikipedia更新了算法内容. 算法以wiki为准 https://en.wikipedia ...
- Winform 获取相对路径 C#
///获取相对路径 ///例如:System.Windows.Forms.Application.StartupPath = "E:\App\CheckingMachine\QueryMac ...
- 【Codeforces Round #453 (Div. 2) C】 Hashing Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有当a[i]和a[i-1]都大于1的时候才会有不同的情况. a[i] >= a[i-1] 且a[i-1]>=2 则 ...
- Android学习笔记之Bitmap位图的旋转
位图的旋转也可以借助Matrix或者Canvas来实现. 通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最后用drawBitmap ...
- bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式)
bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式) 一.总结 一句话总结:通过监听滚动的高,判断滚动的高是否大于元素距离顶端的距离 1.如何知道屏幕滚动的高? st=$ ...
- 4.Windows下安装ZooKeeper
转自:https://www.cnblogs.com/mstmdev/p/5612791.html 官方主页: https://zookeeper.apache.org/ 选择合适的镜像地址下 ...
- 100.dll调用
在dll中声明 _declspec(dllexport) ; _declspec(dllexport)void go() { MessageBoxA(, ); } 调用dll HINSTANCE hl ...
- 1.1 Introduction中 Producers官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Producers 生产者(Producers) Producers publish ...