| 854 | * - {@link detachFromCurrentScope} |
| 855 | */ |
| 856 | export class Instance implements Disposable { |
| 857 | memory: Memory; |
| 858 | exports: Record<string, Function>; |
| 859 | cacheMetadata: Record<string, any> = {}; |
| 860 | private lib: FFILibrary; |
| 861 | private env: Environment; |
| 862 | private objFactory: Map<number, FObjectConstructor>; |
| 863 | private ctx: RuntimeContext; |
| 864 | private asyncifyHandler: AsyncifyHandler; |
| 865 | private initProgressCallback: Array<InitProgressCallback> = []; |
| 866 | private rng: LinearCongruentialGenerator; |
| 867 | private deviceLostIsError = true; // whether device.lost is due to actual error or dispose() |
| 868 | private cacheState: CacheState = new CacheState(); |
| 869 | |
| 870 | /** |
| 871 | * Internal function(registered by the runtime) |
| 872 | */ |
| 873 | private wasmCreateLibraryModule?: PackedFunc & |
| 874 | ((getFunc: PackedFunc, getGlobal: PackedFunc) => PackedFunc); |
| 875 | |
| 876 | /** |
| 877 | * Constructor |
| 878 | * |
| 879 | * importObject can also be a {@link LibraryProvider} object, |
| 880 | * a WASI object, or an object containing wasmLibraryProvider field. |
| 881 | * |
| 882 | * @param wasmModule The input module or instance. |
| 883 | * @param importObject The imports to initialize the wasmInstance if it is not provided. |
| 884 | * @param wasmInstance Additional wasm instance argument for deferred construction. |
| 885 | * @param env Directly specified environment module. |
| 886 | * |
| 887 | * @see Please use the async version {@link instantiate} when targeting browsers. |
| 888 | */ |
| 889 | constructor( |
| 890 | wasmModule: WebAssembly.Module, |
| 891 | importObject: Record<string, any> = {}, |
| 892 | wasmInstance?: WebAssembly.Instance, |
| 893 | env?: Environment |
| 894 | ) { |
| 895 | if (wasmInstance instanceof WebAssembly.Instance) { |
| 896 | assert( |
| 897 | env instanceof Environment, |
| 898 | "env must be provided when passing in instance" |
| 899 | ); |
| 900 | } else { |
| 901 | assert(env === undefined); |
| 902 | env = new Environment(importObject); |
| 903 | wasmInstance = new WebAssembly.Instance(wasmModule, env.imports); |
| 904 | } |
| 905 | env.start(wasmInstance); |
| 906 | this.env = env; |
| 907 | this.lib = new FFILibrary(wasmInstance, env.imports); |
| 908 | this.memory = this.lib.memory; |
| 909 | this.exports = this.lib.exports; |
| 910 | this.asyncifyHandler = new AsyncifyHandler(this.exports, this.memory.memory); |
| 911 | this.objFactory = new Map<number, ObjectConstructor>(); |
| 912 | this.ctx = new RuntimeContext( |
| 913 | (name: string) => { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…