1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const mallocSize = 1024 * 1024; 17 18let Module_T: any = null; 19 20function initConfigWASM(wasmFunctionName:string) { 21 return new Promise((resolve, reject) => { 22 function callModelFun(functionName: string) { 23 let func = eval(functionName); 24 Module_T = new func({ 25 locateFile: (s: any) => { 26 return s 27 }, 28 print: (line: any) => { 29 }, 30 printErr: (line: any) => { 31 }, 32 onRuntimeInitialized: () => { 33 resolve("ok") 34 }, 35 onAbort: () => { 36 } 37 }); 38 } 39 callModelFun(wasmFunctionName) 40 }) 41} 42 43self.onmessage = async (e: MessageEvent) => { 44 if (e.data.action === "open") { 45 let jsFile = e.data.wasmJsName 46 importScripts(jsFile) 47 await initConfigWASM(e.data.WasmName); 48 let dataCallBack = (heapPtr: number, size: number, isEnd: number, isConfig: number) => { 49 if (isConfig == 1) { 50 let jsonOut: Uint8Array = Module_T.HEAPU8.slice(heapPtr, heapPtr + size); 51 let decoder = new TextDecoder(); 52 let result = decoder.decode(jsonOut); 53 let json = JSON.parse(result); 54 self.postMessage({ 55 results: json, 56 }); 57 } 58 } 59 let fn = Module_T.addFunction(dataCallBack, "viiii"); 60 Module_T._Init(fn, mallocSize) 61 Module_T._TraceStreamer_In_JsonConfig(); 62 } 63} 64