~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to infrastructure/rhino1_7R1/src/org/mozilla/javascript/NativeObject.java

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: MPL 1.1/GPL 2.0
 
5
 *
 
6
 * The contents of this file are subject to the Mozilla Public License Version
 
7
 * 1.1 (the "License"); you may not use this file except in compliance with
 
8
 * the License. You may obtain a copy of the License at
 
9
 * http://www.mozilla.org/MPL/
 
10
 *
 
11
 * Software distributed under the License is distributed on an "AS IS" basis,
 
12
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
13
 * for the specific language governing rights and limitations under the
 
14
 * License.
 
15
 *
 
16
 * The Original Code is Rhino code, released
 
17
 * May 6, 1999.
 
18
 *
 
19
 * The Initial Developer of the Original Code is
 
20
 * Netscape Communications Corporation.
 
21
 * Portions created by the Initial Developer are Copyright (C) 1997-1999
 
22
 * the Initial Developer. All Rights Reserved.
 
23
 *
 
24
 * Contributor(s):
 
25
 *   Norris Boyd
 
26
 *   Igor Bukanov
 
27
 *   Bob Jervis
 
28
 *   Mike McCabe
 
29
 *
 
30
 * Alternatively, the contents of this file may be used under the terms of
 
31
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 
32
 * case the provisions of the GPL are applicable instead of those above. If
 
33
 * you wish to allow use of your version of this file only under the terms of
 
34
 * the GPL and not to allow others to use your version of this file under the
 
35
 * MPL, indicate your decision by deleting the provisions above and replacing
 
36
 * them with the notice and other provisions required by the GPL. If you do
 
37
 * not delete the provisions above, a recipient may use your version of this
 
38
 * file under either the MPL or the GPL.
 
39
 *
 
40
 * ***** END LICENSE BLOCK ***** */
 
41
 
 
42
package org.mozilla.javascript;
 
43
 
 
44
/**
 
45
 * This class implements the Object native object.
 
46
 * See ECMA 15.2.
 
47
 * @author Norris Boyd
 
48
 */
 
49
public class NativeObject extends IdScriptableObject
 
50
{
 
51
    static final long serialVersionUID = -6345305608474346996L;
 
52
 
 
53
    private static final Object OBJECT_TAG = new Object();
 
54
 
 
55
    static void init(Scriptable scope, boolean sealed)
 
56
    {
 
57
        NativeObject obj = new NativeObject();
 
58
        obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
 
59
    }
 
60
 
 
61
    public String getClassName()
 
62
    {
 
63
        return "Object";
 
64
    }
 
65
 
 
66
    public String toString()
 
67
    {
 
68
        return ScriptRuntime.defaultObjectToString(this);
 
69
    }
 
70
 
 
71
    protected void initPrototypeId(int id)
 
72
    {
 
73
        String s;
 
74
        int arity;
 
75
        switch (id) {
 
76
          case Id_constructor:    arity=1; s="constructor";    break;
 
77
          case Id_toString:       arity=0; s="toString";       break;
 
78
          case Id_toLocaleString: arity=0; s="toLocaleString"; break;
 
79
          case Id_valueOf:        arity=0; s="valueOf";        break;
 
80
          case Id_hasOwnProperty: arity=1; s="hasOwnProperty"; break;
 
81
          case Id_propertyIsEnumerable:
 
82
            arity=1; s="propertyIsEnumerable"; break;
 
83
          case Id_isPrototypeOf:  arity=1; s="isPrototypeOf";  break;
 
84
          case Id_toSource:       arity=0; s="toSource";       break;
 
85
          case Id___defineGetter__:
 
86
            arity=2; s="__defineGetter__";     break;
 
87
          case Id___defineSetter__:
 
88
            arity=2; s="__defineSetter__";     break;
 
89
          case Id___lookupGetter__:
 
90
            arity=1; s="__lookupGetter__";     break;
 
91
          case Id___lookupSetter__:
 
92
            arity=1; s="__lookupSetter__";     break;
 
93
          default: throw new IllegalArgumentException(String.valueOf(id));
 
94
        }
 
95
        initPrototypeMethod(OBJECT_TAG, id, s, arity);
 
96
    }
 
97
 
 
98
    public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
 
99
                             Scriptable thisObj, Object[] args)
 
