author | mikael |
Thu, 19 Apr 2018 17:13:19 -0700 | |
changeset 49838 | b37e0785ce0b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
1 |
/* |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
4 |
* |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
8 |
* |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
13 |
* accompanied this code). |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
14 |
* |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
18 |
* |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
21 |
* questions. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
22 |
*/ |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
23 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
24 |
/** |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
25 |
* JDK-8157160: JSON.stringify does not work on ScriptObjectMirror objects |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
26 |
* |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
27 |
* @test |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
28 |
* @option -scripting |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
29 |
* @run |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
30 |
*/ |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
31 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
32 |
var SM = Java.type("javax.script.ScriptEngineManager"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
33 |
var AJSO = Java.type("jdk.nashorn.api.scripting.AbstractJSObject"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
34 |
var Supplier = Java.type("java.util.function.Supplier"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
35 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
36 |
var engine = new SM().getEngineByName("nashorn"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
37 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
38 |
// JSON stringify ScriptObjectMirror instances |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
39 |
print(JSON.stringify(engine.eval("({ foo : 42 })"))); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
40 |
print(JSON.stringify(engine.eval("([5, 6, 76, 7])"))); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
41 |
print(JSON.stringify(engine.eval(<<EOF |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
42 |
({ |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
43 |
toJSON: function() "hello" |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
44 |
}) |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
45 |
EOF |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
46 |
))); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
47 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
48 |
print(JSON.stringify(engine.eval(<<EOF |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
49 |
obj = { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
50 |
name: 'nashorn', |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
51 |
versions: [ 'es5.1', 'es6' ] |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
52 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
53 |
EOF |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
54 |
))); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
55 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
56 |
var dm = engine.eval("new Date()"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
57 |
print('"' + dm.toJSON() + '"' == JSON.stringify(dm)); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
58 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
59 |
// JSON stringifying an arbitrary JSObject impl. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
60 |
var jsObj = new AJSO() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
61 |
keySet: function() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
62 |
var keys = new java.util.HashSet(); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
63 |
keys.add("x"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
64 |
keys.add("y"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
65 |
return keys; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
66 |
}, |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
67 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
68 |
getMember: function(name) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
69 |
if (name == "x") { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
70 |
return 42; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
71 |
} else if (name == "y") { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
72 |
return "hello"; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
73 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
74 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
75 |
}; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
76 |
print(JSON.stringify(jsObj)); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
77 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
78 |
// try toJSON implementation on JSObject |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
79 |
var jsObj2 = new AJSO() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
80 |
getMember: function(name) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
81 |
if (name == 'toJSON') { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
82 |
return function() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
83 |
return "my json representation"; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
84 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
85 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
86 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
87 |
}; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
88 |
print(JSON.stringify(jsObj2)); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
89 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
90 |
var jsObj3 = new AJSO() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
91 |
getMember: function(name) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
92 |
if (name == 'toJSON') { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
93 |
return new Supplier() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
94 |
"get": function() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
95 |
return "value from toJSON function"; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
96 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
97 |
}; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
98 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
99 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
100 |
}; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
101 |
print(JSON.stringify(jsObj3)); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
102 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
103 |
// replacer function from another script world |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
104 |
print(JSON.stringify({ |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
105 |
foo: "hello" |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
106 |
}, engine.eval(<<EOF |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
107 |
function (key, value) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
108 |
if (key == "foo") { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
109 |
return value.toUpperCase() |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
110 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
111 |
return value; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
112 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
diff
changeset
|
113 |
EOF))); |