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