Java Code Examples for jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor#getOperand()

The following examples show how to use jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor#getOperand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final String name = NashornCallSiteDescriptor.getOperand(desc);
    final boolean isScope = NashornCallSiteDescriptor.isScope(desc);

    if (lexicalScope != null && isScope && !NashornCallSiteDescriptor.isApplyToCall(desc)) {
        if (lexicalScope.hasOwnProperty(name)) {
            return lexicalScope.findGetMethod(desc, request);
        }
    }

    final GuardedInvocation invocation =  super.findGetMethod(desc, request);

    // We want to avoid adding our generic lexical scope switchpoint to global constant invocations,
    // because those are invalidated per-key in the addBoundProperties method above.
    // We therefore check if the invocation does already have a switchpoint and the property is non-inherited,
    // assuming this only applies to global constants. If other non-inherited properties will
    // start using switchpoints some time in the future we'll have to revisit this.
    if (isScope && context.getEnv()._es6 && (invocation.getSwitchPoints() == null || !hasOwnProperty(name))) {
        return invocation.addSwitchPoint(getLexicalScopeSwitchPoint());
    }

    return invocation;
}
 
Example 2
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final boolean isScope = NashornCallSiteDescriptor.isScope(desc);

    if (lexicalScope != null && isScope) {
        final String name = NashornCallSiteDescriptor.getOperand(desc);
        if (lexicalScope.hasOwnProperty(name)) {
            return lexicalScope.findSetMethod(desc, request);
        }
    }

    final GuardedInvocation invocation = super.findSetMethod(desc, request);

    if (isScope && context.getEnv()._es6) {
        return invocation.addSwitchPoint(getLexicalScopeSwitchPoint());
    }

    return invocation;
}
 
Example 3
Source File: Undefined.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Lookup the appropriate method for an invoke dynamic call.
 * @param desc The invoke dynamic callsite descriptor.
 * @return GuardedInvocation to be invoked at call site.
 */
public static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    switch (NashornCallSiteDescriptor.getStandardOperation(desc)) {
    case CALL:
    case NEW:
        final String name = NashornCallSiteDescriptor.getOperand(desc);
        final String msg = name != null? "not.a.function" : "cant.call.undefined";
        throw typeError(msg, name);
    case GET:
        // NOTE: we support GET:ELEMENT and SET:ELEMENT as JavaScript doesn't distinguish items from properties. Nashorn itself
        // emits "GET:PROPERTY|ELEMENT|METHOD:identifier" for "<expr>.<identifier>" and "GET:ELEMENT|PROPERTY|METHOD" for "<expr>[<expr>]", but we are
        // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
        // operation has an associated name or not.
        if (!(desc.getOperation() instanceof NamedOperation)) {
            return findGetIndexMethod(desc);
        }
        return findGetMethod(desc);
    case SET:
        if (!(desc.getOperation() instanceof NamedOperation)) {
            return findSetIndexMethod(desc);
        }
        return findSetMethod(desc);
    default:
    }
    return null;
}
 
Example 4
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final String name = NashornCallSiteDescriptor.getOperand(desc);

    // if str.length(), then let the bean linker handle it
    if ("length".equals(name) && NashornCallSiteDescriptor.isMethodFirstOperation(desc)) {
        return null;
    }

    return super.findGetMethod(desc, request);
}
 
Example 5
Source File: NativeJSAdapter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final String name = NashornCallSiteDescriptor.getOperand(desc);
    if (overrides && super.hasOwnProperty(name)) {
        try {
            final GuardedInvocation inv = super.findGetMethod(desc, request);
            if (inv != null) {
                return inv;
            }
        } catch (final Exception e) {
            //ignored
        }
    }

    if (!NashornCallSiteDescriptor.isMethodFirstOperation(desc)) {
        return findHook(desc, __get__);
    } else {
        final FindProperty find = adaptee.findProperty(__call__, true);
        if (find != null) {
            final Object value = find.getObjectValue();
            if (value instanceof ScriptFunction) {
                final ScriptFunction func = (ScriptFunction)value;
                // TODO: It's a shame we need to produce a function bound to this and name, when we'd only need it bound
                // to name. Probably not a big deal, but if we can ever make it leaner, it'd be nice.
                return new GuardedInvocation(MH.dropArguments(MH.constant(Object.class,
                        func.createBound(this, new Object[] { name })), 0, Object.class),
                        testJSAdapter(adaptee, null, null, null),
                        adaptee.getProtoSwitchPoints(__call__, find.getOwner()), null);
            }
        }
        throw typeError("no.such.function", name, ScriptRuntime.safeToString(this));
    }
}
 
