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 printErr: (line: any) => {}, 30 onRuntimeInitialized: () => { 31 resolve('ok'); 32 }, 33 onAbort: () => {}, 34 }); 35 } 36 callModelFun(wasmFunctionName); 37 }); 38} 39 40self.onmessage = async (e: MessageEvent) => { 41 if (e.data.action === 'open') { 42 let jsFile = e.data.wasmJsName; 43 importScripts(jsFile); 44 await initConfigWASM(e.data.WasmName); 45 let dataCallBack = (heapPtr: number, size: number, isEnd: number, isConfig: number) => { 46 if (isConfig == 1) { 47 let jsonOut: Uint8Array = Module_T.HEAPU8.slice(heapPtr, heapPtr + size); 48 let decoder = new TextDecoder(); 49 let result = decoder.decode(jsonOut); 50 let json = JSON.parse(result); 51 self.postMessage({ 52 results: json, 53 }); 54 } 55 }; 56 let fn = Module_T.addFunction(dataCallBack, 'viiii'); 57 Module_T._Init(fn, mallocSize); 58 Module_T._TraceStreamer_In_JsonConfig(); 59 } 60}; 61