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

don't allow calling private functions/methods

This commit is contained in:
Alexander Medvednikov 2019-06-26 12:56:49 +02:00
parent 2a3cf0bec9
commit c860bac7bf
8 changed files with 39 additions and 28 deletions

View File

@ -33,7 +33,7 @@ mut:
fn new_cgen(out_name_c string) *CGen { fn new_cgen(out_name_c string) *CGen {
gen := &CGen { gen := &CGen {
out_path: '$TmpPath/$out_name_c' out_path: '$TmpPath/$out_name_c'
out: os.create_file('$TmpPath/$out_name_c') out: os.create('$TmpPath/$out_name_c')
} }
return gen return gen
} }

View File

@ -23,7 +23,7 @@ mut:
name string name string
is_c bool is_c bool
receiver_typ string receiver_typ string
is_private bool is_public bool
is_method bool is_method bool
returns_error bool returns_error bool
is_decl bool // type myfn fn(int, int) is_decl bool // type myfn fn(int, int)
@ -94,11 +94,12 @@ fn (p mut Parser) is_sig() bool {
(p.file_path.contains(TmpPath)) (p.file_path.contains(TmpPath))
} }
fn new_fn(pkg string) *Fn { fn new_fn(pkg string, is_public bool) *Fn {
mut f := &Fn { mut f := &Fn {
pkg: pkg pkg: pkg
local_vars: [Var{} local_vars: [Var{}
; MaxLocalVars] ; MaxLocalVars]
is_public: is_public
} }
return f return f
} }
@ -112,7 +113,7 @@ fn (p mut Parser) fn_decl() {
} }
p.returns = false p.returns = false
p.next() p.next()
mut f := new_fn(p.pkg) mut f := new_fn(p.pkg, is_pub)
// Method receiver // Method receiver
mut receiver_typ := '' mut receiver_typ := ''
if p.tok == LPAR { if p.tok == LPAR {
@ -493,6 +494,9 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
} }
fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type string) { fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type string) {
if !f.is_public && !f.is_c && f.pkg != p.pkg && f.pkg != 'builtin' {
p.error('$p.run function `$f.name` is private $f.is_public')
}
p.calling_c = f.is_c p.calling_c = f.is_c
is_print := p.is_prod &&// Hide prints only in prod is_print := p.is_prod &&// Hide prints only in prod
!p.is_test && !p.is_test &&

View File

@ -515,7 +515,7 @@ mut args := ''
'/usr/lib/x86_64-linux-gnu/crtn.o') '/usr/lib/x86_64-linux-gnu/crtn.o')
println(ress) println(ress)
if ress.contains('error:') { if ress.contains('error:') {
os.exit(1) exit(1)
} }
println('linux cross compilation done. resulting binary: "$c.out_name"') println('linux cross compilation done. resulting binary: "$c.out_name"')
} }

View File

