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