nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java
author mhaupt
Wed, 28 Oct 2015 10:54:05 +0100
changeset 33414 2e284c36d51f
parent 33343 23abd10384a5
child 33690 46a1bc24cf2c
permissions -rw-r--r--
8134941: Implement ES6 template literal support Reviewed-by: attila, hannesw Contributed-by: andreas.woess@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
33414
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
     2
 * Copyright (c) 2010, 2015, 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.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
25250
b5a4e0ac31d1 8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents: 25247
diff changeset
    30
import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import static jdk.nashorn.internal.runtime.ECMAErrors.syntaxError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29281
diff changeset
    35
import static jdk.nashorn.internal.runtime.JSType.isString;
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
    36
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.lang.invoke.MethodHandle;
19460
1b6d8e7b1cdf 8022782: publicLookup access failures in ScriptObject, ScriptFunction and ScriptFunction
sundar
parents: 19097
diff changeset
    38
import java.lang.invoke.MethodHandles;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
    39
import java.lang.invoke.SwitchPoint;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.lang.reflect.Array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import java.util.Collections;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import java.util.Iterator;
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
    43
import java.util.List;
17778
991ccffbeb13 8015459: Octane test run fails on Turkish locale
sundar
parents: 16272
diff changeset
    44
import java.util.Locale;
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
    45
import java.util.Map;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
    46
import java.util.NoSuchElementException;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
import java.util.Objects;
33414
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
    48
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16232
diff changeset
    49
import jdk.internal.dynalink.beans.StaticClass;
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
    50
import jdk.nashorn.api.scripting.JSObject;
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
    51
import jdk.nashorn.api.scripting.ScriptObjectMirror;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
    52
import jdk.nashorn.internal.codegen.ApplySpecialization;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
    53
import jdk.nashorn.internal.codegen.CompilerConstants;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
    55
import jdk.nashorn.internal.ir.debug.JSONWriter;
21446
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
    56
import jdk.nashorn.internal.objects.Global;
25247
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24762
diff changeset
    57
import jdk.nashorn.internal.objects.NativeObject;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
    58
import jdk.nashorn.internal.parser.Lexer;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
import jdk.nashorn.internal.runtime.linker.Bootstrap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
 * Utilities to be called by JavaScript runtime API and generated classes.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
