Skip to content

Commit 95ebf53

Browse files
committed
[GR-45623] Add Process.argv0 method
PullRequest: truffleruby/3738
2 parents b10b9c5 + c9dcb97 commit 95ebf53

File tree

9 files changed

+58
-4
lines changed

9 files changed

+58
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Compatibility:
1010

1111
* Fix `Hash#shift` when Hash is empty but has initial default value or initial default proc (@itarato).
1212
* Make `Array#shuffle` produce the same results as CRuby (@rwstauner).
13+
* Add `Process.argv0` method (@andrykonchin).
1314

1415
Performance:
1516

spec/ruby/core/process/argv0_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require_relative '../../spec_helper'
2+
3+
describe "Process.argv0" do
4+
it "returns a String" do
5+
Process.argv0.should be_kind_of(String)
6+
end
7+
8+
it "is the path given as the main script and the same as __FILE__" do
9+
script = "fixtures/argv0.rb"
10+
11+
Dir.chdir(File.dirname(__FILE__)) do
12+
ruby_exe(script).should == "#{script}\n#{script}\nOK"
13+
end
14+
end
15+
16+
it "returns a non frozen object" do
17+
Process.argv0.should_not.frozen?
18+
end
19+
20+
it "returns every time the same object" do
21+
Process.argv0.should.equal?(Process.argv0)
22+
end
23+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
puts Process.argv0
2+
puts __FILE__
3+
4+
if Process.argv0 == __FILE__
5+
print "OK"
6+
end

spec/tags/core/process/argv0_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slow:Process.argv0 is the path given as the main script and the same as __FILE__

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.truffleruby.core.objectspace.ObjectSpaceManager;
5959
import org.truffleruby.core.proc.ProcOperations;
6060
import org.truffleruby.core.proc.RubyProc;
61+
import org.truffleruby.core.string.RubyString;
6162
import org.truffleruby.core.symbol.RubySymbol;
6263
import org.truffleruby.core.thread.ThreadManager;
6364
import org.truffleruby.core.time.GetTimeZoneNode;
@@ -159,6 +160,8 @@ public class RubyContext {
159160
private final AssumedValue<Boolean> warningCategoryDeprecated;
160161
private final AssumedValue<Boolean> warningCategoryExperimental;
161162

163+
private RubyString scriptName;
164+
162165
private static final ContextReference<RubyContext> REFERENCE = ContextReference.create(RubyLanguage.class);
163166

164167
public static RubyContext get(Node node) {
@@ -760,4 +763,12 @@ public PrintStream getEnvErrStream() {
760763
public GlobalVariableStorage getGlobalVariableStorage(int index) {
761764
return globalVariablesArray.get(index);
762765
}
766+
767+
public void setScriptName(RubyString name) {
768+
this.scriptName = name;
769+
}
770+
771+
public RubyString getScriptName() {
772+
return this.scriptName;
773+
}
763774
}

src/main/java/org/truffleruby/core/ProcessNodes.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
*/
1010
package org.truffleruby.core;
1111

12+
import org.truffleruby.annotations.CoreMethod;
1213
import org.truffleruby.annotations.CoreModule;
1314
import org.truffleruby.annotations.Primitive;
15+
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1416
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
17+
import org.truffleruby.core.string.RubyString;
1518
import org.truffleruby.core.symbol.RubySymbol;
1619

1720
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -21,9 +24,19 @@
2124

2225
import java.time.Instant;
2326

24-
@CoreModule(value = "Process", isClass = true)
27+
@CoreModule(value = "Process")
2528
public abstract class ProcessNodes {
2629

30+
@CoreMethod(names = "argv0", onSingleton = true)
31+
public abstract static class Argv0Node extends CoreMethodArrayArgumentsNode {
32+
33+
@Specialization
34+
protected RubyString argv0() {
35+
return getContext().getScriptName();
36+
}
37+
38+
}
39+
2740
@Primitive(name = "process_time_nanotime")
2841
public abstract static class ProcessTimeNanoTimeNode extends PrimitiveArrayArgumentsNode {
2942

src/main/java/org/truffleruby/language/TruffleBootNodes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void setArgvGlobals() {
170170

171171
private RubySource loadMainSourceSettingDollarZero(String kind, String toExecute) {
172172
final RubySource rubySource;
173-
final Object dollarZeroValue;
173+
final RubyString dollarZeroValue;
174174
final MainLoader mainLoader = new MainLoader(getContext(), getLanguage());
175175
try {
176176
switch (kind) {
@@ -199,6 +199,7 @@ private RubySource loadMainSourceSettingDollarZero(String kind, String toExecute
199199

200200
int index = getLanguage().getGlobalVariableIndex("$0");
201201
getContext().getGlobalVariableStorage(index).setValueInternal(dollarZeroValue);
202+
getContext().setScriptName(dollarZeroValue);
202203

203204
return rubySource;
204205
}

test/mri/excludes/TestProcess.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
exclude :test_execopts_redirect_pipe, "blocks forever"
77
exclude :test_execopts_redirect_self, "blocks forever"
88
exclude :test_abort, "needs investigation"
9-
exclude :test_argv0_keep_alive, "needs investigation"
109
exclude :test_aspawn_too_long_path, "needs investigation"
1110
exclude :test_execopts_chdir, "needs investigation"
1211
exclude :test_execopts_duplex_io, "needs investigation"

test/mri/excludes/TestRubyOptions.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
exclude :test_separator, "pid 86222 exit 1"
1515
exclude :test_cwd_encoding, "pid 86223 exit 1"
1616
exclude :test_chdir, "pid 86224 exit 1"
17-
exclude :test_set_program_name, "Expected /hello world/ to match \"COMMAND\\n\" + \"(truffleruby)\\n\"."
1817
exclude :test_indentation_check, ""
1918
exclude :test_disable, "pid 86382 exit 0."
2019
exclude :test_flag_in_shebang, "pid 86383 exit 0."

0 commit comments

Comments
 (0)