Skip to content

Commit 51fe27b

Browse files
author
Nicolas Laurent
committed
[GR-20150] Make top-level methods available in the Context#getBindings().
PullRequest: truffleruby/1197
2 parents 52c2a86 + 419fdfd commit 51fe27b

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Bug fixes:
5151
* Fixed `Symbol#to_proc` to create proc with nil `source_location` (#1663).
5252
* Make `GC.start` work with keyword arguments.
5353
* Fixed `Kernel#clone` for `nil`, `true`, `false`, `Integer`, and `Symbol`.
54+
* Make top-level methods available in `Context#getBindings()` (#1838).
5455

5556
Compatibility:
5657

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby;
1111

1212
import java.util.Arrays;
13-
import java.util.Collections;
1413

1514
import com.oracle.truffle.api.Assumption;
1615
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -277,7 +276,9 @@ protected Iterable<Scope> findLocalScopes(RubyContext context, Node node, Frame
277276

278277
@Override
279278
protected Iterable<Scope> findTopScopes(RubyContext context) {
280-
return Collections.singletonList(GlobalScope.getGlobalScope(context.getCoreLibrary().globalVariables));
279+
return Arrays.asList(
280+
GlobalScope.getGlobalScope(context.getCoreLibrary().globalVariables),
281+
Scope.newBuilder("main", context.getCoreLibrary().mainObject).build());
281282
}
282283

283284
public String getTruffleLanguageHome() {

src/test/java/org/truffleruby/PolyglotInteropTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,20 @@ public void testLocalVariablesAreNotSharedBetweenInteractiveAndNonInteractive()
193193
}
194194
}
195195

196+
@Test
197+
public void testTopScopes() {
198+
try (Context context = Context.create()) {
199+
Value bindings = context.getBindings("ruby");
200+
assertTrue(bindings.getMember("$DEBUG").isBoolean());
201+
202+
context.eval("ruby", "def my_test_method(); 42; end");
203+
Value myMethod = bindings.getMember("my_test_method");
204+
assertTrue(myMethod.canExecute());
205+
assertEquals(myMethod.execute().asInt(), 42);
206+
207+
Value formatMethod = bindings.getMember("format");
208+
assertTrue(formatMethod.canExecute());
209+
assertEquals(formatMethod.execute("hello").asString(), "hello");
210+
}
211+
}
196212
}

0 commit comments

Comments
 (0)