Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,6 +64,8 @@ protected static void loadModule(URI uri, VmTyped instance) {
// (stdlib module objects are statically shared singletons when running on JVM)
// and ensure compile-time evaluation in AOT mode
instance.force(false, true);
//noinspection ResultOfMethodCallIgnored
instance.hashCode();
})
.close();
}
Expand Down
1 change: 0 additions & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,6 @@ public boolean equals(@Nullable Object obj) {

@Override
public int hashCode() {
// use a more deterministic hash code than System.identityHashCode()
return classInfo.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
11 changes: 9 additions & 2 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,7 +116,11 @@ public boolean equals(Object obj) {
var value = cursor.getValue();
assert value != null;
var otherValue = other.getCachedValue(key);
if (!value.equals(otherValue)) return false;
if (value == this) {
if (otherValue != other) return false;
} else {
if (!value.equals(otherValue)) return false;
}
}

return true;
Expand All @@ -128,6 +132,9 @@ public int hashCode() {
if (cachedHash != 0) return cachedHash;

force(false);
// Seed the cache s.t. we short-circuit when coming back to hash the same value.
// The cached hash will be updated again with the final hash code value.
cachedHash = -1;
var result = 0;
var cursor = cachedValues.getEntries();

Expand Down
2 changes: 1 addition & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmIntSeq.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
14 changes: 11 additions & 3 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -443,9 +443,17 @@ public boolean equals(@Nullable Object other) {
}

@Override
@TruffleBoundary
public int hashCode() {
return rrbt.hashCode();
int ret = 1;

for (Object item : rrbt) {
ret *= 31;
if (item != null) {
ret += item.hashCode();
}
}

return ret;
}

private static final class Builder implements VmCollection.Builder<VmList> {
Expand Down
5 changes: 4 additions & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmListing.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,6 +141,9 @@ public int hashCode() {
if (cachedHash != 0) return cachedHash;

force(false);
// Seed the cache s.t. we short-circuit when coming back to hash the same value.
// The cached hash will be updated again with the final hash code value.
cachedHash = -1;
var result = 0;
var cursor = cachedValues.getEntries();

Expand Down
11 changes: 8 additions & 3 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -279,9 +279,14 @@ public boolean equals(@Nullable Object other) {
}

@Override
@TruffleBoundary
public int hashCode() {
return map.hashCode();
var result = 0;
for (var entry : map) {
var key = entry.getKey();
var value = entry.getValue();
result += key.hashCode() ^ value.hashCode();
}
return result;
}

@TruffleBoundary
Expand Down
6 changes: 4 additions & 2 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmMapping.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,11 +141,13 @@ public boolean equals(Object obj) {
}

@Override
@TruffleBoundary
public int hashCode() {
if (cachedHash != 0) return cachedHash;

force(false);
// Seed the cache s.t. we short-circuit when coming back to hash the same value.
// The cached hash will be updated again with the final hash code value.
cachedHash = -1;
var result = 0;
var cursor = cachedValues.getEntries();

Expand Down
2 changes: 1 addition & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmNull.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
6 changes: 6 additions & 0 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public final boolean iterateMembers(BiFunction<Object, ObjectMember, Boolean> co
}
return true;
}

protected boolean doEquals(VmObject other, Set<VmValue> seenValues) {
if (seenValues.contains(this)) {

}
}

/** Evaluates this object's members. Skips local, hidden, and external members. */
@Override
Expand Down
2 changes: 1 addition & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmPair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmRegex.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
14 changes: 11 additions & 3 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmSet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -343,9 +343,17 @@ public boolean equals(@Nullable Object other) {
}

@Override
@TruffleBoundary
public int hashCode() {
return set.hashCode();
int ret = 1;

for (Object item : set) {
ret *= 31;
if (item != null) {
ret += item.hashCode();
}
}

return ret;
}

private static final class Builder implements VmCollection.Builder<VmSet> {
Expand Down
8 changes: 5 additions & 3 deletions pkl-core/src/main/java/org/pkl/core/runtime/VmTyped.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -195,11 +195,13 @@ public boolean equals(@Nullable Object obj) {
}

@Override
@TruffleBoundary
public int hashCode() {
if (cachedHash != 0) return cachedHash;

// Seed the cache s.t. we short-circuit when coming back to hash the same value.
// The cached hash will be updated again with the final hash code value.
force(false);
cachedHash = -1;

var result = 0;

for (var key : clazz.getAllRegularPropertyNames()) {
Expand Down
2 changes: 1 addition & 1 deletion pkl-core/src/main/java/org/pkl/core/runtime/VmValue.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ facts {
set.add(listing2).contains(listing1)
set.add(listing1).add(listing2).length == 101
}

["can compute hash code of cyclical objects"] {
local myself1: Listing = new { myself1 }
local myself2: Listing = new { Set(myself2) }
local myself3: Listing = new { List(myself3) }
local myself4: Listing = new { Pair(1, myself4) }
local myself5: Listing = new { Map(1, myself5) }

set.add(myself1).contains(myself1)
set.add(myself2).contains(myself2)
set.add(myself3).contains(myself3)
set.add(myself4).contains(myself4)
set.add(myself5).contains(myself5)
}
}

examples {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ facts {
set.add(mapping2).contains(mapping1)
set.add(mapping1).add(mapping2).length == 101
}

["can compute hash code of cyclical objects"] {
local myself1 = new Mapping { ["res"] = myself1 }
local myself2 = new Mapping { ["res"] = Set(myself2) }
local myself3 = new Mapping { ["res"] = List(myself3) }
local myself4 = new Mapping { ["res"] = Pair(1, myself4) }
local myself5 = new Mapping { ["res"] = Map(1, myself5) }

set.add(myself1).contains(myself1)
set.add(myself2).contains(myself2)
set.add(myself3).contains(myself3)
set.add(myself4).contains(myself4)
set.add(myself5).contains(myself5)
}
}

examples {
Expand Down
Loading