nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/WithObject.java
author chegar
Mon, 18 Aug 2014 10:59:40 +0100
changeset 26068 5488f52c2788
parent 26067 nashorn/src/jdk/nashorn/internal/runtime/WithObject.java@b32ccc3a76c9
parent 25865 nashorn/src/jdk/nashorn/internal/runtime/WithObject.java@d38d876f1654
child 26765 97501edd2979
permissions -rw-r--r--
Merge
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;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
16277
fd698c5ee684 8009559: clean up method handle lookup code.
sundar
parents: 16264
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
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;
16780
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
    32
import java.lang.invoke.MethodType;
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    33
import java.lang.invoke.SwitchPoint;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    34
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    35
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    36
import jdk.internal.dynalink.linker.LinkRequest;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    37
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25247
diff changeset
    38
import jdk.nashorn.api.scripting.AbstractJSObject;
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25247
diff changeset
    39
import jdk.nashorn.api.scripting.ScriptObjectMirror;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20933
diff changeset
    41
import jdk.nashorn.internal.runtime.linker.NashornGuards;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
 * This class supports the handling of scope in a with body.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
 */
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16195
diff changeset
    47
public final class WithObject extends ScriptObject implements Scope {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    48
    private static final MethodHandle WITHEXPRESSIONGUARD    = findOwnMH("withExpressionGuard",  boolean.class, Object.class, PropertyMap.class, SwitchPoint.class);
16780
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
    49
    private static final MethodHandle WITHEXPRESSIONFILTER   = findOwnMH("withFilterExpression", Object.class, Object.class);
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
    50
    private static final MethodHandle WITHSCOPEFILTER        = findOwnMH("withFilterScope",      Object.class, Object.class);
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
    51
    private static final MethodHandle BIND_TO_EXPRESSION_OBJ = findOwnMH("bindToExpression",     Object.class, Object.class, Object.class);
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
    52
    private static final MethodHandle BIND_TO_EXPRESSION_FN  = findOwnMH("bindToExpression",     Object.class, ScriptFunction.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
    /** With expression object. */
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    55
    private final ScriptObject expression;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
     * @param scope scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
     * @param expression with expression
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
     */
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    63
    WithObject(final ScriptObject scope, final ScriptObject expression) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 17236
diff changeset
    64
        super(scope, null);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
        setIsScope();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
        this.expression = expression;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
     * Delete a property based on a key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
     * @param key Any valid JavaScript value.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
     * @param strict strict mode execution.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
     * @return True if deleted.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    public boolean delete(final Object key, final boolean strict) {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    77
        final ScriptObject self = expression;
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    78
        final String propName = JSType.toString(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    80
        final FindProperty find = self.findProperty(propName, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    82
        if (find != null) {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
    83
            return self.delete(propName, strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    @Override
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16173
diff changeset
    91
    public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    92
        if (request.isCallSiteUnstable()) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    93
            // Fall back to megamorphic invocation which performs a complete lookup each time without further relinking.
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    94
            return super.lookup(desc, request);
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    95
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    96
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
        // With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
        // necessity have a Nashorn descriptor - it is safe to cast.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
        final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
        FindProperty find = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
        GuardedInvocation link = null;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20933
diff changeset
   102
        ScriptObject self;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
        final boolean isNamedOperation;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
        final String name;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   106
        if (desc.getNameTokenCount() > 2) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
            isNamedOperation = true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
            name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
            isNamedOperation = false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
            name = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   114
        self = expression;
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   115
        if (isNamedOperation) {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   116
             find = self.findProperty(name, true);
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   117
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   119
        if (find != null) {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   120
            link = self.lookup(desc, request);
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   121
            if (link != null) {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   122
                return fixExpressionCallSite(ndesc, link);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
        final ScriptObject scope = getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
        if (isNamedOperation) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
            find = scope.findProperty(name, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
        if (find != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20933
diff changeset
   132
            return fixScopeCallSite(scope.lookup(desc, request), name, find.getOwner());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
        // the property is not found - now check for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
        // __noSuchProperty__ and __noSuchMethod__ in expression
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
        if (self != null) {
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   138
            final String fallBack;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
            final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
            switch (operator) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
            case "callMethod":
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   144
                throw new AssertionError(); // Nashorn never emits callMethod
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
            case "getMethod":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
                fallBack = NO_SUCH_METHOD_NAME;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
                break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
            case "getProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
            case "getElem":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
                fallBack = NO_SUCH_PROPERTY_NAME;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
                break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
            default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
                fallBack = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
                break;
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
            if (fallBack != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
                find = self.findProperty(fallBack, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
                if (find != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
                    switch (operator) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
                    case "getMethod":
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16173
diff changeset
   162
                        link = self.noSuchMethod(desc, request);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
                        break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
                    case "getProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
                    case "getElem":
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16173
diff changeset
   166
                        link = self.noSuchProperty(desc, request);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
                        break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
                    default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
                        break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
            if (link != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
                return fixExpressionCallSite(ndesc, link);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
        // still not found, may be scope can handle with it's own
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
        // __noSuchProperty__, __noSuchMethod__ etc.
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16173
diff changeset
   181
        link = scope.lookup(desc, request);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
        if (link != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20933
diff changeset
   184
            return fixScopeCallSite(link, name, null);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
16264
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   190
    /**
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   191
     * Overridden to try to find the property first in the expression object (and its prototypes), and only then in this
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   192
     * object (and its prototypes).
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   193
     *
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   194
     * @param key  Property key.
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   195
     * @param deep Whether the search should look up proto chain.
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   196
     * @param stopOnNonScope should a deep search stop on the first non-scope object?
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   197
     * @param start the object on which the lookup was originally initiated
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   198
     *
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   199
     * @return FindPropertyData or null if not found.
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   200
     */
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   201
    @Override
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   202
    FindProperty findProperty(final String key, final boolean deep, final boolean stopOnNonScope, final ScriptObject start) {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   203
        final FindProperty exprProperty = expression.findProperty(key, deep, stopOnNonScope, start);
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   204
        if (exprProperty != null) {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   205
             return exprProperty;
16264
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   206
        }
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   207
        return super.findProperty(key, deep, stopOnNonScope, start);
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   208
    }
e0c3c97cd93e 8006984: Introducing local into a function inside with statement confuses its scope
attila
parents: 16234
diff changeset
   209
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
    public void setSplitState(final int state) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
        getNonWithParent().setSplitState(state);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
    public int getSplitState() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
        return getNonWithParent().getSplitState();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
     * Get first parent scope that is not an instance of WithObject.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
    private Scope getNonWithParent() {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   224
        ScriptObject proto = getParentScope();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
        while (proto != null && proto instanceof WithObject) {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   227
            proto = ((WithObject)proto).getParentScope();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
        assert proto instanceof Scope : "with scope without parent scope";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
        return (Scope) proto;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   234
17236
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   235
    private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   236
        // The receiver may be an Object or a ScriptObject.
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   237
        final MethodType invType = link.getInvocation().type();
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   238
        final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   239
        return link.asType(newInvType);
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   240
    }
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   241
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
    private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
        // If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
        // expression.
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   245
        if (!"getMethod".equals(desc.getFirstOperator())) {
17236
75779a53c6a9 8012919: findMegaMorphicSetMethod should not cast result type
jlaskey
parents: 17234
diff changeset
   246
            return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   249
        final MethodHandle linkInvocation      = link.getInvocation();
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   250
        final MethodType   linkType            = linkInvocation.type();
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   251
        final boolean      linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20933
diff changeset
   252
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
        return link.replaceMethods(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
                // Make sure getMethod will bind the script functions it receives to WithObject.expression
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   255
                MH.foldArguments(
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   256
                        linkReturnsFunction ?
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   257
                                BIND_TO_EXPRESSION_FN :
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   258
                                BIND_TO_EXPRESSION_OBJ,
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   259
                        filterReceiver(
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   260
                                linkInvocation.asType(
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   261
                                        linkType.changeReturnType(
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   262
                                                linkReturnsFunction ?
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   263
                                                        ScriptFunction.class :
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   264
                                                        Object.class).
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   265
                                                            changeParameterType(
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   266
                                                                    0,
24727
attila
parents: 24720 23372
diff changeset
   267
                                                                    Object.class)),
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   268
                                        WITHEXPRESSIONFILTER)),
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   269
                         filterGuardReceiver(link, WITHEXPRESSIONFILTER));
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   270
     // No clever things for the guard -- it is still identically filtered.
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   271
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20933
diff changeset
   274
    private GuardedInvocation fixScopeCallSite(final GuardedInvocation link, final String name, final ScriptObject owner) {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   275
        final GuardedInvocation newLink             = fixReceiverType(link, WITHSCOPEFILTER);
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   276
        final MethodHandle      expressionGuard     = expressionGuard(name, owner);
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   277
        final MethodHandle      filterGuardReceiver = filterGuardReceiver(newLink, WITHSCOPEFILTER);
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   278
        return link.replaceMethods(
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   279
                filterReceiver(
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   280
                        newLink.getInvocation(),
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   281
                        WITHSCOPEFILTER),
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   282
                NashornGuards.combineGuards(
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   283
                        expressionGuard,
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24727
diff changeset
   284
                        filterGuardReceiver));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20933
diff changeset
   287
    private static MethodHandle filterGuardReceiver(final GuardedInvocation link, final MethodHandle receiverFilter) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
        final MethodHandle test = link.getGuard();
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   289
        if (test == null) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   290
            return null;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   291
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   292
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   293
        final Class<?> receiverType = test.type().parameterType(0);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   294
        final MethodHandle filter = MH.asType(receiverFilter,
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   295
                receiverFilter.type().changeParameterType(0, receiverType).
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   296
                changeReturnType(receiverType));
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   297
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   298
        return filterReceiver(test, filter);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20933
diff changeset
   301
    private static MethodHandle filterReceiver(final MethodHandle mh, final MethodHandle receiverFilter) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   302
        //With expression filter == receiverFilter, i.e. receiver is cast to withobject and its expression returned
24727
attila
parents: 24720 23372
diff changeset
   303
        return MH.filterArguments(mh, 0, receiverFilter.asType(receiverFilter.type().changeReturnType(mh.type().parameterType(0))));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
     * Drops the WithObject wrapper from the expression.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
     * @param receiver WithObject wrapper.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
     * @return The with expression.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
    public static Object withFilterExpression(final Object receiver) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
        return ((WithObject)receiver).expression;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
    private static Object bindToExpression(final Object fn, final Object receiver) {
25247
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   317
        if (fn instanceof ScriptFunction) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   318
            return bindToExpression((ScriptFunction) fn, receiver);
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   319
        } else if (fn instanceof ScriptObjectMirror) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   320
            final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   321
            if (mirror.isFunction()) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   322
                // We need to make sure correct 'this' is used for calls with Ident call
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   323
                // expressions. We do so here using an AbstractJSObject instance.
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   324
                return new AbstractJSObject() {
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25247
diff changeset
   325
                    @Override
25247
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   326
                    public Object call(final Object thiz, final Object... args) {
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   327
                        return mirror.call(withFilterExpression(receiver), args);
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   328
                    }
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   329
                };
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   330
            }
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   331
        }
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   332
c0f911459863 8046013: TypeError: Cannot apply "with" to non script object
sundar
parents: 24733
diff changeset
   333
        return fn;
16780
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
   334
    }
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
   335
f23743ec1a93 8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
attila
parents: 16277
diff changeset
   336
    private static Object bindToExpression(final ScriptFunction fn, final Object receiver) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20933
diff changeset
   337
        return fn.makeBoundFunction(withFilterExpression(receiver), ScriptRuntime.EMPTY_ARRAY);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20933
diff changeset
   340
    private MethodHandle expressionGuard(final String name, final ScriptObject owner) {
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   341
        final PropertyMap map = expression.getMap();
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20933
diff changeset
   342
        final SwitchPoint sp = expression.getProtoSwitchPoint(name, owner);
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   343
        return MH.insertArguments(WITHEXPRESSIONGUARD, 1, map, sp);
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   344
    }
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   345
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   346
    @SuppressWarnings("unused")
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   347
    private static boolean withExpressionGuard(final Object receiver, final PropertyMap map, final SwitchPoint sp) {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   348
        return ((WithObject)receiver).expression.getMap() == map && (sp == null || !sp.hasBeenInvalidated());
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   349
    }
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   350
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
     * Drops the WithObject wrapper from the scope.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
     * @param receiver WithObject wrapper.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
     * @return The with scope.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
    public static Object withFilterScope(final Object receiver) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
        return ((WithObject)receiver).getProto();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
     * Get the with expression for this {@code WithObject}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
     * @return the with expression
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     */
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   364
    public ScriptObject getExpression() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
        return expression;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
20933
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 19884
diff changeset
   368
    /**
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 19884
diff changeset
   369
     * Get the parent scope for this {@code WithObject}
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 19884
diff changeset
   370
     * @return the parent scope
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 19884
diff changeset
   371
     */
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   372
    public ScriptObject getParentScope() {
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   373
        return getProto();
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   374
    }
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19621
diff changeset
   375
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
        return MH.findStatic(MethodHandles.lookup(), WithObject.class, name, MH.type(rtype, types));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
}