1
1
import JavaScriptKit
2
2
3
- test ( " Literal Conversion " ) {
4
- let global = JSObjectRef . global
3
+ try test ( " Literal Conversion " ) {
4
+ let global = JSObject . global
5
5
let inputs : [ JSValue ] = [
6
6
. boolean( true ) ,
7
7
. boolean( false ) ,
@@ -29,7 +29,7 @@ test("Literal Conversion") {
29
29
}
30
30
}
31
31
32
- test ( " Object Conversion " ) {
32
+ try test ( " Object Conversion " ) {
33
33
// Notes: globalObject1 is defined in JavaScript environment
34
34
//
35
35
// ```js
@@ -60,7 +60,7 @@ test("Object Conversion") {
60
60
let prop_4 = getJSValue ( this: globalObject1Ref, name: " prop_4 " )
61
61
let prop_4Array = try expectObject ( prop_4)
62
62
let expectedProp_4 : [ JSValue ] = [
63
- . number( 3 ) , . number( 4 ) , . string( " str_elm_1 " ) , . number( 5 ) ,
63
+ . number( 3 ) , . number( 4 ) , . string( " str_elm_1 " ) , . null , . undefined , . number( 5 ) ,
64
64
]
65
65
for (index, expectedElement) in expectedProp_4. enumerated ( ) {
66
66
let actualElement = getJSValue ( this: prop_4Array, index: Int32 ( index) )
@@ -70,7 +70,7 @@ test("Object Conversion") {
70
70
try expectEqual ( getJSValue ( this: globalObject1Ref, name: " undefined_prop " ) , . undefined)
71
71
}
72
72
73
- test ( " Value Construction " ) {
73
+ try test ( " Value Construction " ) {
74
74
let globalObject1 = getJSValue ( this: . global, name: " globalObject1 " )
75
75
let globalObject1Ref = try expectObject ( globalObject1)
76
76
let prop_2 = getJSValue ( this: globalObject1Ref, name: " prop_2 " )
@@ -82,29 +82,43 @@ test("Value Construction") {
82
82
try expectEqual ( Float . construct ( from: prop_7) , 3.14 )
83
83
}
84
84
85
- test ( " Array Iterator " ) {
85
+ try test ( " Array Iterator " ) {
86
86
let globalObject1 = getJSValue ( this: . global, name: " globalObject1 " )
87
87
let globalObject1Ref = try expectObject ( globalObject1)
88
88
let prop_4 = getJSValue ( this: globalObject1Ref, name: " prop_4 " )
89
- let array = try expectArray ( prop_4)
89
+ let array1 = try expectArray ( prop_4)
90
90
let expectedProp_4 : [ JSValue ] = [
91
- . number( 3 ) , . number( 4 ) , . string( " str_elm_1 " ) , . number( 5 ) ,
91
+ . number( 3 ) , . number( 4 ) , . string( " str_elm_1 " ) , . null , . undefined , . number( 5 ) ,
92
92
]
93
- try expectEqual ( Array ( array) , expectedProp_4)
93
+ try expectEqual ( Array ( array1) , expectedProp_4)
94
+
95
+ // Ensure that iterator skips empty hole as JavaScript does.
96
+ let prop_8 = getJSValue ( this: globalObject1Ref, name: " prop_8 " )
97
+ let array2 = try expectArray ( prop_8)
98
+ let expectedProp_8 : [ JSValue ] = [ 0 , 2 , 3 , 6 ]
99
+ try expectEqual ( Array ( array2) , expectedProp_8)
94
100
}
95
101
96
- test ( " Array RandomAccessCollection " ) {
102
+ try test ( " Array RandomAccessCollection " ) {
97
103
let globalObject1 = getJSValue ( this: . global, name: " globalObject1 " )
98
104
let globalObject1Ref = try expectObject ( globalObject1)
99
105
let prop_4 = getJSValue ( this: globalObject1Ref, name: " prop_4 " )
100
- let array = try expectArray ( prop_4)
106
+ let array1 = try expectArray ( prop_4)
101
107
let expectedProp_4 : [ JSValue ] = [
102
- . number( 3 ) , . number( 4 ) , . string( " str_elm_1 " ) , . number( 5 ) ,
108
+ . number( 3 ) , . number( 4 ) , . string( " str_elm_1 " ) , . null, . undefined, . number( 5 ) ,
109
+ ]
110
+ try expectEqual ( [ array1 [ 0 ] , array1 [ 1 ] , array1 [ 2 ] , array1 [ 3 ] , array1 [ 4 ] , array1 [ 5 ] ] , expectedProp_4)
111
+
112
+ // Ensure that subscript can access empty hole
113
+ let prop_8 = getJSValue ( this: globalObject1Ref, name: " prop_8 " )
114
+ let array2 = try expectArray ( prop_8)
115
+ let expectedProp_8 : [ JSValue ] = [
116
+ 0 , . undefined, 2 , 3 , . undefined, . undefined, 6
103
117
]
104
- try expectEqual ( [ array [ 0 ] , array [ 1 ] , array [ 2 ] , array [ 3 ] ] , expectedProp_4 )
118
+ try expectEqual ( [ array2 [ 0 ] , array2 [ 1 ] , array2 [ 2 ] , array2 [ 3 ] , array2 [ 4 ] , array2 [ 5 ] , array2 [ 6 ] ] , expectedProp_8 )
105
119
}
106
120
107
- test ( " Value Decoder " ) {
121
+ try test ( " Value Decoder " ) {
108
122
struct GlobalObject1 : Codable {
109
123
struct Prop1 : Codable {
110
124
let nested_prop : Int
@@ -124,7 +138,7 @@ test("Value Decoder") {
124
138
try expectEqual ( globalObject1. prop_7, 3.14 )
125
139
}
126
140
127
- test ( " Function Call " ) {
141
+ try test ( " Function Call " ) {
128
142
// Notes: globalObject1 is defined in JavaScript environment
129
143
//
130
144
// ```js
@@ -168,7 +182,7 @@ test("Function Call") {
168
182
try expectEqual ( func6 ( true , " OK " , 2 ) , . string( " OK " ) )
169
183
}
170
184
171
- test ( " Host Function Registration " ) {
185
+ try test ( " Host Function Registration " ) {
172
186
// ```js
173
187
// global.globalObject1 = {
174
188
// ...
@@ -213,7 +227,7 @@ test("Host Function Registration") {
213
227
hostFunc2. release ( )
214
228
}
215
229
216
- test ( " New Object Construction " ) {
230
+ try test ( " New Object Construction " ) {
217
231
// ```js
218
232
// global.Animal = function(name, age, isCat) {
219
233
// this.name = name
@@ -237,7 +251,7 @@ test("New Object Construction") {
237
251
try expectEqual ( dog1Bark ( ) , . string( " wan " ) )
238
252
}
239
253
240
- test ( " Call Function With This " ) {
254
+ try test ( " Call Function With This " ) {
241
255
// ```js
242
256
// global.Animal = function(name, age, isCat) {
243
257
// this.name = name
@@ -263,7 +277,7 @@ test("Call Function With This") {
263
277
try expectEqual ( gotIsCat, . boolean( true ) )
264
278
}
265
279
266
- test ( " Object Conversion " ) {
280
+ try test ( " Object Conversion " ) {
267
281
let array1 = [ 1 , 2 , 3 ]
268
282
let jsArray1 = array1. jsValue ( ) . object!
269
283
try expectEqual ( jsArray1. length, . number( 3 ) )
@@ -292,7 +306,7 @@ test("Object Conversion") {
292
306
try expectEqual ( jsDict1. prop2, . string( " foo " ) )
293
307
}
294
308
295
- test ( " ObjectRef Lifetime " ) {
309
+ try test ( " ObjectRef Lifetime " ) {
296
310
// ```js
297
311
// global.globalObject1 = {
298
312
// "prop_1": {
@@ -322,21 +336,25 @@ func closureScope() -> ObjectIdentifier {
322
336
return result
323
337
}
324
338
325
- test ( " Closure Identifiers " ) {
339
+ try test ( " Closure Identifiers " ) {
326
340
let oid1 = closureScope ( )
327
341
let oid2 = closureScope ( )
328
342
try expectEqual ( oid1, oid2)
329
343
}
330
344
331
345
func checkArray< T> ( _ array: [ T ] ) throws where T: TypedArrayElement {
332
- try expectEqual ( JSTypedArray ( array) . toString!( ) , . string( jsStringify ( array) ) )
346
+ try expectEqual ( toString ( JSTypedArray ( array) . jsValue ( ) . object!) , jsStringify ( array) )
347
+ }
348
+
349
+ func toString< T: JSObject > ( _ object: T ) -> String {
350
+ return object. toString!( ) . string!
333
351
}
334
352
335
353
func jsStringify( _ array: [ Any ] ) -> String {
336
354
array. map ( { String ( describing: $0) } ) . joined ( separator: " , " )
337
355
}
338
356
339
- test ( " TypedArray " ) {
357
+ try test ( " TypedArray " ) {
340
358
let numbers = [ UInt8] ( 0 ... 255 )
341
359
let typedArray = JSTypedArray ( numbers)
342
360
try expectEqual ( typedArray [ 12 ] , 12 )
@@ -366,13 +384,13 @@ test("TypedArray") {
366
384
}
367
385
}
368
386
369
- test ( " TypedArray_Mutation " ) {
387
+ try test ( " TypedArray_Mutation " ) {
370
388
let array = JSTypedArray < Int > ( length: 100 )
371
389
for i in 0 ..< 100 {
372
390
array [ i] = i
373
391
}
374
392
for i in 0 ..< 100 {
375
393
try expectEqual ( i, array [ i] )
376
394
}
377
- try expectEqual ( array. toString! ( ) , . string ( jsStringify ( Array ( 0 ..< 100 ) ) ) )
395
+ try expectEqual ( toString ( array. jsValue ( ) . object! ) , jsStringify ( Array ( 0 ..< 100 ) ) )
378
396
}
0 commit comments