Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: always free the allocated consts, in the generated _vcleanup() function #20128

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions cmd/tools/test_if_v_test_system_works.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ fn get_vexe_path() string {
fn new_tdir() string {
dir := os.join_path(os.vtmp_dir(), rand.ulid())
os.rmdir_all(dir) or {}
eprintln('... creating tdir: ${tdir}')
os.mkdir_all(dir) or { panic(err) }
C.atexit(cleanup_tdir)
return dir
}

fn cleanup_tdir() {
println('... removing tdir: ${tdir}')
eprintln('... removing tdir: ${tdir}')
os.rmdir_all(tdir) or { eprintln(err) }
}

Expand Down Expand Up @@ -61,27 +62,31 @@ fn (result MyResult) matches(gpattern string) MyResult {
fn create_test(tname string, tcontent string) !string {
tpath := os.join_path(tdir, tname)
os.write_file(tpath, tcontent)!
eprintln('>>>>>>>> tpath: ${tpath} | tcontent: ${tcontent}')
$if trace_create_test ? {
eprintln('>>>>>>>> tpath: ${tpath} | tcontent:')
eprintln(tcontent)
eprintln('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
}
return os.quoted_path(tpath)
}

fn check_assert_continues_works() ! {
os.chdir(tdir)!
create_test('assert_continues_option_works_test.v', 'fn test_fail1() { assert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() { assert false }')!
result := check_fail('${vexe} -assert continues assert_continues_option_works_test.v')
result.has('assert_continues_option_works_test.v:1: fn test_fail1')
create_test('assert_continues_option_works_test.v', 'fn test_fail1() {\nassert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() {\n assert false\n}\n')!
result := check_fail('${vexe} -g -assert continues assert_continues_option_works_test.v')
result.has('assert_continues_option_works_test.v:2: fn test_fail1')
result.has('assert_continues_option_works_test.v:3: fn test_fail1')
result.has('assert_continues_option_works_test.v:5: fn test_fail2')
result.has('assert_continues_option_works_test.v:4: fn test_fail1')
result.has('assert_continues_option_works_test.v:7: fn test_fail2')
result.has('> assert 2 == 4').has('> assert 2 == 1').has('> assert 2 == 0')
// Check if a test function, tagged with [assert_continues], has the same behaviour, without needing additional options
create_test('assert_continues_tag_works_test.v', '[assert_continues]fn test_fail1() { assert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() { assert false\n assert false }')!
tag_res := check_fail('${vexe} assert_continues_tag_works_test.v')
tag_res.has('assert_continues_tag_works_test.v:1: fn test_fail1')
// Check if a test function, tagged with @[assert_continues], has the same behaviour, without needing additional options
create_test('assert_continues_tag_works_test.v', '@[assert_continues]fn test_fail1() {\nassert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() {\n assert false\n assert false\n}\n')!
tag_res := check_fail('${vexe} -g assert_continues_tag_works_test.v')
tag_res.has('assert_continues_tag_works_test.v:2: fn test_fail1')
tag_res.has('assert_continues_tag_works_test.v:3: fn test_fail1')
tag_res.has('assert_continues_tag_works_test.v:5: fn test_fail2')
if tag_res.contains('assert_continues_tag_works_test.v:6: fn test_fail2') {
tag_res.has('assert_continues_tag_works_test.v:4: fn test_fail1')
tag_res.has('assert_continues_tag_works_test.v:7: fn test_fail2')
if tag_res.contains('assert_continues_tag_works_test.v:8: fn test_fail2') {
exit(1)
}
}
Expand Down Expand Up @@ -111,22 +116,22 @@ fn main() {
os.chdir(os.wd_at_startup) or {}
}
println('> vroot: ${vroot} | vexe: ${vexe} | tdir: ${tdir}')
ok_fpath := create_test('a_single_ok_test.v', 'fn test_ok(){ assert true }')!
if check_ok('${vexe} ${ok_fpath}') != '' {
ok_fpath := create_test('a_single_ok_test.v', 'fn test_ok(){\n assert true \n}\n')!
if check_ok('${vexe} -g ${ok_fpath}') != '' {
exit(1)
}
check_ok('${vexe} test ${ok_fpath}').matches('*OK*a_single_ok_test.v*')
check_ok('${vexe} test "${tdir}"').matches('*OK*a_single_ok_test.v*')
check_ok('${vexe} -g test ${ok_fpath}').matches('*OK*a_single_ok_test.v*')
check_ok('${vexe} -g test "${tdir}"').matches('*OK*a_single_ok_test.v*')
//
fail_fpath := create_test('a_single_failing_test.v', 'fn test_fail(){ assert 1 == 2 }')!
check_fail('${vexe} ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:1: fn test_fail')
check_fail('${vexe} test ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:1: fn test_fail')
check_fail('${vexe} test "${tdir}"').has('> assert 1 == 2')
fail_fpath := create_test('a_single_failing_test.v', 'fn test_fail(){\n assert 1 == 2 \n}\n')!
check_fail('${vexe} -g ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:2: fn test_fail')
check_fail('${vexe} -g test ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:2: fn test_fail')
check_fail('${vexe} -g test "${tdir}"').has('> assert 1 == 2')
rel_dir := os.join_path(tdir, rand.ulid())
os.mkdir(rel_dir)!
os.chdir(rel_dir)!
relative_path := '..' + os.path_separator + 'a_single_ok_test.v'
check_ok('${vexe} test ${os.quoted_path(relative_path)}').has('OK').has('a_single_ok_test.v')
check_ok('${vexe} -g test ${os.quoted_path(relative_path)}').has('OK').has('a_single_ok_test.v')
//
check_assert_continues_works()!
println('> all done')
Expand Down
6 changes: 6 additions & 0 deletions cmd/tools/vtest-all.v
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ fn get_all_commands() []Command {
rmfile: 'str_array'
}
}
res << Command{
line: '${vexe} run cmd/tools/test_if_v_test_system_works.v'
okmsg: 'The V test system works'
runcmd: .execute
contains: '> all done'
}
res << Command{
line: '${vexe} ${vargs} -progress test-cleancode'
okmsg: 'All .v files are invariant when processed with `v fmt`'
Expand Down
17 changes: 16 additions & 1 deletion vlib/net/http/request.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@ pub mut:
on_finish RequestFinishFn = unsafe { nil }
}

fn (mut req Request) free() {
// free frees the memory allocated for the request
@[unsafe]
pub fn (mut req Request) free() {
unsafe { req.header.free() }
unsafe { req.host.free() }
// TODO: make the cookies stored in a cookie jar,
// that owns them, so that they can be used by many requests.
// TODO: `unsafe { req.cookie.free() }` is a cgen error; it should be a checker one instead, since cookie is a method...
unsafe { req.cookies.free() }
unsafe { req.data.free() }
unsafe { req.url.free() }
// TODO: the user agent can be shared between many requests too, just like verify, cert and cert_key
unsafe { req.user_agent.free() }
unsafe { req.verify.free() }
unsafe { req.cert.free() }
unsafe { req.cert_key.free() }
// Note: proxy is not owned by the request; it is intended to be used for many requests
}

// add_header adds the key and value of an HTTP request header
Expand Down
7 changes: 6 additions & 1 deletion vlib/net/http/response.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ pub mut:
http_version string
}

fn (mut resp Response) free() {
// free frees the memory allocated for the http response
@[unsafe]
pub fn (mut resp Response) free() {
unsafe { resp.body.free() }
unsafe { resp.header.free() }
unsafe { resp.status_msg.free() }
unsafe { resp.http_version.free() }
}

// Formats resp to bytes suitable for HTTP response transmission
Expand Down
Loading
Loading