Skip to content

Commit

Permalink
[Truffle] Add Kernel.global_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Sep 24, 2016
1 parent 37852c0 commit f85827b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/kernel/global_variables_tags.txt

This file was deleted.

Expand Up @@ -48,6 +48,8 @@
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.CoreMethodNode;
import org.jruby.truffle.builtins.Primitive;
import org.jruby.truffle.builtins.PrimitiveArrayArgumentsNode;
import org.jruby.truffle.builtins.UnaryCoreMethodNode;
import org.jruby.truffle.core.ObjectNodes;
import org.jruby.truffle.core.ObjectNodesFactory;
Expand Down Expand Up @@ -148,6 +150,7 @@
import java.lang.ProcessBuilder.Redirect;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

@CoreClass("Kernel")
Expand Down Expand Up @@ -1777,6 +1780,23 @@ protected CallTarget compileFormat(DynamicObject format) {

}

@Primitive(name = "kernel_global_variables")
public abstract static class KernelGlobalVariablesPrimitiveNode extends PrimitiveArrayArgumentsNode {

@Specialization
public DynamicObject globalVariables() {
final Collection<String> keys = coreLibrary().getGlobalVariables().dynamicObjectKeys();
final Object[] store = new Object[keys.size()];
int i = 0;
for (String key : keys) {
store[i] = getSymbol(key);
i++;
}
return createArray(store, store.length);
}

}

@CoreMethod(names = "taint")
public abstract static class KernelTaintNode extends CoreMethodArrayArgumentsNode {

Expand Down
Expand Up @@ -60,6 +60,10 @@ public void alias(String name, GlobalVariableStorage storage) {
variables.put(name, storage);
}

public Collection<String> dynamicObjectKeys() {

This comment has been minimized.

Copy link
@eregon

eregon Sep 26, 2016

Member

The name below was due to returning only DynamicObject.
I think this should be keys() or keySet() (like in Map).

This comment has been minimized.

Copy link
@bjfish

bjfish Sep 26, 2016

Contributor

Renamed this at: 477bcf3

return variables.keySet();
}

public Collection<DynamicObject> dynamicObjectValues() {
final Collection<GlobalVariableStorage> storages = variables.values();
final ArrayList<DynamicObject> values = new ArrayList<>(storages.size());
Expand Down
7 changes: 7 additions & 0 deletions truffle/src/main/ruby/core/kernel.rb
Expand Up @@ -666,4 +666,11 @@ def at_exit(&block)
Truffle::Kernel.at_exit false, &block
end
module_function :at_exit

def global_variables
Truffle.primitive :kernel_global_variables
raise PrimitiveFailure, "Kernel.global_variables primitive failed"
end
module_function :global_variables

end

0 comments on commit f85827b

Please sign in to comment.