Example 6
Source File: NativeJSAdapter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    if (overrides && super.hasOwnProperty(NashornCallSiteDescriptor.getOperand(desc))) {
        try {
            final GuardedInvocation inv = super.findSetMethod(desc, request);
            if (inv != null) {
                return inv;
            }
        } catch (final Exception e) {
            //ignored
        }
    }

    return findHook(desc, __put__);
}
 
Example 7
Source File: NativeJavaImporter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean createAndSetProperty(final CallSiteDescriptor desc) {
    final String name = NashornCallSiteDescriptor.getOperand(desc);
    final Object value = createProperty(name);
    if(value != null) {
        set(name, value, 0);
        return true;
    }
    return false;
}
 
Example 8
Source File: ScriptObject.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fall back if a function property is not found.
 * @param desc The call site descriptor
 * @param request the link request
 * @return GuardedInvocation to be invoked at call site.
 */
public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final String       name      = NashornCallSiteDescriptor.getOperand(desc);
    final FindProperty find      = findProperty(NO_SUCH_METHOD_NAME, true);
    final boolean      scopeCall = isScope() && NashornCallSiteDescriptor.isScope(desc);

    if (find == null) {
        return noSuchProperty(desc, request)
                // Add proto switchpoint to switch from no-such-property to no-such-method if it is ever defined.
                .addSwitchPoint(getProtoSwitchPoint(NO_SUCH_METHOD_NAME));
    }

    final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);

    final Object value = find.getObjectValue();
    if (!(value instanceof ScriptFunction)) {
        return createEmptyGetter(desc, explicitInstanceOfCheck, name);
    }

    final ScriptFunction func = (ScriptFunction)value;
    final Object         thiz = scopeCall && func.isStrict() ? UNDEFINED : this;
    // TODO: It'd be awesome if we could bind "name" without binding "this".
    // Since we're binding this we must use an identity guard here.
    return new GuardedInvocation(
            MH.dropArguments(
                    MH.constant(
                            ScriptFunction.class,
                            func.createBound(thiz, new Object[] { name })),
                    0,
                    Object.class),
            NashornGuards.combineGuards(
                    NashornGuards.getIdentityGuard(this),
                    NashornGuards.getMapGuard(getMap(), true)))
            // Add a protoype switchpoint for the original name so this gets invalidated if it is ever defined.
            .addSwitchPoint(getProtoSwitchPoint(name));
}
 
Example 9
Source File: NativeJavaPackage.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Handle creation of new attribute.
 * @param desc the call site descriptor
 * @param request the link request
 * @return Link to be invoked at call site.
 */
@Override
public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
    final String propertyName = NashornCallSiteDescriptor.getOperand(desc);
    createProperty(propertyName);
    return super.lookup(desc, request);
}
 
Example 10
Source File: GlobalConstants.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Try to turn a getter into a MethodHandle.constant, if possible
 *
 * @param find      property lookup
 * @param receiver  receiver
 * @param desc      callsite descriptor
 *
 * @return resulting getter, or null if failed to create constant
 */
