jsadapter.js revision 6:5a1b0714df0e
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * Verify that JSAdapter works as expected.
26 *
27 * @test
28 * @run
29 */
30
31var obj = new JSAdapter() {
32    __get__: function(name) {
33        print("getter called for '" + name + "'"); return name;
34    },
35
36    __put__: function(name, value) {
37        print("setter called for '" + name + "' with " + value);
38    },
39
40    __call__: function(name, arg1, arg2) {
41        print("method '" + name + "' called with " + arg1 + ", " + arg2);
42    },
43
44    __new__: function(arg1, arg2) {
45        print("new with " + arg1 + ", " + arg2);
46    },
47
48    __getIds__: function() {
49        print("__getIds__ called");
50        return [ "foo", "bar" ];
51    },
52
53    __getValues__: function() {
54        print("__getValues__ called");
55        return [ "fooval", "barval" ];
56    },
57
58    __has__: function(name) {
59        print("__has__ called with '" + name + "'");
60        return name == "js";
61    },
62
63    __delete__: function(name) {
64        print("__delete__ called with '" + name + "'");
65        return true;
66    }
67};
68
69// calls __get__
70print(obj.foo);
71
72// calls __put__
73obj.foo = 33;
74
75// calls __call__
76obj.func("hello", "world");
77
78// calls __new__
79new obj("hey!", "it works!");
80
81for (i in obj) {
82    print(i);
83}
84
85for each (i in obj) {
86    print(i);
87}
88
89var x = "foo" in obj;
90print(x);
91
92var y = "js" in obj;
93print(y);
94
95print(delete obj.prop);
96
97print(obj["js"]);
98obj["js"] = "javascript";
99print(obj["javascript"]);
100