Fix native build (#1099)

* Add truffle boundaries
* Fix snippet output tests
* Make java LanguageSnippetTests eval output.bytes, just like the CLI does
This commit is contained in:
Daniel Chao
2025-06-17 08:42:49 -07:00
committed by GitHub
parent e9320557b7
commit d6fd7e0942
216 changed files with 870 additions and 5 deletions

View File

@@ -58,6 +58,7 @@ public final class BytesLiteralNode extends ExpressionNode {
var result = (Long) typeNode.execute(frame, elem.executeGeneric(frame));
bytes[i] = result.byteValue();
} catch (VmTypeMismatchException err) {
CompilerDirectives.transferToInterpreter();
// optimization: don't create a new stack frame to check the type, but pretend that one
// exists.
err.putInsertedStackFrame(

View File

@@ -16,6 +16,7 @@
package org.pkl.core.stdlib.base;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
@@ -106,12 +107,17 @@ public final class BytesNodes {
}
public abstract static class decodeToString extends ExternalMethod1Node {
@TruffleBoundary
private String doDecode(VmBytes self, String charset) throws CharacterCodingException {
var byteBuffer = ByteBuffer.wrap(self.getBytes());
var decoder = Charset.forName(charset).newDecoder();
return decoder.decode(byteBuffer).toString();
}
@Specialization
protected String eval(VmBytes self, String charset) {
try {
var byteBuffer = ByteBuffer.wrap(self.getBytes());
var decoder = Charset.forName(charset).newDecoder();
return decoder.decode(byteBuffer).toString();
return doDecode(self, charset);
} catch (CharacterCodingException e) {
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder().evalError("characterCodingException", charset).build();

View File

@@ -15,6 +15,7 @@
*/
package org.pkl.core.util;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
@@ -23,22 +24,28 @@ import org.pkl.core.runtime.VmExceptionBuilder;
public final class ByteArrayUtils {
private ByteArrayUtils() {}
// TODO: implement this directly so we don't need a truffle boundary here.
@TruffleBoundary
public static String base64(byte[] input) {
return Base64.getEncoder().encodeToString(input);
}
@TruffleBoundary
public static String md5(byte[] input) {
return hash(input, "MD5");
}
@TruffleBoundary
public static String sha1(byte[] input) {
return hash(input, "SHA-1");
}
@TruffleBoundary
public static String sha256(byte[] input) {
return hash(input, "SHA-256");
}
@TruffleBoundary
public static long sha256Int(byte[] input) {
return hashInt(input, "SHA-256");
}
@@ -48,7 +55,6 @@ public final class ByteArrayUtils {
* this and do not need a Truffle boundary.
*/
public static String toHex(byte[] hash) {
// return new BigInteger(hash).toString(16);
var hexDigitTable =
new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};