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));
}
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();
}
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));
}
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);
}
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);
}
Aggregations