博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[WASM] Create and Run a Native WebAssembly Function
阅读量:6074 次
发布时间:2019-06-20

本文共 1493 字,大约阅读时间需要 4 分钟。

In this introduction, we show a simple WebAssembly function that returns the square root of a number. To create this function we load up WebAssembly Explorer (), writing the native WAST code to create and export the function. We compile and download the resulting WebAssembly binary, loading this with the Fetch API and WebAssembly JavaScript API to call the function in the browser.

Demo Repo: 

 

 Every WebAssembly starts with Module:
(module)

 

Define a function inside the module:

(module (func $sqrt )   )

Now we defined a empty function, 

To add input and output we can do:

(module  (func $sqrt     (param $num f32) # Input: param is the keyword, $num is the param name, f32 is the type     (result f32) # Output: result is the keyword, f32 is the type   ))

 

Now we can define function body:

(module  (func $sqrt     (param $num f32)    (result f32)        (f32.sqrt (get_local $num))  # call the function sqrt on f32, pass in the params $num though get_local function  ))

The calculation value will be the return value.

 

If we want to use the fucntion in Javascript, we need to export the function:

(module  (export "sqrt" (func $sqrt)) # export the function call "sqrt" refer to $sqrt function we defined below  (func $sqrt     (param $num f32)    (result f32)        (f32.sqrt (get_local $num))  ))

 

After "Assemble" it and "Download" the file, we can load in the project:

    
WASM

 

Open the console,  we can type:

wasmSqrt(25) //5

 

转载地址:http://moxgx.baihongyu.com/

你可能感兴趣的文章
CodeForces 580B Kefa and Company
查看>>
开发规范浅谈
查看>>
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
查看>>
鼠标增强软件StrokeIt使用方法
查看>>
本地连接linux虚拟机的方法
查看>>
某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
查看>>
BABOK - 企业分析(Enterprise Analysis)概要
查看>>
Linux 配置vnc,开启linux远程桌面
查看>>
NLog文章系列——如何优化日志性能
查看>>
Hadoop安装测试简单记录
查看>>
CentOS6.4关闭触控板
查看>>
ThreadPoolExecutor线程池运行机制分析-线程复用原理
查看>>
React Native 极光推送填坑(ios)
查看>>
Terratest:一个用于自动化基础设施测试的开源Go库
查看>>
修改Windows远程终端默认端口,让服务器更安全
查看>>
扩展器必须,SAS 2.0未必(SAS挺进中端存储系统之三)
查看>>
Eclipse遇到Initializing Java Tooling解决办法
查看>>
while((ch = getchar()) != '\n')
查看>>
好程序员web前端分享JS检查浏览器类型和版本
查看>>
Oracle DG 逻辑Standby数据同步性能优化
查看>>