MCPcopy Index your code
hub / github.com/apache/tvm / FFILibrary

Class FFILibrary

web/src/runtime.ts:58–151  ·  view source on GitHub ↗

* @internal * FFI Library wrapper, maintains most runtime states.

Source from the content-addressed store, hash-verified

56 * FFI Library wrapper, maintains most runtime states.
57 */
58class FFILibrary implements Disposable {
59 wasm32: boolean;
60 memory: Memory;
61 exports: Record<string, Function>;
62 webGPUContext?: WebGPUContext;
63 private wasmInstance: WebAssembly.Instance;
64 private recycledCallStacks: Array<CachedCallStack> = [];
65
66 constructor(
67 wasmInstance: WebAssembly.Instance,
68 imports: Record<string, any>
69 ) {
70 this.wasmInstance = wasmInstance;
71 this.memory = new Memory(this.detectWasmMemory(this.wasmInstance, imports));
72 assert(
73 this.wasmInstance.exports !== undefined,
74 "Expect the library module contains exports"
75 );
76 this.exports = this.wasmInstance.exports as Record<string, Function>;
77 this.wasm32 = this.memory.wasm32;
78 this.validateInstance();
79 }
80
81 dispose(): void {
82 while (this.recycledCallStacks.length != 0) {
83 (this.recycledCallStacks.pop() as Disposable).dispose();
84 }
85 this.webGPUContext?.dispose();
86 }
87
88 sizeofPtr(): number {
89 return this.memory.sizeofPtr();
90 }
91
92 checkCall(code: number): void {
93 if (code != 0) {
94 const msgPtr = (this.exports
95 .TVMFFIWasmGetLastError as ctypes.FTVMFFIWasmGetLastError)();
96 throw new Error(this.memory.loadCString(msgPtr));
97 }
98 }
99
100 getOrAllocCallStack(): CachedCallStack {
101 if (this.recycledCallStacks.length != 0) {
102 return this.recycledCallStacks.pop() as CachedCallStack;
103 }
104 return new CachedCallStack(
105 this.memory,
106 this.exports.TVMWasmAllocSpace as ctypes.FTVMWasmAllocSpace,
107 this.exports.TVMWasmFreeSpace as ctypes.FTVMWasmFreeSpace
108 );
109 }
110
111 recycleCallStack(callstack: CachedCallStack): void {
112 callstack.reset();
113 this.recycledCallStacks.push(callstack);
114 }
115

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…