public final class ScriptRuntime {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
    private ScriptRuntime() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
    /** Singleton representing the empty array object '[]' */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    public static final Object[] EMPTY_ARRAY = new Object[0];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    /** Unique instance of undefined. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    public static final Undefined UNDEFINED = Undefined.getUndefined();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
     * Unique instance of undefined used to mark empty array slots.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
     * Can't escape the array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
    public static final Undefined EMPTY = Undefined.getEmpty();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
    /** Method handle to generic + operator, operating on objects */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    public static final Call ADD = staticCallNoLookup(ScriptRuntime.class, "ADD", Object.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    /** Method handle to generic === operator, operating on objects */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
    public static final Call EQ_STRICT = staticCallNoLookup(ScriptRuntime.class, "EQ_STRICT", boolean.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16234
diff changeset
    87
    /** Method handle used to enter a {@code with} scope at runtime. */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
    public static final Call OPEN_WITH = staticCallNoLookup(ScriptRuntime.class, "openWith", ScriptObject.class, ScriptObject.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
     * Method used to place a scope's variable into the Global scope, which has to be done for the
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16234
diff changeset
    92
     * properties declared at outermost script level.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
    public static final Call MERGE_SCOPE = staticCallNoLookup(ScriptRuntime.class, "mergeScope", ScriptObject.class, ScriptObject.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
     * Return an appropriate iterator for the elements in a for-in construct
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
    public static final Call TO_PROPERTY_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toPropertyIterator", Iterator.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
     * Return an appropriate iterator for the elements in a for-each construct
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
    public static final Call TO_VALUE_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toValueIterator", Iterator.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
      * Method handle for apply. Used from {@link ScriptFunction} for looking up calls to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
      * call sites that are known to be megamorphic. Using an invoke dynamic here would
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
      * lead to the JVM deoptimizing itself to death
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
      */
19460
1b6d8e7b1cdf 8022782: publicLookup access failures in ScriptObject, ScriptFunction and ScriptFunction
sundar
parents: 19097
diff changeset
   111
    public static final Call APPLY = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "apply", Object.class, ScriptFunction.class, Object.class, Object[].class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
    /**
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   114
     * Throws a reference error for an undefined variable.
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   115
     */
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   116
    public static final Call THROW_REFERENCE_ERROR = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "throwReferenceError", void.class, String.class);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   117
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   118
    /**
29281
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   119
     * Throws a reference error for an undefined variable.
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   120
     */
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   121
    public static final Call THROW_CONST_TYPE_ERROR = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "throwConstTypeError", void.class, String.class);
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   122
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   123
    /**
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   124
     * Used to invalidate builtin names, e.g "Function" mapping to all properties in Function.prototype and Function.prototype itself.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   125
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   126
    public static final Call INVALIDATE_RESERVED_BUILTIN_NAME = staticCallNoLookup(ScriptRuntime.class, "invalidateReservedBuiltinName", void.class, String.class);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   127
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   128
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
     * Converts a switch tag value to a simple integer. deflt value if it can't.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
     * @param tag   Switch statement tag value.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
     * @param deflt default to use if not convertible.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
     * @return int tag value (or deflt.)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    public static int switchTagAsInt(final Object tag, final int deflt) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
        if (tag instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
            final double d = ((Number)tag).doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
            if (isRepresentableAsInt(d)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
                return (int)d;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
        return deflt;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
     * Converts a switch tag value to a simple integer. deflt value if it can't.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
     * @param tag   Switch statement tag value.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
     * @param deflt default to use if not convertible.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
     * @return int tag value (or deflt.)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
     */
18628
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   152
    public static int switchTagAsInt(final boolean tag, final int deflt) {
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   153
        return deflt;
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   154
    }
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   155
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   156
    /**
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   157
     * Converts a switch tag value to a simple integer. deflt value if it can't.
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   158
     *
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   159
     * @param tag   Switch statement tag value.
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   160
     * @param deflt default to use if not convertible.
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   161
     * @return int tag value (or deflt.)
012cd852f881 8019488: switch on literals result in NoSuchMethodError or VerifyError
sundar
parents: 18614
diff changeset
   162
     */
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    public static int switchTagAsInt(final long tag, final int deflt) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
        return isRepresentableAsInt(tag) ? (int)tag : deflt;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
     * Converts a switch tag value to a simple integer. deflt value if it can't.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     * @param tag   Switch statement tag value.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
     * @param deflt default to use if not convertible.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
     * @return int tag value (or deflt.)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    public static int switchTagAsInt(final double tag, final int deflt) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
        return isRepresentableAsInt(tag) ? (int)tag : deflt;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     * This is the builtin implementation of {@code Object.prototype.toString}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
     * @param self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
     * @return string representation as object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
    public static String builtinObjectToString(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        String className;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
        // Spec tells us to convert primitives by ToObject..
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
        // But we don't need to -- all we need is the right class name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
        // of the corresponding primitive wrapper type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   189
        final JSType type = JSType.ofNoFunction(self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
        switch (type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
        case BOOLEAN:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
            className = "Boolean";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
        case NUMBER:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
            className = "Number";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
        case STRING:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
            className = "String";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
        // special case of null and undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
        case NULL:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
            className = "Null";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        case UNDEFINED:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
            className = "Undefined";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
        case OBJECT:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
            if (self instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
                className = ((ScriptObject)self).getClassName();
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   211
            } else if (self instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   212
                className = ((JSObject)self).getClassName();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
            } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
                className = self.getClass().getName();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
            // Nashorn extension: use Java class name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
            className = self.getClass().getName();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
        final StringBuilder sb = new StringBuilder();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
        sb.append("[object ");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
        sb.append(className);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
        sb.append(']');
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
     * This is called whenever runtime wants to throw an error and wants to provide
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
     * meaningful information about an object. We don't want to call toString which
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
     * ends up calling "toString" from script world which may itself throw error.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
     * When we want to throw an error, we don't additional error from script land
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
     * -- which may sometimes lead to infinite recursion.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
     * @param obj Object to converted to String safely (without calling user script)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
     * @return safe String representation of the given object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
    public static String safeToString(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
        return JSType.toStringImpl(obj, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    /**
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   246
     * Returns an iterator over property identifiers used in the {@code for...in} statement. Note that the ECMAScript
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   247
     * 5.1 specification, chapter 12.6.4. uses the terminology "property names", which seems to imply that the property
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   248
     * identifiers are expected to be strings, but this is not actually spelled out anywhere, and Nashorn will in some
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   249
     * cases deviate from this. Namely, we guarantee to always return an iterator over {@link String} values for any
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   250
     * built-in JavaScript object. We will however return an iterator over {@link Integer} objects for native Java
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   251
     * arrays and {@link List} objects, as well as arbitrary objects representing keys of a {@link Map}. Therefore, the
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   252
     * expression {@code typeof i} within a {@code for(i in obj)} statement can return something other than
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   253
     * {@code string} when iterating over native Java arrays, {@code List}, and {@code Map} objects.
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   254
     * @param obj object to iterate on.
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   255
     * @return iterator over the object's property names.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
     */
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   257
    public static Iterator<?> toPropertyIterator(final Object obj) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
            return ((ScriptObject)obj).propertyIterator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
        if (obj != null && obj.getClass().isArray()) {
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   263
            return new RangeIterator(Array.getLength(obj));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   266
        if (obj instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   267
            return ((JSObject)obj).keySet().iterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   268
        }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   269
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   270
        if (obj instanceof List) {
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   271
            return new RangeIterator(((List<?>)obj).size());
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   272
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   273
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   274
        if (obj instanceof Map) {
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   275
            return ((Map<?,?>)obj).keySet().iterator();
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   276
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   277
21446
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   278
        final Object wrapped = Global.instance().wrapAsObject(obj);
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   279
        if (wrapped instanceof ScriptObject) {
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   280
            return ((ScriptObject)wrapped).propertyIterator();
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   281
        }
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   282
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
        return Collections.emptyIterator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   286
    private static final class RangeIterator implements Iterator<Integer> {
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   287
        private final int length;
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   288
        private int index;
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   289
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   290
        RangeIterator(final int length) {
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   291
            this.length = length;
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   292
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   293
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   294
        @Override
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   295
        public boolean hasNext() {
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   296
            return index < length;
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   297
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   298
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   299
        @Override
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   300
        public Integer next() {
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   301
            return index++;
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   302
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   303
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   304
        @Override
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   305
        public void remove() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   306
            throw new UnsupportedOperationException("remove");
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   307
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   308
    }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   309
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
    /**
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   311
     * Returns an iterator over property values used in the {@code for each...in} statement. Aside from built-in JS
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   312
     * objects, it also operates on Java arrays, any {@link Iterable}, as well as on {@link Map} objects, iterating over
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   313
     * map values.
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   314
     * @param obj object to iterate on.
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   315
     * @return iterator over the object's property values.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
    public static Iterator<?> toValueIterator(final Object obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
            return ((ScriptObject)obj).valueIterator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
        if (obj != null && obj.getClass().isArray()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
            final Object array  = obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
            final int    length = Array.getLength(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
            return new Iterator<Object>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
                private int index = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
                @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
                public boolean hasNext() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
                    return index < length;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
                @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
                public Object next() {
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   336
                    if (index >= length) {
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   337
                        throw new NoSuchElementException();
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   338
                    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
                    return Array.get(array, index++);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
                @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
                public void remove() {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
   344
                    throw new UnsupportedOperationException("remove");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
            };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   349
        if (obj instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   350
            return ((JSObject)obj).values().iterator();
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   351
        }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   352
19618
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   353
        if (obj instanceof Map) {
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   354
            return ((Map<?,?>)obj).values().iterator();
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   355
        }
6b73157185e0 8022903: Enhance for-in and for-each for Lists and Maps
attila
parents: 19460
diff changeset
   356
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
        if (obj instanceof Iterable) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
            return ((Iterable<?>)obj).iterator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
21446
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   361
        final Object wrapped = Global.instance().wrapAsObject(obj);
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   362
        if (wrapped instanceof ScriptObject) {
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   363
            return ((ScriptObject)wrapped).valueIterator();
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   364
        }
2fcb0f7e407f 8026955: for-in should convert primitive values to object
hannesw
parents: 19889
diff changeset
   365
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
        return Collections.emptyIterator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
     * Merge a scope into its prototype's map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
     * Merge a scope into its prototype.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
     * @param scope Scope to merge.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
     * @return prototype object after merge
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
    public static ScriptObject mergeScope(final ScriptObject scope) {
31194
f00cb8259826 8098546: eval within a 'with' leaks definitions into global scope
hannesw
parents: 29283
diff changeset
   377
        final ScriptObject parentScope = scope.getProto();
f00cb8259826 8098546: eval within a 'with' leaks definitions into global scope
hannesw
parents: 29283
diff changeset
   378
        parentScope.addBoundProperties(scope);
f00cb8259826 8098546: eval within a 'with' leaks definitions into global scope
hannesw
parents: 29283
diff changeset
   379
        return parentScope;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
     * Call a function given self and args. If the number of the arguments is known in advance, you can likely achieve
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 31828
diff changeset
   384
     * better performance by creating a dynamic invoker using {@link Bootstrap#createDynamicCallInvoker(Class, Class...)}
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 31828
diff changeset
   385
     * then using its {@link MethodHandle#invokeExact(Object...)} method instead.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
     * @param target ScriptFunction object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
     * @param self   Receiver in call.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
     * @param args   Call arguments.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
     * @return Call result.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    public static Object apply(final ScriptFunction target, final Object self, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
            return target.invoke(self, args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
        } catch (final RuntimeException | Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
            throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
        } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
            throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
    /**
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   403
     * Throws a reference error for an undefined variable.
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   404
     *
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   405
     * @param name the variable name
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   406
     */
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   407
    public static void throwReferenceError(final String name) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   408
        throw referenceError("not.defined", name);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   409
    }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   410
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26068
diff changeset
   411
    /**
29281
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   412
     * Throws a type error for an assignment to a const.
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   413
     *
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   414
     * @param name the const name
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   415
     */
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   416
    public static void throwConstTypeError(final String name) {
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   417
        throw typeError("assign.constant", name);
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   418
    }
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   419
8cc2618a07aa 8073707: const re-assignment should not reported as a early error
hannesw
parents: 29133
diff changeset
   420
    /**
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   421
     * Call a script function as a constructor with given args.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   422
     *
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   423
     * @param target ScriptFunction object.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   424
     * @param args   Call arguments.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   425
     * @return Constructor call result.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   426
     */
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   427
    public static Object construct(final ScriptFunction target, final Object... args) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   428
        try {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   429
            return target.construct(args);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   430
        } catch (final RuntimeException | Error e) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   431
            throw e;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   432
        } catch (final Throwable t) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   433
            throw new RuntimeException(t);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   434
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   435
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   436
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 18334
diff changeset
   437
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
     * Generic implementation of ECMA 9.12 - SameValue algorithm
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
     * @param x first value to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
     * @param y second value to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
     * @return true if both objects have the same value
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
    public static boolean sameValue(final Object x, final Object y) {
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   446
        final JSType xType = JSType.ofNoFunction(x);
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   447
        final JSType yType = JSType.ofNoFunction(y);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
        if (xType != yType) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
        if (xType == JSType.UNDEFINED || xType == JSType.NULL) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
            return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
        if (xType == JSType.NUMBER) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
            final double xVal = ((Number)x).doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
            final double yVal = ((Number)y).doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
            if (Double.isNaN(xVal) && Double.isNaN(yVal)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
            // checking for xVal == -0.0 and yVal == +0.0 or vice versa
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   466
            if (xVal == 0.0 && Double.doubleToLongBits(xVal) != Double.doubleToLongBits(yVal)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
                return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
            return xVal == yVal;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   471
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
        if (xType == JSType.STRING || yType == JSType.BOOLEAN) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
            return x.equals(y);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   477
        return x == y;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
    /**
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   481
     * Returns AST as JSON compatible string. This is used to
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   482
     * implement "parse" function in resources/parse.js script.
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   483
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   484
     * @param code code to be parsed
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   485
     * @param name name of the code source (used for location)
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   486
     * @param includeLoc tells whether to include location information for nodes or not
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   487
     * @return JSON string representation of AST of the supplied code
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   488
     */
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   489
    public static String parse(final String code, final String name, final boolean includeLoc) {
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24733
diff changeset
   490
        return JSONWriter.parse(Context.getContextTrusted(), code, name, includeLoc);
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   491
    }
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   492
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   493
    /**
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   494
     * Test whether a char is valid JavaScript whitespace
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   495
     * @param ch a char
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   496
     * @return true if valid JavaScript whitespace
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   497
     */
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   498
    public static boolean isJSWhitespace(final char ch) {
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   499
        return Lexer.isJSWhitespace(ch);
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   500
    }
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   501
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16206
diff changeset
   502
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   503
     * Entering a {@code with} node requires new scope. This is the implementation. When exiting the with statement,
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   504
     * use {@link ScriptObject#getProto()} on the scope.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
     * @param scope      existing scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
     * @param expression expression in with
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   508
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
     * @return {@link WithObject} that is the new scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   510
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   511
    public static ScriptObject openWith(final ScriptObject scope, final Object expression) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 21446
diff changeset
   512
        final Global global = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   513
        if (expression == UNDEFINED) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   514
            throw typeError(global, "cant.apply.with.to.undefined");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   515
        } else if (expression == null) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   516
            throw typeError(global, "cant.apply.with.to.null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
25247
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24762
diff changeset
   519
        if (expression instanceof ScriptObjectMirror) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24762
diff changeset
   520
            final Object unwrapped = ScriptObjectMirror.unwrap(expression, global);
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24762
diff changeset
   521
            if (unwrapped instanceof ScriptObject) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24762
diff changeset
   522
                return new WithObject(scope, (ScriptObject)unwrapped);
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24762
diff changeset
   523
            }
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   524
            // foreign ScriptObjectMirror
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   525
            final ScriptObject exprObj = global.newObject();
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   526
            NativeObject.bindAllProperties(exprObj, (ScriptObjectMirror)expression);
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   527
            return new WithObject(scope, exprObj);
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   528
        }
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   529
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   530
        final Object wrappedExpr = JSType.toScriptObject(global, expression);
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   531
        if (wrappedExpr instanceof ScriptObject) {
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25250
diff changeset
   532
            return new WithObject(scope, (ScriptObject)wrappedExpr);
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   533
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   534
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19638
diff changeset
   535
        throw typeError(global, "cant.apply.with.to.non.scriptobject");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
     * ECMA 11.6.1 - The addition operator (+) - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
     * @param x  first term
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
     * @param y  second term
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
     * @return result of addition
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
    public static Object ADD(final Object x, final Object y) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
        // This prefix code to handle Number special is for optimization.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
        final boolean xIsNumber = x instanceof Number;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
        final boolean yIsNumber = y instanceof Number;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
        if (xIsNumber && yIsNumber) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
             return ((Number)x).doubleValue() + ((Number)y).doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   553
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
        final boolean xIsUndefined = x == UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
        final boolean yIsUndefined = y == UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   557
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   558
        if (xIsNumber && yIsUndefined || xIsUndefined && yIsNumber || xIsUndefined && yIsUndefined) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   559
            return Double.NaN;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
        // code below is as per the spec.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
        final Object xPrim = JSType.toPrimitive(x);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
        final Object yPrim = JSType.toPrimitive(y);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29281
diff changeset
   566
        if (isString(xPrim) || isString(yPrim)) {
25250
b5a4e0ac31d1 8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents: 25247
diff changeset
   567
            try {
b5a4e0ac31d1 8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents: 25247
diff changeset
   568
                return new ConsString(JSType.toCharSequence(xPrim), JSType.toCharSequence(yPrim));
b5a4e0ac31d1 8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents: 25247
diff changeset
   569
            } catch (final IllegalArgumentException iae) {
b5a4e0ac31d1 8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents: 25247
diff changeset
   570
                throw rangeError(iae, "concat.string.too.big");
b5a4e0ac31d1 8047359: large string size RangeError should be thrown rather than reporting negative length
sundar
parents: 25247
diff changeset
   571
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   572
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
        return JSType.toNumber(xPrim) + JSType.toNumber(yPrim);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   578
     * Debugger hook.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
     * TODO: currently unimplemented
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   580
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
     * @return undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   583
    public static Object DEBUGGER() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
        return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   585
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   587
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
     * New hook
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
     * @param clazz type for the clss
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
     * @param args  constructor arguments
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
     * @return undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
    public static Object NEW(final Object clazz, final Object... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
        return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
     * ECMA 11.4.3 The typeof Operator - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
     * @param object   the object from which to retrieve property to type check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
     * @param property property in object to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   604
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   605
     * @return type name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   606
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   607
    public static Object TYPEOF(final Object object, final Object property) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   608
        Object obj = object;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   609
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
        if (property != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   611
            if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
                obj = ((ScriptObject)obj).get(property);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   613
                if(Global.isLocationPropertyPlaceholder(obj)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   614
                    if(CompilerConstants.__LINE__.name().equals(property)) {
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 31194
diff changeset
   615
                        obj = 0;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   616
                    } else {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   617
                        obj = "";
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   618
                    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21446
diff changeset
   619
                }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   620
            } else if (object instanceof Undefined) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
                obj = ((Undefined)obj).get(property);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   622
            } else if (object == null) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   623
                throw typeError("cant.get.property", safeToString(property), "null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   624
            } else if (JSType.isPrimitive(obj)) {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
   625
                obj = ((ScriptObject)JSType.toScriptObject(obj)).get(property);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   626
            } else if (obj instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   627
                obj = ((JSObject)obj).getMember(property.toString());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   628
            } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   629
                obj = UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   630
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   631
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   632
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   633
        return JSType.of(obj).typeName();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   634
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   635
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   636
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   637
     * Throw ReferenceError when LHS of assignment or increment/decrement
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   638
     * operator is not an assignable node (say a literal)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   639
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   640
     * @param lhs Evaluated LHS
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   641
     * @param rhs Evaluated RHS
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   642
     * @param msg Additional LHS info for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   643
     * @return undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   644
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   645
    public static Object REFERENCE_ERROR(final Object lhs, final Object rhs, final Object msg) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   646
        throw referenceError("cant.be.used.as.lhs", Objects.toString(msg));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   647
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   648
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   649
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   650
     * ECMA 11.4.1 - delete operation, generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   651
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
     * @param obj       object with property to delete
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
     * @param property  property to delete
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   654
     * @param strict    are we in strict mode
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   655
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   656
     * @return true if property was successfully found and deleted
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   657
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   658
    public static boolean DELETE(final Object obj, final Object property, final Object strict) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   659
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   660
            return ((ScriptObject)obj).delete(property, Boolean.TRUE.equals(strict));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   661
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
        if (obj instanceof Undefined) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
            return ((Undefined)obj).delete(property, false);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   665
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
        if (obj == null) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   668
            throw typeError("cant.delete.property", safeToString(property), "null");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   669
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   670
17978
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   671
        if (obj instanceof ScriptObjectMirror) {
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   672
            return ((ScriptObjectMirror)obj).delete(property);
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   673
        }
750d0582d8e2 8015830: Javascript mapping of ScriptEngine bindings does not expose keys
sundar
parents: 17778
diff changeset
   674
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   675
        if (JSType.isPrimitive(obj)) {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
   676
            return ((ScriptObject) JSType.toScriptObject(obj)).delete(property, Boolean.TRUE.equals(strict));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   677
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   678
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   679
        if (obj instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   680
            ((JSObject)obj).removeMember(Objects.toString(property));
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   681
            return true;
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   682
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   683
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   684
        // if object is not reference type, vacuously delete is successful.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   685
        return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   686
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
    /**
31828
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   689
     * ECMA 11.4.1 - delete operator, implementation for slow scopes
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   690
     *
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   691
     * This implementation of 'delete' walks the scope chain to find the scope that contains the
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   692
     * property to be deleted, then invokes delete on it.
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   693
     *
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   694
     * @param obj       top scope object
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   695
     * @param property  property to delete
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   696
     * @param strict    are we in strict mode
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   697
     *
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   698
     * @return true if property was successfully found and deleted
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   699
     */
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   700
    public static boolean SLOW_DELETE(final Object obj, final Object property, final Object strict) {
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   701
        if (obj instanceof ScriptObject) {
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   702
            ScriptObject sobj = (ScriptObject) obj;
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   703
            final String key = property.toString();
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   704
            while (sobj != null && sobj.isScope()) {
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   705
                final FindProperty find = sobj.findProperty(key, false);
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   706
                if (find != null) {
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   707
                    return sobj.delete(key, Boolean.TRUE.equals(strict));
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   708
                }
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   709
                sobj = sobj.getProto();
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   710
            }
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   711
        }
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   712
        return DELETE(obj, property, strict);
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   713
    }
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   714
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31549
diff changeset
   715
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   716
     * ECMA 11.4.1 - delete operator, special case
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   717
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   718
     * This is 'delete' that always fails. We have to check strict mode and throw error.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   719
     * That is why this is a runtime function. Or else we could have inlined 'false'.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   720
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   721
     * @param property  property to delete
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   722
     * @param strict    are we in strict mode
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   723
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   724
     * @return false always
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   725
     */
16206
83069fa0935b 8006529: Methods always get callee - it should be conditional
attila
parents: 16188
diff changeset
   726
    public static boolean FAIL_DELETE(final Object property, final Object strict) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   727
        if (Boolean.TRUE.equals(strict)) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   728
            throw syntaxError("strict.cant.delete", safeToString(property));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   729
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   730
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   731
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   732
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   733
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   734
     * ECMA 11.9.1 - The equals operator (==) - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   735
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   736
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   737
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   738
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   739
     * @return true if type coerced versions of objects are equal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   740
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   741
    public static boolean EQ(final Object x, final Object y) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   742
        return equals(x, y);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   743
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   744
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   745
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   746
     * ECMA 11.9.2 - The does-not-equal operator (==) - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   747
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   748
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   749
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   750
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   751
     * @return true if type coerced versions of objects are not equal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   752
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   753
    public static boolean NE(final Object x, final Object y) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   754
        return !EQ(x, y);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   755
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   756
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   757
    /** ECMA 11.9.3 The Abstract Equality Comparison Algorithm */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   758
    private static boolean equals(final Object x, final Object y) {
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   759
        if (x == y) {
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   760
            return true;
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   761
        }
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   762
        if (x instanceof ScriptObject && y instanceof ScriptObject) {
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   763
            return false; // x != y
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   764
        }
26052
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 25828
diff changeset
   765
        if (x instanceof ScriptObjectMirror || y instanceof ScriptObjectMirror) {
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 25828
diff changeset
   766
            return ScriptObjectMirror.identical(x, y);
41d18e9e45a4 8053910: ScriptObjectMirror causing havoc with Invocation interface
sundar
parents: 25828
diff changeset
   767
        }
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   768
        return equalValues(x, y);
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   769
    }
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   770
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   771
    /**
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   772
     * Extracted portion of {@code equals()} that compares objects by value (or by reference, if no known value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   773
     * comparison applies).
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   774
     * @param x one value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   775
     * @param y another value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   776
     * @return true if they're equal according to 11.9.3
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   777
     */
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   778
    private static boolean equalValues(final Object x, final Object y) {
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   779
        final JSType xType = JSType.ofNoFunction(x);
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   780
        final JSType yType = JSType.ofNoFunction(y);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   781
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   782
        if (xType == yType) {
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   783
            return equalSameTypeValues(x, y, xType);
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   784
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   785
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   786
        return equalDifferentTypeValues(x, y, xType, yType);
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   787
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   788
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   789
    /**
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   790
     * Extracted portion of {@link #equals(Object, Object)} and {@link #strictEquals(Object, Object)} that compares
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   791
     * values belonging to the same JSType.
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   792
     * @param x one value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   793
     * @param y another value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   794
     * @param type the common type for the values
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   795
     * @return true if they're equal
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   796
     */
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   797
    private static boolean equalSameTypeValues(final Object x, final Object y, final JSType type) {
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   798
        if (type == JSType.UNDEFINED || type == JSType.NULL) {
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   799
            return true;
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   800
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   801
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   802
        if (type == JSType.NUMBER) {
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   803
            return ((Number)x).doubleValue() == ((Number)y).doubleValue();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   804
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   805
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   806
        if (type == JSType.STRING) {
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   807
            // String may be represented by ConsString
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   808
            return x.toString().equals(y.toString());
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   809
        }
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   810
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   811
        if (type == JSType.BOOLEAN) {
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   812
            return ((Boolean)x).booleanValue() == ((Boolean)y).booleanValue();
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   813
        }
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   814
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   815
        return x == y;
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   816
    }
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   817
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   818
    /**
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   819
     * Extracted portion of {@link #equals(Object, Object)} that compares values belonging to different JSTypes.
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   820
     * @param x one value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   821
     * @param y another value
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   822
     * @param xType the type for the value x
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   823
     * @param yType the type for the value y
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   824
     * @return true if they're equal
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   825
     */
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   826
    private static boolean equalDifferentTypeValues(final Object x, final Object y, final JSType xType, final JSType yType) {
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   827
        if (isUndefinedAndNull(xType, yType) || isUndefinedAndNull(yType, xType)) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   828
            return true;
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   829
        } else if (isNumberAndString(xType, yType)) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   830
            return equalNumberToString(x, y);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   831
        } else if (isNumberAndString(yType, xType)) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   832
            // Can reverse order as both are primitives
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   833
            return equalNumberToString(y, x);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   834
        } else if (xType == JSType.BOOLEAN) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   835
            return equalBooleanToAny(x, y);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   836
        } else if (yType == JSType.BOOLEAN) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   837
            // Can reverse order as y is primitive
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   838
            return equalBooleanToAny(y, x);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   839
        } else if (isNumberOrStringAndObject(xType, yType)) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   840
            return equalNumberOrStringToObject(x, y);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   841
        } else if (isNumberOrStringAndObject(yType, xType)) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   842
            // Can reverse order as y is primitive
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   843
            return equalNumberOrStringToObject(y, x);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   844
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   845
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   846
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   847
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   848
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   849
    private static boolean isUndefinedAndNull(final JSType xType, final JSType yType) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   850
        return xType == JSType.UNDEFINED && yType == JSType.NULL;
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   851
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   852
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   853
    private static boolean isNumberAndString(final JSType xType, final JSType yType) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   854
        return xType == JSType.NUMBER && yType == JSType.STRING;
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   855
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   856
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   857
    private static boolean isNumberOrStringAndObject(final JSType xType, final JSType yType) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   858
        return (xType == JSType.NUMBER || xType == JSType.STRING) && yType == JSType.OBJECT;
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   859
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   860
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   861
    private static boolean equalNumberToString(final Object num, final Object str) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   862
        // Specification says comparing a number to string should be done as "equals(num, JSType.toNumber(str))". We
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   863
        // can short circuit it to this as we know that "num" is a number, so it'll end up being a number-number
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   864
        // comparison.
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   865
        return ((Number)num).doubleValue() == JSType.toNumber(str.toString());
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   866
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   867
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   868
    private static boolean equalBooleanToAny(final Object bool, final Object any) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   869
        return equals(JSType.toNumber((Boolean)bool), any);
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   870
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   871
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   872
    private static boolean equalNumberOrStringToObject(final Object numOrStr, final Object any) {
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   873
        return equals(numOrStr, JSType.toPrimitive(any));
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   874
    }
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 26768
diff changeset
   875
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   876
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   877
     * ECMA 11.9.4 - The strict equal operator (===) - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   878
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   879
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   880
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   881
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   882
     * @return true if objects are equal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   883
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   884
    public static boolean EQ_STRICT(final Object x, final Object y) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   885
        return strictEquals(x, y);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   886
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   887
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   888
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   889
     * ECMA 11.9.5 - The strict non equal operator (!==) - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   890
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   891
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   892
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   893
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   894
     * @return true if objects are not equal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   895
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   896
    public static boolean NE_STRICT(final Object x, final Object y) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   897
        return !EQ_STRICT(x, y);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   898
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   899
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   900
    /** ECMA 11.9.6 The Strict Equality Comparison Algorithm */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   901
    private static boolean strictEquals(final Object x, final Object y) {
25828
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 25821
diff changeset
   902
        // NOTE: you might be tempted to do a quick x == y comparison. Remember, though, that any Double object having
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 25821
diff changeset
   903
        // NaN value is not equal to itself by value even though it is referentially.
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   904
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   905
        final JSType xType = JSType.ofNoFunction(x);
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   906
        final JSType yType = JSType.ofNoFunction(y);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   907
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   908
        if (xType != yType) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   909
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   910
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   911
24762
96a251e57232 8043608: Make equality tests inline better
attila
parents: 24752
diff changeset
   912
        return equalSameTypeValues(x, y, xType);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   913
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   914
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   915
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   916
     * ECMA 11.8.6 - The in operator - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   917
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   918
     * @param property property to check for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   919
     * @param obj object in which to check for property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   920
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   921
     * @return true if objects are equal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   922
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   923
    public static boolean IN(final Object property, final Object obj) {
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   924
        final JSType rvalType = JSType.ofNoFunction(obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   925
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24745
diff changeset
   926
        if (rvalType == JSType.OBJECT) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   927
            if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   928
                return ((ScriptObject)obj).has(property);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   929
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   930
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   931
            if (obj instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   932
                return ((JSObject)obj).hasMember(Objects.toString(property));
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   933
            }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   934
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   935
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   936
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   937
17778
991ccffbeb13 8015459: Octane test run fails on Turkish locale
sundar
parents: 16272
diff changeset
   938
        throw typeError("in.with.non.object", rvalType.toString().toLowerCase(Locale.ENGLISH));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   941
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   942
     * ECMA 11.8.6 - The strict instanceof operator - generic implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
     * @param obj first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
     * @param clazz type to check against
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
     * @return true if {@code obj} is an instanceof {@code clazz}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   949
    public static boolean INSTANCEOF(final Object obj, final Object clazz) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   950
        if (clazz instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   951
            if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
                return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   953
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   954
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   955
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   956
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   957
        if (clazz instanceof StaticClass) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
            return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   961
        if (clazz instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   962
            return ((JSObject)clazz).isInstance(obj);
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   963
        }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   964
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   965
        // provide for reverse hook
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   966
        if (obj instanceof JSObject) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   967
            return ((JSObject)obj).isInstanceOf(clazz);
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17978
diff changeset
   968
        }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 17978
diff changeset
   969
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16240
diff changeset
   970
        throw typeError("instanceof.on.non.object");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   971
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   972
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   973
    /**
16272
675a0caf75bc 8009263: Fix all javadoc errors in nashorn code
sundar
parents: 16262
diff changeset
   974
     * ECMA 11.8.1 - The less than operator ({@literal <}) - generic implementation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   977
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   978
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   979
     * @return true if x is less than y
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   980
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   981
    public static boolean LT(final Object x, final Object y) {
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   982
        final Object px = JSType.toPrimitive(x, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   983
        final Object py = JSType.toPrimitive(y, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   984
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   985
        return areBothString(px, py) ? px.toString().compareTo(py.toString()) < 0 :
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   986
            JSType.toNumber(px) < JSType.toNumber(py);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   987
    }
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   988
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   989
    private static boolean areBothString(final Object x, final Object y) {
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
   990
        return isString(x) && isString(y);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   991
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   992
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   993
    /**
16272
675a0caf75bc 8009263: Fix all javadoc errors in nashorn code
sundar
parents: 16262
diff changeset
   994
     * ECMA 11.8.2 - The greater than operator ({@literal >}) - generic implementation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   995
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   996
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   997
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   998
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   999
     * @return true if x is greater than y
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1000
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1001
    public static boolean GT(final Object x, final Object y) {
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1002
        final Object px = JSType.toPrimitive(x, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1003
        final Object py = JSType.toPrimitive(y, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1004
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1005
        return areBothString(px, py) ? px.toString().compareTo(py.toString()) > 0 :
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1006
            JSType.toNumber(px) > JSType.toNumber(py);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1007
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1008
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1009
    /**
16272
675a0caf75bc 8009263: Fix all javadoc errors in nashorn code
sundar
parents: 16262
diff changeset
  1010
     * ECMA 11.8.3 - The less than or equal operator ({@literal <=}) - generic implementation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1011
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1012
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1013
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1014
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1015
     * @return true if x is less than or equal to y
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1016
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1017
    public static boolean LE(final Object x, final Object y) {
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1018
        final Object px = JSType.toPrimitive(x, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1019
        final Object py = JSType.toPrimitive(y, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1020
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1021
        return areBothString(px, py) ? px.toString().compareTo(py.toString()) <= 0 :
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1022
            JSType.toNumber(px) <= JSType.toNumber(py);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1023
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1024
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1025
    /**
16272
675a0caf75bc 8009263: Fix all javadoc errors in nashorn code
sundar
parents: 16262
diff changeset
  1026
     * ECMA 11.8.4 - The greater than or equal operator ({@literal >=}) - generic implementation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1027
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1028
     * @param x first object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1029
     * @param y second object to compare
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1030
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1031
     * @return true if x is greater than or equal to y
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1032
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1033
    public static boolean GE(final Object x, final Object y) {
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1034
        final Object px = JSType.toPrimitive(x, Number.class);
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1035
        final Object py = JSType.toPrimitive(y, Number.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1036
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1037
        return areBothString(px, py) ? px.toString().compareTo(py.toString()) >= 0 :
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1038
            JSType.toNumber(px) >= JSType.toNumber(py);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1039
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1040
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1041
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1042
     * Tag a reserved name as invalidated - used when someone writes
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1043
     * to a property with this name - overly conservative, but link time
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1044
     * is too late to apply e.g. apply-&gt;call specialization
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1045
     * @param name property name
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1046
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1047
    public static void invalidateReservedBuiltinName(final String name) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1048
        final Context context = Context.getContextTrusted();
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1049
        final SwitchPoint sp = context.getBuiltinSwitchPoint(name);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1050
        assert sp != null;
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1051
        context.getLogger(ApplySpecialization.class).info("Overwrote special name '" + name +"' - invalidating switchpoint");
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 29282
diff changeset
  1052
        SwitchPoint.invalidateAll(new SwitchPoint[] { sp });
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26377
diff changeset
  1053
    }
33414
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1054
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1055
    /**
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1056
     * ES6 12.2.9.3 Runtime Semantics: GetTemplateObject(templateLiteral).
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1057
     *
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1058
     * @param rawStrings array of template raw values
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1059
     * @param cookedStrings array of template values
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1060
     * @return template object
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1061
     */
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1062
    public static ScriptObject GET_TEMPLATE_OBJECT(final Object rawStrings, final Object cookedStrings) {
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1063
        final ScriptObject template = (ScriptObject)cookedStrings;
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1064
        final ScriptObject rawObj = (ScriptObject)rawStrings;
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1065
        assert rawObj.getArray().length() == template.getArray().length();
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1066
        template.addOwnProperty("raw", Property.NOT_WRITABLE | Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE, rawObj.freeze());
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1067
        template.freeze();
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1068
        return template;
2e284c36d51f 8134941: Implement ES6 template literal support
mhaupt
parents: 33343
diff changeset
  1069
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1070
}