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

runtime as type check: part 2

This commit is contained in:
Alexander Medvednikov 2020-04-25 08:42:23 +02:00
parent 2d187fb951
commit 48857090fc
3 changed files with 16 additions and 9 deletions

View File

@ -17,7 +17,7 @@ pub:
} }
// Internal function, used by V (`nums := []int`) // Internal function, used by V (`nums := []int`)
fn new_array(mylen int, cap int, elm_size int) array { fn __new_array(mylen int, cap int, elm_size int) array {
cap_ := if cap == 0 { 1 } else { cap } cap_ := if cap == 0 { 1 } else { cap }
arr := array{ arr := array{
len: mylen len: mylen
@ -27,14 +27,21 @@ fn new_array(mylen int, cap int, elm_size int) array {
} }
return arr return arr
} }
fn new_array(mylen int, cap int, elm_size int) array {
cap_ := if cap == 0 { 1 } else { cap }
arr := array{
len: mylen
cap: cap_
element_size: elm_size
data: vcalloc(cap_ * elm_size)
}
return arr
fn __new_array(mylen int, cap int, elm_size int) array {
return new_array(mylen, cap, elm_size)
} }
// TODO // TODO
pub fn make(len int, cap int, elm_size int) array { pub fn make(len int, cap int, elm_size int) array {
return new_array(len, cap, elm_size) return __new_array(len, cap, elm_size)
} }
/* /*

View File

@ -966,7 +966,7 @@ pub fn (s string) ustring() ustring {
// runes will have at least s.len elements, save reallocations // runes will have at least s.len elements, save reallocations
// TODO use VLA for small strings? // TODO use VLA for small strings?
runes: new_array(0, s.len, sizeof(int)) runes: __new_array(0, s.len, sizeof(int))
} }
for i := 0; i < s.len; i++ { for i := 0; i < s.len; i++ {
char_len := utf8_char_len(s.str[i]) char_len := utf8_char_len(s.str[i])
@ -984,7 +984,7 @@ __global g_ustring_runes []int
pub fn (s string) ustring_tmp() ustring { pub fn (s string) ustring_tmp() ustring {
if g_ustring_runes.len == 0 { if g_ustring_runes.len == 0 {
g_ustring_runes = new_array(0, 128, sizeof(int)) g_ustring_runes = __new_array(0, 128, sizeof(int))
} }
mut res := ustring{ mut res := ustring{
s: s s: s
@ -1032,7 +1032,7 @@ fn (u ustring) ge(a ustring) bool {
pub fn (u ustring) add(a ustring) ustring { pub fn (u ustring) add(a ustring) ustring {
mut res := ustring{ mut res := ustring{
s: u.s + a.s s: u.s + a.s
runes: new_array(0, u.s.len + a.s.len, sizeof(int)) runes: __new_array(0, u.s.len + a.s.len, sizeof(int))
} }
mut j := 0 mut j := 0
for i := 0; i < u.s.len; i++ { for i := 0; i < u.s.len; i++ {

View File

@ -2913,16 +2913,16 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
styp := g.typ(node.typ) styp := g.typ(node.typ)
expr_type_sym := g.table.get_type_symbol(node.expr_type) expr_type_sym := g.table.get_type_symbol(node.expr_type)
if expr_type_sym.kind == .sum_type { if expr_type_sym.kind == .sum_type {
/*
g.write('/* as */ *($styp*)') g.write('/* as */ *($styp*)')
g.expr(node.expr) g.expr(node.expr)
g.write('.obj') g.write('.obj')
/* */
g.write('/* as */ *($styp*)__as_cast(') g.write('/* as */ *($styp*)__as_cast(')
g.expr(node.expr) g.expr(node.expr)
g.write('.obj, ') g.write('.obj, ')
g.expr(node.expr) g.expr(node.expr)
g.write('.typ, /*expected:*/$node.typ)') g.write('.typ, /*expected:*/$node.typ)')
*/
} }
} }