| 1 | // Copyright (c) 2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module ssa |
| 5 | |
| 6 | import v2.ast |
| 7 | import v2.types |
| 8 | |
| 9 | fn int_contract_num(value string) ast.Expr { |
| 10 | return ast.Expr(ast.BasicLiteral{ |
| 11 | kind: .number |
| 12 | value: value |
| 13 | }) |
| 14 | } |
| 15 | |
| 16 | fn int_contract_cast(type_name string, value string) ast.Expr { |
| 17 | return ast.Expr(ast.CallOrCastExpr{ |
| 18 | lhs: ast.Expr(ast.Ident{ |
| 19 | name: type_name |
| 20 | }) |
| 21 | expr: int_contract_num(value) |
| 22 | }) |
| 23 | } |
| 24 | |
| 25 | fn test_const_untyped_integer_array_serializes_as_dense_i32() { |
| 26 | mut mod := Module.new('array_int_contract') |
| 27 | env := types.Environment.new() |
| 28 | mut b := Builder.new_with_env(mod, env) |
| 29 | data := b.try_serialize_const_array(ast.ArrayInitExpr{ |
| 30 | exprs: [int_contract_num('1'), int_contract_num('2'), |
| 31 | int_contract_num('3')] |
| 32 | }) |
| 33 | assert data == [u8(1), 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0] |
| 34 | } |
| 35 | |
| 36 | fn test_const_explicit_i64_array_serializes_as_dense_i64() { |
| 37 | mut mod := Module.new('array_int_contract_i64') |
| 38 | env := types.Environment.new() |
| 39 | mut b := Builder.new_with_env(mod, env) |
| 40 | data := b.try_serialize_const_array(ast.ArrayInitExpr{ |
| 41 | exprs: [int_contract_cast('i64', '1'), int_contract_cast('i64', '2')] |
| 42 | }) |
| 43 | assert data == [u8(1), 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0] |
| 44 | } |
| 45 | |
| 46 | fn test_const_explicit_u8_array_serializes_as_dense_u8() { |
| 47 | mut mod := Module.new('array_int_contract_u8') |
| 48 | env := types.Environment.new() |
| 49 | mut b := Builder.new_with_env(mod, env) |
| 50 | data := b.try_serialize_const_array(ast.ArrayInitExpr{ |
| 51 | exprs: [int_contract_cast('u8', '1'), int_contract_cast('u8', '2')] |
| 52 | }) |
| 53 | assert data == [u8(1), 2] |
| 54 | } |
| 55 | |
| 56 | fn test_const_explicit_rune_array_serializes_as_dense_i32() { |
| 57 | mut mod := Module.new('array_int_contract_rune') |
| 58 | env := types.Environment.new() |
| 59 | mut b := Builder.new_with_env(mod, env) |
| 60 | data := b.try_serialize_const_array(ast.ArrayInitExpr{ |
| 61 | exprs: [int_contract_cast('rune', '65'), int_contract_cast('rune', '8364')] |
| 62 | }) |
| 63 | assert data == [u8(65), 0, 0, 0, 172, 32, 0, 0] |
| 64 | } |
| 65 | |