Skip to content

Commit cf92d36

Browse files
committed
Turn global_variable_get and global_variable_set into primitives
1 parent d2aab69 commit cf92d36

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

src/main/java/org/truffleruby/core/kernel/TruffleKernelNodes.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.truffleruby.builtins.CoreMethod;
1919
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
2020
import org.truffleruby.builtins.CoreMethodNode;
21+
import org.truffleruby.builtins.Primitive;
2122
import org.truffleruby.core.cast.BooleanCastWithDefaultNodeGen;
2223
import org.truffleruby.core.string.StringOperations;
2324
import org.truffleruby.language.RubyNode;
@@ -104,49 +105,35 @@ private RubySource loadFile(String feature) throws IOException {
104105

105106
}
106107

107-
@CoreMethod(names = "global_variable_set", isModuleFunction = true, required = 2)
108+
// Only used internally with a constant literal name, does not trigger hooks
109+
@Primitive(name = "global_variable_set")
108110
public abstract static class WriteGlobalVariableNode extends CoreMethodArrayArgumentsNode {
111+
109112
@Specialization(guards = { "isRubySymbol(cachedName)", "name == cachedName" }, limit = "1")
110113
public Object write(DynamicObject name, Object value,
111114
@Cached("name") DynamicObject cachedName,
112115
@Cached("create(getStorage(cachedName))") WriteSimpleGlobalVariableNode writeNode) {
113116
return writeNode.execute(value);
114117
}
115118

116-
@Specialization(guards = "isRubySymbol(name)")
117-
@TruffleBoundary
118-
public Object writeGeneric(DynamicObject name, Object value,
119-
@Cached("create()") WriteBarrierNode writeBarrierNode) {
120-
GlobalVariableStorage storage = getStorage(name);
121-
if (getContext().getSharedObjects().isSharing()) {
122-
writeBarrierNode.executeWriteBarrier(value);
123-
}
124-
storage.setValueInternal(value);
125-
return value;
126-
}
127-
128119
protected GlobalVariableStorage getStorage(DynamicObject name) {
129-
return getContext().getCoreLibrary().getGlobalVariables().getStorage(Layouts.SYMBOL.getString(name));
120+
return coreLibrary().getGlobalVariables().getStorage(Layouts.SYMBOL.getString(name));
130121
}
131122
}
132123

133-
@CoreMethod(names = "global_variable_get", isModuleFunction = true, required = 1)
124+
// Only used internally with a constant literal name, does not trigger hooks
125+
@Primitive(name = "global_variable_get")
134126
public abstract static class ReadGlobalVariableNode extends CoreMethodArrayArgumentsNode {
127+
135128
@Specialization(guards = { "isRubySymbol(cachedName)", "name == cachedName" }, limit = "1")
136129
public Object read(DynamicObject name,
137130
@Cached("name") DynamicObject cachedName,
138131
@Cached("create(getStorage(cachedName))") ReadSimpleGlobalVariableNode readNode) {
139132
return readNode.execute();
140133
}
141134

142-
@TruffleBoundary
143-
@Specialization(guards = "isRubySymbol(name)", replaces = "read")
144-
public Object readGeneric(DynamicObject name) {
145-
return getStorage(name).getValue();
146-
}
147-
148135
protected GlobalVariableStorage getStorage(DynamicObject name) {
149-
return getContext().getCoreLibrary().getGlobalVariables().getStorage(Layouts.SYMBOL.getString(name));
136+
return coreLibrary().getGlobalVariables().getStorage(Layouts.SYMBOL.getString(name));
150137
}
151138
}
152139

src/main/ruby/core/process.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,11 @@ def switch
998998

999999
Truffle::KernelOperations.define_hooked_variable(
10001000
:'$0',
1001-
-> { Truffle::KernelOperations.global_variable_get(:'$0') },
1002-
-> v { v = StringValue(v)
1003-
Process.setproctitle(v)
1004-
Truffle::KernelOperations.global_variable_set(:'$0', v) })
1001+
-> { Truffle.invoke_primitive :global_variable_get, :'$0' },
1002+
-> v {
1003+
v = StringValue(v)
1004+
Process.setproctitle(v)
1005+
Truffle.invoke_primitive :global_variable_set, :'$0', v
1006+
})
10051007

10061008
alias $PROGRAM_NAME $0

