• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: unknown = null;
19
20function initConfigWASM(wasmFunctionName: string): Promise<string> {
21  return new Promise((resolve, reject) => {
22    function callModelFun(functionName: string): void {
23      let func = eval(functionName);
24      Module_T = new func({
25        locateFile: (s: unknown): unknown => {
26          return s;
27        },
28        print: (line: unknown): void => {},
29        printErr: (line: unknown): void => {},
30        onRuntimeInitialized: (): void => {
31          resolve('ok');
32        },
33        onAbort: (): void => {},
34      });
35    }
36    callModelFun(wasmFunctionName);
37  });
38}
39
40self.onmessage = async (e: MessageEvent): Promise<void> => {
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): void => {
46      if (isConfig === 1) {
47        // @ts-ignore
48        let jsonOut: Uint8Array = Module_T.HEAPU8.slice(heapPtr, heapPtr + size);
49        let decoder = new TextDecoder();
50        let result = decoder.decode(jsonOut);
51        let json = JSON.parse(result);
52        self.postMessage({
53          results: json,
54        });
55      }
56    };
57    // @ts-ignore
58    let fn = Module_T.addFunction(dataCallBack, 'viiii');
59    // @ts-ignore
60    Module_T._Init(fn, mallocSize);
61    // @ts-ignore
62    Module_T._TraceStreamer_In_JsonConfig();
63  }
64};
65