cgen: do full struct initialisation for V structs, fixes invalid data from `map[string]Struct{}['unknown']` (#9878)

pull/9891/head weekly.2021.17
Don Park 2021-04-26 23:18:48 -06:00 committed by GitHub
parent 787a63dab6
commit aebb551e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 107 additions and 99 deletions

View File

@ -140,10 +140,6 @@ pub fn new_test_session(_vargs string) TestSession {
skip_files << 'examples/sokol/05_instancing_glsl/rt_glsl.v'
// Skip obj_viewer code in the CI
skip_files << 'examples/sokol/06_obj_viewer/show_obj.v'
skip_files << 'examples/sokol/06_obj_viewer/obj/obj.v'
skip_files << 'examples/sokol/06_obj_viewer/obj/rend.v'
skip_files << 'examples/sokol/06_obj_viewer/obj/struct.v'
skip_files << 'examples/sokol/06_obj_viewer/obj/util.v'
}
if testing.github_job != 'ubuntu-tcc' {
skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases

View File

@ -293,7 +293,7 @@ fn init_cube_glsl(mut app App) {
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'}
unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) }
vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t)))
@ -304,7 +304,6 @@ fn init_cube_glsl(mut app App) {
vert_buffer_desc.@type = .vertexbuffer
// vert_buffer_desc.usage = .immutable
vert_buffer_desc.label = 'cube-vertices'.str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
@ -317,7 +316,7 @@ fn init_cube_glsl(mut app App) {
22, 21, 20, 23, 22, 20
]
mut index_buffer_desc := C.sg_buffer_desc{}
mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'}
unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) }
index_buffer_desc.size = size_t(indices.len * int(sizeof(u16)))
@ -327,7 +326,6 @@ fn init_cube_glsl(mut app App) {
}
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = 'cube-indices'.str
ibuf := gfx.make_buffer(&index_buffer_desc)
// create shader

View File

@ -172,7 +172,7 @@ fn init_cube_glsl(mut app App) {
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'}
unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) }
vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t)))
@ -182,7 +182,6 @@ fn init_cube_glsl(mut app App) {
}
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = 'cube-vertices'.str
vbuf := gfx.make_buffer(&vert_buffer_desc)
// create an index buffer for the cube
@ -195,7 +194,7 @@ fn init_cube_glsl(mut app App) {
22, 21, 20, 23, 22, 20,
]
mut index_buffer_desc := C.sg_buffer_desc{}
mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = size_t(indices.len * int(sizeof(u16)))
@ -205,7 +204,6 @@ fn init_cube_glsl(mut app App) {
}
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
// create shader

View File

@ -176,7 +176,7 @@ fn init_cube_glsl_m(mut app App) {
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'}
unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) }
vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t)))
vert_buffer_desc.data = C.sg_range{
@ -184,7 +184,6 @@ fn init_cube_glsl_m(mut app App) {
size: size_t(vertices.len * int(sizeof(Vertex_t)))
}
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = 'cube-vertices'.str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
@ -199,7 +198,7 @@ fn init_cube_glsl_m(mut app App) {
*/
]
mut index_buffer_desc := C.sg_buffer_desc{}
mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'}
unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) }
index_buffer_desc.size = size_t(indices.len * int(sizeof(u16)))
index_buffer_desc.data = C.sg_range{
@ -207,7 +206,6 @@ fn init_cube_glsl_m(mut app App) {
size: size_t(indices.len * int(sizeof(u16)))
}
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = 'cube-indices'.str
ibuf := gfx.make_buffer(&index_buffer_desc)
// create shader
@ -284,7 +282,7 @@ fn init_cube_glsl_p(mut app App) {
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'}
unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) }
vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t)))
vert_buffer_desc.data = C.sg_range{
@ -292,7 +290,6 @@ fn init_cube_glsl_p(mut app App) {
size: size_t(vertices.len * int(sizeof(Vertex_t)))
}
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = 'cube-vertices'.str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
@ -308,7 +305,7 @@ fn init_cube_glsl_p(mut app App) {
]
mut index_buffer_desc := C.sg_buffer_desc{}
mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'}
unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) }
index_buffer_desc.size = size_t(indices.len * int(sizeof(u16)))
index_buffer_desc.data = C.sg_range{
@ -316,7 +313,6 @@ fn init_cube_glsl_p(mut app App) {
size: size_t(indices.len * int(sizeof(u16)))
}
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = 'cube-indices'.str
ibuf := gfx.make_buffer(&index_buffer_desc)
// create shader

View File

@ -189,7 +189,7 @@ fn init_cube_glsl_i(mut app App) {
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'}
unsafe {C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc))}
vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t)))
vert_buffer_desc.data = C.sg_range{
@ -197,17 +197,15 @@ fn init_cube_glsl_i(mut app App) {
size: size_t(vertices.len * int(sizeof(Vertex_t)))
}
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = "cube-vertices".str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an instance buffer for the cube */
mut inst_buffer_desc := C.sg_buffer_desc{}
mut inst_buffer_desc := C.sg_buffer_desc{label: c'instance-data'}
unsafe {C.memset(&inst_buffer_desc, 0, sizeof(inst_buffer_desc))}
inst_buffer_desc.size = size_t(num_inst * int(sizeof(m4.Vec4)))
inst_buffer_desc.@type = .vertexbuffer
inst_buffer_desc.usage = .stream
inst_buffer_desc.label = "instance-data".str
inst_buf := gfx.make_buffer(&inst_buffer_desc)
@ -221,7 +219,7 @@ fn init_cube_glsl_i(mut app App) {
22, 21, 20, 23, 22, 20
]
mut index_buffer_desc := C.sg_buffer_desc{}
mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = size_t(indices.len * int(sizeof(u16)))
index_buffer_desc.data = C.sg_range{
@ -229,7 +227,6 @@ fn init_cube_glsl_i(mut app App) {
size: size_t(indices.len * int(sizeof(u16)))
}
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */

View File

@ -1,3 +1,5 @@
module obj
/**********************************************************************
*
* .obj loader
@ -8,8 +10,6 @@
*
* TODO:
**********************************************************************/
module obj
import gg.m4
import strconv

View File

@ -68,7 +68,9 @@ pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader,
res.material = obj_part.part[in_part[0]].material
// vertex buffer
mut vert_buffer_desc := C.sg_buffer_desc{}
mut vert_buffer_desc := C.sg_buffer_desc{
label: 0
}
unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) }
vert_buffer_desc.size = size_t(obj_buf.vbuf.len * int(sizeof(Vertex_pnct)))
@ -82,7 +84,9 @@ pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader,
vbuf := gfx.make_buffer(&vert_buffer_desc)
// index buffer
mut index_buffer_desc := C.sg_buffer_desc{}
mut index_buffer_desc := C.sg_buffer_desc{
label: 0
}
unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) }
index_buffer_desc.size = size_t(obj_buf.ibuf.len * int(sizeof(u32)))

