0.0.4 • Published 3 years ago

lavats-wasm v0.0.4

Weekly downloads
7
License
ISC
Repository
github
Last release
3 years ago

lavats-wasm

lavats是一个由typescript写成的支持umdDSL式的WebAssembly汇编器。

特色

  • 支持typescript*.d.ts文件
  • 支持umd,可运行在浏览器nodejs环境下
  • 使用DSL式,可与js语言无缝衔接

支持

规范提案

支持的提案:

安装

在命令行中输入:

npm install lavats-wasm

引入

cmd

var lavatsWasm = require("lavats-wasm");

amd

require(["lavats-wasm"], function(lavatsWasm) {

})
define(["lavats-wasm"], function(lavatsWasm) {

})

es6

import * as lavatsWasm from "lavats-wasm";

\

<script src="./node_modules/lavats/dist/index.js"></script>
<script>

lavatsWasm

</script>

使用

生成模块

import { Module, Func, ImportExportType, data } from "lavats-wasm";

let m = new Module({
    name: "module",
    memory: [
        { name: "memory", min: 0 }
    ],
    data: [
        { name: "data", offset: 0, memoryIndex: 0, init: data.i64(10).string("test").i64(10).toBuffer() }
    ],
    table: [
        { name: "table", elementType: ElementType.funcref, min: 0 }
    ],
    element: [
        { name: "element", tableIndex: 0, offset: 0, functionIndexes: [0] }
    ],
    import: [
        { name: "test", module: "js", importName: "test", type: ImportExportType.Memory, min: 0 },
        { name: "imFunc", module: "js", importName: "imFunc", type: ImportExportType.Function }
    ],
    export: [
        { exportName: "js", type: ImportExportType.Memory, index: "test" }
    ],
    type: [
        { name: "ifBlock", params: [Type.I32], results: [] }
    ],
    global: [
        { name: "global", valueType: Type.I32, init: 0 }
    ],
    start: "start",
    function: [
        new Func({ name: "start" }),
        new Func({
            name: "func",
            params: [{ type: Type.I32, name: "p1" }],
            locals: [{ type: Type.I32, name: "l1" }],
            codes: [
                Code.block({
                    label: "block",
                    type: { results: [Type.I32] },
                    codes: [
                        Code.block({
                            label: "block2",
                            codes: [
                                Code.call("imFunc"),
                            ]
                        }),
                        Code.i32.Const(50)
                    ]
                }),
                Code.i32.Const(10),
                Code.If({
                    label: "if",
                    type: { params: [Type.I32] },
                    then: [
                        Code.drop,
                        Code.br(0)
                    ],
                    else: [
                        Code.drop
                    ]
                }),
                Code.i32.Const(10),
                Code.If({
                    else: [
                        Code.nop
                    ]
                })
            ]
        })
    ],
    custom: [
        { name: "hello", buffer: data.string("hello").toBuffer() }
    ]
});

Module转换为二进制

let buf = m.toBuffer();    // Uint8Array(297) [0, 97, 115, 109, 1, 0, 0, 0, 1, 8, 2, 96, 1, 127, 0, ....

二进制转换为Module

let newModule = Module.fromBuffer(buf);

Module转换为字符串

let wat = m.toString();

// (module $module
//     (type $ifBlock (func (param i32)))
//     (import "js" "test" (memory $test 0))
//     (import "js" "imFunc" (func $imFunc))
//     (global $global i32 (i32.const 0))
//     (memory $memory 0)
//     (data $data (i32.const 0) "\0a\00\00\00\00\00\00\00test\0a\00\00\00\00\00\00\00")
//     (table $table 0 anyfunc)
//     (elem $element (i32.const 0) 0)
//     (func $start
//     )
//     (func $func (param $p1 i32) (local $l1 i32)
//         block $block (result i32)
//             i32.const 50
//         end
//         i32.const 10
//         if $if (param i32)
//             drop
//             br 0
//         else
//             drop
//         end
//     )
//     (start $start)
//     (export "js" (memory $test))
// )

字符串转换为Module

请参考lavats-wat

模块校验

// 会返回模块是否合法的boolean
let isValidate = m.validate();

// 当校验失败时,会抛出异常
m.check();
0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago