[WASM] Read WebAssembly Memory from JavaScript
2017-06-29 19:59
483 查看
We use an offset exporting function to get the address of a string in WebAssembly memory. We then create a typed array on top of the WebAssembly memory representing the raw string data, and decode that into a JavaScript string.
WASM Fiddle: https://wasdk.github.io/WasmFiddle/?6wzgh
Demo Repo: https://github.com/guybedford/wasm-intro
C code:
Here we created a pointer, point to the first chat of the string array.
When we compile the C code to the WASM:
Now inside JS, we can get the "memory":
And remember that it points to the first chat's address in memory.
So we can use Unit8Array to read buffer:
WASM Fiddle: https://wasdk.github.io/WasmFiddle/?6wzgh
Demo Repo: https://github.com/guybedford/wasm-intro
C code:
char str[] = "Hello World"; char* getStrOffset () { return &str[0]; }
Here we created a pointer, point to the first chat of the string array.
When we compile the C code to the WASM:
(module (table 0 anyfunc) (memory $0 1) (data (i32.const 16) "Hello World\00") (export "memory" (memory $0)) # export memory to JS (export "getStrOffset" (func $getStrOffset)) (func $getStrOffset (result i32) (i32.const 16) # getStrOffset function return the address in memory as i32.const 16 ) )
Now inside JS, we can get the "memory":
var wasmModule = new WebAssembly.Module(wasmCode); var wasmInstance = new WebAssembly.Instance(wasmModule, wasmImports); const memory = wasmInstance.exports.memory;
And remember that it points to the first chat's address in memory.
So we can use Unit8Array to read buffer:
const strBuf = new Uint8Array(memory.buffer, wasmInstance.exports.getStrOffset(), 11); // read biffer from the memory, getStrOffset return the first chat address, and since string (Hello World) is 11 lenght const str = new TextDecoder().decode(strBuf); log(str); // Hello World
相关文章推荐
- [WASM] Write to WebAssembly Memory from JavaScript
- [WASM] Call a JavaScript Function from WebAssembly
- Read physical memory information from registry
- [WASM] Compile C Code into WebAssembly
- .NET Web 执行错误Could not load type '...' from assembly '..., Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because the me
- CS001496 - Gather data from web page with JavaScript, WebKit, and Qt
- 【拿来主义】WebAssembly - JavaScript Just-in-time (JIT) 工作原理
- 如何利用Win32API取得另一支程式中的ListView內的所有值(RegisterHotKey,ReadProcessMemory,WindowFromPoint和VirtualAllocEx)
- Calling Web Services From Html Pages using Javascript
- /WEB-INF/views/login.jsp(2,61) Unable to read TLD "META-INF/c.tld" from JAR file "file:/D:/Program%
- [WASM] Create and Run a Native WebAssembly Function
- Some JavaScript Codes or Nodes Collected from Web
- Could not load type 'System.Web.UI.ScriptReferenceBase' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
- ORA-39050: parameter _log_private_parallelism is incompatible with parameter _redo_read_from_memory
- Unable to read TLD "META-INF/tlds/struts-bean.tld" from JAR file "file:/xxxWEB-INF/lib/struts.jar": org.apache.jasper.Ja
- Embed client-side JavaScript in an ASP.NET 1.1 web page from server side code
- webpack打包报javaScript heap out of memory的解决方法
- This assembly may have been downloaded from the Web. ......
- Read ConnectionString from web.config in ASP.NET 2.0