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

Method wrapImport

web/src/asyncify.ts:97–141  ·  view source on GitHub ↗

* Wrap a function that can be used as import of the wasm asynctify layer * * @param func The input import function * @returns The wrapped function that can be registered to the system

(func: (...args: Array<any>) => any)

Source from the content-addressed store, hash-verified

95 * @returns The wrapped function that can be registered to the system
96 */
97 wrapImport(func: (...args: Array<any>) => any): (...args: Array<any>) => any {
98 return (...args: any) => {
99 // this is being called second time
100 // where we are rewinding the stack
101 if (this.getState() == AsyncifyStateKind.Rewinding) {
102 // JUMP-PT-REWIND: rewind will jump to this pt
103 // while rewinding the stack
104 this.stopRewind();
105 // the value has been resolved
106 if (this.storedValueBeforeRewind !== null) {
107 assert(this.storedExceptionBeforeRewind === null);
108 const result = this.storedValueBeforeRewind;
109 this.storedValueBeforeRewind = null;
110 return result;
111 } else {
112 assert(this.storedValueBeforeRewind === null);
113 const error = this.storedExceptionBeforeRewind;
114 this.storedExceptionBeforeRewind = null;
115 throw error;
116 }
117 }
118 // this function is being called for the first time
119 assert(this.getState() == AsyncifyStateKind.None);
120
121 // call the function
122 const value = func(...args);
123 // if the value is promise
124 // we need to unwind the stack
125 // so the caller will be able to evaluate the promise
126 if (isPromise(value)) {
127 // The next code step is JUMP-PT-UNWIND in wrapExport
128 // The value will be passed to that pt through storedPromiseBeforeUnwind
129 // getState() == Unwinding and we will enter the while loop in wrapExport
130 this.startUnwind();
131 assert(this.storedPromiseBeforeUnwind == null);
132 this.storedPromiseBeforeUnwind = value;
133 return undefined;
134 } else {
135 // The next code step is JUMP-PT-UNWIND in wrapExport
136 // normal value, we don't have to do anything
137 // getState() == None and we will exit while loop there
138 return value;
139 }
140 };
141 }
142
143 /**
144 * Warp an exported asynctify function so it can return promise

Callers 1

registerAsyncifyFuncMethod · 0.80

Calls 6

getStateMethod · 0.95
stopRewindMethod · 0.95
startUnwindMethod · 0.95
assertFunction · 0.90
isPromiseFunction · 0.90
funcFunction · 0.50

Tested by

no test coverage detected