Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Treat raw PolyglotFunction.apply(null) as one null argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
woess committed Jul 25, 2022
1 parent dde1006 commit fdc159e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
package com.oracle.truffle.api.test.host;

import java.util.Arrays;
import java.util.function.Function;

import org.graalvm.polyglot.Context;
Expand All @@ -65,6 +66,15 @@ public Object[] call(Function<Object, Object> fn) {
};
}

@SuppressWarnings({"static-method", "rawtypes", "unchecked"})
@HostAccess.Export
public Object[] callRaw(Function fn) {
return new Object[]{
fn.apply(null),
fn.apply(0),
};
}

@SuppressWarnings("static-method")
@HostAccess.Export
public Object callVarArgs(Function<Object[], Object> fn) {
Expand Down Expand Up @@ -93,6 +103,17 @@ public void testPolyglotFunctionVarArgs() {
}
}

@Test
public void testPolyglotFunctionRaw() {
try (Context context = Context.newBuilder().build()) {
Value applyTest = context.asValue(new ApplyTest());
Value expectSingleArgument = context.asValue(new ExpectSingleArgument());

Value result = applyTest.invokeMember("callRaw", expectSingleArgument);
Assert.assertArrayEquals(new Object[]{null, 0}, result.as(Object[].class));
}
}

@ExportLibrary(InteropLibrary.class)
static final class ExpectSingleArgument implements TruffleObject {
@SuppressWarnings("static-method")
Expand All @@ -104,7 +125,7 @@ boolean isExecutable() {
@SuppressWarnings("static-method")
@ExportMessage
Object execute(Object[] args) {
Assert.assertEquals(1, args.length);
Assert.assertEquals(Arrays.toString(args), 1, args.length);
return args[0];
}
}
Expand All @@ -120,7 +141,7 @@ boolean isExecutable() {
@SuppressWarnings("static-method")
@ExportMessage
Object execute(Object[] args) {
Assert.assertEquals(0, args.length);
Assert.assertEquals(Arrays.toString(args), 0, args.length);
return args.length;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ public final Object execute(PolyglotLanguageContext languageContext, Object func
argsArray = (Object[]) functionArgsObject;
}
} else {
if (paramType == null && functionArgsObject == null) {
argsArray = EMPTY;
} else if (paramType == null && functionArgsObject instanceof Object[]) {
if (paramType == null && functionArgsObject instanceof Object[]) {
argsArray = (Object[]) functionArgsObject;
} else {
argsArray = new Object[]{functionArgsObject};
Expand Down

0 comments on commit fdc159e

Please sign in to comment.