01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rewriter.test.util;
06:
07: import org.mozilla.javascript.Context;
08: import org.mozilla.javascript.Function;
09: import org.mozilla.javascript.Scriptable;
10:
11: public class JScriptInterpreter {
12: public static String exec(String aFunctionDef, String aFuncName,
13: String[] aFunctionArgs) throws Exception {
14: String result = (String) execObjectArray(aFunctionDef,
15: aFuncName, aFunctionArgs, String.class);
16: return result;
17: }//exec()
18:
19: public static Object execObject(String aFunctionDef,
20: String aFuncName, Object[] aFunctionArgs) throws Exception {
21: return execObjectArray(aFunctionDef, aFuncName, aFunctionArgs,
22: Object.class);
23: }//execObject()
24:
25: public static Object execObject(String aFunctionDef,
26: String aFuncName, Object[] aFunctionArgs, Class aClass)
27: throws Exception {
28: return execObjectArray(aFunctionDef, aFuncName, aFunctionArgs,
29: aClass);
30: }//execObject()
31:
32: private static Object execObjectArray(String aFunctionDef,
33: String aFuncName, Object[] aFunctionArgs, Class aClass)
34: throws Exception {
35:
36: Context lContext = Context.enter();
37: try {
38: Scriptable lScriptable = lContext.initStandardObjects(null);
39: lContext.evaluateString(lScriptable, aFunctionDef, "<cmd>",
40: 1, null);
41: Object function = lScriptable.get(aFuncName, lScriptable);
42: Object returnedObject = ((Function) function).call(
43: lContext, lScriptable, lScriptable, aFunctionArgs);
44: return Context.toType(returnedObject, aClass);
45: } finally {
46: lContext.exit();
47: }
48: }//execObject()
49:
50: }//class JScriptInterpreter
|