Skip to content

Commit 547a896

Browse files
committed
Separate binding of Kernel IO-related methods
This moves IO-related Kernel methods to a separate interface which can be enabled or disabled independently of the rest of Kernel. This relates to recent discussions about making JRuby safely embeddable with only specific classes and features loaded. See other work in jruby#8893
1 parent b2d7868 commit 547a896

File tree

1 file changed

+102
-19
lines changed

1 file changed

+102
-19
lines changed

core/src/main/java/org/jruby/RubyKernel.java

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
173173

174174
public static RubyModule finishKernelModule(ThreadContext context, RubyModule Kernel, RubyInstanceConfig config) {
175175
Kernel.defineMethods(context, RubyKernel.class);
176+
177+
if (context.runtime.getInstanceConfig().getProfile().allowClass("IO")) {
178+
Kernel.defineMethods(context, RubyKernel.KernelIO.class);
179+
}
180+
176181
Kernel.setFlag(RubyModule.NEEDSIMPL_F, false); //Kernel is the only normal Module that doesn't need an implementor
177182

178183
var runtime = context.runtime;
@@ -204,6 +209,103 @@ public IRubyObject call(ThreadContext context1, IRubyObject self, RubyModule cla
204209
return Kernel;
205210
}
206211

212+
public interface KernelIO {
213+
@JRubyMethod(name = "`", module = true, visibility = PRIVATE)
214+
static IRubyObject backquote(ThreadContext context, IRubyObject recv, IRubyObject str) {
215+
return RubyKernel.backquote(context, recv, str);
216+
}
217+
218+
@JRubyMethod(optional = 1, checkArity = false)
219+
static IRubyObject display(ThreadContext context, IRubyObject self, IRubyObject[] args) {
220+
return RubyKernel.display(context, self, args);
221+
}
222+
223+
@JRubyMethod(optional = 1, keywords = true, checkArity = false, module = true, visibility = PRIVATE)
224+
static IRubyObject gets(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
225+
return RubyKernel.gets(context, recv, args);
226+
}
227+
228+
@JRubyMethod(rest = true, module = true, visibility = PRIVATE)
229+
static IRubyObject p(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
230+
return RubyKernel.p(context, recv, args);
231+
}
232+
233+
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE)
234+
static IRubyObject print(ThreadContext context, IRubyObject recv) {
235+
return RubyKernel.print(context, recv);
236+
}
237+
238+
@JRubyMethod(module = true, visibility = PRIVATE)
239+
static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
240+
return RubyKernel.print(context, recv, arg0);
241+
}
242+
243+
@JRubyMethod(module = true, visibility = PRIVATE)
244+
static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
245+
return RubyKernel.print(context, recv, arg0, arg1);
246+
}
247+
248+
@JRubyMethod(module = true, visibility = PRIVATE)
249+
static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
250+
return RubyKernel.print(context, recv, arg0, arg1, arg2);
251+
}
252+
253+
@JRubyMethod(rest = true, module = true, visibility = PRIVATE, reads = LASTLINE)
254+
static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
255+
return RubyKernel.print(context, recv, args);
256+
}
257+
258+
@JRubyMethod(rest = true, module = true, visibility = PRIVATE)
259+
static IRubyObject printf(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
260+
return RubyKernel.printf(context, recv, args);
261+
}
262+
263+
@JRubyMethod(module = true, visibility = PRIVATE)
264+
static IRubyObject putc(ThreadContext context, IRubyObject recv, IRubyObject ch) {
265+
return RubyKernel.putc(context, recv, ch);
266+
}
267+
268+
@JRubyMethod(module = true, visibility = PRIVATE)
269+
static IRubyObject puts(ThreadContext context, IRubyObject recv) {
270+
return RubyKernel.puts(context, recv);
271+
}
272+
273+
@JRubyMethod(module = true, visibility = PRIVATE)
274+
static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
275+
return RubyKernel.puts(context, recv, arg0);
276+
}
277+
278+
@JRubyMethod(module = true, visibility = PRIVATE)
279+
static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
280+
return RubyKernel.puts(context, recv, arg0, arg1);
281+
}
282+
283+
@JRubyMethod(module = true, visibility = PRIVATE)
284+
static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
285+
return RubyKernel.puts(context, recv, arg0, arg1, arg2);
286+
}
287+
288+
@JRubyMethod(rest = true, module = true, visibility = PRIVATE)
289+
static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
290+
return RubyKernel.print(context, recv, args);
291+
}
292+
293+
@JRubyMethod(optional = 1, keywords = true, checkArity = false, module = true, visibility = PRIVATE)
294+
static IRubyObject readline(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
295+
return RubyKernel.readline(context, recv, args);
296+
}
297+
298+
@JRubyMethod(optional = 1, keywords = true, checkArity = false, module = true, visibility = PRIVATE)
299+
static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
300+
return RubyKernel.readlines(context, recv, args);
301+
}
302+
303+
@JRubyMethod(name = {"sprintf", "format"}, required = 1, rest = true, checkArity = false, module = true, visibility = PRIVATE)
304+
static IRubyObject sprintf(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
305+
return RubyKernel.sprintf(context, recv, args);
306+
}
307+
}
308+
207309
/**
208310
* Cache built-in versions of several core methods, to improve performance by using identity comparison (==) rather
209311
* than going ahead with dynamic dispatch.
@@ -346,7 +448,6 @@ public static IRubyObject getc(ThreadContext context, IRubyObject recv) {
346448
}
347449

348450
// MRI: rb_f_gets
349-
@JRubyMethod(optional = 1, keywords = true, checkArity = false, module = true, visibility = PRIVATE)
350451
public static IRubyObject gets(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
351452
var ArgsFile = argsFile(context);
352453

@@ -664,7 +765,6 @@ public static IRubyObject new_string(ThreadContext context, IRubyObject recv, IR
664765
}
665766

666767
// MRI: rb_f_p
667-
@JRubyMethod(rest = true, module = true, visibility = PRIVATE)
668768
public static IRubyObject p(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
669769
return RubyThread.uninterruptible(context, args, RubyKernel::pBody);
670770
}
@@ -713,7 +813,6 @@ public static IRubyObject public_method(ThreadContext context, IRubyObject recv,
713813

714814
/** rb_f_putc
715815
*/
716-
@JRubyMethod(module = true, visibility = PRIVATE)
717816
public static IRubyObject putc(ThreadContext context, IRubyObject recv, IRubyObject ch) {
718817
IRubyObject defout = globalVariables(context).get("$>");
719818
if (recv == defout) {
@@ -722,7 +821,6 @@ public static IRubyObject putc(ThreadContext context, IRubyObject recv, IRubyObj
722821
return sites(context).putc.call(context, defout, defout, ch);
723822
}
724823

725-
@JRubyMethod(module = true, visibility = PRIVATE)
726824
public static IRubyObject puts(ThreadContext context, IRubyObject recv) {
727825
IRubyObject defout = globalVariables(context).get("$>");
728826

@@ -733,7 +831,6 @@ public static IRubyObject puts(ThreadContext context, IRubyObject recv) {
733831
return sites(context).puts.call(context, defout, defout);
734832
}
735833

736-
@JRubyMethod(module = true, visibility = PRIVATE)
737834
public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
738835
IRubyObject defout = globalVariables(context).get("$>");
739836

@@ -744,7 +841,6 @@ public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObj
744841
return sites(context).puts.call(context, defout, defout, arg0);
745842
}
746843

747-
@JRubyMethod(module = true, visibility = PRIVATE)
748844
public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
749845
IRubyObject defout = globalVariables(context).get("$>");
750846

@@ -755,7 +851,6 @@ public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObj
755851
return sites(context).puts.call(context, defout, defout, arg0, arg1);
756852
}
757853

758-
@JRubyMethod(module = true, visibility = PRIVATE)
759854
public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
760855
IRubyObject defout = globalVariables(context).get("$>");
761856

@@ -766,7 +861,6 @@ public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObj
766861
return sites(context).puts.call(context, defout, defout, arg0, arg1, arg2);
767862
}
768863

