nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java
author attila
Mon, 02 Mar 2015 14:33:55 +0100
changeset 29282 a8523237b66c
parent 29280 8a79b4040d2a
child 29624 4fd94b55ead4
permissions -rw-r--r--
8074031: Canonicalize is-a-JS-string tests Reviewed-by: hannesw, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.objects;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
18610
9a052996a7b7 8017260: adjust lookup code in objects.* classes
sundar
parents: 18328
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29280
diff changeset
    31
import static jdk.nashorn.internal.runtime.JSType.isString;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
27369
5a251d0063d1 8062308: Incorrect constant linkage with multiple Globals in a Context
attila
parents: 27361
diff changeset
    33
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.io.IOException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.io.PrintWriter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.lang.invoke.MethodHandles;
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
    38
import java.lang.invoke.MethodType;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    39
import java.lang.invoke.SwitchPoint;
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16277
diff changeset
    40
import java.lang.reflect.Field;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    41
import java.util.ArrayList;
18326
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
    42
import java.util.Arrays;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    43
import java.util.List;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import java.util.Map;
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 27814
diff changeset
    45
import java.util.Objects;
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    46
import java.util.concurrent.Callable;
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    47
import java.util.concurrent.ConcurrentHashMap;
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
    48
import javax.script.ScriptContext;
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
    49
import javax.script.ScriptEngine;
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
    50
import jdk.internal.dynalink.CallSiteDescriptor;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16233
diff changeset
    51
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16233
diff changeset
    52
import jdk.internal.dynalink.linker.LinkRequest;
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
    53
import jdk.nashorn.api.scripting.ClassFilter;
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
    54
import jdk.nashorn.api.scripting.ScriptObjectMirror;
22382
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
    55
import jdk.nashorn.internal.lookup.Lookup;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
import jdk.nashorn.internal.objects.annotations.Attribute;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
import jdk.nashorn.internal.objects.annotations.Property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
import jdk.nashorn.internal.objects.annotations.ScriptClass;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
import jdk.nashorn.internal.runtime.Context;
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
    60
import jdk.nashorn.internal.runtime.ECMAErrors;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
    61
import jdk.nashorn.internal.runtime.GlobalConstants;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
import jdk.nashorn.internal.runtime.GlobalFunctions;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
import jdk.nashorn.internal.runtime.JSType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
import jdk.nashorn.internal.runtime.NativeJavaPackage;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21439
diff changeset
    65
import jdk.nashorn.internal.runtime.PropertyDescriptor;
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
    66
import jdk.nashorn.internal.runtime.PropertyMap;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21439
diff changeset
    67
import jdk.nashorn.internal.runtime.Scope;
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
    68
import jdk.nashorn.internal.runtime.ScriptEnvironment;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
import jdk.nashorn.internal.runtime.ScriptFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
import jdk.nashorn.internal.runtime.ScriptObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
import jdk.nashorn.internal.runtime.ScriptRuntime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
import jdk.nashorn.internal.runtime.ScriptingFunctions;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
    73
import jdk.nashorn.internal.runtime.Specialization;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21439
diff changeset
    74
import jdk.nashorn.internal.runtime.arrays.ArrayData;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
    75
import jdk.nashorn.internal.runtime.linker.Bootstrap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
import jdk.nashorn.internal.runtime.linker.InvokeByName;
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
    77
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 21439
diff changeset
    78
import jdk.nashorn.internal.runtime.regexp.RegExpResult;
16275
d5d430071b22 8009379: Remove $ from generated class names
jlaskey
parents: 16262
diff changeset
    79
import jdk.nashorn.internal.scripts.JO;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
 * Representation of global scope.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
@ScriptClass("Global")
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
    85
