-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy patharraycore_js.mbt
294 lines (261 loc) · 7.34 KB
/
arraycore_js.mbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//#region
// These types are of workaround for the restriction that MoonBit do not support
// generic type parameters of extern ffi
///|
priv extern type JSValue
///|
fn JSValue::ofAny[T](array : T) -> JSValue = "%identity"
///|
fn JSValue::toAny[T](self : JSValue) -> T = "%identity"
///|
priv extern type JSArray
///|
fn JSArray::ofAnyArray[T](array : Array[T]) -> JSArray = "%identity"
///|
fn JSArray::toAnyArray[T](self : JSArray) -> Array[T] = "%identity"
///|
extern "js" fn JSArray::set_length(self : JSArray, new_len : Int) -> Unit =
#| (arr, len) => { arr.length = len; }
///|
extern "js" fn JSArray::push(self : JSArray, value : JSValue) -> Unit =
#| (arr, val) => { arr.push(val); }
///|
extern "js" fn JSArray::pop(self : JSArray) -> JSValue =
#| (arr) => arr.pop()
///|
extern "js" fn JSArray::splice(
self : JSArray,
index : Int,
count : Int
) -> JSArray =
#| (arr, idx, cnt) => arr.splice(idx, cnt)
///|
extern "js" fn JSArray::splice1(
self : JSArray,
index : Int,
count : Int,
value : JSValue
) -> JSArray =
#| (arr, idx, cnt, val) => arr.splice(idx, cnt, val)
//#endregion
///|
/// An `Array` is a collection of values that supports random access and can
/// grow in size.
extern type Array[T]
///|
fn Array::make_uninit[T](len : Int) -> Array[T] = "%fixedarray.make_uninit"
///|
/// Creates a new array.
pub fn Array::new[T](capacity~ : Int = 0) -> Array[T] {
ignore(capacity)
[]
}
///|
/// Returns the number of elements in the array.
pub fn Array::length[T](self : Array[T]) -> Int = "%fixedarray.length"
///|
fn Array::unsafe_truncate_to_length[T](self : Array[T], new_len : Int) -> Unit {
JSArray::ofAnyArray(self).set_length(new_len)
}
///|
fn Array::buffer[T](self : Array[T]) -> UninitializedArray[T] = "%identity"
///|
test "array_unsafe_blit_fixed" {
let src = FixedArray::make(5, 0)
let dst = UninitializedArray::make(5)
for i in 0..<5 {
src[i] = i + 1
}
UninitializedArray::unsafe_blit_fixed(dst, 0, src, 0, 5)
for i in 0..<5 {
assert_eq!(dst[i], src[i])
}
}
///|
test "UninitializedArray::unsafe_blit_fixed" {
let src = FixedArray::make(5, 0)
let dst = UninitializedArray::make(5)
for i in 0..<5 {
src[i] = i + 1
}
UninitializedArray::unsafe_blit_fixed(dst, 0, src, 0, 5)
for i in 0..<5 {
assert_eq!(dst[i], src[i])
}
}
///|
test "UninitializedArray::unsafe_blit_fixed" {
let src = FixedArray::make(5, 0)
let dst = UninitializedArray::make(5)
for i in 0..<5 {
src[i] = i + 1
}
UninitializedArray::unsafe_blit_fixed(dst, 0, src, 0, 5)
for i in 0..<5 {
assert_eq!(dst[i], src[i])
}
}
///|
/// Reserves capacity to ensure that it can hold at least the number of elements
/// specified by the `capacity` argument.
///
/// **NOTE**: This method does nothing on js platform.
/// # Example
///
/// ```
/// let v = [1]
/// v.reserve_capacity(10)
/// assert_eq!(v.capacity(), 1)
/// ```
pub fn Array::reserve_capacity[T](self : Array[T], capacity : Int) -> Unit {
ignore(self)
ignore(capacity)
}
///|
/// Shrinks the capacity of the array as much as possible.
///
/// **NOTE**: This method does nothing on js platform.
/// # Example
///
/// ```
/// let v = Array::new(capacity=10)
/// v.push(1)
/// v.push(2)
/// v.push(3)
/// assert_eq!(v.capacity(), 3)
/// v.shrink_to_fit()
/// assert_eq!(v.capacity(), 3)
/// ```
pub fn Array::shrink_to_fit[T](self : Array[T]) -> Unit {
ignore(self)
}
///|
/// Adds an element to the end of the array.
///
/// If the array is at capacity, it will be reallocated.
///
/// # Example
/// ```
/// let v = []
/// v.push(3)
/// ```
pub fn Array::push[T](self : Array[T], value : T) -> Unit {
JSArray::ofAnyArray(self).push(JSValue::ofAny(value))
}
///|
/// Removes the last element from a array and returns it, or `None` if it is empty.
///
/// # Example
/// ```
/// let v = [1, 2, 3]
/// let vv = v.pop()
/// assert_eq!(vv, Some(3))
/// assert_eq!(v, [1, 2])
/// ```
pub fn Array::pop[T](self : Array[T]) -> T? {
if self.length() == 0 {
None
} else {
let v = self.unsafe_pop()
Some(v)
}
}
///|
#deprecated("Use `unsafe_pop` instead")
#coverage.skip
pub fn Array::pop_exn[T](self : Array[T]) -> T {
self.unsafe_pop()
}
///|
/// Removes the last element from a array and returns it.
///
/// **NOTE** This method will not cause a panic, but it may result in undefined
/// behavior on the JavaScript platform. Use with caution.
///
/// @alert unsafe "Panic if the array is empty."
pub fn Array::unsafe_pop[T](self : Array[T]) -> T {
JSArray::ofAnyArray(self).pop().toAny()
}
///|
/// Remove an element from the array at a given index.
///
/// Removes and returns the element at position index within the array, shifting all elements after it to the left.
///
/// # Example
/// ```
/// let v = [3, 4, 5]
/// let vv = v.remove(1)
/// assert_eq!(vv, 4)
/// assert_eq!(v, [3, 5])
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn Array::remove[T](self : Array[T], index : Int) -> T {
guard index >= 0 && index < self.length() else {
abort(
"index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
)
}
let value = self.buffer()[index]
let _ = JSArray::ofAnyArray(self).splice(index, 1)
value
}
///|
/// Removes the specified range from the array and returns it.
///
/// This functions returns a array range from `begin` to `end` `[begin, end)`
///
/// # Example
/// ```
/// let v = [3, 4, 5]
/// let vv = v.drain(1, 2) // vv = [4], v = [3, 5]
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn Array::drain[T](self : Array[T], begin : Int, end : Int) -> Array[T] {
guard begin >= 0 && end <= self.length() && begin <= end else {
abort(
"index out of bounds: the len is \{self.length()} but the index is (\{begin}, \{end})",
)
}
JSArray::ofAnyArray(self).splice(begin, end - begin).toAnyArray()
}
///|
/// Inserts an element at a given index within the array.
///
/// # Example
/// ```
/// [3, 4, 5].insert(1, 6)
/// ```
/// @alert unsafe "Panic if index is out of bounds."
pub fn Array::insert[T](self : Array[T], index : Int, value : T) -> Unit {
guard index >= 0 && index <= self.length() else {
abort(
"index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
)
}
let _ = JSArray::ofAnyArray(self).splice1(index, 0, JSValue::ofAny(value))
}
///|
/// Resize the array in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the array will be extended by the
/// difference, and the values in the new slots are left uninitilized.
/// If `new_len` is less than `len`, it will panic
///
/// @alert unsafe "Panic if new length is negative."
fn Array::unsafe_grow_to_length[T](self : Array[T], new_len : Int) -> Unit {
guard new_len >= self.length()
JSArray::ofAnyArray(self).set_length(new_len)
}