[WASM] Compile C Code into WebAssembly
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的更多相关文章
- Compile C++ code in Matlab with OpenCV support
Provides a function named as "mex_opencv(src)" The code function mex_opencv(src) ARC = 'x6 ...
- WebAssembly完全入门——了解wasm的前世今身
前言 接触WebAssembly之后,在google上看了很多资料.感觉对WebAssembly的使用.介绍.意义都说的比较模糊和笼统.感觉看了之后收获没有达到预期,要么是文章中的例子自己去实操不能成 ...
- [WASM] Write to WebAssembly Memory from JavaScript
We write a function that converts a string to lowercase in WebAssembly, demonstrating how to set the ...
- 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 ...
- ubuntu compile php from source code
10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...
- [Asp.Net Core] Blazor WebAssembly - 工程向 - 如何在欢迎页面里, 预先加载wasm所需的文件
前言, Blazor Assembly 需要最少 1.9M 的下载量. ( Blazor WebAssembly 船新项目下载量测试 , 仅供参考. ) 随着程序越来越复杂, 引用的东西越来越多, ...
- WebAssembly让你的Javascript计算性能提升70%
现在的JavaScript代码要进行性能优化,通常使用一些常规手段,如:延迟执行.预处理.setTimeout等异步方式避免处理主线程,高大上一点的会使用WebWorker.即使对于WebWorker ...
- webassembly
为什么需要 WebAssembly 自从 JavaScript 诞生起到现在已经变成最流行的编程语言,这背后正是 Web 的发展所推动的.Web 应用变得更多更复杂,但这也渐渐暴露出了 JavaScr ...
- 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 ...
随机推荐
- 认识Linux瘦客户机
(本文完整版见http://os.51cto.com/art/201001/181448.htm) 随着Linux的发展,以及网络计算技术的发展和逐步深入的云计算,基于Li ...
- 给iOS项目中添加图片,并通过UIImageView引用和显示该UIImage图片
[问题] 关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件 过程中,问题转换为,需要给当前iOS项目中,添加一个图片. 类似于Windows开发中的资源文件,其中图片文件属于资源的 ...
- WPF框架ZFS
前文 项目开源地址(非正式版,开发版本), 码云Gitee地址: https://gitee.com/zhgg666/publicWpf XAML XAML能帮助团队真正实现UI与逻辑的剥离.XAM ...
- JXL.jar简单封装Excel读写操作
1.分析 一个excel文件能够有多页,每页excel中能够有多行,每行中能够有多列.用面向对象的思想能够把一行中的某列看作是一个String对象,一行看作是一个包括多个列的对象.一页是包括多行的对面 ...
- 记号(notation)的学习
数学的记号(notation) 记号具体代表什么含义,取决于你的定义: 比如这样的 d⃗ 一个向量,每个分量 d(i) 表示的是从初始结点 v 到当前节点 vi 的最短路径:也即这样的一个向量的每一 ...
- 28.Node.js 函数和匿名函数
转自:http://www.runoob.com/nodejs/nodejs-module-system.html 在JavaScript中,一个函数可以作为另一个函数的参数.我们可以先定义一个函数, ...
- dropdown下拉菜单
<!--声明方式的下拉菜单:三个要点--> <!--1 外围容器用dropdown包裹--> <!--2 内部点击事件data-toggle--> <!--3 ...
- vue的mode: 'history'模式
const router = new VueRouter({ mode: 'history', routes: [...] }) 不用mode: 'history'的时候,页面url地址后面会加上一个 ...
- Zabbix 监控搭建
Zabbix官网地址:https://www.zabbix.com/download 1.服务端 1.操作前安装好Mysql数据库 配置yum源,安装部署Zabbix rpm -i http://re ...
- URL短地址压缩算法 微博短地址原理解析 (Java实现)
原博客地址:http://blog.csdn.net/xyz_lmn/article/details/8057270 最近,项目中需要用到短网址(ShortUrl)的算法,于是在网上搜索一番,发现有C ...