author | sundar |
Thu, 28 Jun 2018 18:04:19 +0530 | |
changeset 50878 | fb7800b66c92 |
parent 47216 | 71c04702a3d5 |
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 |
||
23078
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
28 |
import java.nio.ByteBuffer; |
19459 | 29 |
import java.security.AccessControlContext; |
16185 | 30 |
import java.security.AccessController; |
19459 | 31 |
import java.security.Permissions; |
16185 | 32 |
import java.security.PrivilegedAction; |
19459 | 33 |
import java.security.ProtectionDomain; |
16147 | 34 |
import java.util.AbstractMap; |
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Collection; |
|
37 |
import java.util.Collections; |
|
19459 | 38 |
import java.util.Iterator; |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
39 |
import java.util.LinkedHashSet; |
16147 | 40 |
import java.util.List; |
41 |
import java.util.Map; |
|
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
42 |
import java.util.Objects; |
16147 | 43 |
import java.util.Set; |
44 |
import java.util.concurrent.Callable; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
45 |
import javax.script.Bindings; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
46 |
import jdk.nashorn.internal.objects.Global; |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
47 |
import jdk.nashorn.internal.runtime.ConsString; |
16147 | 48 |
import jdk.nashorn.internal.runtime.Context; |
29133
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
49 |
import jdk.nashorn.internal.runtime.ECMAException; |
30977 | 50 |
import jdk.nashorn.internal.runtime.JSONListAdapter; |
21449 | 51 |
import jdk.nashorn.internal.runtime.JSType; |
16147 | 52 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
53 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
54 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24772
diff
changeset
|
55 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
26235
diff
changeset
|
56 |
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
16147 | 57 |
|
58 |
/** |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
59 |
* Mirror object that wraps a given Nashorn Script object. |
28317
0aeeae75e696
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents:
26765
diff
changeset
|
60 |
* |
50878
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
61 |
* @deprecated Nashorn JavaScript script engine and APIs, and the jjs tool |
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
62 |
* are deprecated with the intent to remove them in a future release. |
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
63 |
* |
28317
0aeeae75e696
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents:
26765
diff
changeset
|
64 |
* @since 1.8u40 |
16147 | 65 |
*/ |
50878
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
66 |
@Deprecated(since="11", forRemoval=true) |
21453
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
21450
diff
changeset
|
67 |
public final class ScriptObjectMirror extends AbstractJSObject implements Bindings { |
19459 | 68 |
private static AccessControlContext getContextAccCtxt() { |
69 |
final Permissions perms = new Permissions(); |
|
70 |
perms.add(new RuntimePermission(Context.NASHORN_GET_CONTEXT)); |
|
71 |
return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) }); |
|
72 |
} |
|
73 |
||
74 |
private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt(); |
|
75 |
||
16147 | 76 |
private final ScriptObject sobj; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
77 |
private final Global global; |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
78 |
private final boolean strict; |
30977 | 79 |
private final boolean jsonCompatible; |
16147 | 80 |
|
81 |
@Override |
|
82 |
public boolean equals(final Object other) { |
|
83 |
if (other instanceof ScriptObjectMirror) { |
|
84 |
return sobj.equals(((ScriptObjectMirror)other).sobj); |
|
85 |
} |
|
86 |
||
87 |
return false; |
|
88 |
} |
|
89 |
||
90 |
@Override |
|
91 |
public int hashCode() { |
|
92 |
return sobj.hashCode(); |
|
93 |
} |
|
94 |
||
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
95 |
@Override |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
96 |
public String toString() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
97 |
return inGlobal(new Callable<String>() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
98 |
@Override |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
99 |
public String call() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
100 |
return ScriptRuntime.safeToString(sobj); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
101 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
102 |
}); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
103 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
104 |
|
16147 | 105 |
// JSObject methods |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
106 |
|
16147 | 107 |
@Override |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
108 |
public Object call(final Object thiz, final Object... args) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
109 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 110 |
final boolean globalChanged = (oldGlobal != global); |
111 |
||
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
112 |
try { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
113 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
114 |
Context.setGlobal(global); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
115 |
} |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
116 |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
117 |
if (sobj instanceof ScriptFunction) { |
30977 | 118 |
final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args; |
119 |
final Object self = globalChanged? wrapLikeMe(thiz, oldGlobal) : thiz; |
|
120 |
return wrapLikeMe(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global))); |
|
16147 | 121 |
} |
122 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
123 |
throw new RuntimeException("not a function: " + toString()); |
22370 | 124 |
} catch (final NashornException ne) { |
125 |
throw ne.initEcmaError(global); |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
126 |
} catch (final RuntimeException | Error e) { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
127 |
throw e; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
128 |
} catch (final Throwable t) { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
129 |
throw new RuntimeException(t); |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
130 |
} finally { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
131 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
132 |
Context.setGlobal(oldGlobal); |
16147 | 133 |
} |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
134 |
} |
16147 | 135 |
} |
136 |
||
137 |
@Override |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
138 |
public Object newObject(final Object... args) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
139 |
final Global oldGlobal = Context.getGlobal(); |
18614 | 140 |
final boolean globalChanged = (oldGlobal != global); |
141 |
||
142 |
try { |
|
143 |
if (globalChanged) { |
|
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
144 |
Context.setGlobal(global); |
18614 | 145 |
} |
146 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
147 |
if (sobj instanceof ScriptFunction) { |
30977 | 148 |
final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args; |
149 |
return wrapLikeMe(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global))); |
|
18614 | 150 |
} |
151 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
152 |
throw new RuntimeException("not a constructor: " + toString()); |
22370 | 153 |
} catch (final NashornException ne) { |
154 |
throw ne.initEcmaError(global); |
|
18614 | 155 |
} catch (final RuntimeException | Error e) { |
156 |
throw e; |
|
157 |
} catch (final Throwable t) { |
|
158 |
throw new RuntimeException(t); |
|
159 |
} finally { |
|
160 |
if (globalChanged) { |
|
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
161 |
Context.setGlobal(oldGlobal); |
18614 | 162 |
} |
163 |
} |
|
164 |
} |
|
165 |
||
166 |
@Override |
|
16147 | 167 |
public Object eval(final String s) { |
168 |
return inGlobal(new Callable<Object>() { |
|
169 |
@Override |
|
170 |
public Object call() { |
|
16185 | 171 |
final Context context = AccessController.doPrivileged( |
172 |
new PrivilegedAction<Context>() { |
|
173 |
@Override |
|
174 |
public Context run() { |
|
175 |
return Context.getContext(); |
|
176 |
} |
|
19459 | 177 |
}, GET_CONTEXT_ACC_CTXT); |
31192
1e019aeea9b5
8087211: Indirect evals should be strict with -strict option
sundar
parents:
31094
diff
changeset
|
178 |
return wrapLikeMe(context.eval(global, s, sobj, null)); |
16147 | 179 |
} |
180 |
}); |
|
181 |
} |
|
182 |
||
24719 | 183 |
/** |
184 |
* Call member function |
|
185 |
* @param functionName function name |
|
186 |
* @param args arguments |
|
187 |
* @return return value of function |
|
188 |
*/ |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
189 |
public Object callMember(final String functionName, final Object... args) { |
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
190 |
Objects.requireNonNull(functionName); |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
191 |
final Global oldGlobal = Context.getGlobal(); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
192 |
final boolean globalChanged = (oldGlobal != global); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
193 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
194 |
try { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
195 |
if (globalChanged) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
196 |
Context.setGlobal(global); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
197 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
198 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
199 |
final Object val = sobj.get(functionName); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
200 |
if (val instanceof ScriptFunction) { |
30977 | 201 |
final Object[] modArgs = globalChanged? wrapArrayLikeMe(args, oldGlobal) : args; |
202 |
return wrapLikeMe(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global))); |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
203 |
} else if (val instanceof JSObject && ((JSObject)val).isFunction()) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
204 |
return ((JSObject)val).call(sobj, args); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
205 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
206 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
207 |
throw new NoSuchMethodException("No such function " + functionName); |
22370 | 208 |
} catch (final NashornException ne) { |
209 |
throw ne.initEcmaError(global); |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
210 |
} catch (final RuntimeException | Error e) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
211 |
throw e; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
212 |
} catch (final Throwable t) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
213 |
throw new RuntimeException(t); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
214 |
} finally { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
215 |
if (globalChanged) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
216 |
Context.setGlobal(oldGlobal); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
217 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
218 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
219 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
220 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
221 |
@Override |
16147 | 222 |
public Object getMember(final String name) { |
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
223 |
Objects.requireNonNull(name); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
224 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
225 |
@Override public Object call() { |
30977 | 226 |
return wrapLikeMe(sobj.get(name)); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
227 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
228 |
}); |
16147 | 229 |
} |
230 |
||
231 |
@Override |
|
232 |
public Object getSlot(final int index) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
233 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
234 |
@Override public Object call() { |
30977 | 235 |
return wrapLikeMe(sobj.get(index)); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
236 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
237 |
}); |
16147 | 238 |
} |
239 |
||
240 |
@Override |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
241 |
public boolean hasMember(final String name) { |
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
242 |
Objects.requireNonNull(name); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
243 |
return inGlobal(new Callable<Boolean>() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
244 |
@Override public Boolean call() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
245 |
return sobj.has(name); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
246 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
247 |
}); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
248 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
249 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
250 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
251 |
public boolean hasSlot(final int slot) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
252 |
return inGlobal(new Callable<Boolean>() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
253 |
@Override public Boolean call() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
254 |
return sobj.has(slot); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
255 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
256 |
}); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
257 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
258 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
259 |
@Override |
16147 | 260 |
public void removeMember(final String name) { |
31094
7a9fa21da791
8085810: Return value of Objects.requireNonNull call can be used
sundar
parents:
30977
diff
changeset
|
261 |
remove(Objects.requireNonNull(name)); |
16147 | 262 |
} |
263 |
||
264 |
@Override |
|
265 |
public void setMember(final String name, final Object value) { |
|
31094
7a9fa21da791
8085810: Return value of Objects.requireNonNull call can be used
sundar
parents:
30977
diff
changeset
|
266 |
put(Objects.requireNonNull(name), value); |
16147 | 267 |
} |
268 |
||
269 |
@Override |
|
270 |
public void setSlot(final int index, final Object value) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
271 |
inGlobal(new Callable<Void>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
272 |
@Override public Void call() { |
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
26235
diff
changeset
|
273 |
sobj.set(index, unwrap(value, global), getCallSiteFlags()); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
274 |
return null; |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
275 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
276 |
}); |
16147 | 277 |
} |
278 |
||
23078
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
279 |
/** |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
280 |
* Nashorn extension: setIndexedPropertiesToExternalArrayData. |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
281 |
* set indexed properties be exposed from a given nio ByteBuffer. |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
282 |
* |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
283 |
* @param buf external buffer - should be a nio ByteBuffer |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
284 |
*/ |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
285 |
public void setIndexedPropertiesToExternalArrayData(final ByteBuffer buf) { |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
286 |
inGlobal(new Callable<Void>() { |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
287 |
@Override public Void call() { |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
288 |
sobj.setArray(ArrayData.allocate(buf)); |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
289 |
return null; |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
290 |
} |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
291 |
}); |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
292 |
} |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
293 |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
294 |
@Override |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
295 |
public boolean isInstance(final Object instance) { |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
296 |
if (! (instance instanceof ScriptObjectMirror)) { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
297 |
return false; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
298 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
299 |
|
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
300 |
final ScriptObjectMirror mirror = (ScriptObjectMirror)instance; |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
301 |
// if not belongs to my global scope, return false |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
302 |
if (global != mirror.global) { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
303 |
return false; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
304 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
305 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
306 |
return inGlobal(new Callable<Boolean>() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
307 |
@Override public Boolean call() { |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
308 |
return sobj.isInstance(mirror.sobj); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
309 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
310 |
}); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
311 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
312 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
313 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
314 |
public String getClassName() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
315 |
return sobj.getClassName(); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
316 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
317 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
318 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
319 |
public boolean isFunction() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
320 |
return sobj instanceof ScriptFunction; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
321 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
322 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
323 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
324 |
public boolean isStrictFunction() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
325 |
return isFunction() && ((ScriptFunction)sobj).isStrict(); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
326 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
327 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
328 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
329 |
public boolean isArray() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
330 |
return sobj.isArray(); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
331 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
332 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
333 |
// javax.script.Bindings methods |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
334 |
|
16147 | 335 |
@Override |
336 |
public void clear() { |
|
337 |
inGlobal(new Callable<Object>() { |
|
338 |
@Override public Object call() { |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
339 |
sobj.clear(strict); |
16147 | 340 |
return null; |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
341 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
342 |
}); |
16147 | 343 |
} |
344 |
||
345 |
@Override |
|
346 |
public boolean containsKey(final Object key) { |
|
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
347 |
checkKey(key); |
16147 | 348 |
return inGlobal(new Callable<Boolean>() { |
349 |
@Override public Boolean call() { |
|
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
350 |
return sobj.containsKey(key); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
351 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
352 |
}); |
16147 | 353 |
} |
354 |
||
355 |
@Override |
|
356 |
public boolean containsValue(final Object value) { |
|
357 |
return inGlobal(new Callable<Boolean>() { |
|
358 |
@Override public Boolean call() { |
|
359 |
return sobj.containsValue(unwrap(value, global)); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
360 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
361 |
}); |
16147 | 362 |
} |
363 |
||
364 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
365 |
public Set<Map.Entry<String, Object>> entrySet() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
366 |
return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
367 |
@Override public Set<Map.Entry<String, Object>> call() { |
16147 | 368 |
final Iterator<String> iter = sobj.propertyIterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
369 |
final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>(); |
16147 | 370 |
|
371 |
while (iter.hasNext()) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
372 |
final String key = iter.next(); |
30977 | 373 |
final Object value = translateUndefined(wrapLikeMe(sobj.get(key))); |
16147 | 374 |
entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value)); |
375 |
} |
|
376 |
||
377 |
return Collections.unmodifiableSet(entries); |
|
378 |
} |
|
379 |
}); |
|
380 |
} |
|
381 |
||
382 |
@Override |
|
383 |
public Object get(final Object key) { |
|
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
384 |
checkKey(key); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
385 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
386 |
@Override public Object call() { |
30977 | 387 |
return translateUndefined(wrapLikeMe(sobj.get(key))); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
388 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
389 |
}); |
16147 | 390 |
} |
391 |
||
392 |
@Override |
|
393 |
public boolean isEmpty() { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
394 |
return inGlobal(new Callable<Boolean>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
395 |
@Override public Boolean call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
396 |
return sobj.isEmpty(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
397 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
398 |
}); |
16147 | 399 |
} |
400 |
||
401 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
402 |
public Set<String> keySet() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
403 |
return inGlobal(new Callable<Set<String>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
404 |
@Override public Set<String> call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
405 |
final Iterator<String> iter = sobj.propertyIterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
406 |
final Set<String> keySet = new LinkedHashSet<>(); |
16147 | 407 |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
408 |
while (iter.hasNext()) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
409 |
keySet.add(iter.next()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
410 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
411 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
412 |
return Collections.unmodifiableSet(keySet); |
16147 | 413 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
414 |
}); |
16147 | 415 |
} |
416 |
||
417 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
418 |
public Object put(final String key, final Object value) { |
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
419 |
checkKey(key); |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
420 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
421 |
final boolean globalChanged = (oldGlobal != global); |
16147 | 422 |
return inGlobal(new Callable<Object>() { |
423 |
@Override public Object call() { |
|
30977 | 424 |
final Object modValue = globalChanged? wrapLikeMe(value, oldGlobal) : value; |
425 |
return translateUndefined(wrapLikeMe(sobj.put(key, unwrap(modValue, global), strict))); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
426 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
427 |
}); |
16147 | 428 |
} |
429 |
||
430 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
431 |
public void putAll(final Map<? extends String, ? extends Object> map) { |
31094
7a9fa21da791
8085810: Return value of Objects.requireNonNull call can be used
sundar
parents:
30977
diff
changeset
|
432 |
Objects.requireNonNull(map); |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
433 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
434 |
final boolean globalChanged = (oldGlobal != global); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
435 |
inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
436 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
437 |
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
|
438 |
final Object value = entry.getValue(); |
30977 | 439 |
final Object modValue = globalChanged? wrapLikeMe(value, oldGlobal) : value; |
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
440 |
final String key = entry.getKey(); |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
441 |
checkKey(key); |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
442 |
sobj.set(key, unwrap(modValue, global), getCallSiteFlags()); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
443 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
444 |
return null; |
16147 | 445 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
446 |
}); |
16147 | 447 |
} |
448 |
||
449 |
@Override |
|
450 |
public Object remove(final Object key) { |
|
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
451 |
checkKey(key); |
16147 | 452 |
return inGlobal(new Callable<Object>() { |
453 |
@Override public Object call() { |
|
30977 | 454 |
return translateUndefined(wrapLikeMe(sobj.remove(key, strict))); |
16147 | 455 |
} |
456 |
}); |
|
457 |
} |
|
458 |
||
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
459 |
/** |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
460 |
* Delete a property from this object. |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
461 |
* |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
462 |
* @param key the property to be deleted |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
463 |
* |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
464 |
* @return if the delete was successful or not |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
465 |
*/ |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
466 |
public boolean delete(final Object key) { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
467 |
return inGlobal(new Callable<Boolean>() { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
468 |
@Override public Boolean call() { |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
469 |
return sobj.delete(unwrap(key, global), strict); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
470 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
471 |
}); |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
472 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
473 |
|
16147 | 474 |
@Override |
475 |
public int size() { |
|
476 |
return inGlobal(new Callable<Integer>() { |
|
477 |
@Override public Integer call() { |
|
478 |
return sobj.size(); |
|
479 |
} |
|
480 |
}); |
|
481 |
} |
|
482 |
||
483 |
@Override |
|
484 |
public Collection<Object> values() { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
485 |
return inGlobal(new Callable<Collection<Object>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
486 |
@Override public Collection<Object> call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
487 |
final List<Object> values = new ArrayList<>(size()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
488 |
final Iterator<Object> iter = sobj.valueIterator(); |
16147 | 489 |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
490 |
while (iter.hasNext()) { |
30977 | 491 |
values.add(translateUndefined(wrapLikeMe(iter.next()))); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
492 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
493 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
494 |
return Collections.unmodifiableList(values); |
16147 | 495 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
496 |
}); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
497 |
} |
16147 | 498 |
|
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
499 |
// Support for ECMAScript Object API on mirrors |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
500 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
501 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
502 |
* Return the __proto__ of this object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
503 |
* @return __proto__ object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
504 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
505 |
public Object getProto() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
506 |
return inGlobal(new Callable<Object>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
507 |
@Override public Object call() { |
30977 | 508 |
return wrapLikeMe(sobj.getProto()); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
509 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
510 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
511 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
512 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
513 |
/** |
19619
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
514 |
* Set the __proto__ of this object. |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
515 |
* @param proto new proto for this object |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
516 |
*/ |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
517 |
public void setProto(final Object proto) { |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
518 |
inGlobal(new Callable<Void>() { |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
519 |
@Override public Void call() { |
24772
0fc1013a1785
8044520: Nashorn cannot execute node.js's express module
sundar
parents:
24770
diff
changeset
|
520 |
sobj.setPrototypeOf(unwrap(proto, global)); |
19619
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
521 |
return null; |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
522 |
} |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
523 |
}); |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
524 |
} |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
525 |
|
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
526 |
/** |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
527 |
* ECMA 8.12.1 [[GetOwnProperty]] (P) |
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 |
* @param key property key |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
530 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
531 |
* @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
|
532 |
* object, or undefined if absent. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
533 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
534 |
public Object getOwnPropertyDescriptor(final String key) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
535 |
return inGlobal(new Callable<Object>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
536 |
@Override public Object call() { |
30977 | 537 |
return wrapLikeMe(sobj.getOwnPropertyDescriptor(key)); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
538 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
539 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
540 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
541 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
542 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
543 |
* 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
|
544 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
545 |
* @param all True if to include non-enumerable keys. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
546 |
* @return Array of keys. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
547 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
548 |
public String[] getOwnKeys(final boolean all) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
549 |
return inGlobal(new Callable<String[]>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
550 |
@Override public String[] call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
551 |
return sobj.getOwnKeys(all); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
552 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
553 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
554 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
555 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
556 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
557 |
* Flag this script object as non extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
558 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
559 |
* @return the object after being made non extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
560 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
561 |
public ScriptObjectMirror preventExtensions() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
562 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
563 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
564 |
sobj.preventExtensions(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
565 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
566 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
567 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
568 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
569 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
570 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
571 |
* Check if this script object is extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
572 |
* @return true if extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
573 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
574 |
public boolean isExtensible() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
575 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
576 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
577 |
return sobj.isExtensible(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
578 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
579 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
580 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
581 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
582 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
583 |
* ECMAScript 15.2.3.8 - seal implementation |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
584 |
* @return the sealed script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
585 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
586 |
public ScriptObjectMirror seal() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
587 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
588 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
589 |
sobj.seal(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
590 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
591 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
592 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
593 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
594 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
595 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
596 |
* Check whether this script object is sealed |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
597 |
* @return true if sealed |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
598 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
599 |
public boolean isSealed() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
600 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
601 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
602 |
return sobj.isSealed(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
603 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
604 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
605 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
606 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
607 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
608 |
* ECMA 15.2.39 - freeze implementation. Freeze this script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
609 |
* @return the frozen script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
610 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
611 |
public ScriptObjectMirror freeze() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
612 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
613 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
614 |
sobj.freeze(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
615 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
616 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
617 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
618 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
619 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
620 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
621 |
* Check whether this script object is frozen |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
622 |
* @return true if frozen |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
623 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
624 |
public boolean isFrozen() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
625 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
626 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
627 |
return sobj.isFrozen(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
628 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
629 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
630 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
631 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
632 |
/** |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
633 |
* Utility to check if given object is ECMAScript undefined value |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
634 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
635 |
* @param obj object to check |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
636 |
* @return true if 'obj' is ECMAScript undefined value |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
637 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
638 |
public static boolean isUndefined(final Object obj) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
639 |
return obj == ScriptRuntime.UNDEFINED; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
640 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
641 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
642 |
/** |
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
643 |
* Utility to convert this script object to the given type. |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
644 |
* |
24639
cde22f00c4e6
8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
23375
diff
changeset
|
645 |
* @param <T> destination type to convert to |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
646 |
* @param type destination type to convert to |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
647 |
* @return converted object |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
648 |
*/ |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
649 |
public <T> T to(final Class<T> type) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
650 |
return inGlobal(new Callable<T>() { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
651 |
@Override |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
652 |
public T call() { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
653 |
return type.cast(ScriptUtils.convert(sobj, type)); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
654 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
655 |
}); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
656 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
657 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
658 |
/** |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
659 |
* Make a script object mirror on given object if needed. |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
660 |
* |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
661 |
* @param obj object to be wrapped/converted |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
662 |
* @param homeGlobal global to which this object belongs. |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
663 |
* @return wrapped/converted object |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
664 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
665 |
public static Object wrap(final Object obj, final Object homeGlobal) { |
30977 | 666 |
return wrap(obj, homeGlobal, false); |
667 |
} |
|
668 |
||
669 |
/** |
|
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
670 |
* Make a script object mirror on given object if needed. The created wrapper will implement |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
671 |
* the Java {@code List} interface if {@code obj} is a JavaScript {@code Array} object; |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
672 |
* this is compatible with Java JSON libraries expectations. Arrays retrieved through its |
30977 | 673 |
* properties (transitively) will also implement the list interface. |
674 |
* |
|
675 |
* @param obj object to be wrapped/converted |
|
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
676 |
* @param homeGlobal global to which this object belongs. |
30977 | 677 |
* @return wrapped/converted object |
678 |
*/ |
|
679 |
public static Object wrapAsJSONCompatible(final Object obj, final Object homeGlobal) { |
|
680 |
return wrap(obj, homeGlobal, true); |
|
681 |
} |
|
682 |
||
683 |
/** |
|
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
684 |
* Make a script object mirror on given object if needed. |
30977 | 685 |
* |
686 |
* @param obj object to be wrapped/converted |
|
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
687 |
* @param homeGlobal global to which this object belongs. |
30977 | 688 |
* @param jsonCompatible if true, the created wrapper will implement the Java {@code List} interface if |
689 |
* {@code obj} is a JavaScript {@code Array} object. Arrays retrieved through its properties (transitively) |
|
690 |
* will also implement the list interface. |
|
691 |
* @return wrapped/converted object |
|
692 |
*/ |
|
693 |
private static Object wrap(final Object obj, final Object homeGlobal, final boolean jsonCompatible) { |
|
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
694 |
if(obj instanceof ScriptObject) { |
30977 | 695 |
if (!(homeGlobal instanceof Global)) { |
696 |
return obj; |
|
697 |
} |
|
698 |
final ScriptObject sobj = (ScriptObject)obj; |
|
699 |
final Global global = (Global)homeGlobal; |
|
700 |
final ScriptObjectMirror mirror = new ScriptObjectMirror(sobj, global, jsonCompatible); |
|
701 |
if (jsonCompatible && sobj.isArray()) { |
|
702 |
return new JSONListAdapter(mirror, global); |
|
703 |
} |
|
704 |
return mirror; |
|
705 |
} else if(obj instanceof ConsString) { |
|
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
706 |
return obj.toString(); |
30977 | 707 |
} else if (jsonCompatible && obj instanceof ScriptObjectMirror) { |
708 |
// Since choosing JSON compatible representation is an explicit decision on user's part, if we're asked to |
|
709 |
// wrap a mirror that was not JSON compatible, explicitly create its compatible counterpart following the |
|
710 |
// principle of least surprise. |
|
711 |
return ((ScriptObjectMirror)obj).asJSONCompatible(); |
|
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
712 |
} |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
713 |
return obj; |
16147 | 714 |
} |
715 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
716 |
/** |
30977 | 717 |
* Wraps the passed object with the same jsonCompatible flag as this mirror. |
718 |
* @param obj the object |
|
719 |
* @param homeGlobal the object's home global. |
|
720 |
* @return a wrapper for the object. |
|
721 |
*/ |
|
722 |
private Object wrapLikeMe(final Object obj, final Object homeGlobal) { |
|
723 |
return wrap(obj, homeGlobal, jsonCompatible); |
|
724 |
} |
|
725 |
||
726 |
/** |
|
727 |
* Wraps the passed object with the same home global and jsonCompatible flag as this mirror. |
|
728 |
* @param obj the object |
|
729 |
* @return a wrapper for the object. |
|
730 |
*/ |
|
731 |
private Object wrapLikeMe(final Object obj) { |
|
732 |
return wrapLikeMe(obj, global); |
|
733 |
} |
|
734 |
||
735 |
/** |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
736 |
* Unwrap a script object mirror if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
737 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
738 |
* @param obj object to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
739 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
740 |
* @return unwrapped object |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
741 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
742 |
public static Object unwrap(final Object obj, final Object homeGlobal) { |
16147 | 743 |
if (obj instanceof ScriptObjectMirror) { |
744 |
final ScriptObjectMirror mirror = (ScriptObjectMirror)obj; |
|
745 |
return (mirror.global == homeGlobal)? mirror.sobj : obj; |
|
30977 | 746 |
} else if (obj instanceof JSONListAdapter) { |
747 |
return ((JSONListAdapter)obj).unwrap(homeGlobal); |
|
16147 | 748 |
} |
749 |
||
750 |
return obj; |
|
751 |
} |
|
752 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
753 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
754 |
* 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
|
755 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
756 |
* @param args array to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
757 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
758 |
* @return wrapped array |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
759 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
760 |
public static Object[] wrapArray(final Object[] args, final Object homeGlobal) { |
30977 | 761 |
return wrapArray(args, homeGlobal, false); |
762 |
} |
|
763 |
||
764 |
private static Object[] wrapArray(final Object[] args, final Object homeGlobal, final boolean jsonCompatible) { |
|
16147 | 765 |
if (args == null || args.length == 0) { |
766 |
return args; |
|
767 |
} |
|
768 |
||
769 |
final Object[] newArgs = new Object[args.length]; |
|
770 |
int index = 0; |
|
771 |
for (final Object obj : args) { |
|
30977 | 772 |
newArgs[index] = wrap(obj, homeGlobal, jsonCompatible); |
16147 | 773 |
index++; |
774 |
} |
|
775 |
return newArgs; |
|
776 |
} |
|
777 |
||
30977 | 778 |
private Object[] wrapArrayLikeMe(final Object[] args, final Object homeGlobal) { |
779 |
return wrapArray(args, homeGlobal, jsonCompatible); |
|
780 |
} |
|
781 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
782 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
783 |
* Unwrap an array of script object mirrors if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
784 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
785 |
* @param args array to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
786 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
787 |
* @return unwrapped array |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
788 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
789 |
public static Object[] unwrapArray(final Object[] args, final Object homeGlobal) { |
16147 | 790 |
if (args == null || args.length == 0) { |
791 |
return args; |
|
792 |
} |
|
793 |
||
794 |
final Object[] newArgs = new Object[args.length]; |
|
795 |
int index = 0; |
|
796 |
for (final Object obj : args) { |
|
797 |
newArgs[index] = unwrap(obj, homeGlobal); |
|
798 |
index++; |
|
799 |
} |
|
800 |
return newArgs; |
|
801 |
} |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
802 |
|
26052
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
803 |
/** |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
804 |
* Are the given objects mirrors to same underlying object? |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
805 |
* |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
806 |
* @param obj1 first object |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
807 |
* @param obj2 second object |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
808 |
* @return true if obj1 and obj2 are identical script objects or mirrors of it. |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
809 |
*/ |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
810 |
public static boolean identical(final Object obj1, final Object obj2) { |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
811 |
final Object o1 = (obj1 instanceof ScriptObjectMirror)? |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
812 |
((ScriptObjectMirror)obj1).sobj : obj1; |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
813 |
|
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
814 |
final Object o2 = (obj2 instanceof ScriptObjectMirror)? |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
815 |
((ScriptObjectMirror)obj2).sobj : obj2; |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
816 |
|
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
817 |
return o1 == o2; |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
818 |
} |
41d18e9e45a4
8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents:
24778
diff
changeset
|
819 |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
820 |
// package-privates below this. |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
821 |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
822 |
ScriptObjectMirror(final ScriptObject sobj, final Global global) { |
30977 | 823 |
this(sobj, global, false); |
824 |
} |
|
825 |
||
826 |
private ScriptObjectMirror(final ScriptObject sobj, final Global global, final boolean jsonCompatible) { |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
827 |
assert sobj != null : "ScriptObjectMirror on null!"; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
828 |
assert global != null : "home Global is null"; |
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
829 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
830 |
this.sobj = sobj; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
831 |
this.global = global; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
832 |
this.strict = global.isStrictContext(); |
30977 | 833 |
this.jsonCompatible = jsonCompatible; |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
834 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
835 |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
836 |
// accessors for script engine |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
837 |
ScriptObject getScriptObject() { |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
838 |
return sobj; |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
839 |
} |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
840 |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
841 |
Global getHomeGlobal() { |
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
842 |
return global; |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
843 |
} |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
844 |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24772
diff
changeset
|
845 |
static Object translateUndefined(final Object obj) { |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
846 |
return (obj == ScriptRuntime.UNDEFINED)? null : obj; |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
847 |
} |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
848 |
|
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
26235
diff
changeset
|
849 |
private int getCallSiteFlags() { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
26235
diff
changeset
|
850 |
return strict ? NashornCallSiteDescriptor.CALLSITE_STRICT : 0; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
26235
diff
changeset
|
851 |
} |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
26235
diff
changeset
|
852 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
853 |
// internals only below this. |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
854 |
private <V> V inGlobal(final Callable<V> callable) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
855 |
final Global oldGlobal = Context.getGlobal(); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
856 |
final boolean globalChanged = (oldGlobal != global); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
857 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
858 |
Context.setGlobal(global); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
859 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
860 |
try { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
861 |
return callable.call(); |
22370 | 862 |
} catch (final NashornException ne) { |
863 |
throw ne.initEcmaError(global); |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
864 |
} catch (final RuntimeException e) { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
865 |
throw e; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
866 |
} catch (final Exception e) { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
867 |
throw new AssertionError("Cannot happen", e); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
868 |
} finally { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
869 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
870 |
Context.setGlobal(oldGlobal); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
871 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
872 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
873 |
} |
21449 | 874 |
|
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
875 |
/** |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
876 |
* Ensures the key is not null, empty string, or a non-String object. The contract of the {@link Bindings} |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
877 |
* interface requires that these are not accepted as keys. |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
878 |
* @param key the key to check |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
879 |
* @throws NullPointerException if key is null |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
880 |
* @throws ClassCastException if key is not a String |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
881 |
* @throws IllegalArgumentException if key is empty string |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
882 |
*/ |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
883 |
private static void checkKey(final Object key) { |
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
884 |
Objects.requireNonNull(key, "key can not be null"); |
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
885 |
|
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28577
diff
changeset
|
886 |
if (!(key instanceof String)) { |
28577
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
887 |
throw new ClassCastException("key should be a String. It is " + key.getClass().getName() + " instead."); |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
888 |
} else if (((String)key).length() == 0) { |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
889 |
throw new IllegalArgumentException("key can not be empty"); |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
890 |
} |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
891 |
} |
d294bba4f2a4
8068603: ScriptObjectMirror should reject null/empty string/non-string parameters in Bindings methods
attila
parents:
28317
diff
changeset
|
892 |
|
29413
153d198cfdc3
8074661: Forward port AbstractJSObject.getDefaultValue(JSObject, Class)
attila
parents:
29133
diff
changeset
|
893 |
@Override @Deprecated |
21449 | 894 |
public double toNumber() { |
895 |
return inGlobal(new Callable<Double>() { |
|
896 |
@Override public Double call() { |
|
897 |
return JSType.toNumber(sobj); |
|
898 |
} |
|
899 |
}); |
|
900 |
} |
|
29133
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
901 |
|
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
902 |
@Override |
29413
153d198cfdc3
8074661: Forward port AbstractJSObject.getDefaultValue(JSObject, Class)
attila
parents:
29133
diff
changeset
|
903 |
public Object getDefaultValue(final Class<?> hint) { |
29133
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
904 |
return inGlobal(new Callable<Object>() { |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
905 |
@Override public Object call() { |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
906 |
try { |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
907 |
return sobj.getDefaultValue(hint); |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
908 |
} catch (final ECMAException e) { |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
909 |
// We're catching ECMAException (likely TypeError), and translating it to |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
910 |
// UnsupportedOperationException. This in turn will be translated into TypeError of the |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
911 |
// caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
912 |
// recognized as "instanceof TypeError" in the caller. |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
913 |
throw new UnsupportedOperationException(e.getMessage(), e); |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
914 |
} |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
915 |
} |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
916 |
}); |
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28878
diff
changeset
|
917 |
} |
30977 | 918 |
|
919 |
private ScriptObjectMirror asJSONCompatible() { |
|
920 |
if (this.jsonCompatible) { |
|
921 |
return this; |
|
922 |
} |
|
923 |
return new ScriptObjectMirror(sobj, global, true); |
|
924 |
} |
|
16147 | 925 |
} |