Skip to content

Expose public methods of the main/top-level object in the top scope/bindings #1838

@eregon

Description

@eregon

From @bmatthews68 on the GraalVM Slack:

I’m trying to get a simple demo working looking up function defined in a Ruby script as follows:

        try (var context = Context.create("ruby")) {
            context.eval("ruby", "def fibonacci0(a, b, n)\n" +
                    "  n == 0 ? a : fibonacci0(b, a + b, n - 1)\n" +
                    "end\n" +
                    "\n" +
                    "def fibonacci(n)\n" +
                    "  fibonacci0(0, 1, n)\n" +
                    "end");
            final var fn = context.getBindings("ruby").getMember("fibonacci");
            if (fn.canExecute()) {
                System.out.println("Found");
            } else {
                System.out.println("Not found");
            }
        }

I have the equivalent working for Javascript, R and Python. But the Ruby one doesn’t work

Currently, we only expose global variables in the bindings.
A workaround is to get the method object explicitly:

final var fn = context.eval("method(:fibonacci)");

Or to eval a lambda:

final var fn = context.eval("-> n { fibonacci(n) }");

which in general is useful for more complicated things

Metadata

Metadata

Assignees

Labels

polyglotUses multiple Truffle languages or host interopquick-fix

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions