/
ScriptingContainer.java
1950 lines (1819 loc) · 71.8 KB
/
ScriptingContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* **** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2009-2013 Yoko Harada <yokolet@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
* **** END LICENSE BLOCK *****
*/
package org.jruby.embed;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jruby.Profile;
import org.jruby.Ruby;
import org.jruby.RubyGlobal.InputGlobalVariable;
import org.jruby.RubyGlobal.OutputGlobalVariable;
import org.jruby.RubyIO;
import org.jruby.RubyInstanceConfig.CompileMode;
import org.jruby.RubyInstanceConfig.LoadServiceCreator;
import org.jruby.RubyInstanceConfig.ProfilingMode;
import org.jruby.embed.internal.BiVariableMap;
import org.jruby.embed.internal.ConcurrentLocalContextProvider;
import org.jruby.embed.internal.EmbedRubyInterfaceAdapterImpl;
import org.jruby.embed.internal.EmbedRubyObjectAdapterImpl;
import org.jruby.embed.internal.EmbedRubyRuntimeAdapterImpl;
import org.jruby.embed.internal.LocalContextProvider;
import org.jruby.embed.internal.SingleThreadLocalContextProvider;
import org.jruby.embed.internal.SingletonLocalContextProvider;
import org.jruby.embed.internal.ThreadSafeLocalContextProvider;
import org.jruby.embed.io.ReaderInputStream;
import org.jruby.embed.io.WriterOutputStream;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.GlobalVariable;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.Block;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.profile.builtin.ProfileOutput;
import org.jruby.util.KCode;
import org.jruby.util.SafePropertyAccessor;
import org.jruby.util.cli.OutputStrings;
import org.jruby.util.cli.Options;
/**
* ScriptingContainer provides various methods and resources that are useful
* for embedding Ruby in Java. Using this class, users can run Ruby scripts from
* Java programs easily. Also, users can use methods defined or implemented by Ruby.
*
* ScriptingContainer allows users to set various configuration parameters.
* Some of them are per-container properties, while others are per-evaluation attributes.
* For example, a local context scope, local variable behavior, load paths are
* per-container properties. Please see {@link PropertyName} and {@link AttributeName}
* for more details. Be aware that the per-container properties should be set prior to
* get Ruby runtime being instantiated; otherwise, default values are applied to.
*
* ScriptingContainer delays Ruby runtime initialization as much as possible to
* improve startup time. When values are put into the ScriptingContainer, or runScriptlet
* method gets run the runtime is created internally. However, the default, singleton
* local context scope behave slightly different. If a Ruby runtime has been already
* instantiated by another ScriptingContainer, application, etc, the same runtime
* will be re-used.
*
* Below are examples.
*
* The first Example is a very simple Hello World. After initializing a ScriptingContainer,
* a Ruby script, puts "Hello World!", runs and produces "Hello World!."
* <pre>
* Example 1:
* {@code
* ScriptingContainer container = new ScriptingContainer();
* container.runScriptlet("puts \"Hello World!\"");
* }
* Produces:
* Hello World!
* </pre>
*
* The second example shows how to share variables between Java and Ruby.
* In this example, a local variable {@code "x"} is shared. To make this happen,
* a local variable behavior should be transient or persistent.
* As for JSR223 JRuby engine, set these types using a System property,
* <b>org.jruby.embed.localvariable.behavior</b>.
* If the local variable behavior is one of transient or persistent,
* Ruby's local, instance, global variables and constants are available to share
* between Java and Ruby. (A class variable sharing does not work on current version)
* Thus, {@code "x"} in Java is also {@code "x"} in Ruby.
*
* <pre>
* Example 2:
* {@code
* ScriptingContainer container = new ScriptingContainer();
* container.put("x", 12345);
* container.runScriptlet("puts x.to_s(2)");
* }
* Produces:
* 11000000111001
* </pre>
*
* The third examples shows how to keep local variables across multiple evaluations.
* This feature simulates BSF engine for JRuby. In terms of Ruby semantics,
* local variables should not survive after the evaluation has completed. Thus,
* this behavior is optional, and users need to specify LocalVariableBehavior.PERSISTENT
* when the container is instantiated.
*
* <pre>
* Example 3:
* {@code
* ScriptingContainer container = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
* container.runScriptlet("p=9.0");
* container.runScriptlet("q = Math.sqrt p");
* container.runScriptlet("puts \"square root of #{p} is #{q}\"");
* System.out.println("Ruby used values: p = " + container.get("p") + ", q = " + container.get("q"));
* }
* Produces:
* square root of 9.0 is 3.0
* Ruby used values: p = 9.0, q = 3.0
* </pre>
*
* Also, ScriptingContainer provides better i18n support. For example,
* Unicode Escape Sequence can be included in Ruby scripts.
*
* <p>In addition, ScriptingContainer supports a parse-once-eval-many-times feature,
* invoking methods defined by Ruby, and getting an instance of a specified interface
* that has been implemented by Ruby.
*
* <pre>
* Example 4:
* {@code
* ScriptingContainer container = new ScriptingContainer();
* String script =
* "def message\n" +
* "\"message: #{@message}\"\n" +
* "end\n" +
* "message";
* container.put("@message", "What's up?");
* JavaEmbedUtils.EvalUnit unit = container.parse(script);
* IRubyObject msg = unit.run(); // a RubyString instance
* System.out.println(JavaEmbedUtils.rubyToJava(msg));
* container.put("@message", "Fabulous!");
* msg = unit.run();
* System.out.println(JavaEmbedUtils.rubyToJava(msg));
* container.put("@message", "That's the way you are.");
* msg = unit.run();
* System.out.println(JavaEmbedUtils.rubyToJava(msg));
* }
* Produces:
* message: What's up?
* message: Fabulous!
* message: That's the way you are.
* </pre>
*
* See more details at project's
* {@see <a href="https://github.com/jruby/jruby/wiki/RedBridge">Wiki</a>}
*
* @author Yoko Harada <yokolet@gmail.com>
*/
public class ScriptingContainer implements EmbedRubyInstanceConfigAdapter {
private final Map<String, String[]> basicProperties;
private final LocalContextScope scope;
private final LocalContextProvider provider;
private final EmbedRubyRuntimeAdapter runtimeAdapter = new EmbedRubyRuntimeAdapterImpl(this);
private final EmbedRubyObjectAdapter objectAdapter = new EmbedRubyObjectAdapterImpl(this);
private final EmbedRubyInterfaceAdapter interfaceAdapter = new EmbedRubyInterfaceAdapterImpl(this);
/**
* Constructs a ScriptingContainer with a default values.
*/
public ScriptingContainer() {
this(LocalContextScope.SINGLETON, LocalVariableBehavior.TRANSIENT, true);
}
/**
* Constructs a ScriptingContainer with a specified local context type.
*
* @param scope a local context type.
*/
public ScriptingContainer(LocalContextScope scope) {
this(scope, LocalVariableBehavior.TRANSIENT, true);
}
/**
* Constructs a ScriptingContainer with a specified local variable behavior.
*
* @param behavior a local variable behavior
*/
public ScriptingContainer(LocalVariableBehavior behavior) {
this(LocalContextScope.SINGLETON, behavior, true);
}
/**
* Constructs a ScriptingContainer with a specified local context type and
* variable behavior.
*
* @param scope a local context type
* @param behavior a local variable behavior
*/
public ScriptingContainer(LocalContextScope scope, LocalVariableBehavior behavior) {
this(scope, behavior, true);
}
/**
* Constructs a ScriptingContainer with a specified local context scope,
* local variable behavior and laziness.
*
* @param scope is one of a local context scope defined by {@link LocalContextScope}
* @param behavior is one of a local variable behavior defined by {@link LocalVariableBehavior}
* @param lazy is a switch to do lazy retrieval of variables/constants from
* Ruby runtime. Default is true. When this value is true, ScriptingContainer tries to
* get as many variables/constants as possible from Ruby runtime.
*/
public ScriptingContainer(LocalContextScope scope, LocalVariableBehavior behavior, boolean lazy) {
this.provider = getProviderInstance(scope, behavior, lazy);
this.scope = scope;
try {
initRubyInstanceConfig();
}
catch (RaiseException ex) {
// TODO this seems useless - except that we get a Java stack trace
throw new RuntimeException(ex);
}
basicProperties = getBasicProperties();
}
static LocalContextProvider getProviderInstance(LocalContextScope scope, LocalVariableBehavior behavior, boolean lazy) {
switch(scope) {
case THREADSAFE :
return new ThreadSafeLocalContextProvider(behavior, lazy);
case CONCURRENT :
return new ConcurrentLocalContextProvider(behavior, lazy);
case SINGLETHREAD :
return new SingleThreadLocalContextProvider(behavior, lazy);
case SINGLETON :
default :
return SingletonLocalContextProvider.getProvider(behavior, lazy);
}
}
private void initRubyInstanceConfig() throws RaiseException {
String home = SafePropertyAccessor.getenv("JRUBY_HOME");
if (home != null) {
provider.getRubyInstanceConfig().setJRubyHome(home);
}
provider.getRubyInstanceConfig().setScriptFileName("<script>");
}
// maybe these properties are not used at all?
private static Map<String, String[]> getBasicProperties() {
Map<String, String[]> properties = new HashMap<String, String[]>();
properties.put("container.ids", new String[]{"ruby", "jruby"});
properties.put("language.extension", new String[]{"rb"});
properties.put("language.name", new String[]{"ruby"});
properties.put("language.mimetypes", new String[]{"application/x-ruby"});
return properties;
}
/**
* Returns a list of load paths for Ruby scripts/libraries. If no paths is
* given, the list is created from java.class.path System property.
*
* @since JRuby 1.5.0.
*
* @return a list of load paths.
*/
public List<String> getLoadPaths() {
return provider.getRubyInstanceConfig().getLoadPaths();
}
/**
* Changes a list of load paths Ruby scripts/libraries. The default value
* is an empty array. If no paths is given, the list is created from
* java.class.path System property. This value can be set by
* org.jruby.embed.class.path System property, also.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given paths will be used.
*
* @since JRuby 1.5.0.
*
* @param paths a new list of load paths.
*/
public void setLoadPaths(List<String> paths) {
provider.getRubyInstanceConfig().setLoadPaths(paths);
}
/**
* Returns an input stream assigned to STDIN and $stdin.
*
* @since JRuby 1.5.0.
*
* @return input stream of STDIN and $stdin
*/
public InputStream getInput() {
return provider.getRubyInstanceConfig().getInput();
}
/**
* Changes STDIN and $stdin to a given input stream. The default standard input
* is java.lang.System.in.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given input stream will be used.
*
* @since JRuby 1.5.0.
*
* @param istream an input stream to be set
*/
public void setInput(InputStream istream) {
provider.getRubyInstanceConfig().setInput(istream);
}
/**
* Changes STDIN and $stdin to a given reader. No reader is set by default.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given reader will be used.
*
* @since JRuby 1.5.0.
*
* @param reader a reader to be set
*/
public void setInput(Reader reader) {
if (reader == null) {
provider.getRubyInstanceConfig().setInput(null);
} else {
ReaderInputStream istream = new ReaderInputStream(reader);
provider.getRubyInstanceConfig().setInput(istream);
}
}
/**
* Returns an output stream assigned to STDOUT and $stdout.
*
* @since JRuby 1.5.0.
*
* @return an output stream of STDOUT and $stdout
*/
public PrintStream getOutput() {
return provider.getRubyInstanceConfig().getOutput();
}
/**
* Changes STDOUT and $stdout to a given output stream. The default standard
* output is java.lang.System.out.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given output stream will be used.
*
* @since JRuby 1.5.0.
*
* @param pstream an output stream to be set
*/
public void setOutput(PrintStream pstream) {
provider.getRubyInstanceConfig().setOutput(pstream);
}
/**
* Changes STDOUT and $stdout to a given writer. No writer is set by default.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given writer will be used.
*
* @since JRuby 1.5.0.
*
* @param writer a writer to be set
*/
public void setOutput(Writer writer) {
if (writer == null) {
provider.getRubyInstanceConfig().setOutput(null);
} else {
WriterOutputStream ostream = new WriterOutputStream(writer);
PrintStream pstream = new PrintStream(ostream);
provider.getRubyInstanceConfig().setOutput(pstream);
}
}
/**
* Returns an error stream assigned to STDERR and $stderr.
*
* @since JRuby 1.5.0.
*
* @return output stream for error stream
*/
public PrintStream getError() {
return provider.getRubyInstanceConfig().getError();
}
/**
* Changes STDERR and $stderr to a given print stream. The default standard error
* is java.lang.System.err.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given print stream will be used.
*
* @since JRuby 1.5.0.
*
* @param pstream a print stream to be set
*/
public void setError(PrintStream pstream) {
provider.getRubyInstanceConfig().setError(pstream);
}
/**
* Changes STDERR and $stderr to a given writer. No writer is set by default.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given writer will be used.
*
* @since JRuby 1.5.0.
*
* @param writer a writer to be set
*/
public void setError(Writer writer) {
if (writer == null) {
provider.getRubyInstanceConfig().setError(null);
} else {
WriterOutputStream ostream = new WriterOutputStream(writer);
PrintStream pstream = new PrintStream(ostream);
provider.getRubyInstanceConfig().setError(pstream);
}
}
/**
* Returns a compile mode currently chosen, which is one of CompileMode.JIT,
* CompileMode.FORCE, CompileMode.OFF. The default mode is CompileMode.OFF
* if CompatVersion.RUBY1_9 is chosen, otherwise, CompileMode.JIT. Also,
* CompileMode.OFF is chosen when a security restriction is set.
*
* @since JRuby 1.5.0.
*
* @return a compile mode.
*/
public CompileMode getCompileMode() {
return provider.getRubyInstanceConfig().getCompileMode();
}
/**
* Changes a compile mode to a given mode, which should be one of CompileMode.JIT,
* CompileMode.FORCE, CompileMode.OFF.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given mode will be used.
*
* @since JRuby 1.5.0.
*
* @param mode compile mode
*/
public void setCompileMode(CompileMode mode) {
provider.getRubyInstanceConfig().setCompileMode(mode);
}
/**
* Tests whether Ruby runs in a process or not.
*
* @since JRuby 1.5.0.
*
* @return true if Ruby is configured to run in a process, otherwise, false.
*/
public boolean isRunRubyInProcess() {
return provider.getRubyInstanceConfig().isRunRubyInProcess();
}
/**
* Changes the value to determine whether Ruby runs in a process or not. The
* default value is true.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given condition will be set.
*
* @since JRuby 1.5.0.
*
* @param inprocess true when Ruby is set to run in the process, or false not to
* run in the process.
*/
public void setRunRubyInProcess(boolean inprocess) {
provider.getRubyInstanceConfig().setRunRubyInProcess(inprocess);
}
/**
* Tests whether the Object Space is enabled or not.
*
* @since JRuby 1.5.0.
*
* @return true if the Object Space is able to use, otherwise, false.
*/
public boolean isObjectSpaceEnabled() {
return provider.getRubyInstanceConfig().isObjectSpaceEnabled();
}
/**
* Changes the value to determine whether the Object Space is enabled or not. The
* default value is false.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given condition will be used.
*
* @since JRuby 1.5.0.
*
* This value can be set by jruby.objectspace.enabled system property.
*
* @param enable true to enable the Object Space, or false to disable.
*/
public void setObjectSpaceEnabled(boolean enable) {
provider.getRubyInstanceConfig().setObjectSpaceEnabled(enable);
}
/**
* Returns a map of environment variables.
*
* @since JRuby 1.5.0.
*
* @return a map that has environment variables' key-value pairs.
*/
public Map getEnvironment() {
return provider.getRubyInstanceConfig().getEnvironment();
}
/**
* Changes an environment variables' map.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* @since JRuby 1.5.0.
*
* @param environment a new map of environment variables.
*/
public void setEnvironment(Map environment) {
provider.getRubyInstanceConfig().setEnvironment(environment);
}
/**
* Returns a current directory.
*
* The default current directory is identical to a value of "user.dir" system
* property if no security restriction is set. If the "user.dir" directory is
* protected by the security restriction, the default value is "/".
*
* @since JRuby 1.5.0.
*
* @return a current directory.
*/
public String getCurrentDirectory() {
if (provider.isRuntimeInitialized()) {
return provider.getRuntime().getCurrentDirectory();
}
return provider.getRubyInstanceConfig().getCurrentDirectory();
}
/**
* Changes a current directory to a given directory.
* The current directory can be changed anytime.
*
* @since JRuby 1.5.0.
*
* @param directory a new directory to be set.
*/
public void setCurrentDirectory(String directory) {
if (provider.isRuntimeInitialized()) {
provider.getRuntime().setCurrentDirectory(directory);
} else {
provider.getRubyInstanceConfig().setCurrentDirectory(directory);
}
}
/**
* Returns a JRuby home directory.
*
* The default JRuby home is the value of JRUBY_HOME environment variable,
* or "jruby.home" system property when no security restriction is set to
* those directories. If none of JRUBY_HOME or jruby.home is set and jruby-complete.jar
* is used, the default JRuby home is "/META-INF/jruby.home" in the jar archive.
* Otherwise, "java.io.tmpdir" system property is the default value.
*
* @since JRuby 1.5.0.
*
* @return a JRuby home directory.
*/
public String getHomeDirectory() {
return provider.getRubyInstanceConfig().getJRubyHome();
}
/**
* Changes a JRuby home directory to a directory of a given name.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given directory will be used.
*
* @since JRuby 1.5.0.
*
* @param home a name of new JRuby home directory.
*/
public void setHomeDirectory(String home) {
provider.getRubyInstanceConfig().setJRubyHome(home);
}
/**
* Returns a class loader object that is currently used. This loader loads
* Ruby files and libraries.
*
* @since JRuby 1.5.0.
*
* @return a class loader object that is currently used.
*/
public ClassLoader getClassLoader() {
return provider.getRubyInstanceConfig().getLoader();
}
/**
* Changes a class loader to a given loader.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given class loader will be used.
*
* @since JRuby 1.5.0.
*
* @param loader a new class loader to be set.
*/
public void setClassLoader(ClassLoader loader) {
provider.getRubyInstanceConfig().setLoader(loader);
}
/**
* Returns a Profile currently used. The default value is Profile.DEFAULT,
* which has the same behavior to Profile.ALL.
* Profile allows you to define a restricted subset of code to be loaded during
* the runtime initialization. When you use JRuby in restricted environment
* such as Google App Engine, Profile is a helpful option.
*
* @since JRuby 1.5.0.
*
* @return a current profiler.
*/
public Profile getProfile() {
return provider.getRubyInstanceConfig().getProfile();
}
/**
* Changes a Profile to a given one. The default value is Profile.DEFAULT,
* which has the same behavior to Profile.ALL.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* Profile allows you to define a restricted subset of code to be loaded during
* the runtime initialization. When you use JRuby in restricted environment
* such as Google App Engine, Profile is a helpful option. For example,
* Profile.NO_FILE_CLASS doesn't load File class.
*
* @since JRuby 1.5.0.
*
* @param profile a new profiler to be set.
*/
public void setProfile(Profile profile) {
provider.getRubyInstanceConfig().setProfile(profile);
}
/**
* Returns currently configured ProfileOutput object, which determines where
* the output of profiling operations will be sent. (e.g., a file specified
* by --profile.out).
*
* @since JRuby 1.7.15
*
* @return current profiling output location.
*/
public ProfileOutput getProfileOutput() {
return provider.getRubyInstanceConfig().getProfileOutput();
}
/**
* Changes ProfileOutput to given one. The default value is a ProfileOutput
* instance that writes to stderr. Similar to passing `--profile.out` from
* the command line
*
* @since JRuby 1.7.15
*
* @param out a new ProfileOutput object, to which profiling data should be written
*/
public void setProfileOutput(ProfileOutput out) {
provider.getRubyInstanceConfig().setProfileOutput(out);
}
/**
* Returns a ProfilingMode currently used. The default value is ProfilingMode.OFF.
*
* @since JRuby 1.6.6.
*
* @return a current profiling mode.
*/
public ProfilingMode getProfilingMode() {
return provider.getRubyInstanceConfig().getProfilingMode();
}
/**
* Changes a ProfilingMode to a given one. The default value is Profiling.OFF.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* ProfilingMode allows you to change profiling style.
*
* Profiling.OFF - default. profiling off.
* Profiling.API - activates Ruby profiler API. equivalent to --profile.api command line option
* Profiling.FLAT - synonym for --profile command line option equivalent to --profile.flat command line option
* Profiling.GRAPH - runs with instrumented (timed) profiling, graph format. equivalent to --profile.graph command line option.
*
* @since JRuby 1.6.6.
*
* @deprecated Use setProfilingMode instead
*
* @param mode a new profiling mode to be set.
*/
@Deprecated
public void setProfile(ProfilingMode mode) {
provider.getRubyInstanceConfig().setProfilingMode(mode);
}
/**
* Changes a ProfilingMode to a given one. The default value is Profiling.OFF.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* ProfilingMode allows you to change profiling style.
*
* Profiling.OFF - default. profiling off.
* Profiling.API - activates Ruby profiler API. equivalent to --profile.api command line option
* Profiling.FLAT - synonym for --profile command line option equivalent to --profile.flat command line option
* Profiling.GRAPH - runs with instrumented (timed) profiling, graph format. equivalent to --profile.graph command line option.
*
* @since JRuby 1.7.15
*
* @param mode a new profiling mode to be set.
*/
public void setProfilingMode(ProfilingMode mode) {
provider.getRubyInstanceConfig().setProfilingMode(mode);
}
/**
* Returns a LoadServiceCreator currently used.
*
* @since JRuby 1.5.0.
*
* @return a current LoadServiceCreator.
*/
public LoadServiceCreator getLoadServiceCreator() {
return provider.getRubyInstanceConfig().getLoadServiceCreator();
}
/**
* Changes a LoadServiceCreator to a given one.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* @since JRuby 1.5.0.
*
* @param creator a new LoadServiceCreator
*/
public void setLoadServiceCreator(LoadServiceCreator creator) {
provider.getRubyInstanceConfig().setLoadServiceCreator(creator);
}
/**
* Returns a list of argument.
*
* @since JRuby 1.5.0.
*
* @return an arguments' list.
*/
public String[] getArgv() {
return provider.getRubyInstanceConfig().getArgv();
}
/**
* Changes values of the arguments' list.
*
* @since JRuby 1.5.0.
*
* @param argv a new arguments' list.
*/
public void setArgv(String[] argv) {
provider.getRubyInstanceConfig().setArgv(argv);
}
/**
* Returns a script filename to run. The default value is "<script>".
*
* @since JRuby 1.5.0.
*
* @return a script filename.
*/
public String getScriptFilename() {
return provider.getRubyInstanceConfig().getScriptFileName();
}
/**
* Changes a script filename to run. The default value is "<script>".
* Call this before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* @since JRuby 1.5.0.
*
* @param filename a new script filename.
*/
public void setScriptFilename(String filename) {
provider.getRubyInstanceConfig().setScriptFileName(filename);
}
/**
* Returns a record separator. The default value is "\n".
*
* @since JRuby 1.5.0.
*
* @return a record separator.
*/
public String getRecordSeparator() {
return provider.getRubyInstanceConfig().getRecordSeparator();
}
/**
* Changes a record separator to a given value. If "0" is given, the record
* separator goes to "\n\n", "777" goes to "\uFFFF", otherwise, an octal value
* of the given number.
* Call this before you use put/get, runScriptlet, and parse methods so that
* initial configurations will work.
*
* @since JRuby 1.5.0.
*
* @param separator a new record separator value, "0" or "777"
*/
public void setRecordSeparator(String separator) {
provider.getRubyInstanceConfig().setRecordSeparator(separator);
}
/**
* Returns a value of KCode currently used. The default value is KCode.NONE.
*
* @since JRuby 1.5.0.
*
* @return a KCode value.
*/
public KCode getKCode() {
return provider.getRubyInstanceConfig().getKCode();
}
/**
* Changes a value of KCode to a given value. The value should be one of
* KCode.NONE, KCode.UTF8, KCode.SJIS, or KCode.EUC. The default value is KCode.NONE.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the given value will be used.
*
* @since JRuby 1.5.0.
*
* @param kcode a new KCode value.
*/
public void setKCode(KCode kcode) {
provider.getRubyInstanceConfig().setKCode(kcode);
}
/**
* Set whether native code is enabled for this config. Disabling it also
* disables C extensions (@see RubyInstanceConfig#setCextEnabled).
*
* @see Options#NATIVE_ENABLED
*
* @param b new value indicating whether native code is enabled
*/
public void setNativeEnabled(boolean b) {
provider.getRubyInstanceConfig().setNativeEnabled(b);
}
/**
* Get whether native code is enabled for this config.
*
* @see Options#NATIVE_ENABLED
*
* @return true if native code is enabled; false otherwise.
*/
public boolean isNativeEnabled() {
return provider.getRubyInstanceConfig().isNativeEnabled();
}
/**
* Returns the value of n, which means that jitted methods are logged in
* every n methods. The default value is 0.
*
* @since JRuby 1.5.0.
*
* @return a value that determines how often jitted methods are logged.
*/
public int getJitLogEvery() {
return provider.getRubyInstanceConfig().getJitLogEvery();
}
/**
* Changes a value of n, so that jitted methods are logged in every n methods.
* The default value is 0. This value can be set by the jruby.jit.logEvery System
* property.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the configurations will work.
*
* @since JRuby 1.5.0.
*
* @param logEvery a new number of methods.
*/
public void setJitLogEvery(int logEvery) {
provider.getRubyInstanceConfig().setJitLogEvery(logEvery);
}
/**
* Returns a value of the threshold that determines whether jitted methods'
* call reached to the limit or not. The default value is -1 when security
* restriction is applied, or 50 when no security restriction exists.
*
* @since JRuby 1.5.0.
*
* @return a value of the threshold.
*/
public int getJitThreshold() {
return provider.getRubyInstanceConfig().getJitThreshold();
}
/**
* Changes a value of the threshold that determines whether jitted methods'
* call reached to the limit or not. The default value is -1 when security
* restriction is applied, or 50 when no security restriction exists. This
* value can be set by jruby.jit.threshold System property.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the configurations will work.
*
* @since JRuby 1.5.0.
*
* @param threshold a new value of the threshold.
*/
public void setJitThreshold(int threshold) {
provider.getRubyInstanceConfig().setJitThreshold(threshold);
}
/**
* Returns a value of a max class cache size. The default value is 0 when
* security restriction is applied, or 4096 when no security restriction exists.
*
* @since JRuby 1.5.0.
*
* @return a value of a max class cache size.
*/
public int getJitMax() {
return provider.getRubyInstanceConfig().getJitMax();
}
/**
* Changes a value of a max class cache size. The default value is 0 when
* security restriction is applied, or 4096 when no security restriction exists.
* This value can be set by jruby.jit.max System property.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the configurations will work.
*
* @since JRuby 1.5.0.
*
* @param max a new value of a max class cache size.
*/
public void setJitMax(int max) {
provider.getRubyInstanceConfig().setJitMax(max);
}
/**
* Returns a value of a max size of the bytecode generated by compiler. The
* default value is -1 when security restriction is applied, or 10000 when
* no security restriction exists.
*
* @since JRuby 1.5.0.
*
* @return a value of a max size of the bytecode.
*/
public int getJitMaxSize() {
return provider.getRubyInstanceConfig().getJitMaxSize();
}
/**
* Changes a value of a max size of the bytecode generated by compiler. The
* default value is -1 when security restriction is applied, or 10000 when
* no security restriction exists. This value can be set by jruby.jit.maxsize
* System property.
* Call this method before you use put/get, runScriptlet, and parse methods so that
* the configurations will work.
*
* @since JRuby 1.5.0.
*