public final class Global extends ScriptObject implements Scope {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
    86
    // Placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
    87
    private static final Object LOCATION_PROPERTY_PLACEHOLDER = new Object();
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    88
    private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
    89
    private final InvokeByName VALUE_OF  = new InvokeByName("valueOf",  ScriptObject.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    91
    /**
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    92
     * Optimistic builtin names that require switchpoint invalidation
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    93
     * upon assignment. Overly conservative, but works for now, to avoid
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    94
     * any complicated scope checks and especially heavy weight guards
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    95
     * like
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    96
     *
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    97
     * <pre>
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    98
     *     public boolean setterGuard(final Object receiver) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
    99
     *         final Global          global = Global.instance();
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   100
     *         final ScriptObject    sobj   = global.getFunctionPrototype();
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   101
     *         final Object          apply  = sobj.get("apply");
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   102
     *         return apply == receiver;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   103
     *     }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   104
     * </pre>
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   105
     *
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   106
     * Naturally, checking for builtin classes like NativeFunction is cheaper,
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   107
     * it's when you start adding property checks for said builtins you have
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   108
     * problems with guard speed.
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   109
     */
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   110
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   111
    /** Nashorn extension: arguments array */
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   112
    @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   113
    public Object arguments;
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   114
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
    /** ECMA 15.1.2.2 parseInt (string , radix) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    public Object parseInt;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
    /** ECMA 15.1.2.3 parseFloat (string) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
    public Object parseFloat;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
    /** ECMA 15.1.2.4 isNaN (number) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
    public Object isNaN;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
    /** ECMA 15.1.2.5 isFinite (number) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
    public Object isFinite;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
    /** ECMA 15.1.3.3 encodeURI */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    public Object encodeURI;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    /** ECMA 15.1.3.4 encodeURIComponent */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
    public Object encodeURIComponent;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    /** ECMA 15.1.3.1 decodeURI */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
    public Object decodeURI;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
    /** ECMA 15.1.3.2 decodeURIComponent */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    public Object decodeURIComponent;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
    /** ECMA B.2.1 escape (string) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    public Object escape;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
    /** ECMA B.2.2 unescape (string) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
    public Object unescape;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
    /** Nashorn extension: global.print */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    public Object print;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
    /** Nashorn extension: global.load */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
    public Object load;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
   163
    /** Nashorn extension: global.loadWithNewGlobal */
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
   164
    @Property(attributes = Attribute.NOT_ENUMERABLE)
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
   165
    public Object loadWithNewGlobal;
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
   166
16219
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   167
    /** Nashorn extension: global.exit */
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   168
    @Property(attributes = Attribute.NOT_ENUMERABLE)
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   169
    public Object exit;
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   170
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   171
    /** Nashorn extension: global.quit */
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   172
    @Property(attributes = Attribute.NOT_ENUMERABLE)
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   173
    public Object quit;
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   174
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    /** Value property NaN of the Global Object - ECMA 15.1.1.1 NaN */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
   177
    public final double NaN = Double.NaN;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    /** Value property Infinity of the Global Object - ECMA 15.1.1.2 Infinity */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
    @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
   181
    public final double Infinity = Double.POSITIVE_INFINITY;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
    /** Value property Undefined of the Global Object - ECMA 15.1.1.3 Undefined */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
    @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
    public final Object undefined = UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    /** ECMA 15.1.2.1 eval(x) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
    public Object eval;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
    /** ECMA 15.1.4.1 Object constructor. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
    @Property(name = "Object", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
    public volatile Object object;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
    /** ECMA 15.1.4.2 Function constructor. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
    @Property(name = "Function", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
    public volatile Object function;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
    /** ECMA 15.1.4.3 Array constructor. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    @Property(name = "Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
    public volatile Object array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
    /** ECMA 15.1.4.4 String constructor */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
    @Property(name = "String", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
    public volatile Object string;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
    /** ECMA 15.1.4.5 Boolean constructor */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    @Property(name = "Boolean", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
    public volatile Object _boolean;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
    /** ECMA 15.1.4.6 - Number constructor */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
    @Property(name = "Number", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
    public volatile Object number;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
    /** ECMA 15.1.4.7 Date constructor */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
    @Property(name = "Date", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
    public volatile Object date;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
    /** ECMA 15.1.4.8 RegExp constructor */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
    @Property(name = "RegExp", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
    public volatile Object regexp;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
    /** ECMA 15.12 - The JSON object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
    @Property(name = "JSON", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
    public volatile Object json;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    /** Nashorn extension: global.JSAdapter */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
    @Property(name = "JSAdapter", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
    public volatile Object jsadapter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
    /** ECMA 15.8 - The Math object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    @Property(name = "Math", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
    public volatile Object math;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
    /** Error object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
    @Property(name = "Error", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    public volatile Object error;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
    /** EvalError object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    @Property(name = "EvalError", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
    public volatile Object evalError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    /** RangeError object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
    @Property(name = "RangeError", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    public volatile Object rangeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
    /** ReferenceError object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    @Property(name = "ReferenceError", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    public volatile Object referenceError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
    /** SyntaxError object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
    @Property(name = "SyntaxError", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
    public volatile Object syntaxError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
    /** TypeError object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
    @Property(name = "TypeError", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    public volatile Object typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    /** URIError object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
    @Property(name = "URIError", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
    public volatile Object uriError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
    /** ArrayBuffer object */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
    @Property(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
    public volatile Object arrayBuffer;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
23374
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
   267
    /** DataView object */
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
   268
    @Property(name = "DataView", attributes = Attribute.NOT_ENUMERABLE)
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
   269
    public volatile Object dataView;
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
   270
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
    /** TypedArray (int8) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    @Property(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
    public volatile Object int8Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
    /** TypedArray (uint8) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
    @Property(name = "Uint8Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
    public volatile Object uint8Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
    /** TypedArray (uint8) - Clamped */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
    @Property(name = "Uint8ClampedArray", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
    public volatile Object uint8ClampedArray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
    /** TypedArray (int16) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
    @Property(name = "Int16Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    public volatile Object int16Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
    /** TypedArray (uint16) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
    @Property(name = "Uint16Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
    public volatile Object uint16Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
    /** TypedArray (int32) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
    @Property(name = "Int32Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
    public volatile Object int32Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
    /** TypedArray (uint32) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
    @Property(name = "Uint32Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
    public volatile Object uint32Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    /** TypedArray (float32) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
    @Property(name = "Float32Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
    public volatile Object float32Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
    /** TypedArray (float64) */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    @Property(name = "Float64Array", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
    public volatile Object float64Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
    /** Nashorn extension: Java access - global.Packages */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
    @Property(name = "Packages", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
    public volatile Object packages;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   311
    /** Nashorn extension: Java access - global.com */
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   312
    @Property(attributes = Attribute.NOT_ENUMERABLE)
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   313
    public volatile Object com;
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   314
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   315
    /** Nashorn extension: Java access - global.edu */
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   316
    @Property(attributes = Attribute.NOT_ENUMERABLE)
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   317
    public volatile Object edu;
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   318
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
    /** Nashorn extension: Java access - global.java */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
    @Property(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
    public volatile Object java;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   323
    /** Nashorn extension: Java access - global.javafx */
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   324
    @Property(attributes = Attribute.NOT_ENUMERABLE)
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   325
    public volatile Object javafx;
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   326
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
    /** Nashorn extension: Java access - global.javax */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
    @Property(attributes = Attribute.NOT_ENUMERABLE)
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   329
    public volatile Object javax;
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   330
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   331
    /** Nashorn extension: Java access - global.org */
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   332
    @Property(attributes = Attribute.NOT_ENUMERABLE)
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   333
    public volatile Object org;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
    /** Nashorn extension: Java access - global.javaImporter */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
    @Property(name = "JavaImporter", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
    public volatile Object javaImporter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
    /** Nashorn extension: global.Java Object constructor. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
    @Property(name = "Java", attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
    public volatile Object javaApi;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
    /** Nashorn extension: current script's file name */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
    @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
   345
    public final Object __FILE__ = LOCATION_PROPERTY_PLACEHOLDER;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    /** Nashorn extension: current script's directory */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
    @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
   349
    public final Object __DIR__ = LOCATION_PROPERTY_PLACEHOLDER;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
    /** Nashorn extension: current source line number being executed */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
    @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
   353
    public final Object __LINE__ = LOCATION_PROPERTY_PLACEHOLDER;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
    /** Used as Date.prototype's default value */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
    public NativeDate   DEFAULT_DATE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
    /** Used as RegExp.prototype's default value */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
    public NativeRegExp DEFAULT_REGEXP;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
    /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
     * Built-in constructor objects: Even if user changes dynamic values of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     * "Object", "Array" etc., we still want to keep original values of these
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     * constructors here. For example, we need to be able to create array,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     * regexp literals even after user overwrites global "Array" or "RegExp"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
     * constructor - see also ECMA 262 spec. Annex D.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
    private ScriptFunction builtinFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
    private ScriptFunction builtinObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
    private ScriptFunction builtinArray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
    private ScriptFunction builtinBoolean;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
    private ScriptFunction builtinDate;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
    private ScriptObject   builtinJSON;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
    private ScriptFunction builtinJSAdapter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
    private ScriptObject   builtinMath;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
    private ScriptFunction builtinNumber;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
    private ScriptFunction builtinRegExp;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
    private ScriptFunction builtinString;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
    private ScriptFunction builtinError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
    private ScriptFunction builtinEval;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
    private ScriptFunction builtinEvalError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
    private ScriptFunction builtinRangeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
    private ScriptFunction builtinReferenceError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
    private ScriptFunction builtinSyntaxError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
    private ScriptFunction builtinTypeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
    private ScriptFunction builtinURIError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
    private ScriptObject   builtinPackages;
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   388
    private ScriptObject   builtinCom;
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   389
    private ScriptObject   builtinEdu;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
    private ScriptObject   builtinJava;
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   391
    private ScriptObject   builtinJavafx;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    private ScriptObject   builtinJavax;
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
   393
    private ScriptObject   builtinOrg;
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   394
    private ScriptFunction builtinJavaImporter;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
    private ScriptObject   builtinJavaApi;
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   396
    private ScriptFunction builtinArrayBuffer;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   397
    private ScriptFunction builtinDataView;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   398
    private ScriptFunction builtinInt8Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   399
    private ScriptFunction builtinUint8Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   400
    private ScriptFunction builtinUint8ClampedArray;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   401
    private ScriptFunction builtinInt16Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   402
    private ScriptFunction builtinUint16Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   403
    private ScriptFunction builtinInt32Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   404
    private ScriptFunction builtinUint32Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   405
    private ScriptFunction builtinFloat32Array;
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
   406
    private ScriptFunction builtinFloat64Array;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
22382
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
   408
    /*
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
   409
     * ECMA section 13.2.3 The [[ThrowTypeError]] Function Object
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
   410
     */
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
   411
    private ScriptFunction typeErrorThrower;
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
   412
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
    // Flag to indicate that a split method issued a return statement
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
    private int splitState = -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
16223
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
   416
    // Used to store the last RegExp result to support deprecated RegExp constructor properties
16258
0e25f785df4d 8008093: Make RegExp engine pluggable
hannesw
parents: 16256
diff changeset
   417
    private RegExpResult lastRegExpResult;
16223
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
   418
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   419
    private static final MethodHandle EVAL                 = findOwnMH_S("eval",                Object.class, Object.class, Object.class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   420
    private static final MethodHandle NO_SUCH_PROPERTY     = findOwnMH_S(NO_SUCH_PROPERTY_NAME, Object.class, Object.class, Object.class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   421
    private static final MethodHandle PRINT                = findOwnMH_S("print",               Object.class, Object.class, Object[].class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   422
    private static final MethodHandle PRINTLN              = findOwnMH_S("println",             Object.class, Object.class, Object[].class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   423
    private static final MethodHandle LOAD                 = findOwnMH_S("load",                Object.class, Object.class, Object.class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   424
    private static final MethodHandle LOAD_WITH_NEW_GLOBAL = findOwnMH_S("loadWithNewGlobal",   Object.class, Object.class, Object[].class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   425
    private static final MethodHandle EXIT                 = findOwnMH_S("exit",                Object.class, Object.class, Object.class);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   426
    private static final MethodHandle LEXICAL_SCOPE_FILTER = findOwnMH_S("lexicalScopeFilter", Object.class, Object.class);
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   427
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
   428
    // initialized by nasgen
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
   429
    private static PropertyMap $nasgenmap$;
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
   430
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   431
    // context to which this global belongs to
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   432
    private final Context context;
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   433
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   434
    // current ScriptContext to use - can be null.
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   435
    private ScriptContext scontext;
26647
a6acc63c2a31 8058422: Users should be able to overwrite "context" and "engine" variables
sundar
parents: 26508
diff changeset
   436
    // current ScriptEngine associated - can be null.
a6acc63c2a31 8058422: Users should be able to overwrite "context" and "engine" variables
sundar
parents: 26508
diff changeset
   437
    private ScriptEngine engine;
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   438
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   439
    // ES6 global lexical scope.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   440
    private final LexicalScope lexicalScope;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   441
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   442
    // Switchpoint for non-constant global callsites in the presence of ES6 lexical scope.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   443
    private SwitchPoint lexicalScopeSwitchPoint;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   444
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   445
    /**
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   446
     * Set the current script context
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   447
     * @param scontext script context
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   448
     */
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   449
    public void setScriptContext(final ScriptContext scontext) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   450
        this.scontext = scontext;
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   451
    }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   452
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   453
    @Override
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   454
    protected Context getContext() {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   455
        return context;
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   456
    }
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   457
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   458
    // performs initialization checks for Global constructor and returns the
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   459
    // PropertyMap, if everything is fine.
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   460
    private static PropertyMap checkAndGetMap(final Context context) {
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   461
        // security check first
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   462
        final SecurityManager sm = System.getSecurityManager();
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   463
        if (sm != null) {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19456
diff changeset
   464
            sm.checkPermission(new RuntimePermission(Context.NASHORN_CREATE_GLOBAL));
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   465
        }
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   466
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 27814
diff changeset
   467
        Objects.requireNonNull(context);
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   468
25421
a1df2de833a2 8044760: Avoid PropertyMap duplicate for global instances
sundar
parents: 25240
diff changeset
   469
        return $nasgenmap$;
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   470
    }
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   471
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
     * Constructor
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16228
diff changeset
   474
     *
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16228
diff changeset
   475
     * @param context the context
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
     */
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16223
diff changeset
   477
    public Global(final Context context) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18855
diff changeset
   478
        super(checkAndGetMap(context));
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   479
        this.context = context;
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
   480
        this.setIsScope();
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
   481
        this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
    /**
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16223
diff changeset
   485
     * Script access to "current" Global instance
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
     * @return the global singleton
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
    public static Global instance() {
24727
attila
parents: 24720 23375
diff changeset
   490
        final Global global = Context.getGlobal();
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 27814
diff changeset
   491
        Objects.requireNonNull(global);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   492
        return global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   495
    private static Global instanceFrom(final Object self) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   496
        return self instanceof Global? (Global)self : instance();
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   497
    }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   498
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
    /**
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   500
     * Check if we have a Global instance
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   501
     * @return true if one exists
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   502
     */
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   503
    public static boolean hasInstance() {
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   504
        return Context.getGlobal() != null;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   505
    }
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   506
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24738
diff changeset
   507
    /**
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   508
     * Script access to {@link ScriptEnvironment}
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   509
     *
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   510
     * @return the script environment
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   511
     */
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   512
    static ScriptEnvironment getEnv() {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   513
        return instance().getContext().getEnv();
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   514
    }
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   515
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
   516
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
     * Script access to {@link Context}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
     * @return the context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
     */
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16181
diff changeset
   521
    static Context getThisContext() {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   522
        return instance().getContext();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   524
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   525
    // Runtime interface to Global
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   527
    /**
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   528
     * Is there a class filter in the current Context?
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   529
     * @return class filter
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   530
     */
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   531
    public ClassFilter getClassFilter() {
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   532
        return context.getClassFilter();
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   533
    }
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   534
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   535
    /**
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   536
     * Is this global of the given Context?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   537
     * @param ctxt the context
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   538
     * @return true if this global belongs to the given Context
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   539
     */
20933
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20216
diff changeset
   540
    public boolean isOfContext(final Context ctxt) {
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20216
diff changeset
   541
        return this.context == ctxt;
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   542
    }
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   543
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   544
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   545
     * Does this global belong to a strict Context?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   546
     * @return true if this global belongs to a strict Context
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   547
     */
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   548
    public boolean isStrictContext() {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   549
        return context.getEnv()._strict;
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   550
    }
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
   551
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   552
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   553
     * Initialize standard builtin objects like "Object", "Array", "Function" etc.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   554
     * as well as our extension builtin objects like "Java", "JSAdapter" as properties
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   555
     * of the global scope object.
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25585
diff changeset
   556
     *
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27102
diff changeset
   557
     * @param eng ScriptEngine to initialize
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   558
     */
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27102
diff changeset
   559
    public void initBuiltinObjects(final ScriptEngine eng) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
        if (this.builtinObject != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
            // already initialized, just return
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
            return;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27102
diff changeset
   565
        this.engine = eng;
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27102
diff changeset
   566
        init(eng);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   569
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   570
     * Wrap a Java object as corresponding script object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   571
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   572
     * @param obj object to wrap
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   573
     * @return    wrapped object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   574
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
    public Object wrapAsObject(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
        if (obj instanceof Boolean) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   577
            return new NativeBoolean((Boolean)obj, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   578
        } else if (obj instanceof Number) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   579
            return new NativeNumber(((Number)obj).doubleValue(), this);
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29280
diff changeset
   580
        } else if (isString(obj)) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   581
            return new NativeString((CharSequence)obj, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
        } else if (obj instanceof Object[]) { // extension
28786
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
   583
            return new NativeArray(ArrayData.allocate((Object[])obj), this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
        } else if (obj instanceof double[]) { // extension
28786
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
   585
            return new NativeArray(ArrayData.allocate((double[])obj), this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
        } else if (obj instanceof long[]) {
28786
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
   587
            return new NativeArray(ArrayData.allocate((long[])obj), this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
        } else if (obj instanceof int[]) {
28786
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
   589
            return new NativeArray(ArrayData.allocate((int[]) obj), this);
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
   590
        } else if (obj instanceof ArrayData) {
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
   591
            return new NativeArray((ArrayData) obj, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
            // FIXME: more special cases? Map? List?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
            return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   598
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   599
     * Lookup helper for JS primitive types
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   600
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   601
     * @param request the link request for the dynamic call site.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   602
     * @param self     self reference
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   603
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   604
     * @return guarded invocation
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   605
     */
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
   606
    public static GuardedInvocation primitiveLookup(final LinkRequest request, final Object self) {
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29280
diff changeset
   607
        if (isString(self)) {
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   608
            return NativeString.lookupPrimitive(request, self);
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   609
        } else if (self instanceof Number) {
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   610
            return NativeNumber.lookupPrimitive(request, self);
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   611
        } else if (self instanceof Boolean) {
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   612
            return NativeBoolean.lookupPrimitive(request, self);
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   613
        }
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   614
        throw new IllegalArgumentException("Unsupported primitive: " + self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   617
    /**
26508
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   618
     * Returns a method handle that creates a wrapper object for a JS primitive value.
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   619
     *
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   620
     * @param self receiver object
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   621
     * @return method handle to create wrapper objects for primitive receiver
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   622
     */
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   623
    public static MethodHandle getPrimitiveWrapFilter(final Object self) {
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29280
diff changeset
   624
        if (isString(self)) {
26508
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   625
            return NativeString.WRAPFILTER;
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   626
        } else if (self instanceof Number) {
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   627
            return NativeNumber.WRAPFILTER;
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   628
        } else if (self instanceof Boolean) {
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   629
            return NativeBoolean.WRAPFILTER;
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   630
        }
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   631
        throw new IllegalArgumentException("Unsupported primitive: " + self);
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   632
    }
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   633
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   634
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 26071
diff changeset
   635
    /**
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   636
     * Create a new empty script object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   637
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   638
     * @return the new ScriptObject
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   639
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   640
    public ScriptObject newObject() {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
   641
        return new JO(getObjectPrototype(), JO.getInitialMap());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   642
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   643
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   644
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   645
     * Default value of given type
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   646
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   647
     * @param sobj     script object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   648
     * @param typeHint type hint
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   649
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   650
     * @return default value
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   651
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
    public Object getDefaultValue(final ScriptObject sobj, final Class<?> typeHint) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
        // When the [[DefaultValue]] internal method of O is called with no hint,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   654
        // then it behaves as if the hint were Number, unless O is a Date object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   655
        // in which case it behaves as if the hint were String.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   656
        Class<?> hint = typeHint;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   657
        if (hint == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   658
            hint = Number.class;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   659
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   660
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   661
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
            if (hint == String.class) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
                final Object toString = TO_STRING.getGetter().invokeExact(sobj);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   665
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   666
                if (Bootstrap.isCallable(toString)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
                    final Object value = TO_STRING.getInvoker().invokeExact(toString, sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   668
                    if (JSType.isPrimitive(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   669
                        return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   670
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   671
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   672
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   673
                final Object valueOf = VALUE_OF.getGetter().invokeExact(sobj);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   674
                if (Bootstrap.isCallable(valueOf)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   675
                    final Object value = VALUE_OF.getInvoker().invokeExact(valueOf, sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   676
                    if (JSType.isPrimitive(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   677
                        return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   678
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   679
                }
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16234
diff changeset
   680
                throw typeError(this, "cannot.get.default.string");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   681
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   682
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   683
            if (hint == Number.class) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   684
                final Object valueOf = VALUE_OF.getGetter().invokeExact(sobj);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   685
                if (Bootstrap.isCallable(valueOf)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   686
                    final Object value = VALUE_OF.getInvoker().invokeExact(valueOf, sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
                    if (JSType.isPrimitive(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
                        return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   689
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   690
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   691
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   692
                final Object toString = TO_STRING.getGetter().invokeExact(sobj);
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 19085
diff changeset
   693
                if (Bootstrap.isCallable(toString)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   694
                    final Object value = TO_STRING.getInvoker().invokeExact(toString, sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   695
                    if (JSType.isPrimitive(value)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   696
                        return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   697
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   698
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   699
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16234
diff changeset
   700
                throw typeError(this, "cannot.get.default.number");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   701
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   702
        } catch (final RuntimeException | Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   703
            throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   704
        } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   705
            throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   706
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   707
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   708
        return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   709
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   710
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   711
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   712
     * Is the given ScriptObject an ECMAScript Error object?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   713
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   714
     * @param sobj the object being checked
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   715
     * @return true if sobj is an Error object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   716
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   717
    public boolean isError(final ScriptObject sobj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   718
        final ScriptObject errorProto = getErrorPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   719
        ScriptObject proto = sobj.getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   720
        while (proto != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   721
            if (proto == errorProto) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   722
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   723
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   724
            proto = proto.getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   725
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   726
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   727
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   728
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   729
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   730
     * Create a new ECMAScript Error object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   731
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   732
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   733
     * @return newly created Error object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   734
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   735
    public ScriptObject newError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   736
        return new NativeError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   737
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   738
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   739
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   740
     * Create a new ECMAScript EvalError object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   741
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   742
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   743
     * @return newly created EvalError object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   744
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   745
    public ScriptObject newEvalError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   746
        return new NativeEvalError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   747
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   748
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   749
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   750
     * Create a new ECMAScript RangeError object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   751
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   752
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   753
     * @return newly created RangeError object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   754
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   755
    public ScriptObject newRangeError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   756
        return new NativeRangeError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   757
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   758
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   759
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   760
     * Create a new ECMAScript ReferenceError object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   761
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   762
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   763
     * @return newly created ReferenceError object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   764
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   765
    public ScriptObject newReferenceError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   766
        return new NativeReferenceError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   767
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   768
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   769
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   770
     * Create a new ECMAScript SyntaxError object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   771
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   772
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   773
     * @return newly created SyntaxError object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   774
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   775
    public ScriptObject newSyntaxError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   776
        return new NativeSyntaxError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   777
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   778
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   779
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   780
     * Create a new ECMAScript TypeError object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   781
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   782
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   783
     * @return newly created TypeError object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   784
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   785
    public ScriptObject newTypeError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   786
        return new NativeTypeError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   787
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   788
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   789
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   790
     * Create a new ECMAScript URIError object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   791
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   792
     * @param msg error message
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   793
     * @return newly created URIError object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   794
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   795
    public ScriptObject newURIError(final String msg) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   796
        return new NativeURIError(msg, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   797
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   798
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   799
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   800
     * Create a new ECMAScript GenericDescriptor object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   801
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   802
     * @param configurable is the property configurable?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   803
     * @param enumerable is the property enumerable?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   804
     * @return newly created GenericDescriptor object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   805
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   806
    public PropertyDescriptor newGenericDescriptor(final boolean configurable, final boolean enumerable) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   807
        return new GenericPropertyDescriptor(configurable, enumerable, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   808
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   809
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   810
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   811
     * Create a new ECMAScript DatePropertyDescriptor object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   812
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   813
     * @param value of the data property
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   814
     * @param configurable is the property configurable?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   815
     * @param enumerable is the property enumerable?
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
   816
     * @param writable is the property writable?
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   817
     * @return newly created DataPropertyDescriptor object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   818
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   819
    public PropertyDescriptor newDataDescriptor(final Object value, final boolean configurable, final boolean enumerable, final boolean writable) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   820
        return new DataPropertyDescriptor(configurable, enumerable, writable, value, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   821
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   822
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   823
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   824
     * Create a new ECMAScript AccessorPropertyDescriptor object.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   825
     *
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   826
     * @param get getter function of the user accessor property
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   827
     * @param set setter function of the user accessor property
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   828
     * @param configurable is the property configurable?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   829
     * @param enumerable is the property enumerable?
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   830
     * @return newly created AccessorPropertyDescriptor object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   831
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   832
    public PropertyDescriptor newAccessorDescriptor(final Object get, final Object set, final boolean configurable, final boolean enumerable) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   833
        final AccessorPropertyDescriptor desc = new AccessorPropertyDescriptor(configurable, enumerable, get == null ? UNDEFINED : get, set == null ? UNDEFINED : set, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   834
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   835
        if (get == null) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18326
diff changeset
   836
            desc.delete(PropertyDescriptor.GET, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   837
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   838
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   839
        if (set == null) {
18328
ebd24057f163 8015355: Array.prototype functions don't honour non-writable length and / or index properties
sundar
parents: 18326
diff changeset
   840
            desc.delete(PropertyDescriptor.SET, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   841
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   842
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   843
        return desc;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   844
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   845
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   846
    private static <T> T getLazilyCreatedValue(final Object key, final Callable<T> creator, final Map<Object, T> map) {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   847
        final T obj = map.get(key);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   848
        if (obj != null) {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   849
            return obj;
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   850
        }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   851
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   852
        try {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   853
            final T newObj = creator.call();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   854
            final T existingObj = map.putIfAbsent(key, newObj);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   855
            return existingObj != null ? existingObj : newObj;
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   856
        } catch (final Exception exp) {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   857
            throw new RuntimeException(exp);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   858
        }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   859
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   860
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   861
    private final Map<Object, InvokeByName> namedInvokers = new ConcurrentHashMap<>();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   862
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   863
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   864
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   865
     * Get cached InvokeByName object for the given key
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   866
     * @param key key to be associated with InvokeByName object
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   867
     * @param creator if InvokeByName is absent 'creator' is called to make one (lazy init)
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   868
     * @return InvokeByName object associated with the key.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   869
     */
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   870
    public InvokeByName getInvokeByName(final Object key, final Callable<InvokeByName> creator) {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   871
        return getLazilyCreatedValue(key, creator, namedInvokers);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   872
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   873
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   874
    private final Map<Object, MethodHandle> dynamicInvokers = new ConcurrentHashMap<>();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   875
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   876
    /**
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   877
     * Get cached dynamic method handle for the given key
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   878
     * @param key key to be associated with dynamic method handle
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   879
     * @param creator if method handle is absent 'creator' is called to make one (lazy init)
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   880
     * @return dynamic method handle associated with the key.
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23374
diff changeset
   881
     */
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   882
    public MethodHandle getDynamicInvoker(final Object key, final Callable<MethodHandle> creator) {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   883
        return getLazilyCreatedValue(key, creator, dynamicInvokers);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   884
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19088
diff changeset
   885
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   886
    /**
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   887
     * Hook to search missing variables in ScriptContext if available
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   888
     * @param self used to detect if scope call or not (this function is 'strict')
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   889
     * @param name name of the variable missing
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   890
     * @return value of the missing variable or undefined (or TypeError for scope search)
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   891
     */
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   892
    public static Object __noSuchProperty__(final Object self, final Object name) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   893
        final Global global = Global.instance();
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   894
        final ScriptContext sctxt = global.scontext;
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   895
        final String nameStr = name.toString();
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   896
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   897
        if (sctxt != null) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   898
            final int scope = sctxt.getAttributesScope(nameStr);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   899
            if (scope != -1) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   900
                return ScriptObjectMirror.unwrap(sctxt.getAttribute(nameStr, scope), global);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   901
            }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   902
        }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   903
26647
a6acc63c2a31 8058422: Users should be able to overwrite "context" and "engine" variables
sundar
parents: 26508
diff changeset
   904
        switch (nameStr) {
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   905
        case "context":
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   906
            return sctxt;
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   907
        case "engine":
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   908
            return global.engine;
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   909
        default:
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   910
            break;
26647
a6acc63c2a31 8058422: Users should be able to overwrite "context" and "engine" variables
sundar
parents: 26508
diff changeset
   911
        }
a6acc63c2a31 8058422: Users should be able to overwrite "context" and "engine" variables
sundar
parents: 26508
diff changeset
   912
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   913
        if (self == UNDEFINED) {
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   914
            // scope access and so throw ReferenceError
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   915
            throw referenceError(global, "not.defined", nameStr);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   916
        }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   917
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   918
        return UNDEFINED;
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   919
    }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   920
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   921
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   922
     * This is the eval used when 'indirect' eval call is made.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   923
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   924
     * var global = this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   925
     * global.eval("print('hello')");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   926
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   927
     * @param self  eval scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   928
     * @param str   eval string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   929
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   930
     * @return the result of eval
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   931
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   932
    public static Object eval(final Object self, final Object str) {
25423
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25422
diff changeset
   933
        return directEval(self, str, UNDEFINED, UNDEFINED, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   934
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   935
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   936
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   937
     * Direct eval
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   938
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
     * @param self     The scope of eval passed as 'self'
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
     * @param str      Evaluated code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   941
     * @param callThis "this" to be passed to the evaluated code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   942
     * @param location location of the eval call
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
     * @param strict   is eval called a strict mode code?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
     * @return the return value of the eval
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
     * This is directly invoked from generated when eval(code) is called in user code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
     */
25423
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25422
diff changeset
   949
    public static Object directEval(final Object self, final Object str, final Object callThis, final Object location, final boolean strict) {
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29280
diff changeset
   950
        if (!isString(str)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   951
            return str;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
        }
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   953
        final Global global = Global.instanceFrom(self);
29280
8a79b4040d2a 8074021: Indirect eval fails when used as an element of an array or as a property of an object
sundar
parents: 28786
diff changeset
   954
        final ScriptObject scope = self instanceof ScriptObject && ((ScriptObject)self).isScope() ? (ScriptObject)self : global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   955
25423
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25422
diff changeset
   956
        return global.getContext().eval(scope, str.toString(), callThis, location, strict, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   957
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
     * Global print implementation - Nashorn extension
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   962
     * @param self    scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   963
     * @param objects arguments to print
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   964
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   965
     * @return result of print (undefined)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   966
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   967
    public static Object print(final Object self, final Object... objects) {
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   968
        return Global.instanceFrom(self).printImpl(false, objects);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   969
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   970
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   971
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   972
     * Global println implementation - Nashorn extension
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   973
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   974
     * @param self    scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
     * @param objects arguments to print
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   977
     * @return result of println (undefined)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   978
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   979
    public static Object println(final Object self, final Object... objects) {
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   980
        return Global.instanceFrom(self).printImpl(true, objects);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   981
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   982
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   983
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   984
     * Global load implementation - Nashorn extension
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   985
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   986
     * @param self    scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   987
     * @param source  source to load
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   988
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   989
     * @return result of load (undefined)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   990
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   991
     * @throws IOException if source could not be read
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   992
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   993
    public static Object load(final Object self, final Object source) throws IOException {
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
   994
        final Global global = Global.instanceFrom(self);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   995
        final ScriptObject scope = self instanceof ScriptObject ? (ScriptObject)self : global;
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   996
        return global.getContext().load(scope, source);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   997
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   998
16219
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
   999
    /**
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1000
     * Global loadWithNewGlobal implementation - Nashorn extension
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1001
     *
18326
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1002
     * @param self scope
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1003
     * @param args from plus (optional) arguments to be passed to the loaded script
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1004
     *
18326
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1005
     * @return result of load (may be undefined)
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1006
     *
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1007
     * @throws IOException if source could not be read
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1008
     */
18326
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1009
    public static Object loadWithNewGlobal(final Object self, final Object...args) throws IOException {
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1010
        final Global global = Global.instanceFrom(self);
18326
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1011
        final int length = args.length;
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1012
        final boolean hasArgs = 0 < length;
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1013
        final Object from = hasArgs ? args[0] : UNDEFINED;
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1014
        final Object[] arguments = hasArgs ? Arrays.copyOfRange(args, 1, length) : args;
512bdf9535a6 8016453: loadWithNewGlobal does not allow apply operation
jlaskey
parents: 18321
diff changeset
  1015
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  1016
        return global.getContext().loadWithNewGlobal(from, arguments);
17974
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1017
    }
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1018
a2818f17324e 8015741: Need a global.load function that starts with a new global scope.
jlaskey
parents: 17972
diff changeset
  1019
    /**
16219
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1020
     * Global exit and quit implementation - Nashorn extension: perform a {@code System.exit} call from the script
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1021
     *
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1022
     * @param self  self reference
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1023
     * @param code  exit code
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1024
     *
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
  1025
     * @return undefined (will never be reached)
16219
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1026
     */
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1027
    public static Object exit(final Object self, final Object code) {
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1028
        System.exit(JSType.toInt32(code));
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1029
        return UNDEFINED;
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1030
    }
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1031
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  1032
    // builtin prototype accessors
28786
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1033
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1034
    /**
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1035
     * Get the builtin Object prototype.
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1036
      * @return the object prototype.
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1037
     */
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1038
    public ScriptObject getObjectPrototype() {
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1039
        return ScriptFunction.getPrototype(builtinObject);
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1040
    }
679fd2d26470 8062141: Various performance issues parsing JSON
hannesw
parents: 28785
diff changeset
  1041
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1042
    ScriptObject getFunctionPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1043
        return ScriptFunction.getPrototype(builtinFunction);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1044
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1045
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1046
    ScriptObject getArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1047
        return ScriptFunction.getPrototype(builtinArray);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1048
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1049
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1050
    ScriptObject getBooleanPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1051
        return ScriptFunction.getPrototype(builtinBoolean);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1052
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1053
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1054
    ScriptObject getNumberPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1055
        return ScriptFunction.getPrototype(builtinNumber);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1056
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1057
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1058
    ScriptObject getDatePrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1059
        return ScriptFunction.getPrototype(builtinDate);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1060
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1061
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1062
    ScriptObject getRegExpPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1063
        return ScriptFunction.getPrototype(builtinRegExp);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1064
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1065
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1066
    ScriptObject getStringPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1067
        return ScriptFunction.getPrototype(builtinString);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1068
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1069
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1070
    ScriptObject getErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1071
        return ScriptFunction.getPrototype(builtinError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1072
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1073
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1074
    ScriptObject getEvalErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1075
        return ScriptFunction.getPrototype(builtinEvalError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1076
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1077
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1078
    ScriptObject getRangeErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1079
        return ScriptFunction.getPrototype(builtinRangeError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1080
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1081
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1082
    ScriptObject getReferenceErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1083
        return ScriptFunction.getPrototype(builtinReferenceError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1084
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1085
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1086
    ScriptObject getSyntaxErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1087
        return ScriptFunction.getPrototype(builtinSyntaxError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1088
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1089
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1090
    ScriptObject getTypeErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1091
        return ScriptFunction.getPrototype(builtinTypeError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1092
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1093
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1094
    ScriptObject getURIErrorPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1095
        return ScriptFunction.getPrototype(builtinURIError);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1096
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1097
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1098
    ScriptObject getJavaImporterPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1099
        return ScriptFunction.getPrototype(builtinJavaImporter);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1100
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1101
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1102
    ScriptObject getJSAdapterPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1103
        return ScriptFunction.getPrototype(builtinJSAdapter);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1104
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1105
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1106
    ScriptObject getArrayBufferPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1107
        return ScriptFunction.getPrototype(builtinArrayBuffer);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1108
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1109
23374
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
  1110
    ScriptObject getDataViewPrototype() {
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
  1111
        return ScriptFunction.getPrototype(builtinDataView);
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
  1112
    }
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
  1113
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1114
    ScriptObject getInt8ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1115
        return ScriptFunction.getPrototype(builtinInt8Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1116
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1118
    ScriptObject getUint8ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1119
        return ScriptFunction.getPrototype(builtinUint8Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1120
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1122
    ScriptObject getUint8ClampedArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1123
        return ScriptFunction.getPrototype(builtinUint8ClampedArray);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1124
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1125
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1126
    ScriptObject getInt16ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1127
        return ScriptFunction.getPrototype(builtinInt16Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1128
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1129
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1130
    ScriptObject getUint16ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1131
        return ScriptFunction.getPrototype(builtinUint16Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1132
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1133
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1134
    ScriptObject getInt32ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1135
        return ScriptFunction.getPrototype(builtinInt32Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1136
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1137
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1138
    ScriptObject getUint32ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1139
        return ScriptFunction.getPrototype(builtinUint32Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1140
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1141
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1142
    ScriptObject getFloat32ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1143
        return ScriptFunction.getPrototype(builtinFloat32Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1144
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1145
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1146
    ScriptObject getFloat64ArrayPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1147
        return ScriptFunction.getPrototype(builtinFloat64Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1148
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1149
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1150
    private ScriptFunction getBuiltinArray() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1151
        return builtinArray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1152
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1153
22382
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  1154
    ScriptFunction getTypeErrorThrower() {
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  1155
        return typeErrorThrower;
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  1156
    }
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  1157
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1158
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1159
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1160
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1161
     * @return true if builtin array has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1162
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1163
    public static boolean isBuiltinArray() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1164
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1165
        return instance.array == instance.getBuiltinArray();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1166
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1167
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1168
    private ScriptFunction getBuiltinBoolean() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1169
        return builtinBoolean;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1170
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1171
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1172
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1173
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1174
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1175
     * @return true if builtin boolean has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1176
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1177
    public static boolean isBuiltinBoolean() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1178
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1179
        return instance._boolean == instance.getBuiltinBoolean();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1180
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1181
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1182
    private ScriptFunction getBuiltinDate() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1183
        return builtinDate;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1184
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1185
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1186
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1187
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1188
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1189
     * @return true if builtin date has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1190
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1191
    public static boolean isBuiltinDate() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1192
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1193
        return instance.date == instance.getBuiltinDate();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1194
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1195
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1196
    private ScriptFunction getBuiltinError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1197
        return builtinError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1198
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1199
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1200
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1201
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1202
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1203
     * @return true if builtin error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1204
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1205
    public static boolean isBuiltinError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1206
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1207
        return instance.error == instance.getBuiltinError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1208
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1209
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1210
    private ScriptFunction getBuiltinEvalError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1211
        return builtinEvalError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1212
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1213
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1214
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1215
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1216
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1217
     * @return true if builtin eval error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1218
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1219
    public static boolean isBuiltinEvalError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1220
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1221
        return instance.evalError == instance.getBuiltinEvalError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1222
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1223
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1224
    private ScriptFunction getBuiltinFunction() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1225
        return builtinFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1226
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1227
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1228
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1229
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1230
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1231
     * @return true if builtin function has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1232
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1233
    public static boolean isBuiltinFunction() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1234
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1235
        return instance.function == instance.getBuiltinFunction();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1236
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1237
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1238
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1239
     * Get the switchpoint used to check property changes for Function.prototype.apply
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1240
     * @return the switchpoint guarding apply (same as guarding call, and everything else in function)
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1241
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1242
    public static SwitchPoint getBuiltinFunctionApplySwitchPoint() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1243
        return ScriptFunction.getPrototype(Global.instance().getBuiltinFunction()).getProperty("apply").getBuiltinSwitchPoint();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1244
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1245
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1246
    private static boolean isBuiltinFunctionProperty(final String name) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1247
        final Global instance = Global.instance();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1248
        final ScriptFunction builtinFunction = instance.getBuiltinFunction();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1249
        if (builtinFunction == null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1250
            return false; //conservative for compile-only mode
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1251
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1252
        final boolean isBuiltinFunction = instance.function == builtinFunction;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1253
        return isBuiltinFunction && ScriptFunction.getPrototype(builtinFunction).getProperty(name).isBuiltin();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1254
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1255
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1256
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1257
     * Check if the Function.prototype.apply has not been replaced
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1258
     * @return true if Function.prototype.apply has been replaced
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1259
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1260
    public static boolean isBuiltinFunctionPrototypeApply() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1261
        return isBuiltinFunctionProperty("apply");
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1262
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1263
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1264
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1265
     * Check if the Function.prototype.apply has not been replaced
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1266
     * @return true if Function.prototype.call has been replaced
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1267
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1268
    public static boolean isBuiltinFunctionPrototypeCall() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1269
        return isBuiltinFunctionProperty("call");
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1270
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1271
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1272
    private ScriptFunction getBuiltinJSAdapter() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1273
        return builtinJSAdapter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1274
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1275
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1276
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1277
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1278
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1279
     * @return true if builtin JSAdapter has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1280
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1281
    public static boolean isBuiltinJSAdapter() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1282
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1283
        return instance.jsadapter == instance.getBuiltinJSAdapter();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1284
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1285
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1286
    private ScriptObject getBuiltinJSON() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1287
        return builtinJSON;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1288
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1289
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1290
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1291
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1292
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1293
     * @return true if builtin JSON has has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1294
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1295
    public static boolean isBuiltinJSON() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1296
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1297
        return instance.json == instance.getBuiltinJSON();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1298
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1299
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1300
    private ScriptObject getBuiltinJava() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1301
        return builtinJava;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1302
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1303
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1304
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1305
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1306
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1307
     * @return true if builtin Java has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1308
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1309
    public static boolean isBuiltinJava() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1310
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1311
        return instance.java == instance.getBuiltinJava();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1312
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1313
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1314
    private ScriptObject getBuiltinJavax() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1315
        return builtinJavax;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1316
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1317
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1318
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1319
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1320
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1321
     * @return true if builtin Javax has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1322
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1323
    public static boolean isBuiltinJavax() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1324
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1325
        return instance.javax == instance.getBuiltinJavax();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1326
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1327
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1328
    private ScriptObject getBuiltinJavaImporter() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1329
        return builtinJavaImporter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1330
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1331
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1332
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1333
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1334
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1335
     * @return true if builtin Java importer has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1336
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1337
    public static boolean isBuiltinJavaImporter() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1338
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1339
        return instance.javaImporter == instance.getBuiltinJavaImporter();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1340
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1341
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1342
    private ScriptObject getBuiltinMath() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1343
        return builtinMath;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1344
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1345
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1346
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1347
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1348
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1349
     * @return true if builtin math has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1350
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1351
    public static boolean isBuiltinMath() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1352
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1353
        return instance.math == instance.getBuiltinMath();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1354
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1355
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1356
    private ScriptFunction getBuiltinNumber() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1357
        return builtinNumber;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1358
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1359
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1360
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1361
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1362
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1363
     * @return true if builtin number has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1364
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1365
    public static boolean isBuiltinNumber() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1366
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1367
        return instance.number == instance.getBuiltinNumber();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1368
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1369
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1370
    private ScriptFunction getBuiltinObject() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1371
        return builtinObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1372
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1373
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1374
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1375
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1376
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1377
     * @return true if builtin object has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1378
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1379
    public static boolean isBuiltinObject() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1380
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1381
        return instance.object == instance.getBuiltinObject();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1382
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1383
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1384
    private ScriptObject getBuiltinPackages() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1385
        return builtinPackages;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1386
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1387
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1388
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1389
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1390
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1391
     * @return true if builtin package has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1392
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1393
    public static boolean isBuiltinPackages() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1394
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1395
        return instance.packages == instance.getBuiltinPackages();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1396
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1397
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1398
    private ScriptFunction getBuiltinRangeError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1399
        return builtinRangeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1400
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1401
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1402
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1403
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1404
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1405
     * @return true if builtin range error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1406
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1407
    public static boolean isBuiltinRangeError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1408
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1409
        return instance.rangeError == instance.getBuiltinRangeError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1410
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1411
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1412
    private ScriptFunction getBuiltinReferenceError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1413
        return builtinReferenceError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1414
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1415
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1416
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1417
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1418
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1419
     * @return true if builtin reference error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1420
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1421
    public static boolean isBuiltinReferenceError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1422
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1423
        return instance.referenceError == instance.getBuiltinReferenceError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1424
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1425
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1426
    private ScriptFunction getBuiltinRegExp() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1427
        return builtinRegExp;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1428
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1429
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1430
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1431
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1432
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1433
     * @return true if builtin regexp has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1434
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1435
    public static boolean isBuiltinRegExp() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1436
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1437
        return instance.regexp == instance.getBuiltinRegExp();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1438
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1439
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1440
    private ScriptFunction getBuiltinString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1441
        return builtinString;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1442
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1443
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1444
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1445
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1446
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1447
     * @return true if builtin Java has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1448
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1449
    public static boolean isBuiltinString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1450
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1451
        return instance.string == instance.getBuiltinString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1452
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1453
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1454
    private ScriptFunction getBuiltinSyntaxError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1455
        return builtinSyntaxError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1456
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1457
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1458
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1459
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1460
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1461
     * @return true if builtin syntax error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1462
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1463
    public static boolean isBuiltinSyntaxError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1464
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1465
        return instance.syntaxError == instance.getBuiltinSyntaxError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1466
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1467
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1468
    private ScriptFunction getBuiltinTypeError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1469
        return builtinTypeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1470
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1471
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1472
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1473
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1474
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1475
     * @return true if builtin type error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1476
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1477
    public static boolean isBuiltinTypeError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1478
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1479
        return instance.typeError == instance.getBuiltinTypeError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1480
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1481
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1482
    private ScriptFunction getBuiltinURIError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1483
        return builtinURIError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1484
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1485
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1486
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1487
     * Called from compiled script code to test if builtin has been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1488
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1489
     * @return true if builtin URI error has not been overridden
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1490
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1491
    public static boolean isBuiltinURIError() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1492
        final Global instance = Global.instance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1493
        return instance.uriError == instance.getBuiltinURIError();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1494
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1495
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1496
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1497
    public String getClassName() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1498
        return "global";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1499
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1500
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1501
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1502
     * Copy function used to clone NativeRegExp objects.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1503
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1504
     * @param regexp a NativeRegExp to clone
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1505
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1506
     * @return copy of the given regexp object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1507
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1508
    public static Object regExpCopy(final Object regexp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1509
        return new NativeRegExp((NativeRegExp)regexp);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1510
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1511
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1512
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1513
     * Convert given object to NativeRegExp type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1514
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1515
     * @param obj object to be converted
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1516
     * @return NativeRegExp instance
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1517
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1518
    public static NativeRegExp toRegExp(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1519
        if (obj instanceof NativeRegExp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1520
            return (NativeRegExp)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1521
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1522
        return new NativeRegExp(JSType.toString(obj));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1523
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1524
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1525
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1526
     * ECMA 9.9 ToObject implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1527
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1528
     * @param obj  an item for which to run ToObject
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1529
     * @return ToObject version of given item
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1530
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1531
    public static Object toObject(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1532
        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
  1533
            throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1534
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1535
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1536
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1537
            return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1538
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1539
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1540
        return instance().wrapAsObject(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1541
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1542
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1543
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1544
     * Allocate a new object array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1545
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1546
     * @param initial object values.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1547
     * @return the new array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1548
     */
16210
8ad1381b69d0 8007215: Varargs broken for the case of passing more than the arg limit arguments.
lagergren
parents: 16207
diff changeset
  1549
    public static NativeArray allocate(final Object[] initial) {
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1550
        ArrayData arrayData = ArrayData.allocate(initial);
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1551
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1552
        for (int index = 0; index < initial.length; index++) {
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1553
            final Object value = initial[index];
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1554
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1555
            if (value == ScriptRuntime.EMPTY) {
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1556
                arrayData = arrayData.delete(index);
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1557
            }
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1558
        }
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1559
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1560
        return new NativeArray(arrayData);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1561
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1562
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1563
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1564
     * Allocate a new number array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1565
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1566
     * @param initial number values.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1567
     * @return the new array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1568
     */
16210
8ad1381b69d0 8007215: Varargs broken for the case of passing more than the arg limit arguments.
lagergren
parents: 16207
diff changeset
  1569
    public static NativeArray allocate(final double[] initial) {
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1570
        return new NativeArray(ArrayData.allocate(initial));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1571
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1572
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1573
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1574
     * Allocate a new long array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1575
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1576
     * @param initial number values.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1577
     * @return the new array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1578
     */
16210
8ad1381b69d0 8007215: Varargs broken for the case of passing more than the arg limit arguments.
lagergren
parents: 16207
diff changeset
  1579
    public static NativeArray allocate(final long[] initial) {
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1580
        return new NativeArray(ArrayData.allocate(initial));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1581
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1582
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1583
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1584
     * Allocate a new integer array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1585
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1586
     * @param initial number values.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1587
     * @return the new array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1588
     */
16210
8ad1381b69d0 8007215: Varargs broken for the case of passing more than the arg limit arguments.
lagergren
parents: 16207
diff changeset
  1589
    public static NativeArray allocate(final int[] initial) {
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1590
        return new NativeArray(ArrayData.allocate(initial));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1591
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1592
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1593
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1594
     * Allocate a new object array for arguments.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1595
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1596
     * @param arguments initial arguments passed.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1597
     * @param callee reference to the function that uses arguments object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1598
     * @param numParams actual number of declared parameters
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1599
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1600
     * @return the new array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1601
     */
16206
83069fa0935b 8006529: Methods always get callee - it should be conditional
attila
parents: 16196
diff changeset
  1602
    public static ScriptObject allocateArguments(final Object[] arguments, final Object callee, final int numParams) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1603
        return NativeArguments.allocate(arguments, (ScriptFunction)callee, numParams);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1604
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1605
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1606
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1607
     * Called from generated to check if given function is the builtin 'eval'. If
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1608
     * eval is used in a script, a lot of optimizations and assumptions cannot be done.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1609
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1610
     * @param  fn function object that is checked
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1611
     * @return true if fn is the builtin eval
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1612
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1613
    public static boolean isEval(final Object fn) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1614
        return fn == Global.instance().builtinEval;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1615
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1616
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1617
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1618
     * Called from generated to replace a location property placeholder with the actual location property value.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1619
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1620
     * @param  placeholder the value tested for being a placeholder for a location property
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1621
     * @param  locationProperty the actual value for the location property
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1622
     * @return locationProperty if placeholder is indeed a placeholder for a location property, the placeholder otherwise
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1623
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1624
    public static Object replaceLocationPropertyPlaceholder(final Object placeholder, final Object locationProperty) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1625
        return isLocationPropertyPlaceholder(placeholder) ? locationProperty : placeholder;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1626
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1627
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1628
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1629
     * Called from runtime internals to check if the passed value is a location property placeholder.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1630
     * @param  placeholder the value tested for being a placeholder for a location property
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1631
     * @return true if the value is a placeholder, false otherwise.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1632
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1633
    public static boolean isLocationPropertyPlaceholder(final Object placeholder) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1634
        return placeholder == LOCATION_PROPERTY_PLACEHOLDER;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1635
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1636
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22389
diff changeset
  1637
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1638
     * Create a new RegExp object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1639
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1640
     * @param expression Regular expression.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1641
     * @param options    Search options.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1642
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1643
     * @return New RegExp object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1644
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1645
    public static Object newRegExp(final String expression, final String options) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1646
        if (options == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1647
            return new NativeRegExp(expression);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1648
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1649
        return new NativeRegExp(expression, options);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1650
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1651
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1652
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1653
     * Get the object prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1654
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1655
     * @return the object prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1656
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1657
    public static ScriptObject objectPrototype() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1658
        return Global.instance().getObjectPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1659
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1660
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1661
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1662
     * Create a new empty object instance.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1663
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1664
     * @return New empty object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1665
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1666
    public static ScriptObject newEmptyInstance() {
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  1667
        return Global.instance().newObject();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1668
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1669
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1670
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1671
     * Check if a given object is a ScriptObject, raises an exception if this is
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1672
     * not the case
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1673
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1674
     * @param obj and object to check
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1675
     * @return the script object
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1676
     */
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1677
    public static ScriptObject checkObject(final Object obj) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1678
        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
  1679
            throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1680
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  1681
        return (ScriptObject)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1682
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1683
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1684
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1685
     * ECMA 9.10 - implementation of CheckObjectCoercible, i.e. raise an exception
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1686
     * if this object is null or undefined.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1687
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1688
     * @param obj an object to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1689
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1690
    public static void checkObjectCoercible(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1691
        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
  1692
            throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1693
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1694
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1695
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1696
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1697
     * Get the current split state.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1698
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1699
     * @return current split state
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1700
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1701
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1702
    public int getSplitState() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1703
        return splitState;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1704
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1705
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1706
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1707
     * Set the current split state.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1708
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1709
     * @param state current split state
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1710
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1711
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1712
    public void setSplitState(final int state) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1713
        splitState = state;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1714
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1715
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1716
    /**
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1717
     * Return the ES6 global scope for lexically declared bindings.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1718
     * @return the ES6 lexical global scope.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1719
     */
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1720
    public final ScriptObject getLexicalScope() {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1721
        assert context.getEnv()._es6;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1722
        return lexicalScope;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1723
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1724
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1725
    @Override
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1726
    public void addBoundProperties(final ScriptObject source, final jdk.nashorn.internal.runtime.Property[] properties) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1727
        PropertyMap ownMap = getMap();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1728
        LexicalScope lexicalScope = null;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1729
        PropertyMap lexicalMap = null;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1730
        boolean hasLexicalDefinitions = false;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1731
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1732
        if (context.getEnv()._es6) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1733
            lexicalScope = (LexicalScope) getLexicalScope();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1734
            lexicalMap = lexicalScope.getMap();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1735
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1736
            for (final jdk.nashorn.internal.runtime.Property property : properties) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1737
                if (property.isLexicalBinding()) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1738
                    hasLexicalDefinitions = true;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1739
                }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1740
                // ES6 15.1.8 steps 6. and 7.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1741
                final jdk.nashorn.internal.runtime.Property globalProperty = ownMap.findProperty(property.getKey());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1742
                if (globalProperty != null && !globalProperty.isConfigurable() && property.isLexicalBinding()) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1743
                    throw ECMAErrors.syntaxError("redeclare.variable", property.getKey());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1744
                }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1745
                final jdk.nashorn.internal.runtime.Property lexicalProperty = lexicalMap.findProperty(property.getKey());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1746
                if (lexicalProperty != null && !property.isConfigurable()) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1747
                    throw ECMAErrors.syntaxError("redeclare.variable", property.getKey());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1748
                }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1749
            }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1750
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1751
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1752
        for (final jdk.nashorn.internal.runtime.Property property : properties) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1753
            if (property.isLexicalBinding()) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1754
                assert lexicalScope != null;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1755
                lexicalMap = lexicalScope.addBoundProperty(lexicalMap, source, property);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1756
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1757
                if (ownMap.findProperty(property.getKey()) != null) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1758
                    // If property exists in the global object invalidate any global constant call sites.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1759
                    invalidateGlobalConstant(property.getKey());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1760
                }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1761
            } else {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1762
                ownMap = addBoundProperty(ownMap, source, property);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1763
            }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1764
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1765
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1766
        setMap(ownMap);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1767
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1768
        if (hasLexicalDefinitions) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1769
            lexicalScope.setMap(lexicalMap);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1770
            invalidateLexicalSwitchPoint();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1771
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1772
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1773
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1774
    @Override
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1775
    public GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1776
        final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1777
        final boolean isScope = NashornCallSiteDescriptor.isScope(desc);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1778
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1779
        if (lexicalScope != null && isScope && !NashornCallSiteDescriptor.isApplyToCall(desc)) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1780
            if (lexicalScope.hasOwnProperty(name)) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1781
                return lexicalScope.findGetMethod(desc, request, operator);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1782
            }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1783
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1784
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1785
        final GuardedInvocation invocation =  super.findGetMethod(desc, request, operator);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1786
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1787
        // We want to avoid adding our generic lexical scope switchpoint to global constant invocations,
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1788
        // because those are invalidated per-key in the addBoundProperties method above.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1789
        // We therefor check if the invocation does already have a switchpoint and the property is non-inherited,
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1790
        // assuming this only applies to global constants. If other non-inherited properties will
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1791
        // start using switchpoints some time in the future we'll have to revisit this.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1792
        if (isScope && context.getEnv()._es6 && (invocation.getSwitchPoints() == null || !hasOwnProperty(name))) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1793
            return invocation.addSwitchPoint(getLexicalScopeSwitchPoint());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1794
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1795
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1796
        return invocation;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1797
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1798
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1799
    @Override
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1800
    public GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1801
        final boolean isScope = NashornCallSiteDescriptor.isScope(desc);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1802
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1803
        if (lexicalScope != null && isScope) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1804
            final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1805
            if (lexicalScope.hasOwnProperty(name)) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1806
                return lexicalScope.findSetMethod(desc, request);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1807
            }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1808
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1809
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1810
        final GuardedInvocation invocation = super.findSetMethod(desc, request);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1811
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1812
        if (isScope && context.getEnv()._es6) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1813
            return invocation.addSwitchPoint(getLexicalScopeSwitchPoint());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1814
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1815
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1816
        return invocation;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1817
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1818
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1819
    private synchronized SwitchPoint getLexicalScopeSwitchPoint() {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1820
        SwitchPoint switchPoint = lexicalScopeSwitchPoint;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1821
        if (switchPoint == null || switchPoint.hasBeenInvalidated()) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1822
            switchPoint = lexicalScopeSwitchPoint = new SwitchPoint();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1823
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1824
        return switchPoint;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1825
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1826
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1827
    private synchronized void invalidateLexicalSwitchPoint() {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1828
        if (lexicalScopeSwitchPoint != null) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1829
            context.getLogger(GlobalConstants.class).info("Invalidating non-constant globals on lexical scope update");
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1830
            SwitchPoint.invalidateAll(new SwitchPoint[]{ lexicalScopeSwitchPoint });
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1831
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1832
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1833
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1834
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1835
    @SuppressWarnings("unused")
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1836
    private static Object lexicalScopeFilter(final Object self) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1837
        if (self instanceof Global) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1838
            return ((Global) self).getLexicalScope();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1839
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1840
        return self;
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1841
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1842
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1843
    private <T extends ScriptObject> T initConstructorAndSwitchPoint(final String name, final Class<T> clazz) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1844
        final T func = initConstructor(name, clazz);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1845
        tagBuiltinProperties(name, func);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1846
        return func;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1847
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1848
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27102
diff changeset
  1849
    private void init(final ScriptEngine eng) {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16181
diff changeset
  1850
        assert Context.getGlobal() == this : "this global is not set as current";
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16181
diff changeset
  1851
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  1852
        final ScriptEnvironment env = getContext().getEnv();
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  1853
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1854
        // initialize Function and Object constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1855
        initFunctionAndObject();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1856
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1857
        // Now fix Global's own proto.
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  1858
        this.setInitialProto(getObjectPrototype());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1859
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1860
        // initialize global function properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1861
        this.eval = this.builtinEval = ScriptFunctionImpl.makeFunction("eval", EVAL);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1862
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1863
        this.parseInt = ScriptFunctionImpl.makeFunction("parseInt",   GlobalFunctions.PARSEINT,
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1864
                    new Specialization[] {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1865
                    new Specialization(GlobalFunctions.PARSEINT_Z),
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1866
                    new Specialization(GlobalFunctions.PARSEINT_I),
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1867
                    new Specialization(GlobalFunctions.PARSEINT_J),
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1868
                    new Specialization(GlobalFunctions.PARSEINT_OI),
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1869
                    new Specialization(GlobalFunctions.PARSEINT_O) });
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1870
        this.parseFloat = ScriptFunctionImpl.makeFunction("parseFloat", GlobalFunctions.PARSEFLOAT);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1871
        this.isNaN = ScriptFunctionImpl.makeFunction("isNaN",   GlobalFunctions.IS_NAN,
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1872
                   new Specialization[] {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1873
                        new Specialization(GlobalFunctions.IS_NAN_I),
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1874
                        new Specialization(GlobalFunctions.IS_NAN_J),
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1875
                        new Specialization(GlobalFunctions.IS_NAN_D) });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1876
        this.parseFloat         = ScriptFunctionImpl.makeFunction("parseFloat", GlobalFunctions.PARSEFLOAT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1877
        this.isNaN              = ScriptFunctionImpl.makeFunction("isNaN",      GlobalFunctions.IS_NAN);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1878
        this.isFinite           = ScriptFunctionImpl.makeFunction("isFinite",   GlobalFunctions.IS_FINITE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1879
        this.encodeURI          = ScriptFunctionImpl.makeFunction("encodeURI",  GlobalFunctions.ENCODE_URI);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1880
        this.encodeURIComponent = ScriptFunctionImpl.makeFunction("encodeURIComponent", GlobalFunctions.ENCODE_URICOMPONENT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1881
        this.decodeURI          = ScriptFunctionImpl.makeFunction("decodeURI",  GlobalFunctions.DECODE_URI);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1882
        this.decodeURIComponent = ScriptFunctionImpl.makeFunction("decodeURIComponent", GlobalFunctions.DECODE_URICOMPONENT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1883
        this.escape             = ScriptFunctionImpl.makeFunction("escape",     GlobalFunctions.ESCAPE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1884
        this.unescape           = ScriptFunctionImpl.makeFunction("unescape",   GlobalFunctions.UNESCAPE);
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
  1885
        this.print              = ScriptFunctionImpl.makeFunction("print",      env._print_no_newline ? PRINT : PRINTLN);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1886
        this.load               = ScriptFunctionImpl.makeFunction("load",       LOAD);
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  1887
        this.loadWithNewGlobal  = ScriptFunctionImpl.makeFunction("loadWithNewGlobal", LOAD_WITH_NEW_GLOBAL);
16219
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1888
        this.exit               = ScriptFunctionImpl.makeFunction("exit",       EXIT);
ccf1c34f26d9 8007629: Remove extraneous quit from shell.js
jlaskey
parents: 16217
diff changeset
  1889
        this.quit               = ScriptFunctionImpl.makeFunction("quit",       EXIT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1890
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1891
        // built-in constructors
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1892
        this.builtinArray     = initConstructorAndSwitchPoint("Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1893
        this.builtinBoolean   = initConstructorAndSwitchPoint("Boolean", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1894
        this.builtinDate      = initConstructorAndSwitchPoint("Date", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1895
        this.builtinJSON      = initConstructorAndSwitchPoint("JSON", ScriptObject.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1896
        this.builtinJSAdapter = initConstructorAndSwitchPoint("JSAdapter", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1897
        this.builtinMath      = initConstructorAndSwitchPoint("Math", ScriptObject.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1898
        this.builtinNumber    = initConstructorAndSwitchPoint("Number", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1899
        this.builtinRegExp    = initConstructorAndSwitchPoint("RegExp", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  1900
        this.builtinString    = initConstructorAndSwitchPoint("String", ScriptFunction.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1901
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1902
        // initialize String.prototype.length to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1903
        // add String.prototype.length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1904
        final ScriptObject stringPrototype = getStringPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1905
        stringPrototype.addOwnProperty("length", Attribute.NON_ENUMERABLE_CONSTANT, 0.0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1906
21439
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 20933
diff changeset
  1907
        // set isArray flag on Array.prototype
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1908
        final ScriptObject arrayPrototype = getArrayPrototype();
21439
31c57355a4a7 8026805: Array.prototype.length doesn't work as expected
hannesw
parents: 20933
diff changeset
  1909
        arrayPrototype.setIsArray();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1910
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  1911
        this.DEFAULT_DATE = new NativeDate(Double.NaN, this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1912
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1913
        // initialize default regexp object
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  1914
        this.DEFAULT_REGEXP = new NativeRegExp("(?:)", this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1915
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1916
        // RegExp.prototype should behave like a RegExp object. So copy the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1917
        // properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1918
        final ScriptObject regExpProto = getRegExpPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1919
        regExpProto.addBoundProperties(DEFAULT_REGEXP);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1920
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1921
        // Error stuff
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1922
        initErrorObjects();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1923
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1924
        // java access
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1925
        if (! env._no_java) {
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1926
            initJavaAccess();
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1927
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1928
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1929
        if (! env._no_typed_arrays) {
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1930
            initTypedArray();
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1931
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1932
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
  1933
        if (env._scripting) {
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  1934
            initScripting(env);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1935
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1936
22389
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1937
        if (Context.DEBUG) {
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1938
            boolean debugOkay;
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1939
            final SecurityManager sm = System.getSecurityManager();
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1940
            if (sm != null) {
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1941
                try {
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1942
                    sm.checkPermission(new RuntimePermission(Context.NASHORN_DEBUG_MODE));
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1943
                    debugOkay = true;
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1944
                } catch (final SecurityException ignored) {
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1945
                    // if no permission, don't initialize Debug object
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1946
                    debugOkay = false;
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1947
                }
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1948
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1949
            } else {
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1950
                debugOkay = true;
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1951
            }
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1952
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1953
            if (debugOkay) {
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1954
                initDebug();
ea3dda90768c 8032060: PropertyMap of Error objects is not stable
sundar
parents: 22382
diff changeset
  1955
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1956
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1957
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1958
        copyBuiltins();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1959
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1960
        // expose script (command line) arguments as "arguments" property of global
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1961
        arguments = wrapAsObject(env.getArguments().toArray());
16262
75513555e603 8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents: 16258
diff changeset
  1962
        if (env._scripting) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1963
            // synonym for "arguments" in scripting mode
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1964
            addOwnProperty("$ARG", Attribute.NOT_ENUMERABLE, arguments);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1965
        }
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1966
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27102
diff changeset
  1967
        if (eng != null) {
25585
0968d8ab046a 8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents: 25423
diff changeset
  1968
            // default file name
0968d8ab046a 8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents: 25423
diff changeset
  1969
            addOwnProperty(ScriptEngine.FILENAME, Attribute.NOT_ENUMERABLE, null);
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1970
            // __noSuchProperty__ hook for ScriptContext search of missing variables
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1971
            final ScriptFunction noSuchProp = ScriptFunctionImpl.makeStrictFunction(NO_SUCH_PROPERTY_NAME, NO_SUCH_PROPERTY);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  1972
            addOwnProperty(NO_SUCH_PROPERTY_NAME, Attribute.NOT_ENUMERABLE, noSuchProp);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1973
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1974
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1975
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1976
    private void initErrorObjects() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1977
        // Error objects
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  1978
        this.builtinError = initConstructor("Error", ScriptFunction.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1979
        final ScriptObject errorProto = getErrorPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1980
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1981
        // Nashorn specific accessors on Error.prototype - stack, lineNumber, columnNumber and fileName
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1982
        final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", NativeError.GET_STACK);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1983
        final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", NativeError.SET_STACK);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1984
        errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1985
        final ScriptFunction getLineNumber = ScriptFunctionImpl.makeFunction("getLineNumber", NativeError.GET_LINENUMBER);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1986
        final ScriptFunction setLineNumber = ScriptFunctionImpl.makeFunction("setLineNumber", NativeError.SET_LINENUMBER);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1987
        errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1988
        final ScriptFunction getColumnNumber = ScriptFunctionImpl.makeFunction("getColumnNumber", NativeError.GET_COLUMNNUMBER);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1989
        final ScriptFunction setColumnNumber = ScriptFunctionImpl.makeFunction("setColumnNumber", NativeError.SET_COLUMNNUMBER);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1990
        errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1991
        final ScriptFunction getFileName = ScriptFunctionImpl.makeFunction("getFileName", NativeError.GET_FILENAME);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1992
        final ScriptFunction setFileName = ScriptFunctionImpl.makeFunction("setFileName", NativeError.SET_FILENAME);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1993
        errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1994
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1995
        // ECMA 15.11.4.2 Error.prototype.name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1996
        // Error.prototype.name = "Error";
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26647
diff changeset
  1997
        errorProto.set(NativeError.NAME, "Error", 0);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1998
        // ECMA 15.11.4.3 Error.prototype.message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1999
        // Error.prototype.message = "";
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26647
diff changeset
  2000
        errorProto.set(NativeError.MESSAGE, "", 0);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2001
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2002
        tagBuiltinProperties("Error", builtinError);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2003
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2004
        this.builtinEvalError = initErrorSubtype("EvalError", errorProto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2005
        this.builtinRangeError = initErrorSubtype("RangeError", errorProto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2006
        this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2007
        this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2008
        this.builtinTypeError = initErrorSubtype("TypeError", errorProto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2009
        this.builtinURIError = initErrorSubtype("URIError", errorProto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2010
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2011
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2012
    private ScriptFunction initErrorSubtype(final String name, final ScriptObject errorProto) {
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2013
        final ScriptFunction cons = initConstructor(name, ScriptFunction.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2014
        final ScriptObject prototype = ScriptFunction.getPrototype(cons);
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26647
diff changeset
  2015
        prototype.set(NativeError.NAME, name, 0);
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26647
diff changeset
  2016
        prototype.set(NativeError.MESSAGE, "", 0);
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2017
        prototype.setInitialProto(errorProto);
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2018
        tagBuiltinProperties(name, cons);
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2019
        return cons;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2020
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2021
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2022
    private void initJavaAccess() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2023
        final ScriptObject objectProto = getObjectPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2024
        this.builtinPackages = new NativeJavaPackage("", objectProto);
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2025
        this.builtinCom = new NativeJavaPackage("com", objectProto);
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2026
        this.builtinEdu = new NativeJavaPackage("edu", objectProto);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2027
        this.builtinJava = new NativeJavaPackage("java", objectProto);
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2028
        this.builtinJavafx = new NativeJavaPackage("javafx", objectProto);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2029
        this.builtinJavax = new NativeJavaPackage("javax", objectProto);
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2030
        this.builtinOrg = new NativeJavaPackage("org", objectProto);
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2031
        this.builtinJavaImporter = initConstructor("JavaImporter", ScriptFunction.class);
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2032
        this.builtinJavaApi = initConstructor("Java", ScriptObject.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2033
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2034
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18851
diff changeset
  2035
    private void initScripting(final ScriptEnvironment scriptEnv) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2036
        Object value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2037
        value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2038
        addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2039
16196
58f6f046bb5e 8006983: Introduce a command line option to switch off syntactic extensions of nashorn
sundar
parents: 16195
diff changeset
  2040
        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
  2041
        addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2042
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
  2043
        final String execName = ScriptingFunctions.EXEC_NAME;
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
  2044
        value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
  2045
        addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
  2046
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2047
        // Nashorn extension: global.echo (scripting-mode-only)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2048
        // alias for "print"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2049
        value = get("print");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2050
        addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2051
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2052
        // Nashorn extension: global.$OPTIONS (scripting-mode-only)
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  2053
        final ScriptObject options = newObject();
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16277
diff changeset
  2054
        copyOptions(options, scriptEnv);
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16277
diff changeset
  2055
        addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
  2056
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
  2057
        // Nashorn extension: global.$ENV (scripting-mode-only)
16213
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2058
        if (System.getSecurityManager() == null) {
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2059
            // 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
  2060
            // Retrieve current state of ENV variables.
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
  2061
            final ScriptObject env = newObject();
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19459
diff changeset
  2062
            env.putAll(System.getenv(), scriptEnv._strict);
16213
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2063
            addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2064
        } else {
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2065
            addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2066
        }
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2067
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2068
        // add other special properties for exec support
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2069
        addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2070
        addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
66cb726a76d5 8007521: $ENV should be undefined when security manager is present
sundar
parents: 16211
diff changeset
  2071
        addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2072
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2073
16525
1409942e618e 8009982: Lazy execution bugfix. Added lazy sunspider unit test. Added mandreel to compile-octane test. Fixed warnings
lagergren
parents: 16522
diff changeset
  2074
    private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
  2075
        for (final Field f : scriptEnv.getClass().getFields()) {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19456
diff changeset
  2076
            try {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26647
diff changeset
  2077
                options.set(f.getName(), f.get(scriptEnv), 0);
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19456
diff changeset
  2078
            } catch (final IllegalArgumentException | IllegalAccessException exp) {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19456
diff changeset
  2079
                throw new RuntimeException(exp);
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16277
diff changeset
  2080
            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19456
diff changeset
  2081
        }
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16277
diff changeset
  2082
    }
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16277
diff changeset
  2083
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2084
    private void initTypedArray() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2085
        this.builtinArrayBuffer       = initConstructorAndSwitchPoint("ArrayBuffer", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2086
        this.builtinDataView          = initConstructorAndSwitchPoint("DataView", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2087
        this.builtinInt8Array         = initConstructorAndSwitchPoint("Int8Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2088
        this.builtinUint8Array        = initConstructorAndSwitchPoint("Uint8Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2089
        this.builtinUint8ClampedArray = initConstructorAndSwitchPoint("Uint8ClampedArray", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2090
        this.builtinInt16Array        = initConstructorAndSwitchPoint("Int16Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2091
        this.builtinUint16Array       = initConstructorAndSwitchPoint("Uint16Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2092
        this.builtinInt32Array        = initConstructorAndSwitchPoint("Int32Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2093
        this.builtinUint32Array       = initConstructorAndSwitchPoint("Uint32Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2094
        this.builtinFloat32Array      = initConstructorAndSwitchPoint("Float32Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2095
        this.builtinFloat64Array      = initConstructorAndSwitchPoint("Float64Array", ScriptFunction.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2096
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2097
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2098
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2099
    private void copyBuiltins() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2100
        this.array             = this.builtinArray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2101
        this._boolean          = this.builtinBoolean;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2102
        this.date              = this.builtinDate;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2103
        this.error             = this.builtinError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2104
        this.evalError         = this.builtinEvalError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2105
        this.function          = this.builtinFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2106
        this.jsadapter         = this.builtinJSAdapter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2107
        this.json              = this.builtinJSON;
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2108
        this.com               = this.builtinCom;
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2109
        this.edu               = this.builtinEdu;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2110
        this.java              = this.builtinJava;
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2111
        this.javafx            = this.builtinJavafx;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2112
        this.javax             = this.builtinJavax;
16532
fe016e9cae59 8010709: org on the top level doesn't resolve
sundar
parents: 16525
diff changeset
  2113
        this.org               = this.builtinOrg;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2114
        this.javaImporter      = this.builtinJavaImporter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2115
        this.javaApi           = this.builtinJavaApi;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2116
        this.math              = this.builtinMath;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2117
        this.number            = this.builtinNumber;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2118
        this.object            = this.builtinObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2119
        this.packages          = this.builtinPackages;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2120
        this.rangeError        = this.builtinRangeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2121
        this.referenceError    = this.builtinReferenceError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2122
        this.regexp            = this.builtinRegExp;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2123
        this.string            = this.builtinString;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2124
        this.syntaxError       = this.builtinSyntaxError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2125
        this.typeError         = this.builtinTypeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2126
        this.uriError          = this.builtinURIError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2127
        this.arrayBuffer       = this.builtinArrayBuffer;
23374
f470afc89c6c 8015958: DataView constructor is not defined
sundar
parents: 23372
diff changeset
  2128
        this.dataView          = this.builtinDataView;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2129
        this.int8Array         = this.builtinInt8Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2130
        this.uint8Array        = this.builtinUint8Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2131
        this.uint8ClampedArray = this.builtinUint8ClampedArray;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2132
        this.int16Array        = this.builtinInt16Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2133
        this.uint16Array       = this.builtinUint16Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2134
        this.int32Array        = this.builtinInt32Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2135
        this.uint32Array       = this.builtinUint32Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2136
        this.float32Array      = this.builtinFloat32Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2137
        this.float64Array      = this.builtinFloat64Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2138
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2139
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2140
    private void initDebug() {
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2141
        this.addOwnProperty("Debug", Attribute.NOT_ENUMERABLE, initConstructor("Debug", ScriptObject.class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2142
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2143
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  2144
    private Object printImpl(final boolean newLine, final Object... objects) {
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25585
diff changeset
  2145
        @SuppressWarnings("resource")
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  2146
        final PrintWriter out = scontext != null? new PrintWriter(scontext.getWriter()) : getContext().getEnv().getOut();
17972
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2147
        final StringBuilder sb = new StringBuilder();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2148
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25585
diff changeset
  2149
        for (final Object obj : objects) {
17972
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2150
            if (sb.length() != 0) {
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2151
                sb.append(' ');
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2152
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2153
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25585
diff changeset
  2154
            sb.append(JSType.toString(obj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2155
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2156
17972
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2157
        // Print all at once to ensure thread friendly result.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2158
        if (newLine) {
17972
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2159
            out.println(sb.toString());
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2160
        } else {
2ed196cf0643 8015727: Thread safe print function
jlaskey
parents: 17524
diff changeset
  2161
            out.print(sb.toString());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2162
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2163
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2164
        out.flush();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2165
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2166
        return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2167
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2168
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2169
    private <T extends ScriptObject> T initConstructor(final String name, final Class<T> clazz) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2170
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2171
            // Assuming class name pattern for built-in JS constructors.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2172
            final StringBuilder sb = new StringBuilder("jdk.nashorn.internal.objects.");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2173
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2174
            sb.append("Native");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2175
            sb.append(name);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2176
            sb.append("$Constructor");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2177
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2178
            final Class<?> funcClass = Class.forName(sb.toString());
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2179
            final T res = clazz.cast(funcClass.newInstance());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2180
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2181
            if (res instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2182
                // All global constructor prototypes are not-writable,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2183
                // not-enumerable and not-configurable.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2184
                final ScriptFunction func = (ScriptFunction)res;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2185
                func.modifyOwnProperty(func.getProperty("prototype"), Attribute.NON_ENUMERABLE_CONSTANT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2186
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2187
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2188
            if (res.getProto() == null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2189
                res.setInitialProto(getObjectPrototype());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2190
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2191
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
  2192
            res.setIsBuiltin();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2193
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2194
            return res;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2195
        } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2196
            throw new RuntimeException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2197
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2198
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2199
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2200
    private List<jdk.nashorn.internal.runtime.Property> extractBuiltinProperties(final String name, final ScriptObject func) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2201
        final List<jdk.nashorn.internal.runtime.Property> list = new ArrayList<>();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2202
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2203
        list.addAll(Arrays.asList(func.getMap().getProperties()));
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2204
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2205
        if (func instanceof ScriptFunction) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2206
            final ScriptObject proto = ScriptFunction.getPrototype((ScriptFunction)func);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2207
            if (proto != null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2208
                list.addAll(Arrays.asList(proto.getMap().getProperties()));
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2209
            }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2210
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2211
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2212
        final jdk.nashorn.internal.runtime.Property prop = getProperty(name);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2213
        if (prop != null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2214
            list.add(prop);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2215
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2216
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2217
        return list;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2218
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2219
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2220
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2221
     * Given a builtin object, traverse its properties recursively and associate them with a name that
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2222
     * will be a key to their invalidation switchpoint.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2223
     * @param name name for key
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2224
     * @param func builtin script object
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2225
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2226
    private void tagBuiltinProperties(final String name, final ScriptObject func) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2227
        SwitchPoint sp = context.getBuiltinSwitchPoint(name);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2228
        if (sp == null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2229
            sp = context.newBuiltinSwitchPoint(name);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2230
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2231
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2232
        //get all builtin properties in this builtin object and register switchpoints keyed on the propery name,
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2233
        //one overwrite destroys all for now, e.g. Function.prototype.apply = 17; also destroys Function.prototype.call etc
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2234
        for (final jdk.nashorn.internal.runtime.Property prop : extractBuiltinProperties(name, func)) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2235
            prop.setBuiltinSwitchPoint(sp);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2236
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2237
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2238
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2239
    // Function and Object constructors are inter-dependent. Also,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2240
    // Function.prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2241
    // functions are not properly initialized. We fix the references here.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2242
    // NOTE: be careful if you want to re-order the operations here. You may
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2243
    // have
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2244
    // to play with object references carefully!!
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2245
    private void initFunctionAndObject() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2246
        // First-n-foremost is Function
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2247
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2248
        this.builtinFunction = initConstructor("Function", ScriptFunction.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2249
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2250
        // create global anonymous function
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
  2251
        final ScriptFunction anon = ScriptFunctionImpl.newAnonymousFunction();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2252
        // need to copy over members of Function.prototype to anon function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2253
        anon.addBoundProperties(getFunctionPrototype());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2254
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2255
        // Function.prototype === Object.getPrototypeOf(Function) ===
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2256
        // <anon-function>
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2257
        builtinFunction.setInitialProto(anon);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2258
        builtinFunction.setPrototype(anon);
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 26647
diff changeset
  2259
        anon.set("constructor", builtinFunction, 0);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2260
        anon.deleteOwnProperty(anon.getMap().findProperty("prototype"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2261
22382
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  2262
        // use "getter" so that [[ThrowTypeError]] function's arity is 0 - as specified in step 10 of section 13.2.3
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
  2263
        this.typeErrorThrower = new ScriptFunctionImpl("TypeErrorThrower", Lookup.TYPE_ERROR_THROWER_GETTER, null, null, 0);
22382
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  2264
        typeErrorThrower.setPrototype(UNDEFINED);
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  2265
        // Non-constructor built-in functions do not have "prototype" property
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  2266
        typeErrorThrower.deleteOwnProperty(typeErrorThrower.getMap().findProperty("prototype"));
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  2267
        typeErrorThrower.preventExtensions();
2abc7240ef59 8029612: the typeErrorThrower field in ScriptFunctionImpl cannot be static and common to all Globals
sundar
parents: 21686
diff changeset
  2268
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2269
        // now initialize Object
26056
a64f0cd60e66 8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
sundar
parents: 25821
diff changeset
  2270
        this.builtinObject = initConstructor("Object", ScriptFunction.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2271
        final ScriptObject ObjectPrototype = getObjectPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2272
        // Object.getPrototypeOf(Function.prototype) === Object.prototype
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2273
        anon.setInitialProto(ObjectPrototype);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2274
24281
58ed42a1ebc6 8042364: Make __proto__ ES6 draft compliant
sundar
parents: 23375
diff changeset
  2275
        // ES6 draft compliant __proto__ property of Object.prototype
58ed42a1ebc6 8042364: Make __proto__ ES6 draft compliant
sundar
parents: 23375
diff changeset
  2276
        // accessors on Object.prototype for "__proto__"
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
  2277
        final ScriptFunction getProto = ScriptFunctionImpl.makeFunction("getProto", NativeObject.GET__PROTO__);
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
  2278
        final ScriptFunction setProto = ScriptFunctionImpl.makeFunction("setProto", NativeObject.SET__PROTO__);
24281
58ed42a1ebc6 8042364: Make __proto__ ES6 draft compliant
sundar
parents: 23375
diff changeset
  2279
        ObjectPrototype.addOwnProperty("__proto__", Attribute.NOT_ENUMERABLE, getProto, setProto);
58ed42a1ebc6 8042364: Make __proto__ ES6 draft compliant
sundar
parents: 23375
diff changeset
  2280
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2281
        // Function valued properties of Function.prototype were not properly
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2282
        // initialized. Because, these were created before global.function and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2283
        // global.object were not initialized.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2284
        jdk.nashorn.internal.runtime.Property[] properties = getFunctionPrototype().getMap().getProperties();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2285
        for (final jdk.nashorn.internal.runtime.Property property : properties) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2286
            final Object key = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2287
            final Object value = builtinFunction.get(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2288
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2289
            if (value instanceof ScriptFunction && value != anon) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2290
                final ScriptFunction func = (ScriptFunction)value;
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2291
                func.setInitialProto(getFunctionPrototype());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2292
                final ScriptObject prototype = ScriptFunction.getPrototype(func);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2293
                if (prototype != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2294
                    prototype.setInitialProto(ObjectPrototype);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2295
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2296
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2297
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2298
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2299
        // For function valued properties of Object and Object.prototype, make
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2300
        // sure prototype's proto chain ends with Object.prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2301
        for (final jdk.nashorn.internal.runtime.Property property : builtinObject.getMap().getProperties()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2302
            final Object key = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2303
            final Object value = builtinObject.get(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2304
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2305
            if (value instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2306
                final ScriptFunction func = (ScriptFunction)value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2307
                final ScriptObject prototype = ScriptFunction.getPrototype(func);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2308
                if (prototype != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2309
                    prototype.setInitialProto(ObjectPrototype);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2310
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2311
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2312
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2313
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2314
        properties = getObjectPrototype().getMap().getProperties();
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
  2315
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2316
        for (final jdk.nashorn.internal.runtime.Property property : properties) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2317
            final Object key   = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2318
            if (key.equals("constructor")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2319
                continue;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2320
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2321
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 25421
diff changeset
  2322
            final Object value = ObjectPrototype.get(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2323
            if (value instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2324
                final ScriptFunction func = (ScriptFunction)value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2325
                final ScriptObject prototype = ScriptFunction.getPrototype(func);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2326
                if (prototype != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 22389
diff changeset
  2327
                    prototype.setInitialProto(ObjectPrototype);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2328
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2329
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2330
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2331
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2332
        tagBuiltinProperties("Object", builtinObject);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2333
        tagBuiltinProperties("Function", builtinFunction);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26765
diff changeset
  2334
        tagBuiltinProperties("Function", anon);
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
  2335
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
  2336
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
  2337
    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
  2338
        return MH.findStatic(MethodHandles.lookup(), Global.class, name, MH.type(rtype, types));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2339
    }
16223
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
  2340
16258
0e25f785df4d 8008093: Make RegExp engine pluggable
hannesw
parents: 16256
diff changeset
  2341
    RegExpResult getLastRegExpResult() {
0e25f785df4d 8008093: Make RegExp engine pluggable
hannesw
parents: 16256
diff changeset
  2342
        return lastRegExpResult;
16223
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
  2343
    }
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
  2344
16258
0e25f785df4d 8008093: Make RegExp engine pluggable
hannesw
parents: 16256
diff changeset
  2345
    void setLastRegExpResult(final RegExpResult regExpResult) {
0e25f785df4d 8008093: Make RegExp engine pluggable
hannesw
parents: 16256
diff changeset
  2346
        this.lastRegExpResult = regExpResult;
16223
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
  2347
    }
dff592a332a4 8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents: 16219
diff changeset
  2348
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
  2349
    @Override
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
  2350
    protected boolean isGlobal() {
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
  2351
        return true;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
  2352
    }
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2353
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2354
    /**
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2355
     * A class representing the ES6 global lexical scope.
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2356
     */
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2357
    private static class LexicalScope extends ScriptObject {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2358
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2359
        LexicalScope(final ScriptObject proto) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2360
            super(proto, PropertyMap.newMap());
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2361
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2362
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2363
        @Override
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2364
        protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2365
            return filterInvocation(super.findGetMethod(desc, request, operator));
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2366
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2367
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2368
        @Override
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2369
        protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2370
            return filterInvocation(super.findSetMethod(desc, request));
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2371
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2372
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2373
        @Override
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2374
        protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final jdk.nashorn.internal.runtime.Property property) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2375
            // We override this method just to make it callable by Global
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2376
            return super.addBoundProperty(propMap, source, property);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2377
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2378
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2379
        private static GuardedInvocation filterInvocation(final GuardedInvocation invocation) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2380
            final MethodType type = invocation.getInvocation().type();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2381
            return invocation.asType(type.changeParameterType(0, Object.class)).filterArguments(0, LEXICAL_SCOPE_FILTER);
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2382
        }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2383
    }
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27369
diff changeset
  2384
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  2385
}