kaitai-io / kaitai_struct_javascript_runtime

Kaitai Struct: runtime for JavaScript
Apache License 2.0
37 stars 22 forks source link

transitionally support js to be loaded as browser module #28

Closed fuweichin closed 2 years ago

fuweichin commented 2 years ago

Propblem:

Error loading KaitaiStream.js or any compiled output js as browser module.

Steps to Reproduce:

  1. Load KaitaiStream.js and some format js (e,g, Ico.js, compiled with kaitai-struct-compiler for Windows) as browser module, like this
import './node_modules/kaitai-struct/KaitaiStream.js'; /* global KaitaiStream */
import './formats/Ico.js'; /* global Ico */

Expected Result:

No loading error, exported variables can be accessed through the global object.

Actual Result:

KaitaiStream.js:9 Uncaught TypeError: Cannot set properties of undefined (setting 'KaitaiStream') at KaitaiStream.js:9:23 at KaitaiStream.js:11:2 Uncaught TypeError: Cannot read properties of undefined (reading 'KaitaiStream') at Ico.js:9:29 at Ico.js:11:2

Additional Information:

By using official latest UMD template like returnExportsGlobal.js this problem can be solved.

Currently I get over it by modifying js code (the UMD wrapper part), replacing this with typeof self === 'object' ? self : this.

generalmimon commented 2 years ago

Thanks for reporting!

Error loading (...) any compiled output js

By using official latest UMD template like returnExportsGlobal.js this problem can be solved.

Currently I get over it by modifying js code (the UMD wrapper part), replacing this with typeof self === 'object' ? self : this.

In fact, the development "unstable" compiler version already includes that modification in the generated JS for a while (https://github.com/kaitai-io/kaitai_struct_compiler/commit/6809f29e1d62ff8aa3036ec23093fdf0515dcd96), but I wasn't quite sure that it was actually needed, so it's really good that you confirm this. I'll make the same change here in the runtime library.