Debian Codegraph @

rhino/examples/RunScript4.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 * RunScript4: Execute scripts in an environment that includes the
11 * example Counter class.
12 *
13 * @author Norris Boyd
14 */
15 public class RunScript4 {
16 public static void main(String args[])
17 throws Exception
18 {
19 Context cx = Context.enter();
20 try {
21 Scriptable scope = cx.initStandardObjects();
22
23 // Use the Counter class to define a Counter constructor
24 // and prototype in JavaScript.
25 ScriptableObject.defineClass(scope, Counter.class);
26
27 // Create an instance of Counter and assign it to
28 // the top-level variable "myCounter". This is
29 // equivalent to the JavaScript code
30 // myCounter = new Counter(7);
31 Object[] arg = { new Integer(7) };
32 Scriptable myCounter = cx.newObject(scope, "Counter", arg);
33 scope.put("myCounter", scope, myCounter);
34
35 String s = "";
36 for (int i=0; i < args.length; i++) {
37 s += args[i];
38 }
39 Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
40 System.err.println(Context.toString(result));
41 } finally {
42 Context.exit();
43 }
44 }
45
46 }
47