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