nashorn/src/jdk/nashorn/internal/objects/NativeObject.java
author attila
Mon, 15 Jul 2013 12:33:48 +0200
changeset 18876 ada98218aaae
parent 18860 e387fde9322a
child 18878 078eb52cf5bc
permissions -rw-r--r--
8020324: Implement Object.bindProperties(target, source) for beans Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.objects;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    30
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    31
import java.lang.invoke.MethodHandle;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    32
import java.lang.invoke.MethodHandles;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    33
import java.lang.invoke.MethodType;
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
    34
import java.util.ArrayList;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    35
import java.util.Collection;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    36
import java.util.HashSet;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    37
import java.util.List;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    38
import java.util.Set;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    39
import jdk.internal.dynalink.beans.BeansLinker;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    40
import jdk.internal.dynalink.beans.StaticClass;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    41
import jdk.internal.dynalink.linker.GuardedInvocation;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    42
import jdk.internal.dynalink.linker.GuardingDynamicLinker;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    43
import jdk.internal.dynalink.linker.LinkRequest;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    44
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    45
import jdk.internal.dynalink.support.LinkRequestImpl;
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    46
import jdk.nashorn.api.scripting.ScriptObjectMirror;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    47
import jdk.nashorn.internal.lookup.Lookup;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
import jdk.nashorn.internal.objects.annotations.Attribute;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
import jdk.nashorn.internal.objects.annotations.Constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import jdk.nashorn.internal.objects.annotations.Function;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
import jdk.nashorn.internal.objects.annotations.ScriptClass;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
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
    53
import jdk.nashorn.internal.runtime.AccessorProperty;
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    54
import jdk.nashorn.internal.runtime.ECMAException;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
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
    56
import jdk.nashorn.internal.runtime.Property;
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
    57
