Debian Codegraph @

rhino/examples/RunScript3.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 import org.mozilla.javascript.*;
8
9 /**
10 * RunScript3: Example of using JavaScript objects
11 *
12 * Collects its arguments from the command line, executes the
13 * script, and then ...
14 *
15 * @author Norris Boyd
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 // Collect the arguments into a single string.
25 String s = "";
26 for (int i=0; i < args.length; i++) {
27 s += args[i];
28 }
29
30 // Now evaluate the string we've collected. We'll ignore the result.
31 cx.evaluateString(scope, s, "<cmd>", 1, null);
32
33 // Print the value of variable "x"
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 // Call function "f('my arg')" and print its result.
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