use of org.graalvm.polyglot.Value in project graal by oracle.
the class PolyglotNativeAPI method polyglot_execute.
@CEntryPoint(name = "polyglot_execute")
public static PolyglotStatus polyglot_execute(IsolateThread isolate_thread, PolyglotValuePointer value_handle, PolyglotValuePointerPointer args, int args_size, PolyglotValuePointerPointer return_value) {
return withHandledErrors(() -> {
Value function = fetchHandle(value_handle);
Object[] jArgs = new Object[args_size];
for (int i = 0; i < args_size; i++) {
PolyglotValuePointer handle = args.read(i);
jArgs[i] = fetchHandle(handle);
}
Value result = function.execute(jArgs);
return_value.write(createHandle(result));
});
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class PolyglotNativeAPI method polyglot_value_as_int8.
@CEntryPoint(name = "polyglot_value_as_int8")
public static PolyglotStatus polyglot_value_as_int8(IsolateThread isolate_thread, PolyglotValuePointer value, CCharPointer result) {
return withHandledErrors(() -> {
Value valueObject = fetchHandle(value);
result.write(valueObject.asByte());
});
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class PolyglotNativeAPI method polyglot_create_function.
@CEntryPoint(name = "polyglot_create_function")
public static PolyglotStatus polyglot_create_function(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, PolyglotCallbackPointer callback, VoidPointer data, PolyglotValuePointerPointer value) {
return withHandledErrors(() -> {
Context c = ObjectHandles.getGlobal().get(polyglot_context);
ProxyExecutable executable = (Value... arguments) -> {
ObjectHandle[] handleArgs = new ObjectHandle[arguments.length];
for (int i = 0; i < arguments.length; i++) {
handleArgs[i] = createHandle(arguments[i]);
}
PolyglotCallbackInfo cbInfo = (PolyglotCallbackInfo) createHandle(new PolyglotCallbackInfoInternal(handleArgs, data));
try {
PolyglotValuePointer result = callback.invoke(CEntryPointContext.getCurrentIsolateThread(), cbInfo);
CallbackException ce = exceptionsTL.get();
if (ce != null) {
exceptionsTL.remove();
throw ce;
} else {
return PolyglotNativeAPI.fetchHandle(result);
}
} finally {
PolyglotCallbackInfoInternal info = fetchHandle(cbInfo);
for (ObjectHandle arg : info.arguments) {
freeHandle(arg);
}
freeHandle(cbInfo);
}
};
value.write(createHandle(c.asValue(executable)));
});
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ToJavaNode method convertImpl.
private Object convertImpl(Object value, Class<?> targetType, Type genericType, Object languageContext) {
Object convertedValue;
if (isAssignableFromTrufflePrimitiveType(targetType)) {
convertedValue = primitive.toPrimitive(value, targetType);
if (convertedValue != null) {
return convertedValue;
} else if (targetType == char.class || targetType == Character.class) {
Integer safeChar = primitive.toInteger(value);
if (safeChar != null) {
int v = safeChar;
if (v >= 0 && v < 65536) {
return (char) v;
}
}
}
}
if (targetType == Value.class && languageContext != null) {
convertedValue = value instanceof Value ? value : JavaInterop.toHostValue(value, languageContext);
} else if (value instanceof TruffleObject) {
convertedValue = asJavaObject((TruffleObject) value, targetType, genericType, languageContext);
} else if (targetType.isAssignableFrom(value.getClass())) {
convertedValue = value;
} else {
CompilerDirectives.transferToInterpreter();
String reason;
if (isAssignableFromTrufflePrimitiveType(targetType)) {
reason = "Invalid or lossy primitive coercion.";
} else {
reason = "Unsupported target type.";
}
throw JavaInteropErrors.cannotConvert(languageContext, value, targetType, reason);
}
return convertedValue;
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLInstrumentTest method testChangeArgumentsOnReenter.
/**
* Test that we can alter function arguments on reenter.
*/
@Test
public void testChangeArgumentsOnReenter() throws Exception {
String code = "function main() {\n" + " y = fce(0, 10000);\n" + " return y;\n" + "}\n" + "function fce(x, z) {\n" + " y = 2 * x;\n" + " if (y < z) {\n" + " print(\"A bad error.\");\n" + " return 0 - 1;\n" + " } else {\n" + " return y;\n" + " }\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
Context context = Context.create();
IncreaseArgOnErrorInstrument incOnError = context.getEngine().getInstruments().get("testIncreaseArgumentOnError").lookup(IncreaseArgOnErrorInstrument.class);
incOnError.attachOn("A bad error");
Value ret = context.eval(source);
assertEquals(10000, ret.asInt());
}
Aggregations