import jdk.nashorn.internal.runtime.PropertyMap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
import jdk.nashorn.internal.runtime.ScriptFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
import jdk.nashorn.internal.runtime.ScriptObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
import jdk.nashorn.internal.runtime.ScriptRuntime;
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.linker.Bootstrap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
import jdk.nashorn.internal.runtime.linker.InvokeByName;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
 * ECMA 15.2 Object objects
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
 * JavaScript Object constructor/prototype. Note: instances of this class are
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
 * never created. This class is not even a subclass of ScriptObject. But, we use
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
 * this class to generate prototype and constructor for "Object".
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
@ScriptClass("Object")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
public final class NativeObject {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
    private static final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
    75
    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
    76
    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
    77
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
    78
    // 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
    79
    @SuppressWarnings("unused")
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
    80
    private static PropertyMap $nasgenmap$;
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18334
diff changeset
    81
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    private NativeObject() {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
    83
        // don't create me!
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
    84
        throw new UnsupportedOperationException();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    87
    private static ECMAException notAnObject(final Object obj) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    88
        return typeError("not.an.object", ScriptRuntime.safeToString(obj));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    89
    }
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
    90
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
     * ECMA 15.2.3.2 Object.getPrototypeOf ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
     * @param  self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
     * @param  obj object to get prototype from
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
     * @return the prototype of an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
    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
   100
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   101
            return ((ScriptObject)obj).getProto();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   102
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   103
            return ((ScriptObjectMirror)obj).getProto();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   104
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   105
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   106
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
     * ECMA 15.2.3.3 Object.getOwnPropertyDescriptor ( O, P )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
     * @param obj   object from which to get property descriptor for {@code ToString(prop)}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
     * @param prop  property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
     * @return property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
    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
   119
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   120
            final String       key  = JSType.toString(prop);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   121
            final ScriptObject sobj = (ScriptObject)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   123
            return sobj.getOwnPropertyDescriptor(key);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   124
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   125
            final String       key  = JSType.toString(prop);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   126
            final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   128
            return sobjMirror.getOwnPropertyDescriptor(key);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   129
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   130
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   131
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
     * ECMA 15.2.3.4 Object.getOwnPropertyNames ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
     * @param obj  object to query for property names
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
     * @return array of property names
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
    public static Object getOwnPropertyNames(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   143
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   144
            return new NativeArray(((ScriptObject)obj).getOwnKeys(true));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   145
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   146
            return new NativeArray(((ScriptObjectMirror)obj).getOwnKeys(true));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   147
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   148
            throw notAnObject(obj);
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
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
     * ECMA 15.2.3.5 Object.create ( O [, Properties] )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
     * @param proto prototype object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
     * @param props properties to define
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
     * @return object created
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
    public static Object create(final Object self, final Object proto, final Object props) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
        if (proto != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
            Global.checkObject(proto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
        // FIXME: should we create a proper object with correct number of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
        // properties?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
        final ScriptObject newObj = Global.newEmptyInstance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
        newObj.setProtoCheck(proto);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
        if (props != UNDEFINED) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
            NativeObject.defineProperties(self, newObj, props);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
        return newObj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * ECMA 15.2.3.6 Object.defineProperty ( O, P, Attributes )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
     * @param obj  object in which to define a property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
     * @param prop property to define
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
     * @param attr attributes for property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
     * @return object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    public static Object defineProperty(final Object self, final Object obj, final Object prop, final Object attr) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
        Global.checkObject(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
        ((ScriptObject)obj).defineOwnProperty(JSType.toString(prop), attr, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
        return obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
     * ECMA 5.2.3.7 Object.defineProperties ( O, Properties )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
     * @param obj   object in which to define properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
     * @param props properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
     * @return object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
    public static Object defineProperties(final Object self, final Object obj, final Object props) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
        Global.checkObject(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        final ScriptObject sobj     = (ScriptObject)obj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
        final Object       propsObj = Global.toObject(props);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
        if (propsObj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
            final Object[] keys = ((ScriptObject)propsObj).getOwnKeys(false);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
            for (final Object key : keys) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
                final String prop = JSType.toString(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
                sobj.defineOwnProperty(prop, ((ScriptObject)propsObj).get(prop), true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
        return sobj;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
     * ECMA 15.2.3.8 Object.seal ( O )
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 to seal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
     * @return sealed object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
    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
   227
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   228
            return ((ScriptObject)obj).seal();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   229
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   230
            return ((ScriptObjectMirror)obj).seal();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   231
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   232
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   233
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
     * ECMA 15.2.3.9 Object.freeze ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
     * @param obj object to freeze
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
     * @return frozen object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    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
   246
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   247
            return ((ScriptObject)obj).freeze();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   248
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   249
            return ((ScriptObjectMirror)obj).freeze();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   250
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   251
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   252
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
     * ECMA 15.2.3.10 Object.preventExtensions ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     * @param obj  object, for which to set the internal extensible property to false
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     * @return object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
    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
   264
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   265
            return ((ScriptObject)obj).preventExtensions();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   266
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   267
            return ((ScriptObjectMirror)obj).preventExtensions();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   268
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   269
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   270
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
     * ECMA 15.2.3.11 Object.isSealed ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
     * @param obj check whether an object is sealed
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
     * @return true if sealed, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
    public static Object isSealed(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   282
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   283
            return ((ScriptObject)obj).isSealed();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   284
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   285
            return ((ScriptObjectMirror)obj).isSealed();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   286
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   287
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   288
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
    }
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
     * ECMA 15.2.3.12 Object.isFrozen ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
     * @param obj check whether an object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
     * @return true if object is frozen, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    public static Object isFrozen(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   300
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   301
            return ((ScriptObject)obj).isFrozen();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   302
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   303
            return ((ScriptObjectMirror)obj).isFrozen();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   304
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   305
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   306
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     * ECMA 15.2.3.13 Object.isExtensible ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
     * @param obj check whether an object is extensible
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
     * @return true if object is extensible, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
    public static Object isExtensible(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   318
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   319
            return ((ScriptObject)obj).isExtensible();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   320
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   321
            return ((ScriptObjectMirror)obj).isExtensible();
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   322
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   323
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   324
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
     * ECMA 15.2.3.14 Object.keys ( O )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
     * @param obj  object from which to extract keys
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
     * @return array of keys in object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
    public static Object keys(final Object self, final Object obj) {
18334
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   336
        if (obj instanceof ScriptObject) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   337
            final ScriptObject sobj = (ScriptObject)obj;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   338
            return new NativeArray(sobj.getOwnKeys(false));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   339
        } else if (obj instanceof ScriptObjectMirror) {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   340
            final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   341
            return new NativeArray(sobjMirror.getOwnKeys(false));
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   342
        } else {
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   343
            throw notAnObject(obj);
47413e8d71b5 8016618: script mirror object access should be improved
sundar
parents: 16256
diff changeset
   344
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
     * ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
     * @param newObj is the new object instantiated with the new operator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
     * @param value  value of object to be instantiated
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
     * @return the new NativeObject
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
    @Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
    public static Object construct(final boolean newObj, final Object self, final Object value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
        final JSType type = JSType.of(value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
        // Object(null), Object(undefined), Object() are same as "new Object()"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
        if (newObj || (type == JSType.NULL || type == JSType.UNDEFINED)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
            switch (type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
            case BOOLEAN:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
            case NUMBER:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
            case STRING:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
                return Global.toObject(value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
            case OBJECT:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
            case FUNCTION:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
                return value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
            case NULL:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
            case UNDEFINED:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
                // fall through..
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
            default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
                break;
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
            return Global.newEmptyInstance();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
        return Global.toObject(value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
     * ECMA 15.2.4.2 Object.prototype.toString ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
     * @return ToString of object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    public static Object toString(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
        return ScriptRuntime.builtinObjectToString(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
    }
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
     * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
     * @return localized ToString
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
    public static Object toLocaleString(final Object self) {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16151
diff changeset
   404
        final Object obj = JSType.toScriptObject(self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
            final ScriptObject sobj = (ScriptObject)self;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
            try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
                final Object toString = TO_STRING.getGetter().invokeExact(sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
                if (toString instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
                    return TO_STRING.getInvoker().invokeExact(toString, sobj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
            } catch (final RuntimeException | Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
                throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
            } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
                throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   419
            throw typeError("not.a.function", "toString");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
        return ScriptRuntime.builtinObjectToString(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
     * ECMA 15.2.4.4 Object.prototype.valueOf ( )
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
     * @return value of object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
    public static Object valueOf(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
        return Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
     * ECMA 15.2.4.5 Object.prototype.hasOwnProperty (V)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
     * @param v property to check for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
     * @return true if property exists in object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
    public static Object hasOwnProperty(final Object self, final Object v) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
        final String str = JSType.toString(v);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
        return (obj instanceof ScriptObject) && ((ScriptObject)obj).hasOwnProperty(str);
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.4.6 Object.prototype.isPrototypeOf (V)
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 v v prototype object to check against
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
     * @return true if object is prototype of v
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
    public static Object isPrototypeOf(final Object self, final Object v) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
        if (!(v instanceof ScriptObject)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
        final Object obj   = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
        ScriptObject proto = (ScriptObject)v;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
        do {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
            proto = proto.getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
            if (proto == obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   471
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
        } while (proto != null);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
     * ECMA 15.2.4.7 Object.prototype.propertyIsEnumerable (V)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
     * @param v property to check if enumerable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
     * @return true if property is enumerable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
    public static Object propertyIsEnumerable(final Object self, final Object v) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
        final String str = JSType.toString(v);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
        final Object obj = Global.toObject(self);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
        if (obj instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
            final jdk.nashorn.internal.runtime.Property property = ((ScriptObject)obj).getMap().findProperty(str);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
            return property != null && property.isEnumerable();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   492
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
    }
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   496
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   497
    /**
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   498
     * Nashorn extension: Object.bindProperties
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   499
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   500
     * 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
   501
     * 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
   502
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   503
     * Example:
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   504
     * <pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   505
     * var obj = { x: 34, y: 100 };
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   506
     * var foo = {}
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   507
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   508
     * // 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
   509
     * Object.bindProperties(foo, obj);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   510
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   511
     * // 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
   512
     * 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
   513
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   514
     * // update obj.x via foo.x
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   515
     * foo.x = "hello";
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   516
     * print(obj.x); // prints "hello" now
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   517
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   518
     * 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
   519
     * print(foo.x); // prints 42
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   520
     * </pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   521
     * <p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   522
     * 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
   523
     * 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
   524
     * </p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   525
     * Example:
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   526
     * <pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   527
     * var obj = loadWithNewGlobal({
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   528
     *    name: "test",
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   529
     *    script: "obj = { x: 33, y: 'hello' }"
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   530
     * });
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   531
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   532
     * // 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
   533
     * Object.bindProperties(this, obj);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   534
     * print(x);         // prints 33
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   535
     * print(y);         // prints "hello"
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   536
     * 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
   537
     * print(obj.x);     // prints Math.PI
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   538
     * </pre>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   539
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   540
     * Limitations of property binding:
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   541
     * <ul>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   542
     * <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
   543
     * <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
   544
     * <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
   545
     * <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
   546
     * <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
   547
     * 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
   548
     * </ul>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   549
     * <p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   550
     * 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
   551
     * properties to avoid surprises.
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   552
     * </p>
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   553
     *
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   554
     * @param self self reference
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   555
     * @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
   556
     * @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
   557
     * @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
   558
     */
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   559
    @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   560
    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
   561
        // target object has to be a ScriptObject
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   562
        Global.checkObject(target);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   563
        // check null or undefined source object
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   564
        Global.checkObjectCoercible(source);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   565
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   566
        final ScriptObject targetObj = (ScriptObject)target;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   567
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   568
        if (source instanceof ScriptObject) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   569
            final ScriptObject sourceObj = (ScriptObject)source;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   570
            final Property[] properties = sourceObj.getMap().getProperties();
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   571
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   572
            // filter non-enumerable properties
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   573
            final ArrayList<Property> propList = new ArrayList<>();
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   574
            for (Property prop : properties) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   575
                if (prop.isEnumerable()) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   576
                    propList.add(prop);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   577
                }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   578
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   579
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   580
            if (! propList.isEmpty()) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   581
                targetObj.addBoundProperties(sourceObj, propList.toArray(new Property[propList.size()]));
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   582
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   583
        } else if (source instanceof ScriptObjectMirror) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   584
            // get enumerable, immediate properties of mirror
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   585
            final ScriptObjectMirror mirror = (ScriptObjectMirror)source;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   586
            final String[] keys = mirror.getOwnKeys(false);
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   587
            if (keys.length == 0) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   588
                // nothing to bind
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   589
                return target;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   590
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   591
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   592
            // 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
   593
            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
   594
            for (int idx = 0; idx < keys.length; idx++) {
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   595
                final String name = keys[idx];
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   596
                final MethodHandle getter = Bootstrap.createDynamicInvoker("dyn:getMethod|getProp|getElem:" + name, MIRROR_GETTER_TYPE);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   597
                final MethodHandle setter = Bootstrap.createDynamicInvoker("dyn:setProp|setElem:" + name, MIRROR_SETTER_TYPE);
18860
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   598
                props[idx] = (AccessorProperty.create(name, 0, getter, setter));
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   599
            }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   600
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   601
            targetObj.addBoundProperties(source, props);
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   602
        } else if (source instanceof StaticClass) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   603
            final Class<?> clazz = ((StaticClass)source).getRepresentedClass();
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   604
            bindBeanProperties(targetObj, source, BeansLinker.getReadableStaticPropertyNames(clazz),
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   605
                    BeansLinker.getWritableStaticPropertyNames(clazz), BeansLinker.getStaticMethodNames(clazz));
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   606
        } else {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   607
            final Class<?> clazz = source.getClass();
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   608
            bindBeanProperties(targetObj, source, BeansLinker.getReadableInstancePropertyNames(clazz),
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   609
                    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
   610
        }
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   611
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   612
        return target;
e387fde9322a 8014785: Ability to extend global instance by binding properties of another object
sundar
parents: 18851
diff changeset
   613
    }
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   614
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   615
    private static void bindBeanProperties(final ScriptObject targetObj, final Object source,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   616
            final Collection<String> readablePropertyNames, final Collection<String> writablePropertyNames,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   617
            final Collection<String> methodNames) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   618
        final Set<String> propertyNames = new HashSet<>(readablePropertyNames);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   619
        propertyNames.addAll(writablePropertyNames);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   620
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   621
        final Class<?> clazz = source.getClass();
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   622
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   623
        final MethodType getterType = MethodType.methodType(Object.class, clazz);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   624
        final MethodType setterType = MethodType.methodType(Object.class, clazz, Object.class);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   625
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   626
        final GuardingDynamicLinker linker = BeansLinker.getLinkerForClass(clazz);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   627
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   628
        final List<AccessorProperty> properties = new ArrayList<>(propertyNames.size() + methodNames.size());
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   629
        for(final String methodName: methodNames) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   630
            properties.add(AccessorProperty.create(methodName, Property.NOT_WRITABLE,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   631
                    getBoundBeanMethodGetter(source, getBeanOperation(linker, "dyn:getMethod:" + methodName, getterType, source)),
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   632
                    null));
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   633
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   634
        for(final String propertyName: propertyNames) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   635
            final boolean isWritable = writablePropertyNames.contains(propertyName);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   636
            properties.add(AccessorProperty.create(propertyName, isWritable ? 0 : Property.NOT_WRITABLE,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   637
                    readablePropertyNames.contains(propertyName) ? getBeanOperation(linker, "dyn:getProp:" + propertyName, getterType, source) : Lookup.EMPTY_GETTER,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   638
                    isWritable ? getBeanOperation(linker, "dyn:setProp:" + propertyName, setterType, source) : Lookup.EMPTY_SETTER));
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   639
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   640
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   641
        targetObj.addBoundProperties(source, properties.toArray(new AccessorProperty[properties.size()]));
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   642
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   643
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   644
    private static MethodHandle getBoundBeanMethodGetter(Object source, MethodHandle methodGetter) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   645
        try {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   646
            // NOTE: we're relying on the fact that "dyn:getMethod:..." return value is constant for any given method
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   647
            // 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
   648
            // constant for any given method name and object's class.)
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   649
            return MethodHandles.dropArguments(MethodHandles.constant(Object.class,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   650
                    Bootstrap.bindDynamicMethod(methodGetter.invoke(source), source)), 0, Object.class);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   651
        } catch(RuntimeException|Error e) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   652
            throw e;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   653
        } catch(Throwable t) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   654
            throw new RuntimeException(t);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   655
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   656
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   657
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   658
    private static MethodHandle getBeanOperation(final GuardingDynamicLinker linker, final String operation,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   659
            final MethodType methodType, final Object source) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   660
        final GuardedInvocation inv;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   661
        try {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   662
            inv = linker.getGuardedInvocation(createLinkRequest(operation, methodType, source),
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   663
                Bootstrap.getLinkerServices());
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   664
            assert passesGuard(source, inv.getGuard());
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   665
        } catch(RuntimeException|Error e) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   666
            throw e;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   667
        } catch(Throwable t) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   668
            throw new RuntimeException(t);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   669
        }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   670
        assert inv.getSwitchPoint() == null; // Linkers in Dynalink's beans package don't use switchpoints.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   671
        // 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
   672
        return inv.getInvocation();
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   673
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   674
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   675
    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
   676
        return guard == null || (boolean)guard.invoke(obj);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   677
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   678
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   679
    private static LinkRequest createLinkRequest(String operation, MethodType methodType, Object source) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   680
        return new LinkRequestImpl(CallSiteDescriptorFactory.create(MethodHandles.publicLookup(), operation,
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   681
                methodType), false, source);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18860
diff changeset
   682
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   683
}