769-
@JRubyMethod(rest = true, module = true, visibility = PRIVATE)
770864
public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
771865
IRubyObject defout = globalVariables(context).get("$>");
772866

@@ -778,33 +872,27 @@ public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObj
778872
}
779873

780874
// rb_f_print
781-
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE)
782875
public static IRubyObject print(ThreadContext context, IRubyObject recv) {
783876
return RubyIO.print0(context, globalVariables(context).get("$>"));
784877
}
785878

786-
@JRubyMethod(module = true, visibility = PRIVATE)
787879
public static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
788880
return RubyIO.print1(context, globalVariables(context).get("$>"), arg0);
789881
}
790882

791-
@JRubyMethod(module = true, visibility = PRIVATE)
792883
public static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
793884
return RubyIO.print2(context, globalVariables(context).get("$>"), arg0, arg1);
794885
}
795886

796-
@JRubyMethod(module = true, visibility = PRIVATE)
797887
public static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
798888
return RubyIO.print3(context, globalVariables(context).get("$>"), arg0, arg1, arg2);
799889
}
800890

801-
@JRubyMethod(rest = true, module = true, visibility = PRIVATE, reads = LASTLINE)
802891
public static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
803892
return RubyIO.print(context, globalVariables(context).get("$>"), args);
804893
}
805894

