Search in sources :

Example 16 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class HostAccessTest method banAccessToEquals.

@Test
public void banAccessToEquals() throws Exception {
    // @formatter:off
    HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(Object.class, false).build();
    // @formatter:on
    setupEnv(config);
    Value readValue = context.eval("sl", "" + "function readValue(x, y) {\n" + "  return x.equals(y);\n" + "}\n" + "function main() {\n" + "  return readValue;\n" + "}\n");
    MyEquals myEquals = new MyEquals();
    assertTrue("MyEquals.equals method is accessible", readValue.execute(myEquals, myEquals).asBoolean());
    Value res;
    try {
        res = readValue.execute(new Object());
    } catch (PolyglotException ex) {
        return;
    }
    fail("expecting no result: " + res);
}
Also used : HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 17 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class JavaStringCoercionTest method before.

@Before
public void before() {
    HostAccess access = TargetMappings.enableStringCoercions(HostAccess.newBuilder().allowPublicAccess(true)).build();
    setupEnv(Context.newBuilder().allowHostAccess(access).allowAllAccess(true).build(), ProxyLanguage.setDelegate(new ProxyLanguage() {
    }));
}
Also used : HostAccess(org.graalvm.polyglot.HostAccess) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Before(org.junit.Before)

Example 18 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class HostAccessExamplesTest method banAccessToReflection.

@Test
public void banAccessToReflection() throws Exception {
    // @formatter:off
    HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(Class.class).denyAccess(Method.class).denyAccess(Field.class).denyAccess(Proxy.class).denyAccess(Object.class, false).build();
    try (Context context = Context.newBuilder().allowHostAccess(config).build()) {
        Value readValue = context.eval("sl", "" + "function readValue(x, y) {\n" + "  return x.equals(y);\n" + "}\n" + "function main() {\n" + "  return readValue;\n" + "}\n");
        MyEquals myEquals = new MyEquals();
        assertTrue("MyEquals.equals method is accessible", readValue.execute(myEquals, myEquals).asBoolean());
        Value res;
        try {
            res = readValue.execute(new Object());
        } catch (PolyglotException ex) {
            return;
        }
        fail("expecting no result: " + res);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Field(java.lang.reflect.Field) MyEquals(com.oracle.truffle.api.test.polyglot.HostAccessTest.MyEquals) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 19 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class ArrayToObjectMappingTest method testArrayAsMapLong.

@Test
public void testArrayAsMapLong() {
    HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL).targetTypeMapping(Value.class, Object.class, v -> v.hasMembers() && v.hasArrayElements(), v -> v.as(new TypeLiteral<Map<Long, Object>>() {
    })).build();
    try (Context c = Context.newBuilder("js").allowHostAccess(hostAccess).build()) {
        Value fn = c.asValue((Function<Object, Object>) f -> {
            if (f instanceof Map) {
                Assert.assertEquals(4, ((Map<?, ?>) f).size());
                return "Map";
            }
            return f.getClass().getName();
        });
        Value result = fn.execute(new ArrayAndMembers("a", "b", "c", "d"));
        Assert.assertEquals("Map", result.asString());
    }
}
Also used : ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) List(java.util.List) HostAccess(org.graalvm.polyglot.HostAccess) TypeLiteral(org.graalvm.polyglot.TypeLiteral) Value(org.graalvm.polyglot.Value) Map(java.util.Map) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) Test(org.junit.Test) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Function(java.util.function.Function) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Map(java.util.Map) Test(org.junit.Test)

Example 20 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class ValueHostConversionTest method testHostObjectTargetTypeMappingLowest.

@Test
public void testHostObjectTargetTypeMappingLowest() {
    HostAccess.Builder b = HostAccess.newBuilder();
    b.allowPublicAccess(true);
    b.targetTypeMapping(Number.class, String.class, null, Number::toString, TargetMappingPrecedence.LOWEST);
    b.targetTypeMapping(Number.class, Boolean.class, null, n -> n.doubleValue() != 0.0, TargetMappingPrecedence.LOWEST);
    b.targetTypeMapping(Number.class, Integer.class, null, n -> n.intValue(), TargetMappingPrecedence.LOWEST);
    b.targetTypeMapping(Number.class, Long.class, null, n -> n.longValue(), TargetMappingPrecedence.LOWEST);
    b.targetTypeMapping(Number.class, Double.class, null, n -> n.doubleValue(), TargetMappingPrecedence.LOWEST);
    HostAccess hostAccess = b.build();
    setupEnv(Context.newBuilder().allowHostAccess(hostAccess).build());
    assertEquals("42", context.asValue(new BigDecimal(42)).as(String.class));
    assertEquals(false, context.asValue(new BigDecimal(0)).as(Boolean.class));
    assertEquals(true, context.asValue(new BigDecimal(1)).as(Boolean.class));
    assertEquals(Integer.valueOf(42), context.asValue(new BigDecimal(42)).as(Integer.class));
    assertEquals(Integer.valueOf(42), context.asValue(new BigDecimal(42.5)).as(Integer.class));
    assertEquals(Long.valueOf(42), context.asValue(new BigDecimal(42)).as(Long.class));
    assertEquals(Long.valueOf(42), context.asValue(new BigDecimal(42.5)).as(Long.class));
    assertEquals(Double.valueOf(42.5), context.asValue(new BigDecimal(42.5)).as(Double.class));
    assertEquals(Double.valueOf(Double.POSITIVE_INFINITY), context.asValue(new BigDecimal(2).pow(9001)).as(Double.class));
    // sanity check: default mappings take precedence over lowest in overload selection.
    assertEquals("object", context.asValue(new StringHierarchy()).invokeMember("hierarchy", new BigDecimal(42)).asString());
    // the only applicable method is one that requires the lowest precedence target mapping.
    Value onlyString = context.asValue(new OnlyString());
    assertEquals("42", onlyString.invokeMember("accept", new BigDecimal(42)).asString());
    Value onlyInt = context.asValue(new OnlyInt());
    assertEquals("42", onlyInt.invokeMember("accept", new BigDecimal(42)).asString());
}
Also used : BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) HostAccess(org.graalvm.polyglot.HostAccess) ValueAssert.assertValue(com.oracle.truffle.tck.tests.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

HostAccess (org.graalvm.polyglot.HostAccess)35 Test (org.junit.Test)30 Context (org.graalvm.polyglot.Context)25 Value (org.graalvm.polyglot.Value)25 LanguageContext (com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)13 PolyglotException (org.graalvm.polyglot.PolyglotException)6 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)6 Export (org.graalvm.polyglot.HostAccess.Export)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 ValueAssert.assertValue (com.oracle.truffle.tck.tests.ValueAssert.assertValue)2 Field (java.lang.reflect.Field)2 URLClassLoader (java.net.URLClassLoader)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Function (java.util.function.Function)2 TypeLiteral (org.graalvm.polyglot.TypeLiteral)2 ProxyArray (org.graalvm.polyglot.proxy.ProxyArray)2 Assert (org.junit.Assert)2 Before (org.junit.Before)2