GuardedInvocation findGetMethod(final FindProperty find, final ScriptObject receiver, final CallSiteDescriptor desc) {
    // Only use constant getter for fast scope access, because the receiver may change between invocations
    // for slow-scope and non-scope callsites.
    // Also return null for user accessor properties as they may have side effects.
    if (invalidatedForever.get() || !NashornCallSiteDescriptor.isFastScope(desc)
            || (GLOBAL_ONLY && !find.getOwner().isGlobal())
            || find.getProperty() instanceof UserAccessorProperty) {
        return null;
    }

    final boolean  isOptimistic = NashornCallSiteDescriptor.isOptimistic(desc);
    final int      programPoint = isOptimistic ? getProgramPoint(desc) : INVALID_PROGRAM_POINT;
    final Class<?> retType      = desc.getMethodType().returnType();
    final String   name         = NashornCallSiteDescriptor.getOperand(desc);

    synchronized (this) {
        final Access acc = getOrCreateSwitchPoint(name);

        log.fine("Starting to look up object value " + name);
        final Object c = find.getObjectValue();

        if (log.isEnabled()) {
            log.fine("Trying to link constant GETTER " + acc + " value = " + c);
        }

        if (acc.hasBeenInvalidated() || acc.guardFailed() || invalidatedForever.get()) {
            if (log.isEnabled()) {
                log.info("*** GET: Giving up on " + quote(name) + " - retry count has exceeded " + DynamicLinker.getLinkedCallSiteLocation());
            }
            return null;
        }

        final MethodHandle cmh = constantGetter(c);

        MethodHandle mh;
        MethodHandle guard;

        if (isOptimistic) {
            if (JSType.getAccessorTypeIndex(cmh.type().returnType()) <= JSType.getAccessorTypeIndex(retType)) {
                //widen return type - this is pessimistic, so it will always work
                mh = MH.asType(cmh, cmh.type().changeReturnType(retType));
            } else {
                //immediately invalidate - we asked for a too wide constant as a narrower one
                mh = MH.dropArguments(MH.insertArguments(JSType.THROW_UNWARRANTED.methodHandle(), 0, c, programPoint), 0, Object.class);
            }
        } else {
            //pessimistic return type filter
            mh = Lookup.filterReturnType(cmh, retType);
        }

        if (find.getOwner().isGlobal()) {
            guard = null;
        } else {
            guard = MH.insertArguments(RECEIVER_GUARD, 0, acc, receiver);
        }

        if (log.isEnabled()) {
            log.info("Linked getter " + quote(name) + " as MethodHandle.constant() -> " + c + " " + acc.getSwitchPoint());
            mh = MethodHandleFactory.addDebugPrintout(log, Level.FINE, mh, "get const " + acc);
        }

        return new GuardedInvocation(mh, guard, acc.getSwitchPoint(), null);
    }
}
 
Example 11
Source File: Undefined.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static ECMAException lookupTypeError(final String msg, final CallSiteDescriptor desc) {
    final String name = NashornCallSiteDescriptor.getOperand(desc);
    return typeError(msg, name != null && !name.isEmpty()? name : null);
}
 
Example 12
Source File: ScriptObject.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Find the appropriate SET method for an invoke dynamic call.
 *
 * @param desc    the call site descriptor
 * @param request the link request
 *
 * @return GuardedInvocation to be invoked at call site.
 */
protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final String name = NashornCallSiteDescriptor.getOperand(desc);

    if (request.isCallSiteUnstable() || hasWithScope()) {
        return findMegaMorphicSetMethod(desc, name);
    }

    final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);

    /*
     * If doing property set on a scope object, we should stop proto search on the first
     * non-scope object. Without this, for example, when assigning "toString" on global scope,
     * we'll end up assigning it on it's proto - which is Object.prototype.toString !!
     *
     * toString = function() { print("global toString"); } // don't affect Object.prototype.toString
     */
    FindProperty find = findProperty(name, true, NashornCallSiteDescriptor.isScope(desc), this);

    // If it's not a scope search, then we don't want any inherited properties except those with user defined accessors.
    if (find != null && find.isInheritedOrdinaryProperty()) {
        // We should still check if inherited data property is not writable
        if (isExtensible() && !find.getProperty().isWritable()) {
            return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.not.writable", true);
        }
        // Otherwise, forget the found property unless this is a scope callsite and the owner is a scope object as well.
        if (!NashornCallSiteDescriptor.isScope(desc) || !find.getOwner().isScope()) {
            find = null;
        }
    }

    if (find != null) {
        if (!find.getProperty().isWritable() && !NashornCallSiteDescriptor.isDeclaration(desc)) {
            if (NashornCallSiteDescriptor.isScope(desc) && find.getProperty().isLexicalBinding()) {
                throw typeError("assign.constant", name); // Overwriting ES6 const should throw also in non-strict mode.
            }
            // Existing, non-writable data property
            return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.not.writable", true);
        }
        if (!find.getProperty().hasNativeSetter()) {
            // Existing accessor property without setter
            return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.has.no.setter", true);
        }
    } else {
        if (!isExtensible()) {
            return createEmptySetMethod(desc, explicitInstanceOfCheck, "object.non.extensible", false);
        }
    }

    final GuardedInvocation inv = new SetMethodCreator(this, find, desc, request).createGuardedInvocation(findBuiltinSwitchPoint(name));

    final GlobalConstants globalConstants = getGlobalConstants();
    if (globalConstants != null) {
        final GuardedInvocation cinv = globalConstants.findSetMethod(find, this, inv, desc, request);
        if (cinv != null) {
            return cinv;
        }
    }

    return inv;
}
 
Example 13
Source File: SetMethodCreator.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private String getName() {
    return NashornCallSiteDescriptor.getOperand(desc);
}