jsadapter.js revision 1456:11b48db399bf
1240116Smarcel/*
2240116Smarcel * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3240116Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4240116Smarcel *
5240116Smarcel * This code is free software; you can redistribute it and/or modify it
6240116Smarcel * under the terms of the GNU General Public License version 2 only, as
7240116Smarcel * published by the Free Software Foundation.
8240116Smarcel *
9240116Smarcel * This code is distributed in the hope that it will be useful, but WITHOUT
10240116Smarcel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11240116Smarcel * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12240116Smarcel * version 2 for more details (a copy is included in the LICENSE file that
13240116Smarcel * accompanied this code).
14240116Smarcel *
15240116Smarcel * You should have received a copy of the GNU General Public License version
16240116Smarcel * 2 along with this work; if not, write to the Free Software Foundation,
17240116Smarcel * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18240116Smarcel *
19240116Smarcel * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20240116Smarcel * or visit www.oracle.com if you need additional information or have any
21240116Smarcel * questions.
22240116Smarcel */
23240116Smarcel
24240116Smarcel/**
25240116Smarcel * Verify that JSAdapter works as expected.
26240116Smarcel *
27240116Smarcel * @test
28240116Smarcel * @run
29240116Smarcel */
30240116Smarcel
31240116Smarcelvar obj = new JSAdapter() {
32240116Smarcel    __get__: function(name) {
33240116Smarcel        print("getter called for '" + name + "'"); return name;
34240116Smarcel    },
35240116Smarcel
36240116Smarcel    __put__: function(name, value) {
37240116Smarcel        print("setter called for '" + name + "' with " + value);
38240116Smarcel    },
39240116Smarcel
40240116Smarcel    __call__: function(name, arg1, arg2) {
41240116Smarcel        print("method '" + name + "' called with " + arg1 + ", " + arg2);
42240116Smarcel    },
43240116Smarcel
44240116Smarcel    __new__: function(arg1, arg2) {
45240116Smarcel        print("new with " + arg1 + ", " + arg2);
46240116Smarcel    },
47240116Smarcel
48240116Smarcel    __getKeys__: function() {
49240116Smarcel        print("__getKeys__ called");
50240116Smarcel        return [ "foo", "bar" ];
51240116Smarcel    },
52240116Smarcel
53240116Smarcel    __getValues__: function() {
54240116Smarcel        print("__getValues__ called");
55240116Smarcel        return [ "fooval", "barval" ];
56240116Smarcel    },
57240116Smarcel
58240116Smarcel    __has__: function(name) {
59240116Smarcel        print("__has__ called with '" + name + "'");
60240116Smarcel        return name == "js";
61240116Smarcel    },
62240116Smarcel
63240116Smarcel    __delete__: function(name) {
64251108Smarcel        print("__delete__ called with '" + name + "'");
65240116Smarcel        return true;
66240116Smarcel    }
67240116Smarcel};
68240116Smarcel
69240116Smarcel// calls __get__
70240116Smarcelprint(obj.foo);
71240116Smarcel
72240116Smarcel// calls __put__
73240116Smarcelobj.foo = 33;
74240116Smarcel
75240116Smarcel// calls __call__
76240116Smarcelobj.func("hello", "world");
77251108Smarcel
78240116Smarcel// calls __new__
79240116Smarcelnew obj("hey!", "it works!");
80240116Smarcel
81240116Smarcel// calls __getKeys__
82240116Smarcelfor (i in obj) {
83240116Smarcel    print(i);
84240116Smarcel}
85240116Smarcel
86240116Smarcel// calls __getValues__
87240116Smarcelfor each (i in obj) {
88240116Smarcel    print(i);
89240116Smarcel}
90240116Smarcel
91240116Smarcel// calls __has__
92240116Smarcelvar x = "foo" in obj;
93240116Smarcelprint(x);
94240116Smarcel
95240116Smarcel// calls __has__
96240116Smarcelvar y = "js" in obj;
97240116Smarcelprint(y);
98240116Smarcel
99240116Smarcel// calls __delete__
100240116Smarcelprint(delete obj.prop);
101240116Smarcel
102240116Smarcel// call __get__ and __set__
103240116Smarcelprint(obj["js"]);
104240116Smarcelobj["js"] = "javascript";
105240116Smarcelprint(obj["javascript"]);
106240116Smarcel