Skip to content

Commit 6b35ced

Browse files
committed
Implement the full semantics for aliasing global variables
* Invalidate an Assumption when a GlobalVariableStorage becomes stale as it the variable gets aliased to another.
1 parent 5c54d4a commit 6b35ced

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,6 @@ public DynamicObject zeroDivisionError(Node currentNode, ArithmeticException exc
698698
return ExceptionOperations.createRubyException(context, exceptionClass, errorMessage, currentNode, exception);
699699
}
700700

701-
// NotImplementedError
702-
703-
@TruffleBoundary
704-
public DynamicObject notImplementedError(String message, Node currentNode) {
705-
DynamicObject exceptionClass = context.getCoreLibrary().getNotImplementedErrorClass();
706-
DynamicObject errorMessage = StringOperations.createString(context, StringOperations.encodeRope(message, UTF8Encoding.INSTANCE));
707-
return ExceptionOperations.createRubyException(context, exceptionClass, errorMessage, currentNode, null);
708-
}
709-
710701
// SyntaxError
711702

712703
@TruffleBoundary

src/main/java/org/truffleruby/language/globals/AliasGlobalVarNode.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,8 @@ public AliasGlobalVarNode(String oldName, String newName) {
2626

2727
@Override
2828
public Object execute(VirtualFrame frame) {
29-
checkExisting();
3029
getContext().getCoreLibrary().getGlobalVariables().alias(oldName, newName);
3130
return nil();
3231
}
3332

34-
@TruffleBoundary
35-
private void checkExisting() {
36-
if (getContext().getCoreLibrary().getGlobalVariables().contains(newName)) {
37-
// TODO CS 4-Apr-18 This exception is non-standard
38-
throw new RaiseException(getContext(), coreExceptions().notImplementedError(String.format("%s is already a global", newName), this), true);
39-
}
40-
}
41-
4233
}

src/main/java/org/truffleruby/language/globals/GlobalVariables.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public GlobalVariables(DynamicObject defaultValue) {
3030
this.defaultValue = defaultValue;
3131
}
3232

33-
public String getOriginalName(String name) {
33+
private String getOriginalName(String name) {
3434
return aliases.getOrDefault(name, name);
3535
}
3636

@@ -65,19 +65,22 @@ public GlobalVariableStorage put(String name, DynamicObject getter, DynamicObjec
6565
return storage;
6666
}
6767

68-
@TruffleBoundary
69-
public boolean contains(String name) {
70-
return variables.containsKey(name);
71-
}
72-
7368
@TruffleBoundary
7469
public void alias(String oldName, String newName) {
75-
// TODO
7670
// Record an alias of an alias against the original.
77-
oldName = aliases.getOrDefault(oldName, oldName);
71+
oldName = getOriginalName(oldName);
7872
aliases.put(newName, oldName);
73+
7974
final GlobalVariableStorage storage = getStorage(oldName);
80-
variables.put(newName, storage);
75+
76+
GlobalVariableStorage previousStorage;
77+
do {
78+
previousStorage = variables.get(newName);
79+
} while (!ConcurrentOperations.replace(variables, newName, previousStorage, storage));
80+
81+
if (previousStorage != null) {
82+
previousStorage.getValidAssumption().invalidate();
83+
}
8184
}
8285

8386
@TruffleBoundary

0 commit comments

Comments
 (0)