@Override public Object execute(VirtualFrame frame) { return JavaInterop.asTruffleObject(allTarget); } });
static TruffleObject asTruffleObject(Object obj, Object languageContext) { return asTruffleObject(obj, languageContext, false); }
@Override public Object toJavaGuestObject(Object obj, Object languageContext) { return JavaInterop.asTruffleObject(obj, languageContext); }
static Object asTruffleValue(Object obj, Object languageContext) { return JavaInterop.isPrimitive(obj) ? obj : JavaInterop.asTruffleObject(obj, languageContext); } }
/** * Prepares a Java object for use in any {@link TruffleLanguage}. If the object is one of * {@link #isPrimitive primitive} values, it is just returned, as all {@link TruffleLanguage}s * are supposed to handle such object. If it is a non-primitive type of Java object, the method * does exactly the same thing as {@link #asTruffleObject}. * * @param obj a Java object to convert into one suitable for <em>Truffle</em> languages * @return converted object, or primitive * @since 0.18 */ public static Object asTruffleValue(Object obj) { return isPrimitive(obj) ? obj : asTruffleObject(obj); }
public Object access(StructuredDataEntry data) { return JavaInterop.asTruffleObject(data.getSchema().getNames()); } }
static void unwrapArgs(PolyglotEngine engine, final Object[] args) { for (int i = 0; i < args.length; i++) { if (args[i] instanceof EngineTruffleObject) { final EngineTruffleObject engineObject = (EngineTruffleObject) args[i]; engineObject.assertEngine(engine); args[i] = engineObject.getDelegate(); } if (args[i] != null && !isPrimitiveType(args[i].getClass())) { args[i] = JavaInterop.asTruffleObject(args[i]); } } }
return asTruffleObject(obj, currentPolyglotContext(), true);
static Object toGuestObject(Object obj, Object languageContext) { assert !isPrimitive(obj); EngineSupport engine = ACCESSOR.engine(); if (engine == null || languageContext == null) { assert !(obj instanceof Value || obj instanceof Proxy); return asTruffleObject(obj, languageContext); } return engine.toGuestValue(obj, languageContext); }
/** @since 0.16 */ @Test public void testReadElementFromForeign() throws Exception { String id = readElementFromForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); Assert.assertEquals(42.0, ((Number) apply.execute(JavaInterop.asTruffleObject(new double[]{-1, -2, 42.0, -4})).get()).doubleValue(), 0.1); }
/** @since 0.16 */ @Test public void testGetSizeOfForeign() throws Exception { String id = getSizeOfForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); double[] arr = {-1, -1, -1, -1}; Number size = (Number) apply.execute(JavaInterop.asTruffleObject(arr)).get(); Assert.assertEquals(4, size.intValue(), 0.1); }
/** @since 0.16 */ @Test public void testReadValueFromForeign() throws Exception { String id = readValueFromForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); Assert.assertEquals(42.0, ((Number) apply.execute(JavaInterop.asTruffleObject(new TestObject(42.0))).get()).doubleValue(), 0.1); }
/** * Test for array access. Creates a {@link TruffleObject} around a Java array, fills it with * integers and asks the language to add one to each of the array elements. * * @since 0.14 */ @Test public void addOneToAnArrayElement() throws Exception { String id = addToArray(); if (id == null) { return; } PolyglotEngine.Value add = findGlobalSymbol(id); Number[] arr = new Number[10]; for (int i = 0; i < arr.length; i++) { arr[i] = i; } TruffleObject truffleArr = JavaInterop.asTruffleObject(arr); int index = RANDOM.nextInt(arr.length - 1); add.execute(truffleArr, index, 1); final Number valueAtIndex = arr[index]; final Number valueAfterIndex = arr[index + 1]; assertNotNull("Non-null value expected at index " + index, valueAtIndex); assertNotNull("Non-null value expected at index " + (index + 1), valueAfterIndex); assertEquals("Expecting same value at both indexes", valueAtIndex.intValue(), valueAfterIndex.intValue()); }
/** @since 0.16 */ @Test public void testWriteElementOfForeign() throws Exception { String id = writeElementToForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); double[] arr = {-1, -1, -1, -1}; apply.execute(JavaInterop.asTruffleObject(arr)); Assert.assertEquals(42.0, arr[2], 0.1); }
/** @since 0.16 */ @Test public void testIsNullOfForeign() throws Exception { String id = isNullForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); boolean result = (boolean) apply.execute(JavaInterop.asTruffleObject(null)).get(); Assert.assertEquals(true, result); }
/** @since 0.16 */ @Test public void testWriteValueToForeign() throws Exception { String id = writeValueToForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TestObject obj = new TestObject(-1.5); apply.execute(JavaInterop.asTruffleObject(obj)); Assert.assertEquals(42.0, obj.value, 0.1); }
/** @since 0.16 */ @Test public void testHasSizeOfForeign() throws Exception { String id = hasSizeOfForeign(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); double[] arr = {-1, -1, -1, -1}; boolean result = (boolean) apply.execute(JavaInterop.asTruffleObject(arr)).get(); Assert.assertEquals(true, result); }
/** @since 0.16 */ @Test public void testCallMethod() throws Exception { String id = callMethod(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TestObject obj = new TestObject(0); apply.execute(JavaInterop.asTruffleObject(obj)); Assert.assertEquals(obj.arg1, 41.0, 0.1); Assert.assertEquals(obj.arg2, 42.0, 0.1); }