| 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 | import org.mozilla.javascript.annotations.JSFunction; |
| 9 | import org.mozilla.javascript.annotations.JSGetter; |
| 10 | |
| 11 | /** |
| 12 | * An example host object class. |
| 13 | * |
| 14 | * Here's a shell session showing the Foo object in action: |
| 15 | * <pre> |
| 16 | * js> defineClass("Foo") |
| 17 | * js> foo = new Foo(); <i>A constructor call, see <a href="#Foo">Foo</a> below.</i> |
| 18 | * [object Foo] <i>The "Foo" here comes from <a href"#getClassName">getClassName</a>.</i> |
| 19 | * js> foo.counter; <i>The counter property is defined by the <code>defineProperty</code></i> |
| 20 | * 0 <i>call below and implemented by the <a href="#getCounter">getCounter</a></i> |
| 21 | * js> foo.counter; <i>method below.</i> |
| 22 | * 1 |
| 23 | * js> foo.counter; |
| 24 | * 2 |
| 25 | * js> foo.resetCounter(); <i>Results in a call to <a href="#resetCounter">resetCounter</a>.</i> |
| 26 | * js> foo.counter; <i>Now the counter has been reset.</i> |
| 27 | * 0 |
| 28 | * js> foo.counter; |
| 29 | * 1 |
| 30 | * js> bar = new Foo(37); <i>Create a new instance.</i> |
| 31 | * [object Foo] |
| 32 | * js> bar.counter; <i>This instance's counter is distinct from</i> |
| 33 | * 37 <i>the other instance's counter.</i> |
| 34 | * js> foo.varargs(3, "hi"); <i>Calls <a href="#varargs">varargs</a>.</i> |
| 35 | * this = [object Foo]; args = [3, hi] |
| 36 | * js> foo[7] = 34; <i>Since we extended ScriptableObject, we get</i> |
| 37 | * 34 <i>all the behavior of a JavaScript object</i> |
| 38 | * js> foo.a = 23; <i>for free.</i> |
| 39 | * 23 |
| 40 | * js> foo.a + foo[7]; |
| 41 | * 57 |
| 42 | * js> |
| 43 | * </pre> |
| 44 | * |
| 45 | * @see org.mozilla.javascript.Context |
| 46 | * @see org.mozilla.javascript.Scriptable |
| 47 | * @see org.mozilla.javascript.ScriptableObject |
| 48 | * |
| 49 | * @author Norris Boyd |
| 50 | */ |
| 51 | |
| 52 | public class Foo extends ScriptableObject { |
| 53 | private static final long serialVersionUID = -3833489808933339159L; |
| 54 | |
| 55 | /** |
| 56 | * The zero-parameter constructor. |
| 57 | * |
| 58 | * When Context.defineClass is called with this class, it will |
| 59 | * construct Foo.prototype using this constructor. |
| 60 | */ |
| 61 | public Foo() { |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * The Java method defining the JavaScript Foo constructor. |
| 66 | * |
| 67 | * Takes an initial value for the counter property. |
| 68 | * Note that in the example Shell session above, we didn't |
| 69 | * supply a argument to the Foo constructor. This means that |
| 70 | * the Undefined value is used as the value of the argument, |
| 71 | * and when the argument is converted to an integer, Undefined |
| 72 | * becomes 0. |
| 73 | */ |
| 74 | public Foo(int counterStart) { |
| 75 | counter = counterStart; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the name of this JavaScript class, "Foo". |
| 80 | */ |
| 81 | @Override |
| 82 | public String getClassName() { |
| 83 | return "Foo"; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * The Java method defining the JavaScript resetCounter function. |
| 88 | * |
| 89 | * Resets the counter to 0. |
| 90 | */ |
| 91 | @JSFunction |
| 92 | public void resetCounter() { |
| 93 | counter = 0; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * The Java method implementing the getter for the counter property. |
| 98 | * <p> |
| 99 | * If "setCounter" had been defined in this class, the runtime would |
| 100 | * call the setter when the property is assigned to. |
| 101 | */ |
| 102 | @JSGetter |
| 103 | public int getCounter() { |
| 104 | return counter++; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * An example of a variable-arguments method. |
| 109 | * |
| 110 | * All variable arguments methods must have the same number and |
| 111 | * types of parameters, and must be static. <p> |
| 112 | * @param cx the Context of the current thread |
| 113 | * @param thisObj the JavaScript 'this' value. |
| 114 | * @param args the array of arguments for this call |
| 115 | * @param funObj the function object of the invoked JavaScript function |
| 116 | * This value is useful to compute a scope using |
| 117 | * Context.getTopLevelScope(). |
| 118 | * @return computes the string values and types of 'this' and |
| 119 | * of each of the supplied arguments and returns them in a string. |
| 120 | * |
| 121 | * @see org.mozilla.javascript.ScriptableObject#getTopLevelScope |
| 122 | */ |
| 123 | @JSFunction |
| 124 | public static Object varargs(Context cx, Scriptable thisObj, |
| 125 | Object[] args, Function funObj) |
| 126 | { |
| 127 | StringBuilder buf = new StringBuilder(); |
| 128 | buf.append("this = "); |
| 129 | buf.append(Context.toString(thisObj)); |
| 130 | buf.append("; args = ["); |
| 131 | for (int i=0; i < args.length; i++) { |
| 132 | buf.append(Context.toString(args[i])); |
| 133 | if (i+1 != args.length) |
| 134 | buf.append(", "); |
| 135 | } |
| 136 | buf.append("]"); |
| 137 | return buf.toString(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * A piece of private data for this class. |
| 142 | */ |
| 143 | private int counter; |
| 144 | } |
| 145 |