author | sundar |
Thu, 25 Jul 2013 20:10:48 +0530 | |
changeset 19101 | 3e6fe94f02a8 |
parent 19099 | 30230d3febb8 |
child 19459 | 79e75274df99 |
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.api.scripting; |
|
27 |
||
16185 | 28 |
import java.security.AccessController; |
29 |
import java.security.PrivilegedAction; |
|
16147 | 30 |
import java.util.AbstractMap; |
31 |
import java.util.ArrayList; |
|
32 |
import java.util.Collection; |
|
33 |
import java.util.Collections; |
|
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
34 |
import java.util.LinkedHashSet; |
16147 | 35 |
import java.util.Iterator; |
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Set; |
|
39 |
import java.util.concurrent.Callable; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
40 |
import javax.script.Bindings; |
16147 | 41 |
import jdk.nashorn.internal.runtime.Context; |
42 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
43 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
44 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
16147 | 45 |
|
46 |
/** |
|
47 |
* Mirror object that wraps a given ScriptObject instance. User can |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
48 |
* access ScriptObject via the javax.script.Bindings interface or |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
49 |
* netscape.javascript.JSObject interface. |
16147 | 50 |
*/ |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
51 |
public final class ScriptObjectMirror extends JSObject implements Bindings { |
16147 | 52 |
private final ScriptObject sobj; |
53 |
private final ScriptObject global; |
|
54 |
||
55 |
@Override |
|
56 |
public boolean equals(final Object other) { |
|
57 |
if (other instanceof ScriptObjectMirror) { |
|
58 |
return sobj.equals(((ScriptObjectMirror)other).sobj); |
|
59 |
} |
|
60 |
||
61 |
return false; |
|
62 |
} |
|
63 |
||
64 |
@Override |
|
65 |
public int hashCode() { |
|
66 |
return sobj.hashCode(); |
|
67 |
} |
|
68 |
||
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
69 |
@Override |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
70 |
public String toString() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
71 |
return inGlobal(new Callable<String>() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
72 |
@Override |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
73 |
public String call() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
74 |
return ScriptRuntime.safeToString(sobj); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
75 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
76 |
}); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
77 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
78 |
|
16147 | 79 |
// JSObject methods |
80 |
@Override |
|
18614 | 81 |
public Object call(final String functionName, final Object... args) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
82 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16147 | 83 |
final boolean globalChanged = (oldGlobal != global); |
84 |
||
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
85 |
try { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
86 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
87 |
Context.setGlobal(global); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
88 |
} |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
89 |
|
18614 | 90 |
final Object val = functionName == null? sobj : sobj.get(functionName); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
91 |
if (! (val instanceof ScriptFunction)) { |
19099
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
19085
diff
changeset
|
92 |
throw new NoSuchMethodException("No such function " + ((functionName != null)? functionName : "")); |
16147 | 93 |
} |
94 |
||
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
95 |
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
96 |
return wrap(ScriptRuntime.checkAndApply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global); |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
97 |
} catch (final RuntimeException | Error e) { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
98 |
throw e; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
99 |
} catch (final Throwable t) { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
100 |
throw new RuntimeException(t); |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
101 |
} finally { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
102 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
103 |
Context.setGlobal(oldGlobal); |
16147 | 104 |
} |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
105 |
} |
16147 | 106 |
} |
107 |
||
108 |
@Override |
|
18614 | 109 |
public Object newObject(final String functionName, final Object... args) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
110 |
final ScriptObject oldGlobal = Context.getGlobal(); |
18614 | 111 |
final boolean globalChanged = (oldGlobal != global); |
112 |
||
113 |
try { |
|
114 |
if (globalChanged) { |
|
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
115 |
Context.setGlobal(global); |
18614 | 116 |
} |
117 |
||
118 |
final Object val = functionName == null? sobj : sobj.get(functionName); |
|
119 |
if (! (val instanceof ScriptFunction)) { |
|
120 |
throw new RuntimeException("not a constructor " + ((functionName != null)? functionName : "")); |
|
121 |
} |
|
122 |
||
123 |
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; |
|
124 |
return wrap(ScriptRuntime.checkAndConstruct((ScriptFunction)val, unwrapArray(modArgs, global)), global); |
|
125 |
} catch (final RuntimeException | Error e) { |
|
126 |
throw e; |
|
127 |
} catch (final Throwable t) { |
|
128 |
throw new RuntimeException(t); |
|
129 |
} finally { |
|
130 |
if (globalChanged) { |
|
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
131 |
Context.setGlobal(oldGlobal); |
18614 | 132 |
} |
133 |
} |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
16147 | 137 |
public Object eval(final String s) { |
138 |
return inGlobal(new Callable<Object>() { |
|
139 |
@Override |
|
140 |
public Object call() { |
|
16185 | 141 |
final Context context = AccessController.doPrivileged( |
142 |
new PrivilegedAction<Context>() { |
|
143 |
@Override |
|
144 |
public Context run() { |
|
145 |
return Context.getContext(); |
|
146 |
} |
|
147 |
}); |
|
148 |
return wrap(context.eval(global, s, null, null, false), global); |
|
16147 | 149 |
} |
150 |
}); |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
154 |
public Object getMember(final String name) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
155 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
156 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
157 |
return wrap(sobj.get(name), global); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
158 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
159 |
}); |
16147 | 160 |
} |
161 |
||
162 |
@Override |
|
163 |
public Object getSlot(final int index) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
164 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
165 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
166 |
return wrap(sobj.get(index), global); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
167 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
168 |
}); |
16147 | 169 |
} |
170 |
||
171 |
@Override |
|
172 |
public void removeMember(final String name) { |
|
173 |
remove(name); |
|
174 |
} |
|
175 |
||
176 |
@Override |
|
177 |
public void setMember(final String name, final Object value) { |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
178 |
put(name, value); |
16147 | 179 |
} |
180 |
||
181 |
@Override |
|
182 |
public void setSlot(final int index, final Object value) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
183 |
inGlobal(new Callable<Void>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
184 |
@Override public Void call() { |
16185 | 185 |
sobj.set(index, unwrap(value, global), global.isStrictContext()); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
186 |
return null; |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
187 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
188 |
}); |
16147 | 189 |
} |
190 |
||
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
191 |
// javax.script.Bindings methods |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
192 |
|
16147 | 193 |
@Override |
194 |
public void clear() { |
|
195 |
inGlobal(new Callable<Object>() { |
|
196 |
@Override public Object call() { |
|
197 |
sobj.clear(); |
|
198 |
return null; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
199 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
200 |
}); |
16147 | 201 |
} |
202 |
||
203 |
@Override |
|
204 |
public boolean containsKey(final Object key) { |
|
205 |
return inGlobal(new Callable<Boolean>() { |
|
206 |
@Override public Boolean call() { |
|
207 |
return sobj.containsKey(unwrap(key, global)); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
208 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
209 |
}); |
16147 | 210 |
} |
211 |
||
212 |
@Override |
|
213 |
public boolean containsValue(final Object value) { |
|
214 |
return inGlobal(new Callable<Boolean>() { |
|
215 |
@Override public Boolean call() { |
|
216 |
return sobj.containsValue(unwrap(value, global)); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
217 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
218 |
}); |
16147 | 219 |
} |
220 |
||
221 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
222 |
public Set<Map.Entry<String, Object>> entrySet() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
223 |
return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
224 |
@Override public Set<Map.Entry<String, Object>> call() { |
16147 | 225 |
final Iterator<String> iter = sobj.propertyIterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
226 |
final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>(); |
16147 | 227 |
|
228 |
while (iter.hasNext()) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
229 |
final String key = iter.next(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
230 |
final Object value = translateUndefined(wrap(sobj.get(key), global)); |
16147 | 231 |
entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value)); |
232 |
} |
|
233 |
||
234 |
return Collections.unmodifiableSet(entries); |
|
235 |
} |
|
236 |
}); |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
240 |
public Object get(final Object key) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
241 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
242 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
243 |
return translateUndefined(wrap(sobj.get(key), global)); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
244 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
245 |
}); |
16147 | 246 |
} |
247 |
||
248 |
@Override |
|
249 |
public boolean isEmpty() { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
250 |
return inGlobal(new Callable<Boolean>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
251 |
@Override public Boolean call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
252 |
return sobj.isEmpty(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
253 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
254 |
}); |
16147 | 255 |
} |
256 |
||
257 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
258 |
public Set<String> keySet() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
259 |
return inGlobal(new Callable<Set<String>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
260 |
@Override public Set<String> call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
261 |
final Iterator<String> iter = sobj.propertyIterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
262 |
final Set<String> keySet = new LinkedHashSet<>(); |
16147 | 263 |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
264 |
while (iter.hasNext()) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
265 |
keySet.add(iter.next()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
266 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
267 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
268 |
return Collections.unmodifiableSet(keySet); |
16147 | 269 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
270 |
}); |
16147 | 271 |
} |
272 |
||
273 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
274 |
public Object put(final String key, final Object value) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
275 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
276 |
final boolean globalChanged = (oldGlobal != global); |
16147 | 277 |
return inGlobal(new Callable<Object>() { |
278 |
@Override public Object call() { |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
279 |
final Object modValue = globalChanged? wrap(value, oldGlobal) : value; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
280 |
return translateUndefined(wrap(sobj.put(key, unwrap(modValue, global)), global)); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
281 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
282 |
}); |
16147 | 283 |
} |
284 |
||
285 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
286 |
public void putAll(final Map<? extends String, ? extends Object> map) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
287 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
288 |
final boolean globalChanged = (oldGlobal != global); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
289 |
inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
290 |
@Override public Object call() { |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18614
diff
changeset
|
291 |
final boolean strict = global.isStrictContext(); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
292 |
for (final Map.Entry<? extends String, ? extends Object> entry : map.entrySet()) { |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
293 |
final Object value = entry.getValue(); |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
294 |
final Object modValue = globalChanged? wrap(value, oldGlobal) : value; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
295 |
sobj.set(entry.getKey(), unwrap(modValue, global), strict); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
296 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
297 |
return null; |
16147 | 298 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
299 |
}); |
16147 | 300 |
} |
301 |
||
302 |
@Override |
|
303 |
public Object remove(final Object key) { |
|
304 |
return inGlobal(new Callable<Object>() { |
|
305 |
@Override public Object call() { |
|
306 |
return wrap(sobj.remove(unwrap(key, global)), global); |
|
307 |
} |
|
308 |
}); |
|
309 |
} |
|
310 |
||
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
311 |
/** |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
312 |
* Delete a property from this object. |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
313 |
* |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
314 |
* @param key the property to be deleted |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
315 |
* |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
316 |
* @return if the delete was successful or not |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
317 |
*/ |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
318 |
public boolean delete(final Object key) { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
319 |
return inGlobal(new Callable<Boolean>() { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
320 |
@Override public Boolean call() { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
321 |
return sobj.delete(unwrap(key, global)); |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
322 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
323 |
}); |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
324 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
325 |
|
16147 | 326 |
@Override |
327 |
public int size() { |
|
328 |
return inGlobal(new Callable<Integer>() { |
|
329 |
@Override public Integer call() { |
|
330 |
return sobj.size(); |
|
331 |
} |
|
332 |
}); |
|
333 |
} |
|
334 |
||
335 |
@Override |
|
336 |
public Collection<Object> values() { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
337 |
return inGlobal(new Callable<Collection<Object>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
338 |
@Override public Collection<Object> call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
339 |
final List<Object> values = new ArrayList<>(size()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
340 |
final Iterator<Object> iter = sobj.valueIterator(); |
16147 | 341 |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
342 |
while (iter.hasNext()) { |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
343 |
values.add(translateUndefined(wrap(iter.next(), global))); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
344 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
345 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
346 |
return Collections.unmodifiableList(values); |
16147 | 347 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
348 |
}); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
349 |
} |
16147 | 350 |
|
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
351 |
// Support for ECMAScript Object API on mirrors |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
352 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
353 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
354 |
* Return the __proto__ of this object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
355 |
* @return __proto__ object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
356 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
357 |
public Object getProto() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
358 |
return inGlobal(new Callable<Object>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
359 |
@Override public Object call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
360 |
return wrap(sobj.getProto(), global); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
361 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
362 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
363 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
364 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
365 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
366 |
* ECMA 8.12.1 [[GetOwnProperty]] (P) |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
367 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
368 |
* @param key property key |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
369 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
370 |
* @return Returns the Property Descriptor of the named own property of this |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
371 |
* object, or undefined if absent. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
372 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
373 |
public Object getOwnPropertyDescriptor(final String key) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
374 |
return inGlobal(new Callable<Object>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
375 |
@Override public Object call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
376 |
return wrap(sobj.getOwnPropertyDescriptor(key), global); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
377 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
378 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
379 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
380 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
381 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
382 |
* return an array of own property keys associated with the object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
383 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
384 |
* @param all True if to include non-enumerable keys. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
385 |
* @return Array of keys. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
386 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
387 |
public String[] getOwnKeys(final boolean all) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
388 |
return inGlobal(new Callable<String[]>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
389 |
@Override public String[] call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
390 |
return sobj.getOwnKeys(all); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
391 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
392 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
393 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
394 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
395 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
396 |
* Flag this script object as non extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
397 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
398 |
* @return the object after being made non extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
399 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
400 |
public ScriptObjectMirror preventExtensions() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
401 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
402 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
403 |
sobj.preventExtensions(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
404 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
405 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
406 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
407 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
408 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
409 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
410 |
* Check if this script object is extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
411 |
* @return true if extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
412 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
413 |
public boolean isExtensible() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
414 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
415 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
416 |
return sobj.isExtensible(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
417 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
418 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
419 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
420 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
421 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
422 |
* ECMAScript 15.2.3.8 - seal implementation |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
423 |
* @return the sealed script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
424 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
425 |
public ScriptObjectMirror seal() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
426 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
427 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
428 |
sobj.seal(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
429 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
430 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
431 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
432 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
433 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
434 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
435 |
* Check whether this script object is sealed |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
436 |
* @return true if sealed |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
437 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
438 |
public boolean isSealed() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
439 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
440 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
441 |
return sobj.isSealed(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
442 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
443 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
444 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
445 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
446 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
447 |
* ECMA 15.2.39 - freeze implementation. Freeze this script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
448 |
* @return the frozen script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
449 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
450 |
public ScriptObjectMirror freeze() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
451 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
452 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
453 |
sobj.freeze(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
454 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
455 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
456 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
457 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
458 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
459 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
460 |
* Check whether this script object is frozen |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
461 |
* @return true if frozen |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
462 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
463 |
public boolean isFrozen() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
464 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
465 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
466 |
return sobj.isFrozen(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
467 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
468 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
469 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
470 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
471 |
// ECMAScript instanceof check |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
472 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
473 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
474 |
* Checking whether a script object is an instance of another by |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
475 |
* walking the proto chain |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
476 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
477 |
* @param instance instace to check |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
478 |
* @return true if 'instance' is an instance of this object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
479 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
480 |
public boolean isInstance(final ScriptObjectMirror instance) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
481 |
// if not belongs to my global scope, return false |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
482 |
if (instance == null || global != instance.global) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
483 |
return false; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
484 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
485 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
486 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
487 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
488 |
return sobj.isInstance(instance.sobj); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
489 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
490 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
491 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
492 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
493 |
/** |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
494 |
* is this a function object? |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
495 |
* |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
496 |
* @return if this mirror wraps a ECMAScript function instance |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
497 |
*/ |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
498 |
public boolean isFunction() { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
499 |
return sobj instanceof ScriptFunction; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
500 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
501 |
|
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
502 |
/** |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
503 |
* is this a 'use strict' function object? |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
504 |
* |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
505 |
* @return true if this mirror represents a ECMAScript 'use strict' function |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
506 |
*/ |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
507 |
public boolean isStrictFunction() { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
508 |
return isFunction() && ((ScriptFunction)sobj).isStrict(); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
509 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
510 |
|
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
511 |
/** |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
512 |
* is this an array object? |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
513 |
* |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
514 |
* @return if this mirror wraps a ECMAScript array object |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
515 |
*/ |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
516 |
public boolean isArray() { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
517 |
return sobj.isArray(); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
518 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
519 |
|
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
520 |
/** |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
521 |
* Utility to check if given object is ECMAScript undefined value |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
522 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
523 |
* @param obj object to check |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
524 |
* @return true if 'obj' is ECMAScript undefined value |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
525 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
526 |
public static boolean isUndefined(final Object obj) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
527 |
return obj == ScriptRuntime.UNDEFINED; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
528 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
529 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
530 |
/** |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
531 |
* Make a script object mirror on given object if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
532 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
533 |
* @param obj object to be wrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
534 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
535 |
* @return wrapped object |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
536 |
*/ |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
537 |
public static Object wrap(final Object obj, final ScriptObject homeGlobal) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
538 |
return (obj instanceof ScriptObject && homeGlobal != null) ? new ScriptObjectMirror((ScriptObject)obj, homeGlobal) : obj; |
16147 | 539 |
} |
540 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
541 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
542 |
* Unwrap a script object mirror if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
543 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
544 |
* @param obj object to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
545 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
546 |
* @return unwrapped object |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
547 |
*/ |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
548 |
public static Object unwrap(final Object obj, final ScriptObject homeGlobal) { |
16147 | 549 |
if (obj instanceof ScriptObjectMirror) { |
550 |
final ScriptObjectMirror mirror = (ScriptObjectMirror)obj; |
|
551 |
return (mirror.global == homeGlobal)? mirror.sobj : obj; |
|
552 |
} |
|
553 |
||
554 |
return obj; |
|
555 |
} |
|
556 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
557 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
558 |
* Wrap an array of object to script object mirrors if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
559 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
560 |
* @param args array to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
561 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
562 |
* @return wrapped array |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
563 |
*/ |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
564 |
public static Object[] wrapArray(final Object[] args, final ScriptObject homeGlobal) { |
16147 | 565 |
if (args == null || args.length == 0) { |
566 |
return args; |
|
567 |
} |
|
568 |
||
569 |
final Object[] newArgs = new Object[args.length]; |
|
570 |
int index = 0; |
|
571 |
for (final Object obj : args) { |
|
572 |
newArgs[index] = wrap(obj, homeGlobal); |
|
573 |
index++; |
|
574 |
} |
|
575 |
return newArgs; |
|
576 |
} |
|
577 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
578 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
579 |
* Unwrap an array of script object mirrors if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
580 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
581 |
* @param args array to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
582 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
583 |
* @return unwrapped array |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
584 |
*/ |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
585 |
public static Object[] unwrapArray(final Object[] args, final ScriptObject homeGlobal) { |
16147 | 586 |
if (args == null || args.length == 0) { |
587 |
return args; |
|
588 |
} |
|
589 |
||
590 |
final Object[] newArgs = new Object[args.length]; |
|
591 |
int index = 0; |
|
592 |
for (final Object obj : args) { |
|
593 |
newArgs[index] = unwrap(obj, homeGlobal); |
|
594 |
index++; |
|
595 |
} |
|
596 |
return newArgs; |
|
597 |
} |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
598 |
|
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
599 |
// package-privates below this. |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
600 |
|
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
601 |
ScriptObjectMirror(final ScriptObject sobj, final ScriptObject global) { |
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
602 |
assert sobj != null : "ScriptObjectMirror on null!"; |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
603 |
assert global != null : "null global for ScriptObjectMirror!"; |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
604 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
605 |
this.sobj = sobj; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
606 |
this.global = global; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
607 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
608 |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
609 |
// accessors for script engine |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
610 |
ScriptObject getScriptObject() { |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
611 |
return sobj; |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
612 |
} |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
613 |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
614 |
ScriptObject getHomeGlobal() { |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
615 |
return global; |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
616 |
} |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
617 |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
618 |
static Object translateUndefined(Object obj) { |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
619 |
return (obj == ScriptRuntime.UNDEFINED)? null : obj; |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
620 |
} |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
621 |
|
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
622 |
// internals only below this. |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
623 |
private <V> V inGlobal(final Callable<V> callable) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
624 |
final ScriptObject oldGlobal = Context.getGlobal(); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
625 |
final boolean globalChanged = (oldGlobal != global); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
626 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
627 |
Context.setGlobal(global); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
628 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
629 |
try { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
630 |
return callable.call(); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
631 |
} catch (final RuntimeException e) { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
632 |
throw e; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
633 |
} catch (final Exception e) { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
634 |
throw new AssertionError("Cannot happen", e); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
635 |
} finally { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
636 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
637 |
Context.setGlobal(oldGlobal); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
638 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
639 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
640 |
} |
16147 | 641 |
} |