src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JSObjectLinker.java
author attila
Wed, 20 Dec 2017 17:36:50 +0100
changeset 48411 4ff5c5206427
parent 47216 71c04702a3d5
permissions -rw-r--r--
8193371: Use Dynalink REMOVE operation in Nashorn Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
36024
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
     2
 * Copyright (c) 2010, 2016, 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
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29133
diff changeset
    28
import static jdk.nashorn.internal.runtime.JSType.isString;
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29133
diff changeset
    29
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.lang.invoke.MethodHandles;
36024
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
    32
import java.lang.invoke.MethodType;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    33
import java.util.Map;
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
    34
import java.util.Objects;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    35
import jdk.dynalink.CallSiteDescriptor;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
    36
import jdk.dynalink.Operation;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    37
import jdk.dynalink.StandardOperation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    38
import jdk.dynalink.linker.GuardedInvocation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    39
import jdk.dynalink.linker.LinkRequest;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    40
import jdk.dynalink.linker.LinkerServices;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    41
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    42
import jdk.nashorn.api.scripting.JSObject;
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
    43
import jdk.nashorn.api.scripting.ScriptObjectMirror;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    44
import jdk.nashorn.internal.lookup.MethodHandleFactory;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    45
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
    46
import jdk.nashorn.internal.runtime.Context;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
import jdk.nashorn.internal.runtime.JSType;
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
    48
import jdk.nashorn.internal.runtime.ScriptRuntime;
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
    49
import jdk.nashorn.internal.objects.Global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
 * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
 * as ScriptObjects from other Nashorn contexts.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
 */
29133
1cd7d8af99ba 8072426: Can't compare Java objects to strings or numbers
attila
parents: 28125
diff changeset
    55
