author | sundar |
Wed, 18 May 2016 14:08:38 +0530 | |
changeset 38483 | 84396baa1cb3 |
parent 33343 | 23abd10384a5 |
child 38595 | 6eceed1919cf |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.objects; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
30 |
||
31 |
import java.lang.invoke.MethodHandle; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.IdentityHashMap; |
|
35 |
import java.util.Iterator; |
|
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
38 |
import java.util.Objects; |
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
39 |
import java.util.concurrent.Callable; |
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
40 |
import jdk.nashorn.api.scripting.JSObject; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
41 |
import jdk.nashorn.api.scripting.ScriptObjectMirror; |
16147 | 42 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
43 |
import jdk.nashorn.internal.objects.annotations.Function; |
|
44 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
45 |
import jdk.nashorn.internal.objects.annotations.Where; |
|
46 |
import jdk.nashorn.internal.runtime.ConsString; |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16201
diff
changeset
|
47 |
import jdk.nashorn.internal.runtime.JSONFunctions; |
16147 | 48 |
import jdk.nashorn.internal.runtime.JSType; |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18328
diff
changeset
|
49 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 50 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
51 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
52 |
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; |
|
53 |
import jdk.nashorn.internal.runtime.linker.Bootstrap; |
|
54 |
import jdk.nashorn.internal.runtime.linker.InvokeByName; |
|
55 |
||
56 |
/** |
|
57 |
* ECMAScript 262 Edition 5, Section 15.12 The NativeJSON Object |
|
58 |
* |
|
59 |
*/ |
|
60 |
@ScriptClass("JSON") |
|
61 |
public final class NativeJSON extends ScriptObject { |
|
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
62 |
private static final Object TO_JSON = new Object(); |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
63 |
|
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
64 |
private static InvokeByName getTO_JSON() { |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
65 |
return Global.instance().getInvokeByName(TO_JSON, |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
66 |
new Callable<InvokeByName>() { |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
67 |
@Override |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
68 |
public InvokeByName call() { |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
69 |
return new InvokeByName("toJSON", ScriptObject.class, Object.class, Object.class); |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
70 |
} |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
71 |
}); |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
72 |
} |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
73 |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
74 |
private static final Object JSOBJECT_INVOKER = new Object(); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
75 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
76 |
private static MethodHandle getJSOBJECT_INVOKER() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
77 |
return Global.instance().getDynamicInvoker(JSOBJECT_INVOKER, |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
78 |
new Callable<MethodHandle>() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
79 |
@Override |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
80 |
public MethodHandle call() { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
81 |
return Bootstrap.createDynamicCallInvoker(Object.class, Object.class, Object.class); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
82 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
83 |
}); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
84 |
} |
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
85 |
|
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
86 |
private static final Object REPLACER_INVOKER = new Object(); |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
87 |
|
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
88 |
private static MethodHandle getREPLACER_INVOKER() { |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
89 |
return Global.instance().getDynamicInvoker(REPLACER_INVOKER, |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
90 |
new Callable<MethodHandle>() { |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
91 |
@Override |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
92 |
public MethodHandle call() { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
29282
diff
changeset
|
93 |
return Bootstrap.createDynamicCallInvoker(Object.class, |
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
94 |
Object.class, Object.class, Object.class, Object.class); |
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
95 |
} |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
96 |
}); |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
97 |
} |
16147 | 98 |
|
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18328
diff
changeset
|
99 |
// initialized by nasgen |
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18851
diff
changeset
|
100 |
@SuppressWarnings("unused") |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18328
diff
changeset
|
101 |
private static PropertyMap $nasgenmap$; |
16147 | 102 |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
103 |
private NativeJSON() { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
104 |
// don't create me!! |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
105 |
throw new UnsupportedOperationException(); |
16147 | 106 |
} |
107 |
||
108 |
/** |
|
109 |
* ECMA 15.12.2 parse ( text [ , reviver ] ) |
|
110 |
* |
|
111 |
* @param self self reference |
|
112 |
* @param text a JSON formatted string |
|
113 |
* @param reviver optional value: function that takes two parameters (key, value) |
|
114 |
* |
|
115 |
* @return an ECMA script value |
|
116 |
*/ |
|
117 |
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
|
118 |
public static Object parse(final Object self, final Object text, final Object reviver) { |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16201
diff
changeset
|
119 |
return JSONFunctions.parse(text, reviver); |
16147 | 120 |
} |
121 |
||
122 |
/** |
|
123 |
* ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] ) |
|
124 |
* |
|
125 |
* @param self self reference |
|
126 |
* @param value ECMA script value (usually object or array) |
|
127 |
* @param replacer either a function or an array of strings and numbers |
|
128 |
* @param space optional parameter - allows result to have whitespace injection |
|
129 |
* |
|
130 |
* @return a string in JSON format |
|
131 |
*/ |
|
132 |
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
|
133 |
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) { |
|
134 |
// The stringify method takes a value and an optional replacer, and an optional |
|
135 |
// space parameter, and returns a JSON text. The replacer can be a function |
|
136 |
// that can replace values, or an array of strings that will select the keys. |
|
137 |
||
138 |
// A default replacer method can be provided. Use of the space parameter can |
|
139 |
// produce text that is more easily readable. |
|
140 |
||
141 |
final StringifyState state = new StringifyState(); |
|
142 |
||
143 |
// If there is a replacer, it must be a function or an array. |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
144 |
if (Bootstrap.isCallable(replacer)) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
145 |
state.replacerFunction = replacer; |
16147 | 146 |
} else if (isArray(replacer) || |
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
147 |
isJSObjectArray(replacer) || |
16147 | 148 |
replacer instanceof Iterable || |
149 |
(replacer != null && replacer.getClass().isArray())) { |
|
150 |
||
151 |
state.propertyList = new ArrayList<>(); |
|
152 |
||
153 |
final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer); |
|
154 |
||
155 |
while (iter.hasNext()) { |
|
156 |
String item = null; |
|
157 |
final Object v = iter.next(); |
|
158 |
||
159 |
if (v instanceof String) { |
|
160 |
item = (String) v; |
|
161 |
} else if (v instanceof ConsString) { |
|
162 |
item = v.toString(); |
|
163 |
} else if (v instanceof Number || |
|
164 |
v instanceof NativeNumber || |
|
165 |
v instanceof NativeString) { |
|
166 |
item = JSType.toString(v); |
|
167 |
} |
|
168 |
||
169 |
if (item != null) { |
|
170 |
state.propertyList.add(item); |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
// If the space parameter is a number, make an indent |
|
176 |
// string containing that many spaces. |
|
177 |
||
178 |
String gap; |
|
179 |
||
20219
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
180 |
// modifiable 'space' - parameter is final |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
181 |
Object modSpace = space; |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
182 |
if (modSpace instanceof NativeNumber) { |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
183 |
modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class)); |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
184 |
} else if (modSpace instanceof NativeString) { |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
185 |
modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class)); |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
186 |
} |
16147 | 187 |
|
20219
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
188 |
if (modSpace instanceof Number) { |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
20219
diff
changeset
|
189 |
final int indent = Math.min(10, JSType.toInteger(modSpace)); |
20219
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
190 |
if (indent < 1) { |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
191 |
gap = ""; |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
192 |
} else { |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
193 |
final StringBuilder sb = new StringBuilder(); |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
194 |
for (int i = 0; i < indent; i++) { |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
195 |
sb.append(' '); |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
196 |
} |
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
197 |
gap = sb.toString(); |
16147 | 198 |
} |
29282 | 199 |
} else if (JSType.isString(modSpace)) { |
20219
809c0f893cb0
8025149: JSON.stringify does not handle 'space' argument as per the spec.
sundar
parents:
19456
diff
changeset
|
200 |
final String str = modSpace.toString(); |
16147 | 201 |
gap = str.substring(0, Math.min(10, str.length())); |
202 |
} else { |
|
203 |
gap = ""; |
|
204 |
} |
|
205 |
||
206 |
state.gap = gap; |
|
207 |
||
208 |
final ScriptObject wrapper = Global.newEmptyInstance(); |
|
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
209 |
wrapper.set("", value, 0); |
16147 | 210 |
|
211 |
return str("", wrapper, state); |
|
212 |
} |
|
213 |
||
214 |
// -- Internals only below this point |
|
215 |
||
216 |
// stringify helpers. |
|
217 |
||
218 |
private static class StringifyState { |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
219 |
final Map<Object, Object> stack = new IdentityHashMap<>(); |
16147 | 220 |
|
221 |
StringBuilder indent = new StringBuilder(); |
|
222 |
String gap = ""; |
|
223 |
List<String> propertyList = null; |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
224 |
Object replacerFunction = null; |
16147 | 225 |
} |
226 |
||
227 |
// Spec: The abstract operation Str(key, holder). |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
228 |
private static Object str(final Object key, final Object holder, final StringifyState state) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
229 |
assert holder instanceof ScriptObject || holder instanceof JSObject; |
16147 | 230 |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
231 |
Object value = getProperty(holder, key); |
16147 | 232 |
try { |
233 |
if (value instanceof ScriptObject) { |
|
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
234 |
final InvokeByName toJSONInvoker = getTO_JSON(); |
16147 | 235 |
final ScriptObject svalue = (ScriptObject)value; |
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
236 |
final Object toJSON = toJSONInvoker.getGetter().invokeExact(svalue); |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18855
diff
changeset
|
237 |
if (Bootstrap.isCallable(toJSON)) { |
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
238 |
value = toJSONInvoker.getInvoker().invokeExact(toJSON, svalue, key); |
16147 | 239 |
} |
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
240 |
} else if (value instanceof JSObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
241 |
final JSObject jsObj = (JSObject)value; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
242 |
final Object toJSON = jsObj.getMember("toJSON"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
243 |
if (Bootstrap.isCallable(toJSON)) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
244 |
value = getJSOBJECT_INVOKER().invokeExact(toJSON, value); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
245 |
} |
16147 | 246 |
} |
247 |
||
248 |
if (state.replacerFunction != null) { |
|
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
19088
diff
changeset
|
249 |
value = getREPLACER_INVOKER().invokeExact(state.replacerFunction, holder, key, value); |
16147 | 250 |
} |
251 |
} catch(Error|RuntimeException t) { |
|
252 |
throw t; |
|
253 |
} catch(final Throwable t) { |
|
254 |
throw new RuntimeException(t); |
|
255 |
} |
|
256 |
final boolean isObj = (value instanceof ScriptObject); |
|
257 |
if (isObj) { |
|
258 |
if (value instanceof NativeNumber) { |
|
259 |
value = JSType.toNumber(value); |
|
260 |
} else if (value instanceof NativeString) { |
|
261 |
value = JSType.toString(value); |
|
262 |
} else if (value instanceof NativeBoolean) { |
|
263 |
value = ((NativeBoolean)value).booleanValue(); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
if (value == null) { |
|
268 |
return "null"; |
|
269 |
} else if (Boolean.TRUE.equals(value)) { |
|
270 |
return "true"; |
|
271 |
} else if (Boolean.FALSE.equals(value)) { |
|
272 |
return "false"; |
|
273 |
} |
|
274 |
||
275 |
if (value instanceof String) { |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16201
diff
changeset
|
276 |
return JSONFunctions.quote((String)value); |
16147 | 277 |
} else if (value instanceof ConsString) { |
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16201
diff
changeset
|
278 |
return JSONFunctions.quote(value.toString()); |
16147 | 279 |
} |
280 |
||
281 |
if (value instanceof Number) { |
|
282 |
return JSType.isFinite(((Number)value).doubleValue()) ? JSType.toString(value) : "null"; |
|
283 |
} |
|
284 |
||
285 |
final JSType type = JSType.of(value); |
|
286 |
if (type == JSType.OBJECT) { |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
287 |
if (isArray(value) || isJSObjectArray(value)) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
288 |
return JA(value, state); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
289 |
} else if (value instanceof ScriptObject || value instanceof JSObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
290 |
return JO(value, state); |
16147 | 291 |
} |
292 |
} |
|
293 |
||
294 |
return UNDEFINED; |
|
295 |
} |
|
296 |
||
297 |
// Spec: The abstract operation JO(value) serializes an object. |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
298 |
private static String JO(final Object value, final StringifyState state) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
299 |
assert value instanceof ScriptObject || value instanceof JSObject; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
300 |
|
16147 | 301 |
if (state.stack.containsKey(value)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16226
diff
changeset
|
302 |
throw typeError("JSON.stringify.cyclic"); |
16147 | 303 |
} |
304 |
||
305 |
state.stack.put(value, value); |
|
306 |
final StringBuilder stepback = new StringBuilder(state.indent.toString()); |
|
307 |
state.indent.append(state.gap); |
|
308 |
||
309 |
final StringBuilder finalStr = new StringBuilder(); |
|
310 |
final List<Object> partial = new ArrayList<>(); |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
311 |
final List<String> k = state.propertyList == null ? |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
312 |
Arrays.asList(getOwnKeys(value)) : state.propertyList; |
16147 | 313 |
|
314 |
for (final Object p : k) { |
|
315 |
final Object strP = str(p, value, state); |
|
316 |
||
317 |
if (strP != UNDEFINED) { |
|
318 |
final StringBuilder member = new StringBuilder(); |
|
319 |
||
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16201
diff
changeset
|
320 |
member.append(JSONFunctions.quote(p.toString())).append(':'); |
16147 | 321 |
if (!state.gap.isEmpty()) { |
322 |
member.append(' '); |
|
323 |
} |
|
324 |
||
325 |
member.append(strP); |
|
326 |
partial.add(member); |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
if (partial.isEmpty()) { |
|
331 |
finalStr.append("{}"); |
|
332 |
} else { |
|
333 |
if (state.gap.isEmpty()) { |
|
334 |
final int size = partial.size(); |
|
335 |
int index = 0; |
|
336 |
||
337 |
finalStr.append('{'); |
|
338 |
||
339 |
for (final Object str : partial) { |
|
340 |
finalStr.append(str); |
|
341 |
if (index < size - 1) { |
|
342 |
finalStr.append(','); |
|
343 |
} |
|
344 |
index++; |
|
345 |
} |
|
346 |
||
347 |
finalStr.append('}'); |
|
348 |
} else { |
|
349 |
final int size = partial.size(); |
|
350 |
int index = 0; |
|
351 |
||
352 |
finalStr.append("{\n"); |
|
353 |
finalStr.append(state.indent); |
|
354 |
||
355 |
for (final Object str : partial) { |
|
356 |
finalStr.append(str); |
|
357 |
if (index < size - 1) { |
|
358 |
finalStr.append(",\n"); |
|
359 |
finalStr.append(state.indent); |
|
360 |
} |
|
361 |
index++; |
|
362 |
} |
|
363 |
||
364 |
finalStr.append('\n'); |
|
365 |
finalStr.append(stepback); |
|
366 |
finalStr.append('}'); |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
state.stack.remove(value); |
|
371 |
state.indent = stepback; |
|
372 |
||
373 |
return finalStr.toString(); |
|
374 |
} |
|
375 |
||
376 |
// Spec: The abstract operation JA(value) serializes an array. |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
377 |
private static Object JA(final Object value, final StringifyState state) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
378 |
assert value instanceof ScriptObject || value instanceof JSObject; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
379 |
|
16147 | 380 |
if (state.stack.containsKey(value)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16226
diff
changeset
|
381 |
throw typeError("JSON.stringify.cyclic"); |
16147 | 382 |
} |
383 |
||
384 |
state.stack.put(value, value); |
|
385 |
final StringBuilder stepback = new StringBuilder(state.indent.toString()); |
|
386 |
state.indent.append(state.gap); |
|
387 |
final List<Object> partial = new ArrayList<>(); |
|
388 |
||
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
389 |
final int length = JSType.toInteger(getLength(value)); |
16147 | 390 |
int index = 0; |
391 |
||
392 |
while (index < length) { |
|
393 |
Object strP = str(index, value, state); |
|
394 |
if (strP == UNDEFINED) { |
|
395 |
strP = "null"; |
|
396 |
} |
|
397 |
partial.add(strP); |
|
398 |
index++; |
|
399 |
} |
|
400 |
||
401 |
final StringBuilder finalStr = new StringBuilder(); |
|
402 |
if (partial.isEmpty()) { |
|
403 |
finalStr.append("[]"); |
|
404 |
} else { |
|
405 |
if (state.gap.isEmpty()) { |
|
406 |
final int size = partial.size(); |
|
407 |
index = 0; |
|
408 |
finalStr.append('['); |
|
409 |
for (final Object str : partial) { |
|
410 |
finalStr.append(str); |
|
411 |
if (index < size - 1) { |
|
412 |
finalStr.append(','); |
|
413 |
} |
|
414 |
index++; |
|
415 |
} |
|
416 |
||
417 |
finalStr.append(']'); |
|
418 |
} else { |
|
419 |
final int size = partial.size(); |
|
420 |
index = 0; |
|
421 |
finalStr.append("[\n"); |
|
422 |
finalStr.append(state.indent); |
|
423 |
for (final Object str : partial) { |
|
424 |
finalStr.append(str); |
|
425 |
if (index < size - 1) { |
|
426 |
finalStr.append(",\n"); |
|
427 |
finalStr.append(state.indent); |
|
428 |
} |
|
429 |
index++; |
|
430 |
} |
|
431 |
||
432 |
finalStr.append('\n'); |
|
433 |
finalStr.append(stepback); |
|
434 |
finalStr.append(']'); |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
state.stack.remove(value); |
|
439 |
state.indent = stepback; |
|
440 |
||
441 |
return finalStr.toString(); |
|
442 |
} |
|
38483
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
443 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
444 |
private static String[] getOwnKeys(final Object obj) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
445 |
if (obj instanceof ScriptObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
446 |
return ((ScriptObject)obj).getOwnKeys(false); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
447 |
} else if (obj instanceof ScriptObjectMirror) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
448 |
return ((ScriptObjectMirror)obj).getOwnKeys(false); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
449 |
} else if (obj instanceof JSObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
450 |
// No notion of "own keys" or "proto" for general JSObject! We just |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
451 |
// return all keys of the object. This will be useful for POJOs |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
452 |
// implementing JSObject interface. |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
453 |
return ((JSObject)obj).keySet().toArray(new String[0]); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
454 |
} else { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
455 |
throw new AssertionError("should not reach here"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
456 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
457 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
458 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
459 |
private static Object getLength(final Object obj) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
460 |
if (obj instanceof ScriptObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
461 |
return ((ScriptObject)obj).getLength(); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
462 |
} else if (obj instanceof JSObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
463 |
return ((JSObject)obj).getMember("length"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
464 |
} else { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
465 |
throw new AssertionError("should not reach here"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
466 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
467 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
468 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
469 |
private static boolean isJSObjectArray(final Object obj) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
470 |
return (obj instanceof JSObject) && ((JSObject)obj).isArray(); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
471 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
472 |
|
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
473 |
private static Object getProperty(final Object holder, final Object key) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
474 |
if (holder instanceof ScriptObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
475 |
return ((ScriptObject)holder).get(key); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
476 |
} else if (holder instanceof JSObject) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
477 |
JSObject jsObj = (JSObject)holder; |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
478 |
if (key instanceof Integer) { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
479 |
return jsObj.getSlot((Integer)key); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
480 |
} else { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
481 |
return jsObj.getMember(Objects.toString(key)); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
482 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
483 |
} else { |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
484 |
return new AssertionError("should not reach here"); |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
485 |
} |
84396baa1cb3
8157160: JSON.stringify does not work on ScriptObjectMirror objects
sundar
parents:
33343
diff
changeset
|
486 |
} |
16147 | 487 |
} |