Error "Unexpected token pair" in JavaScript code

I want to parse some nasty Mork data from Thunderbird. I found Javascript class and want to use the code in JavaScriptEngineMBS. However, my JavaScript is a bit basic and I can’t make sense of the error I get.

The line “for (let pair of pairs)” make an error “unexpected token pair”. As far as I understand the code “var pairs” makes an array with split. The “for (let pair of pairs)” should be something like an “for each pair in pairs” in Xojo.

  function mork_parse_value_table(section, val_part) {
    if (!val_part) {
      return { };
    }

    //Extract pairs (key=value)
    var pairs = val_part.split(/\(([^\)]+)\)/g);
     
    for (let pair of pairs) {
      //Skip empty line
      if (pair.trim().length == 0) {
        continue;
      }
      
      var m = /([\dA-F]*)[\t\n ]*=[\t\n ]*([\S\s]*)/gi.exec(pair);
      if (!m) {
        continue;
      }
      
      key = m[1];
      val = m[2];
      
      if (!val || val.trim().length == 0) {
        //console.error(section + ": unparsable value: " + pair);
        //continue;
      }
      
      //Approximate wchar_t -> ASCII and remove NULs
      //val = mork_fix_encoding(val);
      
      self.val_table[key] = val;
    }
      
    return self.val_table;
  }

What do I need to change?

Some old javascript engines don’t handle “let”. You can try replacing it with “var”. However, old javascript engines may also not support the “for…of” construct.

You could rewrite it like this:

    //Extract pairs (key=value)
    var pairs = val_part.split(/\(([^\)]+)\)/g);
    for (let pair of pairs) {
       ...

    //Extract pairs (key=value)
    var pairs = val_part.split(/\(([^\)]+)\)/g);
    for (var i = 0; i < pairs.length ; i++) {
      var pair = pairs[i];
       ...

JavaScriptEngineMBS uses DukTape, which is barely passable as a JavaScript engine. It has only “partial” support for ES6, which means most modern JavaScript won’t run in it.

1 Like

@Mike_D : I’ve tried the var instead of let but this didn’t work either. So I’ll try the lovely C looking code next.

@Thom_McGrath : the problem is that I don’t know Mork at all. The code mostly uses Regex all over the place but isn’t that complicated.

Yeah I get that. My point was you’re expecting too much from JavaScriptEngineMBS. You’ll need to find another solution.

Blech, the other solution I found was in Ruby.

Interesting - one of my products uses the HTMLViewer under Windows, which (if you set it up properly) supports IE11. As long as you are careful to avoid “newfangled” ES6 ideas, the IE11 engine is solid.

Sound like this DukTape thing is even worse though :sob:

That code looks pretty straightforward to translate into Xojo, as long as you get the RegEx translated correctly.

“it should be easy” :smiley:

Well, if JavaScriptEngineMBS class doesn’t do enough for you, maybe check JSContextMBS class instead. That is the engine from WebKit, which can run cross platform, even on Windows.

2 Likes

Thank you, I wasn’t even aware this class existed.

Why is this commented?

Good question. It’s this way in the original code. Gotta ask the author.