hpi-swa / trufflesqueak

A Squeak/Smalltalk VM and Polyglot Programming Environment for the GraalVM.
MIT License
283 stars 14 forks source link

How can evaluate Smalltalk code from Java? #136

Closed jvdsandt closed 3 years ago

jvdsandt commented 3 years ago

I wrote a small test program to see if I can evaluate Smalltalk code from Java:

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;

public class STTest {

    public static void main(String[] args) {
        new STTest().doTest();
    }

    public void doTest() {

        var sourceCode = "3 + 4";

        Context context = Context.newBuilder("smalltalk")
                .allowHostAccess(HostAccess.ALL)
                .allowHostClassLookup(className -> true)
                .build();
        Value v = context.eval("smalltalk", sourceCode);

        System.out.println("st: " + v);
    }
}

When I run this program I get a SecurityException telling me that the TruffleSqueak-21.0.0.image cannot be read. How can I fix this?

fniephaus commented 3 years ago

Hi @jvdsandt, Thanks for the report.

Could you provide the full error including a stack trace?

Please try again with allowIO(true) and if that doesn't work, try allowAllAccess(true). Let me know how it goes!

jvdsandt commented 3 years ago

Hi,

Yes, just adding allowIO(true) makes it work. Thanks!

If I want to use another Smalltalk image. How could I do this?

fniephaus commented 3 years ago

Great, thanks for the update. Nonetheless, I should have a look why allowIO(true) is required to access the bundled image.

You should be able to pass in a path to an image by setting smalltalk.imagePath when building the context. Here's how it's done in TruffleSqueak's tests:

https://github.com/hpi-swa/trufflesqueak/blob/d3534fec2b07e20829ac7fe1d52b637321393e24/src/de.hpi.swa.trufflesqueak.test/src/de/hpi/swa/trufflesqueak/test/AbstractSqueakTestCase.java#L131

Note that TruffleSqueak only supports 64bit Squeak and Cuis spur images at the moment.

fniephaus commented 3 years ago

I'm going to close this for now. Feel free to re-open if you have any follow up questions!