Use the application's classloader for the Packages object if available.

Merged in changes from FESI 1.1.5
This commit is contained in:
hns 2002-11-29 17:56:20 +00:00
parent 148cd5de94
commit 19fd7b0f7d

View file

@ -33,6 +33,8 @@ import java.util.StringTokenizer;
import java.io.*; import java.io.*;
import java.util.zip.*; import java.util.zip.*;
import helma.scripting.fesi.FesiEngine;
/** /**
* Defines the evaluation interface and contains the evaluation context. * Defines the evaluation interface and contains the evaluation context.
* <P><B>Important:</B> This object is also used as the synchronization * <P><B>Important:</B> This object is also used as the synchronization
@ -41,11 +43,11 @@ import java.util.zip.*;
* for speed reasons. * for speed reasons.
*/ */
public class Evaluator { public class Evaluator {
// used to stop thread, 06.12.99 Hannes Wallnoefer // used to stop thread, 06.12.99 Hannes Wallnoefer
public volatile Thread thread; public volatile Thread thread;
// used to retrieve wrappers with correct Prototype for path elements in ESLoader // used to retrieve wrappers with correct Prototype for path elements in ESLoader
public helma.scripting.fesi.FesiEngine engine; public final FesiEngine engine;
private static String eol = System.getProperty("line.separator", "\n"); private static String eol = System.getProperty("line.separator", "\n");
@ -53,7 +55,7 @@ public class Evaluator {
* Return the version identifier of the interpreter * Return the version identifier of the interpreter
*/ */
public static String getVersion() { public static String getVersion() {
return "1.1.4 (30-Jan-2000)"; return "1.1.5 (29-July-2000)";
} }
/** /**
@ -104,16 +106,25 @@ public class Evaluator {
varDeclarationVisitor = new EcmaScriptVariableVisitor(this); varDeclarationVisitor = new EcmaScriptVariableVisitor(this);
// evaluationVisitor = new EcmaScriptEvaluateVisitor(this); // evaluationVisitor = new EcmaScriptEvaluateVisitor(this);
globalObject = GlobalObject.makeGlobalObject(this); globalObject = GlobalObject.makeGlobalObject(this);
packageObject = new ESPackages(this); packageObject = engine == null ?
new ESPackages(this) :
new ESPackages(this, engine.getClassLoader ());
extensions = new Hashtable(); // forget extensions extensions = new Hashtable(); // forget extensions
} }
/** /**
* Create a new empty evaluator * Create a new empty evaluator
*/ */
public Evaluator () { public Evaluator () {
this.engine = null;
reset();
}
/**
* Create a new empty evaluator
*/
public Evaluator (FesiEngine engine) {
this.engine = engine;
reset(); reset();
} }
@ -283,6 +294,7 @@ public class Evaluator {
*/ */
public void setDatePrototype(ESObject o) { public void setDatePrototype(ESObject o) {
datePrototype = o; datePrototype = o;
} }
/** /**
* Get the Date prototype object * Get the Date prototype object
@ -300,7 +312,6 @@ public class Evaluator {
return packageObject; return packageObject;
} }
//------------------------------------------------------------ //------------------------------------------------------------
// Extension support // Extension support
//------------------------------------------------------------ //------------------------------------------------------------
@ -503,11 +514,16 @@ public class Evaluator {
*/ */
public ESValue evaluateEvalString(String theSource) throws EcmaScriptException { public ESValue evaluateEvalString(String theSource) throws EcmaScriptException {
ESValue theValue = ESUndefined.theUndefined; ESValue theValue = ESUndefined.theUndefined;
ASTProgram programNode = null;
StringEvaluationSource es = new StringEvaluationSource(theSource, null);
// Hack - this ensures correct parsing of // comments
// even if no EOL is present (as in an eval('1//a'))
if (!theSource.endsWith("\n")) {
theSource += "\n";
}
java.io.StringReader is = java.io.StringReader is =
new java.io.StringReader(theSource); new java.io.StringReader(theSource);
EcmaScript parser = new EcmaScript(is); EcmaScript parser = new EcmaScript(is);
ASTProgram programNode = null;
StringEvaluationSource es = new StringEvaluationSource(theSource, null);
try { try {
// ASTProgram n = parser.Program(); // ASTProgram n = parser.Program();
programNode = (ASTProgram)parser.Program(); programNode = (ASTProgram)parser.Program();
@ -760,7 +776,6 @@ public class Evaluator {
// currentEvaluationSource = es; // currentEvaluationSource = es;
try { try {
for (Enumeration e = localVariableNames.elements() ; e.hasMoreElements() ;) { for (Enumeration e = localVariableNames.elements() ; e.hasMoreElements() ;) {
String variable =(String)(e.nextElement()); String variable =(String)(e.nextElement());
createVariable(variable, variable.hashCode()); createVariable(variable, variable.hashCode());
} }
@ -836,6 +851,8 @@ public class Evaluator {
EcmaScriptEvaluateVisitor evaluationVisitor = new EcmaScriptEvaluateVisitor(this); EcmaScriptEvaluateVisitor evaluationVisitor = new EcmaScriptEvaluateVisitor(this);
try { try {
// Hannes Wallnoefer, 2002/11/29: Pass thisObject to EcmaScriptFunctionVisitor so that
// functions are assigned to the prototype instead of the global object
functionDeclarationVisitor.processFunctionDeclarations(node, program.getEvaluationSource(), thisObject); functionDeclarationVisitor.processFunctionDeclarations(node, program.getEvaluationSource(), thisObject);
Vector variables = program.getVariableNames(); Vector variables = program.getVariableNames();
for (Enumeration e = variables.elements() ; e.hasMoreElements() ;) { for (Enumeration e = variables.elements() ; e.hasMoreElements() ;) {
@ -973,6 +990,11 @@ public class Evaluator {
ESValue v = null; ESValue v = null;
EvaluationSource es = new UserEvaluationSource(source, null); EvaluationSource es = new UserEvaluationSource(source, null);
try { try {
// Hack - this ensures correct parsing of // comments
// even if no EOL is present (as in an eveal command)/
if (!text.endsWith("\n")) {
text += "\n";
}
is = new java.io.StringReader(text); is = new java.io.StringReader(text);
v = evaluate(is, globalObject, es, false); v = evaluate(is, globalObject, es, false);
} finally { } finally {
@ -1004,7 +1026,6 @@ public class Evaluator {
synchronized public ESValue evaluate(File file, ESObject thisObject) synchronized public ESValue evaluate(File file, ESObject thisObject)
throws EcmaScriptException, IOException { throws EcmaScriptException, IOException {
EvaluationSource es = new FileEvaluationSource(file.getPath(), null); EvaluationSource es = new FileEvaluationSource(file.getPath(), null);
FileReader fr = null; FileReader fr = null;
ESValue value = null; ESValue value = null;
try { try {
@ -1050,6 +1071,11 @@ public class Evaluator {
java.io.StringReader is = null; java.io.StringReader is = null;
ESValue v = null; ESValue v = null;
EvaluationSource es = new StringEvaluationSource(theSource, null); EvaluationSource es = new StringEvaluationSource(theSource, null);
// Hack - this ensures correct parsing of // comments
// even if no EOL is present (as in an eveal command)/
if (!theSource.endsWith("\n")) {
theSource += "\n";
}
try { try {
is = new java.io.StringReader(theSource); is = new java.io.StringReader(theSource);
v = evaluate(is, thisObject, es, returnAccepted); v = evaluate(is, thisObject, es, returnAccepted);