The following snippet: JSGlobalContextRef context = JSGlobalContextCreateInGroup(NULL, NULL); const char *script = "if (0)"; JSValueRef val = JSEvaluateScript(context, JSStringCreateWithUTF8CString(script), NULL, NULL, 1, NULL); JSStringRef str = JSValueToStringCopy(context, val, NULL); char buf[256]; JSStringGetUTF8CString(str, buf, 256); printf("%s\n", buf); prints "undefined". ECMA-262 5th ed, section 7.9.1 "Rules of automatic semicolon", states: "When, as the program is parsed from left to right, the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete ECMAScript Program, then a semicolon is automatically inserted at the end of the input stream." So far, so good. But then the above is followed by: "However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement [...]." When the program is "if (0)", as in the above snippet, per the above rule, a semicolon should _not_ automatically be inserted. Instead a SyntaxError should be thrown.
This isn't automatic semicolon insertion for the usual reasons eg. { if (0) } will fail to parse. The parse success is due to the lexer automatically inserting a semicolon to the end of a script if it has not seen a line terminator when it reaches the end. I'm no sure what the specific reason for it is.
Created attachment 60976 [details] Patch
Comment on attachment 60976 [details] Patch What’s the chance that some real world WebKit-only content depends on the broken behavior?
(In reply to comment #3) > (From update of attachment 60976 [details]) > What’s the chance that some real world WebKit-only content depends on the broken behavior? Fairly low -- no other browser does this, and it depends on having the incorrect code as the very last thing in the file (eg. no trailing new lines or other syntax)
Committed r62862