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

make function arguments immutable by default

This commit is contained in:
Alexander Medvednikov 2019-08-07 08:19:27 +02:00
parent 06b8bd9382
commit 34e0b164eb
22 changed files with 107 additions and 83 deletions

View File

@ -1,6 +1,10 @@
## V 0.1.18
- Map initialization syntax: `m := { foo: bar, baz: kek }`
- `libcurl` dependency was removed from `http` module.
- All function arguments are now immutable by default (previously they could be
modifed inside the function).
## V 0.1.17

View File

@ -512,8 +512,8 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
// Normal function => just its name, method => TYPE_FN.name
mut fn_name := f.name
if f.is_method {
receiver_type = receiver_type.replace('*', '')
fn_name = '${receiver_type}_${f.name}'
fn_name = receiver_type.replace('*', '') + '_' + f.name
//fn_name = '${receiver_type}_${f.name}'
}
// Generate tmp struct with args
arg_struct_name := 'thread_arg_$fn_name'

View File

@ -115,8 +115,8 @@ fn is_js_prim(typ string) bool {
typ == 'i8' || typ == 'i16' || typ == 'i32' || typ == 'i64'
}
fn (p mut Parser) decode_array(typ string) string {
typ = typ.replace('array_', '')
fn (p mut Parser) decode_array(array_type string) string {
typ := array_type.replace('array_', '')
t := p.table.find_type(typ)
fn_name := js_dec_name(typ)
// If we have `[]Profile`, have to register a Profile en(de)coder first
@ -149,8 +149,8 @@ fn js_dec_name(typ string) string {
return name
}
fn (p &Parser) encode_array(typ string) string {
typ = typ.replace('array_', '')
fn (p &Parser) encode_array(array_type string) string {
typ := array_type.replace('array_', '')
fn_name := js_enc_name(typ)
return '
o = cJSON_CreateArray();

View File

@ -669,7 +669,7 @@ fn (v V) run_compiled_executable_and_exit() {
exit(0)
}
fn (c &V) cc_windows_cross() {
fn (c mut V) cc_windows_cross() {
if !c.out_name.ends_with('.exe') {
c.out_name = c.out_name + '.exe'
}

View File

@ -1192,7 +1192,8 @@ fn (p mut Parser) statement(add_semi bool) string {
fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
p.log('assign_statement() name=$v.name tok=')
tok := p.tok
if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
//if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
if !v.is_mut && !p.pref.translated && !v.is_global{
p.error('`$v.name` is immutable')
}
if !v.is_changed {
@ -1799,7 +1800,8 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
return method.typ
}
fn (p mut Parser) index_expr(typ string, fn_ph int) string {
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
mut typ := typ_
// a[0]
v := p.expr_var
//if p.fileis('fn_test.v') {
@ -2361,8 +2363,8 @@ fn (p mut Parser) char_expr() {
}
fn format_str(str string) string {
str = str.replace('"', '\\"')
fn format_str(_str string) string {
mut str := _str.replace('"', '\\"')
$if windows {
str = str.replace('\r\n', '\\n')
}
@ -3298,10 +3300,12 @@ fn (p mut Parser) go_statement() {
fn (p mut Parser) register_var(v Var) {
if v.line_nr == 0 {
v.line_nr = p.scanner.line_nr
}
//v.line_nr = p.scanner.line_nr
p.cur_fn.register_var({ v | line_nr: p.scanner.line_nr })
} else {
p.cur_fn.register_var(v)
}
}
// user:=jsdecode(User, user_json_string)
fn (p mut Parser) js_decode() string {

View File

@ -550,7 +550,7 @@ fn (s mut Scanner) scan() ScanRes {
fn (s &Scanner) error(msg string) {
file := s.file_path.all_after('/')
println('$file:${s.line_nr + 1} panic: $msg')
println('$file:${s.line_nr + 1} $msg')
exit(1)
}

View File

@ -242,7 +242,8 @@ fn (t mut Table) register_fn(new_fn Fn) {
t.fns[new_fn.name] = new_fn
}
fn (table &Table) known_type(typ string) bool {
fn (table &Table) known_type(typ_ string) bool {
mut typ := typ_
// 'byte*' => look up 'byte', but don't mess up fns
if typ.ends_with('*') && !typ.contains(' ') {
typ = typ.left(typ.len - 1)
@ -441,7 +442,8 @@ fn (p &Parser) find_type(name string) *Type {
return typ
}
fn (t &Table) find_type(name string) *Type {
fn (t &Table) find_type(name_ string) *Type {
mut name := name_
if name.ends_with('*') && !name.contains(' ') {
name = name.left(name.len - 1)
}
@ -454,7 +456,9 @@ fn (t &Table) find_type(name string) *Type {
return &Type{}
}
fn (p mut Parser) _check_types(got, expected string, throw bool) bool {
fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool {
mut got := got_
mut expected := expected_
p.log('check types got="$got" exp="$expected" ')
if p.pref.translated {
return true
@ -582,8 +586,7 @@ fn (p mut Parser) satisfies_interface(interface_name, _typ string, throw bool) b
fn type_default(typ string) string {
if typ.starts_with('array_') {
typ = typ.right(6)
return 'new_array(0, 1, sizeof($typ))'
return 'new_array(0, 1, sizeof( ${typ.right(6)} ))'
}
// Always set pointers to 0
if typ.ends_with('*') {
@ -776,8 +779,8 @@ fn (p mut Parser) typ_to_fmt(typ string, level int) string {
return ''
}
fn is_compile_time_const(s string) bool {
s = s.trim_space()
fn is_compile_time_const(s_ string) bool {
s := s_.trim_space()
if s == '' {
return false
}

View File

@ -7,7 +7,8 @@ module main
import strings
// fmt helpers
fn (scanner mut Scanner) fgen(s string) {
fn (scanner mut Scanner) fgen(s_ string) {
mut s := s_
if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s
}
@ -15,7 +16,8 @@ fn (scanner mut Scanner) fgen(s string) {
scanner.fmt_line_empty = false
}
fn (scanner mut Scanner) fgenln(s string) {
fn (scanner mut Scanner) fgenln(s_ string) {
mut s := s_
if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s
}

View File

@ -8,7 +8,7 @@ module cipher
// xor_bytes xors the bytes in a and b. The destination should have enough
// space, otherwise xor_bytes will panic. Returns the number of bytes xor'd.
pub fn xor_bytes(dst, a, b []byte) int {
pub fn xor_bytes(dst mut []byte, a, b []byte) int {
mut n := a.len
if b.len < n {
n = b.len
@ -17,13 +17,13 @@ pub fn xor_bytes(dst, a, b []byte) int {
return 0
}
safe_xor_bytes(dst, a, b, n)
safe_xor_bytes(mut dst, a, b, n)
return n
}
// n needs to be smaller or equal than the length of a and b.
pub fn safe_xor_bytes(dst, a, b []byte, n int) {
pub fn safe_xor_bytes(dst mut []byte, a, b []byte, n int) {
for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i]
}
@ -32,5 +32,5 @@ pub fn safe_xor_bytes(dst, a, b []byte, n int) {
// fast_xor_words XORs multiples of 4 or 8 bytes (depending on architecture.)
// The slice arguments a and b are assumed to be of equal length.
pub fn xor_words(dst, a, b []byte) {
safe_xor_bytes(dst, a, b, b.len)
safe_xor_bytes(mut dst, a, b, b.len)
}

View File

@ -55,7 +55,7 @@ pub fn new() *Digest {
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
pub fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
@ -106,8 +106,8 @@ pub fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 1 + 63 + 8]
tmp[0] = 0x80
pad := (55 - int(d.len)) % 64 // calculate number of padding bytes
binary.little_endian_put_u64(tmp.right(1+pad), u64(d.len<<u64(3))) // append length in bits
d.write(tmp.left(1+pad+8))
binary.little_endian_put_u64(mut tmp.right(1+pad), u64(d.len<<u64(3))) // append length in bits
d.write(mut tmp.left(1+pad+8))
// The previous write ensures that a whole number of
// blocks (i.e. a multiple of 64 bytes) have been hashed.
@ -117,10 +117,10 @@ pub fn (d mut Digest) checksum() []byte {
digest := [byte(0); Size]
binary.little_endian_put_u32(digest, d.s[0])
binary.little_endian_put_u32(digest.right(4), d.s[1])
binary.little_endian_put_u32(digest.right(8), d.s[2])
binary.little_endian_put_u32(digest.right(12), d.s[3])
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest.right(4), d.s[1])
binary.little_endian_put_u32(mut digest.right(8), d.s[2])
binary.little_endian_put_u32(mut digest.right(12), d.s[3])
return digest
}

View File

@ -13,7 +13,7 @@ import (
encoding.binary
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
// load state
mut a := dig.s[0]
mut b := dig.s[1]

View File

@ -58,7 +58,7 @@ pub fn new() &Digest {
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
pub fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
@ -108,9 +108,9 @@ fn (d mut Digest) checksum() []byte {
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
d.write(mut tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
d.write(mut tmp.left(64+56-int(len)%64))
}
// Length in bits.

View File

@ -17,7 +17,7 @@ const (
_K3 = 0xCA62C1D6
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u32(0); 16]
mut h0 := dig.h[0]
mut h1 := dig.h[1]

View File

@ -51,7 +51,7 @@ mut:
is224 bool // mark if this digest is SHA-224
}
fn (d &Digest) reset() {
fn (d mut Digest) reset() {
d.h = [u32(0); 8]
d.x = [byte(0); Chunk]
if !d.is224 {
@ -92,7 +92,7 @@ pub fn new224() *Digest {
return d
}
fn (d mut Digest) write(p []byte) ?int {
fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
@ -145,14 +145,14 @@ fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 64]
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
d.write(mut tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
d.write(mut tmp.left(64+56-int(len)%64))
}
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, len)
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp.left(8))
if d.nx != 0 {
@ -161,13 +161,13 @@ fn (d mut Digest) checksum() []byte {
digest := [byte(0); Size]
binary.big_endian_put_u32(digest, d.h[0])
binary.big_endian_put_u32(digest.right(4), d.h[1])
binary.big_endian_put_u32(digest.right(8), d.h[2])
binary.big_endian_put_u32(digest.right(12), d.h[3])
binary.big_endian_put_u32(digest.right(16), d.h[4])
binary.big_endian_put_u32(digest.right(20), d.h[5])
binary.big_endian_put_u32(digest.right(24), d.h[6])
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest.right(4), d.h[1])
binary.big_endian_put_u32(mut digest.right(8), d.h[2])
binary.big_endian_put_u32(mut digest.right(12), d.h[3])
binary.big_endian_put_u32(mut digest.right(16), d.h[4])
binary.big_endian_put_u32(mut digest.right(20), d.h[5])
binary.big_endian_put_u32(mut digest.right(24), d.h[6])
if !d.is224 {
binary.big_endian_put_u32(digest.right(28), d.h[7])
}

View File

@ -80,7 +80,7 @@ const (
]
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u32(0); 64]
mut h0 := dig.h[0]

View File

@ -146,14 +146,15 @@ fn new384() *Digest {
return _new(crypto.Hash.SHA384)
}
fn (d mut Digest) write(p []byte) ?int {
fn (d mut Digest) write(p_ []byte) ?int {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
block(d, d.x)
block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
@ -164,7 +165,7 @@ fn (d mut Digest) write(p []byte) ?int {
}
if p.len >= Chunk {
n := p.len &~ (Chunk - 1)
block(d, p.left(n))
block(mut d, p.left(n))
if n >= p.len {
p = []byte
} else {
@ -217,8 +218,8 @@ fn (d mut Digest) checksum() []byte {
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(tmp.right(8), len)
binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(mut tmp.right(8), len)
d.write(tmp.left(16))
if d.nx != 0 {
@ -227,15 +228,15 @@ fn (d mut Digest) checksum() []byte {
mut digest := [byte(0); Size]
binary.big_endian_put_u64(digest, d.h[0])
binary.big_endian_put_u64(digest.right(8), d.h[1])
binary.big_endian_put_u64(digest.right(16), d.h[2])
binary.big_endian_put_u64(digest.right(24), d.h[3])
binary.big_endian_put_u64(digest.right(32), d.h[4])
binary.big_endian_put_u64(digest.right(40), d.h[5])
binary.big_endian_put_u64(mut digest, d.h[0])
binary.big_endian_put_u64(mut digest.right(8), d.h[1])
binary.big_endian_put_u64(mut digest.right(16), d.h[2])
binary.big_endian_put_u64(mut digest.right(24), d.h[3])
binary.big_endian_put_u64(mut digest.right(32), d.h[4])
binary.big_endian_put_u64(mut digest.right(40), d.h[5])
if d.function != crypto.Hash.SHA384 {
binary.big_endian_put_u64(digest.right(48), d.h[6])
binary.big_endian_put_u64(digest.right(56), d.h[7])
binary.big_endian_put_u64(mut digest.right(48), d.h[6])
binary.big_endian_put_u64(mut digest.right(56), d.h[7])
}
return digest
@ -278,10 +279,10 @@ pub fn sum512_256(data []byte) []byte {
return sum256
}
fn block(dig &Digest, p []byte) {
fn block(dig mut Digest, p []byte) {
// For now just use block_generic until we have specific
// architecture optimized versions
block_generic(dig, p)
block_generic(mut dig, p)
}
pub fn (d &Digest) size() int {

View File

@ -94,7 +94,7 @@ const(
]
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u64(0); 80]
mut h0 := dig.h[0]

View File

@ -12,7 +12,7 @@ pub fn little_endian_endian_u16(b []byte) u16 {
}
pub fn little_endian_put_u16(b []byte, v u16) {
pub fn little_endian_put_u16(b mut []byte, v u16) {
_ := b[1] // bounds check
b[0] = byte(v)
b[1] = byte(v >> u16(8))
@ -23,7 +23,7 @@ pub fn little_endian_u32(b []byte) u32 {
return u32(b[0]) | u32(u32(b[1])<<u32(8)) | u32(u32(b[2])<<u32(16)) | u32(u32(b[3])<<u32(24))
}
pub fn little_endian_put_u32(b []byte, v u32) {
pub fn little_endian_put_u32(b mut []byte, v u32) {
_ := b[3] // bounds check
b[0] = byte(v)
b[1] = byte(v >> u32(8))
@ -31,13 +31,13 @@ pub fn little_endian_put_u32(b []byte, v u32) {
b[3] = byte(v >> u32(24))
}
pub fn little_endian_u64(b []byte) u64 {
pub fn little_endian_u64(b mut []byte) u64 {
_ := b[7] // bounds check
return u64(b[0]) | u64(u64(b[1])<<u64(8)) | u64(u64(b[2])<<u64(16)) | u64(u64(b[3])<<u64(24)) |
u64(u64(b[4])<<u64(32)) | u64(u64(b[5])<<u64(40)) | u64(u64(b[6])<<u64(48)) | u64(u64(b[7])<<u64(56))
}
pub fn little_endian_put_u64(b []byte, v u64) {
pub fn little_endian_put_u64(b mut []byte, v u64) {
_ := b[7] // bounds check
b[0] = byte(v)
b[1] = byte(v >> u64(8))
@ -55,7 +55,7 @@ pub fn big_endian_u16(b []byte) u16 {
return u16(b[1]) | u16(u16(b[0])<<u16(8))
}
pub fn big_endian_put_u16(b []byte, v u16) {
pub fn big_endian_put_u16(b mut []byte, v u16) {
_ := b[1] // bounds check
b[0] = byte(v >> u16(8))
b[1] = byte(v)
@ -66,7 +66,7 @@ pub fn big_endian_u32(b []byte) u32 {
return u32(b[3]) | u32(u32(b[2])<<u32(8)) | u32(u32(b[1])<<u32(16)) | u32(u32(b[0])<<u32(24))
}
pub fn big_endian_put_u32(b []byte, v u32) {
pub fn big_endian_put_u32(b mut []byte, v u32) {
_ := b[3] // bounds check
b[0] = byte(v >> u32(24))
b[1] = byte(v >> u32(16))
@ -80,7 +80,7 @@ pub fn big_endian_u64(b []byte) u64 {
u64(u64(b[3])<<u64(32)) | u64(u64(b[2])<<u64(40)) | u64(u64(b[1])<<u64(48)) | u64(u64(b[0])<<u64(56))
}
pub fn big_endian_put_u64(b []byte, v u64) {
pub fn big_endian_put_u64(b mut []byte, v u64) {
_ := b[7] // bounds check
b[0] = byte(v >> u64(56))
b[1] = byte(v >> u64(48))

View File

@ -179,7 +179,9 @@ pub fn gen_buffer() u32 {
return vbo
}
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, stride int, ptr int) {
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride int, _ptr int) {
mut stride := _stride
mut ptr := _ptr
if typ == GL_FLOAT {
stride *= sizeof(f32)
ptr *= sizeof(f32)

View File

@ -134,6 +134,7 @@ pub fn exp2(a f64) f64 {
// factorial calculates the factorial of the provided value.
// TODO bring back once multiple value functions are implemented
/*
fn recursive_product( n int, current_number_ptr &int) int{
mut m := n / 2
if (m == 0){
@ -174,6 +175,7 @@ pub fn factorial(n int) i64 {
}
return i64((r << shift))
}
*/
// floor returns the nearest integer lower or equal of the provided value.
pub fn floor(a f64) f64 {
@ -191,7 +193,9 @@ pub fn gamma(a f64) f64 {
}
// gcd calculates greatest common (positive) divisor (or zero if a and b are both zero).
pub fn gcd(a, b i64) i64 {
pub fn gcd(a_, b_ i64) i64 {
mut a := a_
mut b := b_
if a < 0 {
a = -a
}

View File

@ -27,11 +27,13 @@ fn test_digits() {
assert negative_digits[2] == -1
}
/*
fn test_factorial() {
assert math.factorial(12) == 479001600
assert math.factorial(5) == 120
assert math.factorial(0) == 1
}
*/
fn test_erf() {
assert math.erf(0) == 0

View File

@ -142,7 +142,8 @@ pub fn path_unescape(s string) ?string {
// unescape unescapes a string; the mode specifies
// which section of the URL string is being unescaped.
fn unescape(s string, mode EncodingMode) ?string {
fn unescape(s_ string, mode EncodingMode) ?string {
mut s := s_
// Count %, check that they're well-formed.
mut n := 0
mut has_plus := false
@ -628,8 +629,9 @@ fn parse_host(host string) ?string {
h := unescape(host, .encode_host) or {
return err
}
host = h
return host
return h
//host = h
//return host
}
// set_path sets the path and raw_path fields of the URL based on the provided
@ -640,7 +642,7 @@ fn parse_host(host string) ?string {
// - set_path('/foo%2fbar') will set path='/foo/bar' and raw_path='/foo%2fbar'
// set_path will return an error only if the provided path contains an invalid
// escaping.
fn (u &URL) set_path(p string) ?bool {
fn (u mut URL) set_path(p string) ?bool {
path := unescape(p, .encode_path) or {
return error(err)
}