@ -511,9 +511,6 @@ fn (p mut Parser) struct_decl() {
} }
// `pub` access mod // `pub` access mod
access_mod := if is_pub{PUBLIC} else { PRIVATE} access_mod := if is_pub{PUBLIC} else { PRIVATE}
if typ.name == 'Userf' {
println('$field_name $access_mod mut=$is_mut')
}
p.fgen(' ') p.fgen(' ')
field_type := p.get_type() field_type := p.get_type()
is_atomic := p.tok == ATOMIC is_atomic := p.tok == ATOMIC
@ -644,8 +641,8 @@ fn (p mut Parser) check(expected Token) {
fn (p mut Parser) error(s string) { fn (p mut Parser) error(s string) {
// Dump all vars and types for debugging // Dump all vars and types for debugging
if false { if false {
file_types := os.create_file('$TmpPath/types') file_types := os.create('$TmpPath/types')
file_vars := os.create_file('$TmpPath/vars') file_vars := os.create('$TmpPath/vars')
// ////debug("ALL T", q.J(p.table.types)) // ////debug("ALL T", q.J(p.table.types))
// os.write_to_file('/var/tmp/lang.types', '')//pes(p.table.types)) // os.write_to_file('/var/tmp/lang.types', '')//pes(p.table.types))
// //debug("ALL V", q.J(p.table.vars)) // //debug("ALL V", q.J(p.table.vars))
@ -662,7 +659,7 @@ fn (p mut Parser) error(s string) {
if p.file_path.contains('v/compiler') || cur_path.contains('v/compiler') { if p.file_path.contains('v/compiler') || cur_path.contains('v/compiler') {
println('\n====================') println('\n====================')
println('It looks like you are building V. It is being frequently updated every day.' + println('It looks like you are building V. It is being frequently updated every day.' +
' Most likely there was a change that lead to this error. ') ' If you didn\'t modify the compiler\'s code, most likely there was a change that lead to this error. ')
println('Try to run `git pull`, that will most likely fix it.') println('Try to run `git pull`, that will most likely fix it.')
println('If `git pull` doesn\'t help, re-install V from source or download a precompiled' + println('If `git pull` doesn\'t help, re-install V from source or download a precompiled' +
' binary from https://vlang.io') ' binary from https://vlang.io')

View File

@ -239,7 +239,7 @@ fn (t Token) str() string {
fn (t Token) is_decl() bool { fn (t Token) is_decl() bool {
// TODO return t in [FUNC ,TIP, CONST, IMPORT_CONST ,AT ,EOF] // TODO return t in [FUNC ,TIP, CONST, IMPORT_CONST ,AT ,EOF]
return t == ENUM || t == INTERFACE || t == FUNC || t == STRUCT || t == TIP || return t == ENUM || t == INTERFACE || t == FUNC || t == STRUCT || t == TIP ||
t == CONST || t == IMPORT_CONST || t == AT || t == EOF t == CONST || t == IMPORT_CONST || t == AT || t == PUB || t == EOF
} }
const ( const (

18
os/os.v
View File

@ -21,6 +21,12 @@ import const (
SEEK_END SEEK_END
) )
fn C.getline(voidptr, voidptr, voidptr) int
fn C.ftell(fp voidptr) int
fn todo_remove(){}
fn init_os_args(argc int, _argv *byteptr) []string { fn init_os_args(argc int, _argv *byteptr) []string {
mut args := []string mut args := []string
# char** argv = (char**) _argv; # char** argv = (char**) _argv;
@ -38,8 +44,6 @@ fn parse_windows_cmd_line(cmd byteptr) []string {
return s.split(' ') return s.split(' ')
} }
fn C.ftell(fp voidptr) int
// read_file reads the file in `path` and returns the contents. // read_file reads the file in `path` and returns the contents.
pub fn read_file(path string) ?string { pub fn read_file(path string) ?string {
mut res := '' mut res := ''
@ -185,6 +189,7 @@ pub fn open_append(path string) File {
return create_file(path) return create_file(path)
} }
// TODO remove
fn create_file(file string) File { fn create_file(file string) File {
return create_file2(file, 'w') return create_file2(file, 'w')
} }
@ -227,7 +232,7 @@ fn (f File) write_at(data voidptr, size, pos int) {
C.fseek(f.cfile, 0, SEEK_END) C.fseek(f.cfile, 0, SEEK_END)
} }
fn (f File) appendln(s string) { pub fn (f File) appendln(s string) {
// C.fwrite(s.str, 1, s.len, f.cfile) // C.fwrite(s.str, 1, s.len, f.cfile)
// ss := s.clone() // ss := s.clone()
// TODO perf // TODO perf
@ -236,7 +241,7 @@ fn (f File) appendln(s string) {
C.fputs('\n', f.cfile) C.fputs('\n', f.cfile)
} }
fn (f File) close() { pub fn (f File) close() {
C.fclose(f.cfile) C.fclose(f.cfile)
} }
@ -319,10 +324,6 @@ pub fn getenv(key string) string {
return tos2(s) return tos2(s)
} }
fn exit(code int) {
C.exit(code)
}
// `file_exists` returns true if `path` exists. // `file_exists` returns true if `path` exists.
pub fn file_exists(path string) bool { pub fn file_exists(path string) bool {
// # return access( path.str, F_OK ) != -1 ; // # return access( path.str, F_OK ) != -1 ;
@ -397,7 +398,6 @@ pub fn filename(path string) string {
return path.all_after('/') return path.all_after('/')
} }
fn C.getline(voidptr, voidptr, voidptr) int
pub fn get_line() string { pub fn get_line() string {
max := 256 max := 256

View File

@ -28,13 +28,13 @@ fn chdir(path string) {
C.chdir(path.cstr()) C.chdir(path.cstr())
} }
fn getwd() string { pub fn getwd() string {
cwd := malloc(1024) cwd := malloc(1024)
# if (getcwd(cwd, 1024)) return tos2(cwd); # if (getcwd(cwd, 1024)) return tos2(cwd);
return '' return ''
} }
fn ls(path string) []string { pub fn ls(path string) []string {
mut res := []string mut res := []string
# DIR *dir; # DIR *dir;
# struct dirent *ent; # struct dirent *ent;

View File

@ -4,19 +4,29 @@
module rand module rand
#include <time.h> //#include <time.h>
struct C.time_t{}
fn C.rand() int
fn seed() { struct LOLLO {
}
fn kek() {
}
pub fn seed() {
# time_t t; # time_t t;
# srand((unsigned) time(&t)); # srand((unsigned) time(&t));
} }
fn next(max int) int { fn ffkek() {
}
pub fn next(max int) int {
r := 0 r := 0
# r = rand(); // TODO parser bug `rand` module name conflict # r = rand(); // TODO parser bug `rand` module name conflict
return r % max return r % max
} }
struct C.time_t{}
fn C.rand() int