author | sundar |
Tue, 22 Jan 2013 22:07:12 +0530 | |
changeset 16188 | d6390b0ea32a |
parent 16186 | 91bd9d2aa2f5 |
child 16195 | 3f6c0ab2597a |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.objects; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
30 |
import static jdk.nashorn.internal.runtime.linker.Lookup.MH; |
|
31 |
||
32 |
import java.io.IOException; |
|
33 |
import java.io.PrintWriter; |
|
34 |
import java.lang.invoke.MethodHandle; |
|
35 |
import java.lang.invoke.MethodHandles; |
|
36 |
import java.lang.ref.SoftReference; |
|
37 |
import java.util.LinkedHashMap; |
|
38 |
import java.util.List; |
|
39 |
import java.util.Map; |
|
40 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
|
41 |
import jdk.nashorn.internal.objects.annotations.Property; |
|
42 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
43 |
import jdk.nashorn.internal.runtime.ConsString; |
|
44 |
import jdk.nashorn.internal.runtime.Context; |
|
45 |
import jdk.nashorn.internal.runtime.GlobalFunctions; |
|
46 |
import jdk.nashorn.internal.runtime.GlobalObject; |
|
47 |
import jdk.nashorn.internal.runtime.JSType; |
|
48 |
import jdk.nashorn.internal.runtime.NativeJavaPackage; |
|
16185 | 49 |
import jdk.nashorn.internal.runtime.OptionsObject; |
16147 | 50 |
import jdk.nashorn.internal.runtime.PropertyDescriptor; |
51 |
import jdk.nashorn.internal.runtime.Scope; |
|
52 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
53 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
54 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
55 |
import jdk.nashorn.internal.runtime.ScriptingFunctions; |
|
56 |
import jdk.nashorn.internal.runtime.Source; |
|
57 |
import jdk.nashorn.internal.runtime.linker.InvokeByName; |
|
58 |
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
|
59 |
import jdk.nashorn.internal.scripts.JO$; |
|
60 |
import org.dynalang.dynalink.linker.GuardedInvocation; |
|
61 |
||
62 |
/** |
|
63 |
* Representation of global scope. |
|
64 |
*/ |
|
65 |
@ScriptClass("Global") |
|
66 |
public final class Global extends ScriptObject implements GlobalObject, Scope { |
|
67 |
private static final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class); |
|
68 |
private static final InvokeByName VALUE_OF = new InvokeByName("valueOf", ScriptObject.class); |
|
69 |
||
70 |
/** ECMA 15.1.2.2 parseInt (string , radix) */ |
|
71 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
72 |
public Object parseInt; |
|
73 |
||
74 |
/** ECMA 15.1.2.3 parseFloat (string) */ |
|
75 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
76 |
public Object parseFloat; |
|
77 |
||
78 |
/** ECMA 15.1.2.4 isNaN (number) */ |
|
79 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
80 |
public Object isNaN; |
|
81 |
||
82 |
/** ECMA 15.1.2.5 isFinite (number) */ |
|
83 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
84 |
public Object isFinite; |
|
85 |
||
86 |
/** ECMA 15.1.3.3 encodeURI */ |
|
87 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
88 |
public Object encodeURI; |
|
89 |
||
90 |
/** ECMA 15.1.3.4 encodeURIComponent */ |
|
91 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
92 |
public Object encodeURIComponent; |
|
93 |
||
94 |
/** ECMA 15.1.3.1 decodeURI */ |
|
95 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
96 |
public Object decodeURI; |
|
97 |
||
98 |
/** ECMA 15.1.3.2 decodeURIComponent */ |
|
99 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
100 |
public Object decodeURIComponent; |
|
101 |
||
102 |
/** ECMA B.2.1 escape (string) */ |
|
103 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
104 |
public Object escape; |
|
105 |
||
106 |
/** ECMA B.2.2 unescape (string) */ |
|
107 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
108 |
public Object unescape; |
|
109 |
||
110 |
/** Nashorn extension: global.print */ |
|
111 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
112 |
public Object print; |
|
113 |
||
114 |
/** Nashorn extension: global.load */ |
|
115 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
116 |
public Object load; |
|
117 |
||
118 |
/** Value property NaN of the Global Object - ECMA 15.1.1.1 NaN */ |
|
119 |
@Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT) |
|
120 |
public final Object NaN = Double.NaN; |
|
121 |
||
122 |
/** Value property Infinity of the Global Object - ECMA 15.1.1.2 Infinity */ |
|
123 |
@Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT) |
|
124 |
public final Object Infinity = Double.POSITIVE_INFINITY; |
|
125 |
||
126 |
/** Value property Undefined of the Global Object - ECMA 15.1.1.3 Undefined */ |
|
127 |
@Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT) |
|
128 |
public final Object undefined = UNDEFINED; |
|
129 |
||
130 |
/** ECMA 15.1.2.1 eval(x) */ |
|
131 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
132 |
public Object eval; |
|
133 |
||
134 |
/** ECMA 15.1.4.1 Object constructor. */ |
|
135 |
@Property(name = "Object", attributes = Attribute.NOT_ENUMERABLE) |
|
136 |
public volatile Object object; |
|
137 |
||
138 |
/** ECMA 15.1.4.2 Function constructor. */ |
|
139 |
@Property(name = "Function", attributes = Attribute.NOT_ENUMERABLE) |
|
140 |
public volatile Object function; |
|
141 |
||
142 |
/** ECMA 15.1.4.3 Array constructor. */ |
|
143 |
@Property(name = "Array", attributes = Attribute.NOT_ENUMERABLE) |
|
144 |
public volatile Object array; |
|
145 |
||
146 |
/** ECMA 15.1.4.4 String constructor */ |
|
147 |
@Property(name = "String", attributes = Attribute.NOT_ENUMERABLE) |
|
148 |
public volatile Object string; |
|
149 |
||
150 |
/** ECMA 15.1.4.5 Boolean constructor */ |
|
151 |
@Property(name = "Boolean", attributes = Attribute.NOT_ENUMERABLE) |
|
152 |
public volatile Object _boolean; |
|
153 |
||
154 |
/** ECMA 15.1.4.6 - Number constructor */ |
|
155 |
@Property(name = "Number", attributes = Attribute.NOT_ENUMERABLE) |
|
156 |
public volatile Object number; |
|
157 |
||
158 |
/** ECMA 15.1.4.7 Date constructor */ |
|
159 |
@Property(name = "Date", attributes = Attribute.NOT_ENUMERABLE) |
|
160 |
public volatile Object date; |
|
161 |
||
162 |
/** ECMA 15.1.4.8 RegExp constructor */ |
|
163 |
@Property(name = "RegExp", attributes = Attribute.NOT_ENUMERABLE) |
|
164 |
public volatile Object regexp; |
|
165 |
||
166 |
/** ECMA 15.12 - The JSON object */ |
|
167 |
@Property(name = "JSON", attributes = Attribute.NOT_ENUMERABLE) |
|
168 |
public volatile Object json; |
|
169 |
||
170 |
/** Nashorn extension: global.JSAdapter */ |
|
171 |
@Property(name = "JSAdapter", attributes = Attribute.NOT_ENUMERABLE) |
|
172 |
public volatile Object jsadapter; |
|
173 |
||
174 |
/** ECMA 15.8 - The Math object */ |
|
175 |
@Property(name = "Math", attributes = Attribute.NOT_ENUMERABLE) |
|
176 |
public volatile Object math; |
|
177 |
||
178 |
/** Error object */ |
|
179 |
@Property(name = "Error", attributes = Attribute.NOT_ENUMERABLE) |
|
180 |
public volatile Object error; |
|
181 |
||
182 |
/** EvalError object */ |
|
183 |
@Property(name = "EvalError", attributes = Attribute.NOT_ENUMERABLE) |
|
184 |
public volatile Object evalError; |
|
185 |
||
186 |
/** RangeError object */ |
|
187 |
@Property(name = "RangeError", attributes = Attribute.NOT_ENUMERABLE) |
|
188 |
public volatile Object rangeError; |
|
189 |
||
190 |
/** ReferenceError object */ |
|
191 |
@Property(name = "ReferenceError", attributes = Attribute.NOT_ENUMERABLE) |
|
192 |
public volatile Object referenceError; |
|
193 |
||
194 |
/** SyntaxError object */ |
|
195 |
@Property(name = "SyntaxError", attributes = Attribute.NOT_ENUMERABLE) |
|
196 |
public volatile Object syntaxError; |
|
197 |
||
198 |
/** TypeError object */ |
|
199 |
@Property(name = "TypeError", attributes = Attribute.NOT_ENUMERABLE) |
|
200 |
public volatile Object typeError; |
|
201 |
||
202 |
/** URIError object */ |
|
203 |
@Property(name = "URIError", attributes = Attribute.NOT_ENUMERABLE) |
|
204 |
public volatile Object uriError; |
|
205 |
||
206 |
/** ArrayBuffer object */ |
|
207 |
@Property(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE) |
|
208 |
public volatile Object arrayBuffer; |
|
209 |
||
210 |
/** TypedArray (int8) */ |
|
211 |
@Property(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE) |
|
212 |
public volatile Object int8Array; |
|
213 |
||
214 |
/** TypedArray (uint8) */ |
|
215 |
@Property(name = "Uint8Array", attributes = Attribute.NOT_ENUMERABLE) |
|
216 |
public volatile Object uint8Array; |
|
217 |
||
218 |
/** TypedArray (uint8) - Clamped */ |
|
219 |
@Property(name = "Uint8ClampedArray", attributes = Attribute.NOT_ENUMERABLE) |
|
220 |
public volatile Object uint8ClampedArray; |
|
221 |
||
222 |
/** TypedArray (int16) */ |
|
223 |
@Property(name = "Int16Array", attributes = Attribute.NOT_ENUMERABLE) |
|
224 |
public volatile Object int16Array; |
|
225 |
||
226 |
/** TypedArray (uint16) */ |
|
227 |
@Property(name = "Uint16Array", attributes = Attribute.NOT_ENUMERABLE) |
|
228 |
public volatile Object uint16Array; |
|
229 |
||
230 |
/** TypedArray (int32) */ |
|
231 |
@Property(name = "Int32Array", attributes = Attribute.NOT_ENUMERABLE) |
|
232 |
public volatile Object int32Array; |
|
233 |
||
234 |
/** TypedArray (uint32) */ |
|
235 |
@Property(name = "Uint32Array", attributes = Attribute.NOT_ENUMERABLE) |
|
236 |
public volatile Object uint32Array; |
|
237 |
||
238 |
/** TypedArray (float32) */ |
|
239 |
@Property(name = "Float32Array", attributes = Attribute.NOT_ENUMERABLE) |
|
240 |
public volatile Object float32Array; |
|
241 |
||
242 |
/** TypedArray (float64) */ |
|
243 |
@Property(name = "Float64Array", attributes = Attribute.NOT_ENUMERABLE) |
|
244 |
public volatile Object float64Array; |
|
245 |
||
246 |
/** Nashorn extension: Java access - global.Packages */ |
|
247 |
@Property(name = "Packages", attributes = Attribute.NOT_ENUMERABLE) |
|
248 |
public volatile Object packages; |
|
249 |
||
250 |
/** Nashorn extension: Java access - global.java */ |
|
251 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
252 |
public volatile Object java; |
|
253 |
||
254 |
/** Nashorn extension: Java access - global.javax */ |
|
255 |
@Property(attributes = Attribute.NOT_ENUMERABLE) |
|
256 |
public Object javax; |
|
257 |
||
258 |
/** Nashorn extension: Java access - global.javaImporter */ |
|
259 |
@Property(name = "JavaImporter", attributes = Attribute.NOT_ENUMERABLE) |
|
260 |
public volatile Object javaImporter; |
|
261 |
||
262 |
/** Nashorn extension: global.Java Object constructor. */ |
|
263 |
@Property(name = "Java", attributes = Attribute.NOT_ENUMERABLE) |
|
264 |
public volatile Object javaApi; |
|
265 |
||
266 |
/** Nashorn extension: current script's file name */ |
|
267 |
@Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT) |
|
268 |
public Object __FILE__; |
|
269 |
||
270 |
/** Nashorn extension: current script's directory */ |
|
271 |
@Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT) |
|
272 |
public Object __DIR__; |
|
273 |
||
274 |
/** Nashorn extension: current source line number being executed */ |
|
275 |
@Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT) |
|
276 |
public Object __LINE__; |
|
277 |
||
278 |
/** Used as Date.prototype's default value */ |
|
279 |
public NativeDate DEFAULT_DATE; |
|
280 |
||
281 |
/** Used as RegExp.prototype's default value */ |
|
282 |
public NativeRegExp DEFAULT_REGEXP; |
|
283 |
||
284 |
/* |
|
285 |
* Built-in constructor objects: Even if user changes dynamic values of |
|
286 |
* "Object", "Array" etc., we still want to keep original values of these |
|
287 |
* constructors here. For example, we need to be able to create array, |
|
288 |
* regexp literals even after user overwrites global "Array" or "RegExp" |
|
289 |
* constructor - see also ECMA 262 spec. Annex D. |
|
290 |
*/ |
|
291 |
private ScriptFunction builtinFunction; |
|
292 |
private ScriptFunction builtinObject; |
|
293 |
private ScriptFunction builtinArray; |
|
294 |
private ScriptFunction builtinBoolean; |
|
295 |
private ScriptFunction builtinDate; |
|
296 |
private ScriptObject builtinJSON; |
|
297 |
private ScriptFunction builtinJSAdapter; |
|
298 |
private ScriptObject builtinMath; |
|
299 |
private ScriptFunction builtinNumber; |
|
300 |
private ScriptFunction builtinRegExp; |
|
301 |
private ScriptFunction builtinString; |
|
302 |
private ScriptFunction builtinError; |
|
303 |
private ScriptFunction builtinEval; |
|
304 |
private ScriptFunction builtinEvalError; |
|
305 |
private ScriptFunction builtinRangeError; |
|
306 |
private ScriptFunction builtinReferenceError; |
|
307 |
private ScriptFunction builtinSyntaxError; |
|
308 |
private ScriptFunction builtinTypeError; |
|
309 |
private ScriptFunction builtinURIError; |
|
310 |
private ScriptObject builtinPackages; |
|
311 |
private ScriptObject builtinJava; |
|
312 |
private ScriptObject builtinJavax; |
|
313 |
private ScriptObject builtinJavaImporter; |
|
314 |
private ScriptObject builtinJavaApi; |
|
315 |
private ScriptObject builtinArrayBuffer; |
|
316 |
private ScriptObject builtinInt8Array; |
|
317 |
private ScriptObject builtinUint8Array; |
|
318 |
private ScriptObject builtinUint8ClampedArray; |
|
319 |
private ScriptObject builtinInt16Array; |
|
320 |
private ScriptObject builtinUint16Array; |
|
321 |
private ScriptObject builtinInt32Array; |
|
322 |
private ScriptObject builtinUint32Array; |
|
323 |
private ScriptObject builtinFloat32Array; |
|
324 |
private ScriptObject builtinFloat64Array; |
|
325 |
||
326 |
// Flag to indicate that a split method issued a return statement |
|
327 |
private int splitState = -1; |
|
328 |
||
329 |
// class cache |
|
330 |
private ClassCache classCache; |
|
331 |
||
332 |
private static final MethodHandle EVAL = findOwnMH("eval", Object.class, Object.class, Object.class); |
|
333 |
private static final MethodHandle PRINT = findOwnMH("print", Object.class, Object.class, Object[].class); |
|
334 |
private static final MethodHandle PRINTLN = findOwnMH("println", Object.class, Object.class, Object[].class); |
|
335 |
private static final MethodHandle LOAD = findOwnMH("load", Object.class, Object.class, Object.class); |
|
336 |
||
337 |
/** |
|
338 |
* Constructor |
|
339 |
*/ |
|
340 |
public Global() { |
|
341 |
this.setIsScope(); |
|
342 |
/* |
|
343 |
* Duplicate global's map and use it. This way the initial Map filled |
|
344 |
* by nasgen (referenced from static field in this class) is retained |
|
345 |
* 'as is'. This allows multiple globals to be used within a context. |
|
346 |
*/ |
|
347 |
this.setMap(getMap().duplicate()); |
|
348 |
||
349 |
final int cacheSize = getContext()._class_cache_size; |
|
350 |
if (cacheSize > 0) { |
|
351 |
classCache = new ClassCache(cacheSize); |
|
352 |
} |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Script access to unique context specific Global instance |
|
357 |
* |
|
358 |
* @return the global singleton |
|
359 |
*/ |
|
360 |
public static Global instance() { |
|
361 |
return (Global)Context.getGlobal(); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Script access to {@link Context} |
|
366 |
* |
|
367 |
* @return the context |
|
368 |
*/ |
|
16185 | 369 |
static Context getThisContext() { |
16147 | 370 |
return instance().getContext(); |
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Script access check for strict mode |
|
375 |
* |
|
376 |
* @return true if strict mode enabled in {@link Global#getThisContext()} |
|
377 |
*/ |
|
16185 | 378 |
static boolean isStrict() { |
16147 | 379 |
return getThisContext()._strict; |
380 |
} |
|
381 |
||
382 |
// GlobalObject interface implementation |
|
383 |
||
384 |
@Override |
|
385 |
public void initBuiltinObjects() { |
|
386 |
if (this.builtinObject != null) { |
|
387 |
// already initialized, just return |
|
388 |
return; |
|
389 |
} |
|
390 |
||
16185 | 391 |
init(); |
16147 | 392 |
} |
393 |
||
394 |
@Override |
|
395 |
public ScriptFunction newScriptFunction(final String name, final MethodHandle handle, final ScriptObject scope, final boolean strict) { |
|
396 |
return new ScriptFunctionImpl(name, handle, scope, strict, null); |
|
397 |
} |
|
398 |
||
399 |
@Override |
|
400 |
public Object wrapAsObject(final Object obj) { |
|
401 |
if (obj instanceof Boolean) { |
|
402 |
return new NativeBoolean((Boolean)obj); |
|
403 |
} else if (obj instanceof Number) { |
|
404 |
return new NativeNumber(((Number)obj).doubleValue()); |
|
405 |
} else if (obj instanceof String || obj instanceof ConsString) { |
|
406 |
return new NativeString((CharSequence)obj); |
|
407 |
} else if (obj instanceof Object[]) { // extension |
|
408 |
return new NativeArray((Object[])obj); |
|
409 |
} else if (obj instanceof double[]) { // extension |
|
410 |
return new NativeArray((double[])obj); |
|
411 |
} else if (obj instanceof long[]) { |
|
412 |
return new NativeArray((long[])obj); |
|
413 |
} else if (obj instanceof int[]) { |
|
414 |
return new NativeArray((int[])obj); |
|
415 |
} else { |
|
416 |
// FIXME: more special cases? Map? List? |
|
417 |
return obj; |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
@Override |
|
16186
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
422 |
public MethodHandle getWrapFilter(Object obj) { |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
423 |
if (obj instanceof String || obj instanceof ConsString) { |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
424 |
return NativeString.WRAPFILTER; |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
425 |
} else if (obj instanceof Number) { |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
426 |
return NativeNumber.WRAPFILTER; |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
427 |
} else if (obj instanceof Boolean) { |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
428 |
return NativeBoolean.WRAPFILTER; |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
429 |
} |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
430 |
throw new IllegalArgumentException("Unsupported primitive value: " + obj); |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
431 |
} |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
432 |
|
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
433 |
@Override |
16147 | 434 |
public GuardedInvocation numberLookup(final NashornCallSiteDescriptor callSite, final Number self) { |
435 |
return NativeNumber.lookupPrimitive(callSite, self); |
|
436 |
} |
|
437 |
||
438 |
@Override |
|
439 |
public GuardedInvocation stringLookup(final NashornCallSiteDescriptor callSite, final CharSequence self) { |
|
440 |
return NativeString.lookupPrimitive(callSite, self); |
|
441 |
} |
|
442 |
||
443 |
@Override |
|
444 |
public GuardedInvocation booleanLookup(final NashornCallSiteDescriptor callSite, final Boolean self) { |
|
445 |
return NativeBoolean.lookupPrimitive(callSite, self); |
|
446 |
} |
|
447 |
||
448 |
@Override |
|
449 |
public ScriptObject newObject() { |
|
450 |
return newEmptyInstance(); |
|
451 |
} |
|
452 |
||
453 |
@Override |
|
454 |
public Object getDefaultValue(final ScriptObject sobj, final Class<?> typeHint) { |
|
455 |
// When the [[DefaultValue]] internal method of O is called with no hint, |
|
456 |
// then it behaves as if the hint were Number, unless O is a Date object |
|
457 |
// in which case it behaves as if the hint were String. |
|
458 |
Class<?> hint = typeHint; |
|
459 |
if (hint == null) { |
|
460 |
hint = Number.class; |
|
461 |
} |
|
462 |
||
463 |
try { |
|
464 |
if (hint == String.class) { |
|
465 |
||
466 |
final Object toString = TO_STRING.getGetter().invokeExact(sobj); |
|
467 |
if (toString instanceof ScriptFunction) { |
|
468 |
final Object value = TO_STRING.getInvoker().invokeExact(toString, sobj); |
|
469 |
if (JSType.isPrimitive(value)) { |
|
470 |
return value; |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
final Object valueOf = VALUE_OF.getGetter().invokeExact(sobj); |
|
475 |
if (valueOf instanceof ScriptFunction) { |
|
476 |
final Object value = VALUE_OF.getInvoker().invokeExact(valueOf, sobj); |
|
477 |
if (JSType.isPrimitive(value)) { |
|
478 |
return value; |
|
479 |
} |
|
480 |
} |
|
481 |
typeError(this, "cannot.get.default.string"); |
|
482 |
} |
|
483 |
||
484 |
if (hint == Number.class) { |
|
485 |
final Object valueOf = VALUE_OF.getGetter().invokeExact(sobj); |
|
486 |
if (valueOf instanceof ScriptFunction) { |
|
487 |
final Object value = VALUE_OF.getInvoker().invokeExact(valueOf, sobj); |
|
488 |
if (JSType.isPrimitive(value)) { |
|
489 |
return value; |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
final Object toString = TO_STRING.getGetter().invokeExact(sobj); |
|
494 |
if (toString instanceof ScriptFunction) { |
|
495 |
final Object value = TO_STRING.getInvoker().invokeExact(toString, sobj); |
|
496 |
if (JSType.isPrimitive(value)) { |
|
497 |
return value; |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
typeError(this, "cannot.get.default.number"); |
|
502 |
} |
|
503 |
} catch (final RuntimeException | Error e) { |
|
504 |
throw e; |
|
505 |
} catch (final Throwable t) { |
|
506 |
throw new RuntimeException(t); |
|
507 |
} |
|
508 |
||
509 |
return UNDEFINED; |
|
510 |
} |
|
511 |
||
512 |
@Override |
|
513 |
public boolean isError(final ScriptObject sobj) { |
|
514 |
final ScriptObject errorProto = getErrorPrototype(); |
|
515 |
ScriptObject proto = sobj.getProto(); |
|
516 |
while (proto != null) { |
|
517 |
if (proto == errorProto) { |
|
518 |
return true; |
|
519 |
} |
|
520 |
proto = proto.getProto(); |
|
521 |
} |
|
522 |
return false; |
|
523 |
} |
|
524 |
||
525 |
@Override |
|
526 |
public ScriptObject newError(final String msg) { |
|
527 |
return new NativeError(msg); |
|
528 |
} |
|
529 |
||
530 |
@Override |
|
531 |
public ScriptObject newEvalError(final String msg) { |
|
532 |
return new NativeEvalError(msg); |
|
533 |
} |
|
534 |
||
535 |
@Override |
|
536 |
public ScriptObject newRangeError(final String msg) { |
|
537 |
return new NativeRangeError(msg); |
|
538 |
} |
|
539 |
||
540 |
@Override |
|
541 |
public ScriptObject newReferenceError(final String msg) { |
|
542 |
return new NativeReferenceError(msg); |
|
543 |
} |
|
544 |
||
545 |
@Override |
|
546 |
public ScriptObject newSyntaxError(final String msg) { |
|
547 |
return new NativeSyntaxError(msg); |
|
548 |
} |
|
549 |
||
550 |
@Override |
|
551 |
public ScriptObject newTypeError(final String msg) { |
|
552 |
return new NativeTypeError(msg); |
|
553 |
} |
|
554 |
||
555 |
@Override |
|
556 |
public ScriptObject newURIError(final String msg) { |
|
557 |
return new NativeURIError(msg); |
|
558 |
} |
|
559 |
||
560 |
@Override |
|
561 |
public PropertyDescriptor newGenericDescriptor(final boolean configurable, final boolean enumerable) { |
|
562 |
return new GenericPropertyDescriptor(configurable, enumerable); |
|
563 |
} |
|
564 |
||
565 |
@Override |
|
566 |
public PropertyDescriptor newDataDescriptor(final Object value, final boolean configurable, final boolean enumerable, final boolean writable) { |
|
567 |
return new DataPropertyDescriptor(configurable, enumerable, writable, value); |
|
568 |
} |
|
569 |
||
570 |
@Override |
|
571 |
public PropertyDescriptor newAccessorDescriptor(final Object get, final Object set, final boolean configurable, final boolean enumerable) { |
|
572 |
final AccessorPropertyDescriptor desc = new AccessorPropertyDescriptor(configurable, enumerable, get == null ? UNDEFINED : get, set == null ? UNDEFINED : set); |
|
573 |
||
574 |
final boolean strict = getContext()._strict; |
|
575 |
||
576 |
if (get == null) { |
|
577 |
desc.delete(PropertyDescriptor.GET, strict); |
|
578 |
} |
|
579 |
||
580 |
if (set == null) { |
|
581 |
desc.delete(PropertyDescriptor.SET, strict); |
|
582 |
} |
|
583 |
||
584 |
return desc; |
|
585 |
} |
|
586 |
||
587 |
||
588 |
/** |
|
589 |
* Cache for compiled script classes. |
|
590 |
*/ |
|
591 |
@SuppressWarnings("serial") |
|
592 |
private static class ClassCache extends LinkedHashMap<Source, SoftReference<Class<?>>> { |
|
593 |
private final int size; |
|
594 |
||
595 |
ClassCache(int size) { |
|
596 |
super(size, 0.75f, true); |
|
597 |
this.size = size; |
|
598 |
} |
|
599 |
||
600 |
@Override |
|
601 |
protected boolean removeEldestEntry(final Map.Entry<Source, SoftReference<Class<?>>> eldest) { |
|
602 |
return size() >= size; |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
// Class cache management |
|
607 |
@Override |
|
608 |
public Class<?> findCachedClass(final Source source) { |
|
609 |
assert classCache != null : "Class cache used without being initialized"; |
|
610 |
SoftReference<Class<?>> ref = classCache.get(source); |
|
611 |
if (ref != null) { |
|
612 |
final Class<?> clazz = ref.get(); |
|
613 |
if (clazz == null) { |
|
614 |
classCache.remove(source); |
|
615 |
} |
|
616 |
return clazz; |
|
617 |
} |
|
618 |
||
619 |
return null; |
|
620 |
} |
|
621 |
||
622 |
@Override |
|
623 |
public void cacheClass(final Source source, final Class<?> clazz) { |
|
624 |
assert classCache != null : "Class cache used without being initialized"; |
|
625 |
classCache.put(source, new SoftReference<Class<?>>(clazz)); |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* This is the eval used when 'indirect' eval call is made. |
|
630 |
* |
|
631 |
* var global = this; |
|
632 |
* global.eval("print('hello')"); |
|
633 |
* |
|
634 |
* @param self eval scope |
|
635 |
* @param str eval string |
|
636 |
* |
|
637 |
* @return the result of eval |
|
638 |
*/ |
|
639 |
public static Object eval(final Object self, final Object str) { |
|
640 |
return directEval(self, str, UNDEFINED, UNDEFINED, UNDEFINED); |
|
641 |
} |
|
642 |
||
643 |
/** |
|
644 |
* Direct eval |
|
645 |
* |
|
646 |
* @param self The scope of eval passed as 'self' |
|
647 |
* @param str Evaluated code |
|
648 |
* @param callThis "this" to be passed to the evaluated code |
|
649 |
* @param location location of the eval call |
|
650 |
* @param strict is eval called a strict mode code? |
|
651 |
* |
|
652 |
* @return the return value of the eval |
|
653 |
* |
|
654 |
* This is directly invoked from generated when eval(code) is called in user code |
|
655 |
*/ |
|
656 |
public static Object directEval(final Object self, final Object str, final Object callThis, final Object location, final Object strict) { |
|
657 |
if (!(str instanceof String || str instanceof ConsString)) { |
|
658 |
return str; |
|
659 |
} |
|
16185 | 660 |
final Global global = Global.instance(); |
661 |
final ScriptObject scope = (self instanceof ScriptObject) ? (ScriptObject)self : global; |
|
16147 | 662 |
|
16185 | 663 |
return global.getContext().eval(scope, str.toString(), callThis, location, Boolean.TRUE.equals(strict)); |
16147 | 664 |
} |
665 |
||
666 |
/** |
|
667 |
* Global print implementation - Nashorn extension |
|
668 |
* |
|
669 |
* @param self scope |
|
670 |
* @param objects arguments to print |
|
671 |
* |
|
672 |
* @return result of print (undefined) |
|
673 |
*/ |
|
674 |
public static Object print(final Object self, final Object... objects) { |
|
675 |
return printImpl(false, objects); |
|
676 |
} |
|
677 |
||
678 |
/** |
|
679 |
* Global println implementation - Nashorn extension |
|
680 |
* |
|
681 |
* @param self scope |
|
682 |
* @param objects arguments to print |
|
683 |
* |
|
684 |
* @return result of println (undefined) |
|
685 |
*/ |
|
686 |
public static Object println(final Object self, final Object... objects) { |
|
687 |
return printImpl(true, objects); |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Global load implementation - Nashorn extension |
|
692 |
* |
|
693 |
* @param self scope |
|
694 |
* @param source source to load |
|
695 |
* |
|
696 |
* @return result of load (undefined) |
|
697 |
* |
|
698 |
* @throws IOException if source could not be read |
|
699 |
*/ |
|
700 |
public static Object load(final Object self, final Object source) throws IOException { |
|
16185 | 701 |
final Global global = Global.instance(); |
702 |
final ScriptObject scope = (self instanceof ScriptObject) ? (ScriptObject)self : global; |
|
703 |
return global.getContext().load(scope, source); |
|
16147 | 704 |
} |
705 |
||
706 |
ScriptObject getFunctionPrototype() { |
|
707 |
return ScriptFunction.getPrototype(builtinFunction); |
|
708 |
} |
|
709 |
||
710 |
ScriptObject getObjectPrototype() { |
|
711 |
return ScriptFunction.getPrototype(builtinObject); |
|
712 |
} |
|
713 |
||
714 |
ScriptObject getArrayPrototype() { |
|
715 |
return ScriptFunction.getPrototype(builtinArray); |
|
716 |
} |
|
717 |
||
718 |
ScriptObject getBooleanPrototype() { |
|
719 |
return ScriptFunction.getPrototype(builtinBoolean); |
|
720 |
} |
|
721 |
||
722 |
ScriptObject getNumberPrototype() { |
|
723 |
return ScriptFunction.getPrototype(builtinNumber); |
|
724 |
} |
|
725 |
||
726 |
ScriptObject getDatePrototype() { |
|
727 |
return ScriptFunction.getPrototype(builtinDate); |
|
728 |
} |
|
729 |
||
730 |
ScriptObject getRegExpPrototype() { |
|
731 |
return ScriptFunction.getPrototype(builtinRegExp); |
|
732 |
} |
|
733 |
||
734 |
ScriptObject getStringPrototype() { |
|
735 |
return ScriptFunction.getPrototype(builtinString); |
|
736 |
} |
|
737 |
||
738 |
ScriptObject getErrorPrototype() { |
|
739 |
return ScriptFunction.getPrototype(builtinError); |
|
740 |
} |
|
741 |
||
742 |
ScriptObject getEvalErrorPrototype() { |
|
743 |
return ScriptFunction.getPrototype(builtinEvalError); |
|
744 |
} |
|
745 |
||
746 |
ScriptObject getRangeErrorPrototype() { |
|
747 |
return ScriptFunction.getPrototype(builtinRangeError); |
|
748 |
} |
|
749 |
||
750 |
ScriptObject getReferenceErrorPrototype() { |
|
751 |
return ScriptFunction.getPrototype(builtinReferenceError); |
|
752 |
} |
|
753 |
||
754 |
ScriptObject getSyntaxErrorPrototype() { |
|
755 |
return ScriptFunction.getPrototype(builtinSyntaxError); |
|
756 |
} |
|
757 |
||
758 |
ScriptObject getTypeErrorPrototype() { |
|
759 |
return ScriptFunction.getPrototype(builtinTypeError); |
|
760 |
} |
|
761 |
||
762 |
ScriptObject getURIErrorPrototype() { |
|
763 |
return ScriptFunction.getPrototype(builtinURIError); |
|
764 |
} |
|
765 |
||
766 |
ScriptObject getJavaImporterPrototype() { |
|
767 |
return ScriptFunction.getPrototype(builtinJavaImporter); |
|
768 |
} |
|
769 |
||
770 |
ScriptObject getJSAdapterPrototype() { |
|
771 |
return ScriptFunction.getPrototype(builtinJSAdapter); |
|
772 |
} |
|
773 |
||
774 |
ScriptObject getArrayBufferPrototype() { |
|
775 |
return ScriptFunction.getPrototype(builtinArrayBuffer); |
|
776 |
} |
|
777 |
||
778 |
ScriptObject getInt8ArrayPrototype() { |
|
779 |
return ScriptFunction.getPrototype(builtinInt8Array); |
|
780 |
} |
|
781 |
||
782 |
ScriptObject getUint8ArrayPrototype() { |
|
783 |
return ScriptFunction.getPrototype(builtinUint8Array); |
|
784 |
} |
|
785 |
||
786 |
ScriptObject getUint8ClampedArrayPrototype() { |
|
787 |
return ScriptFunction.getPrototype(builtinUint8ClampedArray); |
|
788 |
} |
|
789 |
||
790 |
ScriptObject getInt16ArrayPrototype() { |
|
791 |
return ScriptFunction.getPrototype(builtinInt16Array); |
|
792 |
} |
|
793 |
||
794 |
ScriptObject getUint16ArrayPrototype() { |
|
795 |
return ScriptFunction.getPrototype(builtinUint16Array); |
|
796 |
} |
|
797 |
||
798 |
ScriptObject getInt32ArrayPrototype() { |
|
799 |
return ScriptFunction.getPrototype(builtinInt32Array); |
|
800 |
} |
|
801 |
||
802 |
ScriptObject getUint32ArrayPrototype() { |
|
803 |
return ScriptFunction.getPrototype(builtinUint32Array); |
|
804 |
} |
|
805 |
||
806 |
ScriptObject getFloat32ArrayPrototype() { |
|
807 |
return ScriptFunction.getPrototype(builtinFloat32Array); |
|
808 |
} |
|
809 |
||
810 |
ScriptObject getFloat64ArrayPrototype() { |
|
811 |
return ScriptFunction.getPrototype(builtinFloat64Array); |
|
812 |
} |
|
813 |
||
814 |
private ScriptFunction getBuiltinArray() { |
|
815 |
return builtinArray; |
|
816 |
} |
|
817 |
||
818 |
||
819 |
/** |
|
820 |
* Called from compiled script code to test if builtin has been overridden |
|
821 |
* |
|
822 |
* @return true if builtin array has not been overridden |
|
823 |
*/ |
|
824 |
public static boolean isBuiltinArray() { |
|
825 |
final Global instance = Global.instance(); |
|
826 |
return instance.array == instance.getBuiltinArray(); |
|
827 |
} |
|
828 |
||
829 |
private ScriptFunction getBuiltinBoolean() { |
|
830 |
return builtinBoolean; |
|
831 |
} |
|
832 |
||
833 |
/** |
|
834 |
* Called from compiled script code to test if builtin has been overridden |
|
835 |
* |
|
836 |
* @return true if builtin boolean has not been overridden |
|
837 |
*/ |
|
838 |
public static boolean isBuiltinBoolean() { |
|
839 |
final Global instance = Global.instance(); |
|
840 |
return instance._boolean == instance.getBuiltinBoolean(); |
|
841 |
} |
|
842 |
||
843 |
private ScriptFunction getBuiltinDate() { |
|
844 |
return builtinDate; |
|
845 |
} |
|
846 |
||
847 |
/** |
|
848 |
* Called from compiled script code to test if builtin has been overridden |
|
849 |
* |
|
850 |
* @return true if builtin date has not been overridden |
|
851 |
*/ |
|
852 |
public static boolean isBuiltinDate() { |
|
853 |
final Global instance = Global.instance(); |
|
854 |
return instance.date == instance.getBuiltinDate(); |
|
855 |
} |
|
856 |
||
857 |
private ScriptFunction getBuiltinError() { |
|
858 |
return builtinError; |
|
859 |
} |
|
860 |
||
861 |
/** |
|
862 |
* Called from compiled script code to test if builtin has been overridden |
|
863 |
* |
|
864 |
* @return true if builtin error has not been overridden |
|
865 |
*/ |
|
866 |
public static boolean isBuiltinError() { |
|
867 |
final Global instance = Global.instance(); |
|
868 |
return instance.error == instance.getBuiltinError(); |
|
869 |
} |
|
870 |
||
871 |
private ScriptFunction getBuiltinEvalError() { |
|
872 |
return builtinEvalError; |
|
873 |
} |
|
874 |
||
875 |
/** |
|
876 |
* Called from compiled script code to test if builtin has been overridden |
|
877 |
* |
|
878 |
* @return true if builtin eval error has not been overridden |
|
879 |
*/ |
|
880 |
public static boolean isBuiltinEvalError() { |
|
881 |
final Global instance = Global.instance(); |
|
882 |
return instance.evalError == instance.getBuiltinEvalError(); |
|
883 |
} |
|
884 |
||
885 |
private ScriptFunction getBuiltinFunction() { |
|
886 |
return builtinFunction; |
|
887 |
} |
|
888 |
||
889 |
/** |
|
890 |
* Called from compiled script code to test if builtin has been overridden |
|
891 |
* |
|
892 |
* @return true if builtin function has not been overridden |
|
893 |
*/ |
|
894 |
public static boolean isBuiltinFunction() { |
|
895 |
final Global instance = Global.instance(); |
|
896 |
return instance.function == instance.getBuiltinFunction(); |
|
897 |
} |
|
898 |
||
899 |
private ScriptFunction getBuiltinJSAdapter() { |
|
900 |
return builtinJSAdapter; |
|
901 |
} |
|
902 |
||
903 |
/** |
|
904 |
* Called from compiled script code to test if builtin has been overridden |
|
905 |
* |
|
906 |
* @return true if builtin JSAdapter has not been overridden |
|
907 |
*/ |
|
908 |
public static boolean isBuiltinJSAdapter() { |
|
909 |
final Global instance = Global.instance(); |
|
910 |
return instance.jsadapter == instance.getBuiltinJSAdapter(); |
|
911 |
} |
|
912 |
||
913 |
private ScriptObject getBuiltinJSON() { |
|
914 |
return builtinJSON; |
|
915 |
} |
|
916 |
||
917 |
/** |
|
918 |
* Called from compiled script code to test if builtin has been overridden |
|
919 |
* |
|
920 |
* @return true if builtin JSON has has not been overridden |
|
921 |
*/ |
|
922 |
public static boolean isBuiltinJSON() { |
|
923 |
final Global instance = Global.instance(); |
|
924 |
return instance.json == instance.getBuiltinJSON(); |
|
925 |
} |
|
926 |
||
927 |
private ScriptObject getBuiltinJava() { |
|
928 |
return builtinJava; |
|
929 |
} |
|
930 |
||
931 |
/** |
|
932 |
* Called from compiled script code to test if builtin has been overridden |
|
933 |
* |
|
934 |
* @return true if builtin Java has not been overridden |
|
935 |
*/ |
|
936 |
public static boolean isBuiltinJava() { |
|
937 |
final Global instance = Global.instance(); |
|
938 |
return instance.java == instance.getBuiltinJava(); |
|
939 |
} |
|
940 |
||
941 |
private ScriptObject getBuiltinJavax() { |
|
942 |
return builtinJavax; |
|
943 |
} |
|
944 |
||
945 |
/** |
|
946 |
* Called from compiled script code to test if builtin has been overridden |
|
947 |
* |
|
948 |
* @return true if builtin Javax has not been overridden |
|
949 |
*/ |
|
950 |
public static boolean isBuiltinJavax() { |
|
951 |
final Global instance = Global.instance(); |
|
952 |
return instance.javax == instance.getBuiltinJavax(); |
|
953 |
} |
|
954 |
||
955 |
private ScriptObject getBuiltinJavaImporter() { |
|
956 |
return builtinJavaImporter; |
|
957 |
} |
|
958 |
||
959 |
/** |
|
960 |
* Called from compiled script code to test if builtin has been overridden |
|
961 |
* |
|
962 |
* @return true if builtin Java importer has not been overridden |
|
963 |
*/ |
|
964 |
public static boolean isBuiltinJavaImporter() { |
|
965 |
final Global instance = Global.instance(); |
|
966 |
return instance.javaImporter == instance.getBuiltinJavaImporter(); |
|
967 |
} |
|
968 |
||
969 |
private ScriptObject getBuiltinMath() { |
|
970 |
return builtinMath; |
|
971 |
} |
|
972 |
||
973 |
/** |
|
974 |
* Called from compiled script code to test if builtin has been overridden |
|
975 |
* |
|
976 |
* @return true if builtin math has not been overridden |
|
977 |
*/ |
|
978 |
public static boolean isBuiltinMath() { |
|
979 |
final Global instance = Global.instance(); |
|
980 |
return instance.math == instance.getBuiltinMath(); |
|
981 |
} |
|
982 |
||
983 |
private ScriptFunction getBuiltinNumber() { |
|
984 |
return builtinNumber; |
|
985 |
} |
|
986 |
||
987 |
/** |
|
988 |
* Called from compiled script code to test if builtin has been overridden |
|
989 |
* |
|
990 |
* @return true if builtin number has not been overridden |
|
991 |
*/ |
|
992 |
public static boolean isBuiltinNumber() { |
|
993 |
final Global instance = Global.instance(); |
|
994 |
return instance.number == instance.getBuiltinNumber(); |
|
995 |
} |
|
996 |
||
997 |
private ScriptFunction getBuiltinObject() { |
|
998 |
return builtinObject; |
|
999 |
} |
|
1000 |
||
1001 |
/** |
|
1002 |
* Called from compiled script code to test if builtin has been overridden |
|
1003 |
* |
|
1004 |
* @return true if builtin object has not been overridden |
|
1005 |
*/ |
|
1006 |
public static boolean isBuiltinObject() { |
|
1007 |
final Global instance = Global.instance(); |
|
1008 |
return instance.object == instance.getBuiltinObject(); |
|
1009 |
} |
|
1010 |
||
1011 |
private ScriptObject getBuiltinPackages() { |
|
1012 |
return builtinPackages; |
|
1013 |
} |
|
1014 |
||
1015 |
/** |
|
1016 |
* Called from compiled script code to test if builtin has been overridden |
|
1017 |
* |
|
1018 |
* @return true if builtin package has not been overridden |
|
1019 |
*/ |
|
1020 |
public static boolean isBuiltinPackages() { |
|
1021 |
final Global instance = Global.instance(); |
|
1022 |
return instance.packages == instance.getBuiltinPackages(); |
|
1023 |
} |
|
1024 |
||
1025 |
private ScriptFunction getBuiltinRangeError() { |
|
1026 |
return builtinRangeError; |
|
1027 |
} |
|
1028 |
||
1029 |
/** |
|
1030 |
* Called from compiled script code to test if builtin has been overridden |
|
1031 |
* |
|
1032 |
* @return true if builtin range error has not been overridden |
|
1033 |
*/ |
|
1034 |
public static boolean isBuiltinRangeError() { |
|
1035 |
final Global instance = Global.instance(); |
|
1036 |
return instance.rangeError == instance.getBuiltinRangeError(); |
|
1037 |
} |
|
1038 |
||
1039 |
private ScriptFunction getBuiltinReferenceError() { |
|
1040 |
return builtinReferenceError; |
|
1041 |
} |
|
1042 |
||
1043 |
/** |
|
1044 |
* Called from compiled script code to test if builtin has been overridden |
|
1045 |
* |
|
1046 |
* @return true if builtin reference error has not been overridden |
|
1047 |
*/ |
|
1048 |
public static boolean isBuiltinReferenceError() { |
|
1049 |
final Global instance = Global.instance(); |
|
1050 |
return instance.referenceError == instance.getBuiltinReferenceError(); |
|
1051 |
} |
|
1052 |
||
1053 |
private ScriptFunction getBuiltinRegExp() { |
|
1054 |
return builtinRegExp; |
|
1055 |
} |
|
1056 |
||
1057 |
/** |
|
1058 |
* Called from compiled script code to test if builtin has been overridden |
|
1059 |
* |
|
1060 |
* @return true if builtin regexp has not been overridden |
|
1061 |
*/ |
|
1062 |
public static boolean isBuiltinRegExp() { |
|
1063 |
final Global instance = Global.instance(); |
|
1064 |
return instance.regexp == instance.getBuiltinRegExp(); |
|
1065 |
} |
|
1066 |
||
1067 |
private ScriptFunction getBuiltinString() { |
|
1068 |
return builtinString; |
|
1069 |
} |
|
1070 |
||
1071 |
/** |
|
1072 |
* Called from compiled script code to test if builtin has been overridden |
|
1073 |
* |
|
1074 |
* @return true if builtin Java has not been overridden |
|
1075 |
*/ |
|
1076 |
public static boolean isBuiltinString() { |
|
1077 |
final Global instance = Global.instance(); |
|
1078 |
return instance.string == instance.getBuiltinString(); |
|
1079 |
} |
|
1080 |
||
1081 |
private ScriptFunction getBuiltinSyntaxError() { |
|
1082 |
return builtinSyntaxError; |
|
1083 |
} |
|
1084 |
||
1085 |
/** |
|
1086 |
* Called from compiled script code to test if builtin has been overridden |
|
1087 |
* |
|
1088 |
* @return true if builtin syntax error has not been overridden |
|
1089 |
*/ |
|
1090 |
public static boolean isBuiltinSyntaxError() { |
|
1091 |
final Global instance = Global.instance(); |
|
1092 |
return instance.syntaxError == instance.getBuiltinSyntaxError(); |
|
1093 |
} |
|
1094 |
||
1095 |
private ScriptFunction getBuiltinTypeError() { |
|
1096 |
return builtinTypeError; |
|
1097 |
} |
|
1098 |
||
1099 |
/** |
|
1100 |
* Called from compiled script code to test if builtin has been overridden |
|
1101 |
* |
|
1102 |
* @return true if builtin type error has not been overridden |
|
1103 |
*/ |
|
1104 |
public static boolean isBuiltinTypeError() { |
|
1105 |
final Global instance = Global.instance(); |
|
1106 |
return instance.typeError == instance.getBuiltinTypeError(); |
|
1107 |
} |
|
1108 |
||
1109 |
private ScriptFunction getBuiltinURIError() { |
|
1110 |
return builtinURIError; |
|
1111 |
} |
|
1112 |
||
1113 |
/** |
|
1114 |
* Called from compiled script code to test if builtin has been overridden |
|
1115 |
* |
|
1116 |
* @return true if builtin URI error has not been overridden |
|
1117 |
*/ |
|
1118 |
public static boolean isBuiltinURIError() { |
|
1119 |
final Global instance = Global.instance(); |
|
1120 |
return instance.uriError == instance.getBuiltinURIError(); |
|
1121 |
} |
|
1122 |
||
1123 |
@Override |
|
1124 |
public String getClassName() { |
|
1125 |
return "global"; |
|
1126 |
} |
|
1127 |
||
1128 |
/** |
|
1129 |
* Copy function used to clone NativeRegExp objects. |
|
1130 |
* |
|
1131 |
* @param regexp a NativeRegExp to clone |
|
1132 |
* |
|
1133 |
* @return copy of the given regexp object |
|
1134 |
*/ |
|
1135 |
public static Object regExpCopy(final Object regexp) { |
|
1136 |
return new NativeRegExp((NativeRegExp)regexp); |
|
1137 |
} |
|
1138 |
||
1139 |
/** |
|
1140 |
* Convert given object to NativeRegExp type. |
|
1141 |
* |
|
1142 |
* @param obj object to be converted |
|
1143 |
* @return NativeRegExp instance |
|
1144 |
*/ |
|
1145 |
public static NativeRegExp toRegExp(final Object obj) { |
|
1146 |
if (obj instanceof NativeRegExp) { |
|
1147 |
return (NativeRegExp)obj; |
|
1148 |
} |
|
1149 |
return new NativeRegExp(JSType.toString(obj)); |
|
1150 |
} |
|
1151 |
||
1152 |
/** |
|
1153 |
* ECMA 9.9 ToObject implementation |
|
1154 |
* |
|
1155 |
* @param obj an item for which to run ToObject |
|
1156 |
* @return ToObject version of given item |
|
1157 |
*/ |
|
1158 |
public static Object toObject(final Object obj) { |
|
1159 |
if (obj == null || obj == UNDEFINED) { |
|
16188 | 1160 |
typeError("not.an.object", ScriptRuntime.safeToString(obj)); |
16147 | 1161 |
} |
1162 |
||
1163 |
if (obj instanceof ScriptObject) { |
|
1164 |
return obj; |
|
1165 |
} |
|
1166 |
||
1167 |
return instance().wrapAsObject(obj); |
|
1168 |
} |
|
1169 |
||
1170 |
/** |
|
1171 |
* Allocate a new object array. |
|
1172 |
* |
|
1173 |
* @param initial object values. |
|
1174 |
* @return the new array |
|
1175 |
*/ |
|
1176 |
public static Object allocate(final Object[] initial) { |
|
1177 |
return new NativeArray(initial); |
|
1178 |
} |
|
1179 |
||
1180 |
/** |
|
1181 |
* Allocate a new number array. |
|
1182 |
* |
|
1183 |
* @param initial number values. |
|
1184 |
* @return the new array |
|
1185 |
*/ |
|
1186 |
public static Object allocate(final double[] initial) { |
|
1187 |
return new NativeArray(initial); |
|
1188 |
} |
|
1189 |
||
1190 |
/** |
|
1191 |
* Allocate a new long array. |
|
1192 |
* |
|
1193 |
* @param initial number values. |
|
1194 |
* @return the new array |
|
1195 |
*/ |
|
1196 |
public static Object allocate(final long[] initial) { |
|
1197 |
return new NativeArray(initial); |
|
1198 |
} |
|
1199 |
||
1200 |
/** |
|
1201 |
* Allocate a new integer array. |
|
1202 |
* |
|
1203 |
* @param initial number values. |
|
1204 |
* @return the new array |
|
1205 |
*/ |
|
1206 |
public static Object allocate(final int[] initial) { |
|
1207 |
return new NativeArray(initial); |
|
1208 |
} |
|
1209 |
||
1210 |
/** |
|
1211 |
* Allocate a new object array for arguments. |
|
1212 |
* |
|
1213 |
* @param arguments initial arguments passed. |
|
1214 |
* @param callee reference to the function that uses arguments object |
|
1215 |
* @param numParams actual number of declared parameters |
|
1216 |
* |
|
1217 |
* @return the new array |
|
1218 |
*/ |
|
1219 |
public static Object allocateArguments(final Object[] arguments, final Object callee, final int numParams) { |
|
1220 |
return NativeArguments.allocate(arguments, (ScriptFunction)callee, numParams); |
|
1221 |
} |
|
1222 |
||
1223 |
/** |
|
1224 |
* Called from generated to check if given function is the builtin 'eval'. If |
|
1225 |
* eval is used in a script, a lot of optimizations and assumptions cannot be done. |
|
1226 |
* |
|
1227 |
* @param fn function object that is checked |
|
1228 |
* @return true if fn is the builtin eval |
|
1229 |
*/ |
|
1230 |
public static boolean isEval(final Object fn) { |
|
1231 |
return fn == Global.instance().builtinEval; |
|
1232 |
} |
|
1233 |
||
1234 |
/** |
|
1235 |
* Create a new RegExp object. |
|
1236 |
* |
|
1237 |
* @param expression Regular expression. |
|
1238 |
* @param options Search options. |
|
1239 |
* |
|
1240 |
* @return New RegExp object. |
|
1241 |
*/ |
|
1242 |
public static Object newRegExp(final String expression, final String options) { |
|
1243 |
if (options == null) { |
|
1244 |
return new NativeRegExp(expression); |
|
1245 |
} |
|
1246 |
return new NativeRegExp(expression, options); |
|
1247 |
} |
|
1248 |
||
1249 |
/** |
|
1250 |
* Get the object prototype |
|
1251 |
* |
|
1252 |
* @return the object prototype |
|
1253 |
*/ |
|
1254 |
public static ScriptObject objectPrototype() { |
|
1255 |
return Global.instance().getObjectPrototype(); |
|
1256 |
} |
|
1257 |
||
1258 |
/** |
|
1259 |
* Create a new empty object instance. |
|
1260 |
* |
|
1261 |
* @return New empty object. |
|
1262 |
*/ |
|
1263 |
public static ScriptObject newEmptyInstance() { |
|
1264 |
final ScriptObject sobj = new JO$(); |
|
1265 |
sobj.setProto(objectPrototype()); |
|
1266 |
return sobj; |
|
1267 |
} |
|
1268 |
||
1269 |
/** |
|
1270 |
* Check if a given object is a ScriptObject, raises an exception if this is |
|
1271 |
* not the case |
|
1272 |
* |
|
1273 |
* @param obj and object to check |
|
1274 |
*/ |
|
1275 |
public static void checkObject(final Object obj) { |
|
1276 |
if (!(obj instanceof ScriptObject)) { |
|
16188 | 1277 |
typeError("not.an.object", ScriptRuntime.safeToString(obj)); |
16147 | 1278 |
} |
1279 |
} |
|
1280 |
||
1281 |
/** |
|
1282 |
* ECMA 9.10 - implementation of CheckObjectCoercible, i.e. raise an exception |
|
1283 |
* if this object is null or undefined. |
|
1284 |
* |
|
1285 |
* @param obj an object to check |
|
1286 |
*/ |
|
1287 |
public static void checkObjectCoercible(final Object obj) { |
|
1288 |
if (obj == null || obj == UNDEFINED) { |
|
16188 | 1289 |
typeError("not.an.object", ScriptRuntime.safeToString(obj)); |
16147 | 1290 |
} |
1291 |
} |
|
1292 |
||
1293 |
/** |
|
1294 |
* Get the current split state. |
|
1295 |
* |
|
1296 |
* @return current split state |
|
1297 |
*/ |
|
1298 |
@Override |
|
1299 |
public int getSplitState() { |
|
1300 |
return splitState; |
|
1301 |
} |
|
1302 |
||
1303 |
/** |
|
1304 |
* Set the current split state. |
|
1305 |
* |
|
1306 |
* @param state current split state |
|
1307 |
*/ |
|
1308 |
@Override |
|
1309 |
public void setSplitState(final int state) { |
|
1310 |
splitState = state; |
|
1311 |
} |
|
1312 |
||
1313 |
private void init() { |
|
16185 | 1314 |
assert Context.getGlobal() == this : "this global is not set as current"; |
1315 |
||
16147 | 1316 |
// initialize Function and Object constructor |
1317 |
initFunctionAndObject(); |
|
1318 |
||
1319 |
// Now fix Global's own proto. |
|
1320 |
this.setProto(getObjectPrototype()); |
|
1321 |
||
1322 |
// initialize global function properties |
|
1323 |
this.eval = this.builtinEval = ScriptFunctionImpl.makeFunction("eval", EVAL); |
|
1324 |
((ScriptFunction)this.eval).setArity(1); |
|
1325 |
||
1326 |
this.parseInt = ScriptFunctionImpl.makeFunction("parseInt", GlobalFunctions.PARSEINT); |
|
1327 |
this.parseFloat = ScriptFunctionImpl.makeFunction("parseFloat", GlobalFunctions.PARSEFLOAT); |
|
1328 |
this.isNaN = ScriptFunctionImpl.makeFunction("isNaN", GlobalFunctions.IS_NAN); |
|
1329 |
this.isFinite = ScriptFunctionImpl.makeFunction("isFinite", GlobalFunctions.IS_FINITE); |
|
1330 |
this.encodeURI = ScriptFunctionImpl.makeFunction("encodeURI", GlobalFunctions.ENCODE_URI); |
|
1331 |
this.encodeURIComponent = ScriptFunctionImpl.makeFunction("encodeURIComponent", GlobalFunctions.ENCODE_URICOMPONENT); |
|
1332 |
this.decodeURI = ScriptFunctionImpl.makeFunction("decodeURI", GlobalFunctions.DECODE_URI); |
|
1333 |
this.decodeURIComponent = ScriptFunctionImpl.makeFunction("decodeURIComponent", GlobalFunctions.DECODE_URICOMPONENT); |
|
1334 |
this.escape = ScriptFunctionImpl.makeFunction("escape", GlobalFunctions.ESCAPE); |
|
1335 |
this.unescape = ScriptFunctionImpl.makeFunction("unescape", GlobalFunctions.UNESCAPE); |
|
1336 |
this.print = ScriptFunctionImpl.makeFunction("print", getContext()._print_no_newline ? PRINT : PRINTLN); |
|
1337 |
this.load = ScriptFunctionImpl.makeFunction("load", LOAD); |
|
1338 |
||
1339 |
// built-in constructors |
|
1340 |
this.builtinArray = (ScriptFunction)initConstructor("Array"); |
|
1341 |
this.builtinBoolean = (ScriptFunction)initConstructor("Boolean"); |
|
1342 |
this.builtinDate = (ScriptFunction)initConstructor("Date"); |
|
1343 |
this.builtinJSON = initConstructor("JSON"); |
|
1344 |
this.builtinJSAdapter = (ScriptFunction)initConstructor("JSAdapter"); |
|
1345 |
this.builtinMath = initConstructor("Math"); |
|
1346 |
this.builtinNumber = (ScriptFunction)initConstructor("Number"); |
|
1347 |
this.builtinRegExp = (ScriptFunction)initConstructor("RegExp"); |
|
1348 |
this.builtinString = (ScriptFunction)initConstructor("String"); |
|
1349 |
||
1350 |
// initialize String.prototype.length to 0 |
|
1351 |
// add String.prototype.length |
|
1352 |
final ScriptObject stringPrototype = getStringPrototype(); |
|
1353 |
stringPrototype.addOwnProperty("length", Attribute.NON_ENUMERABLE_CONSTANT, 0.0); |
|
1354 |
||
1355 |
// add Array.prototype.length |
|
1356 |
final ScriptObject arrayPrototype = getArrayPrototype(); |
|
1357 |
arrayPrototype.addOwnProperty("length", Attribute.NON_ENUMERABLE_CONSTANT, 0.0); |
|
1358 |
||
1359 |
this.DEFAULT_DATE = new NativeDate(Double.NaN); |
|
1360 |
||
1361 |
// initialize default regexp object |
|
1362 |
this.DEFAULT_REGEXP = new NativeRegExp("(?:)"); |
|
1363 |
||
1364 |
// RegExp.prototype should behave like a RegExp object. So copy the |
|
1365 |
// properties. |
|
1366 |
final ScriptObject regExpProto = getRegExpPrototype(); |
|
1367 |
regExpProto.addBoundProperties(DEFAULT_REGEXP); |
|
1368 |
||
1369 |
// Error stuff |
|
1370 |
initErrorObjects(); |
|
1371 |
||
1372 |
// java access |
|
1373 |
initJavaAccess(); |
|
1374 |
||
1375 |
initTypedArray(); |
|
1376 |
||
1377 |
if (getContext()._scripting) { |
|
1378 |
initScripting(); |
|
1379 |
} |
|
1380 |
||
16185 | 1381 |
if (Context.DEBUG && System.getSecurityManager() == null) { |
16147 | 1382 |
initDebug(); |
1383 |
} |
|
1384 |
||
1385 |
copyBuiltins(); |
|
1386 |
||
1387 |
// initialized with strings so that typeof will work as expected. |
|
1388 |
this.__FILE__ = ""; |
|
1389 |
this.__DIR__ = ""; |
|
1390 |
this.__LINE__ = 0.0; |
|
1391 |
||
1392 |
// expose script (command line) arguments as "arguments" property of global |
|
1393 |
final List<String> arguments = getContext().getOptions().getArguments(); |
|
1394 |
final Object argsObj = wrapAsObject(arguments.toArray()); |
|
1395 |
||
1396 |
addOwnProperty("arguments", Attribute.NOT_ENUMERABLE, argsObj); |
|
1397 |
if (getContext()._scripting) { |
|
1398 |
// synonym for "arguments" in scripting mode |
|
1399 |
addOwnProperty("$ARG", Attribute.NOT_ENUMERABLE, argsObj); |
|
1400 |
} |
|
1401 |
} |
|
1402 |
||
1403 |
private void initErrorObjects() { |
|
1404 |
// Error objects |
|
1405 |
this.builtinError = (ScriptFunction)initConstructor("Error"); |
|
1406 |
final ScriptObject errorProto = getErrorPrototype(); |
|
1407 |
final boolean strict = Global.isStrict(); |
|
1408 |
||
1409 |
// Nashorn specific accessors on Error.prototype - stack, lineNumber, columnNumber and fileName |
|
1410 |
final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", NativeError.GET_STACK); |
|
1411 |
final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", NativeError.SET_STACK); |
|
1412 |
errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack); |
|
1413 |
final ScriptFunction getLineNumber = ScriptFunctionImpl.makeFunction("getLineNumber", NativeError.GET_LINENUMBER); |
|
1414 |
final ScriptFunction setLineNumber = ScriptFunctionImpl.makeFunction("setLineNumber", NativeError.SET_LINENUMBER); |
|
1415 |
errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber); |
|
1416 |
final ScriptFunction getColumnNumber = ScriptFunctionImpl.makeFunction("getColumnNumber", NativeError.GET_COLUMNNUMBER); |
|
1417 |
final ScriptFunction setColumnNumber = ScriptFunctionImpl.makeFunction("setColumnNumber", NativeError.SET_COLUMNNUMBER); |
|
1418 |
errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber); |
|
1419 |
final ScriptFunction getFileName = ScriptFunctionImpl.makeFunction("getFileName", NativeError.GET_FILENAME); |
|
1420 |
final ScriptFunction setFileName = ScriptFunctionImpl.makeFunction("setFileName", NativeError.SET_FILENAME); |
|
1421 |
errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName); |
|
1422 |
||
1423 |
// ECMA 15.11.4.2 Error.prototype.name |
|
1424 |
// Error.prototype.name = "Error"; |
|
1425 |
errorProto.set(NativeError.NAME, "Error", strict); |
|
1426 |
// ECMA 15.11.4.3 Error.prototype.message |
|
1427 |
// Error.prototype.message = ""; |
|
1428 |
errorProto.set(NativeError.MESSAGE, "", strict); |
|
1429 |
||
1430 |
this.builtinEvalError = initErrorSubtype("EvalError", errorProto); |
|
1431 |
this.builtinRangeError = initErrorSubtype("RangeError", errorProto); |
|
1432 |
this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto); |
|
1433 |
this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto); |
|
1434 |
this.builtinTypeError = initErrorSubtype("TypeError", errorProto); |
|
1435 |
this.builtinURIError = initErrorSubtype("URIError", errorProto); |
|
1436 |
} |
|
1437 |
||
1438 |
private ScriptFunction initErrorSubtype(final String name, final ScriptObject errorProto) { |
|
1439 |
final ScriptObject cons = initConstructor(name); |
|
1440 |
final ScriptObject prototype = ScriptFunction.getPrototype(cons); |
|
1441 |
final boolean strict = Global.isStrict(); |
|
1442 |
prototype.set(NativeError.NAME, name, strict); |
|
1443 |
prototype.set(NativeError.MESSAGE, "", strict); |
|
1444 |
prototype.setProto(errorProto); |
|
1445 |
return (ScriptFunction)cons; |
|
1446 |
} |
|
1447 |
||
1448 |
private void initJavaAccess() { |
|
1449 |
final ScriptObject objectProto = getObjectPrototype(); |
|
1450 |
this.builtinPackages = new NativeJavaPackage("", objectProto); |
|
1451 |
this.builtinJava = new NativeJavaPackage("java", objectProto); |
|
1452 |
this.builtinJavax = new NativeJavaPackage("javax", objectProto); |
|
1453 |
this.builtinJavaImporter = initConstructor("JavaImporter"); |
|
1454 |
this.builtinJavaApi = initConstructor("Java"); |
|
1455 |
} |
|
1456 |
||
1457 |
private void initScripting() { |
|
1458 |
Object value; |
|
1459 |
value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE); |
|
1460 |
addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value); |
|
1461 |
||
1462 |
value = ScriptFunctionImpl.makeFunction("read", ScriptingFunctions.READ); |
|
1463 |
addOwnProperty("read", Attribute.NOT_ENUMERABLE, value); |
|
1464 |
||
1465 |
value = ScriptFunctionImpl.makeFunction("quit", ScriptingFunctions.QUIT); |
|
1466 |
addOwnProperty("quit", Attribute.NOT_ENUMERABLE, value); |
|
1467 |
||
1468 |
// Nashorn extension: global.echo (scripting-mode-only) |
|
1469 |
// alias for "print" |
|
1470 |
value = get("print"); |
|
1471 |
addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value); |
|
1472 |
||
1473 |
// Nashorn extension: global.$OPTIONS (scripting-mode-only) |
|
16185 | 1474 |
value = new OptionsObject(this.getContext()); |
16147 | 1475 |
addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, value); |
1476 |
} |
|
1477 |
||
1478 |
private void initTypedArray() { |
|
1479 |
this.builtinArrayBuffer = initConstructor("ArrayBuffer"); |
|
1480 |
this.builtinInt8Array = initConstructor("Int8Array"); |
|
1481 |
this.builtinUint8Array = initConstructor("Uint8Array"); |
|
1482 |
this.builtinUint8ClampedArray = initConstructor("Uint8ClampedArray"); |
|
1483 |
this.builtinInt16Array = initConstructor("Int16Array"); |
|
1484 |
this.builtinUint16Array = initConstructor("Uint16Array"); |
|
1485 |
this.builtinInt32Array = initConstructor("Int32Array"); |
|
1486 |
this.builtinUint32Array = initConstructor("Uint32Array"); |
|
1487 |
this.builtinFloat32Array = initConstructor("Float32Array"); |
|
1488 |
this.builtinFloat64Array = initConstructor("Float64Array"); |
|
1489 |
} |
|
1490 |
||
1491 |
private void copyBuiltins() { |
|
1492 |
this.array = this.builtinArray; |
|
1493 |
this._boolean = this.builtinBoolean; |
|
1494 |
this.date = this.builtinDate; |
|
1495 |
this.error = this.builtinError; |
|
1496 |
this.evalError = this.builtinEvalError; |
|
1497 |
this.function = this.builtinFunction; |
|
1498 |
this.jsadapter = this.builtinJSAdapter; |
|
1499 |
this.json = this.builtinJSON; |
|
1500 |
this.java = this.builtinJava; |
|
1501 |
this.javax = this.builtinJavax; |
|
1502 |
this.javaImporter = this.builtinJavaImporter; |
|
1503 |
this.javaApi = this.builtinJavaApi; |
|
1504 |
this.math = this.builtinMath; |
|
1505 |
this.number = this.builtinNumber; |
|
1506 |
this.object = this.builtinObject; |
|
1507 |
this.packages = this.builtinPackages; |
|
1508 |
this.rangeError = this.builtinRangeError; |
|
1509 |
this.referenceError = this.builtinReferenceError; |
|
1510 |
this.regexp = this.builtinRegExp; |
|
1511 |
this.string = this.builtinString; |
|
1512 |
this.syntaxError = this.builtinSyntaxError; |
|
1513 |
this.typeError = this.builtinTypeError; |
|
1514 |
this.uriError = this.builtinURIError; |
|
1515 |
this.arrayBuffer = this.builtinArrayBuffer; |
|
1516 |
this.int8Array = this.builtinInt8Array; |
|
1517 |
this.uint8Array = this.builtinUint8Array; |
|
1518 |
this.uint8ClampedArray = this.builtinUint8ClampedArray; |
|
1519 |
this.int16Array = this.builtinInt16Array; |
|
1520 |
this.uint16Array = this.builtinUint16Array; |
|
1521 |
this.int32Array = this.builtinInt32Array; |
|
1522 |
this.uint32Array = this.builtinUint32Array; |
|
1523 |
this.float32Array = this.builtinFloat32Array; |
|
1524 |
this.float64Array = this.builtinFloat64Array; |
|
1525 |
} |
|
1526 |
||
1527 |
private void initDebug() { |
|
1528 |
this.addOwnProperty("Debug", Attribute.NOT_ENUMERABLE, initConstructor("Debug")); |
|
1529 |
} |
|
1530 |
||
1531 |
@SuppressWarnings("resource") |
|
1532 |
private static Object printImpl(final boolean newLine, final Object... objects) { |
|
1533 |
final PrintWriter out = Global.getThisContext().getOut(); |
|
1534 |
||
1535 |
boolean first = true; |
|
1536 |
for (final Object object : objects) { |
|
1537 |
if (first) { |
|
1538 |
first = false; |
|
1539 |
} else { |
|
1540 |
out.print(' '); |
|
1541 |
} |
|
1542 |
||
1543 |
out.print(JSType.toString(object)); |
|
1544 |
} |
|
1545 |
||
1546 |
if (newLine) { |
|
1547 |
out.println(); |
|
1548 |
} |
|
1549 |
||
1550 |
out.flush(); |
|
1551 |
||
1552 |
return UNDEFINED; |
|
1553 |
} |
|
1554 |
||
1555 |
/** |
|
1556 |
* These classes are generated by nasgen tool and so we have to use |
|
1557 |
* reflection to load and create new instance of these classes. |
|
1558 |
*/ |
|
1559 |
private ScriptObject initConstructor(final String name) { |
|
1560 |
try { |
|
1561 |
// Assuming class name pattern for built-in JS constructors. |
|
1562 |
final StringBuilder sb = new StringBuilder("jdk.nashorn.internal.objects."); |
|
1563 |
||
1564 |
sb.append("Native"); |
|
1565 |
sb.append(name); |
|
1566 |
sb.append("$Constructor"); |
|
1567 |
||
1568 |
final Class<?> funcClass = Class.forName(sb.toString()); |
|
1569 |
final ScriptObject res = (ScriptObject)funcClass.newInstance(); |
|
1570 |
||
1571 |
if (res instanceof ScriptFunction) { |
|
1572 |
// All global constructor prototypes are not-writable, |
|
1573 |
// not-enumerable and not-configurable. |
|
1574 |
final ScriptFunction func = (ScriptFunction)res; |
|
1575 |
func.modifyOwnProperty(func.getProperty("prototype"), Attribute.NON_ENUMERABLE_CONSTANT); |
|
1576 |
} |
|
1577 |
||
1578 |
if (res.getProto() == null) { |
|
1579 |
res.setProto(getObjectPrototype()); |
|
1580 |
} |
|
1581 |
||
1582 |
return res; |
|
1583 |
||
1584 |
} catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) { |
|
1585 |
throw new RuntimeException(e); |
|
1586 |
} |
|
1587 |
} |
|
1588 |
||
1589 |
// Function and Object constructors are inter-dependent. Also, |
|
1590 |
// Function.prototype |
|
1591 |
// functions are not properly initialized. We fix the references here. |
|
1592 |
// NOTE: be careful if you want to re-order the operations here. You may |
|
1593 |
// have |
|
1594 |
// to play with object references carefully!! |
|
1595 |
private void initFunctionAndObject() { |
|
1596 |
// First-n-foremost is Function |
|
1597 |
this.builtinFunction = (ScriptFunction)initConstructor("Function"); |
|
1598 |
||
1599 |
// create global anonymous function |
|
1600 |
final ScriptFunction anon = ScriptFunctionImpl.newAnonymousFunction(); |
|
1601 |
// need to copy over members of Function.prototype to anon function |
|
1602 |
anon.addBoundProperties(getFunctionPrototype()); |
|
1603 |
||
1604 |
// Function.prototype === Object.getPrototypeOf(Function) === |
|
1605 |
// <anon-function> |
|
1606 |
builtinFunction.setProto(anon); |
|
1607 |
builtinFunction.setPrototype(anon); |
|
1608 |
anon.set("constructor", builtinFunction, anon.isStrict()); |
|
1609 |
anon.deleteOwnProperty(anon.getMap().findProperty("prototype")); |
|
1610 |
||
1611 |
// now initialize Object |
|
1612 |
this.builtinObject = (ScriptFunction)initConstructor("Object"); |
|
1613 |
final ScriptObject ObjectPrototype = getObjectPrototype(); |
|
1614 |
// Object.getPrototypeOf(Function.prototype) === Object.prototype |
|
1615 |
anon.setProto(ObjectPrototype); |
|
1616 |
||
1617 |
// Function valued properties of Function.prototype were not properly |
|
1618 |
// initialized. Because, these were created before global.function and |
|
1619 |
// global.object were not initialized. |
|
1620 |
jdk.nashorn.internal.runtime.Property[] properties = getFunctionPrototype().getMap().getProperties(); |
|
1621 |
for (final jdk.nashorn.internal.runtime.Property property : properties) { |
|
1622 |
final Object key = property.getKey(); |
|
1623 |
final Object value = builtinFunction.get(key); |
|
1624 |
||
1625 |
if (value instanceof ScriptFunction && value != anon) { |
|
1626 |
final ScriptFunction func = (ScriptFunction)value; |
|
1627 |
func.setProto(getFunctionPrototype()); |
|
1628 |
final ScriptObject prototype = ScriptFunction.getPrototype(func); |
|
1629 |
if (prototype != null) { |
|
1630 |
prototype.setProto(ObjectPrototype); |
|
1631 |
} |
|
1632 |
} |
|
1633 |
} |
|
1634 |
||
1635 |
// For function valued properties of Object and Object.prototype, make |
|
1636 |
// sure prototype's proto chain ends with Object.prototype |
|
1637 |
for (final jdk.nashorn.internal.runtime.Property property : builtinObject.getMap().getProperties()) { |
|
1638 |
final Object key = property.getKey(); |
|
1639 |
final Object value = builtinObject.get(key); |
|
1640 |
||
1641 |
if (value instanceof ScriptFunction) { |
|
1642 |
final ScriptFunction func = (ScriptFunction)value; |
|
1643 |
final ScriptObject prototype = ScriptFunction.getPrototype(func); |
|
1644 |
if (prototype != null) { |
|
1645 |
prototype.setProto(ObjectPrototype); |
|
1646 |
} |
|
1647 |
} |
|
1648 |
} |
|
1649 |
||
1650 |
properties = getObjectPrototype().getMap().getProperties(); |
|
1651 |
for (final jdk.nashorn.internal.runtime.Property property : properties) { |
|
1652 |
final Object key = property.getKey(); |
|
1653 |
final Object value = ObjectPrototype.get(key); |
|
1654 |
||
1655 |
if (key.equals("constructor")) { |
|
1656 |
continue; |
|
1657 |
} |
|
1658 |
||
1659 |
if (value instanceof ScriptFunction) { |
|
1660 |
final ScriptFunction func = (ScriptFunction)value; |
|
1661 |
final ScriptObject prototype = ScriptFunction.getPrototype(func); |
|
1662 |
if (prototype != null) { |
|
1663 |
prototype.setProto(ObjectPrototype); |
|
1664 |
} |
|
1665 |
} |
|
1666 |
} |
|
1667 |
} |
|
1668 |
||
1669 |
||
1670 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
16181
f6a125580f62
8006562: findOwnMH in nashorn "objects" package should be cleaned up
sundar
parents:
16151
diff
changeset
|
1671 |
return MH.findStatic(MethodHandles.publicLookup(), Global.class, name, MH.type(rtype, types)); |
16147 | 1672 |
} |
1673 |
} |