src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeObject.java
author hannesw
Tue, 08 May 2018 09:41:10 +0200
changeset 50046 26d9c0cf53d5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200716: Object propertyIsEnumerable buggy behavior on short integer-string key Reviewed-by: jlaskey, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
35407
204abe4d8cbc 8147591: Revisit Collection.toArray(new T[size]) calls in nashorn and dynalink code
mhaupt
parents: 34979
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.objects;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
    28
import static jdk.dynalink.StandardNamespace.METHOD;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
    29
import static jdk.dynalink.StandardNamespace.PROPERTY;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
    30
import static jdk.dynalink.StandardOperation.GET;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
    31
import static jdk.dynalink.StandardOperation.SET;
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
    32
import static jdk.nashorn.internal.lookup.Lookup.MH;
16147
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.ScriptRuntime.UNDEFINED;
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27307
diff changeset
    35
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    36
import java.lang.invoke.MethodHandle;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    37
import java.lang.invoke.MethodHandles;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    38
import java.lang.invoke.MethodType;
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
    39
import java.nio.ByteBuffer;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    40
import java.util.ArrayList;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    41
import java.util.Collection;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    42
import java.util.HashSet;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    43
import java.util.List;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    44
import java.util.Set;
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    45
import java.util.concurrent.Callable;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    46
import jdk.dynalink.CallSiteDescriptor;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    47
import jdk.dynalink.Operation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    48
import jdk.dynalink.beans.BeansLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    49
import jdk.dynalink.beans.StaticClass;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    50
import jdk.dynalink.linker.GuardedInvocation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    51
import jdk.dynalink.linker.GuardingDynamicLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    52
import jdk.dynalink.linker.LinkRequest;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33690
diff changeset
    53
import jdk.dynalink.linker.support.SimpleLinkRequest;
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    54
import jdk.nashorn.api.scripting.ScriptObjectMirror;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    55
import jdk.nashorn.internal.lookup.Lookup;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
import jdk.nashorn.internal.objects.annotations.Attribute;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
import jdk.nashorn.internal.objects.annotations.Constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
import jdk.nashorn.internal.objects.annotations.Function;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
import jdk.nashorn.internal.objects.annotations.ScriptClass;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
import jdk.nashorn.internal.objects.annotations.Where;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    61
import jdk.nashorn.internal.runtime.AccessorProperty;
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    62
import jdk.nashorn.internal.runtime.ECMAException;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
import jdk.nashorn.internal.runtime.JSType;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    64
import jdk.nashorn.internal.runtime.Property;
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
    65
import jdk.nashorn.internal.runtime.PropertyMap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
import jdk.nashorn.internal.runtime.ScriptObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
import jdk.nashorn.internal.runtime.ScriptRuntime;
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
    68
import jdk.nashorn.internal.runtime.arrays.ArrayData;
50046
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
    69
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    70
import jdk.nashorn.internal.runtime.linker.Bootstrap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
import jdk.nashorn.internal.runtime.linker.InvokeByName;
21686
5c6946f97d6f 8027236: Ensure ScriptObject and ConsString aren't visible to Java
attila
parents: 19637
diff changeset
    72
