reflection: fix a panic on reflection.type_of(ptr) like c strings, etc (fix #19595) (#19599)

This commit is contained in:
shove 2023-10-19 16:02:02 +08:00 committed by GitHub
parent 0a89f3082d
commit 092358fd24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -242,7 +242,7 @@ pub fn get_string_by_idx(idx int) string {
// type_of returns the type info of the passed value // type_of returns the type info of the passed value
pub fn type_of[T](val T) Type { pub fn type_of[T](val T) Type {
return g_reflection.types.filter(it.idx == typeof[T]().idx)[0] return g_reflection.types.filter(it.idx == typeof[T]().idx & 0xff00ffff)[0]
} }
// get_modules returns the module name built with V source // get_modules returns the module name built with V source

View file

@ -86,3 +86,10 @@ fn test_get_string_by_idx() {
file_idx := reflection.get_funcs().filter(it.name == 'all_after_last')[0].file_idx file_idx := reflection.get_funcs().filter(it.name == 'all_after_last')[0].file_idx
assert reflection.get_string_by_idx(file_idx).ends_with('string.v') assert reflection.get_string_by_idx(file_idx).ends_with('string.v')
} }
fn test_ref() {
cstr := c'abc' // &u8
assert reflection.type_of(cstr).str().contains("name: 'u8'")
ptr_user := &User{}
assert reflection.type_of(ptr_user).str().contains("name: 'User'")
}