8157251: BeanLinker relinks array length operations for array types

Reviewed-by: hannesw, jlaskey, attila
This commit is contained in:
Priya Lakshmi Muthuswamy 2018-01-12 10:33:06 +01:00 committed by Hannes Wallnöfer
parent a34fdd1809
commit f815c509cf
3 changed files with 41 additions and 1 deletions

View File

@ -116,7 +116,7 @@ class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicL
if(clazz.isArray()) {
// Some languages won't have a notion of manipulating collections. Exposing "length" on arrays as an
// explicit property is beneficial for them.
setPropertyGetter("length", MethodHandles.arrayLength(clazz), ValidationType.EXACT_CLASS);
setPropertyGetter("length", GET_ARRAY_LENGTH, ValidationType.IS_ARRAY);
} else if(Collection.class.isAssignableFrom(clazz)) {
setPropertyGetter("length", GET_COLLECTION_LENGTH, ValidationType.INSTANCE_OF);
} else if(Map.class.isAssignableFrom(clazz)) {
@ -546,6 +546,9 @@ class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicL
private static final MethodHandle GET_MAP_LENGTH = Lookup.PUBLIC.findVirtual(Map.class, "size",
MethodType.methodType(int.class));
private static final MethodHandle GET_ARRAY_LENGTH = Lookup.PUBLIC.findStatic(Array.class, "getLength",
MethodType.methodType(int.class, Object.class));
private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
if(descriptor.getMethodType().parameterCount() != paramCount) {
throw new BootstrapMethodError(descriptor.getOperation() + " must have exactly " + paramCount + " parameters.");

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8157251: BeanLinker relinks array length operations for array types
*
* @test
* @run
*/
var intArray = Java.type("int[]")
var doubleArray = Java.type("double[]")
var arrs = [new intArray(20), new doubleArray(0)]
for (var i in arrs)
print(arrs[i].length)

View File

@ -0,0 +1,2 @@
20
0