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

gen: use new_map, new_map_init_1 functions (#7444)

This commit is contained in:
Nick Treleaven 2020-12-21 13:37:19 +00:00 committed by GitHub
parent 8eac658ed1
commit 0edec06eac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 6 deletions

View File

@ -460,5 +460,6 @@ fn test_map_or() {
'second': 2
'third': 3
}
_ = m
// num := m['first'] or { return }
}

View File

@ -2515,9 +2515,9 @@ fn (mut g Gen) expr(node ast.Expr) {
}
if size > 0 {
if value_typ.kind == .function {
g.write('new_map_init($size, sizeof(voidptr), _MOV(($key_typ_str[$size]){')
g.write('new_map_init_1($size, sizeof($key_typ_str), sizeof(voidptr), _MOV(($key_typ_str[$size]){')
} else {
g.write('new_map_init($size, sizeof($value_typ_str), _MOV(($key_typ_str[$size]){')
g.write('new_map_init_1($size, sizeof($key_typ_str), sizeof($value_typ_str), _MOV(($key_typ_str[$size]){')
}
for expr in node.keys {
g.expr(expr)
@ -2534,7 +2534,7 @@ fn (mut g Gen) expr(node ast.Expr) {
}
g.write('}))')
} else {
g.write('new_map_1(sizeof($value_typ_str))')
g.write('new_map(sizeof($key_typ_str), sizeof($value_typ_str))')
}
if g.is_shared {
g.write(', .mtx = sync__new_rwmutex()}')
@ -5447,8 +5447,10 @@ fn (mut g Gen) type_default(typ_ table.Type) string {
return '__new_array(0, 1, sizeof($elem_type_str))'
}
if sym.kind == .map {
value_type_str := g.typ(sym.map_info().value_type)
return 'new_map_1(sizeof($value_type_str))'
info := sym.map_info()
key_type_str := g.typ(info.key_type)
value_type_str := g.typ(info.value_type)
return 'new_map(sizeof($key_type_str), sizeof($value_type_str))'
}
// User struct defined in another module.
// if typ.contains('__') {

View File

@ -258,7 +258,7 @@ fn (mut g Gen) decode_map(key_type table.Type, value_type table.Type) string {
Option err = v_error( string_add(_SLIT("Json element is not an object: "), tos2(cJSON_PrintUnformatted(root))) );
return *(Option_map_${styp}_$styp_v *)&err;
}
res = new_map_1(sizeof($styp_v));
res = new_map(sizeof($styp), sizeof($styp_v));
cJSON *jsval = NULL;
cJSON_ArrayForEach(jsval, root)
{