gen: use new_map, new_map_init_1 functions (#7444)

pull/7450/head
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 'second': 2
'third': 3 'third': 3
} }
_ = m
// num := m['first'] or { return } // num := m['first'] or { return }
} }

View File

@ -2515,9 +2515,9 @@ fn (mut g Gen) expr(node ast.Expr) {
} }
if size > 0 { if size > 0 {
if value_typ.kind == .function { 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 { } 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 { for expr in node.keys {
g.expr(expr) g.expr(expr)
@ -2534,7 +2534,7 @@ fn (mut g Gen) expr(node ast.Expr) {
} }
g.write('}))') g.write('}))')
} else { } 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 { if g.is_shared {
g.write(', .mtx = sync__new_rwmutex()}') 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))' return '__new_array(0, 1, sizeof($elem_type_str))'
} }
if sym.kind == .map { if sym.kind == .map {
value_type_str := g.typ(sym.map_info().value_type) info := sym.map_info()
return 'new_map_1(sizeof($value_type_str))' 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. // User struct defined in another module.
// if typ.contains('__') { // 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))) ); Option err = v_error( string_add(_SLIT("Json element is not an object: "), tos2(cJSON_PrintUnformatted(root))) );
return *(Option_map_${styp}_$styp_v *)&err; 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 *jsval = NULL;
cJSON_ArrayForEach(jsval, root) cJSON_ArrayForEach(jsval, root)
{ {