From a2742fd635d1915c81c3d7f9c4ae5139a60bb17d Mon Sep 17 00:00:00 2001 From: Sylwester Wroblewski Date: Wed, 23 Oct 2024 14:32:12 +0200 Subject: [PATCH] Switching Kaitai Worker type to module --- build-related/build_language_workers.sh | 1 - package-lock.json | 575 +++++++++++++++++- package.json | 15 +- src/Components/GoldenLayout/GoldenLayoutUI.ts | 12 +- .../HexViewer/Actions/ExportToJson.ts | 4 +- src/Components/InfoPanel/InfoPanel.vue | 2 + .../CodeExecution/KaitaiCodeWorker.ts | 48 +- .../CodeExecution/KaitaiCodeWorkerWrapper.ts | 44 +- .../ParsingModule/CodeExecution/Types.ts | 18 +- .../CodeExecution/WorkerFunctionStack.ts | 8 +- .../CodeExecution/WorkerMessages.ts | 15 +- .../CodeExecution/WorkerResponses.ts | 15 +- .../ParsingModule/KaitaiCodeWorkerApi.ts | 6 + .../ParsingModule/ParseWorkerApi.ts | 24 - src/GlobalActions/CompileGrammar.ts | 31 +- src/GlobalActions/LoadBinaryFile.ts | 4 +- src/GlobalActions/ParseAction.ts | 4 +- src/Stores/IdeSettingsStore.ts | 8 +- src/Utils/LocalStorageApi.ts | 3 +- 19 files changed, 704 insertions(+), 133 deletions(-) create mode 100644 src/DataManipulation/ParsingModule/KaitaiCodeWorkerApi.ts delete mode 100644 src/DataManipulation/ParsingModule/ParseWorkerApi.ts diff --git a/build-related/build_language_workers.sh b/build-related/build_language_workers.sh index 322ef3d1..94603026 100644 --- a/build-related/build_language_workers.sh +++ b/build-related/build_language_workers.sh @@ -1,7 +1,6 @@ ROOT=$PWD/node_modules/monaco-editor/esm/vs OPTS="--no-source-maps" -echo $ROOT parcel build $ROOT/language/json/json.worker.js $OPTS parcel build $ROOT/language/css/css.worker.js $OPTS parcel build $ROOT/language/html/html.worker.js $OPTS diff --git a/package-lock.json b/package-lock.json index 28992a86..7d684f51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "bowser": "^1.7.3", "dateformat": "^5.0.3", "golden-layout": "^1.5.9", + "iconv-lite": "^0.6.3", "jquery": "^3.5.0", "js-yaml": "^4.1.0", "kaitai-struct": "next", @@ -22,6 +23,7 @@ "monaco-editor": "^0.52.0", "pako": "^1.0.6", "pinia": "^2.1.7", + "quickjs-emscripten": "^0.31.0", "vue": "^3.4.29" }, "devDependencies": { @@ -33,15 +35,21 @@ "@types/js-yaml": "^4.0.9", "@types/mocha": "^10.0.9", "@types/node": "^22.7.7", + "assert": "^2.1.0", + "browserify-zlib": "^0.2.0", + "buffer": "^6.0.3", "chai": "^5.1.1", + "events": "^3.3.0", "mocha": "^10.7.3", "parcel": "^2.12.0", "parcel-reporter-static-files-copy": "^1.5.3", "process": "^0.11.10", "serve": "^14.2.4", + "stream-browserify": "^3.0.0", "ts-node": "^10.9.2", "tslint": "^6.1.3", - "typescript": "^5.6.3" + "typescript": "^5.6.3", + "util": "^0.12.5" } }, "node_modules/@babel/code-frame": { @@ -202,6 +210,43 @@ "resolved": "https://registry.npmjs.org/@imengyu/vue3-context-menu/-/vue3-context-menu-1.4.3.tgz", "integrity": "sha512-W2rT+xllLSg+u/Z36jE+mrjzO8gCIw0YDDyBxywuASmwrBotU4deftW8Zz9GK/QEHBRNyV5nBINZJo2J257Wlg==" }, + "node_modules/@jitl/quickjs-ffi-types": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/@jitl/quickjs-ffi-types/-/quickjs-ffi-types-0.31.0.tgz", + "integrity": "sha512-1yrgvXlmXH2oNj3eFTrkwacGJbmM0crwipA3ohCrjv52gBeDaD7PsTvFYinlAnqU8iPME3LGP437yk05a2oejw==" + }, + "node_modules/@jitl/quickjs-wasmfile-debug-asyncify": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-debug-asyncify/-/quickjs-wasmfile-debug-asyncify-0.31.0.tgz", + "integrity": "sha512-YkdzQdr1uaftFhgEnTRjTTZHk2SFZdpWO7XhOmRVbi6CEVsH9g5oNF8Ta1q3OuSJHRwwT8YsuR1YzEiEIJEk6w==", + "dependencies": { + "@jitl/quickjs-ffi-types": "0.31.0" + } + }, + "node_modules/@jitl/quickjs-wasmfile-debug-sync": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-debug-sync/-/quickjs-wasmfile-debug-sync-0.31.0.tgz", + "integrity": "sha512-8XvloaaWBONqcHXYs5tWOjdhQVxzULilIfB2hvZfS6S+fI4m2+lFiwQy7xeP8ExHmiZ7D8gZGChNkdLgjGfknw==", + "dependencies": { + "@jitl/quickjs-ffi-types": "0.31.0" + } + }, + "node_modules/@jitl/quickjs-wasmfile-release-asyncify": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-release-asyncify/-/quickjs-wasmfile-release-asyncify-0.31.0.tgz", + "integrity": "sha512-uz0BbQYTxNsFkvkurd7vk2dOg57ElTBLCuvNtRl4rgrtbC++NIndD5qv2+AXb6yXDD3Uy1O2PCwmoaH0eXgEOg==", + "dependencies": { + "@jitl/quickjs-ffi-types": "0.31.0" + } + }, + "node_modules/@jitl/quickjs-wasmfile-release-sync": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-release-sync/-/quickjs-wasmfile-release-sync-0.31.0.tgz", + "integrity": "sha512-hYduecOByj9AsAfsJhZh5nA6exokmuFC8cls39+lYmTCGY51bgjJJJwReEu7Ff7vBWaQCL6TeDdVlnp2WYz0jw==", + "dependencies": { + "@jitl/quickjs-ffi-types": "0.31.0" + } + }, "node_modules/@jridgewell/resolve-uri": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", @@ -2645,6 +2690,19 @@ "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, + "node_modules/assert": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/assert/-/assert-2.1.0.tgz", + "integrity": "sha512-eLHpSK/Y4nhMJ07gDaAzoX/XAKS8PSaojml3M0DM4JpV1LAi5JOJ/p6H/XWrl8L+DzVEvVCW1z3vWAaB9oTsQw==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.2", + "is-nan": "^1.3.2", + "object-is": "^1.1.5", + "object.assign": "^4.1.4", + "util": "^0.12.5" + } + }, "node_modules/assertion-error": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", @@ -2654,6 +2712,21 @@ "node": ">=12" } }, + "node_modules/available-typed-arrays": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz", + "integrity": "sha512-wvUjBtSGN7+7SjNpq/9M2Tg350UZD3q62IFZLbRAR1bSMlCo1ZaeW+BJ+D090e4hIIZLBcTDWe4Mh4jvUDajzQ==", + "dev": true, + "dependencies": { + "possible-typed-array-names": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2669,6 +2742,26 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/big-integer": { "version": "1.6.52", "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz", @@ -2780,6 +2873,15 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, + "node_modules/browserify-zlib": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/browserify-zlib/-/browserify-zlib-0.2.0.tgz", + "integrity": "sha512-Z942RysHXmJrhqk88FmKBVq/v5tqmSkDz7p54G/MGyjMnCFFnC79XWNbg+Vta8W6Wb2qtSZTSxIGkJrRpCFEiA==", + "dev": true, + "dependencies": { + "pako": "~1.0.5" + } + }, "node_modules/browserslist": { "version": "4.24.0", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.24.0.tgz", @@ -2812,6 +2914,30 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/buffer": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.2.1" + } + }, "node_modules/builtin-modules": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", @@ -2830,6 +2956,25 @@ "node": ">= 0.8" } }, + "node_modules/call-bind": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.7.tgz", + "integrity": "sha512-GHTSNSYICQ7scH7sZ+M2rFopRoLh8t2bLSW6BbgrtLsahOIB5iyAVJf9GjWK3cYTDaMj4XdBpM1cA6pIS0Kv2w==", + "dev": true, + "dependencies": { + "es-define-property": "^1.0.0", + "es-errors": "^1.3.0", + "function-bind": "^1.1.2", + "get-intrinsic": "^1.2.4", + "set-function-length": "^1.2.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -3389,6 +3534,40 @@ "node": ">=4.0.0" } }, + "node_modules/define-data-property": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", + "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==", + "dev": true, + "dependencies": { + "es-define-property": "^1.0.0", + "es-errors": "^1.3.0", + "gopd": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/define-properties": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.2.1.tgz", + "integrity": "sha512-8QmQKqEASLd5nx0U1B1okLElbUuuttJ/AnYmRXbbbGDWh6uS208EjD4Xqq/I9wK7u0v6O08XhTWnt5XtEbR6Dg==", + "dev": true, + "dependencies": { + "define-data-property": "^1.0.1", + "has-property-descriptors": "^1.0.0", + "object-keys": "^1.1.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/detect-libc": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", @@ -3536,6 +3715,27 @@ "is-arrayish": "^0.2.1" } }, + "node_modules/es-define-property": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz", + "integrity": "sha512-jxayLKShrEqqzJ0eumQbVhTYQM27CfT1T35+gCgDFoL82JLsXqTJ76zv6A0YLOgEnLUMvLzsDsGIrl8NFpT2gQ==", + "dev": true, + "dependencies": { + "get-intrinsic": "^1.2.4" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "dev": true, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/escalade": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", @@ -3572,6 +3772,15 @@ "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==" }, + "node_modules/events": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", + "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==", + "dev": true, + "engines": { + "node": ">=0.8.x" + } + }, "node_modules/execa": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", @@ -3638,6 +3847,15 @@ "flat": "cli.js" } }, + "node_modules/for-each": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", + "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", + "dev": true, + "dependencies": { + "is-callable": "^1.1.3" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3676,6 +3894,25 @@ "node": "6.* || 8.* || >= 10.*" } }, + "node_modules/get-intrinsic": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", + "integrity": "sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==", + "dev": true, + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2", + "has-proto": "^1.0.1", + "has-symbols": "^1.0.3", + "hasown": "^2.0.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/get-port": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/get-port/-/get-port-4.2.0.tgz", @@ -3753,6 +3990,18 @@ "jquery": "*" } }, + "node_modules/gopd": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", + "integrity": "sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==", + "dev": true, + "dependencies": { + "get-intrinsic": "^1.1.3" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -3762,6 +4011,57 @@ "node": ">=8" } }, + "node_modules/has-property-descriptors": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz", + "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==", + "dev": true, + "dependencies": { + "es-define-property": "^1.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-proto": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.3.tgz", + "integrity": "sha512-SJ1amZAJUiZS+PhsVLf5tGydlaVB8EdFpaSO4gmiUKUOxk8qzn5AIy4ZeJUmh22znIdk/uMAUT2pl3FxzVUH+Q==", + "dev": true, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", + "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==", + "dev": true, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "dev": true, + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/hasown": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", @@ -3870,6 +4170,37 @@ "node": ">=10.17.0" } }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/immediate": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", @@ -3914,6 +4245,22 @@ "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", "dev": true }, + "node_modules/is-arguments": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.1.tgz", + "integrity": "sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.2", + "has-tostringtag": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", @@ -3932,6 +4279,18 @@ "node": ">=8" } }, + "node_modules/is-callable": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz", + "integrity": "sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA==", + "dev": true, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-core-module": { "version": "2.15.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.15.1.tgz", @@ -3980,6 +4339,21 @@ "node": ">=8" } }, + "node_modules/is-generator-function": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.10.tgz", + "integrity": "sha512-jsEjy9l3yiXEQ+PsXdmBwEPcOxaXWLspKdplFUVI9vq1iZgIekeC0L167qeu86czQaxed3q/Uzuw0swL0irL8A==", + "dev": true, + "dependencies": { + "has-tostringtag": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-glob": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", @@ -3998,6 +4372,22 @@ "integrity": "sha512-6BEnpVn1rcf3ngfmViLM6vjUjGErbdrL4rwlv+u1NO1XO8kqT4YGL8+19Q+Z/bas8tY90BTWMk2+fW1g6hQjbA==", "dev": true }, + "node_modules/is-nan": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/is-nan/-/is-nan-1.3.2.tgz", + "integrity": "sha512-E+zBKpQ2t6MEo1VsonYmluk9NxGrbzpeeLC2xIViuO2EjU2xsXsBPwTr3Ykv9l08UYEVEdWeRZNouaZqF6RN0w==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.0", + "define-properties": "^1.1.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-number": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", @@ -4040,6 +4430,21 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/is-typed-array": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/is-typed-array/-/is-typed-array-1.1.13.tgz", + "integrity": "sha512-uZ25/bUAlUY5fR4OKT4rZQEBrzQWYV9ZJYGGsUmEJ6thodVJ1HX64ePQ6Z0qPWP+m+Uq6e9UugrE38jeYsDSMw==", + "dev": true, + "dependencies": { + "which-typed-array": "^1.1.14" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-unicode-supported": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", @@ -4895,6 +5300,49 @@ "integrity": "sha512-2vPPEi+Z7WqML2jZYddDIfy5Dqb0r2fze2zTxNNknZaFpVHU3mFB3R+DWeJWGVx0ecvttSGlJTI+WG+8Z4cDWw==", "dev": true }, + "node_modules/object-is": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.1.6.tgz", + "integrity": "sha512-F8cZ+KfGlSGi09lJT7/Nd6KJZ9ygtvYC0/UYYLI9nmQKLMnydpB9yvbv9K1uSkEu7FU9vYPmVwLg328tX+ot3Q==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.7", + "define-properties": "^1.2.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/object-keys": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", + "dev": true, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/object.assign": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.5.tgz", + "integrity": "sha512-byy+U7gp+FVwmyzKPYhW2h5l3crpmGsxl7X2s8y43IgxvG4g3QZ6CffDtsNQy1WsmZpQbO+ybo0AlW7TY6DcBQ==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.5", + "define-properties": "^1.2.1", + "has-symbols": "^1.0.3", + "object-keys": "^1.1.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/on-headers": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", @@ -5164,6 +5612,15 @@ } } }, + "node_modules/possible-typed-array-names": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz", + "integrity": "sha512-d7Uw+eZoloe0EHDIYoe+bQ5WXnGMOpmiZFTuMWCwpjzzkL2nTjcKiAk4hh8TjnGye2TwWOk3UXucZ+3rbmBa8Q==", + "dev": true, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/postcss": { "version": "8.4.47", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.47.tgz", @@ -5264,6 +5721,29 @@ "node": ">=6" } }, + "node_modules/quickjs-emscripten": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/quickjs-emscripten/-/quickjs-emscripten-0.31.0.tgz", + "integrity": "sha512-K7Yt78aRPLjPcqv3fIuLW1jW3pvwO21B9pmFOolsjM/57ZhdVXBr51GqJpalgBlkPu9foAvhEAuuQPnvIGvLvQ==", + "dependencies": { + "@jitl/quickjs-wasmfile-debug-asyncify": "0.31.0", + "@jitl/quickjs-wasmfile-debug-sync": "0.31.0", + "@jitl/quickjs-wasmfile-release-asyncify": "0.31.0", + "@jitl/quickjs-wasmfile-release-sync": "0.31.0", + "quickjs-emscripten-core": "0.31.0" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/quickjs-emscripten-core": { + "version": "0.31.0", + "resolved": "https://registry.npmjs.org/quickjs-emscripten-core/-/quickjs-emscripten-core-0.31.0.tgz", + "integrity": "sha512-oQz8p0SiKDBc1TC7ZBK2fr0GoSHZKA0jZIeXxsnCyCs4y32FStzCW4d1h6E1sE0uHDMbGITbk2zhNaytaoJwXQ==", + "dependencies": { + "@jitl/quickjs-ffi-types": "0.31.0" + } + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -5312,6 +5792,20 @@ "node": ">=0.10.0" } }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dev": true, + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", @@ -5416,6 +5910,11 @@ } ] }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, "node_modules/semver": { "version": "7.6.3", "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", @@ -5510,6 +6009,23 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/set-function-length": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.2.tgz", + "integrity": "sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==", + "dev": true, + "dependencies": { + "define-data-property": "^1.1.4", + "es-errors": "^1.3.0", + "function-bind": "^1.1.2", + "get-intrinsic": "^1.2.4", + "gopd": "^1.0.1", + "has-property-descriptors": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -5581,6 +6097,25 @@ "deprecated": "Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility", "dev": true }, + "node_modules/stream-browserify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-3.0.0.tgz", + "integrity": "sha512-H73RAHsVBapbim0tU2JwwOiXUj+fikfiaoYAKHF3VJfA0pe2BCzkhAHBlLG6REzE+2WNZcxOXjK7lkso+9euLA==", + "dev": true, + "dependencies": { + "inherits": "~2.0.4", + "readable-stream": "^3.5.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dev": true, + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/string-width": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-5.1.2.tgz", @@ -6009,6 +6544,25 @@ "punycode": "^2.1.0" } }, + "node_modules/util": { + "version": "0.12.5", + "resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz", + "integrity": "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==", + "dev": true, + "dependencies": { + "inherits": "^2.0.3", + "is-arguments": "^1.0.4", + "is-generator-function": "^1.0.7", + "is-typed-array": "^1.1.3", + "which-typed-array": "^1.1.2" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "dev": true + }, "node_modules/utility-types": { "version": "3.11.0", "resolved": "https://registry.npmjs.org/utility-types/-/utility-types-3.11.0.tgz", @@ -6074,6 +6628,25 @@ "node": ">= 8" } }, + "node_modules/which-typed-array": { + "version": "1.1.15", + "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.15.tgz", + "integrity": "sha512-oV0jmFtUky6CXfkqehVvBP/LSWJ2sy4vWMioiENyJLePrBO/yKyV9OyJySfAKosh+RYkIl5zJCNZ8/4JncrpdA==", + "dev": true, + "dependencies": { + "available-typed-arrays": "^1.0.7", + "call-bind": "^1.0.7", + "for-each": "^0.3.3", + "gopd": "^1.0.1", + "has-tostringtag": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/widest-line": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/widest-line/-/widest-line-4.0.1.tgz", diff --git a/package.json b/package.json index 2824c595..33e3b209 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "bowser": "^1.7.3", "dateformat": "^5.0.3", "golden-layout": "^1.5.9", + "iconv-lite": "^0.6.3", "jquery": "^3.5.0", "js-yaml": "^4.1.0", "kaitai-struct": "next", @@ -32,15 +33,21 @@ "@types/js-yaml": "^4.0.9", "@types/mocha": "^10.0.9", "@types/node": "^22.7.7", + "assert": "^2.1.0", + "browserify-zlib": "^0.2.0", + "buffer": "^6.0.3", "chai": "^5.1.1", + "events": "^3.3.0", "mocha": "^10.7.3", "parcel": "^2.12.0", "parcel-reporter-static-files-copy": "^1.5.3", "process": "^0.11.10", "serve": "^14.2.4", + "stream-browserify": "^3.0.0", "ts-node": "^10.9.2", "tslint": "^6.1.3", - "typescript": "^5.6.3" + "typescript": "^5.6.3", + "util": "^0.12.5" }, "scripts": { "test": "mocha 'test/**'", @@ -67,12 +74,6 @@ }, { "staticPath": "LICENSE.txt" - }, - { - "staticPath": "./node_modules/kaitai-struct/KaitaiStream.js" - }, - { - "staticPath": "./node_modules/pako/dist/pako_inflate.js" } ] } diff --git a/src/Components/GoldenLayout/GoldenLayoutUI.ts b/src/Components/GoldenLayout/GoldenLayoutUI.ts index 4c3d68c8..55e9bf4c 100644 --- a/src/Components/GoldenLayout/GoldenLayoutUI.ts +++ b/src/Components/GoldenLayout/GoldenLayoutUI.ts @@ -5,8 +5,9 @@ import {MonacoEditorComponent, MonacoEditorOptions} from "./MonacoEditorComponen import {mainEditorOnChange, mainEditorRecompile} from "../../GlobalActions/KsyEditorActions"; import {DelayAction} from "../../Utils/DelayAction"; import {editor, KeyCode, KeyMod} from "monaco-editor"; -import IStandaloneCodeEditor = editor.IStandaloneCodeEditor; import {parseAction} from "../../GlobalActions/ParseAction"; +import {KaitaiCodeWorkerApi} from "../../DataManipulation/ParsingModule/KaitaiCodeWorkerApi"; +import IStandaloneCodeEditor = editor.IStandaloneCodeEditor; export class GoldenLayoutUI { dynCompId = 1; @@ -25,7 +26,7 @@ export class GoldenLayoutUI { this.addMonacoCodeEditorTab("ksyEditor", {lang: "yaml"}); this.addMonacoCodeEditorTab("genCodeViewer", {lang: "javascript", isReadOnly: true}); - this.addMonacoCodeEditorTab("genCodeDebugViewer", {lang: "javascript", isReadOnly: true}); + this.addMonacoCodeEditorTab("genCodeDebugViewer", {lang: "javascript"}); this.addHexViewer("hex-viewer"); this.addExistingDiv("parsedDataTree"); @@ -108,9 +109,10 @@ export class GoldenLayoutUI { break; case "genCodeDebugViewer": { this.genCodeDebugViewer = newEditor; - newEditor.addCommand(KeyMod.CtrlCmd | KeyCode.Enter, (args) => { - parseAction(); - }, "compile"); + newEditor.addCommand(KeyMod.CtrlCmd | KeyCode.KeyK, async (args) => { + KaitaiCodeWorkerApi.setOnlyCodeAction(this.genCodeDebugViewer.getValue()); + await parseAction(); + }); } } return newEditor; diff --git a/src/Components/HexViewer/Actions/ExportToJson.ts b/src/Components/HexViewer/Actions/ExportToJson.ts index e188c525..5232c4a2 100644 --- a/src/Components/HexViewer/Actions/ExportToJson.ts +++ b/src/Components/HexViewer/Actions/ExportToJson.ts @@ -1,7 +1,7 @@ import {IWorkerParsedResponse} from "../../../DataManipulation/ParsingModule/CodeExecution/Types"; import {IExportedValue} from "../../../DataManipulation/ExportedValueTypes"; import {ExportedValueMappers} from "../../../DataManipulation/ExportedValueMappers"; -import {codeExecutionWorkerApi} from "../../../DataManipulation/ParsingModule/ParseWorkerApi"; +import {KaitaiCodeWorkerApi} from "../../../DataManipulation/ParsingModule/KaitaiCodeWorkerApi"; export const exportToJson = async (useHexForNumbers: boolean = false): Promise => { const overrideDefaultNumbersWithHex = (key: string, value: any) => { @@ -22,7 +22,7 @@ export const exportToJson = async (useHexForNumbers: boolean = false): Promise { :toggle="ideSettingsStore.setEagerMode" text="Eager parsing mode"/> + diff --git a/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorker.ts b/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorker.ts index d43a3d71..3ad3c51e 100644 --- a/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorker.ts +++ b/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorker.ts @@ -1,15 +1,21 @@ -declare function importScripts(...urls: string[]): void; +import {IWorkerMessage, IWorkerMessageParse} from "./WorkerMessages"; +import {IWorkerResponse, ParseResultType} from "./WorkerResponses"; +import KaitaiStream from "kaitai-struct/KaitaiStream"; +import {EvaluatedClass, PARSE_SCRIPTS} from "./Types"; -const myself = self; +// @ts-ignore - It's required for evaluating module code, without this line, eval throws error: +// TypeError: KaitaiStream is undefined +self.KaitaiStream = KaitaiStream; -const evaluateCodeToCreateMainClass = (sourceCode: string, mainClassName: string) => { - let evaluatedMainClass = undefined; + +const evaluateCodeToCreateMainClass = (sourceCode: any, mainClassName: string): typeof EvaluatedClass => { + let evaluatedMainClass: typeof EvaluatedClass = undefined; eval(`${sourceCode} evaluatedMainClass = ${mainClassName}.${mainClassName};`); return evaluatedMainClass; }; -const extractEnumsFromMainClass = (MainClass): any => { +const extractEnumsFromMainClass = (MainClass: typeof EvaluatedClass): any => { const enums = JSON.stringify({...MainClass}); const mainClassName = MainClass.name; return { @@ -17,14 +23,14 @@ const extractEnumsFromMainClass = (MainClass): any => { }; }; -const parseObjectUsingMainClass = (MainClass, inputBuffer) => { +const parseObjectUsingMainClass = (MainClass: typeof EvaluatedClass, inputBuffer: ArrayBuffer) => { const ioInput = new KaitaiStream(inputBuffer, 0); let parseResult = new MainClass(ioInput); try { parseResult._read(); return {root: parseResult}; - } catch (error: Error) { + } catch (error) { return { root: parseResult, error: error @@ -32,24 +38,6 @@ const parseObjectUsingMainClass = (MainClass, inputBuffer) => { } }; -const INIT_WORKER_SCRIPTS = "INIT_WORKER_SCRIPTS"; -const PARSE_SCRIPTS = "PARSE_SCRIPTS"; - - -let scriptsInitialized = false; -const initWorkerScripts = ({scripts}: IWorkerMessageInit) => { - if (scriptsInitialized) return; - console.log("Initializing scripts for worker", scripts); - - importScripts(scripts.kaitaiStream); - (KaitaiStream as any).depUrls = (KaitaiStream as any).depUrls || {}; - (KaitaiStream as any).depUrls.zlib = scripts.zlib; - scriptsInitialized = true; - - myself.postMessage({ - type: INIT_WORKER_SCRIPTS - }); -}; const parseScripts = (msg: IWorkerMessageParse) => { const MainClass = evaluateCodeToCreateMainClass(msg.sourceCode, msg.mainClass); @@ -59,22 +47,18 @@ const parseScripts = (msg: IWorkerMessageParse) => { const response: IWorkerResponse = { type: PARSE_SCRIPTS, error: error, - resultObject: root, + resultObject: root as unknown as ParseResultType, msgId: msg.msgId, eagerMode: msg.eagerMode, enums: enums }; - myself.postMessage(response); + self.postMessage(response); }; -myself.onmessage = (ev: MessageEvent) => { +self.onmessage = (ev: MessageEvent) => { const msg = ev.data; switch (msg.type) { - case INIT_WORKER_SCRIPTS: { - initWorkerScripts(msg as IWorkerMessageInit); - return; - } case PARSE_SCRIPTS: { parseScripts(msg as IWorkerMessageParse); return; diff --git a/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorkerWrapper.ts b/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorkerWrapper.ts index 6ba14aa7..8c13eb52 100644 --- a/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorkerWrapper.ts +++ b/src/DataManipulation/ParsingModule/CodeExecution/KaitaiCodeWorkerWrapper.ts @@ -2,8 +2,8 @@ import {fetchInstance, IParsingOptions, mapObjectToExportedValue} from "../../Ex import {IExportedValue} from "../../ExportedValueTypes"; import {WorkerFunctionStack} from "./WorkerFunctionStack"; import {IWorkerMessage, IWorkerMessageParse} from "./WorkerMessages"; -import {IWorkerResponse, IWorkerResponseInit, IWorkerResponseParse, ParseResultType} from "./WorkerResponses"; -import {IKsyTypes, IWorkerApiMethods, PARSE_SCRIPTS, INIT_WORKER_SCRIPTS, IWorkerParsedResponse} from "./Types"; +import {IWorkerResponse, IWorkerResponseParse, ParseResultObject, ParseResultType} from "./WorkerResponses"; +import {IKsyTypes, IWorkerApiMethods, IWorkerParsedResponse, PARSE_SCRIPTS} from "./Types"; export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { stack: WorkerFunctionStack; @@ -18,7 +18,11 @@ export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { constructor(stack: WorkerFunctionStack, worker: Worker) { this.stack = stack; this.worker = worker; - worker.onmessage = this.onMessageReceive; + worker.onmessage = this.onMessageReceive.bind(this); + } + + setOnlyCodeAction = (sourceCode: string): void => { + this.sourceCode = sourceCode; } setCodeAction = (sourceCode: string, mainClassName: string, ksyTypes: IKsyTypes): void => { @@ -41,9 +45,9 @@ export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { } getPropertyByPathAction = (path: string[]): Promise => { - let parent = this.root; + let parent = this.root as ParseResultObject; const parentPath = path.slice(0, -1); - parentPath.forEach(key => parent = parent[key]); + parentPath.forEach(key => parent = parent[key] as ParseResultObject); const propName = path[path.length - 1]; const property = fetchInstance(parent, propName, parentPath, false); return Promise.resolve(property); @@ -57,10 +61,6 @@ export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { onMessageReceive = (ev: MessageEvent): void => { const msg = ev.data; switch (msg.type) { - case INIT_WORKER_SCRIPTS: { - this.onMessageReceiveInit(msg as IWorkerResponseInit); - return; - } case PARSE_SCRIPTS: { this.onMessageReceiveParse(msg as IWorkerResponseParse); return; @@ -68,37 +68,34 @@ export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { } } - private onMessageReceiveInit = (msg: IWorkerResponseInit) => { - console.log("[KaitaiCodeWorkerWrapper][RESPONSE]", msg); - } - private onMessageReceiveParse = (msg: IWorkerResponseParse) => { console.log("[KaitaiCodeWorkerWrapper][RESPONSE]", msg); const parseResponsePromise = this.stack.removeFunctionFromStack(msg.msgId); - if(!!parseResponsePromise) { + if (!!parseResponsePromise) { parseResponsePromise(msg); } } private digestResponseFromWorker = (response: IWorkerResponseParse, - resolve: (value?: (PromiseLike | IWorkerParsedResponse)) => void, - reject: (reason?: any) => void) => { + resolve: (value?: (PromiseLike | IWorkerParsedResponse)) => void, + reject: (reason?: any) => void) => { let {error, resultObject, eagerMode, enums} = response; const parsingOptions = this.prepareParsingOptions(eagerMode, enums, error); - this.root = {...resultObject}; + this.root = {...resultObject as ParseResultObject}; this.exported = mapObjectToExportedValue(resultObject, parsingOptions); - error && console.log("[KaitaiCodeWorkerWrapper] Error: ", error); + if (error) { + console.log("[KaitaiCodeWorkerWrapper] Error: ", error); + } if (error && (this.exported === undefined || this.exported === null)) { reject(error); } else { - const response: IWorkerParsedResponse = {resultObject: this.exported, error}; - resolve(response); + resolve({resultObject: this.exported, error}); } - }; + } prepareIWorkerMessageParse = (msgId: number, eagerMode: boolean): IWorkerMessageParse => { return { @@ -110,7 +107,7 @@ export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { ksyTypes: this.ksyTypes, inputBuffer: this.inputBuffer, }; - }; + } prepareParsingOptions = (eagerMode: boolean, enums: any, error: Error): IParsingOptions => { return { @@ -120,5 +117,6 @@ export class KaitaiCodeWorkerWrapper implements IWorkerApiMethods { incomplete: !!error, streamLength: this.inputBuffer.byteLength, }; - }; + } + } \ No newline at end of file diff --git a/src/DataManipulation/ParsingModule/CodeExecution/Types.ts b/src/DataManipulation/ParsingModule/CodeExecution/Types.ts index 14f3a8bb..73281702 100644 --- a/src/DataManipulation/ParsingModule/CodeExecution/Types.ts +++ b/src/DataManipulation/ParsingModule/CodeExecution/Types.ts @@ -1,7 +1,7 @@ -import {IExportedValue} from "../../ExportedValueTypes"; +import {IExportedObject, IExportedValue} from "../../ExportedValueTypes"; +import KaitaiStream from "kaitai-struct/KaitaiStream"; -export const INIT_WORKER_SCRIPTS = "INIT_WORKER_SCRIPTS"; export const PARSE_SCRIPTS = "PARSE_SCRIPTS"; @@ -14,7 +14,21 @@ export interface IKsyTypes { [name: string]: KsySchema.IType; } +export class EvaluatedClass { + + constructor(public kaitaiStream: KaitaiStream) { + } + + _read(): IExportedObject { + return {}; + } + + name: string; +} + export interface IWorkerApiMethods { + setOnlyCodeAction(sourceCode: string): void; + setCodeAction(sourceCode: string, mainClassName: string, ksyTypes: IKsyTypes): void; setInputAction(inputBuffer: ArrayBuffer): void; diff --git a/src/DataManipulation/ParsingModule/CodeExecution/WorkerFunctionStack.ts b/src/DataManipulation/ParsingModule/CodeExecution/WorkerFunctionStack.ts index a7277347..93b71440 100644 --- a/src/DataManipulation/ParsingModule/CodeExecution/WorkerFunctionStack.ts +++ b/src/DataManipulation/ParsingModule/CodeExecution/WorkerFunctionStack.ts @@ -4,18 +4,18 @@ export class WorkerFunctionStack { msgHandlers: { [msgId: number]: (msg: IWorkerResponse) => void } = {}; lastMsgId = 0; - pushFunctionToStack = (parseResponseFromWorker: (response: IWorkerResponseParse) => void): number => { + pushFunctionToStack(parseResponseFromWorker: (response: IWorkerResponseParse) => void): number { const msgId = ++this.lastMsgId; this.msgHandlers[msgId] = parseResponseFromWorker; return msgId; - }; + } - removeFunctionFromStack = (msgId: number) => { + removeFunctionFromStack(msgId: number) { if (!this.msgHandlers[msgId]) { return null; } const parseResponseMethod = this.msgHandlers[msgId]; delete this.msgHandlers[msgId]; return parseResponseMethod; - }; + } } \ No newline at end of file diff --git a/src/DataManipulation/ParsingModule/CodeExecution/WorkerMessages.ts b/src/DataManipulation/ParsingModule/CodeExecution/WorkerMessages.ts index ac383bdf..c47909da 100644 --- a/src/DataManipulation/ParsingModule/CodeExecution/WorkerMessages.ts +++ b/src/DataManipulation/ParsingModule/CodeExecution/WorkerMessages.ts @@ -1,15 +1,10 @@ -import {IKsyTypes, INIT_WORKER_SCRIPTS, PARSE_SCRIPTS} from "./Types"; +import {IKsyTypes} from "./Types"; + +const PARSE_SCRIPTS = "PARSE_SCRIPTS"; -export interface IWorkerMessageInit { - type: typeof INIT_WORKER_SCRIPTS; - scripts: { - kaitaiStream: string; - zlib: string; - }; -} export interface IWorkerMessageParse { type: typeof PARSE_SCRIPTS; - sourceCode: string; + sourceCode: any; mainClass: string; ksyTypes: IKsyTypes; inputBuffer: ArrayBuffer; @@ -18,4 +13,4 @@ export interface IWorkerMessageParse { msgId: number; } -export type IWorkerMessage = IWorkerMessageInit | IWorkerMessageParse +export type IWorkerMessage = IWorkerMessageParse; diff --git a/src/DataManipulation/ParsingModule/CodeExecution/WorkerResponses.ts b/src/DataManipulation/ParsingModule/CodeExecution/WorkerResponses.ts index 65f0daa5..cae4fe08 100644 --- a/src/DataManipulation/ParsingModule/CodeExecution/WorkerResponses.ts +++ b/src/DataManipulation/ParsingModule/CodeExecution/WorkerResponses.ts @@ -1,16 +1,6 @@ -import {INIT_WORKER_SCRIPTS, PARSE_SCRIPTS} from "./Types"; +import {PARSE_SCRIPTS} from "./Types"; import {IDebugInfo} from "../../ExportedValueMappers/ObjectToIExportedValueMapper"; -export interface IWorkerResponseInit { - type: typeof INIT_WORKER_SCRIPTS; - - resultObject: any; - enums: any; - error: Error; - - eagerMode: boolean; - msgId: number; -} export interface DebugMap { [key: string]: IDebugInfo; @@ -21,6 +11,7 @@ export interface ParseResultObject { _root: ParseResultObject; _parent?: ParseResultObject; _io: any; + [name: string]: ParseResultType; } @@ -37,4 +28,4 @@ export interface IWorkerResponseParse { msgId: number; } -export type IWorkerResponse = IWorkerResponseInit | IWorkerResponseParse; +export type IWorkerResponse = IWorkerResponseParse; diff --git a/src/DataManipulation/ParsingModule/KaitaiCodeWorkerApi.ts b/src/DataManipulation/ParsingModule/KaitaiCodeWorkerApi.ts new file mode 100644 index 00000000..4ec6dccf --- /dev/null +++ b/src/DataManipulation/ParsingModule/KaitaiCodeWorkerApi.ts @@ -0,0 +1,6 @@ +import {KaitaiCodeWorkerWrapper} from "./CodeExecution/KaitaiCodeWorkerWrapper"; +import {WorkerFunctionStack} from "./CodeExecution/WorkerFunctionStack"; + +const codeExecutorWorker = new Worker(new URL("data-url:./CodeExecution/KaitaiCodeWorker.ts", import.meta.url), {type: "module"}); +const workerFunctionStack = new WorkerFunctionStack(); +export const KaitaiCodeWorkerApi = new KaitaiCodeWorkerWrapper(workerFunctionStack, codeExecutorWorker); \ No newline at end of file diff --git a/src/DataManipulation/ParsingModule/ParseWorkerApi.ts b/src/DataManipulation/ParsingModule/ParseWorkerApi.ts deleted file mode 100644 index 8a786cb6..00000000 --- a/src/DataManipulation/ParsingModule/ParseWorkerApi.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {KaitaiCodeWorkerWrapper} from "./CodeExecution/KaitaiCodeWorkerWrapper"; -import {WorkerFunctionStack} from "./CodeExecution/WorkerFunctionStack"; -import {IWorkerMessageInit} from "./CodeExecution/WorkerMessages"; -import {INIT_WORKER_SCRIPTS, IWorkerApiMethods} from "./CodeExecution/Types"; - -const codeExecutorWorker = new Worker(new URL("data-url:./CodeExecution/KaitaiCodeWorker.ts", import.meta.url)); -const workerFunctionStack = new WorkerFunctionStack(); -const workerWrapper = new KaitaiCodeWorkerWrapper(workerFunctionStack, codeExecutorWorker); - -const initScriptsMessage: IWorkerMessageInit = { - type: INIT_WORKER_SCRIPTS, - scripts: { - kaitaiStream: `${location.protocol}//${location.host}/KaitaiStream.js`, - zlib: `${location.protocol}//${location.host}/pako_inflate.js` - } -}; -workerWrapper.sendMessageToWorker(initScriptsMessage); - -export const codeExecutionWorkerApi: IWorkerApiMethods = { - setCodeAction: workerWrapper.setCodeAction, - setInputAction: workerWrapper.setInputAction, - parseAction: workerWrapper.parseAction, - getPropertyByPathAction: workerWrapper.getPropertyByPathAction -}; \ No newline at end of file diff --git a/src/GlobalActions/CompileGrammar.ts b/src/GlobalActions/CompileGrammar.ts index cf6116bb..30148b47 100644 --- a/src/GlobalActions/CompileGrammar.ts +++ b/src/GlobalActions/CompileGrammar.ts @@ -1,12 +1,35 @@ import {YamlFileInfo} from "../DataManipulation/CompilationModule/JsImporter"; -import {CompilerService} from "../DataManipulation/CompilationModule/CompilerService"; -import {codeExecutionWorkerApi} from "../DataManipulation/ParsingModule/ParseWorkerApi"; +import {CompilerService, KaitaiCompilationResult} from "../DataManipulation/CompilationModule/CompilerService"; import {CurrentGoldenLayout} from "../Components/GoldenLayout/GoldenLayoutUI"; +import {KaitaiCodeWorkerApi} from "../DataManipulation/ParsingModule/KaitaiCodeWorkerApi"; +import {useIdeSettingsStore} from "../Stores/IdeSettingsStore"; + +/** + * After switching to Worker being a module instead of plain JS file it fulfils commonJS module requirements: + * + * else if (typeof exports === 'object' && exports !== null && typeof exports.nodeType !== 'number') { + * factory(exports, require('kaitai-struct/KaitaiStream')); + * + * Those two lines need to be removed for Worker to function properly. + */ +const prepareDebugCodeForWorkerAsModule = (debug: KaitaiCompilationResult) => { + const store = useIdeSettingsStore(); + const startingIndexOfCommonJSModulesCheck = 5; + + return !store.removeCommonJsHeader + ? Object.values(debug).join("") + : Object.values(debug) + .map((content: string) => { + let splitContent = content.split("\n"); + splitContent.splice(startingIndexOfCommonJSModulesCheck, 2); + return splitContent.join("\n"); + }).join(""); +}; export const compileInternalDebugAndRelease = async (yamlInfo: YamlFileInfo) => { const compiledGrammar = await new CompilerService().compileDebugAndReleaseTargets(yamlInfo, "javascript"); - const debugCode = Object.values(compiledGrammar.debug).join(""); + const debugCode = prepareDebugCodeForWorkerAsModule(compiledGrammar.debug); const releaseCode = Object.values(compiledGrammar.release).join(""); CurrentGoldenLayout.updateReleaseAndDebugCodeTabs(debugCode, releaseCode); - codeExecutionWorkerApi.setCodeAction(debugCode, compiledGrammar.jsMainClassName, compiledGrammar.ksyTypes); + KaitaiCodeWorkerApi.setCodeAction(debugCode, compiledGrammar.jsMainClassName, compiledGrammar.ksyTypes); }; \ No newline at end of file diff --git a/src/GlobalActions/LoadBinaryFile.ts b/src/GlobalActions/LoadBinaryFile.ts index fa6cfa2d..354d8f27 100644 --- a/src/GlobalActions/LoadBinaryFile.ts +++ b/src/GlobalActions/LoadBinaryFile.ts @@ -1,4 +1,4 @@ -import {codeExecutionWorkerApi} from "../DataManipulation/ParsingModule/ParseWorkerApi"; +import {KaitaiCodeWorkerApi} from "../DataManipulation/ParsingModule/KaitaiCodeWorkerApi"; import {useCurrentBinaryFileStore} from "../Stores/CurrentBinaryFileStore"; import {useFileSystems} from "../Components/FileTree/Store/FileSystemsStore"; import {CurrentGoldenLayout} from "../Components/GoldenLayout/GoldenLayoutUI"; @@ -9,5 +9,5 @@ export const loadBinaryFileAction = async (binaryFileLocation: FileSystemPath) = const fileContent = await useFileSystems().getFile(binaryFileLocation.storeId, binaryFileLocation.path) as ArrayBuffer; CurrentGoldenLayout.updateHexViewerTitle(binaryFileLocation.path); store.newBinaryFileSelected(binaryFileLocation.path, fileContent, "SOURCE_REPARSE"); - codeExecutionWorkerApi.setInputAction(fileContent); + KaitaiCodeWorkerApi.setInputAction(fileContent); }; \ No newline at end of file diff --git a/src/GlobalActions/ParseAction.ts b/src/GlobalActions/ParseAction.ts index ee51a7c9..b25448bd 100644 --- a/src/GlobalActions/ParseAction.ts +++ b/src/GlobalActions/ParseAction.ts @@ -1,10 +1,10 @@ import {useCurrentBinaryFileStore} from "../Stores/CurrentBinaryFileStore"; import {useIdeSettingsStore} from "../Stores/IdeSettingsStore"; -import {codeExecutionWorkerApi} from "../DataManipulation/ParsingModule/ParseWorkerApi"; +import {KaitaiCodeWorkerApi} from "../DataManipulation/ParsingModule/KaitaiCodeWorkerApi"; export const parseAction = async () => { const store = useCurrentBinaryFileStore(); const ideSettingsStore = useIdeSettingsStore(); - const {resultObject: exportedRoot, error: parseError} = await codeExecutionWorkerApi.parseAction(ideSettingsStore.eagerMode); + const {resultObject: exportedRoot, error: parseError} = await KaitaiCodeWorkerApi.parseAction(ideSettingsStore.eagerMode); store.updateParsedFile(exportedRoot); }; \ No newline at end of file diff --git a/src/Stores/IdeSettingsStore.ts b/src/Stores/IdeSettingsStore.ts index 868afe7c..e876480e 100644 --- a/src/Stores/IdeSettingsStore.ts +++ b/src/Stores/IdeSettingsStore.ts @@ -4,6 +4,7 @@ import {LocalStorageApi} from "../Utils/LocalStorageApi"; export interface IdeSettings { eagerMode: boolean; generateOnlyMainFile: boolean; + removeCommonJsHeader: boolean; } export const useIdeSettingsStore = defineStore("IDESettingsStore", { @@ -11,7 +12,8 @@ export const useIdeSettingsStore = defineStore("IDESettingsStore", { const settings = LocalStorageApi.getIdeSettings(); return settings || { eagerMode: true, - generateOnlyMainFile: false + generateOnlyMainFile: false, + removeCommonJsHeader: true }; }, actions: { @@ -22,6 +24,10 @@ export const useIdeSettingsStore = defineStore("IDESettingsStore", { setGenerateOnlyMainFile(generateOnlyMainFile: boolean) { this.generateOnlyMainFile = generateOnlyMainFile; LocalStorageApi.storeIdeSettings(this); + }, + setRemoveCommonJsHeader(removeCommonJsHeader: boolean) { + this.removeCommonJsHeader = removeCommonJsHeader; + LocalStorageApi.storeIdeSettings(this); } } }); \ No newline at end of file diff --git a/src/Utils/LocalStorageApi.ts b/src/Utils/LocalStorageApi.ts index fe1cf11f..21a5bb62 100644 --- a/src/Utils/LocalStorageApi.ts +++ b/src/Utils/LocalStorageApi.ts @@ -31,7 +31,8 @@ export class LocalStorageApi { public static storeIdeSettings = (store: IdeSettings): void => { const state: IdeSettings = { eagerMode: store.eagerMode, - generateOnlyMainFile: store.generateOnlyMainFile + generateOnlyMainFile: store.generateOnlyMainFile, + removeCommonJsHeader: store.removeCommonJsHeader }; localStorage.setItem("ideSettings", JSON.stringify(state)); }