Strange inline JS assembler issue

Issue #116 resolved
Christian Budde created an issue

The below code compiles fine on the first run:

var Console external 'console': Variant;

{ This is a comment }

(*
function StringReplace(s,o,n: String): String;
begin
  asm
    var r = o.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
    return @o?@s.replace(new RegExp(r, "g"), @n):@s;
  end;
end;

Console.Log(StringReplace('Test', 't', 'a');
*)

However, if you uncomment the comments "(" and ")" it fails (while it probably should, but that's another story).

The strange thing happens if you then comment the same piece of code again (by placing the comment identifiers to the original location again). Now the code should look identical to the initial code, which compiled fine. But unfortunately it doesn't. Instead it throws an error at the very first comment:

#!

Unknown name "{"

It seems like the compiler is still somehow in a different parsing mode. This is of course only in case the same compiler is used again to compile the very same code.

Comments (2)

  1. Eric Grange repo owner

    Diff for previous commit is a bit messy, but a try..finally was basically missing around the parsing mode changes.

    As for the RegExp not compiling, it is because the asm sections just use a "tweaked" Pascal parser, and RegExp literals are not quite supported by it (workaround right now is to use "new RegExp" form instead)

  2. Log in to comment