100
    {
 
101
        if (!f.hasTag(OBJECT_TAG)) {
 
102
            return super.execIdCall(f, cx, scope, thisObj, args);
 
103
        }
 
104
        int id = f.methodId();
 
105
        switch (id) {
 
106
          case Id_constructor: {
 
107
            if (thisObj != null) {
 
108
                // BaseFunction.construct will set up parent, proto
 
109
                return f.construct(cx, scope, args);
 
110
            }
 
111
            if (args.length == 0 || args[0] == null
 
112
                || args[0] == Undefined.instance)
 
113
            {
 
114
                return new NativeObject();
 
115
            }
 
116
            return ScriptRuntime.toObject(cx, scope, args[0]);
 
117
          }
 
118
 
 
119
          case Id_toLocaleString: // For now just alias toString
 
120
          case Id_toString: {
 
121
            if (cx.hasFeature(Context.FEATURE_TO_STRING_AS_SOURCE)) {
 
122
                String s = ScriptRuntime.defaultObjectToSource(cx, scope,
 
123
                                                               thisObj, args);
 
124
                int L = s.length();
 
125
                if (L != 0 && s.charAt(0) == '(' && s.charAt(L - 1) == ')') {
 
126
                    // Strip () that surrounds toSource
 
127
                    s = s.substring(1, L - 1);
 
128
                }
 
129
                return s;
 
130
            }
 
131
            return ScriptRuntime.defaultObjectToString(thisObj);
 
132
          }
 
133
 
 
134
          case Id_valueOf:
 
135
            return thisObj;
 
136
 
 
137
          case Id_hasOwnProperty: {
 
138
            boolean result;
 
139
            if (args.length == 0) {
 
140
                result = false;
 
141
            } else {
 
142
                String s = ScriptRuntime.toStringIdOrIndex(cx, args[0]);
 
143
                if (s == null) {
 
144
                    int index = ScriptRuntime.lastIndexResult(cx);
 
145
                    result = thisObj.has(index, thisObj);
 
146
                } else {
 
147
                    result = thisObj.has(s, thisObj);
 
148
                }
 
149
            }
 
150
            return ScriptRuntime.wrapBoolean(result);
 
151
          }
 
152
 
 
153
          case Id_propertyIsEnumerable: {
 
154
            boolean result;
 
155
            if (args.length == 0) {
 
156
                result = false;
 
157
            } else {
 
158
                String s = ScriptRuntime.toStringIdOrIndex(cx, args[0]);
 
159
                if (s == null) {
 
160
                    int index = ScriptRuntime.lastIndexResult(cx);
 
161
                    result = thisObj.has(index, thisObj);
 
162
                    if (result && thisObj instanceof ScriptableObject) {
 
163
                        ScriptableObject so = (ScriptableObject)thisObj;
 
164
                        int attrs = so.getAttributes(index);
 
165
                        result = ((attrs & ScriptableObject.DONTENUM) == 0);
 
166
                    }
 
167
                } else {
 
168
                    result = thisObj.has(s, thisObj);
 
169
                    if (result && thisObj instanceof ScriptableObject) {
 
170
                        ScriptableObject so = (ScriptableObject)thisObj;
 
171
                        int attrs = so.getAttributes(s);
 
172
                        result = ((attrs & ScriptableObject.DONTENUM) == 0);
 
173
                    }
 
174
                }
 
175
            }
 
176
            return ScriptRuntime.wrapBoolean(result);
 
177
          }
 
178
 
 
179
          case Id_isPrototypeOf: {
 
180
            boolean result = false;
 
181
            if (args.length != 0 && args[0] instanceof Scriptable) {
 
182
                Scriptable v = (Scriptable) args[0];
 
183
                do {
 
184
                    v = v.getPrototype();
 
185
                    if (v == thisObj) {
 
186
                        result = true;
 
187
                        break;
 
188
                    }
 
189
                } while (v != null);
 
190
            }
 
191
            return ScriptRuntime.wrapBoolean(result);
 
192
          }
 
193
 
 
194
          case Id_toSource:
 
195
            return ScriptRuntime.defaultObjectToSource(cx, scope, thisObj,
 
196
                                                       args);
 
197
          case Id___defineGetter__:
 
198
          case Id___defineSetter__:
 
199
            {
 
200
                if (args.length < 2 || !(args[1] instanceof Callable)) {
 
201
                    Object badArg = (args.length >= 2 ? args[1]
 
202
                                     : Undefined.instance);
 
203
                    throw ScriptRuntime.notFunctionError(badArg);
 
204
                }
 
205
                if (!(thisObj instanceof ScriptableObject)) {
 
206
                    throw Context.reportRuntimeError2(
 
207
                        "msg.extend.scriptable",
 
208
                        thisObj.getClass().getName(),
 
209
                        String.valueOf(args[0]));
 
210
                }
 
211
                ScriptableObject so = (ScriptableObject)thisObj;
 
212
                String name = ScriptRuntime.toStringIdOrIndex(cx, args[0]);
 
213
                int index = (name != null ? 0
 
214
                             : ScriptRuntime.lastIndexResult(cx));
 
215
                Callable getterOrSetter = (Callable)args[1];
 
216
                boolean isSetter = (id == Id___defineSetter__);
 
217
                so.setGetterOrSetter(name, index, getterOrSetter, isSetter);
 
218
                if (so instanceof NativeArray)
 
219
                    ((NativeArray)so).setDenseOnly(false);
 
220
            }
 
221
            return Undefined.instance;
 
222
 
 
223
            case Id___lookupGetter__:
 
224
            case Id___lookupSetter__:
 
225
              {
 
226
                  if (args.length < 1 ||
 
227
                      !(thisObj instanceof ScriptableObject))
 
228
                      return Undefined.instance;
 
229
                  
 
230
                  ScriptableObject so = (ScriptableObject)thisObj;
 
231
                  String name = ScriptRuntime.toStringIdOrIndex(cx, args[0]);
 
232
                  int index = (name != null ? 0
 
233
                               : ScriptRuntime.lastIndexResult(cx));
 
234
                  boolean isSetter = (id == Id___lookupSetter__);
 
235
                  Object gs;
 
236
                  for (;;) {
 
237
                      gs = so.getGetterOrSetter(name, index, isSetter);
 
238
                      if (gs != null)
 
239
                          break;
 
240
                      // If there is no getter or setter for the object itself,
 
241
                      // how about the prototype?
 
242
                      Scriptable v = so.getPrototype();
 
243
                      if (v == null)
 
244
                          break;
 
245
                      if (v instanceof ScriptableObject)
 
246
                          so = (ScriptableObject)v;
 
247
                      else
 
248
                          break;
 
249
                  }
 
250
                  if (gs != null)
 
251
                      return gs;
 
252
              }
 
253
              return Undefined.instance;
 
254
 
 
255
          default:
 
256
            throw new IllegalArgumentException(String.valueOf(id));
 
257
        }
 
258
    }
 