src/main/ruby/core/truffle/kernel_operations.rb

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def self.define_read_only_global(name, getter)
3737
alias $" $LOADED_FEATURES
3838

3939
# The runtime needs to access these values, so we want them to be set in the variable storage.
40-
global_variable_set(:$LOAD_PATH, LOAD_PATH)
41-
global_variable_set(:$LOADED_FEATURES, LOADED_FEATURES)
40+
Truffle.invoke_primitive :global_variable_set, :$LOAD_PATH, LOAD_PATH
41+
Truffle.invoke_primitive :global_variable_set, :$LOADED_FEATURES, LOADED_FEATURES
4242

4343
define_read_only_global(:$*, -> { ARGV })
4444

@@ -48,37 +48,39 @@ def self.define_read_only_global(name, getter)
4848

4949
define_hooked_variable(
5050
:$/,
51-
-> { global_variable_get(:$/) },
52-
-> v { if v && !Truffle::Type.object_kind_of?(v, String)
53-
raise TypeError, '$/ must be a String'
54-
end
55-
global_variable_set(:$/, v) })
51+
-> { Truffle.invoke_primitive :global_variable_get, :$/ },
52+
-> v {
53+
if v && !Truffle::Type.object_kind_of?(v, String)
54+
raise TypeError, '$/ must be a String'
55+
end
56+
Truffle.invoke_primitive :global_variable_set, :$/, v
57+
})
5658

5759
$/ = "\n".freeze
5860

5961
alias $-0 $/
6062

6163
define_hooked_variable(
6264
:'$,',
63-
-> { global_variable_get(:$,) },
64-
-> v { if v && !Truffle::Type.object_kind_of?(v, String)
65-
raise TypeError, '$, must be a String'
66-
end
67-
global_variable_set(:$,, v) })
65+
-> { Truffle.invoke_primitive :global_variable_get, :$, },
66+
-> v {
67+
if v && !Truffle::Type.object_kind_of?(v, String)
68+
raise TypeError, '$, must be a String'
69+
end
70+
Truffle.invoke_primitive :global_variable_set, :$,, v
71+
})
6872

6973
$, = nil # It should be defined by the time boot has finished.
7074

7175
$= = false
7276

7377
define_hooked_variable(
7478
:$VERBOSE,
75-
-> { global_variable_get(:$VERBOSE) },
76-
-> v { v = if v.nil?
77-
nil
78-
else
79-
!!v
80-
end
81-
global_variable_set(:$VERBOSE, v) })
79+
-> { Truffle.invoke_primitive :global_variable_get, :$VERBOSE },
80+
-> v {
81+
v = v.nil? ? nil : !!v
82+
Truffle.invoke_primitive :global_variable_set, :$VERBOSE, v
83+
})
8284

8385
Truffle::Boot.redo do
8486
$DEBUG = Truffle::Boot.get_option 'debug'
@@ -98,17 +100,21 @@ def self.define_read_only_global(name, getter)
98100

99101
define_hooked_variable(
100102
:$stdout,
101-
-> { global_variable_get(:$stdout) },
102-
-> v { raise TypeError, "$stdout must have a write method, #{v.class} given" unless v.respond_to?(:write)
103-
global_variable_set(:$stdout, v) })
103+
-> { Truffle.invoke_primitive :global_variable_get, :$stdout },
104+
-> v {
105+
raise TypeError, "$stdout must have a write method, #{v.class} given" unless v.respond_to?(:write)
106+
Truffle.invoke_primitive :global_variable_set, :$stdout, v
107+
})
104108

105109
alias $> $stdout
106110

107111
define_hooked_variable(
108112
:$stderr,
109-
-> { global_variable_get(:$stderr) },
110-
-> v { raise TypeError, "$stderr must have a write method, #{v.class} given" unless v.respond_to?(:write)
111-
global_variable_set(:$stderr, v) })
113+
-> { Truffle.invoke_primitive :global_variable_get, :$stderr },
114+
-> v {
115+
raise TypeError, "$stderr must have a write method, #{v.class} given" unless v.respond_to?(:write)
116+
Truffle.invoke_primitive :global_variable_set, :$stderr, v
117+
})
112118

113119
def self.internal_raise(exc, msg, ctx, internal)
114120
skip = false

0 commit comments

Comments
 (0)