Skip to content

Commit

Permalink
[Truffle] Implement alias of global variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Nov 8, 2016
1 parent 4d4a170 commit fd5b2f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/

package org.jruby.truffle.language.globals;

import org.jruby.truffle.language.RubyNode;

import com.oracle.truffle.api.frame.VirtualFrame;

public class AliasGlobalVarNode extends RubyNode {

private final String oldName;
private final String newName;

public AliasGlobalVarNode(String oldName, String newName) {
this.oldName = oldName;
this.newName = newName;
}

@Override
public Object execute(VirtualFrame frame) {
GlobalVariables globalVariables = getContext().getCoreLibrary().getGlobalVariables();
GlobalVariableStorage storage = globalVariables.getStorage(oldName);
globalVariables.alias(newName, storage);
return nil();
}

}
Expand Up @@ -47,14 +47,17 @@ public GlobalVariableStorage put(String key, Object value) {
return storage;
}

@TruffleBoundary
public void alias(String name, GlobalVariableStorage storage) {
variables.put(name, storage);
}

@TruffleBoundary
public Collection<String> keys() {
return variables.keySet();
}

@TruffleBoundary
public Collection<DynamicObject> dynamicObjectValues() {
final Collection<GlobalVariableStorage> storages = variables.values();
final ArrayList<DynamicObject> values = new ArrayList<>(storages.size());
Expand Down
Expand Up @@ -101,6 +101,7 @@
import org.jruby.truffle.language.exceptions.RescueNode;
import org.jruby.truffle.language.exceptions.RescueSplatNode;
import org.jruby.truffle.language.exceptions.TryNode;
import org.jruby.truffle.language.globals.AliasGlobalVarNode;
import org.jruby.truffle.language.globals.CheckMatchVariableTypeNode;
import org.jruby.truffle.language.globals.CheckOutputSeparatorVariableTypeNode;
import org.jruby.truffle.language.globals.CheckProgramNameVariableTypeNode;
Expand Down Expand Up @@ -250,6 +251,7 @@
import org.jruby.truffle.parser.ast.TruffleFragmentParseNode;
import org.jruby.truffle.parser.ast.UndefParseNode;
import org.jruby.truffle.parser.ast.UntilParseNode;
import org.jruby.truffle.parser.ast.VAliasParseNode;
import org.jruby.truffle.parser.ast.VCallParseNode;
import org.jruby.truffle.parser.ast.WhenParseNode;
import org.jruby.truffle.parser.ast.WhileParseNode;
Expand Down Expand Up @@ -337,6 +339,15 @@ public RubyNode visitAliasNode(AliasParseNode node) {
return addNewlineIfNeeded(node, ret);
}

@Override
public RubyNode visitVAliasNode(VAliasParseNode node) {
final RubySourceSection sourceSection = translate(node.getPosition());
final RubyNode ret = new AliasGlobalVarNode(node.getOldName(), node.getNewName());

ret.unsafeSetSourceSection(sourceSection);
return addNewlineIfNeeded(node, ret);
}

@Override
public RubyNode visitAndNode(AndParseNode node) {
final RubySourceSection sourceSection = translate(node.getPosition());
Expand Down Expand Up @@ -3209,7 +3220,7 @@ protected RubyNode initFlipFlopStates(RubySourceSection sourceSection) {

@Override
protected RubyNode defaultVisit(ParseNode node) {
throw new UnsupportedOperationException(node.toString());
throw new UnsupportedOperationException(node.toString() + " " + node.getPosition());
}

public TranslatorEnvironment getEnvironment() {
Expand Down

2 comments on commit fd5b2f1

@bjfish
Copy link
Contributor

@bjfish bjfish commented on fd5b2f1 Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe some of English.rb will work now. However the aliasing might conflict with the aliasing in the java code. This is one aliasing related issue #3051

@bjfish
Copy link
Contributor

@bjfish bjfish commented on fd5b2f1 Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the MRI tests!

Please sign in to comment.