Skip to content

Reflection issue from javascript passing Uint8Array argument to a java byte array #2118

@javabully

Description

@javabully

I would expect a Uint8Array created in a javascript context to be converted to a java byte array when invoking a context bound java service. This works as expected unless a value > 127 is inserted into the array. The following class demonstrates this.
`
package test;

import java.util.Base64;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.EnvironmentAccess;
import org.graalvm.polyglot.HostAccess;

public class BugExample {

public static void main(String[] args) {
	
	Context context = null;
	try {
		
		context = Context.newBuilder("js")
				.allowCreateProcess(false)
				.allowIO(false)
				.allowHostClassLoading(false)
				.allowEnvironmentAccess(EnvironmentAccess.NONE)
				.allowHostAccess(HostAccess.EXPLICIT)
				.allowNativeAccess(false)
				.build();
		
		context.getBindings("js").putMember("base64", new Base64Util());
		// test the working case
		String source = "base64.encodeToString(new Uint8Array([22]));";
		System.err.println("base64: " + context.eval("js", source));
		
		// now test the fail
		source = "base64.encodeToString(new Uint8Array([223]));";
		System.err.println("base64: " + context.eval("js", source));
		
	} catch(Exception e) {
		e.printStackTrace();
	} finally {
		context.close(true);
	}
}

public static class Base64Util {

	@HostAccess.Export
	public String encodeToString(byte[] data) {

		return Base64.getEncoder().encodeToString(data);
		
	}
}

}
`
Executing the class outputs the following:
base64: Fg==
TypeError: invokeMember (encodeToString) on JavaObject[test.BugExample$Base64Util@1ede5e85 (test.BugExample$Base64Util)] failed due to: UnsupportedTypeException
at :program(Unnamed:1:0-43)
at org.graalvm.polyglot.Context.eval(Context.java:370)
at test.BugExample.main(BugExample.java:36)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions