aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js
blob: 85e2161c004eac5e58804c47b7d5a305e92f5757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * This script contains non-standard, Mozilla compatibility functionality on
 * the standard global objects. Please note that it is incomplete. Only the most
 * often used functionality is supported.
 */

// JavaAdapter
Object.defineProperty(this, "JavaAdapter", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        if (arguments.length < 2) {
            throw new TypeError("JavaAdapter requires atleast two arguments");
        }

        var types = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
        var NewType = Java.extend.apply(Java, types);
        return new NewType(arguments[arguments.length - 1]);
    }
});


// importPackage
// avoid unnecessary chaining of __noSuchProperty__ again
// in case user loads this script more than once.
if (typeof importPackage == 'undefined') {

Object.defineProperty(this, "importPackage", {
    configurable: true, enumerable: false, writable: true,
    value: (function() {
        var _packages = [];
        var global = this;
        var oldNoSuchProperty = global.__noSuchProperty__;
        var __noSuchProperty__ = function(name) {
            'use strict';
            for (var i in _packages) {
                try {
                    var type = Java.type(_packages[i] + "." + name);
                    global[name] = type;
                    return type;
                } catch (e) {}
            }

            if (oldNoSuchProperty) {
                return oldNoSuchProperty.call(this, name);
            } else {
                if (this === undefined) {
                    throw new ReferenceError(name + " is not defined");
                } else {
                    return undefined;
                }
            }
        }

        Object.defineProperty(global, "__noSuchProperty__", {
            writable: true, configurable: true, enumerable: false,
            value: __noSuchProperty__
        });

        var prefix = "[JavaPackage ";
        return function() {
            for (var i in arguments) {
                var pkgName = arguments[i];
                if ((typeof pkgName) != 'string') {
                    pkgName = String(pkgName);
                    // extract name from JavaPackage object
                    if (pkgName.startsWith(prefix)) {
                        pkgName = pkgName.substring(prefix.length, pkgName.length - 1);
                    }
                }
                _packages.push(pkgName);
            }
        }
    })()
});

}

// sync
Object.defineProperty(this, "sync", {
    configurable: true, enumerable: false, writable: true,
    value: function(func, syncobj) {
        if (arguments.length < 1 || arguments.length > 2 ) {
            throw "sync(function [,object]) parameter count mismatch";
        }
        return Packages.jdk.nashorn.api.scripting.ScriptUtils.makeSynchronizedFunction(func, syncobj);
    }
});

// Object.prototype.__defineGetter__
Object.defineProperty(Object.prototype, "__defineGetter__", {
    configurable: true, enumerable: false, writable: true,
    value: function(name, func) {
        Object.defineProperty(this, name, {
            configurable: true, enumerable: true, get: func });
    }
});

// Object.prototype.__defineSetter__
Object.defineProperty(Object.prototype, "__defineSetter__", {
    configurable: true, enumerable: false, writable: true,
    value: function(name, func) {
        Object.defineProperty(this, name, {
            configurable: true, enumerable: true, set: func });
    }
});

// Object.prototype.__lookupGetter__
Object.defineProperty(Object.prototype, "__lookupGetter__", {
    configurable: true, enumerable: false, writable: true,
    value: function(name) {
        var obj = this;
        while (obj) {
            var desc = Object.getOwnPropertyDescriptor(obj, name);
            if (desc) return desc.get;
            obj = Object.getPrototypeOf(obj);
        }
        return undefined;
    }
});

// Object.prototype.__lookupSetter__
Object.defineProperty(Object.prototype, "__lookupSetter__", {
    configurable: true, enumerable: false, writable: true,
    value: function(name) {
        var obj = this;
        while (obj) {
            var desc = Object.getOwnPropertyDescriptor(obj, name);
            if (desc) return desc.set;
            obj = Object.getPrototypeOf(obj);
        }
        return undefined;
    }
});

