We use the C language instead of pure WAST to create a square root function using WASM Fiddle (https://wasdk.github.io/WasmFiddle//). We show how to run the WebAssembly in WASM Fiddle, then download and run it in the browser using a helper function to load the WebAssembly.

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

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

// C

#include <math.h>

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

Compile to WASM:

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

index.html:

<!doctype>
<html>
<header>
<title>
WASM
</title>
<script> 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')
.then(m => {
window.getSqrt = m.getSqrt;
});
</script>
</header>
</html>

[WASM] Compile C Code into WebAssembly的更多相关文章

  1. Compile C++ code in Matlab with OpenCV support

    Provides a function named as "mex_opencv(src)" The code function mex_opencv(src) ARC = 'x6 ...

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

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

  3. [WASM] Write to WebAssembly Memory from JavaScript

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

  4. How to Build MySQL from Source Code on Windows & compile MySQL on win7+vs2010

    Not counting obtaining the source code, and once you have the prerequisites satisfied, [Windows] use ...

  5. ubuntu compile php from source code

    10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...

  6. [Asp.Net Core] Blazor WebAssembly - 工程向 - 如何在欢迎页面里, 预先加载wasm所需的文件

    前言, Blazor Assembly 需要最少 1.9M 的下载量.  ( Blazor WebAssembly 船新项目下载量测试 , 仅供参考. ) 随着程序越来越复杂, 引用的东西越来越多,  ...

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

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

  8. webassembly

    为什么需要 WebAssembly 自从 JavaScript 诞生起到现在已经变成最流行的编程语言,这背后正是 Web 的发展所推动的.Web 应用变得更多更复杂,但这也渐渐暴露出了 JavaScr ...

  9. C Socket Programming for Linux with a Server and Client Example Code

    Typically two processes communicate with each other on a single system through one of the following ...

随机推荐

  1. jq ---- 实现浏览器全屏

    // 点击进入全屏 方法. var fullscreen=function(){ elem=document.body; if(elem.webkitRequestFullScreen){ elem. ...

  2. [lougu1341]无序字母对

    Description: 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. Solution: 欧 ...

  3. python2 pip安装包等出现各种编码错误UnicodeDecodeError: 'ascii'(/或者utf-8) codec can't decode byte 0xd2...

    1.问题描述: python2环境,pip安装包时报错UnicodeDecodeError: 'ascii'(/或者utf-8) codec can't decode byte 0xd2... 类似如 ...

  4. Linux 关闭正在运行的程序---命令

    Ctrl + C 终止 Ctrl + D 退出 Ctrl + S 挂起 Ctrl + Q 解挂 Ctrl + Z 强制结束

  5. python 内存中写入文件(read读取不到文件解决)

    from io import StringIO a = StringIO.StringIO('title') a.write('content1\n') a.write('content2') a.s ...

  6. 【习题 8-10 UVA - 1614】Hell on the Markets

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 证明:前i个数一定能凑够1..sum[i]中的所有数字 i=1时显然成立. 现在假设i>=2时结论成立 即前i个数字能凑出1. ...

  7. 洛谷 P3385 【模板】负环

    P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M ...

  8. DataGridView控件绑定数据源

    前言: 近期听说DataGridView控件能直接绑定数据源.而不用穿越这层那层的忍辱负重.获取数据.真是高兴的屁颠屁颠的.后来一想二狗肯定不会弄.特意写了一个笨蛋版的教程--也算记录生活.欢度端午了 ...

  9. UVA 10306 e-Coins(全然背包: 二维限制条件)

    UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...

  10. 【iOS与EV3混合机器人编程系列之中的一个】iOS要干嘛?EV3能够更酷!

    乐高Mindstorm EV3智能机器人(下面简称EV3)自从在2013年的CES(Consumer Electronics Show美国消费电子展)上展出之后,就吸引了全球广大机器人爱好者的眼球!E ...