broccoli-rust2wasm
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -6,2 +6,3 @@ /// <reference types="node" /> | ||
generateWrapper?: boolean; | ||
generateAsyncWrapper?: boolean; | ||
} | ||
@@ -12,2 +13,3 @@ export default class RustPlugin extends Plugin { | ||
private generateWrapper; | ||
private generateAsyncWrapper; | ||
constructor(input: any, options?: RustPluginOptions); | ||
@@ -21,2 +23,4 @@ build(): void; | ||
protected wrapper(buffer: Buffer): string; | ||
protected wasm_gc(wasm: Buffer): Buffer; | ||
protected wasm_opt(wasm: Buffer): Buffer; | ||
} | ||
@@ -23,0 +27,0 @@ export interface CompileResult { |
@@ -14,12 +14,15 @@ "use strict"; | ||
this.generateWrapper = options !== undefined && options.generateWrapper === true; | ||
this.generateAsyncWrapper = options !== undefined && options.generateAsyncWrapper === true; | ||
} | ||
build() { | ||
const { name, wasm } = this.compile(); | ||
if (this.generateWrapper) { | ||
let wasm_gc = this.wasm_gc(wasm); | ||
let wasm_gc_opt = this.debug ? wasm_gc : this.wasm_opt(wasm_gc); | ||
if (this.generateWrapper || this.generateAsyncWrapper) { | ||
const outputFile = path.join(this.outputPath, `${name}.js`); | ||
fs.writeFileSync(outputFile, this.wrapper(wasm)); | ||
fs.writeFileSync(outputFile, this.wrapper(wasm_gc_opt)); | ||
} | ||
else { | ||
const outputFile = path.join(this.outputPath, `${name}.wasm`); | ||
fs.writeFileSync(outputFile, wasm); | ||
fs.writeFileSync(outputFile, wasm_gc_opt); | ||
} | ||
@@ -89,9 +92,43 @@ } | ||
// tslint:disable-next-line:max-line-length | ||
return `const toBuffer = typeof Buffer === 'undefined' ? (str) => Uint8Array.from(atob(str), c => c.charCodeAt(0)) : (str) => Buffer.from(str, 'base64'); | ||
const mod = new WebAssembly.Module(toBuffer("${buffer.toString("base64")}")); | ||
export default (imports) => new WebAssembly.Instance(mod, imports).exports; | ||
`; | ||
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")}")`; | ||
if (this.generateAsyncWrapper) { | ||
return `${toBuffer} | ||
export default async (imports) => | ||
const mod = await WebAssembly.compile(${deserialized}); | ||
return (await WebAssembly.instantiate(mod, imports)).exports;`; | ||
} | ||
else { | ||
return `${toBuffer} | ||
const mod = new WebAssembly.Module(${deserialized}); | ||
export default (imports) => new WebAssembly.Instance(mod, imports).exports;`; | ||
} | ||
} | ||
wasm_gc(wasm) { | ||
const temp1 = path.join(this.cachePath, `gc-input.wasm`); | ||
const temp2 = path.join(this.cachePath, `gc-output.wasm`); | ||
fs.writeFileSync(temp1, wasm); | ||
child_process_1.execFileSync(`wasm-gc`, [temp1, temp2]); | ||
return fs.readFileSync(temp2); | ||
} | ||
// Optionally run the `wasm-opt` binary from | ||
// https://github.com/WebAssembly/binaryen but it's not always installed | ||
// everywhere or easy to install so try to gracfully handle the case where it | ||
// can't be found and instead just skip this step. | ||
wasm_opt(wasm) { | ||
const temp1 = path.join(this.cachePath, `opt-input.wasm`); | ||
const temp2 = path.join(this.cachePath, `opt-output.wasm`); | ||
fs.writeFileSync(temp1, wasm); | ||
try { | ||
child_process_1.execFileSync(`wasm-opt`, [`-Os`, temp1, `-o`, temp2]); | ||
} | ||
catch (err) { | ||
if (err.code == 'ENOENT') | ||
return wasm; | ||
throw err; | ||
} | ||
return fs.readFileSync(temp2); | ||
} | ||
} | ||
exports.default = RustPlugin; | ||
//# sourceMappingURL=plugin.js.map |
{ | ||
"name": "broccoli-rust2wasm", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "broccoli plugin to build rust to wasm", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
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
12053
176