kaitai_struct_webide icon indicating copy to clipboard operation
kaitai_struct_webide copied to clipboard

Add ability to inject custom JS code for processors

Open Mingun opened this issue 1 year ago • 2 comments

I have a KSY that needs to decode some field from HEX before parsing it, something like this:

seq:
  - id: hex_encoded_field
    process: hex
    terminator: 0x1C # FS

Unfortunately, there is strange obstacles of making that very common algorithm would be available in the core, and WebIDE produces an error:

Parse error (TypeError): _process.decode is not a function
Call stack: 

The corresponding code from "JS code" tab does the follow:

var _process = new Hex();
this.hex_encoded_field = _process.decode(this._raw_hex_encoded_field);

so I tried to implement necessary class in browser console by monkey-patching. I've open a https://ide.kaitai.io/# in Firefox, open console and put the following code to it:

class Hex {
  decode(bytes) {
    let result = Array(bytes.length / 2);
    for (let i = 0; i < bytes.length; i+=2) {
      let byte = (bytes[i] - 0x30) * 10 + bytes[i+1] - 0x30;
      result[i/2] = byte;
    }
    return result;
  }
}

but webide still does not see it.

Is it possible to add a new window to the IDE with the ability to enter JS code that would be injected to parser? And until this would be implemented provide a workaround, how I can inject code right now.

Mingun avatar Oct 26 '22 07:10 Mingun