Using WASM Fiddle, we show how to write a simple number logger function that calls a consoleLog function defined in JavaScript. We then download and run the same function in a local project.

WASM Fiddle: https://wasdk.github.io/WasmFiddle/?cvrmt

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

Basiclly WASM is hard to debug, we still need Javascript to help, the way to do debugging is we pass Javascript Console.log function into WASM though "imports".
 
Defined a C code:
#include <math.h>

void consoleLog (float num);

float getSqrt (float num) {
consoleLog(num);
return sqrt(num);
}

Defined a function called "consoleLog".

After build to wasm:

(module
(type $FUNCSIG$vf (func (param f32)))
(type $FUNCSIG$ff (func (param f32) (result f32)))
(import "env" "consoleLog" (func $consoleLog (param f32)))
(table anyfunc)
(memory $ )
(export "memory" (memory $))
(export "getSqrt" (func $getSqrt))
(func $getSqrt (param $ f32) (result f32)
(call $consoleLog
(get_local $)
)
(f32.sqrt
(get_local $)
)
)
)

It's importing consoleLog from a module called environment.

This is just a default module namespace name for the externals of a C code compilation process.

Now on JS side, we need to pass the console.log function from "imports" param:

        function fetchAndInstantiateWasm(url, imports) {
return fetch(url)
.then((res) => {
if (res.ok) {
return res.arrayBuffer();
}
throw new Error('Unable to fetch WASM')
})
.then((bytes) => {
return WebAssembly.compile(bytes);
})
.then(module => {
return WebAssembly.instantiate(module, imports || {});
})
.then(instance => instance.exports);
} fetchAndInstantiateWasm('./program.wasm', {
env: {
consoleLog: (num) => console.log(num)
}
})
.then(m => {
window.getSqrt = m.getSqrt;
});

[WASM] Call a JavaScript Function from WebAssembly的更多相关文章

  1. [Rust] Pass a JavaScript Function to WebAssembly and Invoke it from Rust

    In some cases it’s useful to be able to invoke a JavaScript function inside Rust. This session showc ...

  2. href="javascript:function()" 和onclick的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  3. 关于<a href='javascript:function()'>

    <a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...

  4. javascript function对象

    <html> <body> <script type="text/javascript"> Function.prototype.get_my_ ...

  5. Understanding JavaScript Function Invocation and "this"

    Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...

  6. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

  7. JavaScript function函数种类介绍

    JavaScript function函数种类介绍 本篇主要介绍普通函数.匿名函数.闭包函数 1.普通函数介绍 1.1 示例 ? 1 2 3 function ShowName(name) {     ...

  8. 【转】onclick事件与href='javascript:function()'的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  9. javascript:function 函数声明和函数表达式 详解

    函数声明(缩写为FD)是这样一种函数: 有一个特定的名称 在源码中的位置:要么处于程序级(Program level),要么处于其它函数的主体(FunctionBody)中 在进入上下文阶段创建 影响 ...

随机推荐

  1. 40.Node.js Web 模块

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html 什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计 ...

  2. git 版本管理工具说明

    $ git init                 (初始化本地仓库,会生成.git 文件夹  .git 文件夹里存储了所有的版本信息.标记等内容) $ git add .              ...

  3. POJ 3039 搜索??? (逼近)

    思路: 抄的题解 这叫搜索? 难以理解 我觉得就是枚举+逼近 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  4. Cloudera Hue是什么?

     Hue是cdh专门的一套web管理器,它包括3个部分hue ui,hue server,hue db.hue提供所有的cdh组件的shell界面的接口.你可以在hue编写mr,查看修改hdfs的文件 ...

  5. JavaScript--数据结构与算法之图

    图和图的算法:图的定义:由边的集合及顶点的集合组成. 例如地图,每个城镇是顶点,道路是边,由顶点对来定义(城镇1,城镇2)简称(v1,v2)顶点也有权重——成本.基本概念: 有向图:图的顶点对是有序的 ...

  6. php课程 12-39 继承中parent的作用是什么

    php课程 12-39 继承中parent的作用是什么 一.总结 一句话总结:PHP5中使用parent::来引用父类的方法.parent:: 可用于调用父类中定义的成员方法. parent::的追溯 ...

  7. Java学习笔记二.1

    和其他高级语言类似,Java也具有以下部分 1.关键字:见下表,注意Java严格区分大小写,关键字都是小写 2.标识符:见下图 3.注释.有两种://(单行注释)和/**/(多行注释).还有一种文档注 ...

  8. jmeter响应数据中文乱码问题

    进入jmeter安装文件目录:D:\Program File\apache-jmeter-2.13\apache-jmeter-2.13\bin\ 修改jmeter.properties文件,在最下方 ...

  9. WebService 的Description 属性说明(转)

    转自:http://exception.thinksaas.cn/0/173/173623.html 在WebMethod的description 中可使用超文本, 举例: 如上图中,红框类的WebS ...

  10. [USACO08JAN]电话线Telephone Lines(分层图)/洛谷P1948

    这道题其实是分层图,但和裸的分层图不太一样.因为它只要求路径总权值为路径上最大一条路径的权值,但仔细考虑,这同时也满足一个贪心的性质,那就是当你每次用路径总权值小的方案来更新,那么可以保证新的路径权值 ...