(handle: Pointer)
| 2204 | } |
| 2205 | |
| 2206 | private makePackedFunc(handle: Pointer): PackedFunc { |
| 2207 | const cell = new PackedFuncCell(handle, this.lib, this.ctx); |
| 2208 | const packedFunc = (...args: any): any => { |
| 2209 | const stack = this.lib.getOrAllocCallStack(); |
| 2210 | const argsOffset = stack.allocRawBytes(SizeOf.TVMFFIAny * args.length); |
| 2211 | this.setPackedArguments(stack, args, argsOffset); |
| 2212 | const retOffset = stack.allocRawBytes(SizeOf.TVMFFIAny); |
| 2213 | // pre-store the result to be null |
| 2214 | stack.storeI32(retOffset, TypeIndex.kTVMFFINone); |
| 2215 | // clear off the extra zero padding before ptr storage |
| 2216 | stack.storeI32(retOffset + SizeOf.I32, 0); |
| 2217 | stack.commitToWasmMemory(); |
| 2218 | this.lib.checkCall( |
| 2219 | (this.exports.TVMFFIFunctionCall as ctypes.FTVMFFIFunctionCall)( |
| 2220 | cell.getHandle(), |
| 2221 | stack.ptrFromOffset(argsOffset), |
| 2222 | args.length, |
| 2223 | stack.ptrFromOffset(retOffset) |
| 2224 | ) |
| 2225 | ); |
| 2226 | |
| 2227 | const ret = this.retValueToJS(stack.ptrFromOffset(retOffset), false); |
| 2228 | this.lib.recycleCallStack(stack); |
| 2229 | return ret; |
| 2230 | }; |
| 2231 | // Attach attributes to the function type. |
| 2232 | // This is because javascript do not allow us to overload call. |
| 2233 | const ret: any = packedFunc; |
| 2234 | ret.dispose = (): void => { |
| 2235 | cell.dispose(); |
| 2236 | }; |
| 2237 | ret._tvmPackedCell = cell; |
| 2238 | return ret as PackedFunc; |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * Creaye return value of the packed func. The value us auto-tracked for dispose. |
no test coverage detected