259
 
 
260
// #string_id_map#
 
261
 
 
262
    protected int findPrototypeId(String s)
 
263
    {
 
264
        int id;
 
265
// #generated# Last update: 2007-05-09 08:15:55 EDT
 
266
        L0: { id = 0; String X = null; int c;
 
267
            L: switch (s.length()) {
 
268
            case 7: X="valueOf";id=Id_valueOf; break L;
 
269
            case 8: c=s.charAt(3);
 
270
                if (c=='o') { X="toSource";id=Id_toSource; }
 
271
                else if (c=='t') { X="toString";id=Id_toString; }
 
272
                break L;
 
273
            case 11: X="constructor";id=Id_constructor; break L;
 
274
            case 13: X="isPrototypeOf";id=Id_isPrototypeOf; break L;
 
275
            case 14: c=s.charAt(0);
 
276
                if (c=='h') { X="hasOwnProperty";id=Id_hasOwnProperty; }
 
277
                else if (c=='t') { X="toLocaleString";id=Id_toLocaleString; }
 
278
                break L;
 
279
            case 16: c=s.charAt(2);
 
280
                if (c=='d') {
 
281
                    c=s.charAt(8);
 
282
                    if (c=='G') { X="__defineGetter__";id=Id___defineGetter__; }
 
283
                    else if (c=='S') { X="__defineSetter__";id=Id___defineSetter__; }
 
284
                }
 
285
                else if (c=='l') {
 
286
                    c=s.charAt(8);
 
287
                    if (c=='G') { X="__lookupGetter__";id=Id___lookupGetter__; }
 
288
                    else if (c=='S') { X="__lookupSetter__";id=Id___lookupSetter__; }
 
289
                }
 
290
                break L;
 
291
            case 20: X="propertyIsEnumerable";id=Id_propertyIsEnumerable; break L;
 
292
            }
 
293
            if (X!=null && X!=s && !X.equals(s)) id = 0;
 
294
            break L0;
 
295
        }
 
296
// #/generated#
 
297
        return id;
 
298
    }
 
299
 
 
300
    private static final int
 
301
        Id_constructor           = 1,
 
302
        Id_toString              = 2,
 
303
        Id_toLocaleString        = 3,
 
304
        Id_valueOf               = 4,
 
305
        Id_hasOwnProperty        = 5,
 
306
        Id_propertyIsEnumerable  = 6,
 
307
        Id_isPrototypeOf         = 7,
 
308
        Id_toSource              = 8,
 
309
        Id___defineGetter__      = 9,
 
310
        Id___defineSetter__      = 10,
 
311
        Id___lookupGetter__      = 11,
 
312
        Id___lookupSetter__      = 12,
 
313
        MAX_PROTOTYPE_ID         = 12;
 
314
 
 
315
// #/string_id_map#
 
316
}