WebAssembly学习(三):AssemblyScript - TypeScript到WebAssembly的编译

虽然说只要高级语言能转换成 LLVM IR,就能被编译成 WebAssembly 字节码,官方也推荐c/c++的方式,但是让一个前端工程师去熟练使用c/c++显然是有点困难,那么TypeScript 的方式便是前端编写 WebAssembly 最佳选择。

要将TypeScript 编译为WebAssembly,就要用到AssemblyScript编译器了。

AssemblyScript使用Binaryen(Emscripten的WebAssembly后端)将严格类型化的TypeScript(基本的带有类型的JavaScript)编译为WebAssembly,虽然它提供了几种新的特定于WebAssembly的类型和内置函数,但它本身并不是一种真正的语言,而是一种编译器变体。 它生成精简的WebAssembly模块,只需要一个npm安装。

1.安装

推荐使用cnpm安装,npm太慢了。

1 cnpm install --save-dev AssemblyScript/assemblyscript

执行过程如下,安装完成之后只有一个node_modules文件夹

1 E:\Code\assembly>cnpm install --save-dev AssemblyScript/assemblyscript
2 - [@AssemblyScript/assemblyscript] install  from git github:AssemblyScript/assemblyscript, may be very slow, please keep patience
3 √ Installed 1 packages
4 √ Linked 15 latest versions
5 √ Run 0 scripts
6 Recently updated (since 2019-02-17): 1 packages (detail see file E:\Code\assembly\node_modules\.recently_updates.txt)
7 √ All packages installed (14 packages installed from npm registry, 1 packages installed from git, used 1m(network 1m), speed 10.27kB/s, json 14(31.43kB), tarball 707.23kB)

2.创建项目

正确安装AssemblyScript后,它会提供一个名为asinit的小型实用工具来构建一个新项目,例如,在当前目录中:

1 npx asinit .

 就像使用express初始化项目一样,执行过程如下

 1 E:\Code\assembly>npx asinit .
 2 Version: 0.6.0
 3 
 4 This command will make sure that the following files exist in the project
 5 directory 'E:\Code\assembly':
 6 
 7   ./assembly
 8   Directory holding the AssemblyScript sources being compiled to WebAssembly.
 9 
10   ./assembly/tsconfig.json
11   TypeScript configuration inheriting recommended AssemblyScript settings.
12 
13   ./assembly/index.ts
14   Exemplary entry file being compiled to WebAssembly to get you started.
15 
16   ./build
17   Build artifact directory where compiled WebAssembly files are stored.
18 
19   ./build/.gitignore
20   Git configuration that excludes compiled binaries from source control.
21 
22   ./index.js
23   Main file loading the WebAssembly module and exporting its exports.
24 
25   ./package.json
26   Package info containing the necessary commands to compile to WebAssembly.
27 
28 The command will try to update exis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值