Search in sources :

Example 86 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class BuiltinTest method testInvalidArguments.

@Test
public void testInvalidArguments() throws InteropException {
    BuiltinTestObject testObject = new BuiltinTestObject();
    InteropLibrary builtinLib = createLibrary(InteropLibrary.class, testObject);
    try {
        builtinLib.invokeMember(testObject, EXISTING);
        Assert.fail();
    } catch (ArityException e) {
    }
    try {
        builtinLib.invokeMember(testObject, EXISTING, "", "");
        Assert.fail();
    } catch (ArityException e) {
    }
    try {
        builtinLib.invokeMember(testObject, EXISTING, 42);
        Assert.fail();
    } catch (UnsupportedTypeException e) {
    }
    Object function = builtinLib.readMember(testObject, EXISTING);
    InteropLibrary functionLib = createLibrary(InteropLibrary.class, function);
    try {
        Assert.assertEquals("test42", functionLib.execute(function));
        Assert.fail();
    } catch (ArityException e) {
    }
    try {
        Assert.assertEquals("test42", functionLib.execute(function, "", ""));
        Assert.fail();
    } catch (ArityException e) {
    }
    try {
        Assert.assertEquals("test42", functionLib.execute(function, 42));
        Assert.fail();
    } catch (UnsupportedTypeException e) {
    }
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test) InteropLibraryBaseTest(com.oracle.truffle.api.test.interop.InteropLibraryBaseTest)

Example 87 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class BuiltinTest method testExisting.

@Test
public void testExisting() throws InteropException {
    BuiltinTestObject testObject = new BuiltinTestObject();
    InteropLibrary builtinLib = createLibrary(InteropLibrary.class, testObject);
    Assert.assertTrue("test42", builtinLib.isMemberReadable(testObject, EXISTING));
    Assert.assertTrue("test42", builtinLib.isMemberInvocable(testObject, EXISTING));
    Object function = builtinLib.readMember(testObject, EXISTING);
    InteropLibrary functionLib = createLibrary(InteropLibrary.class, function);
    Assert.assertEquals("test", builtinLib.invokeMember(testObject, "testArg0"));
    Assert.assertEquals("test42", builtinLib.invokeMember(testObject, EXISTING, "42"));
    Assert.assertEquals("test42", builtinLib.invokeMember(testObject, "testArg2", "4", "2"));
    Assert.assertEquals("test42", functionLib.execute(function, "42"));
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test) InteropLibraryBaseTest(com.oracle.truffle.api.test.interop.InteropLibraryBaseTest)

Example 88 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class BuiltinTest method testNotExisting.

@Test
public void testNotExisting() throws InteropException {
    BuiltinTestObject testObject = new BuiltinTestObject();
    InteropLibrary builtinLib = createLibrary(InteropLibrary.class, testObject);
    Assert.assertFalse("test42", builtinLib.isMemberReadable(testObject, NOT_EXISTING));
    Assert.assertFalse("test42", builtinLib.isMemberInvocable(testObject, NOT_EXISTING));
    try {
        builtinLib.readMember(testObject, NOT_EXISTING);
        Assert.fail();
    } catch (UnknownIdentifierException e) {
    }
    try {
        builtinLib.invokeMember(testObject, NOT_EXISTING);
        Assert.fail();
    } catch (UnknownIdentifierException e) {
    }
}
Also used : UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test) InteropLibraryBaseTest(com.oracle.truffle.api.test.interop.InteropLibraryBaseTest)

Example 89 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class BuiltinTest method testGetMembers.

@Test
public void testGetMembers() throws InteropException {
    BuiltinTestObject testObject = new BuiltinTestObject();
    InteropLibrary builtinLib = createLibrary(InteropLibrary.class, testObject);
    Object members = builtinLib.getMembers(testObject);
    InteropLibrary membersLib = createLibrary(InteropLibrary.class, members);
    Assert.assertTrue(membersLib.hasArrayElements(members));
    Assert.assertEquals(4, membersLib.getArraySize(members));
    Assert.assertEquals(EXISTING, membersLib.readArrayElement(members, 1));
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test) InteropLibraryBaseTest(com.oracle.truffle.api.test.interop.InteropLibraryBaseTest)

Example 90 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class AbstractExecutableTestLanguage method parse.

@Override
@SuppressWarnings("unused")
protected final CallTarget parse(ParsingRequest request) throws Exception {
    ExecutableContext executableContext = TestAPIAccessor.engineAccess().getCurrentContext(AbstractExecutableTestLanguage.this.getClass());
    Object[] ctxArgs = createArgumentsArray(executableContext);
    onParse(request, executableContext.env, ctxArgs);
    String rootNodeName = getRootName(request, executableContext.env, ctxArgs);
    SourceSection srcSec = request.getSource().createSection(0, request.getSource().getLength());
    return new RootNode(this, null) {

        @Child
        InteropLibrary interopLibrary = interop;

        private final SourceSection sourceSection = srcSec;

        @CompilationFinal(dimensions = 1)
        private final Object[] contextArguments = ctxArgs;

        @Override
        public Object execute(VirtualFrame frame) {
            ExecutableContext execCtx = TestAPIAccessor.engineAccess().getCurrentContext(AbstractExecutableTestLanguage.this.getClass());
            try {
                Object returnValue = AbstractExecutableTestLanguage.this.execute(this, execCtx.env, contextArguments, frame.getArguments());
                if (returnValue == null) {
                    return NullObject.SINGLETON;
                }
                return returnValue;
            } catch (AbstractTruffleException e) {
                throw e;
            } catch (Exception e) {
                throw throwAssertionError(e);
            }
        }

        @TruffleBoundary
        private AssertionError throwAssertionError(Exception e) {
            throw new AssertionError(e);
        }

        @Override
        public String getName() {
            return rootNodeName;
        }

        @Override
        public SourceSection getSourceSection() {
            return srcSec;
        }
    }.getCallTarget();
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CompilationFinal(com.oracle.truffle.api.CompilerDirectives.CompilationFinal) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceSection(com.oracle.truffle.api.source.SourceSection)

Aggregations

InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)158 Test (org.junit.Test)76 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)60 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)51 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)18 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)18 ArityException (com.oracle.truffle.api.interop.ArityException)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)15 WasmInstance (org.graalvm.wasm.WasmInstance)15 WebAssembly (org.graalvm.wasm.api.WebAssembly)15 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)12 RootNode (com.oracle.truffle.api.nodes.RootNode)12 InteropException (com.oracle.truffle.api.interop.InteropException)11 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)11 Dictionary (org.graalvm.wasm.api.Dictionary)8 SourceSection (com.oracle.truffle.api.source.SourceSection)6 CallTarget (com.oracle.truffle.api.CallTarget)5 Node (com.oracle.truffle.api.nodes.Node)5 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)5 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)5