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