DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

WebAssembly

创建 WebAssembly 源代码: 创建一个简单的 C 文件(例如 example.c),其中包含一个简单的函数,它将两个数字相加:

c
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

编译成 wasm 文件: emcc example.c -o example.wasm

或者编译输出 html 文件 ./emcc example.c -o index.html。打开html 文件目录 npx serve -p 3000

这将生成 example.wasm 文件

导出 ES 模块

 emcc -o hello.js hello.c -s MODULARIZE=1

没有直接支持的 es 模块,需要手动调整,加上

Javascript
var Module = (() => {
    var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;
    if (typeof __filename !== 'undefined') _scriptDir ||= __filename;
    return function (moduleArg = {}) {
        //
    };
})();
if (typeof exports === 'object' && typeof module === 'object') module.exports = Module;
else if (typeof define === 'function' && define['amd']) define([], () => Module);

没有直接支持的 es 模块,需要手动调整,加上

const exportModule = Module();
export default exportModule;

// for debug use 
globalThis.wasm = exportModule;
    async function echo(event) {
        await wasmModule.ready;
        wasmModule._echo();
    }

创建 HTML 文件: 创建一个 HTML 文件(例如 index.html),并在其中加载并运行 wasm 文件:

Assembler

Nasm https://www.nasm.us

Emscripten

https://emscripten.org/docs/introducing_emscripten/about_emscripten.html

bash
git clone https://github.com/emscripten-core/emsdk.git

# Fetch the latest version of the emsdk (not needed the first time you clone)
git pull

# Download and install the latest SDK tools.
./emsdk install latest

# Make the "latest" SDK "active" for the current user. (writes .emscripten file)
./emsdk activate latest

# Activate PATH and other environment variables in the current terminal
source ./emsdk_env.sh

Debug

https://developer.chrome.com/blog/wasm-debugging-2020/

编译优化

https://jeromewu.github.io/improving-performance-using-webassembly-simd-intrinsics/

Refer

  • https://blog.scottlogic.com/2022/06/20/state-of-wasm-2022.html
  • mdn WebAssembly
  • ruanyifeng asm.js 和 Emscripten 入门教程
Last Updated:
Contributors: rosendo
Prev
Note
Next
public api