// Object.prototype.toSource
Object.defineProperty(Object.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function(state) {
        if (! state) {
            state = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap());
        }
        if (state.contains(this)) {
            return "{}";
        }
        state.add(this);
        var str = new java.lang.StringBuffer('({');
        for (i in this) {
            str.append(i);
            str.append(':');
            if (this[i] instanceof Object && typeof(this[i].toSource) == 'function') {
                str.append(this[i].toSource(state));
            } else {
                str.append(String(this[i]));
            }
            str.append(', ');
        }
        // delete last extra command and space
        str = str.deleteCharAt(str.length() - 1);
        str = str.deleteCharAt(str.length() - 1);
        str.append('})');
        return str.toString();
    }
});

// Boolean.prototype.toSource
Object.defineProperty(Boolean.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '(new Boolean(' + this.toString() + '))';
    }
});

// Date.prototype.toSource
Object.defineProperty(Date.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '(new Date(' + this.valueOf() + '))';
    }
});

// Function.prototype.toSource -- already implemented in nashorn

// Number.prototype.toSource
Object.defineProperty(Number.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '(new Number(' + this.toString() + '))';
    }
});

// RegExp.prototype.toSource
Object.defineProperty(RegExp.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return this.toString();
    }
});

// String.prototype.toSource
Object.defineProperty(String.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '(new String(' + this.quote() + '))';
    }
});

// Error.prototype.toSource
Object.defineProperty(Error.prototype, "toSource", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        var msg = this.message? String(this.message).quote() : "''";
        return '(new ' + this.name + '(' + msg + '))';
    }
});

// Function.prototype.arity
Object.defineProperty(Function.prototype, "arity", {
    configurable: true, enumerable: false,
    get: function() { return this.length; },
    set: function(x) {
        throw new TypeError("Function arity can not be modified");
    }
});

// String.prototype.quote
Object.defineProperty(String.prototype, "quote", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return JSON.stringify(this);
    }
});

// HTML generation methods of String.prototype

// String.prototype.anchor
Object.defineProperty(String.prototype, "anchor", {
    configurable: true, enumerable: false, writable: true,
    value: function(name) {
        return '<a name="' + name + '">' + this + '</a>';
    }
});

// String.prototype.big
Object.defineProperty(String.prototype, "big", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<big>' + this + '</big>';
    }
});

// String.prototype.blink
Object.defineProperty(String.prototype, "blink", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<blink>' + this + '</blink>';
    }
});

// String.prototype.bold
Object.defineProperty(String.prototype, "bold", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<b>' + this + '</b>';
    }
});

// String.prototype.fixed
Object.defineProperty(String.prototype, "fixed", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<tt>' + this + '</tt>';
    }
});

// String.prototype.fontcolor
Object.defineProperty(String.prototype, "fontcolor", {
    configurable: true, enumerable: false, writable: true,
    value: function(clr) {
        return '<font color="' + clr + '">' + this + '</font>';
    }
});

// String.prototype.fontsize
Object.defineProperty(String.prototype, "fontsize", {
    configurable: true, enumerable: false, writable: true,
    value: function(size) {
        return '<font size="' + size + '">' + this + '</font>';
    }
});

// String.prototype.italics
Object.defineProperty(String.prototype, "italics", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<i>' + this + '</i>';
    }
});

// String.prototype.link
Object.defineProperty(String.prototype, "link", {
    configurable: true, enumerable: false, writable: true,
    value: function(url) {
        return '<a href="' + url + '">' + this + '</a>';
    }
});

// String.prototype.small
Object.defineProperty(String.prototype, "small", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<small>' + this + '</small>';
    }
});

// String.prototype.strike
Object.defineProperty(String.prototype, "strike", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<strike>' + this + '</strike>';
    }
});

// String.prototype.sub
Object.defineProperty(String.prototype, "sub", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<sub>' + this + '</sub>';
    }
});

// String.prototype.sup
Object.defineProperty(String.prototype, "sup", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        return '<sup>' + this + '</sup>';
    }
});

// Rhino: global.importClass
Object.defineProperty(this, "importClass", {
    configurable: true, enumerable: false, writable: true,
    value: function() {
        for (var arg in arguments) {
            var clazz = arguments[arg];
            if (Java.isType(clazz)) {
                var className = Java.typeName(clazz);
                var simpleName = className.substring(className.lastIndexOf('.') + 1);
                this[simpleName] = clazz;
            } else {
                throw new TypeError(clazz + " is not a Java class");
            }
        }
    }
});