Java Code Examples for org.graalvm.polyglot.Context#create()

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

    final org.graalvm.polyglot.Value bindings = context.getBindings("js");
    bindings.putMember("absent", bridge.hostToGuest(Item.absent()));
    bindings.putMember("extant", bridge.hostToGuest(Item.extant()));
    bindings.putMember("empty", bridge.hostToGuest(Item.empty()));

    assertEquals(bridge.guestToHost(context.eval("js", "absent")), Item.absent());
    assertEquals(context.eval("js", "absent.isDefined()").asBoolean(), false);
    assertEquals(context.eval("js", "absent.isDistinct()").asBoolean(), false);

    assertEquals(bridge.guestToHost(context.eval("js", "extant")), Item.extant());
    assertEquals(context.eval("js", "extant.isDefined()").asBoolean(), true);
    assertEquals(context.eval("js", "extant.isDistinct()").asBoolean(), false);

    assertEquals(bridge.guestToHost(context.eval("js", "empty")), Item.empty());
    assertEquals(context.eval("js", "empty.isDefined()").asBoolean(), true);
    assertEquals(context.eval("js", "empty.isDistinct()").asBoolean(), true);
  }
}
 
Example 2
Source File: HashemInstrumentTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
/**
 * Test that we can alter bebin arguments on reenter.
 */
@Test
public void testChangeArgumentsOnReenter() throws Exception {
    String code = "bebin azinja() {\n" +
            "  y = fce(0, 10000);\n" +
            "  bede y;\n" +
            "}\n" +
            "bebin fce(x, z) {\n" +
            "  y = 2 * x;\n" +
            "  age (y < z) bood {\n" +
            "    print(\"A bad error.\");\n" +
            "    bede 0 - 1;\n" +
            "  } na? {\n" +
            "    bede y;\n" +
            "  }\n" +
            "}\n";
    final Source source = Source.newBuilder("hashemi", code, "testing").build();
    Context context = Context.create();
    IncreaseArgOnErrorInstrument incOnError = context.getEngine().getInstruments().get("testIncreaseArgumentOnError").lookup(IncreaseArgOnErrorInstrument.class);
    incOnError.attachOn("A bad error");

    Value ret = context.eval(source);
    assertEquals(10000, ret.asInt());
}
 
Example 3
Source File: HashemFactorialTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Before
public void initEngine() throws Exception {
    context = Context.create();
    // @formatter:off
    context.eval("hashemi", "\n" +
            "bebin fac(n) {\n" +
            "  age (n <= 1) bood {\n" +
            "    bede 1;\n" +
            "  }\n" +
            "  prev = fac(n - 1);\n" +
            "  bede prev * n;\n" +
            "}\n"
    );
    // @formatter:on
    factorial = context.getBindings("hashemi").getMember("fac");
}
 
Example 4
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 5
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 6
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 7
Source File: JsHostPrototypeSpec.java    From swim with Apache License 2.0 6 votes vote down vote up
@Test
public void testHostObjectPrototypeChain() {
  try (Context context = Context.create()) {
    final JsHostRuntime runtime = new JsHostRuntime();
    final JsBridge bridge = new JsBridge(runtime, context);

    runtime.addHostLibrary(SwimStructure.LIBRARY);
    runtime.addHostModule("@swim/structure", SwimStructure.LIBRARY);

    final JsModule module = bridge.eval("test", ""
        + "const {Item, Value, Absent} = require('@swim/structure');\n"
        + "console.log('Item.prototype: ' + Item.prototype);\n"
        + "console.log('Value.prototype: ' + Value.prototype);\n"
        + "console.log('Value.prototype.__proto__: ' + Value.prototype.__proto__);\n"
        + "console.log('Absent.prototype.__proto__: ' + Absent.prototype.__proto__);\n"
        + "console.log('Absent.prototype.__proto__.__proto__: ' + Absent.prototype.__proto__.__proto__);\n"
        + "console.log('Item.absent().__proto__: ' + Item.absent().__proto__);\n"
        + "module.exports = Item.absent().__proto__.constructor;\n");
    System.out.println("Item.absent().__proto__.constructor: " + bridge.guestToHost(module.moduleExports()));
  }
}
 
Example 8
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 9
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 10
Source File: HashemReadPropertyTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Before
public void setUp() {
    this.ctx = Context.create("hashemi");
    this.hashemObject = ctx.eval("hashemi", "bebin createObject() {\n" +
                    "obj1 = jadid();\n" +
                    "obj1.number = 42;\n" +
                    "bede obj1;\n" +
                    "}\n" +
                    "bebin azinja() {\n" +
                    "bede createObject;\n" +
                    "}").execute();
}
 
Example 11
Source File: BasicAnalysisTest.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    this.context = Context.create("js");
    context.eval(JavaScriptLanguage.ID, "");
    this.instrument = context.getEngine().getInstruments().get(NodeProfInstrument.ID).lookup(NodeProfInstrument.class);
    this.initAnalysis();
    GlobalObjectCache.reset();
    Logger.info("Test starts");
}
 
Example 12
Source File: JalangiTest.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithTemplate() throws IOException {
    Context context = Context.create("js");

    // TODO this walks to the NodeProf root to find emptyTemplate.js, is that how it's done?
    File dir = new File(new File(".").getAbsolutePath());
    do {
        dir = dir.getParentFile();
    } while (dir != null && dir.list() != null && !Arrays.asList(dir.list()).contains("mx.nodeprof"));
    assertNotNull(dir);
    assertTrue(dir.isDirectory());

    String templatePath = dir + "/src/ch.usi.inf.nodeprof/js/analysis/trivial/emptyTemplate.js";

    // minimal harness
    context.eval("js", "J$ = {};");

    // evaluate analysis template
    context.eval("js", Source.readFully(new FileReader(templatePath)));

    // retrieve properties (ie. the callbacks) defined in analysis object
    Value v = context.eval("js", "Object.getOwnPropertyNames(J$.analysis)");
    assertTrue(v.hasArrayElements());

    // test callbacks from template
    @SuppressWarnings("unchecked")
    List<String> callbacks = v.as(List.class);
    for (String cb : callbacks) {
        if (JalangiAnalysis.unimplementedCallbacks.contains(cb) || JalangiAnalysis.ignoredCallbacks.contains(cb)) {
            // nothing to test
            continue;
        }
        // for all other callbacks, check if they map to a tag
        assertNotNull("not in callback map: " + cb, JalangiAnalysis.callbackMap.get(cb));
        assertTrue("does not map to any tag: " + cb, JalangiAnalysis.callbackMap.get(cb).size() > 0);
    }
}
 
Example 13
Source File: HashemExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    this.ctx = Context.create("hashemi");
}
 
Example 14
Source File: HashemInteropPrimitiveTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    context = Context.create("hashemi");
}
 
Example 15
Source File: HashemInteropOperatorTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    context = Context.create("hashemi");
}
 
Example 16
Source File: HashemParseErrorTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    context = Context.create("hashemi");
}
 
Example 17
Source File: ToStringOfEvalTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void initialize() {
    context = Context.create();
}
 
Example 18
Source File: HashemInteropObjectTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    context = Context.create("hashemi");
}
 
Example 19
Source File: HashemInteropControlFlowTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    context = Context.create("hashemi");
}
 
Example 20
Source File: HashemExecutionListenerTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Before
public void setUp() {
    context = Context.create("hashemi");
}