final class JSObjectLinker implements TypeBasedGuardingDynamicLinker {
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    56
    private final NashornBeansLinker nashornBeansLinker;
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    57
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    58
    JSObjectLinker(final NashornBeansLinker nashornBeansLinker) {
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    59
        this.nashornBeansLinker = nashornBeansLinker;
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    60
    }
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    61
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
    public boolean canLinkType(final Class<?> type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
        return canLinkTypeStatic(type);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
    67
    private static boolean canLinkTypeStatic(final Class<?> type) {
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
    68
        // can link JSObject also handles Map (this includes Bindings) to make
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    69
        // sure those are not JSObjects.
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    70
        return Map.class.isAssignableFrom(type) ||
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    71
               JSObject.class.isAssignableFrom(type);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
    public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
33332
f180be6368d8 8139588: Remove concept of runtime context arguments, call site tokens, and link counts
attila
parents: 33330
diff changeset
    76
        final Object self = request.getReceiver();
f180be6368d8 8139588: Remove concept of runtime context arguments, call site tokens, and link counts
attila
parents: 33330
diff changeset
    77
        final CallSiteDescriptor desc = request.getCallSiteDescriptor();
37833
ae7b3314b209 8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents: 36024
diff changeset
    78
        if (self == null || !canLinkTypeStatic(self.getClass())) {
ae7b3314b209 8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents: 36024
diff changeset
    79
            return null;
ae7b3314b209 8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents: 36024
diff changeset
    80
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
32889
aef39bbfac15 8137258: JSObjectLinker and BrowserJSObjectLinker should not expose internal JS objects
sundar
parents: 29282
diff changeset
    82
        GuardedInvocation inv;
16147
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);
32889
aef39bbfac15 8137258: JSObjectLinker and BrowserJSObjectLinker should not expose internal JS objects
sundar
parents: 29282
diff changeset
    85
            inv = inv.replaceMethods(linkerServices.filterInternalObjects(inv.getInvocation()), inv.getGuard());
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
    86
        } else if (self instanceof Map) {
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    87
            // 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
    88
            final GuardedInvocation beanInv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    89
            inv = new GuardedInvocation(beanInv.getInvocation(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    90
                NashornGuards.combineGuards(beanInv.getGuard(), NashornGuards.getNotJSObjectGuard()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        } else {
37833
ae7b3314b209 8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents: 36024
diff changeset
    92
            throw new AssertionError("got instanceof: " + self.getClass()); // Should never reach here.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
    95
        return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
    98
    private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
    99
        final Operation op = NashornCallSiteDescriptor.getBaseOperation(desc);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   100
        if (op instanceof StandardOperation) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   101
            final String name = NashornCallSiteDescriptor.getOperand(desc);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   102
            switch ((StandardOperation)op) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   103
            case GET:
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   104
                if (NashornCallSiteDescriptor.hasStandardNamespace(desc)) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   105
                    if (name != null) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   106
                        return findGetMethod(name);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   107
                    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   108
                    // For indexed get, we want get GuardedInvocation beans linker and pass it.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   109
                    // JSObjectLinker.get uses this fallback getter for explicit signature method access.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   110
                    return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   111
                }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   112
                break;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   113
            case SET:
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   114
                if (NashornCallSiteDescriptor.hasStandardNamespace(desc)) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   115
                    return name != null ? findSetMethod(name) : findSetIndexMethod();
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   116
                }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   117
                break;
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   118
            case REMOVE:
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   119
                if (NashornCallSiteDescriptor.hasStandardNamespace(desc)) {
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   120
                    return new GuardedInvocation(
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   121
                            name == null ? JSOBJECTLINKER_DEL : MH.insertArguments(JSOBJECTLINKER_DEL, 1, name),
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   122
                            IS_JSOBJECT_GUARD);
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   123
                }
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   124
                break;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   125
            case CALL:
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   126
                return findCallMethod(desc);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   127
            case NEW:
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   128
                return findNewMethod(desc);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 37833
diff changeset
   129
            default:
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33332
diff changeset
   130
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
        }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33332
diff changeset
   132
        return null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33332
diff changeset
   135
    private static GuardedInvocation findGetMethod(final String name) {
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   136
        final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   137
        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   140
    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
   141
        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
   142
        return inv.replaceMethods(getter, inv.getGuard());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33332
diff changeset
   145
    private static GuardedInvocation findSetMethod(final String name) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33332
diff changeset
   146
        final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, name);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   147
        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    private static GuardedInvocation findSetIndexMethod() {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   151
        return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   154
    private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) {
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   155
        MethodHandle mh = NashornCallSiteDescriptor.isScope(desc)? JSOBJECT_SCOPE_CALL : JSOBJECT_CALL;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   156
        if (NashornCallSiteDescriptor.isApplyToCall(desc)) {
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   157
            mh = MH.insertArguments(JSOBJECT_CALL_TO_APPLY, 0, mh);
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   158
        }
36024
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
   159
        final MethodType type = desc.getMethodType();
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
   160
        mh = type.parameterType(type.parameterCount() - 1) == Object[].class ?
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
   161
                mh :
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
   162
                MH.asCollector(mh, Object[].class, type.parameterCount() - 2);
1a7614804158 8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents: 34447
diff changeset
   163
        return new GuardedInvocation(mh, IS_JSOBJECT_GUARD);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   164
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   165
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   166
    private static GuardedInvocation findNewMethod(final CallSiteDescriptor desc) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   167
        final MethodHandle func = MH.asCollector(JSOBJECT_NEW, Object[].class, desc.getMethodType().parameterCount() - 1);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   168
        return new GuardedInvocation(func, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
    private static boolean isJSObject(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
        return self instanceof JSObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    @SuppressWarnings("unused")
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   177
    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
   178
        throws Throwable {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   179
        if (key instanceof Integer) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19889
diff changeset
   180
            return ((JSObject)jsobj).getSlot((Integer)key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
        } else if (key instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
            final int index = getIndex((Number)key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
            if (index > -1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
                return ((JSObject)jsobj).getSlot(index);
44168
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   185
            } else {
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   186
                return ((JSObject)jsobj).getMember(JSType.toString(key));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
            }
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29133
diff changeset
   188
        } else if (isString(key)) {
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   189
            final String name = key.toString();
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   190
            // 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
   191
            if (name.indexOf('(') != -1) {
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   192
                return fallback.invokeExact(jsobj, (Object) name);
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   193
            }
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26237
diff changeset
   194
            return ((JSObject)jsobj).getMember(name);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    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
   201
        if (key instanceof Integer) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19889
diff changeset
   202
            ((JSObject)jsobj).setSlot((Integer)key, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
        } else if (key instanceof Number) {
44168
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   204
            final int index = getIndex((Number)key);
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   205
            if (index > -1) {
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   206
                ((JSObject)jsobj).setSlot(index, value);
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   207
            } else {
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   208
                ((JSObject)jsobj).setMember(JSType.toString(key), value);
604022687602 8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents: 42382
diff changeset
   209
            }
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 29133
diff changeset
   210
        } else if (isString(key)) {
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   211
            ((JSObject)jsobj).setMember(key.toString(), value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   215
    @SuppressWarnings("unused")
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   216
    private static boolean del(final Object jsobj, final Object key) {
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   217
        if (jsobj instanceof ScriptObjectMirror) {
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   218
            return ((ScriptObjectMirror)jsobj).delete(key);
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   219
        }
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   220
        ((JSObject) jsobj).removeMember(Objects.toString(key));
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   221
        return true;
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   222
    }
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   223
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
    private static int getIndex(final Number n) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
        final double value = n.doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
        return JSType.isRepresentableAsInt(value) ? (int)value : -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   229
    @SuppressWarnings("unused")
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   230
    private static Object callToApply(final MethodHandle mh, final JSObject obj, final Object thiz, final Object... args) {
47037
07210623c057 8184723: jdk.nashorn.internal.runtime.linker.JSObjectLinker.callToApply erroneously asserts given arguments
hannesw
parents: 44168
diff changeset
   231
        assert args.length >= 1;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   232
        final Object   receiver  = args[0];
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   233
        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
   234
        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
   235
        try {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   236
            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
   237
        } catch (final RuntimeException | Error e) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   238
            throw e;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   239
        } catch (final Throwable e) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   240
            throw new RuntimeException(e);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   241
        }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   242
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   243
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   244
    // This is used when a JSObject is called as scope call to do undefined -> Global this translation.
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   245
    @SuppressWarnings("unused")
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   246
    private static Object jsObjectScopeCall(final JSObject jsObj, final Object thiz, final Object[] args) {
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   247
        final Object modifiedThiz;
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   248
        if (thiz == ScriptRuntime.UNDEFINED && !jsObj.isStrictFunction()) {
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   249
            final Global global = Context.getGlobal();
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   250
            modifiedThiz = ScriptObjectMirror.wrap(global, global);
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   251
        } else {
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   252
            modifiedThiz = thiz;
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   253
        }
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   254
        return jsObj.call(modifiedThiz, args);
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   255
    }
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   256
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   259
    // method handles of the current class
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   260
    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
   261
    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
   262
    private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class);
48411
4ff5c5206427 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 47216
diff changeset
   263
    private static final MethodHandle JSOBJECTLINKER_DEL = findOwnMH_S("del", boolean.class, Object.class, Object.class);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   264
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   265
    // method handles of JSObject class
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   266
    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
   267
    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
   268
    private static final MethodHandle JSOBJECT_CALL          = findJSObjectMH_V("call", Object.class, Object.class, Object[].class);
42382
e56aaa43d9f4 8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents: 41842
diff changeset
   269
    private static final MethodHandle JSOBJECT_SCOPE_CALL    = findOwnMH_S("jsObjectScopeCall", Object.class, JSObject.class, Object.class, Object[].class);
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   270
    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
   271
    private static final MethodHandle JSOBJECT_NEW           = findJSObjectMH_V("newObject", Object.class, Object[].class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   273
    private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   274
        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
   275
    }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   276
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   277
    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   278
        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
   279
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
}