1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

js: add more tests & add array prepend codegen (#10988)

This commit is contained in:
playX 2021-07-29 11:39:36 +03:00 committed by GitHub
parent 08aa6c08f6
commit 75c41252d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 217 additions and 3 deletions

View File

@ -149,3 +149,14 @@ pub fn (mut a array) delete(i int) {
pub fn (mut a array) delete_many(i int, size int) {
#a.arr.splice(i.valueOf(),size.valueOf())
}
// prepend prepends one value to the array.
pub fn (mut a array) prepend(val voidptr) {
a.insert(0, val)
}
// prepend_many prepends another array to this array.
[unsafe]
pub fn (mut a array) prepend_many(val voidptr, size int) {
unsafe { a.insert_many(0, val, size) }
}

View File

@ -1444,6 +1444,26 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) {
g.write(')')
return
}
'prepend' {
arg_sym := g.table.get_type_symbol(node.args[0].typ)
is_arg_array := arg_sym.kind == .array && node.args[0].typ == node.left_type
if is_arg_array {
g.write('prepend_many(')
} else {
g.write('prepend(')
}
if is_arg_array {
g.expr(node.args[0].expr)
g.write('.arr, ')
g.expr(node.args[0].expr)
g.write('.len')
} else {
g.expr(node.args[0].expr)
}
g.write(')')
return
}
else {}
}
}
@ -1975,7 +1995,7 @@ fn (mut g JsGen) gen_integer_literal_expr(it ast.IntegerLiteral) {
// Skip cast if type is the same as the parrent caster
if g.cast_stack.len > 0 {
if g.cast_stack[g.cast_stack.len - 1] in ast.integer_type_idxs {
g.write('$it.val')
g.write('new int($it.val)')
return
}
}
@ -2013,7 +2033,7 @@ fn (mut g JsGen) gen_float_literal_expr(it ast.FloatLiteral) {
// Skip cast if type is the same as the parrent caster
if g.cast_stack.len > 0 {
if g.cast_stack[g.cast_stack.len - 1] in ast.float_type_idxs {
g.write('$it.val')
g.write('new f32($it.val)')
return
} else if g.cast_stack[g.cast_stack.len - 1] in ast.integer_type_idxs {
g.write(int(it.val.f64()).str())

View File

@ -61,4 +61,51 @@ true
1
1.1
[1,2,3,4]
[1,5,6,2,3,4]
[1,5,6,2,3,4]
0
1
1
0
1
1.1
[1,2,3,4]
[5,6,1,2,3,4]
5
true
1.1
1.1
1.1
-123
-123
-123
123
123
123
1.1
1.1
1.1
1
2
1
2
1
abc
1
abc
0
abc
2
3
2
3
1
2
1
2
2
1
4
6
1
4
6

View File

@ -183,4 +183,140 @@ fn main() {
a.insert(1, b)
println(a)
}
{
// test prepend
mut a := []int{}
println(a.len)
a.prepend(1)
println(a.len)
println(a[0])
mut b := []f64{}
println(b.len)
b.prepend(f64(1.1))
println(b.len)
println(b[0])
}
{
// test prepend many
mut a := [3, 4]
a.prepend([1, 2])
println(a)
b := [5, 6]
a.prepend(b)
println(a)
}
{
// test repeat
{
a := [0].repeat(5)
println(a.len)
println(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0)
}
{
a := [1.1].repeat(10)
println(a[0])
println(a[5])
println(a[9])
}
{
a := [i64(-123)].repeat(10)
println(a[0])
println(a[5])
println(a[9])
}
{
a := [u64(123)].repeat(10)
println(a[0])
println(a[5])
println(a[9])
}
{
a := [1.1].repeat(10)
println(a[0])
println(a[5])
println(a[9])
}
{
a := [1, 2].repeat(2)
println(a[0])
println(a[1])
println(a[2])
println(a[3])
}
{
a := ['1', 'abc'].repeat(2)
println(a[0])
println(a[1])
println(a[2])
println(a[3])
}
{
mut a := ['1', 'abc'].repeat(0)
println(a.len)
a << 'abc'
println(a[0])
}
}
{
/*
// test deep repeat
mut a3 := [[[1, 1], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
r := a3.repeat(3)
a3[1][1][0] = 17
print(r)
assert r == [
[[1, 1], [2, 2], [3, 3]],
[[4, 4], [5, 5], [6, 6]],
[[1, 1], [2, 2], [3, 3]],
[[4, 4], [5, 5], [6, 6]],
[[1, 1], [2, 2], [3, 3]],
[[4, 4], [5, 5], [6, 6]],
]
assert a3 == [[[1, 1], [2, 2], [3, 3]], [[4, 4], [17, 5],
[6, 6],
]]
*/
}
{
// test right
a := [1, 2, 3, 4]
c := a[1..a.len]
d := a[1..]
println(c[0])
println(c[1])
println(d[0])
println(d[1])
}
{
// test left
a := [1, 2, 3]
c := a[0..2]
d := a[..2]
println(c[0])
println(c[1])
println(d[0])
println(d[1])
}
{
// test slice
a := [1, 2, 3, 4]
b := a[2..4]
println(b.len)
println(a[1..2].len)
println(a.len)
}
{
// test push many
mut a := [1, 2, 3]
b := [4, 5, 6]
a << b
println(a.len)
println(a[0])
println(a[3])
println(a[5])
}
}