Search in sources :

Example 51 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Bootstrap method relCmpSetup.

@SuppressWarnings("unused")
private static MethodHandle relCmpSetup(MutableCallSite callsite, RelationalOperator op, Object arg1, Object arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = filterReturnValue(relCmpStringMH, op);
    } else if (type == Type.Number) {
        target = filterReturnValue(relCmpNumberMH, op);
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestFor(type), filterReturnValue(MethodHandles.insertArguments(relCmpGenericMH, 2, op), op));
}
Also used : Type(com.github.anba.es6draft.runtime.types.Type) MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 52 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class NativeCalls method getNativeMethodHandle.

private static MethodHandle getNativeMethodHandle(String name) {
    if (!name.startsWith("native:")) {
        throw new IllegalArgumentException();
    }
    String methodName = name.substring("native:".length());
    for (Class<?> lookupClass : lookupClasses) {
        MethodLookup lookup = new MethodLookup(MethodHandles.publicLookup().in(lookupClass));
        Method m = findMethod(lookup, methodName);
        if (m == null) {
            continue;
        }
        MethodHandle mh;
        try {
            mh = lookup.getLookup().unreflect(m);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException();
        }
        // Allow to omit execution context argument.
        MethodType type = mh.type();
        if (type.parameterCount() == 0 || !type.parameterType(0).equals(ExecutionContext.class)) {
            mh = MethodHandles.dropArguments(mh, 0, ExecutionContext.class);
        }
        // Allow void return type.
        if (type.returnType() == void.class) {
            mh = MethodHandles.filterReturnValue(mh, MethodHandles.constant(Object.class, UNDEFINED));
        }
        return mh;
    }
    throw new IllegalArgumentException();
}
Also used : MethodType(java.lang.invoke.MethodType) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 53 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class NativeCalls method toObjectArray.

private static MethodHandle toObjectArray(List<Class<?>> types) {
    MethodHandle mh = MethodHandles.identity(Object[].class);
    mh = mh.asCollector(Object[].class, types.size());
    return mh.asType(MethodType.methodType(Object[].class, types));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 54 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Properties method createExternalFunction.

private static <OWNER> NativeFunction createExternalFunction(ExecutionContext cx, OWNER owner, Class<OWNER> holder) {
    ObjectLayout layout = externalLayouts.get(holder);
    if (layout.functions == null || layout.functions.size() != 1) {
        throw new IllegalArgumentException();
    }
    Converter converter = new Converter(cx);
    Entry<Function, MethodHandle> entry = layout.functions.entrySet().iterator().next();
    Function function = entry.getKey();
    MethodHandle handle = getInstanceMethodHandle(cx, converter, entry.getValue(), owner);
    return new NativeFunction(cx.getRealm(), function.name(), function.arity(), handle);
}
Also used : NativeFunction(com.github.anba.es6draft.runtime.types.builtins.NativeFunction) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction) NativeTailCallFunction(com.github.anba.es6draft.runtime.types.builtins.NativeTailCallFunction) NativeFunction(com.github.anba.es6draft.runtime.types.builtins.NativeFunction) MethodHandle(java.lang.invoke.MethodHandle)

Example 55 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Properties method createExternalAccessors.

private static <OWNER> void createExternalAccessors(ExecutionContext cx, ScriptObject target, OWNER owner, ObjectLayout layout, Converter converter) {
    LinkedHashMap<String, PropertyDescriptor> stringProps = new LinkedHashMap<>();
    EnumMap<BuiltinSymbol, PropertyDescriptor> symbolProps = new EnumMap<>(BuiltinSymbol.class);
    for (Entry<Accessor, MethodHandle> entry : layout.accessors.entrySet()) {
        MethodHandle handle = getInstanceMethodHandle(cx, converter, entry.getValue(), owner);
        createExternalAccessor(cx, entry.getKey(), handle, stringProps, symbolProps);
    }
    defineProperties(cx, target, stringProps, symbolProps);
}
Also used : BuiltinSymbol(com.github.anba.es6draft.runtime.types.BuiltinSymbol) PropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor) AccessorPropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor.AccessorPropertyDescriptor) EnumMap(java.util.EnumMap) LinkedHashMap(java.util.LinkedHashMap) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)302 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)44 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)13 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6