| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import org.mozilla.javascript.*; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class RunScript3 { |
| 18 |
public static void main(String args[]) |
| 19 |
{ |
| 20 |
Context cx = Context.enter(); |
| 21 |
try { |
| 22 |
Scriptable scope = cx.initStandardObjects(); |
| 23 |
|
| 24 |
|
| 25 |
String s = ""; |
| 26 |
for (int i=0; i < args.length; i++) { |
| 27 |
s += args[i]; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
cx.evaluateString(scope, s, "<cmd>", 1, null); |
| 32 |
|
| 33 |
|
| 34 |
Object x = scope.get("x", scope); |
| 35 |
if (x == Scriptable.NOT_FOUND) { |
| 36 |
System.out.println("x is not defined."); |
| 37 |
} else { |
| 38 |
System.out.println("x = " + Context.toString(x)); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
Object fObj = scope.get("f", scope); |
| 43 |
if (!(fObj instanceof Function)) { |
| 44 |
System.out.println("f is undefined or not a function."); |
| 45 |
} else { |
| 46 |
Object functionArgs[] = { "my arg" }; |
| 47 |
Function f = (Function)fObj; |
| 48 |
Object result = f.call(cx, scope, scope, functionArgs); |
| 49 |
String report = "f('my args') = " + Context.toString(result); |
| 50 |
System.out.println(report); |
| 51 |
} |
| 52 |
} finally { |
| 53 |
Context.exit(); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|