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
This commit is contained in:
rogerl%netscape.com 1999-12-02 01:16:02 +00:00
parent c56a65dcc6
commit 206f778c8d
16 changed files with 110 additions and 66 deletions

View File

@ -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());

View File

@ -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;
}

View File

@ -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,

View File

@ -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);
}

View File

@ -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];

View File

@ -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,

View File

@ -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;
}

View File

@ -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() {

View File

@ -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());

View File

@ -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;
}

View File

@ -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,

View File

@ -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);
}

View File

@ -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];

View File

@ -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,

View File

@ -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;
}

View File

@ -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() {