View File

@ -14,9 +14,9 @@ pub fn read_lines_from_file(file_path string) []string {
}
rows = bts.bytestr().split_into_lines()
} $else {
path = 'assets/models/' + file_path
path = os.resource_abs_path('assets/models/' + file_path)
rows = os.read_lines(path) or {
eprintln('File [$path] NOT FOUND!')
eprintln('File [$path] NOT FOUND! file_path: $file_path')
return rows
}
}
@ -34,7 +34,7 @@ pub fn read_bytes_from_file(file_path string) []byte {
exit(0)
}
} $else {
path = 'assets/models/' + file_path
path = os.resource_abs_path('assets/models/' + file_path)
buffer = os.read_bytes(path) or {
eprintln('Texure file: [$path] NOT FOUND!')
exit(0)

View File

@ -189,7 +189,9 @@ fn gg_init_sokol_window(user_data voidptr) {
}
}
//
mut pipdesc := C.sg_pipeline_desc{}
mut pipdesc := C.sg_pipeline_desc{
label: c'alpha_image'
}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }
color_state := C.sg_color_state{

View File

@ -9,8 +9,8 @@ const (
struct UdpSocket {
handle int
l Addr
r ?Addr
local Addr
remote ?Addr
}
pub struct UdpConn {
@ -28,8 +28,8 @@ pub fn dial_udp(laddr string, raddr string) ?&UdpConn {
sbase := new_udp_socket(local.port) ?
sock := UdpSocket{
handle: sbase.handle
l: local
r: resolve_wrapper(raddr)
local: local
remote: resolve_wrapper(raddr)
}
return &UdpConn{
sock: sock
@ -45,7 +45,7 @@ fn resolve_wrapper(raddr string) ?Addr {
return x
}
pub fn (mut c UdpConn) write_ptr(b byteptr, len int) ?int {
pub fn (mut c UdpConn) write_ptr(b &byte, len int) ?int {
remote := c.sock.remote() or { return err_no_udp_remote }
return c.write_to_ptr(remote, b, len)
}
@ -63,7 +63,7 @@ pub fn (mut c UdpConn) write_string(s string) ?int {
return c.write_ptr(s.str, s.len)
}
pub fn (mut c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ?int {
pub fn (mut c UdpConn) write_to_ptr(addr Addr, b &byte, len int) ?int {
res := C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len)
if res >= 0 {
return res
@ -207,7 +207,7 @@ fn new_udp_socket(local_port int) ?&UdpSocket {
}
pub fn (s &UdpSocket) remote() ?Addr {
return s.r
return s.remote
}
pub fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ? {

View File

@ -43,7 +43,7 @@ pub struct C.sg_context_desc {
}
pub struct C.sg_gl_context_desc {
gl_force_gles2 bool
force_gles2 bool
}
pub struct C.sg_metal_context_desc {
@ -82,7 +82,7 @@ pub mut:
sample_count int
blend_color C.sg_color
alpha_to_coverage_enabled bool
label byteptr
label &char = &char(0)
_end_canary u32
}
@ -156,7 +156,7 @@ pub mut:
attrs [16]C.sg_shader_attr_desc
vs C.sg_shader_stage_desc
fs C.sg_shader_stage_desc
label byteptr
label &char
_end_canary u32
}
@ -210,16 +210,16 @@ pub fn (desc &C.sg_shader_desc) make_shader() C.sg_shader {
pub struct C.sg_shader_attr_desc {
pub mut:
name byteptr // GLSL vertex attribute name (only required for GLES2)
sem_name byteptr // HLSL semantic name
name &char // GLSL vertex attribute name (only required for GLES2)
sem_name &char // HLSL semantic name
sem_index int // HLSL semantic index
}
pub struct C.sg_shader_stage_desc {
pub mut:
source byteptr
source &char
bytecode C.sg_range
entry byteptr
entry &char
uniform_blocks [4]C.sg_shader_uniform_block_desc
images [12]C.sg_shader_image_desc
}
@ -238,14 +238,14 @@ pub mut:
pub struct C.sg_shader_uniform_desc {
pub mut:
name byteptr
name &char
@type UniformType
array_count int
}
pub struct C.sg_shader_image_desc {
pub mut:
name byteptr
name &char
image_type ImageType
}
@ -284,7 +284,7 @@ pub mut:
_start_canary u32
color_attachments [4]C.sg_pass_attachment_desc
depth_stencil_attachment C.sg_pass_attachment_desc
label byteptr
label &char
_end_canary u32
}
@ -316,7 +316,7 @@ pub mut:
@type BufferType
usage Usage
data C.sg_range
label byteptr
label &char
// GL specific
gl_buffers [2]u32
// Metal specific
@ -364,7 +364,7 @@ pub mut:
min_lod f32
max_lod f32
data C.sg_image_data
label byteptr
label &char
// GL specific
gl_textures [2]u32
gl_texture_target u32
@ -397,7 +397,7 @@ pub fn (i C.sg_image) free() {
pub struct C.sg_image_data {
pub mut:
//subimage [C.SG_CUBEFACE_NUM][C.SG_MAX_MIPMAPS]C.sg_range
// subimage [C.SG_CUBEFACE_NUM][C.SG_MAX_MIPMAPS]C.sg_range
subimage [6][16]C.sg_range
}

View File

@ -197,9 +197,7 @@ pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
}
pub fn (t &Table) find_fn(name string) ?Fn {
f := t.fns[name]
if f.name.str != 0 {
// TODO
if f := t.fns[name] {
return f
}
return none

View File

@ -2730,8 +2730,10 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
}
return_stmt.types = got_types
// allow `none` & `error` return types for function that returns optional
option_type_idx := c.table.type_idxs['Option']
got_types_0_idx := got_types[0].idx()
if exp_is_optional
&& got_types[0].idx() in [ast.none_type_idx, ast.error_type_idx, c.table.type_idxs['Option']] {
&& got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx] {
return
}
if expected_types.len > 0 && expected_types.len != got_types.len {
@ -4485,7 +4487,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
if to_type_sym.language != .c {
c.ensure_type_exists(node.typ, node.pos) or {}
}
expr_is_ptr := node.expr_type.is_ptr() || node.expr_type.idx() in ast.pointer_type_idxs
n_e_t_idx := node.expr_type.idx()
expr_is_ptr := node.expr_type.is_ptr() || n_e_t_idx in ast.pointer_type_idxs
if expr_is_ptr && to_type_sym.kind == .string && !node.in_prexpr {
if node.has_arg {
c.warn('to convert a C string buffer pointer to a V string, please use x.vstring_with_len(len) instead of string(x,len)',

View File

@ -5250,7 +5250,9 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
continue
}
if field.typ.has_flag(.optional) {
// TODO handle/require optionals in inits
field_name := c_name(field.name)
g.write('.$field_name = {0},')
initialized = true
continue
}
if field.typ in info.embeds {
@ -5906,18 +5908,23 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
mut has_none_zero := false
mut init_str := '{'
info := sym.info as ast.Struct
typ_is_shared_f := typ.has_flag(.shared_f)
if sym.language == .v && !typ_is_shared_f {
for field in info.fields {
field_sym := g.table.get_type_symbol(field.typ)
if field_sym.kind in [.array, .map] || field.has_default_expr {
if field.has_default_expr
|| field_sym.kind in [.array, .map, .string, .ustring, .bool, .alias, .size_t, .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .char, .voidptr, .byteptr, .charptr, .struct_] {
field_name := c_name(field.name)
if field.has_default_expr {
expr_str := g.expr_string(field.default_expr)
init_str += '.$field.name = $expr_str,'
init_str += '.$field_name = $expr_str,'
} else {
init_str += '.$field.name = ${g.type_default(field.typ)},'
init_str += '.$field_name = ${g.type_default(field.typ)},'
}
has_none_zero = true
}
}
}
if has_none_zero {
init_str += '}'
type_name := g.typ(typ)

View File

@ -0,0 +1,9 @@
struct Test {
s string
}
fn test_map_value_init() {
m := map[string]Test{}
empty := m['not-here']
assert !isnil(empty.s.str)
}