// JSRWrapper.java // FESI Copyright (c) Jean-Marc Lugrin, 1999 // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package FESI.Data; import FESI.jslib.*; import FESI.Exceptions.*; import FESI.Extensions.Extension; import FESI.Interpreter.Evaluator; import FESI.Interpreter.UserEvaluationSource; import java.io.Reader; /** * Package an EcmaScript object as a JSObject for use by the * Netscape like interface. */ public class JSWrapper implements JSObject { protected Evaluator evaluator; protected ESObject object; /** * Create a JSWraper for an EcmaScript object * @param object the EcmaScript object * @param evaluator theEvaluator */ public JSWrapper(ESObject object, Evaluator evaluator) { super(); this.object = object; this.evaluator = evaluator; } // overrides public ESObject getESObject() { return object; } /** * Return the global object attached to this object * @return the global object */ public JSGlobalObject getGlobalObject() { return new JSGlobalWrapper(evaluator.getGlobalObject(), evaluator); } /** * Implements the call the specified EcmaScript method of this object * * @param methodName The name of the method to call * @param args An array of parameters. * @return The result of the evaluation * @exception JSException For any error during interpretation */ public Object call(String methodName,Object args[]) throws JSException { Object obj = null; synchronized (evaluator) { try { ESValue function = object.getProperty(methodName, methodName.hashCode()); ESValue[] esargs = null; if (args == null) { esargs = new ESValue[0]; } else { esargs = new ESValue[args.length]; for (int i=0; i", null); } else { ses = new UserEvaluationSource(d, null); } ESValue value = evaluator.evaluate(r, object, ses, false); if (value != null) obj = value.toJavaObject(); } catch (EcmaScriptException e) { throw new JSException (e.getMessage(), e); } } return obj; } /** * Evaluate a Reader stream with this object as the 'this' object. * Consider the stream being a function program, allowing the * return statement. * * @param r The Reader stream to evaluate * @param d A description of the Reader for error messages * @return The result of the evaluation (null if no value returned) * @exception JSException For any error during interpretation */ public Object evalAsFunction(Reader r, String d) throws JSException { Object obj = null; synchronized (evaluator) { try { UserEvaluationSource ses; if (d==null) { ses = new UserEvaluationSource("", null); } else { ses = new UserEvaluationSource(d, null); } ESValue value = evaluator.evaluate(r, object, ses, true); if (value != null) obj = value.toJavaObject(); } catch (EcmaScriptException e) { throw new JSException (e.getMessage(), e); } } return obj; } /** * Implements the evaluation of a string with this object as the 'this' object. * The string is considered a function (top level return are allowed) * Passing the specified parameters (names and values must have the same length) * * @param s The string to evaluate * @return The result of the evaluation (null if no value returned) * @exception JSException For any error during interpretation */ public Object evalAsFunction(String s) throws JSException { Object obj = null; synchronized (evaluator) { try { ESValue value = evaluator.evaluate(s, object, true); // Can return null ! if (value != null) obj = value.toJavaObject(); } catch (EcmaScriptException e) { throw new JSException (e.getMessage(), e); } } return obj; /* // This work but is less efficient evalAsFunction(s, null, null); */ } /** * Evaluate a Reader stream with this object as the 'this' object. * Consider the stream being a function program, allowing the * return statement. * Passing the specified parameters (names and values must have the same length) * * @param r The Reader stream to evaluate * @param d A description of the Reader for error messages * @param names the names of the parameters * @param values the values of the parameters * @return The result of the evaluation (null if no value returned) * @exception JSException For any error during interpretation */ public Object evalAsFunction(Reader r, String d, String [] names, Object values[]) throws JSException { Object obj = null; throw new ProgrammingError("NOT IMPLEMENTED"); /* synchronized (evaluator) { try { UserEvaluationSource ses; if (d==null) { ses = new UserEvaluationSource("", null); } else { ses = new UserEvaluationSource(d, null); } ESValue value = evaluator.evaluate(r, object, ses, true); if (value != null) obj = value.toJavaObject(); } catch (EcmaScriptException e) { throw new JSException (e.getMessage(), e); } } return obj; */ } /** * Implements the evaluation of a string with this object as the 'this' object. * The string is considered a function (top level return are allowed) * * @param body The string to evaluate * @param names the names of the parameters * @param values the values of the parameters * @return The result of the evaluation (null if no value returned) * @exception JSException For any error during interpretation */ public Object evalAsFunction(String body, String [] names, Object values[]) throws JSException { Object obj = null; synchronized (evaluator) { try { // Create function int argLength = (names==null ? 0 : names.length); int checkLength = (values==null ? 0 : names.length); if (argLength!=checkLength) { throw new JSException("argument names and values arrays must have the same length, now: " + argLength + ", " + checkLength); } ESValue esArgs[] = new ESValue[argLength+1]; // space for body for (int i=0; i