nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java
author hannesw
Tue, 16 Dec 2014 17:02:54 +0100
changeset 28127 08a5e0d3248d
parent 26768 751b0f427090
child 33330 35531ae624ef
permissions -rw-r--r--
8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory Reviewed-by: attila, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime.linker;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
16277
fd698c5ee684 8009559: clean up method handle lookup code.
sundar
parents: 16234
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    30
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.lang.invoke.MethodHandle;
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    32
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.lang.invoke.MethodType;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
    34
import java.lang.invoke.SwitchPoint;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16207
diff changeset
    35
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16207
diff changeset
    36
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16207
diff changeset
    37
import jdk.internal.dynalink.linker.LinkRequest;
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    38
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16207
diff changeset
    39
import jdk.internal.dynalink.support.Guards;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
    40
import jdk.nashorn.internal.runtime.Context;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
    41
import jdk.nashorn.internal.runtime.FindProperty;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
    42
import jdk.nashorn.internal.runtime.GlobalConstants;
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    43
import jdk.nashorn.internal.runtime.JSType;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import jdk.nashorn.internal.runtime.ScriptObject;
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    45
import jdk.nashorn.internal.runtime.ScriptRuntime;
26508
b40ef4386b01 8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents: 25865
diff changeset
    46
import jdk.nashorn.internal.runtime.UserAccessorProperty;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
 * Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
 * numbers). This class is only public so it can be accessed by classes in the {@code jdk.nashorn.internal.objects}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
 * package.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
 */
19105
a01ba9d17eab 8021571: @fork tests should use VM options passed from project.properties
sundar
parents: 16534
diff changeset
    53
public final class PrimitiveLookup {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    55
    /** Method handle to link setters on primitive base. See ES5 8.7.2. */
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    56
    private static final MethodHandle PRIMITIVE_SETTER = findOwnMH("primitiveSetter",
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    57
            MH.type(void.class, ScriptObject.class, Object.class, Object.class, boolean.class, Object.class));
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    58
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
    59
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    private PrimitiveLookup() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
     * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value.
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16186
diff changeset
    65
     * @param request the link request for the dynamic call site.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
     * @param receiverClass the class of the receiver value (e.g., {@link java.lang.Boolean}, {@link java.lang.String} etc.)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
     * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
     * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
     * primitive base value. This instance will be used to delegate actual lookup.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
     * @param wrapFilter A method handle that takes a primitive value of type specified in the {@code receiverClass} and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
     * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
     * method - it will be combined into the returned invocation as an argument filter on the receiver.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
     * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 23372
diff changeset
    74
     * @param protoFilter A method handle that walks up the proto chain of this receiver object
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
     * type {@code receiverClass}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
     */
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16186
diff changeset
    77
    public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass,
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
    78
                                                    final ScriptObject wrappedReceiver, final MethodHandle wrapFilter,
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
    79
                                                    final MethodHandle protoFilter) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
    80
        return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter, protoFilter);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
     * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value.
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16186
diff changeset
    85
     * @param request the link request for the dynamic call site.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
     * @param guard an explicit guard that will be used for the returned guarded invocation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
     * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
     * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
     * primitive base value. This instance will be used to delegate actual lookup.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
     * @param wrapFilter A method handle that takes a primitive value of type guarded by the {@code guard} and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
     * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
     * method - it will be combined into the returned invocation as an argument filter on the receiver.
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 23372
diff changeset
    93
     * @param protoFilter A method handle that walks up the proto chain of this receiver object
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
     * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
     * type (that is implied by both {@code guard} and {@code wrappedReceiver}).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
     */
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16186
diff changeset
    97
    public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard,
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
    98
                                                    final ScriptObject wrappedReceiver, final MethodHandle wrapFilter,
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
    99
                                                    final MethodHandle protoFilter) {
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16186
diff changeset
   100
        final CallSiteDescriptor desc = request.getCallSiteDescriptor();
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   101
        final String name;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   102
        final FindProperty find;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   104
        if (desc.getNameTokenCount() > 2) {
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   105
            name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   106
            find = wrappedReceiver.findProperty(name, true);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   107
        } else {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   108
            name = null;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   109
            find = null;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   110
        }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   111
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   112
        final String firstOp = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   113
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   114
        switch (firstOp) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   115
        case "getProp":
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   116
        case "getElem":
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   117
        case "getMethod":
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   118
            //checks whether the property name is hard-coded in the call-site (i.e. a getProp vs a getElem, or setProp vs setElem)
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   119
            //if it is we can make assumptions on the property: that if it is not defined on primitive wrapper itself it never will be.
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   120
            //so in that case we can skip creation of primitive wrapper and start our search with the prototype.
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   121
            if (name != null) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   122
                if (find == null) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   123
                    // Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it.
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   124
                    return null;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   125
                }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   126
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   127
                final SwitchPoint sp = find.getProperty().getBuiltinSwitchPoint(); //can use this instead of proto filter
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   128
                if (sp instanceof Context.BuiltinSwitchPoint && !sp.hasBeenInvalidated()) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   129
                    return new GuardedInvocation(GlobalConstants.staticConstantGetter(find.getObjectValue()), guard, sp, null);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   130
                }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
   131
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   132
                if (find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   133
                    // If property is found in the prototype object bind the method handle directly to
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   134
                    // the proto filter instead of going through wrapper instantiation below.
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   135
                    final ScriptObject proto = wrappedReceiver.getProto();
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   136
                    final GuardedInvocation link = proto.lookup(desc, request);
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   137
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   138
                    if (link != null) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   139
                        final MethodHandle invocation = link.getInvocation(); //this contains the builtin switchpoint
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   140
                        final MethodHandle adaptedInvocation = MH.asType(invocation, invocation.type().changeParameterType(0, Object.class));
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   141
                        final MethodHandle method = MH.filterArguments(adaptedInvocation, 0, protoFilter);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   142
                        final MethodHandle protoGuard = MH.filterArguments(link.getGuard(), 0, protoFilter);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   143
                        return new GuardedInvocation(method, NashornGuards.combineGuards(guard, protoGuard));
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   144
                    }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 19105
