@bytecodealliance/jco-transpile
这个 @bytecodealliance/jco 子项目允许转录 Web大会组件 输入ES模块
可以在像NodeJS和浏览器这样的Javascript环境中运行.
@bytecodealliance/jco-transpile ,主要用于仅用于转接功能 jco 需要帮助,
财务报告和审定财务报表 jco 来源于这个图书馆的转录.
快速启动
用于 @bytecodealliance/jco-transpile 作为您自己的代码中的库,您可以使用一些导出函数.
看看我们 gitHub 上的例子。 。 。 .
传输
要传送现有的WebAssembly组件(磁盘路径, Buffer:
import { transpile, writeFiles } from '@bytecodealliance/jco-transpile';
async function example() {
// Transpile a given WebAssembly component into JS runnabe in NodeJS
const { files, imports, exports } = await transpile('path/to/component.wasm', { outDir: 'path/to/output/dir' });
// Write out the files that have been generated to disk
await writeFiles(files);
}
[注]
transpile选择很多选项 -- -- 咨询transpile.d.ts用于更详细的类型信息.
outDir控制生成文件路径的前缀, 所以指定它, 尽管没有文件 实际写入磁盘.
转换示例
如果您使用给定的 WIT 写入组件 :
package docs:[email protected];
interface add {
add: func(x: u32, y: u32) -> u32;
}
world adder {
export add;
}
在该组件转接后,您应该看到输出如下:
dist
└── transpiled
├── add.core.wasm
├── add.d.ts
├── adder.core.wasm
├── adder.d.ts
├── adder.js
├── add.js
├── add.mjs
└── interfaces
└── docs-adder-add.d.ts
3 directories, 8 files
dist/transpiled/adder.js 是可以从“ host” 代码中使用的切入点 :
import { add } from './dist/transpiled/adder.js';
console.log('1 + 2 = ' + add.add(1, 2));
你可以自己试试这个例子 以 GitHub 为单位。 。 。 .
生成来宾( 组件) 类型
写入组件时,您可以生成与您相应的打字声明 Web Assembly 接口类型 产生客体的进出口:
import { generateGuestTypes, writeFiles } from '@bytecodealliance/jco-transpile';
async function example() {
const files = await generateGuestTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });
// NOTE: Files is a serialization of the files produced of the following type:
// type FileBytes = { [filepath: string]: Uint8Array; };
//
// You can use the code below to write out all files to a given directory
await Promise.all(
Object.entries(files).map(async ([filePath, bytes]) => {
await mkdir(dirname(filePath), { recursive: true });
await writeFile(filePath, bytes);
})
);
}
来宾类型是JS组件使用的WIT接口的执行 -- -- 即JS代码
变成一个组件 jco componentize编号 :componentize-js进行有益的工作.
对于WIT世界中指定的导入,该类型生成将启用 电话编号 :使用 界面.
来宾类型生成实例
例如,考虑到以下WIT接口:
package docs:[email protected];
interface add {
add: func(x: u32, y: u32) -> u32;
}
world adder {
export add;
}
制作的打字声明如下:
declare module 'docs:adder/[email protected]' {
export function add(x: number, y: number): number;
}
生成主机类型
在构建运行转录组件的平台时,可以生成 Typescript 与《公约》相对应的声明 Web Assembly 接口类型 (构成部分需要) 生成主机类型 :
import { generateHostTypes, writeFiles } from '@bytecodealliance/jco-transpile';
async function example() {
// Generate types for a host binding (i.e. supplying imports to a transpiled component)
const files = await generateHostTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });
// Write out the files that have been generated to disk
await writeFiles(files);
}
主机类型是WIT接口的实现,用于 翻译后 JS代码, 即"主机"(NodeJS + V8 或浏览器)上,以制作平台 进口 工作时.
对于WIT世界中指定的导入,该类型生成将启用 执行 进口数量 功能界面.
主机类型生成示例
例如,考虑到以下WIT接口:
package docs:[email protected];
interface add {
add: func(x: u32, y: u32) -> u32;
}
world adder {
export add;
}
制作的打字声明如下:
/** @module Interface docs:adder/[email protected] **/
export function add(x: number, y: number): number;
许可证
这个项目根据Apache 2.0许可证获得许可,LLVM例外. 见 自由主义 更多细节.
捐款
除非您另有明确声明,否则有意提交的任何捐款 根据Apache-2.0许可证的定义, 应照此办理,不附加任何条件.