Skip to content

Commit dd1e57c

Browse files
committed
Add examples
1 parent a8c1e6c commit dd1e57c

1 file changed

Lines changed: 138 additions & 3 deletions

File tree

usvm-ts/src/main/kotlin/org/usvm/machine/expr/CallApproximations.kt

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ private fun TsExprResolver.handlePromiseResolveReject(expr: EtsInstanceCallExpr)
259259
* Appends the specified `items` to the end of the array.
260260
* This method modifies the array in place and returns the new length of the array.
261261
*
262+
* ### Examples:
263+
* ```
264+
* let a = [10, 20];
265+
*
266+
* a.push(30) -> 3
267+
* a == [10, 20, 30]
268+
*
269+
* a.push(5, 4, 3) -> 6
270+
* a == [10, 20, 30, 5, 4, 3]
271+
* ```
272+
*
262273
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.push
263274
*/
264275
private fun TsExprResolver.handleArrayPush(
@@ -306,6 +317,21 @@ private fun TsExprResolver.handleArrayPush(
306317
* Pops the last element from the array and returns it.
307318
* If the array is empty, it returns `undefined`.
308319
*
320+
* ### Examples:
321+
* ```
322+
* let a = [10, 20, 30];
323+
*
324+
* a.pop() -> 30 // last element
325+
* a == [10, 20] // modified in place
326+
*
327+
* a.pop() -> 20 // last element
328+
* a == [10] // modified in place
329+
*
330+
* let empty = [];
331+
* empty.pop() -> undefined // no elements to pop
332+
* empty == [] // still empty
333+
* ```
334+
*
309335
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.pop
310336
*/
311337
private fun TsExprResolver.handleArrayPop(
@@ -354,6 +380,22 @@ private fun TsExprResolver.handleArrayPop(
354380
* If `end` index is not provided, it defaults to the length of the array.
355381
* This method modifies the array in place and returns it.
356382
*
383+
* ### Examples:
384+
* ```
385+
* let a = [1, 2, 3, 4, 5];
386+
*
387+
* a.fill(0) -> [0, 0, 0, 0, 0] (fill entire array)
388+
* a == [0, 0, 0, 0, 0]
389+
*
390+
* let b = [1, 2, 3, 4, 5];
391+
* b.fill(9, 2) -> [1, 2, 9, 9, 9] (fill from index 2 to end)
392+
* b == [1, 2, 9, 9, 9]
393+
*
394+
* let c = [1, 2, 3, 4, 5];
395+
* c.fill(8, 1, 3) -> [1, 8, 8, 4, 5] (fill from index 1 to 3)
396+
* c == [1, 8, 8, 4, 5]
397+
* ```
398+
*
357399
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.fill
358400
*/
359401
private fun TsExprResolver.handleArrayFill(
@@ -367,6 +409,7 @@ private fun TsExprResolver.handleArrayFill(
367409
}
368410
val value = resolve(expr.args[0]) ?: return null
369411

412+
// TODO: Support negative `start` and `end` indices.
370413
val start = if (expr.args.size > 1) {
371414
resolve(expr.args[1]) ?: return null
372415
} else {
@@ -451,6 +494,21 @@ private const val ARRAY_FILL_MAX_SIZE = 10_000
451494
* Removes the first element from the array and returns it.
452495
* If the array is empty, it returns `undefined`.
453496
*
497+
* ### Examples:
498+
* ```
499+
* let a = [1, 2, 3];
500+
*
501+
* a.shift() -> 1 (removed element)
502+
* a == [2, 3] // modified in place
503+
*
504+
* a.shift() -> 2 (removed element)
505+
* a == [3] // modified in place
506+
*
507+
* let empty = [];
508+
* empty.shift() -> undefined
509+
* empty == [] // still empty
510+
* ```
511+
*
454512
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.shift
455513
*/
456514
private fun TsExprResolver.handleArrayShift(
@@ -506,6 +564,17 @@ private fun TsExprResolver.handleArrayShift(
506564
* Prepends the specified `items` to the start of the array.
507565
* This method modifies the array in place and returns the new length of the array.
508566
*
567+
* ### Examples:
568+
* ```
569+
* let a = [2, 3];
570+
*
571+
* a.unshift(1) -> 3 (new length)
572+
* a == [1, 2, 3] // modified in place
573+
*
574+
* a.unshift(0) -> 4 (new length)
575+
* a == [0, 1, 2, 3] // modified in place
576+
* ```
577+
*
509578
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.unshift
510579
*/
511580
private fun TsExprResolver.handleArrayUnshift(
@@ -565,6 +634,18 @@ private fun TsExprResolver.handleArrayUnshift(
565634
* If `separator` is not provided, it defaults to `","`.
566635
* This method returns the resulting string.
567636
*
637+
* ### Examples:
638+
* ```
639+
* let a = [1, 2, 3];
640+
*
641+
* a.join() -> "1,2,3" (default comma separator)
642+
* a.join("-") -> "1-2-3" (custom separator)
643+
* a.join("") -> "123" (no separator)
644+
*
645+
* let b = ["hello", "world"];
646+
* b.join(" ") -> "hello world"
647+
* ```
648+
*
568649
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.join
569650
*/
570651
private fun TsExprResolver.handleArrayJoin(
@@ -596,6 +677,18 @@ private const val ARRAY_JOIN_RESULT = "joined_array_result"
596677
* If `start` is not provided, it defaults to `0`.
597678
* If `end` is not provided, it defaults to the length of the array.
598679
*
680+
* ### Examples:
681+
* ```
682+
* let a = [1, 2, 3, 4, 5];
683+
*
684+
* a.slice() -> [1, 2, 3, 4, 5] (copy entire array)
685+
* a.slice(2) -> [3, 4, 5] (from index 2 to end)
686+
* a.slice(1, 4) -> [2, 3, 4] (from index 1 to 4, exclusive)
687+
* a.slice(-2) -> [4, 5] (last 2 elements)
688+
*
689+
* a == [1, 2, 3, 4, 5] // original array not modified
690+
* ```
691+
*
599692
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.slice
600693
*/
601694
private fun TsExprResolver.handleArraySlice(
@@ -608,6 +701,7 @@ private fun TsExprResolver.handleArraySlice(
608701
"Array.slice() should have at most two arguments, but got ${expr.args.size}"
609702
}
610703

704+
// TODO: Support negative `start` and `end` indices.
611705
val start = if (expr.args.isNotEmpty()) {
612706
resolve(expr.args[0]) ?: return null
613707
} else {
@@ -683,9 +777,9 @@ private fun TsExprResolver.handleArraySlice(
683777
* let a = [1, 2];
684778
* let b = [3, 4];
685779
*
686-
* a.concat() -> [1, 2]
687-
* a.concat(b) -> [1, 2, 3, 4]
688-
* a.concat(b, 5) -> [1, 2, 3, 4, 5]
780+
* a.concat() -> [1, 2] // same array
781+
* a.concat(b) -> [1, 2, 3, 4] // concat two arrays
782+
* a.concat(b, 5) -> [1, 2, 3, 4, 5] // concat with an array and a single element
689783
*
690784
* a == [1, 2] // not modified
691785
* ```
@@ -776,6 +870,19 @@ private fun TsExprResolver.handleArrayConcat(
776870
* Handles the `Array.indexOf(searchElement)` method call.
777871
* Returns the index of the first occurrence of `searchElement` in the array, or `-1` if not found.
778872
*
873+
* ### Examples:
874+
* ```
875+
* let a = [1, 2, 3, 2, 4];
876+
*
877+
* a.indexOf(2) -> 1 (first occurrence at index 1)
878+
* a.indexOf(5) -> -1 (not found)
879+
* a.indexOf(3) -> 2 (found at index 2)
880+
*
881+
* let b = ["hello", "world", "hello"];
882+
* b.indexOf("hello") -> 0 (first occurrence)
883+
* b.indexOf("foo") -> -1 (not found)
884+
* ```
885+
*
779886
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.indexof
780887
*/
781888
private fun TsExprResolver.handleArrayIndexOf(
@@ -824,6 +931,19 @@ private fun TsExprResolver.handleArrayIndexOf(
824931
* Handles the `Array.includes(searchElement)` method call.
825932
* Returns `true` if the array contains the `searchElement`, and `false` otherwise.
826933
*
934+
* ### Examples:
935+
* ```
936+
* let a = [1, 2, 3, 4, 5];
937+
*
938+
* a.includes(3) -> true (element found)
939+
* a.includes(6) -> false (element not found)
940+
* a.includes(1) -> true (first element)
941+
*
942+
* let b = ["hello", "world"];
943+
* b.includes("hello") -> true (string found)
944+
* b.includes("foo") -> false (string not found)
945+
* ```
946+
*
827947
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.includes
828948
*/
829949
private fun TsExprResolver.handleArrayIncludes(
@@ -858,6 +978,21 @@ private fun TsExprResolver.handleArrayIncludes(
858978
* Handles the `Array.reverse()` method call.
859979
* Reverses the elements of the array in place and returns the modified array.
860980
*
981+
* ### Examples:
982+
* ```
983+
* let a = [1, 2, 3, 4, 5];
984+
*
985+
* a.reverse() -> [5, 4, 3, 2, 1] (reversed array)
986+
* a == [5, 4, 3, 2, 1] // modified in place
987+
*
988+
* let b = ["hello", "world"];
989+
* b.reverse() -> ["world", "hello"]
990+
* b == ["world", "hello"] // modified in place
991+
*
992+
* let empty = [];
993+
* empty.reverse() -> [] // still empty
994+
* ```
995+
*
861996
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.reverse
862997
*/
863998
private fun TsExprResolver.handleArrayReverse(

0 commit comments

Comments
 (0)