@@ -6,7 +6,7 @@ module bytearray
6
6
extern type ByteArray
7
7
// = llvm "%Pos"
8
8
// = js "Uint8Array"
9
-
9
+ // = chez "bytevector"
10
10
11
11
/// Allocates a new bytearray with the given `size`, its values are undefined.
12
12
extern global def allocate(size: Int): ByteArray =
@@ -15,27 +15,31 @@ extern global def allocate(size: Int): ByteArray =
15
15
%arr = call %Pos @c_bytearray_new(%Int ${size})
16
16
ret %Pos %arr
17
17
"""
18
+ chez "(make-bytevector ${size})"
18
19
19
20
extern pure def size(arr: ByteArray): Int =
20
21
js "${arr}.length"
21
22
llvm """
22
23
%size = call %Int @c_bytearray_size(%Pos ${arr})
23
24
ret %Int %size
24
25
"""
26
+ chez "(bytevector-length ${arr})"
25
27
26
28
extern global def unsafeGet(arr: ByteArray, index: Int): Byte =
27
29
js "(${arr})[${index}]"
28
30
llvm """
29
31
%byte = call %Byte @c_bytearray_get(%Pos ${arr}, %Int ${index})
30
32
ret %Byte %byte
31
33
"""
34
+ chez "(bytevector-u8-ref ${arr} ${index})"
32
35
33
36
extern global def unsafeSet(arr: ByteArray, index: Int, value: Byte): Unit =
34
37
js "bytearray$set(${arr}, ${index}, ${value})"
35
38
llvm """
36
39
%z = call %Pos @c_bytearray_set(%Pos ${arr}, %Int ${index}, %Byte ${value})
37
40
ret %Pos %z
38
41
"""
42
+ chez "(bytevector-u8-set! ${arr} ${index} ${value})"
39
43
40
44
def resize(source: ByteArray, size: Int): ByteArray = {
41
45
val target = allocate(size)
@@ -75,13 +79,15 @@ extern pure def fromString(str: String): ByteArray =
75
79
llvm """
76
80
ret %Pos ${str}
77
81
"""
82
+ chez "(string->utf8 ${str})"
78
83
79
84
extern pure def toString(arr: ByteArray): String =
80
85
js "(new TextDecoder('utf-8').decode(${arr}))"
81
86
// assuming the buffer is already in UTF-8
82
87
llvm """
83
88
ret %Pos ${arr}
84
89
"""
90
+ chez "(utf8->string ${arr})"
85
91
86
92
extern js """
87
93
function bytearray$set(bytes, index, value) {
@@ -106,12 +112,29 @@ extern js """
106
112
}
107
113
"""
108
114
115
+ extern chez """
116
+ (define (bytearray$compare b1 b2)
117
+ (let ([len1 (bytevector-length b1)]
118
+ [len2 (bytevector-length b2)]
119
+ [minlen (min (bytevector-length b1) (bytevector-length b2))])
120
+ (let loop ([i 0])
121
+ (cond
122
+ [(= i minlen)
123
+ (cond [(< len1 len2) -1]
124
+ [(> len1 len2) 1]
125
+ [else 0])]
126
+ [(< (bytevector-u8-ref b1 i) (bytevector-u8-ref b2 i)) -1]
127
+ [(> (bytevector-u8-ref b1 i) (bytevector-u8-ref b2 i)) 1]
128
+ [else (loop (+ i 1))]))))
129
+ """
130
+
109
131
extern pure def compareByteArrayImpl(b1: ByteArray, b2: ByteArray): Int =
110
132
js "bytearray$compare(${b1}, ${b2})"
111
133
llvm """
112
134
%x = call %Int @c_bytearray_compare(%Pos ${b1}, %Pos ${b2})
113
135
ret %Int %x
114
136
"""
137
+ chez "(bytearray$compare ${b1} ${b2})"
115
138
116
139
def compareByteArray(b1: ByteArray, b2: ByteArray): Ordering =
117
140
compareByteArrayImpl(b1, b2) match {
0 commit comments