import jdk.nashorn.internal.runtime.linker.NashornBeansLinker;
33330
35531ae624ef 8139304: Remove elaborate call site descriptor class hierarchy and factory for them. Remove AutoDiscovery, DefaultPrelinkFilter, and BottomGuardingDynamicLinker as they can be inlined into DynamicLinkerFactory. Remove CallerSensitiveDetector as it can be inlined into AbstractJavaLinker. Make ClassMap non-public.
attila
parents: 30702
diff changeset
    73
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
16147
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
 * ECMA 15.2 Object objects
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
 * JavaScript Object constructor/prototype. Note: instances of this class are
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
 * never created. This class is not even a subclass of ScriptObject. But, we use
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
 * this class to generate prototype and constructor for "Object".
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
@ScriptClass("Object")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
public final class NativeObject {
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24772
diff changeset
    85
    /** Methodhandle to proto getter */
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
    86
    public static final MethodHandle GET__PROTO__ = findOwnMH("get__proto__", ScriptObject.class, Object.class);
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24772
diff changeset
    87
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24772
diff changeset
    88
    /** Methodhandle to proto setter */
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
    89
    public static final MethodHandle SET__PROTO__ = findOwnMH("set__proto__", Object.class, Object.class, Object.class);
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
    90
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    91
    private static final Object TO_STRING = new Object();
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    92
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    93
    private static InvokeByName getTO_STRING() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    94
        return Global.instance().getInvokeByName(TO_STRING,
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    95
                new Callable<InvokeByName>() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    96
                    @Override
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    97
                    public InvokeByName call() {
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    98
                        return new InvokeByName("toString", ScriptObject.class);
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
    99
                    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
   100
                });
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
   101
    }
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
   102
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   103
    private static final Operation GET_METHOD   = GET.withNamespace(METHOD);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   104
    private static final Operation GET_PROPERTY = GET.withNamespace(PROPERTY);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   105
    private static final Operation SET_PROPERTY = SET.withNamespace(PROPERTY);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   106
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   107
    @SuppressWarnings("unused")
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   108
    private static ScriptObject get__proto__(final Object self) {
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   109
        // See ES6 draft spec: B.2.2.1.1 get Object.prototype.__proto__
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   110
        // Step 1 Let O be the result of calling ToObject passing the this.
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   111
        final ScriptObject sobj = Global.checkObject(Global.toObject(self));
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   112
        return sobj.getProto();
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   113
    }
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   114
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   115
    @SuppressWarnings("unused")
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   116
    private static Object set__proto__(final Object self, final Object proto) {
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   117
        // See ES6 draft spec: B.2.2.1.2 set Object.prototype.__proto__
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   118
        // Step 1
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   119
        Global.checkObjectCoercible(self);
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   120
        // Step 4
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   121
        if (! (self instanceof ScriptObject)) {
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   122
            return UNDEFINED;
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   123
        }
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   124
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   125
        final ScriptObject sobj = (ScriptObject)self;
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   126
        // __proto__ assignment ignores non-nulls and non-objects
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   127
        // step 3: If Type(proto) is neither Object nor Null, then return undefined.
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   128
        if (proto == null || proto instanceof ScriptObject) {
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   129
            sobj.setPrototypeOf(proto);
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   130
        }
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   131
        return UNDEFINED;
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   132
    }
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   133
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   134
    private static final MethodType MIRROR_GETTER_TYPE = MethodType.methodType(Object.class, ScriptObjectMirror.class);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   135
    private static final MethodType MIRROR_SETTER_TYPE = MethodType.methodType(Object.class, ScriptObjectMirror.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
   137
    // initialized by nasgen
18842
3c3be808b593 8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
lagergren
parents: 18618
diff changeset
   138
    @SuppressWarnings("unused")
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
   139
    private static PropertyMap $nasgenmap$;
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
   140
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
    private NativeObject() {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   142
        // don't create me!
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   143
        throw new UnsupportedOperationException();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   146
    private static ECMAException notAnObject(final Object obj) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   147
        return typeError("not.an.object", ScriptRuntime.safeToString(obj));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   148
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   149
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    /**
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   151
     * Nashorn extension: setIndexedPropertiesToExternalArrayData
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   152
     *
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   153
     * @param self self reference
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   154
     * @param obj object whose index properties are backed by buffer
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   155
     * @param buf external buffer - should be a nio ByteBuffer
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   156
     * @return the 'obj' object
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   157
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   158
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   159
    public static ScriptObject setIndexedPropertiesToExternalArrayData(final Object self, final Object obj, final Object buf) {
23078
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   160
        Global.checkObject(obj);
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   161
        final ScriptObject sobj = (ScriptObject)obj;
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   162
        if (buf instanceof ByteBuffer) {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   163
            sobj.setArray(ArrayData.allocate((ByteBuffer)buf));
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   164
        } else {
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   165
            throw typeError("not.a.bytebuffer", "setIndexedPropertiesToExternalArrayData's buf argument");
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   166
        }
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   167
        return sobj;
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   168
    }
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   169
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   170
06c03700f884 8011964: need indexed access to externally-managed ByteBuffer
sundar
parents: 22668
diff changeset
   171
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
     * ECMA 15.2.3.2 Object.getPrototypeOf ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
     * @param  self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
     * @param  obj object to get prototype from
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
     * @return the prototype of an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   178
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    public static Object getPrototypeOf(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   180
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   181
            return ((ScriptObject)obj).getProto();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   182
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   183
            return ((ScriptObjectMirror)obj).getProto();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   184
        } else {
19465
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   185
            final JSType type = JSType.of(obj);
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   186
            if (type == JSType.OBJECT) {
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   187
                // host (Java) objects have null __proto__
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   188
                return null;
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   189
            }
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   190
0dcae2f1ca9f 8022598: Object.getPrototypeOf should return null for host objects rather than throwing TypeError
sundar
parents: 19456
diff changeset
   191
            // must be some JS primitive
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   192
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   193
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
    /**
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   197
     * Nashorn extension: Object.setPrototypeOf ( O, proto )
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   198
     * Also found in ES6 draft specification.
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   199
     *
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   200
     * @param  self self reference
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   201
     * @param  obj object to set prototype for
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   202
     * @param  proto prototype object to be used
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   203
     * @return object whose prototype is set
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   204
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   205
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   206
    public static Object setPrototypeOf(final Object self, final Object obj, final Object proto) {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   207
        if (obj instanceof ScriptObject) {
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   208
            ((ScriptObject)obj).setPrototypeOf(proto);
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   209
            return obj;
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   210
        } else if (obj instanceof ScriptObjectMirror) {
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   211
            ((ScriptObjectMirror)obj).setProto(proto);
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   212
            return obj;
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   213
        }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   214
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   215
        throw notAnObject(obj);
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   216
    }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   217
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   218
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
     * ECMA 15.2.3.3 Object.getOwnPropertyDescriptor ( O, P )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
     * @param obj   object from which to get property descriptor for {@code ToString(prop)}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
     * @param prop  property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     * @return property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   226
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    public static Object getOwnPropertyDescriptor(final Object self, final Object obj, final Object prop) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   228
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   229
            final String       key  = JSType.toString(prop);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   230
            final ScriptObject sobj = (ScriptObject)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   232
            return sobj.getOwnPropertyDescriptor(key);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   233
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   234
            final String       key  = JSType.toString(prop);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   235
            final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   237
            return sobjMirror.getOwnPropertyDescriptor(key);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   238
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   239
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   240
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
     * ECMA 15.2.3.4 Object.getOwnPropertyNames ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
     * @param obj  object to query for property names
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
     * @return array of property names
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   250
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   251
    public static ScriptObject getOwnPropertyNames(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   252
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   253
            return new NativeArray(((ScriptObject)obj).getOwnKeys(true));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   254
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   255
            return new NativeArray(((ScriptObjectMirror)obj).getOwnKeys(true));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   256
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   257
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   258
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
    /**
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   262
     * ECMA 2 19.1.2.8 Object.getOwnPropertySymbols ( O )
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   263
     *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   264
     * @param self self reference
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   265
     * @param obj  object to query for property names
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   266
     * @return array of property names
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   267
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   268
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   269
    public static ScriptObject getOwnPropertySymbols(final Object self, final Object obj) {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   270
        if (obj instanceof ScriptObject) {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   271
            return new NativeArray(((ScriptObject)obj).getOwnSymbols(true));
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   272
        } else {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   273
            // TODO: we don't support this on ScriptObjectMirror objects yet
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   274
            throw notAnObject(obj);
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   275
        }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   276
    }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   277
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   278
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
     * ECMA 15.2.3.5 Object.create ( O [, Properties] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
     * @param proto prototype object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
     * @param props properties to define
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
     * @return object created
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   286
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   287
    public static ScriptObject create(final Object self, final Object proto, final Object props) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
        if (proto != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
            Global.checkObject(proto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
        // FIXME: should we create a proper object with correct number of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
        // properties?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
        final ScriptObject newObj = Global.newEmptyInstance();
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19465
diff changeset
   295
        newObj.setProto((ScriptObject)proto);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
        if (props != UNDEFINED) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
            NativeObject.defineProperties(self, newObj, props);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
        return newObj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
     * ECMA 15.2.3.6 Object.defineProperty ( O, P, Attributes )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
     * @param obj  object in which to define a property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
     * @param prop property to define
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
     * @param attr attributes for property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     * @return object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   312
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   313
    public static ScriptObject defineProperty(final Object self, final Object obj, final Object prop, final Object attr) {
24769
attila
parents: 24752 23763
diff changeset
   314
        final ScriptObject sobj = Global.checkObject(obj);
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   315
        sobj.defineOwnProperty(JSType.toPropertyKey(prop), attr, true);
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   316
        return sobj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
     * ECMA 5.2.3.7 Object.defineProperties ( O, Properties )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
     * @param obj   object in which to define properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
     * @param props properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
     * @return object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   327
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   328
    public static ScriptObject defineProperties(final Object self, final Object obj, final Object props) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   329
        final ScriptObject sobj     = Global.checkObject(obj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
        final Object       propsObj = Global.toObject(props);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
        if (propsObj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
            final Object[] keys = ((ScriptObject)propsObj).getOwnKeys(false);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
            for (final Object key : keys) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
                final String prop = JSType.toString(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
                sobj.defineOwnProperty(prop, ((ScriptObject)propsObj).get(prop), true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
        return sobj;
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
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
     * ECMA 15.2.3.8 Object.seal ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
     * @param obj  object to seal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
     * @return sealed object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   349
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
    public static Object seal(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   351
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   352
            return ((ScriptObject)obj).seal();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   353
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   354
            return ((ScriptObjectMirror)obj).seal();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   355
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   356
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   357
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
     * ECMA 15.2.3.9 Object.freeze ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     * @param obj object to freeze
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
     * @return frozen object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   368
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
    public static Object freeze(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   370
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   371
            return ((ScriptObject)obj).freeze();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   372
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   373
            return ((ScriptObjectMirror)obj).freeze();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   374
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   375
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   376
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
     * ECMA 15.2.3.10 Object.preventExtensions ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
     * @param obj  object, for which to set the internal extensible property to false
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
     * @return object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   386
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
    public static Object preventExtensions(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   388
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   389
            return ((ScriptObject)obj).preventExtensions();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   390
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   391
            return ((ScriptObjectMirror)obj).preventExtensions();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   392
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   393
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   394
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
     * ECMA 15.2.3.11 Object.isSealed ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
     * @param obj check whether an object is sealed
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
     * @return true if sealed, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   404
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   405
    public static boolean isSealed(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   406
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   407
            return ((ScriptObject)obj).isSealed();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   408
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   409
            return ((ScriptObjectMirror)obj).isSealed();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   410
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   411
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   412
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
     * ECMA 15.2.3.12 Object.isFrozen ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
     * @param obj check whether an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
     * @return true if object is frozen, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   422
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   423
    public static boolean isFrozen(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   424
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   425
            return ((ScriptObject)obj).isFrozen();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   426
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   427
            return ((ScriptObjectMirror)obj).isFrozen();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   428
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   429
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   430
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
     * ECMA 15.2.3.13 Object.isExtensible ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
     * @param obj check whether an object is extensible
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
     * @return true if object is extensible, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   440
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   441
    public static boolean isExtensible(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   442
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   443
            return ((ScriptObject)obj).isExtensible();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   444
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   445
            return ((ScriptObjectMirror)obj).isExtensible();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   446
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   447
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   448
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
     * ECMA 15.2.3.14 Object.keys ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
     * @param obj  object from which to extract keys
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
     * @return array of keys in object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   458
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   459
    public static ScriptObject keys(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   460
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   461
            final ScriptObject sobj = (ScriptObject)obj;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   462
            return new NativeArray(sobj.getOwnKeys(false));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   463
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   464
            final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   465
            return new NativeArray(sobjMirror.getOwnKeys(false));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   466
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   467
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   468
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   471
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
     * ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
     * @param newObj is the new object instantiated with the new operator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
     * @param value  value of object to be instantiated
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
     * @return the new NativeObject
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   481
    @Constructor
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
    public static Object construct(final boolean newObj, final Object self, final Object value) {
24752
c835f368e8e0 8043002: Improve performance of Nashorn equality operators
attila
parents: 24738
diff changeset
   483
        final JSType type = JSType.ofNoFunction(value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
        // Object(null), Object(undefined), Object() are same as "new Object()"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   487
        if (newObj || type == JSType.NULL || type == JSType.UNDEFINED) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
            switch (type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
            case BOOLEAN:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
            case NUMBER:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
            case STRING:
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 33343
diff changeset
   492
            case SYMBOL:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
                return Global.toObject(value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
            case OBJECT:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
                return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   496
            case NULL:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   497
            case UNDEFINED:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   498
                // fall through..
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
            default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   500
                break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   501
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   502
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   503
            return Global.newEmptyInstance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
        return Global.toObject(value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   508
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   510
     * ECMA 15.2.4.2 Object.prototype.toString ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   511
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   512
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   513
     * @return ToString of object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   514
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   515
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   516
    public static String toString(final Object self) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
        return ScriptRuntime.builtinObjectToString(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
     * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   524
     * @return localized ToString
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   525
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   527
    public static Object toLocaleString(final Object self) {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
   528
        final Object obj = JSType.toScriptObject(self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   529
        if (obj instanceof ScriptObject) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
   530
            final InvokeByName toStringInvoker = getTO_STRING();
27820
cd39dfeab986 8066214: Fuzzing bug: Object.prototype.toLocaleString(0)
hannesw
parents: 27815
diff changeset
   531
            final ScriptObject sobj = (ScriptObject)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
            try {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
   533
                final Object toString = toStringInvoker.getGetter().invokeExact(sobj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   534
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   535
                if (Bootstrap.isCallable(toString)) {
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 19097
diff changeset
   536
                    return toStringInvoker.getInvoker().invokeExact(toString, sobj);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
            } catch (final RuntimeException | Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
                throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
            } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
                throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   544
            throw typeError("not.a.function", "toString");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
        return ScriptRuntime.builtinObjectToString(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
     * ECMA 15.2.4.4 Object.prototype.valueOf ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   553
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
     * @return value of object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   557
    public static Object valueOf(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   558
        return Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   559
    }
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
     * ECMA 15.2.4.5 Object.prototype.hasOwnProperty (V)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
     * @param v property to check for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   566
     * @return true if property exists in object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   568
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   569
    public static boolean hasOwnProperty(final Object self, final Object v) {
21867
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 21686
diff changeset
   570
        // Convert ScriptObjects to primitive with String.class hint
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 21686
diff changeset
   571
        // but no need to convert other primitives to string.
4e5ee0aeb468 8028210: Missing conversions on array index expression
hannesw
parents: 21686
diff changeset
   572
        final Object key = JSType.toPrimitive(v, String.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   575
        return obj instanceof ScriptObject && ((ScriptObject)obj).hasOwnProperty(key);
16147
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
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
     * ECMA 15.2.4.6 Object.prototype.isPrototypeOf (V)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   580
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
     * @param v v prototype object to check against
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   583
     * @return true if object is prototype of v
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   585
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   586
    public static boolean isPrototypeOf(final Object self, final Object v) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   587
        if (!(v instanceof ScriptObject)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
        final Object obj   = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
        ScriptObject proto = (ScriptObject)v;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
        do {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
            proto = proto.getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
            if (proto == obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
        } while (proto != null);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   604
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   605
     * ECMA 15.2.4.7 Object.prototype.propertyIsEnumerable (V)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   606
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   607
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   608
     * @param v property to check if enumerable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   609
     * @return true if property is enumerable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   611
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 23078
diff changeset
   612
    public static boolean propertyIsEnumerable(final Object self, final Object v) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
        final String str = JSType.toString(v);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
        if (obj instanceof ScriptObject) {
50046
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   617
            final ScriptObject sobj = (ScriptObject) obj;
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   618
            final Property property = sobj.getProperty(str);
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   619
            if (property != null) {
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   620
                return property.isEnumerable();
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   621
            } else {
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   622
                return (sobj.getArray().has(ArrayIndex.getArrayIndex(v)));
26d9c0cf53d5 8200716: Object propertyIsEnumerable buggy behavior on short integer-string key
hannesw
parents: 47216
diff changeset
   623
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   624
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   625
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   626
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   627
    }
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   628
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   629
    /**
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   630
     * Nashorn extension: Object.bindProperties
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   631
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   632
     * Binds the source object's properties to the target object. Binding
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   633
     * properties allows two-way read/write for the properties of the source object.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   634
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   635
     * Example:
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   636
     * <pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   637
     * var obj = { x: 34, y: 100 };
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   638
     * var foo = {}
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   639
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   640
     * // bind properties of "obj" to "foo" object
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   641
     * Object.bindProperties(foo, obj);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   642
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   643
     * // now, we can access/write on 'foo' properties
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   644
     * print(foo.x); // prints obj.x which is 34
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   645
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   646
     * // update obj.x via foo.x
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   647
     * foo.x = "hello";
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   648
     * print(obj.x); // prints "hello" now
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   649
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   650
     * obj.x = 42;   // foo.x also becomes 42
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   651
     * print(foo.x); // prints 42
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   652
     * </pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   653
     * <p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   654
     * The source object bound can be a ScriptObject or a ScriptOjectMirror.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   655
     * null or undefined source object results in TypeError being thrown.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   656
     * </p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   657
     * Example:
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   658
     * <pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   659
     * var obj = loadWithNewGlobal({
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   660
     *    name: "test",
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   661
     *    script: "obj = { x: 33, y: 'hello' }"
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   662
     * });
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   663
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   664
     * // bind 'obj's properties to global scope 'this'
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   665
     * Object.bindProperties(this, obj);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   666
     * print(x);         // prints 33
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   667
     * print(y);         // prints "hello"
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   668
     * x = Math.PI;      // changes obj.x to Math.PI
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   669
     * print(obj.x);     // prints Math.PI
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   670
     * </pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   671
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   672
     * Limitations of property binding:
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   673
     * <ul>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   674
     * <li> Only enumerable, immediate (not proto inherited) properties of the source object are bound.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   675
     * <li> If the target object already contains a property called "foo", the source's "foo" is skipped (not bound).
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   676
     * <li> Properties added to the source object after binding to the target are not bound.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   677
     * <li> Property configuration changes on the source object (or on the target) is not propagated.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   678
     * <li> Delete of property on the target (or the source) is not propagated -
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   679
     * only the property value is set to 'undefined' if the property happens to be a data property.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   680
     * </ul>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   681
     * <p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   682
     * It is recommended that the bound properties be treated as non-configurable
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   683
     * properties to avoid surprises.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   684
     * </p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   685
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   686
     * @param self self reference
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   687
     * @param target the target object to which the source object's properties are bound
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   688
     * @param source the source object whose properties are bound to the target
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   689
     * @return the target object after property binding
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   690
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34735
diff changeset
   691
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   692
    public static Object bindProperties(final Object self, final Object target, final Object source) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   693
        // target object has to be a ScriptObject
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   694
        final ScriptObject targetObj = Global.checkObject(target);
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   695
        // check null or undefined source object
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   696
        Global.checkObjectCoercible(source);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   697
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   698
        if (source instanceof ScriptObject) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   699
            final ScriptObject sourceObj  = (ScriptObject)source;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   700
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   701
            final PropertyMap  sourceMap  = sourceObj.getMap();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   702
            final Property[]   properties = sourceMap.getProperties();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   703
            //replace the map and blow up everything to objects to work with dual fields :-(
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   704
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   705
            // filter non-enumerable properties
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   706
            final ArrayList<Property> propList = new ArrayList<>();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   707
            for (final Property prop : properties) {
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   708
                if (prop.isEnumerable()) {
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   709
                    final Object value = sourceObj.get(prop.getKey());
27307
62ed492cbe63 8062401: User accessors require boxing and do not support optimistic types
hannesw
parents: 27209
diff changeset
   710
                    prop.setType(Object.class);
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   711
                    prop.setValue(sourceObj, sourceObj, value, false);
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   712
                    propList.add(prop);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   713
                }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   714
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   715
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 23078
diff changeset
   716
            if (!propList.isEmpty()) {
35407
204abe4d8cbc 8147591: Revisit Collection.toArray(new T[size]) calls in nashorn and dynalink code
mhaupt
parents: 34979
diff changeset
   717
                targetObj.addBoundProperties(sourceObj, propList.toArray(new Property[0]));
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   718
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   719
        } else if (source instanceof ScriptObjectMirror) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   720
            // get enumerable, immediate properties of mirror
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   721
            final ScriptObjectMirror mirror = (ScriptObjectMirror)source;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   722
            final String[] keys = mirror.getOwnKeys(false);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   723
            if (keys.length == 0) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   724
                // nothing to bind
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   725
                return target;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   726
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   727
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   728
            // make accessor properties using dynamic invoker getters and setters
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   729
            final AccessorProperty[] props = new AccessorProperty[keys.length];
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   730
            for (int idx = 0; idx < keys.length; idx++) {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   731
                props[idx] = createAccessorProperty(keys[idx]);
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   732
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   733
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   734
            targetObj.addBoundProperties(source, props);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   735
        } else if (source instanceof StaticClass) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   736
            final Class<?> clazz = ((StaticClass)source).getRepresentedClass();
22668
245094625886 8032948: Nashorn linkages awry
sundar
parents: 22666
diff changeset
   737
            Bootstrap.checkReflectionAccess(clazz, true);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   738
            bindBeanProperties(targetObj, source, BeansLinker.getReadableStaticPropertyNames(clazz),
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   739
                    BeansLinker.getWritableStaticPropertyNames(clazz), BeansLinker.getStaticMethodNames(clazz));
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   740
        } else {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   741
            final Class<?> clazz = source.getClass();
22668
245094625886 8032948: Nashorn linkages awry
sundar
parents: 22666
diff changeset
   742
            Bootstrap.checkReflectionAccess(clazz, false);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   743
            bindBeanProperties(targetObj, source, BeansLinker.getReadableInstancePropertyNames(clazz),
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   744
                    BeansLinker.getWritableInstancePropertyNames(clazz), BeansLinker.getInstanceMethodNames(clazz));
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   745
        }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   746
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   747
        return target;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   748
    }
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   749
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   750
    private static AccessorProperty createAccessorProperty(final String name) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   751
        final MethodHandle getter = Bootstrap.createDynamicInvoker(name, NashornCallSiteDescriptor.GET_METHOD_PROPERTY, MIRROR_GETTER_TYPE);
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   752
        final MethodHandle setter = Bootstrap.createDynamicInvoker(name, NashornCallSiteDescriptor.SET_PROPERTY, MIRROR_SETTER_TYPE);
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   753
        return AccessorProperty.create(name, 0, getter, setter);
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   754
    }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   755
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25247
diff changeset
   756
    /**
25247
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   757
     * Binds the source mirror object's properties to the target object. Binding
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   758
     * properties allows two-way read/write for the properties of the source object.
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   759
     * All inherited, enumerable properties are also bound. This method is used to
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   760
     * to make 'with' statement work with ScriptObjectMirror as scope object.
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   761
     *
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   762
     * @param target the target object to which the source object's properties are bound
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   763
     * @param source the source object whose properties are bound to the target
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   764
     * @return the target object after property binding
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   765
     */
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   766
    public static Object bindAllProperties(final ScriptObject target, final ScriptObjectMirror source) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   767
        final Set<String> keys = source.keySet();
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   768
        // make accessor properties using dynamic invoker getters and setters
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   769
        final AccessorProperty[] props = new AccessorProperty[keys.size()];
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   770
        int idx = 0;
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25247
diff changeset
   771
        for (final String name : keys) {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   772
            props[idx] = createAccessorProperty(name);
25247
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   773
            idx++;
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   774
        }
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   775
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   776
        target.addBoundProperties(source, props);
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   777
        return target;
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   778
    }
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24993
diff changeset
   779
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   780
    private static void bindBeanProperties(final ScriptObject targetObj, final Object source,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   781
            final Collection<String> readablePropertyNames, final Collection<String> writablePropertyNames,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   782
            final Collection<String> methodNames) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   783
        final Set<String> propertyNames = new HashSet<>(readablePropertyNames);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   784
        propertyNames.addAll(writablePropertyNames);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   785
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   786
        final Class<?> clazz = source.getClass();
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   787
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   788
        final MethodType getterType = MethodType.methodType(Object.class, clazz);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   789
        final MethodType setterType = MethodType.methodType(Object.class, clazz, Object.class);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   790
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34844
diff changeset
   791
        final GuardingDynamicLinker linker = Bootstrap.getBeanLinkerForClass(clazz);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   792
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   793
        final List<AccessorProperty> properties = new ArrayList<>(propertyNames.size() + methodNames.size());
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   794
        for(final String methodName: methodNames) {
19637
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   795
            final MethodHandle method;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   796
            try {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   797
                method = getBeanOperation(linker, GET_METHOD, methodName, getterType, source);
19637
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   798
            } catch(final IllegalAccessError e) {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   799
                // Presumably, this was a caller sensitive method. Ignore it and carry on.
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   800
                continue;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   801
            }
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   802
            properties.add(AccessorProperty.create(methodName, Property.NOT_WRITABLE, getBoundBeanMethodGetter(source,
30702
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents: 27820
diff changeset
   803
                    method), Lookup.EMPTY_SETTER));
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   804
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   805
        for(final String propertyName: propertyNames) {
19637
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   806
            MethodHandle getter;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   807
            if(readablePropertyNames.contains(propertyName)) {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   808
                try {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   809
                    getter = getBeanOperation(linker, GET_PROPERTY, propertyName, getterType, source);
19637
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   810
                } catch(final IllegalAccessError e) {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   811
                    // Presumably, this was a caller sensitive method. Ignore it and carry on.
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   812
                    getter = Lookup.EMPTY_GETTER;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   813
                }
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   814
            } else {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   815
                getter = Lookup.EMPTY_GETTER;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   816
            }
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   817
            final boolean isWritable = writablePropertyNames.contains(propertyName);
19637
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   818
            MethodHandle setter;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   819
            if(isWritable) {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   820
                try {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   821
                    setter = getBeanOperation(linker, SET_PROPERTY, propertyName, setterType, source);
19637
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   822
                } catch(final IllegalAccessError e) {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   823
                    // Presumably, this was a caller sensitive method. Ignore it and carry on.
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   824
                    setter = Lookup.EMPTY_SETTER;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   825
                }
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   826
            } else {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   827
                setter = Lookup.EMPTY_SETTER;
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   828
            }
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   829
            if(getter != Lookup.EMPTY_GETTER || setter != Lookup.EMPTY_SETTER) {
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   830
                properties.add(AccessorProperty.create(propertyName, isWritable ? 0 : Property.NOT_WRITABLE, getter, setter));
fbba16718d6c 8023780: Gracefully handle @CS methods while binding bean properties
attila
parents: 19619
diff changeset
   831
            }
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   832
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   833
35407
204abe4d8cbc 8147591: Revisit Collection.toArray(new T[size]) calls in nashorn and dynalink code
mhaupt
parents: 34979
diff changeset
   834
        targetObj.addBoundProperties(source, properties.toArray(new AccessorProperty[0]));
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   835
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   836
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   837
    private static MethodHandle getBoundBeanMethodGetter(final Object source, final MethodHandle methodGetter) {
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   838
        try {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   839
            // NOTE: we're relying on the fact that StandardOperation.GET_METHOD return value is constant for any given method
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   840
            // name and object linked with BeansLinker. (Actually, an even stronger assumption is true: return value is
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   841
            // constant for any given method name and object's class.)
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   842
            return MethodHandles.dropArguments(MethodHandles.constant(Object.class,
27815
6ed8e97e8fb6 8051778: support bind on all Nashorn callables
attila
parents: 27307
diff changeset
   843
                    Bootstrap.bindCallable(methodGetter.invoke(source), source, null)), 0, Object.class);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   844
        } catch(RuntimeException|Error e) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   845
            throw e;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   846
        } catch(final Throwable t) {
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   847
            throw new RuntimeException(t);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   848
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   849
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   850
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   851
    private static MethodHandle getBeanOperation(final GuardingDynamicLinker linker, final Operation operation,
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   852
            final String name, final MethodType methodType, final Object source) {
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   853
        final GuardedInvocation inv;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   854
        try {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 35407
diff changeset
   855
            inv = NashornBeansLinker.getGuardedInvocation(linker, createLinkRequest(operation.named(name), methodType, source), Bootstrap.getLinkerServices());
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   856
            assert passesGuard(source, inv.getGuard());
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   857
        } catch(RuntimeException|Error e) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   858
            throw e;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   859
        } catch(final Throwable t) {
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   860
            throw new RuntimeException(t);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   861
        }
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24734
diff changeset
   862
        assert inv.getSwitchPoints() == null; // Linkers in Dynalink's beans package don't use switchpoints.
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   863
        // We discard the guard, as all method handles will be bound to a specific object.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   864
        return inv.getInvocation();
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   865
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   866
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   867
    private static boolean passesGuard(final Object obj, final MethodHandle guard) throws Throwable {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   868
        return guard == null || (boolean)guard.invoke(obj);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   869
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   870
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   871
    private static LinkRequest createLinkRequest(final Operation operation, final MethodType methodType, final Object source) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   872
        return new SimpleLinkRequest(new CallSiteDescriptor(MethodHandles.publicLookup(), operation,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   873
                methodType), false, source);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   874
    }
24772
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   875
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   876
    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   877
        return MH.findStatic(MethodHandles.lookup(), NativeObject.class, name, MH.type(rtype, types));
0fc1013a1785 8044520: Nashorn cannot execute node.js's express module
sundar
parents: 24769
diff changeset
   878
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   879
}