MCPcopy Create free account
hub / github.com/mozilla/rhino / main

Method main

examples/src/main/java/RunScript4.java:70–96  ·  view source on GitHub ↗

Main method that demonstrates creating and using custom JavaScript classes from Java objects. This method shows advanced JavaScript-Java integration by defining a custom JavaScript class (Counter) based on a Java class, creating an instance with initial parameters, and making it available to Jav

(String args[])

Source from the content-addressed store, hash-verified

68 * reflection errors during class definition.
69 */
70 public static void main(String args[]) throws Exception {
71 Context cx = Context.enter();
72 try {
73 TopLevel scope = cx.initStandardObjects();
74
75 // Use the Counter class to define a Counter constructor
76 // and prototype in JavaScript.
77 ScriptableObject.defineClass(scope, Counter.class);
78
79 // Create an instance of Counter and assign it to
80 // the top-level variable "myCounter". This is
81 // equivalent to the JavaScript code
82 // myCounter = new Counter(7);
83 Object[] arg = {Integer.valueOf(7)};
84 Scriptable myCounter = cx.newObject(scope, "Counter", arg);
85 scope.put("myCounter", scope, myCounter);
86
87 String s = "";
88 for (int i = 0; i < args.length; i++) {
89 s += args[i];
90 }
91 Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
92 System.err.println(Context.toString(result));
93 } finally {
94 Context.exit();
95 }
96 }
97
98 /** Private constructor to prevent instantiation of this utility class. */
99 private RunScript4() {

Callers

nothing calls this directly

Calls 10

enterMethod · 0.95
initStandardObjectsMethod · 0.95
defineClassMethod · 0.95
newObjectMethod · 0.95
putMethod · 0.95
evaluateStringMethod · 0.95
toStringMethod · 0.95
exitMethod · 0.95
printlnMethod · 0.65
valueOfMethod · 0.45

Tested by

no test coverage detected