author | sundar |
Fri, 30 May 2014 17:22:38 +0530 | |
changeset 24639 | cde22f00c4e6 |
parent 23375 | a1110f2cbe75 |
child 24770 | 17f8e3b82ad3 |
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; |
|
42 |
import java.util.Set; |
|
43 |
import java.util.concurrent.Callable; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
44 |
import javax.script.Bindings; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
45 |
import jdk.nashorn.internal.objects.Global; |
23078
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
46 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
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; |
21449 | 49 |
import jdk.nashorn.internal.runtime.JSType; |
16147 | 50 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
51 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
52 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
16147 | 53 |
|
54 |
/** |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
55 |
* Mirror object that wraps a given Nashorn Script object. |
16147 | 56 |
*/ |
21453
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
21450
diff
changeset
|
57 |
public final class ScriptObjectMirror extends AbstractJSObject implements Bindings { |
19459 | 58 |
private static AccessControlContext getContextAccCtxt() { |
59 |
final Permissions perms = new Permissions(); |
|
60 |
perms.add(new RuntimePermission(Context.NASHORN_GET_CONTEXT)); |
|
61 |
return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) }); |
|
62 |
} |
|
63 |
||
64 |
private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt(); |
|
65 |
||
16147 | 66 |
private final ScriptObject sobj; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
67 |
private final Global global; |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
68 |
private final boolean strict; |
16147 | 69 |
|
70 |
@Override |
|
71 |
public boolean equals(final Object other) { |
|
72 |
if (other instanceof ScriptObjectMirror) { |
|
73 |
return sobj.equals(((ScriptObjectMirror)other).sobj); |
|
74 |
} |
|
75 |
||
76 |
return false; |
|
77 |
} |
|
78 |
||
79 |
@Override |
|
80 |
public int hashCode() { |
|
81 |
return sobj.hashCode(); |
|
82 |
} |
|
83 |
||
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
84 |
@Override |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
85 |
public String toString() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
86 |
return inGlobal(new Callable<String>() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
87 |
@Override |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
88 |
public String call() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
89 |
return ScriptRuntime.safeToString(sobj); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
90 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
91 |
}); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
92 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
93 |
|
16147 | 94 |
// JSObject methods |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
95 |
|
16147 | 96 |
@Override |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
97 |
public Object call(final Object thiz, final Object... args) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
98 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 99 |
final boolean globalChanged = (oldGlobal != global); |
100 |
||
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
101 |
try { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
102 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
103 |
Context.setGlobal(global); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
104 |
} |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
105 |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
106 |
if (sobj instanceof ScriptFunction) { |
19620
3f0c79b63846
8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents:
19619
diff
changeset
|
107 |
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
108 |
final Object self = globalChanged? wrap(thiz, oldGlobal) : thiz; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
109 |
return wrap(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)), global); |
16147 | 110 |
} |
111 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
112 |
throw new RuntimeException("not a function: " + toString()); |
22370 | 113 |
} catch (final NashornException ne) { |
114 |
throw ne.initEcmaError(global); |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
115 |
} catch (final RuntimeException | Error e) { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
116 |
throw e; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
117 |
} catch (final Throwable t) { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
118 |
throw new RuntimeException(t); |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
119 |
} finally { |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
120 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
121 |
Context.setGlobal(oldGlobal); |
16147 | 122 |
} |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
123 |
} |
16147 | 124 |
} |
125 |
||
126 |
@Override |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
127 |
public Object newObject(final Object... args) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
128 |
final Global oldGlobal = Context.getGlobal(); |
18614 | 129 |
final boolean globalChanged = (oldGlobal != global); |
130 |
||
131 |
try { |
|
132 |
if (globalChanged) { |
|
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
133 |
Context.setGlobal(global); |
18614 | 134 |
} |
135 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
136 |
if (sobj instanceof ScriptFunction) { |
19620
3f0c79b63846
8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents:
19619
diff
changeset
|
137 |
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
138 |
return wrap(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global)), global); |
18614 | 139 |
} |
140 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
141 |
throw new RuntimeException("not a constructor: " + toString()); |
22370 | 142 |
} catch (final NashornException ne) { |
143 |
throw ne.initEcmaError(global); |
|
18614 | 144 |
} catch (final RuntimeException | Error e) { |
145 |
throw e; |
|
146 |
} catch (final Throwable t) { |
|
147 |
throw new RuntimeException(t); |
|
148 |
} finally { |
|
149 |
if (globalChanged) { |
|
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
150 |
Context.setGlobal(oldGlobal); |
18614 | 151 |
} |
152 |
} |
|
153 |
} |
|
154 |
||
155 |
@Override |
|
16147 | 156 |
public Object eval(final String s) { |
157 |
return inGlobal(new Callable<Object>() { |
|
158 |
@Override |
|
159 |
public Object call() { |
|
16185 | 160 |
final Context context = AccessController.doPrivileged( |
161 |
new PrivilegedAction<Context>() { |
|
162 |
@Override |
|
163 |
public Context run() { |
|
164 |
return Context.getContext(); |
|
165 |
} |
|
19459 | 166 |
}, GET_CONTEXT_ACC_CTXT); |
16185 | 167 |
return wrap(context.eval(global, s, null, null, false), global); |
16147 | 168 |
} |
169 |
}); |
|
170 |
} |
|
171 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
172 |
public Object callMember(final String functionName, final Object... args) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
173 |
functionName.getClass(); // null check |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
174 |
final Global oldGlobal = Context.getGlobal(); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
175 |
final boolean globalChanged = (oldGlobal != global); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
176 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
177 |
try { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
178 |
if (globalChanged) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
179 |
Context.setGlobal(global); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
180 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
181 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
182 |
final Object val = sobj.get(functionName); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
183 |
if (val instanceof ScriptFunction) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
184 |
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
185 |
return wrap(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
186 |
} else if (val instanceof JSObject && ((JSObject)val).isFunction()) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
187 |
return ((JSObject)val).call(sobj, args); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
188 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
189 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
190 |
throw new NoSuchMethodException("No such function " + functionName); |
22370 | 191 |
} catch (final NashornException ne) { |
192 |
throw ne.initEcmaError(global); |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
193 |
} catch (final RuntimeException | Error e) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
194 |
throw e; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
195 |
} catch (final Throwable t) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
196 |
throw new RuntimeException(t); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
197 |
} finally { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
198 |
if (globalChanged) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
199 |
Context.setGlobal(oldGlobal); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
200 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
201 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
202 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
203 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
204 |
@Override |
16147 | 205 |
public Object getMember(final String name) { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
206 |
name.getClass(); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
207 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
208 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
209 |
return wrap(sobj.get(name), global); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
210 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
211 |
}); |
16147 | 212 |
} |
213 |
||
214 |
@Override |
|
215 |
public Object getSlot(final int index) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
216 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
217 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
218 |
return wrap(sobj.get(index), global); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
219 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
220 |
}); |
16147 | 221 |
} |
222 |
||
223 |
@Override |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
224 |
public boolean hasMember(final String name) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
225 |
name.getClass(); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
226 |
return inGlobal(new Callable<Boolean>() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
227 |
@Override public Boolean call() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
228 |
return sobj.has(name); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
229 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
230 |
}); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
231 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
232 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
233 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
234 |
public boolean hasSlot(final int slot) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
235 |
return inGlobal(new Callable<Boolean>() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
236 |
@Override public Boolean call() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
237 |
return sobj.has(slot); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
238 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
239 |
}); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
240 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
241 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
242 |
@Override |
16147 | 243 |
public void removeMember(final String name) { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
244 |
name.getClass(); |
16147 | 245 |
remove(name); |
246 |
} |
|
247 |
||
248 |
@Override |
|
249 |
public void setMember(final String name, final Object value) { |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
250 |
name.getClass(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
251 |
put(name, value); |
16147 | 252 |
} |
253 |
||
254 |
@Override |
|
255 |
public void setSlot(final int index, final Object value) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
256 |
inGlobal(new Callable<Void>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
257 |
@Override public Void call() { |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
258 |
sobj.set(index, unwrap(value, global), strict); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
259 |
return null; |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
260 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
261 |
}); |
16147 | 262 |
} |
263 |
||
23078
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
264 |
/** |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
265 |
* Nashorn extension: setIndexedPropertiesToExternalArrayData. |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
266 |
* set indexed properties be exposed from a given nio ByteBuffer. |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
267 |
* |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
268 |
* @param buf external buffer - should be a nio ByteBuffer |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
269 |
*/ |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
270 |
public void setIndexedPropertiesToExternalArrayData(final ByteBuffer buf) { |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
271 |
inGlobal(new Callable<Void>() { |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
272 |
@Override public Void call() { |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
273 |
sobj.setArray(ArrayData.allocate(buf)); |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
274 |
return null; |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
275 |
} |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
276 |
}); |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
277 |
} |
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
278 |
|
06c03700f884
8011964: need indexed access to externally-managed ByteBuffer
sundar
parents:
22370
diff
changeset
|
279 |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
280 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
281 |
public boolean isInstance(final Object obj) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
282 |
if (! (obj instanceof ScriptObjectMirror)) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
283 |
return false; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
284 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
285 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
286 |
final ScriptObjectMirror instance = (ScriptObjectMirror)obj; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
287 |
// if not belongs to my global scope, return false |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
288 |
if (global != instance.global) { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
289 |
return false; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
290 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
291 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
292 |
return inGlobal(new Callable<Boolean>() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
293 |
@Override public Boolean call() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
294 |
return sobj.isInstance(instance.sobj); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
295 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
296 |
}); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
297 |
} |
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 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
300 |
public String getClassName() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
301 |
return sobj.getClassName(); |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
302 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
303 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
304 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
305 |
public boolean isFunction() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
306 |
return sobj instanceof ScriptFunction; |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
307 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
308 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
309 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
310 |
public boolean isStrictFunction() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
311 |
return isFunction() && ((ScriptFunction)sobj).isStrict(); |
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 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
314 |
@Override |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
315 |
public boolean isArray() { |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19884
diff
changeset
|
316 |
return sobj.isArray(); |
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 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
319 |
// javax.script.Bindings methods |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
320 |
|
16147 | 321 |
@Override |
322 |
public void clear() { |
|
323 |
inGlobal(new Callable<Object>() { |
|
324 |
@Override public Object call() { |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
325 |
sobj.clear(strict); |
16147 | 326 |
return null; |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
327 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
328 |
}); |
16147 | 329 |
} |
330 |
||
331 |
@Override |
|
332 |
public boolean containsKey(final Object key) { |
|
333 |
return inGlobal(new Callable<Boolean>() { |
|
334 |
@Override public Boolean call() { |
|
335 |
return sobj.containsKey(unwrap(key, global)); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
336 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
337 |
}); |
16147 | 338 |
} |
339 |
||
340 |
@Override |
|
341 |
public boolean containsValue(final Object value) { |
|
342 |
return inGlobal(new Callable<Boolean>() { |
|
343 |
@Override public Boolean call() { |
|
344 |
return sobj.containsValue(unwrap(value, global)); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
345 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
346 |
}); |
16147 | 347 |
} |
348 |
||
349 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
350 |
public Set<Map.Entry<String, Object>> entrySet() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
351 |
return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
352 |
@Override public Set<Map.Entry<String, Object>> call() { |
16147 | 353 |
final Iterator<String> iter = sobj.propertyIterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
354 |
final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>(); |
16147 | 355 |
|
356 |
while (iter.hasNext()) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
357 |
final String key = iter.next(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
358 |
final Object value = translateUndefined(wrap(sobj.get(key), global)); |
16147 | 359 |
entries.add(new AbstractMap.SimpleImmutableEntry<>(key, value)); |
360 |
} |
|
361 |
||
362 |
return Collections.unmodifiableSet(entries); |
|
363 |
} |
|
364 |
}); |
|
365 |
} |
|
366 |
||
367 |
@Override |
|
368 |
public Object get(final Object key) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
369 |
return inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
370 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
371 |
return translateUndefined(wrap(sobj.get(key), global)); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
372 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
373 |
}); |
16147 | 374 |
} |
375 |
||
376 |
@Override |
|
377 |
public boolean isEmpty() { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
378 |
return inGlobal(new Callable<Boolean>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
379 |
@Override public Boolean call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
380 |
return sobj.isEmpty(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
381 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
382 |
}); |
16147 | 383 |
} |
384 |
||
385 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
386 |
public Set<String> keySet() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
387 |
return inGlobal(new Callable<Set<String>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
388 |
@Override public Set<String> call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
389 |
final Iterator<String> iter = sobj.propertyIterator(); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
390 |
final Set<String> keySet = new LinkedHashSet<>(); |
16147 | 391 |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
392 |
while (iter.hasNext()) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
393 |
keySet.add(iter.next()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
394 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
395 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
396 |
return Collections.unmodifiableSet(keySet); |
16147 | 397 |
} |
16182
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 Object put(final String key, final Object value) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
403 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
404 |
final boolean globalChanged = (oldGlobal != global); |
16147 | 405 |
return inGlobal(new Callable<Object>() { |
406 |
@Override public Object call() { |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
407 |
final Object modValue = globalChanged? wrap(value, oldGlobal) : value; |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
408 |
return translateUndefined(wrap(sobj.put(key, unwrap(modValue, global), strict), global)); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
409 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
410 |
}); |
16147 | 411 |
} |
412 |
||
413 |
@Override |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
414 |
public void putAll(final Map<? extends String, ? extends Object> map) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
415 |
final ScriptObject oldGlobal = Context.getGlobal(); |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
416 |
final boolean globalChanged = (oldGlobal != global); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
417 |
inGlobal(new Callable<Object>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
418 |
@Override public Object call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
419 |
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
|
420 |
final Object value = entry.getValue(); |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
421 |
final Object modValue = globalChanged? wrap(value, oldGlobal) : value; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
422 |
sobj.set(entry.getKey(), unwrap(modValue, global), strict); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
423 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
424 |
return null; |
16147 | 425 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
426 |
}); |
16147 | 427 |
} |
428 |
||
429 |
@Override |
|
430 |
public Object remove(final Object key) { |
|
431 |
return inGlobal(new Callable<Object>() { |
|
432 |
@Override public Object call() { |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
433 |
return wrap(sobj.remove(unwrap(key, global), strict), global); |
16147 | 434 |
} |
435 |
}); |
|
436 |
} |
|
437 |
||
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
438 |
/** |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
439 |
* Delete a property from this object. |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
440 |
* |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
441 |
* @param key the property to be deleted |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
442 |
* |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
443 |
* @return if the delete was successful or not |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
444 |
*/ |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
445 |
public boolean delete(final Object key) { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
446 |
return inGlobal(new Callable<Boolean>() { |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
447 |
@Override public Boolean call() { |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
19638
diff
changeset
|
448 |
return sobj.delete(unwrap(key, global), strict); |
17978
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
449 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
450 |
}); |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
451 |
} |
750d0582d8e2
8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents:
17751
diff
changeset
|
452 |
|
16147 | 453 |
@Override |
454 |
public int size() { |
|
455 |
return inGlobal(new Callable<Integer>() { |
|
456 |
@Override public Integer call() { |
|
457 |
return sobj.size(); |
|
458 |
} |
|
459 |
}); |
|
460 |
} |
|
461 |
||
462 |
@Override |
|
463 |
public Collection<Object> values() { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
464 |
return inGlobal(new Callable<Collection<Object>>() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
465 |
@Override public Collection<Object> call() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
466 |
final List<Object> values = new ArrayList<>(size()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
467 |
final Iterator<Object> iter = sobj.valueIterator(); |
16147 | 468 |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
469 |
while (iter.hasNext()) { |
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16189
diff
changeset
|
470 |
values.add(translateUndefined(wrap(iter.next(), global))); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
471 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
472 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
473 |
return Collections.unmodifiableList(values); |
16147 | 474 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
475 |
}); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
476 |
} |
16147 | 477 |
|
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
478 |
// Support for ECMAScript Object API on mirrors |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
479 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
480 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
481 |
* Return the __proto__ of this object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
482 |
* @return __proto__ object. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
483 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
484 |
public Object getProto() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
485 |
return inGlobal(new Callable<Object>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
486 |
@Override public Object call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
487 |
return wrap(sobj.getProto(), global); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
488 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
489 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
490 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
491 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
492 |
/** |
19619
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
493 |
* Set the __proto__ of this object. |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
494 |
* @param proto new proto for this object |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
495 |
*/ |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
496 |
public void setProto(final Object proto) { |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
497 |
inGlobal(new Callable<Void>() { |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
498 |
@Override public Void call() { |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
499 |
sobj.setProtoCheck(unwrap(proto, global)); |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
500 |
return null; |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
501 |
} |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
502 |
}); |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
503 |
} |
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
504 |
|
4085b74056ee
8023368: Instance __proto__ property should exist and be writable.
sundar
parents:
19459
diff
changeset
|
505 |
/** |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
506 |
* ECMA 8.12.1 [[GetOwnProperty]] (P) |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
507 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
508 |
* @param key property key |
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 |
* @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
|
511 |
* object, or undefined if absent. |
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 |
public Object getOwnPropertyDescriptor(final String key) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
514 |
return inGlobal(new Callable<Object>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
515 |
@Override public Object call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
516 |
return wrap(sobj.getOwnPropertyDescriptor(key), global); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
517 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
518 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
519 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
520 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
521 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
522 |
* 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
|
523 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
524 |
* @param all True if to include non-enumerable keys. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
525 |
* @return Array of keys. |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
526 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
527 |
public String[] getOwnKeys(final boolean all) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
528 |
return inGlobal(new Callable<String[]>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
529 |
@Override public String[] call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
530 |
return sobj.getOwnKeys(all); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
531 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
532 |
}); |
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 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
535 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
536 |
* Flag this script object as non extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
537 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
538 |
* @return the object after being made non extensible |
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 |
public ScriptObjectMirror preventExtensions() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
541 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
542 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
543 |
sobj.preventExtensions(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
544 |
return ScriptObjectMirror.this; |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
545 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
546 |
}); |
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 |
|
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
549 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
550 |
* Check if this script object is extensible |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
551 |
* @return true if extensible |
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 |
public boolean isExtensible() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
554 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
555 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
556 |
return sobj.isExtensible(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
557 |
} |
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 |
} |
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 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
562 |
* ECMAScript 15.2.3.8 - seal implementation |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
563 |
* @return the sealed script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
564 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
565 |
public ScriptObjectMirror seal() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
566 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
567 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
568 |
sobj.seal(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
569 |
return ScriptObjectMirror.this; |
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 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
572 |
} |
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 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
575 |
* Check whether this script object is sealed |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
576 |
* @return true if sealed |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
577 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
578 |
public boolean isSealed() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
579 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
580 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
581 |
return sobj.isSealed(); |
18334
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 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
584 |
} |
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 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
587 |
* ECMA 15.2.39 - freeze implementation. Freeze this script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
588 |
* @return the frozen script object |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
589 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
590 |
public ScriptObjectMirror freeze() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
591 |
return inGlobal(new Callable<ScriptObjectMirror>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
592 |
@Override public ScriptObjectMirror call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
593 |
sobj.freeze(); |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
594 |
return ScriptObjectMirror.this; |
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 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
597 |
} |
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 |
/** |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
600 |
* Check whether this script object is frozen |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
601 |
* @return true if frozen |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
602 |
*/ |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
603 |
public boolean isFrozen() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
604 |
return inGlobal(new Callable<Boolean>() { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
605 |
@Override public Boolean call() { |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
606 |
return sobj.isFrozen(); |
18334
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 |
}); |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
609 |
} |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
610 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
611 |
/** |
18334
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
612 |
* Utility to check if given object is ECMAScript undefined value |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
613 |
* |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
614 |
* @param obj object to check |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
615 |
* @return true if 'obj' is ECMAScript undefined value |
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 |
public static boolean isUndefined(final Object obj) { |
47413e8d71b5
8016618: script mirror object access should be improved
sundar
parents:
17979
diff
changeset
|
618 |
return obj == ScriptRuntime.UNDEFINED; |
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 |
/** |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
622 |
* Utilitity to convert this script object to the given type. |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
623 |
* |
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
|
624 |
* @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
|
625 |
* @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
|
626 |
* @return converted object |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
627 |
*/ |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
628 |
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
|
629 |
return inGlobal(new Callable<T>() { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
630 |
@Override |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
631 |
public T call() { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
632 |
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
|
633 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
634 |
}); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
635 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
636 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
637 |
/** |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
638 |
* Make a script object mirror on given object if needed. Also converts ConsString instances to Strings. |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
639 |
* |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
640 |
* @param obj object to be wrapped/converted |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
641 |
* @param homeGlobal global to which this object belongs. Not used for ConsStrings. |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
642 |
* @return wrapped/converted object |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
643 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
644 |
public static Object wrap(final Object obj, final Object homeGlobal) { |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
645 |
if(obj instanceof ScriptObject) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
646 |
return homeGlobal instanceof Global ? new ScriptObjectMirror((ScriptObject)obj, (Global)homeGlobal) : obj; |
21686
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
647 |
} |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
648 |
if(obj instanceof ConsString) { |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
649 |
return obj.toString(); |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
650 |
} |
5c6946f97d6f
8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents:
21453
diff
changeset
|
651 |
return obj; |
16147 | 652 |
} |
653 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
654 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
655 |
* Unwrap a script object mirror if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
656 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
657 |
* @param obj object to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
658 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
659 |
* @return unwrapped object |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
660 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
661 |
public static Object unwrap(final Object obj, final Object homeGlobal) { |
16147 | 662 |
if (obj instanceof ScriptObjectMirror) { |
663 |
final ScriptObjectMirror mirror = (ScriptObjectMirror)obj; |
|
664 |
return (mirror.global == homeGlobal)? mirror.sobj : obj; |
|
665 |
} |
|
666 |
||
667 |
return obj; |
|
668 |
} |
|
669 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
670 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
671 |
* 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
|
672 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
673 |
* @param args array to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
674 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
675 |
* @return wrapped array |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
676 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
677 |
public static Object[] wrapArray(final Object[] args, final Object homeGlobal) { |
16147 | 678 |
if (args == null || args.length == 0) { |
679 |
return args; |
|
680 |
} |
|
681 |
||
682 |
final Object[] newArgs = new Object[args.length]; |
|
683 |
int index = 0; |
|
684 |
for (final Object obj : args) { |
|
685 |
newArgs[index] = wrap(obj, homeGlobal); |
|
686 |
index++; |
|
687 |
} |
|
688 |
return newArgs; |
|
689 |
} |
|
690 |
||
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
691 |
/** |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
692 |
* Unwrap an array of script object mirrors if needed. |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
693 |
* |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
694 |
* @param args array to be unwrapped |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
695 |
* @param homeGlobal global to which this object belongs |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
696 |
* @return unwrapped array |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
697 |
*/ |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
21686
diff
changeset
|
698 |
public static Object[] unwrapArray(final Object[] args, final Object homeGlobal) { |
16147 | 699 |
if (args == null || args.length == 0) { |
700 |
return args; |
|
701 |
} |
|
702 |
||
703 |
final Object[] newArgs = new Object[args.length]; |
|
704 |
int index = 0; |
|
705 |
for (final Object obj : args) { |
|
706 |
newArgs[index] = unwrap(obj, homeGlobal); |
|
707 |
index++; |
|
708 |
} |
|
709 |
return newArgs; |
|
710 |
} |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
711 |
|
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
712 |
// package-privates below this. |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
713 |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
714 |
ScriptObjectMirror(final ScriptObject sobj, final Global global) { |
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
715 |
assert sobj != null : "ScriptObjectMirror on null!"; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
716 |
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
|
717 |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
718 |
this.sobj = sobj; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
719 |
this.global = global; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
720 |
this.strict = global.isStrictContext(); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
721 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
722 |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
723 |
// accessors for script engine |
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
724 |
ScriptObject getScriptObject() { |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
725 |
return sobj; |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
726 |
} |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
727 |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
728 |
Global getHomeGlobal() { |
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
729 |
return global; |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
730 |
} |
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
731 |
|
17979
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
732 |
static Object translateUndefined(Object obj) { |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
733 |
return (obj == ScriptRuntime.UNDEFINED)? null : obj; |
adae4d39ee07
8015945: loadWithNewGlobal return value has to be properly wrapped
sundar
parents:
17978
diff
changeset
|
734 |
} |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
735 |
|
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
736 |
// internals only below this. |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
737 |
private <V> V inGlobal(final Callable<V> callable) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23078
diff
changeset
|
738 |
final Global oldGlobal = Context.getGlobal(); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
739 |
final boolean globalChanged = (oldGlobal != global); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
740 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
741 |
Context.setGlobal(global); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
742 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
743 |
try { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
744 |
return callable.call(); |
22370 | 745 |
} catch (final NashornException ne) { |
746 |
throw ne.initEcmaError(global); |
|
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
747 |
} catch (final RuntimeException e) { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
748 |
throw e; |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
749 |
} catch (final Exception e) { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
750 |
throw new AssertionError("Cannot happen", e); |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
751 |
} finally { |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
752 |
if (globalChanged) { |
19085
066c9e5afd79
8020731: Revisit checkPermission calls in Context class
sundar
parents:
18874
diff
changeset
|
753 |
Context.setGlobal(oldGlobal); |
18874
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
754 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
755 |
} |
8ba96bd382d3
8020463: Input argument array wrapping in loadWithNewGlobal is wrong
sundar
parents:
18851
diff
changeset
|
756 |
} |
21449 | 757 |
|
21450
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
758 |
@Override |
21449 | 759 |
public double toNumber() { |
760 |
return inGlobal(new Callable<Double>() { |
|
761 |
@Override public Double call() { |
|
762 |
return JSType.toNumber(sobj); |
|
763 |
} |
|
764 |
}); |
|
765 |
} |
|
16147 | 766 |
} |