From ac13c7d81e5a8e64e1811d529a181483d5704a61 Mon Sep 17 00:00:00 2001 From: tuchida Date: Mon, 17 Jan 2022 20:30:48 +0900 Subject: [PATCH] Make it possible to throw SyntaxError in IRFactory --- src/org/mozilla/javascript/Context.java | 2 +- src/org/mozilla/javascript/IRFactory.java | 389 ++++++++++++------ src/org/mozilla/javascript/Parser.java | 90 ++-- src/org/mozilla/javascript/TokenStream.java | 19 +- .../javascript/optimizer/ClassCompiler.java | 2 +- .../javascript/tests/Bug708801Test.java | 2 +- .../javascript/tests/Bug782363Test.java | 2 +- .../javascript/tests/ReadCommentsTest.java | 2 +- 8 files changed, 330 insertions(+), 178 deletions(-) diff --git a/src/org/mozilla/javascript/Context.java b/src/org/mozilla/javascript/Context.java index 952a24293e..8574f8fc83 100644 --- a/src/org/mozilla/javascript/Context.java +++ b/src/org/mozilla/javascript/Context.java @@ -2507,7 +2507,7 @@ private ScriptNode parse( } } - IRFactory irf = new IRFactory(compilerEnv, compilationErrorReporter); + IRFactory irf = new IRFactory(compilerEnv, sourceString, compilationErrorReporter); ScriptNode tree = irf.transformTree(ast); if (compilerEnv.isGeneratingSource()) { diff --git a/src/org/mozilla/javascript/IRFactory.java b/src/org/mozilla/javascript/IRFactory.java index 3012704ded..30a27a426b 100644 --- a/src/org/mozilla/javascript/IRFactory.java +++ b/src/org/mozilla/javascript/IRFactory.java @@ -6,6 +6,7 @@ package org.mozilla.javascript; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; import org.mozilla.javascript.ast.ArrayComprehension; @@ -90,13 +91,16 @@ public final class IRFactory { private static final int ALWAYS_FALSE_BOOLEAN = -1; private Parser parser; + private AstNodePosition astNodePos; - public IRFactory(CompilerEnvirons env) { - this(env, env.getErrorReporter()); + public IRFactory(CompilerEnvirons env, String sourceString) { + this(env, sourceString, env.getErrorReporter()); } - public IRFactory(CompilerEnvirons env, ErrorReporter errorReporter) { + public IRFactory(CompilerEnvirons env, String sourceString, ErrorReporter errorReporter) { parser = new Parser(env, errorReporter); + astNodePos = new AstNodePosition(sourceString); + parser.currentPos = astNodePos; } /** Transforms the tree into a lower-level IR suitable for codegen. */ @@ -109,7 +113,12 @@ public ScriptNode transformTree(AstRoot root) { System.out.println(root.debugPrint()); } - return (ScriptNode) transform(root); + try { + return (ScriptNode) transform(root); + } catch (Parser.ParserException e) { + parser.reportErrorsIfExists(root.getLineno()); + return null; + } } // Might want to convert this to polymorphism - move transform* @@ -262,19 +271,25 @@ private Node transformArrayComp(ArrayComprehension node) { String arrayName = parser.currentScriptOrFn.getNextTempName(); parser.pushScope(scopeNode); try { - parser.defineSymbol(Token.LET, arrayName, false); - Node block = new Node(Token.BLOCK, lineno); - Node newArray = createCallOrNew(Token.NEW, parser.createName("Array")); - Node init = - new Node( - Token.EXPR_VOID, - createAssignment(Token.ASSIGN, parser.createName(arrayName), newArray), - lineno); - block.addChildToBack(init); - block.addChildToBack(arrayCompTransformHelper(node, arrayName)); - scopeNode.addChildToBack(block); - scopeNode.addChildToBack(parser.createName(arrayName)); - return scopeNode; + astNodePos.push(node); + try { + parser.defineSymbol(Token.LET, arrayName, false); + Node block = new Node(Token.BLOCK, lineno); + Node newArray = createCallOrNew(Token.NEW, parser.createName("Array")); + Node init = + new Node( + Token.EXPR_VOID, + createAssignment( + Token.ASSIGN, parser.createName(arrayName), newArray), + lineno); + block.addChildToBack(init); + block.addChildToBack(arrayCompTransformHelper(node, arrayName)); + scopeNode.addChildToBack(block); + scopeNode.addChildToBack(parser.createName(arrayName)); + return scopeNode; + } finally { + astNodePos.pop(); + } } finally { parser.popScope(); } @@ -294,24 +309,29 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) for (int i = 0; i < numLoops; i++) { ArrayComprehensionLoop acl = loops.get(i); AstNode iter = acl.getIterator(); - String name = null; - if (iter.getType() == Token.NAME) { - name = iter.getString(); - } else { - // destructuring assignment - name = parser.currentScriptOrFn.getNextTempName(); - parser.defineSymbol(Token.LP, name, false); - expr = - createBinary( - Token.COMMA, - createAssignment(Token.ASSIGN, iter, parser.createName(name)), - expr); + astNodePos.push(iter); + try { + String name = null; + if (iter.getType() == Token.NAME) { + name = iter.getString(); + } else { + // destructuring assignment + name = parser.currentScriptOrFn.getNextTempName(); + parser.defineSymbol(Token.LP, name, false); + expr = + createBinary( + Token.COMMA, + createAssignment(Token.ASSIGN, iter, parser.createName(name)), + expr); + } + Node init = parser.createName(name); + // Define as a let since we want the scope of the variable to + // be restricted to the array comprehension + parser.defineSymbol(Token.LET, name, false); + iterators[i] = init; + } finally { + astNodePos.pop(); } - Node init = parser.createName(name); - // Define as a let since we want the scope of the variable to - // be restricted to the array comprehension - parser.defineSymbol(Token.LET, name, false); - iterators[i] = init; iteratedObjs[i] = transform(acl.getIteratedObject()); } @@ -346,6 +366,7 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) iterators[i], iteratedObjs[i], body, + acl, acl.isForEach(), acl.isForOf()); } @@ -400,7 +421,12 @@ private Node transformAssignment(Assignment node) { target = transform(left); } - return createAssignment(node.getType(), target, transform(right)); + astNodePos.push(left); + try { + return createAssignment(node.getType(), target, transform(right)); + } finally { + astNodePos.pop(); + } } private AstNode transformAssignmentLeft(Assignment node, AstNode left, AstNode right) { @@ -505,7 +531,8 @@ private Node transformForInLoop(ForInLoop loop) { Node lhs = transform(iter); Node obj = transform(loop.getIteratedObject()); Node body = transform(loop.getBody()); - return createForIn(declType, loop, lhs, obj, body, loop.isForEach(), loop.isForOf()); + return createForIn( + declType, loop, lhs, obj, body, loop, loop.isForEach(), loop.isForOf()); } finally { parser.popScope(); } @@ -550,7 +577,12 @@ private Node transformFunction(FunctionNode fn) { int syntheticType = fn.getFunctionType(); Node pn = initFunction(fn, index, body, syntheticType); if (mexpr != null) { - pn = createAssignment(Token.ASSIGN, mexpr, pn); + astNodePos.push(fn); + try { + pn = createAssignment(Token.ASSIGN, mexpr, pn); + } finally { + astNodePos.pop(); + } if (syntheticType != FunctionNode.FUNCTION_EXPRESSION) { pn = createExprStatementNoReturn(pn, fn.getLineno()); } @@ -604,7 +636,12 @@ private Node transformGenExpr(GeneratorExpression node) { int syntheticType = fn.getFunctionType(); pn = initFunction(fn, index, body, syntheticType); if (mexpr != null) { - pn = createAssignment(Token.ASSIGN, mexpr, pn); + astNodePos.push(fn); + try { + pn = createAssignment(Token.ASSIGN, mexpr, pn); + } finally { + astNodePos.pop(); + } if (syntheticType != FunctionNode.FUNCTION_EXPRESSION) { pn = createExprStatementNoReturn(pn, fn.getLineno()); } @@ -634,24 +671,29 @@ private Node genExprTransformHelper(GeneratorExpression node) { GeneratorExpressionLoop acl = loops.get(i); AstNode iter = acl.getIterator(); - String name = null; - if (iter.getType() == Token.NAME) { - name = iter.getString(); - } else { - // destructuring assignment - name = parser.currentScriptOrFn.getNextTempName(); - parser.defineSymbol(Token.LP, name, false); - expr = - createBinary( - Token.COMMA, - createAssignment(Token.ASSIGN, iter, parser.createName(name)), - expr); + astNodePos.push(iter); + try { + String name = null; + if (iter.getType() == Token.NAME) { + name = iter.getString(); + } else { + // destructuring assignment + name = parser.currentScriptOrFn.getNextTempName(); + parser.defineSymbol(Token.LP, name, false); + expr = + createBinary( + Token.COMMA, + createAssignment(Token.ASSIGN, iter, parser.createName(name)), + expr); + } + Node init = parser.createName(name); + // Define as a let since we want the scope of the variable to + // be restricted to the array comprehension + parser.defineSymbol(Token.LET, name, false); + iterators[i] = init; + } finally { + astNodePos.pop(); } - Node init = parser.createName(name); - // Define as a let since we want the scope of the variable to - // be restricted to the array comprehension - parser.defineSymbol(Token.LET, name, false); - iterators[i] = init; iteratedObjs[i] = transform(acl.getIteratedObject()); } @@ -683,6 +725,7 @@ private Node genExprTransformHelper(GeneratorExpression node) { iterators[i], iteratedObjs[i], body, + acl, acl.isForEach(), acl.isForOf()); } @@ -1044,8 +1087,13 @@ private Node transformVariableInitializers(VariableDeclaration node) { if (right == null) { // TODO: should this ever happen? node.addChildToBack(left); } else { - Node d = parser.createDestructuringAssignment(node.getType(), left, right); - node.addChildToBack(d); + astNodePos.push(var); + try { + Node d = parser.createDestructuringAssignment(node.getType(), left, right); + node.addChildToBack(d); + } finally { + astNodePos.pop(); + } } } else { if (right != null) { @@ -1351,80 +1399,86 @@ private Node createForIn( Node lhs, Node obj, Node body, + AstNode ast, boolean isForEach, boolean isForOf) { - int destructuring = -1; - int destructuringLen = 0; - Node lvalue; - int type = lhs.getType(); - if (type == Token.VAR || type == Token.LET) { - Node kid = lhs.getLastChild(); - int kidType = kid.getType(); - if (kidType == Token.ARRAYLIT || kidType == Token.OBJECTLIT) { - type = destructuring = kidType; - lvalue = kid; + astNodePos.push(ast); + try { + int destructuring = -1; + int destructuringLen = 0; + Node lvalue; + int type = lhs.getType(); + if (type == Token.VAR || type == Token.LET) { + Node kid = lhs.getLastChild(); + int kidType = kid.getType(); + if (kidType == Token.ARRAYLIT || kidType == Token.OBJECTLIT) { + type = destructuring = kidType; + lvalue = kid; + destructuringLen = 0; + if (kid instanceof ArrayLiteral) + destructuringLen = ((ArrayLiteral) kid).getDestructuringLength(); + } else if (kidType == Token.NAME) { + lvalue = Node.newString(Token.NAME, kid.getString()); + } else { + parser.reportError("msg.bad.for.in.lhs"); + return null; + } + } else if (type == Token.ARRAYLIT || type == Token.OBJECTLIT) { + destructuring = type; + lvalue = lhs; destructuringLen = 0; - if (kid instanceof ArrayLiteral) - destructuringLen = ((ArrayLiteral) kid).getDestructuringLength(); - } else if (kidType == Token.NAME) { - lvalue = Node.newString(Token.NAME, kid.getString()); + if (lhs instanceof ArrayLiteral) + destructuringLen = ((ArrayLiteral) lhs).getDestructuringLength(); } else { - parser.reportError("msg.bad.for.in.lhs"); - return null; - } - } else if (type == Token.ARRAYLIT || type == Token.OBJECTLIT) { - destructuring = type; - lvalue = lhs; - destructuringLen = 0; - if (lhs instanceof ArrayLiteral) - destructuringLen = ((ArrayLiteral) lhs).getDestructuringLength(); - } else { - lvalue = makeReference(lhs); - if (lvalue == null) { - parser.reportError("msg.bad.for.in.lhs"); - return null; + lvalue = makeReference(lhs); + if (lvalue == null) { + parser.reportError("msg.bad.for.in.lhs"); + return null; + } } - } - Node localBlock = new Node(Token.LOCAL_BLOCK); - int initType = - isForEach - ? Token.ENUM_INIT_VALUES - : isForOf - ? Token.ENUM_INIT_VALUES_IN_ORDER - : (destructuring != -1 - ? Token.ENUM_INIT_ARRAY - : Token.ENUM_INIT_KEYS); - Node init = new Node(initType, obj); - init.putProp(Node.LOCAL_BLOCK_PROP, localBlock); - Node cond = new Node(Token.ENUM_NEXT); - cond.putProp(Node.LOCAL_BLOCK_PROP, localBlock); - Node id = new Node(Token.ENUM_ID); - id.putProp(Node.LOCAL_BLOCK_PROP, localBlock); - - Node newBody = new Node(Token.BLOCK); - Node assign; - if (destructuring != -1) { - assign = parser.createDestructuringAssignment(declType, lvalue, id); - if (!isForEach - && !isForOf - && (destructuring == Token.OBJECTLIT || destructuringLen != 2)) { - // destructuring assignment is only allowed in for..each or - // with an array type of length 2 (to hold key and value) - parser.reportError("msg.bad.for.in.destruct"); + Node localBlock = new Node(Token.LOCAL_BLOCK); + int initType = + isForEach + ? Token.ENUM_INIT_VALUES + : isForOf + ? Token.ENUM_INIT_VALUES_IN_ORDER + : (destructuring != -1 + ? Token.ENUM_INIT_ARRAY + : Token.ENUM_INIT_KEYS); + Node init = new Node(initType, obj); + init.putProp(Node.LOCAL_BLOCK_PROP, localBlock); + Node cond = new Node(Token.ENUM_NEXT); + cond.putProp(Node.LOCAL_BLOCK_PROP, localBlock); + Node id = new Node(Token.ENUM_ID); + id.putProp(Node.LOCAL_BLOCK_PROP, localBlock); + + Node newBody = new Node(Token.BLOCK); + Node assign; + if (destructuring != -1) { + assign = parser.createDestructuringAssignment(declType, lvalue, id); + if (!isForEach + && !isForOf + && (destructuring == Token.OBJECTLIT || destructuringLen != 2)) { + // destructuring assignment is only allowed in for..each or + // with an array type of length 2 (to hold key and value) + parser.reportError("msg.bad.for.in.destruct"); + } + } else { + assign = parser.simpleAssignment(lvalue, id); } - } else { - assign = parser.simpleAssignment(lvalue, id); - } - newBody.addChildToBack(new Node(Token.EXPR_VOID, assign)); - newBody.addChildToBack(body); + newBody.addChildToBack(new Node(Token.EXPR_VOID, assign)); + newBody.addChildToBack(body); - loop = createLoop((Jump) loop, LOOP_WHILE, newBody, cond, null, null); - loop.addChildToFront(init); - if (type == Token.VAR || type == Token.LET) loop.addChildToFront(lhs); - localBlock.addChildToBack(loop); + loop = createLoop((Jump) loop, LOOP_WHILE, newBody, cond, null, null); + loop.addChildToFront(init); + if (type == Token.VAR || type == Token.LET) loop.addChildToFront(lhs); + localBlock.addChildToBack(loop); - return localBlock; + return localBlock; + } finally { + astNodePos.pop(); + } } /** @@ -2126,4 +2180,95 @@ Node decompileFunctionHeader(FunctionNode fn) { } return null; } + + public static class AstNodePosition implements Parser.CurrentPositionReporter { + private ArrayDeque stack; + private String sourceString; + + private int savedLineno = -1; + private String savedLine; + private int savedLineOffset; + + public AstNodePosition(String sourceString) { + stack = new ArrayDeque<>(); + this.sourceString = sourceString; + } + + public void push(AstNode node) { + stack.push(node); + } + + public void pop() { + stack.pop(); + } + + @Override + public int getPosition() { + return stack.peek().getAbsolutePosition(); + } + + @Override + public int getLength() { + return stack.peek().getLength(); + } + + @Override + public int getLineno() { + return stack.peek().getLineno(); + } + + private void cutAndSaveLine() { + int lineno = getLineno(); + if (savedLineno == lineno) { + return; + } + + int l = 1; + boolean isPrevCR = false; + int begin = 0; + for (; begin < sourceString.length(); begin++) { + char c = sourceString.charAt(begin); + if (isPrevCR && c == '\n') { + continue; + } + isPrevCR = (c == '\r'); + + if (l == lineno) { + break; + } + if (ScriptRuntime.isJSLineTerminator(c)) { + l++; + } + } + + int end = begin; + for (; end < sourceString.length(); end++) { + char c = sourceString.charAt(end); + if (ScriptRuntime.isJSLineTerminator(c)) { + break; + } + } + + savedLineno = lineno; + if (end == 0) { + savedLine = ""; + savedLineOffset = 0; + } else { + savedLine = sourceString.substring(begin, end); + savedLineOffset = getPosition() - begin + 1; + } + } + + @Override + public String getLine() { + cutAndSaveLine(); + return savedLine; + } + + @Override + public int getOffset() { + cutAndSaveLine(); + return savedLineOffset; + } + } } diff --git a/src/org/mozilla/javascript/Parser.java b/src/org/mozilla/javascript/Parser.java index 46667eb20b..e2e625c69d 100644 --- a/src/org/mozilla/javascript/Parser.java +++ b/src/org/mozilla/javascript/Parser.java @@ -123,6 +123,7 @@ public class Parser { private boolean parseFinished; // set when finished to prevent reuse private TokenStream ts; + CurrentPositionReporter currentPos; private int currentFlaggedToken = Token.EOF; private int currentToken; private int syntaxErrorCount; @@ -156,7 +157,7 @@ public class Parser { private boolean defaultUseStrictDirective; // Exception to unwind - private static class ParserException extends RuntimeException { + public static class ParserException extends RuntimeException { private static final long serialVersionUID = 5882582646773765630L; } @@ -178,12 +179,7 @@ public Parser(CompilerEnvirons compilerEnv, ErrorReporter errorReporter) { // Add a strict warning on the last matched token. void addStrictWarning(String messageId, String messageArg) { - int beg = -1, end = -1; - if (ts != null) { - beg = ts.tokenBeg; - end = ts.tokenEnd - ts.tokenBeg; - } - addStrictWarning(messageId, messageArg, beg, end); + addStrictWarning(messageId, messageArg, currentPos.getPosition(), currentPos.getLength()); } void addStrictWarning(String messageId, String messageArg, int position, int length) { @@ -191,12 +187,7 @@ void addStrictWarning(String messageId, String messageArg, int position, int len } void addWarning(String messageId, String messageArg) { - int beg = -1, end = -1; - if (ts != null) { - beg = ts.tokenBeg; - end = ts.tokenEnd - ts.tokenBeg; - } - addWarning(messageId, messageArg, beg, end); + addWarning(messageId, messageArg, currentPos.getPosition(), currentPos.getLength()); } void addWarning(String messageId, int position, int length) { @@ -209,19 +200,18 @@ void addWarning(String messageId, String messageArg, int position, int length) { addError(messageId, messageArg, position, length); } else if (errorCollector != null) { errorCollector.warning(message, sourceURI, position, length); - } else if (ts != null) { - errorReporter.warning(message, sourceURI, ts.getLineno(), ts.getLine(), ts.getOffset()); } else { - errorReporter.warning(message, sourceURI, 1, "", 1); + errorReporter.warning( + message, + sourceURI, + currentPos.getLineno(), + currentPos.getLine(), + currentPos.getOffset()); } } void addError(String messageId) { - if (ts == null) { - addError(messageId, 0, 0); - } else { - addError(messageId, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); - } + addError(messageId, currentPos.getPosition(), currentPos.getLength()); } void addError(String messageId, int position, int length) { @@ -229,11 +219,7 @@ void addError(String messageId, int position, int length) { } void addError(String messageId, String messageArg) { - if (ts == null) { - addError(messageId, messageArg, 0, 0); - } else { - addError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); - } + addError(messageId, messageArg, currentPos.getPosition(), currentPos.getLength()); } void addError(String messageId, int c) { @@ -247,14 +233,12 @@ void addError(String messageId, String messageArg, int position, int length) { if (errorCollector != null) { errorCollector.error(message, sourceURI, position, length); } else { - int lineno = 1, offset = 1; - String line = ""; - if (ts != null) { // happens in some regression tests - lineno = ts.getLineno(); - line = ts.getLine(); - offset = ts.getOffset(); - } - errorReporter.error(message, sourceURI, lineno, line, offset); + errorReporter.error( + message, + sourceURI, + currentPos.getLineno(), + currentPos.getLine(), + currentPos.getOffset()); } } @@ -321,11 +305,7 @@ void reportError(String messageId) { } void reportError(String messageId, String messageArg) { - if (ts == null) { // happens in some regression tests - reportError(messageId, messageArg, 1, 1); - } else { - reportError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); - } + reportError(messageId, messageArg, currentPos.getPosition(), currentPos.getLength()); } void reportError(String messageId, int position, int length) { @@ -557,7 +537,7 @@ public AstRoot parse(String sourceString, String sourceURI, int lineno) { if (compilerEnv.isIdeMode()) { this.sourceChars = sourceString.toCharArray(); } - this.ts = new TokenStream(this, null, sourceString, lineno); + currentPos = ts = new TokenStream(this, null, sourceString, lineno); try { return parse(); } catch (IOException iox) { @@ -583,7 +563,7 @@ public AstRoot parse(Reader sourceReader, String sourceURI, int lineno) throws I } try { this.sourceURI = sourceURI; - ts = new TokenStream(this, sourceReader, null, lineno); + currentPos = ts = new TokenStream(this, sourceReader, null, lineno); return parse(); } finally { parseFinished = true; @@ -652,12 +632,7 @@ private AstRoot parse() throws IOException { inUseStrictDirective = savedStrictMode; } - if (this.syntaxErrorCount != 0) { - String msg = String.valueOf(this.syntaxErrorCount); - msg = lookupMessage("msg.got.syntax.errors", msg); - if (!compilerEnv.isIdeMode()) - throw errorReporter.runtimeError(msg, sourceURI, baseLineno, null, 0); - } + reportErrorsIfExists(baseLineno); // add comments to root in lexical order if (scannedComments != null) { @@ -4268,4 +4243,25 @@ public void setDefaultUseStrictDirective(boolean useStrict) { public boolean inUseStrictDirective() { return inUseStrictDirective; } + + public void reportErrorsIfExists(int baseLineno) { + if (this.syntaxErrorCount != 0) { + String msg = String.valueOf(this.syntaxErrorCount); + msg = lookupMessage("msg.got.syntax.errors", msg); + if (!compilerEnv.isIdeMode()) + throw errorReporter.runtimeError(msg, sourceURI, baseLineno, null, 0); + } + } + + public interface CurrentPositionReporter { + public int getPosition(); + + public int getLength(); + + public int getLineno(); + + public String getLine(); + + public int getOffset(); + } } diff --git a/src/org/mozilla/javascript/TokenStream.java b/src/org/mozilla/javascript/TokenStream.java index e38a9dbe73..82f6b437c4 100644 --- a/src/org/mozilla/javascript/TokenStream.java +++ b/src/org/mozilla/javascript/TokenStream.java @@ -19,7 +19,7 @@ * @author Mike McCabe * @author Brendan Eich */ -class TokenStream { +class TokenStream implements Parser.CurrentPositionReporter { /* * For chars - because we need something out-of-range * to check. (And checking EOF by exception is annoying.) @@ -582,7 +582,8 @@ final String getSourceString() { return sourceString; } - final int getLineno() { + @Override + public int getLineno() { return lineno; } @@ -2094,7 +2095,8 @@ private void skipLine() throws IOException { } /** Returns the offset into the current line. */ - final int getOffset() { + @Override + public int getOffset() { int n = sourceCursor - lineStart; if (lineEndChar >= 0) { --n; @@ -2137,7 +2139,8 @@ private final String substring(int beginIndex, int endIndex) { return new String(sourceBuffer, beginIndex, count); } - final String getLine() { + @Override + public String getLine() { int lineEnd = sourceCursor; if (lineEndChar >= 0) { // move cursor before newline sequence @@ -2290,6 +2293,14 @@ private static String convertLastCharToHex(String str) { return buf.toString(); } + public int getPosition() { + return tokenBeg; + } + + public int getLength() { + return tokenEnd - tokenBeg; + } + // stuff other than whitespace since start of line private boolean dirtyLine; diff --git a/src/org/mozilla/javascript/optimizer/ClassCompiler.java b/src/org/mozilla/javascript/optimizer/ClassCompiler.java index 389f24fa1d..25f91b72c0 100644 --- a/src/org/mozilla/javascript/optimizer/ClassCompiler.java +++ b/src/org/mozilla/javascript/optimizer/ClassCompiler.java @@ -111,7 +111,7 @@ public Object[] compileToClassFiles( String source, String sourceLocation, int lineno, String mainClassName) { Parser p = new Parser(compilerEnv); AstRoot ast = p.parse(source, sourceLocation, lineno); - IRFactory irf = new IRFactory(compilerEnv); + IRFactory irf = new IRFactory(compilerEnv, source); ScriptNode tree = irf.transformTree(ast); if (compilerEnv.isGeneratingSource()) { diff --git a/testsrc/org/mozilla/javascript/tests/Bug708801Test.java b/testsrc/org/mozilla/javascript/tests/Bug708801Test.java index 6bd914b3e1..de3edcd122 100644 --- a/testsrc/org/mozilla/javascript/tests/Bug708801Test.java +++ b/testsrc/org/mozilla/javascript/tests/Bug708801Test.java @@ -64,7 +64,7 @@ protected ScriptNode compile(CharSequence source) { ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter(); Parser p = new Parser(compilerEnv, compilationErrorReporter); AstRoot ast = p.parse(source.toString(), "", 1); - IRFactory irf = new IRFactory(compilerEnv); + IRFactory irf = new IRFactory(compilerEnv, source.toString()); ScriptNode tree = irf.transformTree(ast); Codegen codegen = new Codegen(); diff --git a/testsrc/org/mozilla/javascript/tests/Bug782363Test.java b/testsrc/org/mozilla/javascript/tests/Bug782363Test.java index 3601712ddc..904fcf27df 100755 --- a/testsrc/org/mozilla/javascript/tests/Bug782363Test.java +++ b/testsrc/org/mozilla/javascript/tests/Bug782363Test.java @@ -55,7 +55,7 @@ protected ScriptNode compile(CharSequence source) { compilerEnv.initFromContext(cx); Parser p = new Parser(compilerEnv); AstRoot ast = p.parse(source.toString(), "", 1); - IRFactory irf = new IRFactory(compilerEnv); + IRFactory irf = new IRFactory(compilerEnv, source.toString()); ScriptNode tree = irf.transformTree(ast); Codegen codegen = new Codegen(); diff --git a/testsrc/org/mozilla/javascript/tests/ReadCommentsTest.java b/testsrc/org/mozilla/javascript/tests/ReadCommentsTest.java index 882d13858d..d2ef198006 100644 --- a/testsrc/org/mozilla/javascript/tests/ReadCommentsTest.java +++ b/testsrc/org/mozilla/javascript/tests/ReadCommentsTest.java @@ -29,7 +29,7 @@ public void readComments() throws IOException { testJs = scriptIn.lines().collect(Collectors.joining(System.lineSeparator())); } AstRoot ast = p.parse(testJs, "test", 1); - IRFactory irf = new IRFactory(compilerEnv); + IRFactory irf = new IRFactory(compilerEnv, testJs); ScriptNode tree = irf.transformTree(ast); Assert.assertEquals(1, tree.getFunctions().size());