examples: fix `v build-examples`

pull/5965/head
Delyan Angelov 2020-07-24 13:29:47 +03:00
parent 5acc437367
commit bfca55b87a
8 changed files with 38 additions and 39 deletions

View File

@ -2,7 +2,7 @@ import sync
import time import time
// Simulate expensive computing using sleep function // Simulate expensive computing using sleep function
fn expensive_computing(id, duration int, wg &sync.WaitGroup) { fn expensive_computing(id, duration int, mut wg sync.WaitGroup) {
println('Executing expensive computing task (${id})...') println('Executing expensive computing task (${id})...')
time.sleep_ms(duration) time.sleep_ms(duration)
println('Finish task ${id} on ${duration} ms') println('Finish task ${id} on ${duration} ms')
@ -10,11 +10,11 @@ fn expensive_computing(id, duration int, wg &sync.WaitGroup) {
} }
fn main() { fn main() {
wg := sync.new_waitgroup() mut wg := sync.new_waitgroup()
wg.add(3) wg.add(3)
go expensive_computing(1, 100, wg) go expensive_computing(1, 100, mut wg)
go expensive_computing(2, 500, wg) go expensive_computing(2, 500, mut wg)
go expensive_computing(3, 1000, wg) go expensive_computing(3, 1000, mut wg)
// Join all tasks // Join all tasks
wg.wait() wg.wait()
println('All jobs finished!') println('All jobs finished!')

View File

@ -2,7 +2,7 @@ import net.http
import sync import sync
import time import time
fn vlang_time(wg &sync.WaitGroup) ?string { fn vlang_time(mut wg sync.WaitGroup) ?string {
start := time.ticks() start := time.ticks()
data := http.get('https://vlang.io/utc_now')? data := http.get('https://vlang.io/utc_now')?
finish := time.ticks() finish := time.ticks()
@ -12,7 +12,7 @@ fn vlang_time(wg &sync.WaitGroup) ?string {
return data.text return data.text
} }
fn remote_ip(wg &sync.WaitGroup) ?string { fn remote_ip(mut wg sync.WaitGroup) ?string {
start := time.ticks() start := time.ticks()
data := http.get('https://api.ipify.org')? data := http.get('https://api.ipify.org')?
finish := time.ticks() finish := time.ticks()
@ -23,10 +23,10 @@ fn remote_ip(wg &sync.WaitGroup) ?string {
} }
fn main() { fn main() {
wg := sync.new_waitgroup() mut wg := sync.new_waitgroup()
wg.add(2) wg.add(2)
// Run tasks async // Run tasks async
go vlang_time(wg) go vlang_time(mut wg)
go remote_ip(wg) go remote_ip(mut wg)
wg.wait() wg.wait()
} }

View File

@ -119,14 +119,11 @@ fn arr_position() []Position {
} }
fn main() { fn main() {
mut sys := &System {arr_momentum(), arr_position()}
sys := &System {arr_momentum(), arr_position()}
offsetmomentum(mut sys) offsetmomentum(mut sys)
println('${energy(sys):.9f}') //-0.169075164 println('${energy(sys):.9f}') //-0.169075164
for _ in 0..50000000 { for _ in 0..50_000_000 {
advance(mut sys, 0.01) advance(mut sys, 0.01)
} }
println('${energy(sys):.9f}') //-0.169059907 println('${energy(sys):.9f}') //-0.169059907
} }

View File

@ -46,5 +46,6 @@ fn callback(req picohttpparser.Request, mut res picohttpparser.Response) {
} }
fn main() { fn main() {
println('Starting webserver on http://127.0.0.1:8088/ ...')
picoev.new(8088, &callback).serve() picoev.new(8088, &callback).serve()
} }

View File

@ -128,7 +128,7 @@ const (
black = C.sfons_rgba(0, 0, 0, 255) black = C.sfons_rgba(0, 0, 0, 255)
) )
fn (state &AppState) render_font() { fn (mut state AppState) render_font() {
lh := 30 lh := 30
mut dy := lh mut dy := lh
if !state.inited { if !state.inited {

View File

@ -137,7 +137,7 @@ struct Game {
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') ) const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
[if showfps] [if showfps]
fn (game &Game) showfps() { fn (mut game Game) showfps() {
game.frame++ game.frame++
last_frame_ms := f64(game.frame_sw.elapsed().microseconds())/1000.0 last_frame_ms := f64(game.frame_sw.elapsed().microseconds())/1000.0
ticks := f64(game.second_sw.elapsed().microseconds())/1000.0 ticks := f64(game.second_sw.elapsed().microseconds())/1000.0
@ -149,7 +149,7 @@ fn (game &Game) showfps() {
} }
} }
fn frame(game &Game) { fn frame(mut game Game) {
game.frame_sw.restart() game.frame_sw.restart()
game.gg.begin() game.gg.begin()
game.draw_scene() game.draw_scene()
@ -301,7 +301,7 @@ fn (mut g Game) get_tetro() {
} }
// TODO mut // TODO mut
fn (g &Game) drop_tetro() { fn (mut g Game) drop_tetro() {
for i in 0..tetro_size{ for i in 0..tetro_size{
tetro := g.tetro[i] tetro := g.tetro[i]
x := tetro.x + g.pos_x x := tetro.x + g.pos_x

View File

@ -165,12 +165,13 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
for { for {
pret := req.parse_request(s, 100) pret := req.parse_request(s, 100)
if pret <= 0 && s.len > 0 { if pret <= 0 && s.len > 0 {
C.memmove(buf, s.str, s.len) unsafe { C.memmove(buf, s.str, s.len) }
p.idx[fd] = s.len p.idx[fd] = s.len
p.oidx[fd] = int(res.buf) - int(res.buf_start) p.oidx[fd] = int(res.buf) - int(res.buf_start)
break break
} }
if req.method.str[0]==`p` || req.method.str[0]==`P` || req.method.str[0]==`d` || req.method.str[0]==`D` { c0 := unsafe { req.method.str[0] }
if c0 ==`p` || c0 == `P` || c0 == `d` || c0 == `D` {
mut j := 0 mut j := 0
for { for {
if j == req.num_headers { if j == req.num_headers {
@ -238,7 +239,7 @@ pub fn new(port int, cb voidptr) &Picoev {
C.picoev_init(max_fds) C.picoev_init(max_fds)
loop := C.picoev_create_loop(max_timeout) loop := C.picoev_create_loop(max_timeout)
pv := &Picoev{ mut pv := &Picoev{
loop: loop loop: loop
cb: cb cb: cb
date: C.get_date() date: C.get_date()

View File

@ -9,7 +9,7 @@ pub mut:
buf byteptr buf byteptr
} }
[inline] [unsafe_fn] [inline]
fn (mut r Response) write_str(s string) { fn (mut r Response) write_str(s string) {
unsafe { unsafe {
C.memcpy(r.buf, s.str, s.len) C.memcpy(r.buf, s.str, s.len)