Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to inject custom JS code for processors #148

Open
Mingun opened this issue Oct 26, 2022 · 2 comments
Open

Add ability to inject custom JS code for processors #148

Mingun opened this issue Oct 26, 2022 · 2 comments

Comments

@Mingun
Copy link

Mingun commented Oct 26, 2022

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.

@generalmimon
Copy link
Member

@Mingun:

I've open a https://ide.kaitai.io/# in Firefox, open console and put the following code to it:

(...)

but webide still does not see it.

You can paste your custom type to the JS code (debug) tab before all generated code there and then hit Ctrl+Enter while your cursor is still in that tab. However, when you touch your source .ksy, the code in the JS code (debug) tab will be regenerated and the changes you made there will be lost.

Therefore, Web IDE has a feature for accepting custom code (intended exactly for opaque type and custom processor definitions) that will be injected at the beginning of JS code (debug) tab after every .ksy recompilation - there is even documentation for it: https://github.com/kaitai-io/kaitai_struct_webide/wiki/Features#opaque-type-support

Is it possible to add a new window to the IDE with the ability to enter JS code that would be injected to parser?

I guess that's the idea in the long run, but to me, the current situation is already good enough - there's an easy workaround for this fairly niche feature, which is documented. So quite low priority for me - nice to have, but at least a few dozens of things come before this.

@Mingun
Copy link
Author

Mingun commented Oct 26, 2022

So quite low priority for me - nice to have, but at least a few dozens of things come before this.

I can try to look at this myself if

  • someone give me some hints how to add a new input window (I thought about of tab in KSY area) -- where I should start look
  • I would be sure that PR will be reviewed and accepted

My concrete use-case was solved by this method: open browser console and execute this code:

localStorage.setItem("userTypes", `class Hex {
  decode(bytes) {
    let result = Array(bytes.length / 2);
    for (let i = 0; i < bytes.length; i += 2) {
      let hi = bytes[i    ] - 0x30;
      let lo = bytes[i + 1] - 0x30;
      if (hi > 9) hi -= 7;
      if (lo > 9) lo -= 7;
      let byte = hi * 16 + lo;
      result[i/2] = byte;
    }
    return Int8Array.from(result);
  }
}
this.Hex = Hex;`)

Note: if you have a type named hex in your KSY, export thing under a different name:

this.HexDecoder = Hex;

(use as process: hex_decoder), otherwise you will get a cryptic error

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants