broccoli-rust2wasm
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -7,2 +7,3 @@ /// <reference types="node" /> | ||
generateAsyncWrapper?: boolean; | ||
generateTypescript?: boolean; | ||
} | ||
@@ -14,2 +15,3 @@ export default class RustPlugin extends Plugin { | ||
private generateAsyncWrapper; | ||
private generateTypescript; | ||
constructor(input: any, options?: RustPluginOptions); | ||
@@ -23,4 +25,6 @@ build(): void; | ||
protected wrapper(buffer: Buffer): string; | ||
protected wasm_gc(wasm: Buffer): Buffer; | ||
protected wasm_opt(wasm: Buffer): Buffer; | ||
protected wasmGc(wasm: Buffer): Buffer; | ||
protected wasmOpt(wasm: Buffer): Buffer; | ||
protected typescript(wasm: Buffer): string; | ||
protected wasmTypeToTypescript(ty: string): string; | ||
} | ||
@@ -27,0 +31,0 @@ export interface CompileResult { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const child_process_1 = require("child_process"); | ||
const describe_wasm_1 = require("describe-wasm"); | ||
const fs = require("fs"); | ||
@@ -15,14 +16,19 @@ const path = require("path"); | ||
this.generateAsyncWrapper = options !== undefined && options.generateAsyncWrapper === true; | ||
this.generateTypescript = options !== undefined && options.generateTypescript === true; | ||
} | ||
build() { | ||
const { name, wasm } = this.compile(); | ||
let wasm_gc = this.wasm_gc(wasm); | ||
let wasm_gc_opt = this.debug ? wasm_gc : this.wasm_opt(wasm_gc); | ||
const wasmGc = this.wasmGc(wasm); | ||
const wasmGcOpt = this.debug ? wasmGc : this.wasmOpt(wasmGc); | ||
if (this.generateWrapper || this.generateAsyncWrapper) { | ||
const outputFile = path.join(this.outputPath, `${name}.js`); | ||
fs.writeFileSync(outputFile, this.wrapper(wasm_gc_opt)); | ||
fs.writeFileSync(outputFile, this.wrapper(wasmGcOpt)); | ||
if (this.generateTypescript) { | ||
const typescriptFile = path.join(this.outputPath, `${name}.d.ts`); | ||
fs.writeFileSync(typescriptFile, this.typescript(wasmGcOpt)); | ||
} | ||
} | ||
else { | ||
const outputFile = path.join(this.outputPath, `${name}.wasm`); | ||
fs.writeFileSync(outputFile, wasm_gc_opt); | ||
fs.writeFileSync(outputFile, wasmGcOpt); | ||
} | ||
@@ -92,4 +98,4 @@ } | ||
// tslint:disable-next-line:max-line-length | ||
let toBuffer = `const toBuffer = typeof Buffer === 'undefined' ? (str) => Uint8Array.from(atob(str), c => c.charCodeAt(0)) : (str) => Buffer.from(str, 'base64');`; | ||
let deserialized = `toBuffer("${buffer.toString("base64")}")`; | ||
const toBuffer = `const toBuffer = typeof Buffer === 'undefined' ? (str) => Uint8Array.from(atob(str), c => c.charCodeAt(0)) : (str) => Buffer.from(str, 'base64');`; | ||
const deserialized = `toBuffer("${buffer.toString("base64")}")`; | ||
if (this.generateAsyncWrapper) { | ||
@@ -107,3 +113,3 @@ return `${toBuffer} | ||
} | ||
wasm_gc(wasm) { | ||
wasmGc(wasm) { | ||
const temp1 = path.join(this.cachePath, `gc-input.wasm`); | ||
@@ -119,3 +125,3 @@ const temp2 = path.join(this.cachePath, `gc-output.wasm`); | ||
// can't be found and instead just skip this step. | ||
wasm_opt(wasm) { | ||
wasmOpt(wasm) { | ||
const temp1 = path.join(this.cachePath, `opt-input.wasm`); | ||
@@ -128,4 +134,5 @@ const temp2 = path.join(this.cachePath, `opt-output.wasm`); | ||
catch (err) { | ||
if (err.code == 'ENOENT') | ||
if (err.code === "ENOENT") { | ||
return wasm; | ||
} | ||
throw err; | ||
@@ -135,4 +142,72 @@ } | ||
} | ||
typescript(wasm) { | ||
const parsed = describe_wasm_1.default(wasm); | ||
let imports = `export interface FunctionImports {\n`; | ||
let importedFunctions = 0; | ||
for (const imp of parsed.imports) { | ||
if (imp.kind !== "Function") { | ||
continue; | ||
} | ||
importedFunctions += 1; | ||
if (imp.module !== "env") { | ||
continue; | ||
} | ||
const signature = parsed.signatures[imp.signature]; | ||
imports += ` ${imp.name}(`; | ||
for (let j = 0; j < signature.params.length; j++) { | ||
if (j > 0) { | ||
imports += `, `; | ||
} | ||
imports += `arg${j}: ${this.wasmTypeToTypescript(signature.params[j])}`; | ||
} | ||
imports += `): ${this.wasmTypeToTypescript(signature.return)};\n`; | ||
} | ||
imports += `}\n`; | ||
imports += ` | ||
export interface Imports { | ||
env: FunctionImports; | ||
}`; | ||
let exports = `export interface Exports {\n`; | ||
for (const exp of parsed.exports) { | ||
if (exp.kind === "Memory") { | ||
exports += ` ${exp.name}: WebAssembly.Memory;\n`; | ||
continue; | ||
} | ||
if (exp.kind !== "Function") { | ||
continue; | ||
} | ||
const func = parsed.functions[exp.index - importedFunctions]; | ||
const signature = parsed.signatures[func]; | ||
exports += ` ${exp.name}(`; | ||
for (let j = 0; j < signature.params.length; j++) { | ||
if (j > 0) { | ||
exports += `, `; | ||
} | ||
exports += `arg${j}: ${this.wasmTypeToTypescript(signature.params[j])}`; | ||
} | ||
exports += `): ${this.wasmTypeToTypescript(signature.return)};\n`; | ||
} | ||
exports += `}`; | ||
const ret = this.generateAsyncWrapper ? `Promise<Exports>` : `Exports`; | ||
return ` | ||
${imports} | ||
${exports} | ||
declare const Mod: (imports: Imports) => ${ret}; | ||
export default Mod; | ||
`; | ||
} | ||
wasmTypeToTypescript(ty) { | ||
if (ty === "i32" || ty === "i64" || ty === "f32" || ty === "f64") { | ||
return "number"; | ||
} | ||
if (ty === "void") { | ||
return "void"; | ||
} | ||
throw new Error(`unknown wasm type: ${ty}`); | ||
} | ||
} | ||
exports.default = RustPlugin; | ||
//# sourceMappingURL=plugin.js.map |
{ | ||
"name": "broccoli-rust2wasm", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "broccoli plugin to build rust to wasm", | ||
@@ -32,4 +32,5 @@ "main": "dist/index.js", | ||
"broccoli-plugin": "^1.3.0", | ||
"describe-wasm": "^0.1.1", | ||
"toml": "^2.3.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17611
252
3
+ Addeddescribe-wasm@^0.1.1
+ Addeddescribe-wasm@0.1.5(transitive)