/** @since 0.13 */ @Test public void testPropertiesInteropMessage() throws Exception { PolyglotEngine.Value values = findGlobalSymbol(valuesObject()); Value valueObj = values.execute(); assertIsObjectOfLanguage(valueObj.get()); Map<?, ?> res = valueObj.as(Map.class); Map<String, Object> expected = new HashMap<>(); expected.put("intValue", 0); expected.put("byteValue", 0); expected.put("doubleValue", 0.0); for (Map.Entry<? extends Object, ? extends Object> entry : res.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); Object expValue = expected.remove(key); if (expValue != null) { assertEquals("For key " + key, ((Number) expValue).doubleValue(), ((Number) value).doubleValue(), 0.01); } } assertTrue("All expected properties found: " + expected, expected.isEmpty()); }
/** @since 0.16 */ @Test public void testWriteToObjectWithElement() throws Exception { String id = objectWithElement(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); @SuppressWarnings("unchecked") List<Object> object = JavaInterop.asJavaObject(List.class, truffleObject); Assert.assertEquals(42.0, ((Number) object.get(2)).doubleValue(), 0.1); object.set(2, 13.0); Assert.assertEquals(13.0, ((Number) object.get(2)).doubleValue(), 0.1); }
/** @since 0.16 */ @Test public void testReadFromObjectWithElement() throws Exception { String id = objectWithElement(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); List<?> object = JavaInterop.asJavaObject(List.class, truffleObject); Assert.assertEquals(42.0, ((Number) object.get(2)).doubleValue(), 0.1); }
private CompoundObject findCompoundSymbol() throws Exception { final String compoundObjectName = compoundObject(); PolyglotEngine.Value s = vm().findGlobalSymbol(compoundObjectName); assert s != null : "Symbol " + compoundObjectName + " is not found!"; final PolyglotEngine.Value value = s.execute(); assertIsObjectOfLanguage(value.get()); CompoundObject obj = value.as(CompoundObject.class); assertNotNull("Compound object for " + value + " found", obj); int traverse = RANDOM.nextInt(10); for (int i = 1; i <= traverse; i++) { obj = obj.returnsThis(); assertNotNull("Remains non-null even after " + i + " iteration", obj); } return obj; }
/** @since 0.16 */ @Test public void testIsExecutable() throws Exception { String id = functionAddNumbers(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); MessageInterface object = JavaInterop.asJavaObject(MessageInterface.class, truffleObject); Assert.assertEquals(true, object.isExecutable()); }
/** @since 0.16 */ @Test public void testFunctionAddNumbers() throws Exception { String id = functionAddNumbers(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); DoubleBinaryOperator object = JavaInterop.asJavaFunction(DoubleBinaryOperator.class, truffleObject); Assert.assertEquals(42.0, object.applyAsDouble(20.0, 22.0), 0.1); }
/** @since 0.16 */ @Test public void testReadFromObjectWithValueProperty() throws Exception { String id = objectWithValueProperty(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); ObjectWithValueInterface object = JavaInterop.asJavaObject(ObjectWithValueInterface.class, truffleObject); Assert.assertEquals(42.0, object.value(), 0.1); }
/** @since 0.8 or earlier */ @Test public void testGlobalObjectIsAccessible() throws Exception { String globalObjectFunction = globalObject(); if (globalObjectFunction == null) { return; } Language language = vm().getLanguages().get(mimeType()); assertNotNull("Language for " + mimeType() + " found", language); PolyglotEngine.Value function = vm().findGlobalSymbol(globalObjectFunction); Object global = function.execute().get(); assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global); assertIsObjectOfLanguage(global); }
/** @since 0.16 */ @Test public void testWriteToObjectWithValueProperty() throws Exception { String id = objectWithValueProperty(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); ObjectWithValueInterface object = JavaInterop.asJavaObject(ObjectWithValueInterface.class, truffleObject); Assert.assertEquals(42.0, object.value(), 0.1); object.value(13.0); Assert.assertEquals(13.0, object.value(), 0.1); }
/** @since 0.16 */ @Test public void testObjectWithValueAndAddProperty() throws Exception { String id = objectWithValueAndAddProperty(); if (id == null) { return; } PolyglotEngine.Value apply = findGlobalSymbol(id); TruffleObject truffleObject = (TruffleObject) apply.execute().get(); assertIsObjectOfLanguage(truffleObject); ObjectWithValueInterface object = JavaInterop.asJavaObject(ObjectWithValueInterface.class, truffleObject); object.add(20.0); object.add(22.0); Assert.assertEquals(42.0, object.value(), 0.1); }
assertIsObjectOfLanguage(obj); KeyInfoInterface object = JavaInterop.asJavaObject(KeyInfoInterface.class, obj);