nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JSObjectLinker.java
author sundar
Fri, 22 Aug 2014 15:47:28 +0530
changeset 26237 b6509e37ce64
parent 25865 d38d876f1654
child 27361 86c4ddb4797b
permissions -rw-r--r--
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly Reviewed-by: attila, hannesw
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import java.lang.invoke.MethodHandles;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.lang.invoke.MethodType;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    31
import java.util.HashMap;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    32
import java.util.Map;
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    33
import javax.script.Bindings;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    34
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    35
import jdk.internal.dynalink.linker.GuardedInvocation;
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21453
diff changeset
    36
import jdk.internal.dynalink.linker.GuardedTypeConversion;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    37
import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    38
import jdk.internal.dynalink.linker.LinkRequest;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    39
import jdk.internal.dynalink.linker.LinkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    40
import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    41
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    42
import jdk.nashorn.api.scripting.JSObject;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    43
import jdk.nashorn.internal.lookup.MethodHandleFactory;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    44
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
import jdk.nashorn.internal.runtime.JSType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
 * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
 * as ScriptObjects from other Nashorn contexts.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
 */
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    51
final class JSObjectLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory {
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    52
    private final NashornBeansLinker nashornBeansLinker;
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    53
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    54
    JSObjectLinker(final NashornBeansLinker nashornBeansLinker) {
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    55
        this.nashornBeansLinker = nashornBeansLinker;
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    56
    }
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    57
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    public boolean canLinkType(final Class<?> type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
        return canLinkTypeStatic(type);
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
    static boolean canLinkTypeStatic(final Class<?> type) {
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    64
        // can link JSObject also handles Map, Bindings to make
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    65
        // sure those are not JSObjects.
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    66
        return Map.class.isAssignableFrom(type) ||
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    67
               Bindings.class.isAssignableFrom(type) ||
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    68
               JSObject.class.isAssignableFrom(type);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
        final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
        final Object self = requestWithoutContext.getReceiver();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
        final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
        if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
            // We only support standard "dyn:*[:*]" operations
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
        final GuardedInvocation inv;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
        if (self instanceof JSObject) {
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
    84
            inv = lookup(desc, request, linkerServices);
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    85
        } else if (self instanceof Map || self instanceof Bindings) {
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    86
            // guard to make sure the Map or Bindings does not turn into JSObject later!
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    87
            final GuardedInvocation beanInv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    88
            inv = new GuardedInvocation(beanInv.getInvocation(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    89
                NashornGuards.combineGuards(beanInv.getGuard(), NashornGuards.getNotJSObjectGuard()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
            throw new AssertionError(); // Should never reach here.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
    94
        return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    97
    @Override
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21453
diff changeset
    98
    public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception {
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
    99
        final boolean sourceIsAlwaysJSObject = JSObject.class.isAssignableFrom(sourceType);
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   100
        if(!sourceIsAlwaysJSObject && !sourceType.isAssignableFrom(JSObject.class)) {
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   101
            return null;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   102
        }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   103
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   104
        final MethodHandle converter = CONVERTERS.get(targetType);
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   105
        if(converter == null) {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   106
            return null;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   107
        }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   108
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21453
diff changeset
   109
        return new GuardedTypeConversion(new GuardedInvocation(converter, sourceIsAlwaysJSObject ? null : IS_JSOBJECT_GUARD).asType(MethodType.methodType(targetType, sourceType)), true);
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   110
    }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   111
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   112
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   113
    private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
        final int c = desc.getNameTokenCount();
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   116
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
        switch (operator) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
            case "getProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
            case "getElem":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
            case "getMethod":
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   121
                if (c > 2) {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   122
                    return findGetMethod(desc);
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   123
                } else {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   124
                    // For indexed get, we want get GuardedInvocation beans linker and pass it.
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   125
                    // JSObjectLinker.get uses this fallback getter for explicit signature method access.
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   126
                    final GuardedInvocation beanInv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   127
                    return findGetIndexMethod(beanInv);
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   128
                }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
            case "setProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
            case "setElem":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
                return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
            case "call":
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   133
                return findCallMethod(desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
            case "new":
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   135
                return findNewMethod(desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
            default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
                return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   141
    private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) {
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   142
        final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   143
        final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   144
        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   147
    private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   148
        final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation());
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   149
        return inv.replaceMethods(getter, inv.getGuard());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   153
        final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, desc.getNameToken(2));
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   154
        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    private static GuardedInvocation findSetIndexMethod() {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   158
        return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   161
    private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   162
        // TODO: if call site is already a vararg, don't do asCollector
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   163
        MethodHandle mh = JSOBJECT_CALL;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   164
        if (NashornCallSiteDescriptor.isApplyToCall(desc)) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   165
            mh = MH.insertArguments(JSOBJECT_CALL_TO_APPLY, 0, JSOBJECT_CALL);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   166
        }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   167
        return new GuardedInvocation(MH.asCollector(mh, Object[].class, desc.getMethodType().parameterCount() - 2), IS_JSOBJECT_GUARD);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   168
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   169
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   170
    private static GuardedInvocation findNewMethod(final CallSiteDescriptor desc) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   171
        final MethodHandle func = MH.asCollector(JSOBJECT_NEW, Object[].class, desc.getMethodType().parameterCount() - 1);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   172
        return new GuardedInvocation(func, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    private static boolean isJSObject(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
        return self instanceof JSObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
    @SuppressWarnings("unused")
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   181
    private static Object get(final MethodHandle fallback, final Object jsobj, final Object key)
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   182
        throws Throwable {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   183
        if (key instanceof Integer) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19889
diff changeset
   184
            return ((JSObject)jsobj).getSlot((Integer)key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
        } else if (key instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
            final int index = getIndex((Number)key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
            if (index > -1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
                return ((JSObject)jsobj).getSlot(index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
            }
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   190
        } else if (key instanceof String) {
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   191
            final String name = (String)key;
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   192
            // get with method name and signature. delegate it to beans linker!
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   193
            if (name.indexOf('(') != -1) {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   194
                return fallback.invokeExact(jsobj, key);
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   195
            } else {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   196
                return ((JSObject)jsobj).getMember(name);
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   197
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
    private static void put(final Object jsobj, final Object key, final Object value) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   204
        if (key instanceof Integer) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19889
diff changeset
   205
            ((JSObject)jsobj).setSlot((Integer)key, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
        } else if (key instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
            ((JSObject)jsobj).setSlot(getIndex((Number)key), value);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   208
        } else if (key instanceof String) {
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   209
            ((JSObject)jsobj).setMember((String)key, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   213
    @SuppressWarnings("unused")
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   214
    private static int toInt32(final JSObject obj) {
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   215
        return JSType.toInt32(toNumber(obj));
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   216
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   217
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   218
    @SuppressWarnings("unused")
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   219
    private static long toLong(final JSObject obj) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   220
        return JSType.toLong(toNumber(obj));
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   221
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   222
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   223
    private static double toNumber(final JSObject obj) {
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   224
        return obj == null ? 0 : obj.toNumber();
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   225
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   226
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   227
    @SuppressWarnings("unused")
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   228
    private static boolean toBoolean(final JSObject obj) {
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   229
        return obj != null;
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   230
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   231
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    private static int getIndex(final Number n) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
        final double value = n.doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
        return JSType.isRepresentableAsInt(value) ? (int)value : -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   237
    @SuppressWarnings("unused")
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   238
    private static Object callToApply(final MethodHandle mh, final JSObject obj, final Object thiz, final Object... args) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   239
        assert args.length >= 2;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   240
        final Object   receiver  = args[0];
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   241
        final Object[] arguments = new Object[args.length - 1];
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   242
        System.arraycopy(args, 1, arguments, 0, arguments.length);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   243
        try {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   244
            return mh.invokeExact(obj, thiz, new Object[] { receiver, arguments });
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   245
        } catch (final RuntimeException | Error e) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   246
            throw e;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   247
        } catch (final Throwable e) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   248
            throw new RuntimeException(e);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   249
        }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   250
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   251
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   254
    // method handles of the current class
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   255
    private static final MethodHandle IS_JSOBJECT_GUARD  = findOwnMH_S("isJSObject", boolean.class, Object.class);
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   256
    private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, MethodHandle.class, Object.class, Object.class);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   257
    private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   258
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   259
    // method handles of JSObject class
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   260
    private static final MethodHandle JSOBJECT_GETMEMBER     = findJSObjectMH_V("getMember", Object.class, String.class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   261
    private static final MethodHandle JSOBJECT_SETMEMBER     = findJSObjectMH_V("setMember", Void.TYPE, String.class, Object.class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   262
    private static final MethodHandle JSOBJECT_CALL          = findJSObjectMH_V("call", Object.class, Object.class, Object[].class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   263
    private static final MethodHandle JSOBJECT_CALL_TO_APPLY = findOwnMH_S("callToApply", Object.class, MethodHandle.class, JSObject.class, Object.class, Object[].class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   264
    private static final MethodHandle JSOBJECT_NEW           = findJSObjectMH_V("newObject", Object.class, Object[].class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   266
    private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>();
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   267
    static {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   268
        CONVERTERS.put(boolean.class, findOwnMH_S("toBoolean", boolean.class, JSObject.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   269
        CONVERTERS.put(int.class,     findOwnMH_S("toInt32", int.class, JSObject.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   270
        CONVERTERS.put(long.class,    findOwnMH_S("toLong", long.class, JSObject.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   271
        CONVERTERS.put(double.class,  findOwnMH_S("toNumber", double.class, JSObject.class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    }
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   273
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   274
    private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   275
        return MH.findVirtual(MethodHandles.lookup(), JSObject.class, name, MH.type(rtype, types));
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   276
    }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   277
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   278
    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   279
        return MH.findStatic(MethodHandles.lookup(), JSObjectLinker.class, name, MH.type(rtype, types));
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   280
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
}