806895
// rb_f_printf
807-
@JRubyMethod(rest = true, module = true, visibility = PRIVATE)
808896
public static IRubyObject printf(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
809897
if (args.length == 0) return context.nil;
810898

@@ -821,7 +909,6 @@ public static IRubyObject printf(ThreadContext context, IRubyObject recv, IRubyO
821909
return context.nil;
822910
}
823911

824-
@JRubyMethod(optional = 1, keywords = true, checkArity = false, module = true, visibility = PRIVATE)
825912
public static IRubyObject readline(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
826913
IRubyObject line = gets(context, recv, args);
827914

@@ -832,7 +919,6 @@ public static IRubyObject readline(ThreadContext context, IRubyObject recv, IRub
832919
return line;
833920
}
834921

835-
@JRubyMethod(optional = 1, keywords = true, checkArity = false, module = true, visibility = PRIVATE)
836922
public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
837923
return RubyArgsFile.readlines(context, argsFile(context), args);
838924
}
@@ -1007,7 +1093,6 @@ public static RubyBoolean blockGiven(ThreadContext context, IRubyObject recv, Bl
10071093
return asBoolean(context, frameBlock.isGiven());
10081094
}
10091095

1010-
@JRubyMethod(name = {"sprintf", "format"}, required = 1, rest = true, checkArity = false, module = true, visibility = PRIVATE)
10111096
public static IRubyObject sprintf(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
10121097
if (args.length == 0) throw argumentError(context, "sprintf must have at least one argument");
10131098

@@ -1786,7 +1871,6 @@ private static int getTestCommand(ThreadContext context, IRubyObject arg0) {
17861871
return cmd;
17871872
}
17881873

1789-
@JRubyMethod(name = "`", module = true, visibility = PRIVATE)
17901874
public static IRubyObject backquote(ThreadContext context, IRubyObject recv, IRubyObject str) {
17911875
Ruby runtime = context.runtime;
17921876

@@ -2248,7 +2332,6 @@ public static IRubyObject dup(IRubyObject self) {
22482332
return ((RubyBasicObject)self).dup();
22492333
}
22502334

2251-
@JRubyMethod(optional = 1, checkArity = false)
22522335
public static IRubyObject display(ThreadContext context, IRubyObject self, IRubyObject[] args) {
22532336
Arity.checkArgumentCount(context, args, 0, 1);
22542337

0 commit comments

Comments
 (0)