-
| 
         The class: package io.korandoru.trufflebf.language;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
@ExportLibrary(InteropLibrary.class)
@SuppressWarnings("unused")
public class BFState implements TruffleObject {
    // code segment
    private final CharSequence instructions;
    private final int end;
    private int ip;
    // data segment
    private long[] data;
    private int dp;
    public BFState(CharSequence instructions) {
        this.instructions = instructions;
        this.end = instructions.length();
    }
    @ExportMessage
    boolean hasLanguage() {
        return true;
    }
    @ExportMessage
    Class<? extends TruffleLanguage<?>> getLanguage() {
        return BFLanguage.class;
    }
    @ExportMessage
    @CompilerDirectives.TruffleBoundary
    Object toDisplayString(boolean allowSideEffects) {
        return "State";
    }
    @ExportMessage
    boolean isExecutable() {
        return true;
    }
    @ExportMessage
    Object execute(Object[] arguments) throws UnsupportedMessageException {
        // reset instructions pointer
        this.ip = 0;
        boolean initialized = false;
        switch (arguments.length) {
            case 0 -> {
                this.data = new long[1024];
                this.dp = 0;
                initialized = true;
            }
            case 2 -> {
                if (arguments[0] instanceof TruffleObject object) {
                    InteropLibrary library = InteropLibrary.getFactory().getUncached();
                    int size = (int) library.getArraySize(object);
                    this.data = new long[size];
                    for (int i = 0; i < size; i++) {
                        try {
                            this.data[i] = ((Number) library.readArrayElement(object, i)).longValue();
                        } catch (InvalidArrayIndexException e) {
                            throw UnsupportedMessageException.create(e);
                        }
                    }
                    if (arguments[1] instanceof Number pointer) {
                        this.dp = pointer.intValue();
                        initialized = true;
                    }
                }
            }
        }
        if (!initialized) {
            throw UnsupportedMessageException.create();
        }
        return this.instructions;
    }
}The output: The build command:  | 
  
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            tisonkun
          
      
      
        Dec 15, 2022 
      
    
    Replies: 2 comments
-
| 
         The general question can be: how do I write code as I wish in an export message context?  | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            -
| 
         It seems adding a   | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        tisonkun
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
It seems adding a
@CompilerDirectives.TruffleBoundaryannotation to theexecutemethod helps.