0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-12 16:59:32 -05:00

fix(cli): use upstream type definitions for WebAssembly (#7216)

This commit is contained in:
Casper Beyer 2020-08-31 18:04:47 +08:00 committed by GitHub
parent 5c23388f6d
commit a451a97486
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 147 deletions

View file

@ -6,173 +6,129 @@
/// <reference lib="esnext" /> /// <reference lib="esnext" />
/// <reference lib="deno.web" /> /// <reference lib="deno.web" />
// This follows the WebIDL at: https://webassembly.github.io/spec/js-api/
// and: https://webassembly.github.io/spec/web-api/
declare namespace WebAssembly { declare namespace WebAssembly {
interface WebAssemblyInstantiatedSource { interface CompileError {
module: Module; }
instance: Instance;
}
/** Compiles a `WebAssembly.Module` from WebAssembly binary code. This var CompileError: {
* function is useful if it is necessary to a compile a module before it can prototype: CompileError;
* be instantiated (otherwise, the `WebAssembly.instantiate()` function new(): CompileError;
* should be used). */ };
function compile(bufferSource: BufferSource): Promise<Module>;
/** Compiles a `WebAssembly.Module` directly from a streamed underlying interface Global {
* source. This function is useful if it is necessary to a compile a module value: any;
* before it can be instantiated (otherwise, the valueOf(): any;
* `WebAssembly.instantiateStreaming()` function should be used). */ }
function compileStreaming(source: Promise<Response>): Promise<Module>;
/** Takes the WebAssembly binary code, in the form of a typed array or var Global: {
* `ArrayBuffer`, and performs both compilation and instantiation in one step. prototype: Global;
* The returned `Promise` resolves to both a compiled `WebAssembly.Module` and new(descriptor: GlobalDescriptor, v?: any): Global;
* its first `WebAssembly.Instance`. */ };
function instantiate(
bufferSource: BufferSource,
importObject?: object,
): Promise<WebAssemblyInstantiatedSource>;
/** Takes an already-compiled `WebAssembly.Module` and returns a `Promise` interface Instance {
* that resolves to an `Instance` of that `Module`. This overload is useful if readonly exports: Exports;
* the `Module` has already been compiled. */ }
function instantiate(
module: Module,
importObject?: object,
): Promise<Instance>;
/** Compiles and instantiates a WebAssembly module directly from a streamed var Instance: {
* underlying source. This is the most efficient, optimized way to load wasm prototype: Instance;
* code. */ new(module: Module, importObject?: Imports): Instance;
function instantiateStreaming( };
source: Promise<Response>,
importObject?: object,
): Promise<WebAssemblyInstantiatedSource>;
/** Validates a given typed array of WebAssembly binary code, returning interface LinkError {
* whether the bytes form a valid wasm module (`true`) or not (`false`). */ }
function validate(bufferSource: BufferSource): boolean;
type ImportExportKind = "function" | "table" | "memory" | "global"; var LinkError: {
prototype: LinkError;
new(): LinkError;
};
interface ModuleExportDescriptor { interface Memory {
name: string; readonly buffer: ArrayBuffer;
kind: ImportExportKind; grow(delta: number): number;
} }
interface ModuleImportDescriptor {
module: string;
name: string;
kind: ImportExportKind;
}
class Module { var Memory: {
constructor(bufferSource: BufferSource); prototype: Memory;
new(descriptor: MemoryDescriptor): Memory;
};
/** Given a `Module` and string, returns a copy of the contents of all interface Module {
* custom sections in the module with the given string name. */ }
static customSections(
moduleObject: Module,
sectionName: string,
): ArrayBuffer;
/** Given a `Module`, returns an array containing descriptions of all the var Module: {
* declared exports. */ prototype: Module;
static exports(moduleObject: Module): ModuleExportDescriptor[]; new(bytes: BufferSource): Module;
customSections(moduleObject: Module, sectionName: string): ArrayBuffer[];
exports(moduleObject: Module): ModuleExportDescriptor[];
imports(moduleObject: Module): ModuleImportDescriptor[];
};
/** Given a `Module`, returns an array containing descriptions of all the interface RuntimeError {
* declared imports. */ }
static imports(moduleObject: Module): ModuleImportDescriptor[];
}
class Instance<T extends object = { [key: string]: any }> { var RuntimeError: {
constructor(module: Module, importObject?: object); prototype: RuntimeError;
new(): RuntimeError;
};
/** An object containing as its members all the functions exported from the interface Table {
* WebAssembly module instance, to allow them to be accessed and used by readonly length: number;
* JavaScript. */ get(index: number): Function | null;
readonly exports: T; grow(delta: number): number;
} set(index: number, value: Function | null): void;
}
interface MemoryDescriptor { var Table: {
initial: number; prototype: Table;
maximum?: number; new(descriptor: TableDescriptor): Table;
} };
class Memory { interface GlobalDescriptor {
constructor(descriptor: MemoryDescriptor); mutable?: boolean;
value: ValueType;
}
/** An accessor property that returns the buffer contained in the memory. */ interface MemoryDescriptor {
readonly buffer: ArrayBuffer; initial: number;
maximum?: number;
}
/** Increases the size of the memory instance by a specified number of interface ModuleExportDescriptor {
* WebAssembly pages (each one is 64KB in size). */ kind: ImportExportKind;
grow(delta: number): number; name: string;
} }
type TableKind = "anyfunc"; interface ModuleImportDescriptor {
kind: ImportExportKind;
module: string;
name: string;
}
interface TableDescriptor { interface TableDescriptor {
element: TableKind; element: TableKind;
initial: number; initial: number;
maximum?: number; maximum?: number;
} }
class Table { interface WebAssemblyInstantiatedSource {
constructor(descriptor: TableDescriptor); instance: Instance;
module: Module;
}
/** Returns the length of the table, i.e. the number of elements. */ type ImportExportKind = "function" | "global" | "memory" | "table";
readonly length: number; type TableKind = "anyfunc";
type ValueType = "f32" | "f64" | "i32" | "i64";
/** Accessor function — gets the element stored at a given index. */ type ExportValue = Function | Global | Memory | Table;
get(index: number): (...args: any[]) => any; type Exports = Record<string, ExportValue>;
type ImportValue = ExportValue | number;
/** Increases the size of the Table instance by a specified number of type ModuleImports = Record<string, ImportValue>;
* elements. */ type Imports = Record<string, ModuleImports>;
grow(delta: number): number; function compile(bytes: BufferSource): Promise<Module>;
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
/** Sets an element stored at a given index to a given value. */ function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
set(index: number, value: (...args: any[]) => any): void; function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
} function instantiateStreaming(response: Response | PromiseLike<Response>, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function validate(bytes: BufferSource): boolean;
type ValueType = "i32" | "i64" | "f32" | "f64";
interface GlobalDescriptor {
value: ValueType;
mutable?: boolean;
}
/** Represents a global variable instance, accessible from both JavaScript and
* importable/exportable across one or more `WebAssembly.Module` instances.
* This allows dynamic linking of multiple modules. */
class Global {
constructor(descriptor: GlobalDescriptor, value?: any);
/** Old-style method that returns the value contained inside the global
* variable. */
valueOf(): any;
/** The value contained inside the global variable this can be used to
* directly set and get the global's value. */
value: any;
}
/** Indicates an error during WebAssembly decoding or validation */
class CompileError extends Error {
constructor(message: string, fileName?: string, lineNumber?: string);
}
/** Indicates an error during module instantiation (besides traps from the
* start function). */
class LinkError extends Error {
constructor(message: string, fileName?: string, lineNumber?: string);
}
/** Is thrown whenever WebAssembly specifies a trap. */
class RuntimeError extends Error {
constructor(message: string, fileName?: string, lineNumber?: string);
}
} }
/** Sets a timer which executes a function once after the timer expires. Returns /** Sets a timer which executes a function once after the timer expires. Returns

View file

@ -12,4 +12,5 @@ const wasmModule = new WebAssembly.Module(wasmCode);
const wasmInstance = new WebAssembly.Instance(wasmModule); const wasmInstance = new WebAssembly.Instance(wasmModule);
console.log(wasmInstance.exports.main().toString()); const main = wasmInstance.exports.main as CallableFunction;
console.log(main().toString());

View file

@ -15,6 +15,8 @@ const instance = new WebAssembly.Instance(module, {
wasi_snapshot_preview1: context.exports, wasi_snapshot_preview1: context.exports,
}); });
context.memory = instance.exports.memory; const memory = instance.exports.memory as WebAssembly.Memory;
context.memory = memory;
instance.exports._start(); const start = instance.exports._start as CallableFunction;
start();