diff changeset
   145
                }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
            }
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   147
            break;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   148
        case "setProp":
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   149
        case "setElem":
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   150
            return getPrimitiveSetter(name, guard, wrapFilter, NashornCallSiteDescriptor.isStrict(desc));
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   151
        default:
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   152
            break;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
        }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   154
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16186
diff changeset
   155
        final GuardedInvocation link = wrappedReceiver.lookup(desc, request);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
        if (link != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
            MethodHandle method = link.getInvocation();
16186
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16151
diff changeset
   158
            final Class<?> receiverType = method.type().parameterType(0);
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16195
diff changeset
   159
            if (receiverType != Object.class) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
                final MethodType wrapType = wrapFilter.type();
16186
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16151
diff changeset
   161
                assert receiverType.isAssignableFrom(wrapType.returnType());
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16151
diff changeset
   162
                method = MH.filterArguments(method, 0, MH.asType(wrapFilter, wrapType.changeReturnType(receiverType)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
            }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   164
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24731
diff changeset
   165
            return new GuardedInvocation(method, guard, link.getSwitchPoints(), null);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
        }
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26508
diff changeset
   167
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
    }
28127
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   170
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   171
    private static GuardedInvocation getPrimitiveSetter(final String name, final MethodHandle guard,
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   172
                                                        final MethodHandle wrapFilter, final boolean isStrict) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   173
        MethodHandle filter = MH.asType(wrapFilter, wrapFilter.type().changeReturnType(ScriptObject.class));
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   174
        final MethodHandle target;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   175
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   176
        if (name == null) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   177
            filter = MH.dropArguments(filter, 1, Object.class, Object.class);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   178
            target = MH.insertArguments(PRIMITIVE_SETTER, 3, isStrict);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   179
        } else {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   180
            filter = MH.dropArguments(filter, 1, Object.class);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   181
            target = MH.insertArguments(PRIMITIVE_SETTER, 2, name, isStrict);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   182
        }
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   183
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   184
        return new GuardedInvocation(MH.foldArguments(target, filter), guard);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   185
    }
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   186
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   187
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   188
    @SuppressWarnings("unused")
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   189
    private static void primitiveSetter(final ScriptObject wrappedSelf, final Object self, final Object key,
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   190
                                        final boolean strict, final Object value) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   191
        // See ES5.1 8.7.2 PutValue (V, W)
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   192
        final String name = JSType.toString(key);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   193
        final FindProperty find = wrappedSelf.findProperty(name, true);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   194
        if (find == null || !(find.getProperty() instanceof UserAccessorProperty) || !find.getProperty().isWritable()) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   195
            if (strict) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   196
                throw typeError("property.not.writable", name, ScriptRuntime.safeToString(self));
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   197
            }
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   198
            return;
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   199
        }
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   200
        // property found and is a UserAccessorProperty
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   201
        find.setValue(value, strict);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   202
    }
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   203
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   204
    private static MethodHandle findOwnMH(final String name, final MethodType type) {
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   205
        return MH.findStatic(MethodHandles.lookup(), PrimitiveLookup.class, name, type);
08a5e0d3248d 8066226: Fuzzing bug: parameter counts differ in TypeConverterFactory
hannesw
parents: 26768
diff changeset
   206
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
}