Skip to content

Commit 36020ad

Browse files
committed
Use the new inner context APIs
1 parent 540ca43 commit 36020ad

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ New features:
66
* Add `Java.add_to_classpath` method to add jar paths at runtime (#2693, @bjfish).
77
* Add support for Ruby 3.1's Hash shorthand/punning syntax (@nirvdrum).
88
* Add support for Ruby 3.1's anonymous block forwarding syntax (@nirvdrum).
9+
* Added the following keyword arguments to `Polyglot::InnerContext.new`: `languages, language_options, inherit_all_access, code_sharing` (@eregon).
910

1011
Bug fixes:
1112

spec/truffle/interop/polyglot/inner_context_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323
end
2424
end
2525

26+
it "can specify languages explicitly" do
27+
Polyglot::InnerContext.new(languages: ['ruby']) do |context|
28+
context.eval('ruby', '6 * 7').should == 42
29+
end
30+
end
31+
32+
it "can specify language options" do
33+
Polyglot::InnerContext.new(language_options: { 'ruby.debug' => 'true' }) do |context|
34+
context.eval('ruby', '$DEBUG').should == true
35+
end
36+
end
37+
38+
it "can specify whether to inherit access" do
39+
Polyglot::InnerContext.new(inherit_all_access: false, language_options: { 'ruby.core-load-path' => 'resource:/truffleruby' }) do |context|
40+
context.eval('ruby', 'Truffle::POSIX::NATIVE').should == false
41+
end
42+
end
43+
2644
it "treats Ruby objects from the inner context as foreign" do
2745
Polyglot::InnerContext.new do |context|
2846
obj = context.eval('ruby', "Object.new")

src/main/java/org/truffleruby/interop/PolyglotNodes.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.truffleruby.builtins.CoreModule;
2020
import org.truffleruby.builtins.Primitive;
2121
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
22+
import org.truffleruby.core.array.ArrayOperations;
23+
import org.truffleruby.core.array.RubyArray;
2224
import org.truffleruby.core.encoding.RubyEncoding;
2325
import org.truffleruby.core.klass.RubyClass;
2426
import org.truffleruby.core.proc.RubyProc;
@@ -170,14 +172,35 @@ public abstract static class InnerContextNewNode extends PrimitiveArrayArguments
170172

171173
@TruffleBoundary
172174
@Specialization
173-
protected RubyInnerContext newInnerContext(RubyClass rubyClass, RubyProc onCancelledCallback) {
174-
final TruffleContext innerContext = getContext()
175-
.getEnv()
176-
.newContextBuilder()
177-
.onCancelled(() -> {
178-
CallBlockNode.getUncached().yield(onCancelledCallback);
179-
})
175+
protected RubyInnerContext newInnerContext(
176+
RubyClass rubyClass,
177+
RubyArray languages,
178+
RubyArray languageOptions,
179+
boolean inheritAllAccess,
180+
Object codeSharing,
181+
RubyProc onCancelledCallback) {
182+
String[] permittedLanguages = new String[languages.size];
183+
int i = 0;
184+
for (Object language : ArrayOperations.toIterable(languages)) {
185+
permittedLanguages[i++] = RubyGuards.getJavaString(language);
186+
}
187+
188+
var builder = getContext().getEnv().newInnerContextBuilder(permittedLanguages);
189+
190+
var iterator = ArrayOperations.toIterable(languageOptions).iterator();
191+
while (iterator.hasNext()) {
192+
Object key = iterator.next();
193+
Object value = iterator.next();
194+
builder.option(RubyGuards.getJavaString(key), RubyGuards.getJavaString(value));
195+
}
196+
197+
Boolean codeSharingBoolean = codeSharing == nil ? null : (boolean) codeSharing;
198+
199+
final TruffleContext innerContext = builder
180200
.initializeCreatorContext(false)
201+
.inheritAllAccess(inheritAllAccess)
202+
.forceSharing(codeSharingBoolean)
203+
.onCancelled(() -> CallBlockNode.getUncached().yield(onCancelledCallback))
181204
.build();
182205

183206
return new RubyInnerContext(

src/main/ruby/truffleruby/core/truffle/polyglot.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ class UnsupportedMessageError < StandardError
1414
end
1515

1616
class InnerContext
17+
DEFAULT_ON_CANCELLED = -> { raise RuntimeError, 'Polyglot::InnerContext was terminated forcefully' }
18+
private_constant :DEFAULT_ON_CANCELLED
19+
1720
# Create a new isolated inner context to eval code in any available public language
1821
# (those languages can be listed with +Polyglot.languages+).
1922
# Automatically closes the context when given a block.
20-
def self.new(on_cancelled: -> { raise RuntimeError, 'Polyglot::InnerContext was terminated forcefully' })
21-
inner_context = Primitive.inner_context_new(self, on_cancelled)
23+
def self.new(languages: [], language_options: {}, inherit_all_access: true, code_sharing: true, on_cancelled: DEFAULT_ON_CANCELLED)
24+
languages = languages.map { |language| StringValue(language) }
25+
language_options = language_options.flat_map { |k, v| [StringValue(k), StringValue(v)] }
26+
code_sharing = code_sharing == :inherit ? nil : Primitive.as_boolean(code_sharing)
27+
28+
inner_context = Primitive.inner_context_new(self, languages, language_options, inherit_all_access, code_sharing, on_cancelled)
2229
if block_given?
2330
begin
2431
yield inner_context

0 commit comments

Comments
 (0)