Skip to content

Commit aedda07

Browse files
committed
Fixes for clojure-test-suite
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent 3b7987f commit aedda07

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

pkg/lang/catch.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,33 @@ func CatchMatches(r, expect any) bool {
1616
return false
1717
}
1818

19+
expectType := expect.(reflect.Type)
20+
1921
// if expect is an error type, check if r is an instance of it
2022
if rErr, ok := r.(error); ok {
21-
if expectTyp, ok := expect.(reflect.Type); ok && expectTyp.Implements(errorType) {
22-
expectVal := reflect.New(expectTyp).Elem().Interface().(error)
23-
if errors.Is(rErr, expectVal) {
23+
if expectType.Implements(errorType) {
24+
// if expectType is a pointer type, instantiate a new value of that type
25+
// and check if rErr is an instance of it
26+
if expectType.Kind() == reflect.Ptr {
27+
expectVal := reflect.New(expectType.Elem()).Interface()
28+
if errors.As(rErr, expectVal) {
29+
return true
30+
}
31+
}
32+
// if expectType is an interface type, check if rErr implements it
33+
if expectType.Kind() == reflect.Interface {
34+
if reflect.TypeOf(rErr).Implements(expectType) {
35+
return true
36+
}
37+
}
38+
// otherwise, create a new value of the expectType and check if
39+
// rErr is an instance of it
40+
expectVal := reflect.New(expectType).Interface()
41+
if errors.As(rErr, expectVal) {
2442
return true
2543
}
2644
}
2745
}
2846

29-
return reflect.TypeOf(r).AssignableTo(expect.(reflect.Type))
47+
return reflect.TypeOf(r).AssignableTo(expectType)
3048
}

pkg/lang/equal.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ func Equals(a, b any) bool {
7070
}
7171

7272
func Identical(a, b any) bool {
73+
aVal, bVal := reflect.ValueOf(a), reflect.ValueOf(b)
74+
75+
// check if comparing functions, because == panics on func comparison.
76+
if aVal.Kind() == reflect.Func || bVal.Kind() == reflect.Func {
77+
if !(aVal.Kind() == reflect.Func && bVal.Kind() == reflect.Func) {
78+
return false
79+
}
80+
return aVal.Pointer() == bVal.Pointer()
81+
}
82+
// slices
83+
if aVal.Kind() == reflect.Slice || bVal.Kind() == reflect.Slice {
84+
if !(aVal.Kind() == reflect.Slice && bVal.Kind() == reflect.Slice) {
85+
return false
86+
}
87+
return aVal.Pointer() == bVal.Pointer()
88+
}
89+
// arrays
90+
if aVal.Kind() == reflect.Array || bVal.Kind() == reflect.Array {
91+
if !(aVal.Kind() == reflect.Array && bVal.Kind() == reflect.Array) {
92+
return false
93+
}
94+
return aVal.Pointer() == bVal.Pointer()
95+
}
96+
// maps
97+
if aVal.Kind() == reflect.Map || bVal.Kind() == reflect.Map {
98+
if !(aVal.Kind() == reflect.Map && bVal.Kind() == reflect.Map) {
99+
return false
100+
}
101+
return aVal.Pointer() == bVal.Pointer()
102+
}
103+
73104
return a == b
74105
}
75106

pkg/lang/slices.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ import (
77

88
func SliceSet(slc any, idx int, val any) {
99
slcVal := reflect.ValueOf(slc)
10-
slcVal.Index(idx).Set(reflect.ValueOf(val))
10+
valVal := reflect.ValueOf(val)
11+
// coerce valVal to the element type of slcVal
12+
if valVal.Type() != slcVal.Type().Elem() {
13+
if valVal.Type().ConvertibleTo(slcVal.Type().Elem()) {
14+
valVal = valVal.Convert(slcVal.Type().Elem())
15+
} else {
16+
panic(NewIllegalArgumentError(fmt.Sprintf("Cannot convert %T to %s", val, slcVal.Type().Elem().String())))
17+
}
18+
}
19+
slcVal.Index(idx).Set(valVal)
1120
}
1221

1322
func ToSlice(x any) []any {

pkg/runtime/rtcompat.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,39 @@ func (rt *RTMethods) Alength(x any) int {
261261
panic(fmt.Errorf("Alength not supported on type: %T", x))
262262
}
263263

264+
func (rt *RTMethods) Aclone(x any) any {
265+
xVal := reflect.ValueOf(x)
266+
switch xVal.Kind() {
267+
case reflect.Slice:
268+
cloned := reflect.MakeSlice(xVal.Type(), xVal.Len(), xVal.Cap())
269+
reflect.Copy(cloned, xVal)
270+
return cloned.Interface()
271+
case reflect.Array:
272+
cloned := reflect.New(xVal.Type()).Elem()
273+
reflect.Copy(cloned, xVal)
274+
return cloned.Interface()
275+
}
276+
panic(fmt.Errorf("Aclone not supported on type: %T", x))
277+
}
278+
264279
func (rt *RTMethods) ToArray(coll any) any {
265280
return lang.ToSlice(coll)
266281
}
267282

283+
// ObjectArray creates an array of objects ([]any)
284+
func (rt *RTMethods) ObjectArray(sizeOrSeq any) any {
285+
if lang.IsNumber(sizeOrSeq) {
286+
return make([]any, lang.MustAsInt(sizeOrSeq))
287+
}
288+
s := lang.Seq(sizeOrSeq)
289+
size := lang.Count(s)
290+
ret := make([]any, size)
291+
for i := 0; i < len(ret) && s != nil; i, s = i+1, s.Next() {
292+
ret[i] = s.First()
293+
}
294+
return ret
295+
}
296+
268297
var (
269298
mungeCharMap = map[rune]string{
270299
'-': "_",

pkg/stdlib/clojure/core.glj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5350,10 +5350,10 @@
53505350

53515351
(defn object-array
53525352
"Creates an array of objects"
5353-
{:inline (fn [arg] `(. github.com:glojurelang:glojure:pkg:runtime.RT Object_array ~arg))
5353+
{:inline (fn [arg] `(. github.com:glojurelang:glojure:pkg:runtime.RT ObjectArray ~arg))
53545354
:inline-arities #{1}
53555355
:added "1.2"}
5356-
([size-or-seq] (. github.com:glojurelang:glojure:pkg:runtime.RT Object_array size-or-seq)))
5356+
([size-or-seq] (. github.com:glojurelang:glojure:pkg:runtime.RT ObjectArray size-or-seq)))
53575357

53585358
(defn int-array
53595359
"Creates an array of ints"

pkg/stdlib/clojure/core/loader.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/rewrite-core/rewrite.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@
774774
(sexpr-replace 'clojure.lang.RT 'github.com:glojurelang:glojure:pkg:runtime.RT)
775775
(sexpr-replace '(nextID) '(NextID))
776776

777+
;; Replace Object_array with ObjectArray
778+
(sexpr-replace 'Object_array 'ObjectArray)
779+
777780
(sexpr-replace '(nth coll index not-found) '(NthDefault coll index not-found))
778781

779782
[(fn select [zloc] (and (z/list? zloc)

0 commit comments

Comments
 (0)