org.graalvm.polyglot.HostAccess Java Examples

The following examples show how to use org.graalvm.polyglot.HostAccess. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GraalContext.java    From netbeans with Apache License 2.0 6 votes vote down vote up
synchronized final Context ctx() {
    if (ctx == null) {
        final Context.Builder b = Context.newBuilder();
        b.out(writer);
        b.err(errorWriter);
        if (reader != null) {
            try {
                b.in(new ReaderInputStream(reader, "UTF-8"));
            } catch (IOException ex) {
                throw raise(RuntimeException.class, ex);
            }
        }
        b.allowPolyglotAccess(PolyglotAccess.ALL);
        if (Boolean.TRUE.equals(getAttribute(ALLOW_ALL_ACCESS, ScriptContext.GLOBAL_SCOPE))) {
            b.allowHostAccess(HostAccess.ALL);
            b.allowAllAccess(true);
        } else {
            b.allowHostAccess(SANDBOX);
        }
        ctx = b.build();
    }
    return ctx;
}
 
Example #2
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
public void sumArray(List<Pair> pairs) {
    Object[] arr = pairs.toArray();
    assertNotNull("Array created", arr);
    for (Pair p : pairs) {
        sum(p);
    }
}
 
Example #3
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
public void sumArrayArray(List<List<Pair>> pairs) {
    Object[] arr = pairs.toArray();
    assertNotNull("Array created", arr);
    assertEquals("Two lists", 2, arr.length);
    for (List<Pair> list : pairs) {
        sumArray(list);
    }
}
 
Example #4
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
public void sumArrayMap(List<List<Map<String, Integer>>> pairs) {
    Object[] arr = pairs.toArray();
    assertNotNull("Array created", arr);
    assertEquals("Two lists", 2, arr.length);
    for (List<Map<String, Integer>> list : pairs) {
        for (Map<String, Integer> map : list) {
            Integer value = map.get("value");
            sum += value;
        }
    }
}
 
Example #5
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
public void sumMapArray(Map<String, List<Pair>> pairs) {
    assertEquals("Two elements", 2, pairs.size());
    Object one = pairs.get("one");
    assertNotNull(one);
    Object two = pairs.get("two");
    assertNotNull(two);

    sumArray(pairs.get("two"));
    sumArray(pairs.get("one"));
}
 
Example #6
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
public void validateNested() throws Exception {
    String sourceText = "bebin test(validator) {\n" +
                    "  bede validator.validateException();\n" +
                    "}";
    try (Context context = Context.newBuilder(HashemLanguage.ID).build()) {
        context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(HashemLanguage.ID).getMember("test");
        test.execute(Validator.this);
    }
}
 
Example #7
Source File: HashemJavaInteropConversionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
@SuppressWarnings("unchecked")
public int validateObject(Object value1, Value value2) {
    assertThat(value1, instanceOf(Map.class));
    assertTrue(!((Map<?, ?>) value1).isEmpty());
    assertThat(((Map<String, ?>) value1).keySet(), hasItems("a", "b"));
    assertThat(value2, instanceOf(Value.class));
    assertTrue(value2.hasMembers());
    assertThat(value2.getMemberKeys(), hasItems("a", "b"));
    return 42;
}
 
Example #8
Source File: HashemJavaInteropConversionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void testGR7318List() throws Exception {
    String sourceText = "bebin test(validator, array) {\n" +
            "  array[0] = jadid();\n" +
            "  array[1] = jadid();\n" +
            "  bede validator.validateList(array, array);\n" +
            "}";
    try (Context context = Context.newBuilder(HashemLanguage.ID).allowHostAccess(HostAccess.ALL).build()) {
        context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(HashemLanguage.ID).getMember("test");
        Value res = test.execute(new Validator(), new Object[2]);
        assertTrue(res.isNumber() && res.asInt() == 42);
    }
}
 
Example #9
Source File: SymbolTable.java    From typescript-generator with MIT License 5 votes vote down vote up
private CustomTypeNamingFunction getCustomTypeNamingFunction() throws ScriptException {
    if (customTypeNamingFunction == null) {
        final ScriptEngine engine = GraalJSScriptEngine.create(null, Context.newBuilder("js").allowHostAccess(HostAccess.ALL));
        engine.eval("var getName = " + settings.customTypeNamingFunction);
        final Invocable invocable = (Invocable) engine;
        customTypeNamingFunction = invocable.getInterface(CustomTypeNamingFunction.class);
    }
    return customTypeNamingFunction;
}
 
Example #10
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void create() {
    os = new ByteArrayOutputStream();
    context = Context.newBuilder().allowHostAccess(HostAccess.ALL).out(os).build();
}
 
Example #11
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@HostAccess.Export
public Sum sum(Pair p) {
    sum += p.value();
    return this;
}
 
Example #12
Source File: HashemWebServerTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@HostAccess.Export
public String getMethod() {
    return "POST";
}
 
Example #13
Source File: HashemWebServerTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@HostAccess.Export
@Override
public V get(Object key) {
    return super.get(key);
}
 
Example #14
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@HostAccess.Export
public int validateException() {
    throw new NoSuchElementException();
}
 
Example #15
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@HostAccess.Export
public long validateFunction(Supplier<Long> function) {
    return function.get();
}
 
Example #16
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@HostAccess.Export
public void validateMap(Map<String, Object> map) {
    Assert.assertNull(map.get(null));
}