jsadapter.js revision 877:cf4d2252d444
1322038Simp/*
2322038Simp * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3322038Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4322038Simp *
5322038Simp * This code is free software; you can redistribute it and/or modify it
6322038Simp * under the terms of the GNU General Public License version 2 only, as
7322038Simp * published by the Free Software Foundation.
8322038Simp *
9322038Simp * This code is distributed in the hope that it will be useful, but WITHOUT
10322038Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11322038Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12322038Simp * version 2 for more details (a copy is included in the LICENSE file that
13322038Simp * accompanied this code).
14322038Simp *
15322038Simp * You should have received a copy of the GNU General Public License version
16322038Simp * 2 along with this work; if not, write to the Free Software Foundation,
17322038Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18322038Simp *
19322038Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20322038Simp * or visit www.oracle.com if you need additional information or have any
21322038Simp * questions.
22322038Simp */
23322038Simp
24322038Simp/**
25322038Simp * Verify that JSAdapter works as expected.
26322038Simp *
27322038Simp * @test
28322038Simp * @run
29322038Simp */
30322038Simp
31322038Simpvar obj = new JSAdapter() {
32322038Simp    __get__: function(name) {
33322038Simp        print("getter called for '" + name + "'"); return name;
34322038Simp    },
35322038Simp
36322038Simp    __put__: function(name, value) {
37322038Simp        print("setter called for '" + name + "' with " + value);
38322038Simp    },
39322038Simp
40322038Simp    __call__: function(name, arg1, arg2) {
41322038Simp        print("method '" + name + "' called with " + arg1 + ", " + arg2);
42322038Simp    },
43322038Simp
44322038Simp    __new__: function(arg1, arg2) {
45322038Simp        print("new with " + arg1 + ", " + arg2);
46322038Simp    },
47322038Simp
48322038Simp    __getIds__: function() {
49322038Simp        print("__getIds__ called");
50322038Simp        return [ "foo", "bar" ];
51322038Simp    },
52322038Simp
53322038Simp    __getValues__: function() {
54322038Simp        print("__getValues__ called");
55322038Simp        return [ "fooval", "barval" ];
56322038Simp    },
57322038Simp
58322038Simp    __has__: function(name) {
59322038Simp        print("__has__ called with '" + name + "'");
60322038Simp        return name == "js";
61322038Simp    },
62322038Simp
63322038Simp    __delete__: function(name) {
64322038Simp        print("__delete__ called with '" + name + "'");
65322038Simp        return true;
66322038Simp    }
67322038Simp};
68322038Simp
69322038Simp// calls __get__
70322038Simpprint(obj.foo);
71322038Simp
72322038Simp// calls __put__
73322038Simpobj.foo = 33;
74322038Simp
75322038Simp// calls __call__
76322038Simpobj.func("hello", "world");
77322038Simp
78322038Simp// calls __new__
79322038Simpnew obj("hey!", "it works!");
80322038Simp
81322038Simpfor (i in obj) {
82322038Simp    print(i);
83322038Simp}
84322038Simp
85322038Simpfor each (i in obj) {
86322038Simp    print(i);
87322038Simp}
88322038Simp
89322038Simpvar x = "foo" in obj;
90322038Simpprint(x);
91322038Simp
92var y = "js" in obj;
93print(y);
94
95print(delete obj.prop);
96
97print(obj["js"]);
98obj["js"] = "javascript";
99print(obj["javascript"]);
100