From db1ff05e66e54dc1ae12a8762cc570a8c795e41d Mon Sep 17 00:00:00 2001 From: kamakiri01 Date: Sat, 1 Jul 2023 02:44:14 +0900 Subject: [PATCH 1/5] build without typings include --- package.json | 4 ++-- src-ts/main/emsciptenModule/QulacsWasmModule.ts | 1 + src-ts/main/init.ts | 4 +++- .../wasm/emscriptem-types.ts | 3 +-- src-ts/wasm/module.ts | 6 ++++++ tsconfig.json | 4 ---- typings/ModuleQulacsWasm.d.ts | 6 ------ 7 files changed, 13 insertions(+), 15 deletions(-) rename typings/emscriptem-types.d.ts => src-ts/wasm/emscriptem-types.ts (99%) create mode 100644 src-ts/wasm/module.ts delete mode 100644 typings/ModuleQulacsWasm.d.ts diff --git a/package.json b/package.json index 71eaf67..522d315 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,8 @@ "build:emscripten:em++:bundle": "em++ -O1 -s SINGLE_FILE=1 -I ./submodules/boost -I ./submodules/qulacs/include -L ./submodules/qulacs/lib -o ./lib-cpp/bundle/module.js ./src-cpp/QulacsWasmModule.cpp -lvqcsim_static -lcppsim_static -lcsim_static -s MODULARIZE=1 -s 'EXPORT_NAME=\"ModuleQulacsWasm\"' --bind -s DISABLE_EXCEPTION_CATCHING=0 -s EXPORTED_RUNTIME_METHODS=addFunction,removeFunction,ccall,cwrap,dyncall -s ALLOW_TABLE_GROWTH -s EXTRA_EXPORTED_RUNTIME_METHODS=['dynCall','getValue','setValue'] -s ALLOW_MEMORY_GROWTH", "build:emscripten:em++:nobundle": "em++ -O1 -I ./submodules/boost -I ./submodules/qulacs/include -L ./submodules/qulacs/lib -o ./lib-cpp/nobundle/module.js ./src-cpp/QulacsWasmModule.cpp -lvqcsim_static -lcppsim_static -lcsim_static -s MODULARIZE=1 -s 'EXPORT_NAME=\"ModuleQulacsWasm\"' --bind -s DISABLE_EXCEPTION_CATCHING=0 -s EXPORTED_RUNTIME_METHODS=addFunction,removeFunction,ccall,cwrap,dyncall -s ALLOW_TABLE_GROWTH -s EXTRA_EXPORTED_RUNTIME_METHODS=['dynCall','getValue','setValue'] -s ALLOW_MEMORY_GROWTH", "build:emscripten:cp": "npm run build:emscripten:cp:bundle && npm run build:emscripten:cp:nobundle", - "build:emscripten:cp:bundle": "cpx \"./lib-cpp/bundle/**\" \"./lib/bundle/wasm/\" --clean", - "build:emscripten:cp:nobundle": "cpx \"./lib-cpp/nobundle/**\" \"./lib/nobundle/wasm/\" --clean", + "build:emscripten:cp:bundle": "cpx \"./lib-cpp/bundle/**\" \"./lib/bundle/wasm/\"", + "build:emscripten:cp:nobundle": "cpx \"./lib-cpp/nobundle/**\" \"./lib/nobundle/wasm/\"", "test": "jest && npm run lint", "lint": "eslint \"./src-ts/**/*.ts\" \"./test/**/*.ts\"" }, diff --git a/src-ts/main/emsciptenModule/QulacsWasmModule.ts b/src-ts/main/emsciptenModule/QulacsWasmModule.ts index ce8089d..715b320 100644 --- a/src-ts/main/emsciptenModule/QulacsWasmModule.ts +++ b/src-ts/main/emsciptenModule/QulacsWasmModule.ts @@ -1,3 +1,4 @@ +import { EmscriptenWasm } from "../../wasm/emscriptem-types"; export interface QulacsWasmModule extends EmscriptenWasm.Module { //QuantumState: QuantumStateI; // NOTE: applyModuleのObject.keysで取得するため、メンバを型では直接参照しない。そのため、ここでメンバを定義する必要はない getExceptionMessage(exceptionPtr: number): string; diff --git a/src-ts/main/init.ts b/src-ts/main/init.ts index f85736c..8859f7c 100644 --- a/src-ts/main/init.ts +++ b/src-ts/main/init.ts @@ -1,4 +1,5 @@ -const ModuleQulacsWasm = require("../wasm/module.js"); +import ModuleQulacsWasm from "../wasm/module"; +import { EmscriptenWasm } from "../wasm/emscriptem-types"; import { QulacsWasmModule } from "./emsciptenModule/QulacsWasmModule"; import { applyModule } from "./instance"; @@ -30,6 +31,7 @@ function _initQulacsFromModule(compiledModule: WebAssembly.Module): Promise reject(e)); + return undefined!; } ModuleQulacsWasm({ instantiateWasm: onInstantiateWasm }) .then((emscriptenModule: EmscriptenWasm.Module) => { diff --git a/typings/emscriptem-types.d.ts b/src-ts/wasm/emscriptem-types.ts similarity index 99% rename from typings/emscriptem-types.d.ts rename to src-ts/wasm/emscriptem-types.ts index 0d23d5e..45e8f79 100644 --- a/typings/emscriptem-types.d.ts +++ b/src-ts/wasm/emscriptem-types.ts @@ -1,8 +1,7 @@ // reference from https://github.com/jamsinclair/jSquash/blob/db110f3adb463615e9022bd62b124c87ae2c8bc0/packages/jpeg/emscriptem-types.d.ts - // These types roughly model the object that the JS files generated by Emscripten define. Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/emscripten/index.d.ts and turned into a type definition rather than a global to support our way of using Emscripten. // TODO(@surma): Upstream this? -declare namespace EmscriptenWasm { +export declare namespace EmscriptenWasm { type ModuleFactory = ( moduleOverrides?: ModuleOpts, ) => Promise; diff --git a/src-ts/wasm/module.ts b/src-ts/wasm/module.ts new file mode 100644 index 0000000..9159ec1 --- /dev/null +++ b/src-ts/wasm/module.ts @@ -0,0 +1,6 @@ +// NOTE: このファイルはtscビルドを通す型定義のためだけに利用する。ビルド後の本ファイルはemscriptenのmodule.jsに上書きされる +import {EmscriptenWasm} from "./emscriptem-types"; +import { QulacsWasmModule } from "../main/emsciptenModule/QulacsWasmModule"; + +declare const ModuleQulacsWasm: EmscriptenWasm.ModuleFactory; +export = ModuleQulacsWasm; diff --git a/tsconfig.json b/tsconfig.json index b3d6863..8148bf4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,10 +10,6 @@ "esModuleInterop": true, "forceConsistentCasingInFileNames": true }, - "files": [ - "./typings/ModuleQulacsWasm.d.ts", - "./typings/emscriptem-types.d.ts" - ], "include": [ "./src-ts/**/*" ] diff --git a/typings/ModuleQulacsWasm.d.ts b/typings/ModuleQulacsWasm.d.ts deleted file mode 100644 index 2151fe7..0000000 --- a/typings/ModuleQulacsWasm.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -// "@types/emscripten": "^1.39.6" では WebAssembly.instantiateのimportObject: WebAssembly.ImportsとinstantiateWasm関数のimportObject: Emscripten.WebAssemblyImportsに互換性がない -declare var ModuleQulacsWasm: EmscriptenWasm.ModuleFactory; - -declare module "ModuleQulacsWasm" { - export = ModuleQulacsWasm -} From 8157f47c1b2bd0882cc2ee4e1bbc7c92e208ad18 Mon Sep 17 00:00:00 2001 From: kamakiri01 Date: Sat, 1 Jul 2023 02:49:00 +0900 Subject: [PATCH 2/5] fix comment --- src-ts/main/init.ts | 2 +- src-ts/wasm/module.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src-ts/main/init.ts b/src-ts/main/init.ts index 8859f7c..b2f9a30 100644 --- a/src-ts/main/init.ts +++ b/src-ts/main/init.ts @@ -31,7 +31,7 @@ function _initQulacsFromModule(compiledModule: WebAssembly.Module): Promise reject(e)); - return undefined!; + return undefined!; // NOTE: @types/emscriptenでは不要なnon-null assertionかもしれない } ModuleQulacsWasm({ instantiateWasm: onInstantiateWasm }) .then((emscriptenModule: EmscriptenWasm.Module) => { diff --git a/src-ts/wasm/module.ts b/src-ts/wasm/module.ts index 9159ec1..d7fee6f 100644 --- a/src-ts/wasm/module.ts +++ b/src-ts/wasm/module.ts @@ -1,4 +1,4 @@ -// NOTE: このファイルはtscビルドを通す型定義のためだけに利用する。ビルド後の本ファイルはemscriptenのmodule.jsに上書きされる +// NOTE: このファイルはtscビルドを通す型整合性のためだけに利用し、publishには含まない。このファイルはnpm run build終了時点でemscriptenのmodule.jsに上書きされる import {EmscriptenWasm} from "./emscriptem-types"; import { QulacsWasmModule } from "../main/emsciptenModule/QulacsWasmModule"; From b749695db881f8420719c2f646a371edcd802684 Mon Sep 17 00:00:00 2001 From: kamakiri01 Date: Sat, 1 Jul 2023 02:50:15 +0900 Subject: [PATCH 3/5] use ITYPE --- src-cpp/QulacsWasmModule.cpp | 5 ++--- src-cpp/emjs.cpp | 2 +- src-cpp/util.cpp | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src-cpp/QulacsWasmModule.cpp b/src-cpp/QulacsWasmModule.cpp index 4edde05..d79c015 100644 --- a/src-cpp/QulacsWasmModule.cpp +++ b/src-cpp/QulacsWasmModule.cpp @@ -85,9 +85,8 @@ EMSCRIPTEN_BINDINGS(Bindings) { .function("multiply_elementwise_function_wrapper", emscripten::optional_override([](QuantumState& self, intptr_t funcPtr) { // JSのfuncPtr先の関数をC++の型でラップする std::function func = [funcPtr](ITYPE num) -> CPPCTYPE { - int castedNum = (int) num; double complexArr[2]; // 戻り値のcomplex要素を格納するメモリを確保する - QuantumStateMultiplyElementwiseFunctionWrapper(funcPtr, castedNum, complexArr); // メモリにfuncPtrの実行結果を書き込む + QuantumStateMultiplyElementwiseFunctionWrapper(funcPtr, num, complexArr); // メモリにfuncPtrの実行結果を書き込む double real = complexArr[0]; double imag = complexArr[1]; std::complex c(real, imag); @@ -743,7 +742,7 @@ EMSCRIPTEN_BINDINGS(Bindings) { auto size = list.size(); int arr[size]; for (int i = 0; i < size; i++) { - arr[i] = (int)list[i]; + arr[i] = list[i]; } return AdaptiveWrapper(funcPtr, arr, size); }; diff --git a/src-cpp/emjs.cpp b/src-cpp/emjs.cpp index 3472006..5c2803e 100644 --- a/src-cpp/emjs.cpp +++ b/src-cpp/emjs.cpp @@ -65,7 +65,7 @@ EM_JS(int, AdaptiveWrapper, (intptr_t funcPtr, int* arr, int size), { return re; }); -EM_JS(void, QuantumStateMultiplyElementwiseFunctionWrapper, (intptr_t funcPtr, int num, double* complexArrPtr), { +EM_JS(void, QuantumStateMultiplyElementwiseFunctionWrapper, (intptr_t funcPtr, ITYPE num, double* complexArrPtr), { // 受け取ったfuncPtrの関数がcomplexArrPtrにstd::Complexの要素をそれぞれ格納することを期待する // @see https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-functions-as-function-pointers-from-c Module['dynCall']('vii', funcPtr, [num, complexArrPtr]); diff --git a/src-cpp/util.cpp b/src-cpp/util.cpp index 64563ca..0024fd0 100644 --- a/src-cpp/util.cpp +++ b/src-cpp/util.cpp @@ -64,7 +64,7 @@ emscripten::EM_VAL transpaleITYPEVecToJSArray(const std::vector &vec) { int size = vec.size(); int arr[size]; for (int i = 0; i < size; i++) { - arr[i] = (int) vec[i]; // NOTE: long long intをintに丸めている。JSで (un)signed long long intを取得する方法を検討 + arr[i] = vec[i]; } return convertIntArrayToJSArray(arr, size); } From 6087ffc451e6c031d93a2db20bd83de5034c8402 Mon Sep 17 00:00:00 2001 From: kamakiri01 Date: Sat, 1 Jul 2023 20:25:31 +0900 Subject: [PATCH 4/5] revert em_js type --- src-cpp/emjs.cpp | 2 +- src-ts/wasm/emscriptem-types.ts | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src-cpp/emjs.cpp b/src-cpp/emjs.cpp index 5c2803e..3472006 100644 --- a/src-cpp/emjs.cpp +++ b/src-cpp/emjs.cpp @@ -65,7 +65,7 @@ EM_JS(int, AdaptiveWrapper, (intptr_t funcPtr, int* arr, int size), { return re; }); -EM_JS(void, QuantumStateMultiplyElementwiseFunctionWrapper, (intptr_t funcPtr, ITYPE num, double* complexArrPtr), { +EM_JS(void, QuantumStateMultiplyElementwiseFunctionWrapper, (intptr_t funcPtr, int num, double* complexArrPtr), { // 受け取ったfuncPtrの関数がcomplexArrPtrにstd::Complexの要素をそれぞれ格納することを期待する // @see https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-functions-as-function-pointers-from-c Module['dynCall']('vii', funcPtr, [num, complexArrPtr]); diff --git a/src-ts/wasm/emscriptem-types.ts b/src-ts/wasm/emscriptem-types.ts index 45e8f79..1546b56 100644 --- a/src-ts/wasm/emscriptem-types.ts +++ b/src-ts/wasm/emscriptem-types.ts @@ -1,6 +1,8 @@ // reference from https://github.com/jamsinclair/jSquash/blob/db110f3adb463615e9022bd62b124c87ae2c8bc0/packages/jpeg/emscriptem-types.d.ts // These types roughly model the object that the JS files generated by Emscripten define. Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/emscripten/index.d.ts and turned into a type definition rather than a global to support our way of using Emscripten. // TODO(@surma): Upstream this? + +// eslint-disable-next-line @typescript-eslint/no-namespace export declare namespace EmscriptenWasm { type ModuleFactory = ( moduleOverrides?: ModuleOpts, @@ -29,6 +31,7 @@ export declare namespace EmscriptenWasm { preInit: { (): void }[]; preRun: { (): void }[]; postRun: { (): void }[]; + // eslint-disable-next-line no-undef preinitializedWebGLContext: WebGLRenderingContext; noInitialRun: boolean; noExitRuntime: boolean; From 762625e42eba8d297d97f2bb37f622ad4697b0c3 Mon Sep 17 00:00:00 2001 From: kamakiri01 Date: Mon, 3 Jul 2023 19:32:41 +0900 Subject: [PATCH 5/5] bump version --- CHANGELOG.md | 7 +++++-- package.json | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d4e358..2b560cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # ChangeLog -# 0.0.3 -- release +## 0.0.4 +- Reorganized type definition dependencies + +## 0.0.3 +- Release diff --git a/package.json b/package.json index 522d315..a1cc0e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qulacs-wasm", - "version": "0.0.3", + "version": "0.0.4", "description": "Qulacs WebAssembly version", "main": "lib/bundle/index.js", "scripts": {