Java Code Examples for org.graalvm.polyglot.Value#putMember()

The following examples show how to use org.graalvm.polyglot.Value#putMember() . 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: HostThrowableSpec.java    From swim with Apache License 2.0 6 votes vote down vote up
@Test
public void testThrowables() {
  try (Context context = Context.create()) {
    final JavaHostRuntime runtime = new JavaHostRuntime();
    final VmBridge bridge = new VmBridge(runtime, "js");
    runtime.addHostLibrary(JavaBase.LIBRARY);

    final Throwable testCause = new Throwable("test cause");
    final Throwable testThrowable = new Throwable("test throwable", testCause);

    final Value bindings = context.getBindings("js");
    bindings.putMember("testThrowable", bridge.hostToGuest(testThrowable));

    assertEquals(bridge.guestToHost(context.eval("js", "testThrowable")), testThrowable);
    assertEquals(context.eval("js", "testThrowable.getMessage()").asString(), "test throwable");
    assertEquals(bridge.guestToHost(context.eval("js", "testThrowable.getCause()")), testCause);
  }
}
 
Example 2
Source File: HostMapSpec.java    From swim with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpecializedMap() {
  try (Context context = Context.create()) {
    final JavaHostRuntime runtime = new JavaHostRuntime();
    final VmBridge bridge = new VmBridge(runtime, "js");
    runtime.addHostLibrary(JavaBase.LIBRARY);

    final Map<String, String> testMap = new HashMap<String, String>();
    testMap.put("foo", "bar");

    final Value bindings = context.getBindings("js");
    bindings.putMember("testMap", bridge.hostToGuest(testMap));

    assertNotNull(context.eval("js", "testMap.has").as(Object.class));
    assertNull(context.eval("js", "testMap.containsKey").as(Object.class));
    assertTrue(context.eval("js", "testMap.has('foo')").asBoolean());
    assertFalse(context.eval("js", "testMap.has('bar')").asBoolean());
  }
}
 
Example 3
Source File: HostMapSpec.java    From swim with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnspecializedMap() {
  try (Context context = Context.create()) {
    final JavaHostRuntime runtime = new JavaHostRuntime();
    final VmBridge bridge = new VmBridge(runtime, "unknown");
    runtime.addHostLibrary(JavaBase.LIBRARY);

    final Map<String, String> testMap = new HashMap<String, String>();
    testMap.put("foo", "bar");

    final Value bindings = context.getBindings("js");
    bindings.putMember("testMap", bridge.hostToGuest(testMap));

    assertNull(context.eval("js", "testMap.has").as(Object.class));
    assertNotNull(context.eval("js", "testMap.containsKey").as(Object.class));
    assertTrue(context.eval("js", "testMap.containsKey('foo')").asBoolean());
    assertFalse(context.eval("js", "testMap.containsKey('bar')").asBoolean());
  }
}
 
Example 4
Source File: HashemInteropObjectTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Test
public void testObject() {
    final Source src = Source.newBuilder("hashemi", "bebin azinja() {o = jadid(); o.a = 10; o.b = \"B\"; bede o;}", "testObject.hashem").buildLiteral();
    final Value obj = context.eval(src);
    Assert.assertTrue(obj.hasMembers());

    Value a = obj.getMember("a");
    Assert.assertNotNull(a);
    Assert.assertTrue(a.isNumber());
    Assert.assertEquals(10, a.asInt());

    Value b = obj.getMember("b");
    Assert.assertNotNull(b);
    Assert.assertTrue(b.isString());
    Assert.assertEquals("B", b.asString());

    obj.putMember("a", b);
    a = obj.getMember("a");
    Assert.assertTrue(a.isString());
    Assert.assertEquals("B", a.asString());

    obj.removeMember("a");
    Assert.assertFalse(obj.hasMember("a"));

    Assert.assertEquals("[b]", obj.getMemberKeys().toString());
}
 
Example 5
Source File: JsPlaneFactory.java    From swim with Apache License 2.0 5 votes vote down vote up
protected Value createGuestPlane(PlaneContext planeContext, JsBridge jsBridge, JsModule planeModule) {
  final Object guestPlaneContext = jsBridge.hostToGuest(planeContext);
  final Value planeExports = planeModule.moduleExports();
  final Value guestPlane;
  if (planeExports.canInstantiate()) {
    guestPlane = planeExports.newInstance(guestPlaneContext);
  } else {
    guestPlane = planeExports;
    if (guestPlane.hasMembers()) {
      guestPlane.putMember("context", guestPlaneContext);
    }
  }
  return guestPlane;
}
 
Example 6
Source File: JsGuestModule.java    From swim with Apache License 2.0 5 votes vote down vote up
@Override
public void evalModule() {
  final Context jsContext = this.moduleSystem.jsContext;
  final Value bindings = jsContext.getBindings("js");
  bindings.putMember("require", this.requireFunction);
  bindings.putMember("module", this.moduleObject);
  bindings.putMember("exports", this.moduleExports);
  this.moduleSystem.jsContext.eval(this.moduleSource);
  bindings.removeMember("exports");
  bindings.removeMember("module");
  bindings.removeMember("require");
}
 
Example 7
Source File: VmHostObjectSpec.java    From swim with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMember() {
  try (Context context = Context.create()) {
    final JavaHostRuntime runtime = new JavaHostRuntime();
    final VmBridge bridge = new VmBridge(runtime, "js");
    runtime.addHostType(Foo.TYPE);

    final Value bindings = context.getBindings("js");
    bindings.putMember("foo", bridge.hostToGuest(new Foo()));

    assertEquals(context.eval("js", "foo.bar").asString(), "BAR");
  }
}
 
Example 8
Source File: VmHostObjectSpec.java    From swim with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeMember() {
  try (Context context = Context.create()) {
    final JavaHostRuntime runtime = new JavaHostRuntime();
    final VmBridge bridge = new VmBridge(runtime, "js");
    runtime.addHostType(Foo.TYPE);

    final Value bindings = context.getBindings("js");
    bindings.putMember("foo", bridge.hostToGuest(new Foo()));

    assertEquals(context.eval("js", "foo.baz()").asString(), "BAZ");
  }
}
 
Example 9
Source File: JavaScriptSourceLoader.java    From camel-k-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public Result load(Runtime runtime, Source source) throws Exception {
    RoutesBuilder builder = new EndpointRouteBuilder() {
        @Override
        public void configure() throws Exception {
            final Context context = Context.newBuilder("js").allowAllAccess(true).build();

            try (InputStream is = source.resolveAsInputStream(getContext())) {
                Value bindings = context.getBindings(LANGUAGE_ID);

                // configure bindings
                bindings.putMember("__dsl", new IntegrationConfiguration(this));

                final String script = new String(is.readAllBytes(), StandardCharsets.UTF_8);
                final String wrappedScript = "with (__dsl) { " + script + " }";

                context.eval(LANGUAGE_ID, wrappedScript);

                //
                // Close the polyglot context when the camel context stops
                //
                getContext().addLifecycleStrategy(new LifecycleStrategySupport() {
                    @Override
                    public void onContextStop(CamelContext camelContext) {
                        context.close(true);
                    }
                });
            }
        }
    };

    return Result.on(builder);
}