From 206f778c8dbbc48089cd5031734394367cc97df2 Mon Sep 17 00:00:00 2001 From: "rogerl%netscape.com" Date: Thu, 2 Dec 1999 01:16:02 +0000 Subject: [PATCH] Passing scope down into parser/IRFactory/NodeTransformer so that syntax errors can be packaged as ECMA Error objects. git-svn-id: svn://10.0.0.236/trunk@54985 18797224-902f-48f8-a5cc-f745e15eee43 --- .../rhino/org/mozilla/javascript/Context.java | 7 ++--- .../org/mozilla/javascript/IRFactory.java | 20 ++++++++++--- .../org/mozilla/javascript/Interpreter.java | 8 ++--- .../mozilla/javascript/NodeTransformer.java | 30 ++++++++++++------- .../mozilla/javascript/optimizer/Block.java | 2 +- .../mozilla/javascript/optimizer/Codegen.java | 8 ++--- .../javascript/optimizer/OptIRFactory.java | 4 +-- .../javascript/optimizer/OptTransformer.java | 9 +++--- .../src/org/mozilla/javascript/Context.java | 7 ++--- .../src/org/mozilla/javascript/IRFactory.java | 20 ++++++++++--- .../org/mozilla/javascript/Interpreter.java | 8 ++--- .../mozilla/javascript/NodeTransformer.java | 30 ++++++++++++------- .../mozilla/javascript/optimizer/Block.java | 2 +- .../mozilla/javascript/optimizer/Codegen.java | 8 ++--- .../javascript/optimizer/OptIRFactory.java | 4 +-- .../javascript/optimizer/OptTransformer.java | 9 +++--- 16 files changed, 110 insertions(+), 66 deletions(-) diff --git a/mozilla/js/rhino/org/mozilla/javascript/Context.java b/mozilla/js/rhino/org/mozilla/javascript/Context.java index 6498488871a..aa5adba3aa8 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/Context.java +++ b/mozilla/js/rhino/org/mozilla/javascript/Context.java @@ -761,7 +761,7 @@ public final class Context { boolean errorseen = false; try { - IRFactory irf = new IRFactory(ts); + IRFactory irf = new IRFactory(ts, null); Parser p = new Parser(irf); p.parse(ts); } catch (IOException ioe) { @@ -773,7 +773,6 @@ public final class Context { setErrorReporter(currentReporter); setErrorReporterHook(hook); } - // Return false only if an error occurred as a result of reading past // the end of the file, i.e. if the source could be fixed by // appending more source. @@ -1738,13 +1737,13 @@ public final class Context { : getCompiler(); errorCount = 0; - IRFactory irf = compiler.createIRFactory(ts, nameHelper); + IRFactory irf = compiler.createIRFactory(ts, nameHelper, scope); Parser p = new Parser(irf); Node tree = (Node) p.parse(ts); if (tree == null) return null; - tree = compiler.transform(tree, ts); + tree = compiler.transform(tree, ts, scope); if (printTrees) System.out.println(tree.toStringTree()); diff --git a/mozilla/js/rhino/org/mozilla/javascript/IRFactory.java b/mozilla/js/rhino/org/mozilla/javascript/IRFactory.java index 302162ba5ba..b826572df54 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/IRFactory.java +++ b/mozilla/js/rhino/org/mozilla/javascript/IRFactory.java @@ -47,8 +47,9 @@ package org.mozilla.javascript; */ public class IRFactory { - public IRFactory(TokenStream ts) { + public IRFactory(TokenStream ts, Scriptable scope) { this.ts = ts; + this.scope = scope; } /** @@ -1012,14 +1013,25 @@ public class IRFactory { } private void reportError(String msgResource) { - String message = Context.getMessage(msgResource, null); - Context.reportError(message, ts.getSourceName(), ts.getLineno(), - ts.getLine(), ts.getOffset()); + + if (scope != null) + throw NativeGlobal.constructError( + Context.getContext(), "SyntaxError", + ScriptRuntime.getMessage(msgResource, null), + scope); + else { + String message = Context.getMessage(msgResource, null); + Context.reportError(message, ts.getSourceName(), ts.getLineno(), + ts.getLine(), ts.getOffset()); + } } // Only needed to get file/line information. Could create an interface // that TokenStream implements if we want to make the connection less // direct. private TokenStream ts; + + // Only needed to pass to the Erorr exception constructors + private Scriptable scope; } diff --git a/mozilla/js/rhino/org/mozilla/javascript/Interpreter.java b/mozilla/js/rhino/org/mozilla/javascript/Interpreter.java index 918ac0b0ec8..35c08917439 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/Interpreter.java +++ b/mozilla/js/rhino/org/mozilla/javascript/Interpreter.java @@ -45,13 +45,13 @@ public class Interpreter extends LabelTable { public static final boolean printICode = false; public IRFactory createIRFactory(TokenStream ts, - ClassNameHelper nameHelper) + ClassNameHelper nameHelper, Scriptable scope) { - return new IRFactory(ts); + return new IRFactory(ts, scope); } - public Node transform(Node tree, TokenStream ts) { - return (new NodeTransformer()).transform(tree, null, ts); + public Node transform(Node tree, TokenStream ts, Scriptable scope) { + return (new NodeTransformer()).transform(tree, null, ts, scope); } public Object compile(Context cx, Scriptable scope, Node tree, diff --git a/mozilla/js/rhino/org/mozilla/javascript/NodeTransformer.java b/mozilla/js/rhino/org/mozilla/javascript/NodeTransformer.java index be4817c5158..4d0dde0d721 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/NodeTransformer.java +++ b/mozilla/js/rhino/org/mozilla/javascript/NodeTransformer.java @@ -58,18 +58,19 @@ public class NodeTransformer { return new NodeTransformer(); } - public IRFactory createIRFactory(TokenStream ts) { - return new IRFactory(ts); + public IRFactory createIRFactory(TokenStream ts, Scriptable scope) { + return new IRFactory(ts, scope); } - public Node transform(Node tree, Node enclosing, TokenStream ts) { + public Node transform(Node tree, Node enclosing, TokenStream ts, + Scriptable scope) { loops = new Stack(); loopEnds = new Stack(); inFunction = tree.getType() == TokenStream.FUNCTION; if (!inFunction) { addVariables(tree, getVariableTable(tree)); } - irFactory = createIRFactory(ts); + irFactory = createIRFactory(ts, scope); // to save against upchecks if no finally blocks are used. boolean hasFinally = false; @@ -107,7 +108,8 @@ public class NodeTransformer { node.getProp(Node.FUNCTION_PROP); addParameters(fnNode); NodeTransformer inner = newInstance(); - fnNode = (FunctionNode) inner.transform(fnNode, tree, ts); + fnNode = (FunctionNode) + inner.transform(fnNode, tree, ts, scope); node.putProp(Node.FUNCTION_PROP, fnNode); Vector fns = (Vector) tree.getProp(Node.FUNCTION_PROP); if (fns == null) { @@ -134,7 +136,7 @@ public class NodeTransformer { String message = Context.getMessage("msg.dup.label", errArgs); reportMessage(Context.getContext(), message, node, - tree, true); + tree, true, scope); break typeswitch; } } @@ -366,7 +368,7 @@ public class NodeTransformer { ("msg.undef.label", errArgs); } reportMessage(Context.getContext(), message, node, - tree, true); + tree, true, scope); node.setType(TokenStream.NOP); break; } @@ -662,7 +664,8 @@ public class NodeTransformer { } protected void reportMessage(Context cx, String msg, Node stmt, - Node tree, boolean isError) + Node tree, boolean isError, + Scriptable scope) { Object obj = stmt.getDatum(); int lineno = 0; @@ -671,8 +674,15 @@ public class NodeTransformer { Object prop = tree == null ? null : tree.getProp(Node.SOURCENAME_PROP); - if (isError) - cx.reportError(msg, (String) prop, lineno, null, 0); + if (isError) { + if (scope != null) + throw NativeGlobal.constructError( + cx, "SyntaxError", + msg, + scope, (String) prop, lineno); + else + cx.reportError(msg, (String) prop, lineno, null, 0); + } else cx.reportWarning(msg, (String) prop, lineno, null, 0); } diff --git a/mozilla/js/rhino/org/mozilla/javascript/optimizer/Block.java b/mozilla/js/rhino/org/mozilla/javascript/optimizer/Block.java index 12aa10e8181..07522523b8c 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/optimizer/Block.java +++ b/mozilla/js/rhino/org/mozilla/javascript/optimizer/Block.java @@ -601,7 +601,7 @@ public class Block { Hashtable localCSE(Hashtable theCSETable, OptFunctionNode theFunction) { - itsIRFactory = new IRFactory(null); + itsIRFactory = new IRFactory(null, null); if (theCSETable == null) theCSETable = new Hashtable(5); for (int i = itsStartNodeIndex; i <= itsEndNodeIndex; i++) { Node n = itsStatementNodes[i]; diff --git a/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java b/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java index d1a1becc742..bf90f05fd7d 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java +++ b/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java @@ -56,14 +56,14 @@ public class Codegen extends Interpreter { } public IRFactory createIRFactory(TokenStream ts, - ClassNameHelper nameHelper) + ClassNameHelper nameHelper, Scriptable scope) { - return new OptIRFactory(ts, nameHelper); + return new OptIRFactory(ts, nameHelper, scope); } - public Node transform(Node tree, TokenStream ts) { + public Node transform(Node tree, TokenStream ts, Scriptable scope) { OptTransformer opt = new OptTransformer(new Hashtable(11)); - return opt.transform(tree, null, ts); + return opt.transform(tree, null, ts, scope); } public Object compile(Context cx, Scriptable scope, Node tree, diff --git a/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptIRFactory.java b/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptIRFactory.java index 5b756fb425b..a4ead222b3e 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptIRFactory.java +++ b/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptIRFactory.java @@ -46,8 +46,8 @@ import org.mozilla.javascript.*; */ public class OptIRFactory extends IRFactory { - public OptIRFactory(TokenStream ts, ClassNameHelper nameHelper) { - super(ts); + public OptIRFactory(TokenStream ts, ClassNameHelper nameHelper, Scriptable scope) { + super(ts, scope); this.nameHelper = nameHelper; } diff --git a/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptTransformer.java b/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptTransformer.java index efc414bc1c9..829f270bf38 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptTransformer.java +++ b/mozilla/js/rhino/org/mozilla/javascript/optimizer/OptTransformer.java @@ -59,18 +59,19 @@ class OptTransformer extends NodeTransformer { return new OptTransformer((Hashtable) theFnClassNameList.clone()); } - public IRFactory createIRFactory(TokenStream ts) { - return new IRFactory(ts); + public IRFactory createIRFactory(TokenStream ts, Scriptable scope) { + return new IRFactory(ts, scope); } - public Node transform(Node tree, Node enclosing, TokenStream ts) { + public Node transform(Node tree, Node enclosing, TokenStream ts, + Scriptable scope) { // Collect all of the contained functions into a hashtable // so that the call optimizer can access the class name & parameter // count for any call it encounters collectContainedFunctions(tree.getFirstChild()); - return super.transform(tree, enclosing, ts); + return super.transform(tree, enclosing, ts, scope); } protected VariableTable createVariableTable() { diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java index 6498488871a..aa5adba3aa8 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java @@ -761,7 +761,7 @@ public final class Context { boolean errorseen = false; try { - IRFactory irf = new IRFactory(ts); + IRFactory irf = new IRFactory(ts, null); Parser p = new Parser(irf); p.parse(ts); } catch (IOException ioe) { @@ -773,7 +773,6 @@ public final class Context { setErrorReporter(currentReporter); setErrorReporterHook(hook); } - // Return false only if an error occurred as a result of reading past // the end of the file, i.e. if the source could be fixed by // appending more source. @@ -1738,13 +1737,13 @@ public final class Context { : getCompiler(); errorCount = 0; - IRFactory irf = compiler.createIRFactory(ts, nameHelper); + IRFactory irf = compiler.createIRFactory(ts, nameHelper, scope); Parser p = new Parser(irf); Node tree = (Node) p.parse(ts); if (tree == null) return null; - tree = compiler.transform(tree, ts); + tree = compiler.transform(tree, ts, scope); if (printTrees) System.out.println(tree.toStringTree()); diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/IRFactory.java b/mozilla/js/rhino/src/org/mozilla/javascript/IRFactory.java index 302162ba5ba..b826572df54 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/IRFactory.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/IRFactory.java @@ -47,8 +47,9 @@ package org.mozilla.javascript; */ public class IRFactory { - public IRFactory(TokenStream ts) { + public IRFactory(TokenStream ts, Scriptable scope) { this.ts = ts; + this.scope = scope; } /** @@ -1012,14 +1013,25 @@ public class IRFactory { } private void reportError(String msgResource) { - String message = Context.getMessage(msgResource, null); - Context.reportError(message, ts.getSourceName(), ts.getLineno(), - ts.getLine(), ts.getOffset()); + + if (scope != null) + throw NativeGlobal.constructError( + Context.getContext(), "SyntaxError", + ScriptRuntime.getMessage(msgResource, null), + scope); + else { + String message = Context.getMessage(msgResource, null); + Context.reportError(message, ts.getSourceName(), ts.getLineno(), + ts.getLine(), ts.getOffset()); + } } // Only needed to get file/line information. Could create an interface // that TokenStream implements if we want to make the connection less // direct. private TokenStream ts; + + // Only needed to pass to the Erorr exception constructors + private Scriptable scope; } diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/Interpreter.java b/mozilla/js/rhino/src/org/mozilla/javascript/Interpreter.java index 918ac0b0ec8..35c08917439 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/Interpreter.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/Interpreter.java @@ -45,13 +45,13 @@ public class Interpreter extends LabelTable { public static final boolean printICode = false; public IRFactory createIRFactory(TokenStream ts, - ClassNameHelper nameHelper) + ClassNameHelper nameHelper, Scriptable scope) { - return new IRFactory(ts); + return new IRFactory(ts, scope); } - public Node transform(Node tree, TokenStream ts) { - return (new NodeTransformer()).transform(tree, null, ts); + public Node transform(Node tree, TokenStream ts, Scriptable scope) { + return (new NodeTransformer()).transform(tree, null, ts, scope); } public Object compile(Context cx, Scriptable scope, Node tree, diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/NodeTransformer.java b/mozilla/js/rhino/src/org/mozilla/javascript/NodeTransformer.java index be4817c5158..4d0dde0d721 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/NodeTransformer.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/NodeTransformer.java @@ -58,18 +58,19 @@ public class NodeTransformer { return new NodeTransformer(); } - public IRFactory createIRFactory(TokenStream ts) { - return new IRFactory(ts); + public IRFactory createIRFactory(TokenStream ts, Scriptable scope) { + return new IRFactory(ts, scope); } - public Node transform(Node tree, Node enclosing, TokenStream ts) { + public Node transform(Node tree, Node enclosing, TokenStream ts, + Scriptable scope) { loops = new Stack(); loopEnds = new Stack(); inFunction = tree.getType() == TokenStream.FUNCTION; if (!inFunction) { addVariables(tree, getVariableTable(tree)); } - irFactory = createIRFactory(ts); + irFactory = createIRFactory(ts, scope); // to save against upchecks if no finally blocks are used. boolean hasFinally = false; @@ -107,7 +108,8 @@ public class NodeTransformer { node.getProp(Node.FUNCTION_PROP); addParameters(fnNode); NodeTransformer inner = newInstance(); - fnNode = (FunctionNode) inner.transform(fnNode, tree, ts); + fnNode = (FunctionNode) + inner.transform(fnNode, tree, ts, scope); node.putProp(Node.FUNCTION_PROP, fnNode); Vector fns = (Vector) tree.getProp(Node.FUNCTION_PROP); if (fns == null) { @@ -134,7 +136,7 @@ public class NodeTransformer { String message = Context.getMessage("msg.dup.label", errArgs); reportMessage(Context.getContext(), message, node, - tree, true); + tree, true, scope); break typeswitch; } } @@ -366,7 +368,7 @@ public class NodeTransformer { ("msg.undef.label", errArgs); } reportMessage(Context.getContext(), message, node, - tree, true); + tree, true, scope); node.setType(TokenStream.NOP); break; } @@ -662,7 +664,8 @@ public class NodeTransformer { } protected void reportMessage(Context cx, String msg, Node stmt, - Node tree, boolean isError) + Node tree, boolean isError, + Scriptable scope) { Object obj = stmt.getDatum(); int lineno = 0; @@ -671,8 +674,15 @@ public class NodeTransformer { Object prop = tree == null ? null : tree.getProp(Node.SOURCENAME_PROP); - if (isError) - cx.reportError(msg, (String) prop, lineno, null, 0); + if (isError) { + if (scope != null) + throw NativeGlobal.constructError( + cx, "SyntaxError", + msg, + scope, (String) prop, lineno); + else + cx.reportError(msg, (String) prop, lineno, null, 0); + } else cx.reportWarning(msg, (String) prop, lineno, null, 0); } diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Block.java b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Block.java index 12aa10e8181..07522523b8c 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Block.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Block.java @@ -601,7 +601,7 @@ public class Block { Hashtable localCSE(Hashtable theCSETable, OptFunctionNode theFunction) { - itsIRFactory = new IRFactory(null); + itsIRFactory = new IRFactory(null, null); if (theCSETable == null) theCSETable = new Hashtable(5); for (int i = itsStartNodeIndex; i <= itsEndNodeIndex; i++) { Node n = itsStatementNodes[i]; diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java index d1a1becc742..bf90f05fd7d 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java @@ -56,14 +56,14 @@ public class Codegen extends Interpreter { } public IRFactory createIRFactory(TokenStream ts, - ClassNameHelper nameHelper) + ClassNameHelper nameHelper, Scriptable scope) { - return new OptIRFactory(ts, nameHelper); + return new OptIRFactory(ts, nameHelper, scope); } - public Node transform(Node tree, TokenStream ts) { + public Node transform(Node tree, TokenStream ts, Scriptable scope) { OptTransformer opt = new OptTransformer(new Hashtable(11)); - return opt.transform(tree, null, ts); + return opt.transform(tree, null, ts, scope); } public Object compile(Context cx, Scriptable scope, Node tree, diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptIRFactory.java b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptIRFactory.java index 5b756fb425b..a4ead222b3e 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptIRFactory.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptIRFactory.java @@ -46,8 +46,8 @@ import org.mozilla.javascript.*; */ public class OptIRFactory extends IRFactory { - public OptIRFactory(TokenStream ts, ClassNameHelper nameHelper) { - super(ts); + public OptIRFactory(TokenStream ts, ClassNameHelper nameHelper, Scriptable scope) { + super(ts, scope); this.nameHelper = nameHelper; } diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java index efc414bc1c9..829f270bf38 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java @@ -59,18 +59,19 @@ class OptTransformer extends NodeTransformer { return new OptTransformer((Hashtable) theFnClassNameList.clone()); } - public IRFactory createIRFactory(TokenStream ts) { - return new IRFactory(ts); + public IRFactory createIRFactory(TokenStream ts, Scriptable scope) { + return new IRFactory(ts, scope); } - public Node transform(Node tree, Node enclosing, TokenStream ts) { + public Node transform(Node tree, Node enclosing, TokenStream ts, + Scriptable scope) { // Collect all of the contained functions into a hashtable // so that the call optimizer can access the class name & parameter // count for any call it encounters collectContainedFunctions(tree.getFirstChild()); - return super.transform(tree, enclosing, ts); + return super.transform(tree, enclosing, ts, scope); } protected VariableTable createVariableTable() {