Skip to content

Commit

Permalink
Fix temporary error when imports are used, support circular imports
Browse files Browse the repository at this point in the history
Fixes #59

Fixes #159 -
this is a particularly notable instance of the bug, since `TypeError:
DosDatetime is not a constructor` is an error that initially everyone
gets when they open the Web IDE in a fresh browser (this is because
`zip.ksy` is selected by default, and it is affected by the bug since it
contains an import).

See kaitai-io/kaitai_struct#1074 and
kaitai-io/kaitai_struct_compiler#264 - the
actual fix was done in the KSC-generated JS code, which was changed to
allow interdependent format modules to be loaded (and added to the
global context in the case of the Web IDE) in any order. However, this
was a backwards-incompatible change, so this set of Web IDE changes just
updates our code to work with the new compiler.
  • Loading branch information
generalmimon committed Feb 21, 2024
1 parent 00c6083 commit 12d4036
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/v1/kaitaiWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ function exportValue(obj: any, debug: IDebugInfo, path: string[], noLazy?: boole
if (debug && debug.enumName) {
result.enumName = debug.enumName;
var enumObj = myself;
debug.enumName.split(".").forEach(p => enumObj = enumObj[p]);
var enumPath = debug.enumName.split(".");

// Because of https://github.com/kaitai-io/kaitai_struct/issues/1074,
// KSC-generated modules export a plain object (not the constructor
// function directly) - for example, the exported value from the
// `Zip.js` module is `{ Zip: function (_io, _parent, _root) {...} }`.
//
// This means we have to use the top-level name twice in the path resolution.
enumPath.unshift(enumPath[0]);
enumPath.forEach(p => enumObj = enumObj[p]);

var flagCheck = 0, flagSuccess = true;
var flagStr = Object.keys(enumObj).filter(x => isNaN(<any>x)).filter(x => {
Expand Down Expand Up @@ -114,7 +123,7 @@ importScripts("../../lib/_npm/kaitai-struct/KaitaiStream.js");
var apiMethods = {
initCode: (sourceCode: string, mainClassName: string, ksyTypes: IKsyTypes) => {
wi.ksyTypes = ksyTypes;
eval(`${sourceCode}\nwi.MainClass = ${mainClassName};`);
eval(`${sourceCode}\nwi.MainClass = ${mainClassName}.${mainClassName};`);
},
setInput: (inputBuffer: ArrayBuffer) => wi.inputBuffer = inputBuffer,
reparse: (eagerMode: boolean) => {
Expand Down Expand Up @@ -156,4 +165,4 @@ myself.onmessage = (ev: MessageEvent) => {

//console.log("[Worker] Send response", msg, ev);
myself.postMessage(msg);
};
};

0 comments on commit 12d4036

Please sign in to comment.