mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
all: update attributes to use new syntax
This commit is contained in:
parent
dd81cb98c6
commit
757929392e
739 changed files with 2982 additions and 2982 deletions
|
@ -16,7 +16,7 @@ const (
|
||||||
separation_distance = 5.0
|
separation_distance = 5.0
|
||||||
)
|
)
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn main() {
|
fn main() {
|
||||||
mut positions := [boids_count]Vector{}
|
mut positions := [boids_count]Vector{}
|
||||||
mut velocities := [boids_count]Vector{}
|
mut velocities := [boids_count]Vector{}
|
||||||
|
|
|
@ -40,7 +40,7 @@ mut:
|
||||||
nmaxs int // number of maximums to discard
|
nmaxs int // number of maximums to discard
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut result CmdResult) free() {
|
fn (mut result CmdResult) free() {
|
||||||
unsafe {
|
unsafe {
|
||||||
result.cmd.free()
|
result.cmd.free()
|
||||||
|
@ -52,7 +52,7 @@ fn (mut result CmdResult) free() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut context Context) free() {
|
fn (mut context Context) free() {
|
||||||
unsafe {
|
unsafe {
|
||||||
context.commands.free()
|
context.commands.free()
|
||||||
|
@ -75,7 +75,7 @@ mut:
|
||||||
nmaxs int // number of discarded slowest results
|
nmaxs int // number of discarded slowest results
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a Aints) free() {
|
fn (mut a Aints) free() {
|
||||||
unsafe { a.values.free() }
|
unsafe { a.values.free() }
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ fn cleanup_tdir() {
|
||||||
|
|
||||||
type MyResult = string
|
type MyResult = string
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn (result MyResult) fail(reason string) {
|
fn (result MyResult) fail(reason string) {
|
||||||
eprintln('> ${reason}, but it does not. Result:\n${result}')
|
eprintln('> ${reason}, but it does not. Result:\n${result}')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
|
@ -53,57 +53,57 @@ fn C.cJSON_Delete(voidptr)
|
||||||
|
|
||||||
fn C.cJSON_Print(voidptr) &u8
|
fn C.cJSON_Print(voidptr) &u8
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_object() &C.cJSON {
|
fn create_object() &C.cJSON {
|
||||||
return C.cJSON_CreateObject()
|
return C.cJSON_CreateObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_array() &C.cJSON {
|
fn create_array() &C.cJSON {
|
||||||
return C.cJSON_CreateArray()
|
return C.cJSON_CreateArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_string(val string) &C.cJSON {
|
fn create_string(val string) &C.cJSON {
|
||||||
return C.cJSON_CreateString(val.str)
|
return C.cJSON_CreateString(val.str)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_number(val f64) &C.cJSON {
|
fn create_number(val f64) &C.cJSON {
|
||||||
return C.cJSON_CreateNumber(val)
|
return C.cJSON_CreateNumber(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_bool(val bool) &C.cJSON {
|
fn create_bool(val bool) &C.cJSON {
|
||||||
return C.cJSON_CreateBool(val)
|
return C.cJSON_CreateBool(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_true() &C.cJSON {
|
fn create_true() &C.cJSON {
|
||||||
return C.cJSON_CreateTrue()
|
return C.cJSON_CreateTrue()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_false() &C.cJSON {
|
fn create_false() &C.cJSON {
|
||||||
return C.cJSON_CreateFalse()
|
return C.cJSON_CreateFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn create_null() &C.cJSON {
|
fn create_null() &C.cJSON {
|
||||||
return C.cJSON_CreateNull()
|
return C.cJSON_CreateNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn delete(b voidptr) {
|
fn delete(b voidptr) {
|
||||||
C.cJSON_Delete(b)
|
C.cJSON_Delete(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn add_item_to_object(obj &C.cJSON, key string, item &C.cJSON) {
|
fn add_item_to_object(obj &C.cJSON, key string, item &C.cJSON) {
|
||||||
C.cJSON_AddItemToObject(obj, key.str, item)
|
C.cJSON_AddItemToObject(obj, key.str, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn add_item_to_array(obj &C.cJSON, item &C.cJSON) {
|
fn add_item_to_array(obj &C.cJSON, item &C.cJSON) {
|
||||||
C.cJSON_AddItemToArray(obj, item)
|
C.cJSON_AddItemToArray(obj, item)
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,13 +150,13 @@ mut:
|
||||||
pub type Node = C.cJSON
|
pub type Node = C.cJSON
|
||||||
|
|
||||||
// create an object node
|
// create an object node
|
||||||
[inline]
|
@[inline]
|
||||||
fn new_object() &Node {
|
fn new_object() &Node {
|
||||||
return C.cJSON_CreateObject()
|
return C.cJSON_CreateObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
// add item to object node
|
// add item to object node
|
||||||
[inline]
|
@[inline]
|
||||||
fn (node &Node) add(key string, child &Node) {
|
fn (node &Node) add(key string, child &Node) {
|
||||||
if context.hide_names.len > 0 && key in context.hide_names {
|
if context.hide_names.len > 0 && key in context.hide_names {
|
||||||
return
|
return
|
||||||
|
@ -168,7 +168,7 @@ fn (node &Node) add(key string, child &Node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add item to object node
|
// add item to object node
|
||||||
[inline]
|
@[inline]
|
||||||
fn (node &Node) add_terse(key string, child &Node) {
|
fn (node &Node) add_terse(key string, child &Node) {
|
||||||
if context.hide_names.len > 0 && key in context.hide_names {
|
if context.hide_names.len > 0 && key in context.hide_names {
|
||||||
return
|
return
|
||||||
|
@ -177,13 +177,13 @@ fn (node &Node) add_terse(key string, child &Node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create an array node
|
// create an array node
|
||||||
[inline]
|
@[inline]
|
||||||
fn new_array() &Node {
|
fn new_array() &Node {
|
||||||
return C.cJSON_CreateArray()
|
return C.cJSON_CreateArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
// add item to array node
|
// add item to array node
|
||||||
[inline]
|
@[inline]
|
||||||
fn (node &Node) add_item(child &Node) {
|
fn (node &Node) add_item(child &Node) {
|
||||||
add_item_to_array(node, child)
|
add_item_to_array(node, child)
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,7 @@ div.products-table {
|
||||||
|
|
||||||
import vweb
|
import vweb
|
||||||
|
|
||||||
['/controller/auth'; post]
|
@['/controller/auth'; post]
|
||||||
pub fn (mut app App) controller_auth(username string, password string) vweb.Result {
|
pub fn (mut app App) controller_auth(username string, password string) vweb.Result {
|
||||||
response := app.service_auth(username, password) or {
|
response := app.service_auth(username, password) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -167,8 +167,8 @@ pub fn (mut app App) controller_auth(username string, password string) vweb.Resu
|
||||||
content: 'module main
|
content: 'module main
|
||||||
|
|
||||||
struct AuthRequestDto {
|
struct AuthRequestDto {
|
||||||
username string [nonull]
|
username string @[nonull]
|
||||||
password string [nonull]
|
password string @[nonull]
|
||||||
}
|
}
|
||||||
'
|
'
|
||||||
}
|
}
|
||||||
|
@ -399,7 +399,7 @@ import vweb
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
import json
|
import json
|
||||||
|
|
||||||
['/controller/products'; get]
|
@['/controller/products'; get]
|
||||||
pub fn (mut app App) controller_get_all_products() vweb.Result {
|
pub fn (mut app App) controller_get_all_products() vweb.Result {
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
|
||||||
|
@ -425,7 +425,7 @@ pub fn (mut app App) controller_get_all_products() vweb.Result {
|
||||||
// return app.text('response')
|
// return app.text('response')
|
||||||
}
|
}
|
||||||
|
|
||||||
['/controller/product/create'; post]
|
@['/controller/product/create'; post]
|
||||||
pub fn (mut app App) controller_create_product(product_name string) vweb.Result {
|
pub fn (mut app App) controller_create_product(product_name string) vweb.Result {
|
||||||
if product_name == '' {
|
if product_name == '' {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -461,12 +461,12 @@ pub fn (mut app App) controller_create_product(product_name string) vweb.Result
|
||||||
path: join_path(c.name, 'src', 'product_entities.v')
|
path: join_path(c.name, 'src', 'product_entities.v')
|
||||||
content: "module main
|
content: "module main
|
||||||
|
|
||||||
[table: 'products']
|
@[table: 'products']
|
||||||
struct Product {
|
struct Product {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
user_id int
|
user_id int
|
||||||
name string [nonull; sql_type: 'TEXT']
|
name string @[nonull; sql_type: 'TEXT']
|
||||||
created_at string [default: 'CURRENT_TIMESTAMP']
|
created_at string @[default: 'CURRENT_TIMESTAMP']
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
@ -562,7 +562,7 @@ pub fn get_product(token string) ![]User {
|
||||||
|
|
||||||
import vweb
|
import vweb
|
||||||
|
|
||||||
['/products'; get]
|
@['/products'; get]
|
||||||
pub fn (mut app App) products() !vweb.Result {
|
pub fn (mut app App) products() !vweb.Result {
|
||||||
token := app.get_cookie('token') or {
|
token := app.get_cookie('token') or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -586,7 +586,7 @@ import vweb
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
import json
|
import json
|
||||||
|
|
||||||
['/controller/users'; get]
|
@['/controller/users'; get]
|
||||||
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
// token := app.get_cookie('token') or { '' }
|
// token := app.get_cookie('token') or { '' }
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
@ -603,7 +603,7 @@ pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/controller/user'; get]
|
@['/controller/user'; get]
|
||||||
pub fn (mut app App) controller_get_user() vweb.Result {
|
pub fn (mut app App) controller_get_user() vweb.Result {
|
||||||
// token := app.get_cookie('token') or { '' }
|
// token := app.get_cookie('token') or { '' }
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
@ -629,7 +629,7 @@ pub fn (mut app App) controller_get_user() vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/controller/user/create'; post]
|
@['/controller/user/create'; post]
|
||||||
pub fn (mut app App) controller_create_user(username string, password string) vweb.Result {
|
pub fn (mut app App) controller_create_user(username string, password string) vweb.Result {
|
||||||
if username == '' {
|
if username == '' {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -652,14 +652,14 @@ pub fn (mut app App) controller_create_user(username string, password string) vw
|
||||||
path: join_path(c.name, 'src', 'user_entities.v')
|
path: join_path(c.name, 'src', 'user_entities.v')
|
||||||
content: "module main
|
content: "module main
|
||||||
|
|
||||||
[table: 'users']
|
@[table: 'users']
|
||||||
pub struct User {
|
pub struct User {
|
||||||
mut:
|
mut:
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
username string [nonull; sql_type: 'TEXT'; unique]
|
username string @[nonull; sql_type: 'TEXT'; unique]
|
||||||
password string [nonull; sql_type: 'TEXT']
|
password string @[nonull; sql_type: 'TEXT']
|
||||||
active bool
|
active bool
|
||||||
products []Product [fkey: 'user_id']
|
products []Product @[fkey: 'user_id']
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ fn clean_line_endings(s string) string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
struct CheckOutputParams {
|
struct CheckOutputParams {
|
||||||
program string = 'some/dir/main.v'
|
program string = 'some/dir/main.v'
|
||||||
cmd string = 'v doc'
|
cmd string = 'v doc'
|
||||||
|
|
|
@ -9,7 +9,7 @@ import v.token
|
||||||
import strings
|
import strings
|
||||||
import v.pref
|
import v.pref
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn slug(title string) string {
|
fn slug(title string) string {
|
||||||
return title.replace(' ', '-')
|
return title.replace(' ', '-')
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,9 @@ enum OutputType {
|
||||||
stdout
|
stdout
|
||||||
}
|
}
|
||||||
|
|
||||||
[heap]
|
@[heap]
|
||||||
struct VDoc {
|
struct VDoc {
|
||||||
cfg Config [required]
|
cfg Config @[required]
|
||||||
mut:
|
mut:
|
||||||
docs []doc.Doc
|
docs []doc.Doc
|
||||||
assets map[string]string
|
assets map[string]string
|
||||||
|
|
|
@ -351,7 +351,7 @@ fn get_compile_name_of_potential_v_project(file string) string {
|
||||||
return pfolder
|
return pfolder
|
||||||
}
|
}
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn verror(s string) {
|
fn verror(s string) {
|
||||||
util.verror('vfmt error', s)
|
util.verror('vfmt error', s)
|
||||||
}
|
}
|
||||||
|
|
|
@ -418,7 +418,7 @@ fn (upd VlsUpdater) error_details(err IError) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn (upd VlsUpdater) cli_error(err IError) {
|
fn (upd VlsUpdater) cli_error(err IError) {
|
||||||
match upd.output {
|
match upd.output {
|
||||||
.text {
|
.text {
|
||||||
|
|
|
@ -39,7 +39,7 @@ mut:
|
||||||
exec_err bool
|
exec_err bool
|
||||||
}
|
}
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
struct ErrorOptions {
|
struct ErrorOptions {
|
||||||
details string
|
details string
|
||||||
verbose bool // is used to only output the error message if the verbose setting is enabled.
|
verbose bool // is used to only output the error message if the verbose setting is enabled.
|
||||||
|
|
|
@ -64,7 +64,7 @@ struct VFileStat {
|
||||||
mtime i64
|
mtime i64
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut vfs VFileStat) free() {
|
fn (mut vfs VFileStat) free() {
|
||||||
unsafe { vfs.path.free() }
|
unsafe { vfs.path.free() }
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ mut:
|
||||||
only_watch []string // If not empty, *all* files that trigger updates, should match *at least one* of these s.match_glob() patterns. This is also triggered for vweb apps, to monitor for just *.v,*.js,*.css,*.html in vweb projects.
|
only_watch []string // If not empty, *all* files that trigger updates, should match *at least one* of these s.match_glob() patterns. This is also triggered for vweb apps, to monitor for just *.v,*.js,*.css,*.html in vweb projects.
|
||||||
}
|
}
|
||||||
|
|
||||||
[if debug_vwatch ?]
|
@[if debug_vwatch ?]
|
||||||
fn (mut context Context) elog(msg string) {
|
fn (mut context Context) elog(msg string) {
|
||||||
eprintln('> vwatch ${context.pid}, ${msg}')
|
eprintln('> vwatch ${context.pid}, ${msg}')
|
||||||
}
|
}
|
||||||
|
|
|
@ -235,9 +235,9 @@ fn (fdr Finder) str() string {
|
||||||
|
|
||||||
// Match is one result of the search_for_matches() process
|
// Match is one result of the search_for_matches() process
|
||||||
struct Match {
|
struct Match {
|
||||||
path string [required]
|
path string @[required]
|
||||||
line int [required]
|
line int @[required]
|
||||||
text string [required]
|
text string @[required]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mtc Match) show() {
|
fn (mtc Match) show() {
|
||||||
|
|
|
@ -200,7 +200,7 @@ enum Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utility functions
|
// Utility functions
|
||||||
[inline]
|
@[inline]
|
||||||
fn avg(a int, b int) int {
|
fn avg(a int, b int) int {
|
||||||
return (a + b) / 2
|
return (a + b) / 2
|
||||||
}
|
}
|
||||||
|
@ -357,7 +357,7 @@ fn (mut app App) new_game() {
|
||||||
app.new_random_tile()
|
app.new_random_tile()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) check_for_victory() {
|
fn (mut app App) check_for_victory() {
|
||||||
for y in 0 .. 4 {
|
for y in 0 .. 4 {
|
||||||
for x in 0 .. 4 {
|
for x in 0 .. 4 {
|
||||||
|
@ -370,7 +370,7 @@ fn (mut app App) check_for_victory() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) check_for_game_over() {
|
fn (mut app App) check_for_game_over() {
|
||||||
if app.board.is_game_over() {
|
if app.board.is_game_over() {
|
||||||
app.state = .over
|
app.state = .over
|
||||||
|
@ -547,7 +547,7 @@ fn (app &App) label_format(kind LabelKind) gx.TextCfg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) set_theme(idx int) {
|
fn (mut app App) set_theme(idx int) {
|
||||||
theme := themes[idx]
|
theme := themes[idx]
|
||||||
app.theme_idx = idx
|
app.theme_idx = idx
|
||||||
|
@ -761,12 +761,12 @@ fn (mut app App) handle_swipe() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) next_theme() {
|
fn (mut app App) next_theme() {
|
||||||
app.set_theme(if app.theme_idx == themes.len - 1 { 0 } else { app.theme_idx + 1 })
|
app.set_theme(if app.theme_idx == themes.len - 1 { 0 } else { app.theme_idx + 1 })
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) next_tile_format() {
|
fn (mut app App) next_tile_format() {
|
||||||
app.tile_format = unsafe { TileFormat(int(app.tile_format) + 1) }
|
app.tile_format = unsafe { TileFormat(int(app.tile_format) + 1) }
|
||||||
if app.tile_format == .end_ {
|
if app.tile_format == .end_ {
|
||||||
|
@ -774,7 +774,7 @@ fn (mut app App) next_tile_format() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) undo() {
|
fn (mut app App) undo() {
|
||||||
if app.undo.len > 0 {
|
if app.undo.len > 0 {
|
||||||
undo := app.undo.pop()
|
undo := app.undo.pop()
|
||||||
|
|
|
@ -75,7 +75,7 @@ fn (mut state BFState) find_matching_pairs() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn (state &BFState) panic_for_bracket(b1 rune, b2 rune) {
|
fn (state &BFState) panic_for_bracket(b1 rune, b2 rune) {
|
||||||
panic('unbalanced `${b1}` found, its target `${b2}` is not known; address: ${state.address}, pc: ${state.pc}')
|
panic('unbalanced `${b1}` found, its target `${b2}` is not known; address: ${state.address}, pc: ${state.pc}')
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ fn (mut state BFState) run() ? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn show_usage() {
|
fn show_usage() {
|
||||||
eprintln('you need to supply a brainfuck program/expression as a string argument,')
|
eprintln('you need to supply a brainfuck program/expression as a string argument,')
|
||||||
eprintln('or filename.b, if it is located in a file (note the `.b` extension).')
|
eprintln('or filename.b, if it is located in a file (note the `.b` extension).')
|
||||||
|
|
|
@ -2,12 +2,12 @@ module test_math
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
[export: 'square']
|
@[export: 'square']
|
||||||
fn calculate_square(i int) int {
|
fn calculate_square(i int) int {
|
||||||
return i * i
|
return i * i
|
||||||
}
|
}
|
||||||
|
|
||||||
[export: 'sqrt_of_sum_of_squares']
|
@[export: 'sqrt_of_sum_of_squares']
|
||||||
fn calculate_sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
fn calculate_sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
||||||
return math.sqrt(x * x + y * y)
|
return math.sqrt(x * x + y * y)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module test_print
|
module test_print
|
||||||
|
|
||||||
[export: 'foo']
|
@[export: 'foo']
|
||||||
fn show_foo(s &char) {
|
fn show_foo(s &char) {
|
||||||
println(unsafe { cstring_to_vstring(s) })
|
println(unsafe { cstring_to_vstring(s) })
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,12 @@ module test
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
[export: 'square']
|
@[export: 'square']
|
||||||
fn square(i int) int {
|
fn square(i int) int {
|
||||||
return i * i
|
return i * i
|
||||||
}
|
}
|
||||||
|
|
||||||
[export: 'sqrt_of_sum_of_squares']
|
@[export: 'sqrt_of_sum_of_squares']
|
||||||
fn sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
fn sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
||||||
return math.sqrt(x * x + y * y)
|
return math.sqrt(x * x + y * y)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,12 @@ module test
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
[export: 'square']
|
@[export: 'square']
|
||||||
fn square(i int) int {
|
fn square(i int) int {
|
||||||
return i * i
|
return i * i
|
||||||
}
|
}
|
||||||
|
|
||||||
[export: 'sqrt_of_sum_of_squares']
|
@[export: 'sqrt_of_sum_of_squares']
|
||||||
fn sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
fn sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
||||||
return math.sqrt(x * x + y * y)
|
return math.sqrt(x * x + y * y)
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ fn on_frame(mut app App) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotate a polygon round the centerpoint
|
// Rotate a polygon round the centerpoint
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
fn draw_convex_poly_rotate(mut ctx gg.Context, dpi_scale f32, points []f32, c gx.Color, angle f32) {
|
fn draw_convex_poly_rotate(mut ctx gg.Context, dpi_scale f32, points []f32, c gx.Color, angle f32) {
|
||||||
sa := math.sin(math.pi * angle / 180.0)
|
sa := math.sin(math.pi * angle / 180.0)
|
||||||
ca := math.cos(math.pi * angle / 180.0)
|
ca := math.cos(math.pi * angle / 180.0)
|
||||||
|
|
|
@ -13,7 +13,7 @@ fn my_print1() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2MB stack size
|
// 2MB stack size
|
||||||
[spawn_stack: 2097152]
|
@[spawn_stack: 2097152]
|
||||||
fn my_print2() {
|
fn my_print2() {
|
||||||
println('ahoj svet')
|
println('ahoj svet')
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,30 +35,30 @@ const (
|
||||||
pg_db = os.getenv_opt('PGDATABASE') or { 'test' }
|
pg_db = os.getenv_opt('PGDATABASE') or { 'test' }
|
||||||
)
|
)
|
||||||
|
|
||||||
[table: 'modules']
|
@[table: 'modules']
|
||||||
struct Module {
|
struct Module {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
name string
|
name string
|
||||||
nr_downloads int [sql: u64]
|
nr_downloads int @[sql: u64]
|
||||||
creator User
|
creator User
|
||||||
}
|
}
|
||||||
|
|
||||||
struct User {
|
struct User {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
age u32 [unique: 'user']
|
age u32 @[unique: 'user']
|
||||||
name string [sql: 'username'; sql_type: 'VARCHAR(200)'; unique]
|
name string @[sql: 'username'; sql_type: 'VARCHAR(200)'; unique]
|
||||||
is_customer bool [sql: 'abc'; unique: 'user']
|
is_customer bool @[sql: 'abc'; unique: 'user']
|
||||||
skipped_string string [skip]
|
skipped_string string @[skip]
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Parent {
|
struct Parent {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
name string
|
name string
|
||||||
children []Child [fkey: 'parent_id']
|
children []Child @[fkey: 'parent_id']
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Child {
|
struct Child {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
parent_id int
|
parent_id int
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ module library
|
||||||
// (Normally, the exported name is a V mangled version based on the module
|
// (Normally, the exported name is a V mangled version based on the module
|
||||||
// name followed by __, followed by the fn name, i.e. it would have been
|
// name followed by __, followed by the fn name, i.e. it would have been
|
||||||
// `library__add_1`, if not for the export: tag).
|
// `library__add_1`, if not for the export: tag).
|
||||||
[export: 'add_1']
|
@[export: 'add_1']
|
||||||
pub fn add_1(x int, y int) int {
|
pub fn add_1(x int, y int) int {
|
||||||
return my_private_function(x + y)
|
return my_private_function(x + y)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ module library
|
||||||
// (Normally, the exported name is a V mangled version based on the module
|
// (Normally, the exported name is a V mangled version based on the module
|
||||||
// name followed by __, followed by the fn name, i.e. it would have been
|
// name followed by __, followed by the fn name, i.e. it would have been
|
||||||
// `library__add_1`, if not for the export: tag).
|
// `library__add_1`, if not for the export: tag).
|
||||||
[export: 'add_1']
|
@[export: 'add_1']
|
||||||
pub fn add_1(x int, y int) int {
|
pub fn add_1(x int, y int) int {
|
||||||
return my_private_function(x + y)
|
return my_private_function(x + y)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ const (
|
||||||
filled_color = gx.blue
|
filled_color = gx.blue
|
||||||
)
|
)
|
||||||
|
|
||||||
[live]
|
@[live]
|
||||||
fn print_automaton(app &App) {
|
fn print_automaton(app &App) {
|
||||||
square_size := 18
|
square_size := 18
|
||||||
for y := 1; y < app.a.field.maxy; y++ {
|
for y := 1; y < app.a.field.maxy; y++ {
|
||||||
|
|
|
@ -17,7 +17,7 @@ fn new_a2d(maxx int, maxy int) &A2D {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (a &A2D) set(x int, y int, newval int) {
|
pub fn (a &A2D) set(x int, y int, newval int) {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut e := &int(0)
|
mut e := &int(0)
|
||||||
|
@ -26,7 +26,7 @@ pub fn (a &A2D) set(x int, y int, newval int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (a &A2D) get(x int, y int) int {
|
pub fn (a &A2D) get(x int, y int) int {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut e := &int(0)
|
mut e := &int(0)
|
||||||
|
@ -36,7 +36,7 @@ pub fn (a &A2D) get(x int, y int) int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (a &A2D) clear() {
|
pub fn (a &A2D) clear() {
|
||||||
for y := 0; y < a.maxy; y++ {
|
for y := 0; y < a.maxy; y++ {
|
||||||
for x := 0; x < a.maxx; x++ {
|
for x := 0; x < a.maxx; x++ {
|
||||||
|
|
|
@ -3,24 +3,24 @@ import rand
|
||||||
import net.http
|
import net.http
|
||||||
|
|
||||||
struct Weather {
|
struct Weather {
|
||||||
status string [skip] // drop this field
|
status string @[skip] // drop this field
|
||||||
api_version string [skip]
|
api_version string @[skip]
|
||||||
api_status string [skip]
|
api_status string @[skip]
|
||||||
lang string [skip]
|
lang string @[skip]
|
||||||
unit string [skip]
|
unit string @[skip]
|
||||||
tzshift int [skip]
|
tzshift int @[skip]
|
||||||
timezone string [skip]
|
timezone string @[skip]
|
||||||
server_time u32 [skip]
|
server_time u32 @[skip]
|
||||||
location []f32 [skip]
|
location []f32 @[skip]
|
||||||
result Result //[json: result] if the field name is different in JSON, it can be specified
|
result Result //[json: result] if the field name is different in JSON, it can be specified
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Result {
|
struct Result {
|
||||||
realtime Realtime [skip]
|
realtime Realtime @[skip]
|
||||||
minutely Minutely [skip]
|
minutely Minutely @[skip]
|
||||||
hourly Hourly [skip]
|
hourly Hourly @[skip]
|
||||||
daily Daily [skip]
|
daily Daily @[skip]
|
||||||
primary int [skip]
|
primary int @[skip]
|
||||||
forecast_keypoint string
|
forecast_keypoint string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import gg
|
||||||
import gx
|
import gx
|
||||||
import math
|
import math
|
||||||
|
|
||||||
[heap]
|
@[heap]
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
pub mut:
|
pub mut:
|
||||||
ctx &gg.Context = unsafe { 0 }
|
ctx &gg.Context = unsafe { 0 }
|
||||||
|
|
|
@ -91,7 +91,7 @@ fn (mut state AppState) update() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn (mut state AppState) worker(id int, input chan MandelChunk, ready chan bool) {
|
fn (mut state AppState) worker(id int, input chan MandelChunk, ready chan bool) {
|
||||||
for {
|
for {
|
||||||
chunk := <-input or { break }
|
chunk := <-input or { break }
|
||||||
|
|
|
@ -14,7 +14,7 @@ mut:
|
||||||
pixels [pheight][pwidth]u32
|
pixels [pheight][pwidth]u32
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn (mut state AppState) update() {
|
fn (mut state AppState) update() {
|
||||||
mut rcolor := u64(state.gg.frame)
|
mut rcolor := u64(state.gg.frame)
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -51,7 +51,7 @@ fn main() {
|
||||||
|
|
||||||
// Try uncommenting or changing the lines inside the live functions.
|
// Try uncommenting or changing the lines inside the live functions.
|
||||||
// Guess what will happen:
|
// Guess what will happen:
|
||||||
[live]
|
@[live]
|
||||||
fn frame(mut game Game) {
|
fn frame(mut game Game) {
|
||||||
game.gg.begin()
|
game.gg.begin()
|
||||||
game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates')
|
game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates')
|
||||||
|
@ -63,7 +63,7 @@ fn frame(mut game Game) {
|
||||||
game.gg.end()
|
game.gg.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
[live]
|
@[live]
|
||||||
fn (mut game Game) update_model() {
|
fn (mut game Game) update_model() {
|
||||||
speed := 2
|
speed := 2
|
||||||
game.x += speed * game.dx
|
game.x += speed * game.dx
|
||||||
|
|
|
@ -39,7 +39,7 @@ fn frame(mut ctx Context) {
|
||||||
ctx.gg.end()
|
ctx.gg.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
[live]
|
@[live]
|
||||||
fn (ctx &Context) draw() {
|
fn (ctx &Context) draw() {
|
||||||
s := gg.window_size()
|
s := gg.window_size()
|
||||||
mut w := s.width
|
mut w := s.width
|
||||||
|
|
|
@ -4,7 +4,7 @@ module main
|
||||||
import time
|
import time
|
||||||
import v.live
|
import v.live
|
||||||
|
|
||||||
[live]
|
@[live]
|
||||||
fn print_message() {
|
fn print_message() {
|
||||||
info := live.info()
|
info := live.info()
|
||||||
println('OK reloads: ${info.reloads_ok:4d} | Total reloads: ${info.reloads:4d} | Hello! Modify this message while the program is running.')
|
println('OK reloads: ${info.reloads_ok:4d} | Total reloads: ${info.reloads:4d} | Hello! Modify this message while the program is running.')
|
||||||
|
|
|
@ -8,10 +8,10 @@ import net.http
|
||||||
import math
|
import math
|
||||||
import v.util.version
|
import v.util.version
|
||||||
|
|
||||||
[table: 'benchmark']
|
@[table: 'benchmark']
|
||||||
struct Task {
|
struct Task {
|
||||||
mut:
|
mut:
|
||||||
id u32 [primary; serial; sql: serial]
|
id u32 @[primary; serial; sql: serial]
|
||||||
title string
|
title string
|
||||||
status string
|
status string
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ fn new_app() &App {
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
['/'; get]
|
@['/'; get]
|
||||||
pub fn (mut app App) controller_get_all_task() !vweb.Result {
|
pub fn (mut app App) controller_get_all_task() !vweb.Result {
|
||||||
v_version := version.full_v_version(true)
|
v_version := version.full_v_version(true)
|
||||||
orm_stmt_kinds := ['insert', 'select', 'update']
|
orm_stmt_kinds := ['insert', 'select', 'update']
|
||||||
|
|
|
@ -8,7 +8,7 @@ struct App {
|
||||||
vweb.Context
|
vweb.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
[table: 'benchmark']
|
@[table: 'benchmark']
|
||||||
struct Task {
|
struct Task {
|
||||||
mut:
|
mut:
|
||||||
id u32 [primary; serial; sql: serial]
|
id u32 [primary; serial; sql: serial]
|
||||||
|
@ -33,12 +33,12 @@ fn new_app() &App {
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
['/hello-world']
|
@['/hello-world']
|
||||||
pub fn (mut app App) hello_world() vweb.Result {
|
pub fn (mut app App) hello_world() vweb.Result {
|
||||||
return app.text('hello world')
|
return app.text('hello world')
|
||||||
}
|
}
|
||||||
|
|
||||||
['/sqlite-memory/:count']
|
@['/sqlite-memory/:count']
|
||||||
pub fn (mut app App) sqlite_memory(count int) vweb.Result {
|
pub fn (mut app App) sqlite_memory(count int) vweb.Result {
|
||||||
mut insert_stopwatchs := []int{}
|
mut insert_stopwatchs := []int{}
|
||||||
mut select_stopwatchs := []int{}
|
mut select_stopwatchs := []int{}
|
||||||
|
@ -100,7 +100,7 @@ pub fn (mut app App) sqlite_memory(count int) vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/sqlite-file/:count']
|
@['/sqlite-file/:count']
|
||||||
pub fn (mut app App) sqlite_file(count int) vweb.Result {
|
pub fn (mut app App) sqlite_file(count int) vweb.Result {
|
||||||
response := Response{
|
response := Response{
|
||||||
insert: []
|
insert: []
|
||||||
|
@ -110,7 +110,7 @@ pub fn (mut app App) sqlite_file(count int) vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/postgres/:count']
|
@['/postgres/:count']
|
||||||
pub fn (mut app App) postgres(count int) vweb.Result {
|
pub fn (mut app App) postgres(count int) vweb.Result {
|
||||||
response := Response{
|
response := Response{
|
||||||
insert: []
|
insert: []
|
||||||
|
@ -120,7 +120,7 @@ pub fn (mut app App) postgres(count int) vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/mysql/:count']
|
@['/mysql/:count']
|
||||||
pub fn (mut app App) mysql(count int) vweb.Result {
|
pub fn (mut app App) mysql(count int) vweb.Result {
|
||||||
response := Response{
|
response := Response{
|
||||||
insert: []
|
insert: []
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import log
|
import log
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn should_not_return(mut logger log.Log) {
|
fn should_not_return(mut logger log.Log) {
|
||||||
logger.fatal('${@FILE_LINE}: yikes!')
|
logger.fatal('${@FILE_LINE}: yikes!')
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,13 @@ fn C.tray_app_run(&TrayInfo)
|
||||||
fn C.tray_app_exit(&TrayInfo)
|
fn C.tray_app_exit(&TrayInfo)
|
||||||
|
|
||||||
struct TrayMenuItem {
|
struct TrayMenuItem {
|
||||||
id string [required] // Unique ID.
|
id string @[required] // Unique ID.
|
||||||
text string [required] // Text to display.
|
text string @[required] // Text to display.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameters to configure the tray button.
|
// Parameters to configure the tray button.
|
||||||
struct TrayParams {
|
struct TrayParams {
|
||||||
items []TrayMenuItem [required]
|
items []TrayMenuItem @[required]
|
||||||
on_click fn (item TrayMenuItem)
|
on_click fn (item TrayMenuItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ struct TrayInfo {
|
||||||
app_delegate voidptr // pointer to AppDelegate
|
app_delegate voidptr // pointer to AppDelegate
|
||||||
}
|
}
|
||||||
|
|
||||||
[heap]
|
@[heap]
|
||||||
struct MyApp {
|
struct MyApp {
|
||||||
mut:
|
mut:
|
||||||
tray_info &TrayInfo = unsafe { nil }
|
tray_info &TrayInfo = unsafe { nil }
|
||||||
|
|
|
@ -44,37 +44,37 @@ mut:
|
||||||
z f64 = 0.0
|
z f64 = 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) + (b Vec) Vec {
|
fn (v Vec) + (b Vec) Vec {
|
||||||
return Vec{v.x + b.x, v.y + b.y, v.z + b.z}
|
return Vec{v.x + b.x, v.y + b.y, v.z + b.z}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) - (b Vec) Vec {
|
fn (v Vec) - (b Vec) Vec {
|
||||||
return Vec{v.x - b.x, v.y - b.y, v.z - b.z}
|
return Vec{v.x - b.x, v.y - b.y, v.z - b.z}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) * (b Vec) Vec {
|
fn (v Vec) * (b Vec) Vec {
|
||||||
return Vec{v.x * b.x, v.y * b.y, v.z * b.z}
|
return Vec{v.x * b.x, v.y * b.y, v.z * b.z}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) dot(b Vec) f64 {
|
fn (v Vec) dot(b Vec) f64 {
|
||||||
return v.x * b.x + v.y * b.y + v.z * b.z
|
return v.x * b.x + v.y * b.y + v.z * b.z
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) mult_s(b f64) Vec {
|
fn (v Vec) mult_s(b f64) Vec {
|
||||||
return Vec{v.x * b, v.y * b, v.z * b}
|
return Vec{v.x * b, v.y * b, v.z * b}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) cross(b Vec) Vec {
|
fn (v Vec) cross(b Vec) Vec {
|
||||||
return Vec{v.y * b.z - v.z * b.y, v.z * b.x - v.x * b.z, v.x * b.y - v.y * b.x}
|
return Vec{v.y * b.z - v.z * b.y, v.z * b.x - v.x * b.z, v.x * b.y - v.y * b.x}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (v Vec) norm() Vec {
|
fn (v Vec) norm() Vec {
|
||||||
tmp_norm := 1.0 / math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
|
tmp_norm := 1.0 / math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
|
||||||
return Vec{v.x * tmp_norm, v.y * tmp_norm, v.z * tmp_norm}
|
return Vec{v.x * tmp_norm, v.y * tmp_norm, v.z * tmp_norm}
|
||||||
|
@ -323,7 +323,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
//********************************** Utilities ******************************
|
//********************************** Utilities ******************************
|
||||||
[inline]
|
@[inline]
|
||||||
fn clamp(x f64) f64 {
|
fn clamp(x f64) f64 {
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
return 0
|
return 0
|
||||||
|
@ -334,7 +334,7 @@ fn clamp(x f64) f64 {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn to_int(x f64) int {
|
fn to_int(x f64) int {
|
||||||
p := math.pow(clamp(x), 1.0 / 2.2)
|
p := math.pow(clamp(x), 1.0 / 2.2)
|
||||||
return int(p * 255.0 + 0.5)
|
return int(p * 255.0 + 0.5)
|
||||||
|
|
|
@ -9,7 +9,7 @@ import math
|
||||||
// customisable through setting VJOBS
|
// customisable through setting VJOBS
|
||||||
const max_parallel_workers = runtime.nr_jobs()
|
const max_parallel_workers = runtime.nr_jobs()
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
pub struct ParserSettings {
|
pub struct ParserSettings {
|
||||||
sequential bool
|
sequential bool
|
||||||
img bool
|
img bool
|
||||||
|
@ -146,7 +146,7 @@ fn parse_parallel_args(extra_workers int) !ParallelArgs {
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn get_workers(workers int, extra_workers int) int {
|
fn get_workers(workers int, extra_workers int) int {
|
||||||
result := if workers + extra_workers <= args.max_parallel_workers {
|
result := if workers + extra_workers <= args.max_parallel_workers {
|
||||||
workers
|
workers
|
||||||
|
|
|
@ -4,7 +4,7 @@ import gx
|
||||||
import os
|
import os
|
||||||
import sim
|
import sim
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
pub struct ImageSettings {
|
pub struct ImageSettings {
|
||||||
pub:
|
pub:
|
||||||
width int = sim.default_width
|
width int = sim.default_width
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module sim
|
module sim
|
||||||
|
|
||||||
// log is a helper function to print debug info
|
// log is a helper function to print debug info
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn log(info string) {
|
pub fn log(info string) {
|
||||||
$if verbose ? {
|
$if verbose ? {
|
||||||
println(info)
|
println(info)
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub const (
|
||||||
default_gravity = 4.9
|
default_gravity = 4.9
|
||||||
)
|
)
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
pub struct SimParams {
|
pub struct SimParams {
|
||||||
rope_length f64 = sim.default_rope_length
|
rope_length f64 = sim.default_rope_length
|
||||||
bearing_mass f64 = sim.default_bearing_mass
|
bearing_mass f64 = sim.default_bearing_mass
|
||||||
|
|
|
@ -14,7 +14,7 @@ pub const (
|
||||||
default_height = 600
|
default_height = 600
|
||||||
)
|
)
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
pub struct GridSettings {
|
pub struct GridSettings {
|
||||||
pub:
|
pub:
|
||||||
width int = sim.default_width
|
width int = sim.default_width
|
||||||
|
@ -27,7 +27,7 @@ pub fn new_grid_settings(settings GridSettings) GridSettings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[params]
|
@[params]
|
||||||
pub struct RunnerSettings {
|
pub struct RunnerSettings {
|
||||||
pub:
|
pub:
|
||||||
grid GridSettings
|
grid GridSettings
|
||||||
|
|
|
@ -10,7 +10,7 @@ struct Message {
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn json_response() string {
|
fn json_response() string {
|
||||||
msg := Message{
|
msg := Message{
|
||||||
message: 'Hello, World!'
|
message: 'Hello, World!'
|
||||||
|
@ -18,7 +18,7 @@ fn json_response() string {
|
||||||
return json.encode(msg)
|
return json.encode(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn hello_response() string {
|
fn hello_response() string {
|
||||||
return 'Hello, World!'
|
return 'Hello, World!'
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,7 +239,7 @@ fn init_cube_glsl(mut app App) {
|
||||||
println('GLSL init DONE!')
|
println('GLSL init DONE!')
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
||||||
return m4.Vec4{
|
return m4.Vec4{
|
||||||
e: [x, y, z, w]!
|
e: [x, y, z, w]!
|
||||||
|
|
|
@ -358,7 +358,7 @@ fn init_cube_glsl_p(mut app App) {
|
||||||
println('GLSL Puppy init DONE!')
|
println('GLSL Puppy init DONE!')
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
||||||
return m4.Vec4{
|
return m4.Vec4{
|
||||||
e: [x, y, z, w]!
|
e: [x, y, z, w]!
|
||||||
|
|
|
@ -67,7 +67,7 @@ mut:
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Draw functions
|
* Draw functions
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
[inline]
|
@[inline]
|
||||||
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
||||||
return m4.Vec4{
|
return m4.Vec4{
|
||||||
e: [x, y, z, w]!
|
e: [x, y, z, w]!
|
||||||
|
|
|
@ -60,7 +60,7 @@ fn graphics_frame(mut state AppState) {
|
||||||
state.gg.end()
|
state.gg.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut state AppState) bsample(idx int) u8 {
|
fn (mut state AppState) bsample(idx int) u8 {
|
||||||
return u8(127 + state.frames[(state.gframe + idx) & 2047] * 128)
|
return u8(127 + state.frames[(state.gframe + idx) & 2047] * 128)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ const (
|
||||||
sw_start_ms = sw.elapsed().milliseconds()
|
sw_start_ms = sw.elapsed().milliseconds()
|
||||||
)
|
)
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn sintone(periods int, frame int, num_frames int) f32 {
|
fn sintone(periods int, frame int, num_frames int) f32 {
|
||||||
return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
|
return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ const (
|
||||||
orange = ui.Color{255, 140, 0}
|
orange = ui.Color{255, 140, 0}
|
||||||
)
|
)
|
||||||
|
|
||||||
[heap]
|
@[heap]
|
||||||
struct App {
|
struct App {
|
||||||
mut:
|
mut:
|
||||||
tui &ui.Context = unsafe { nil }
|
tui &ui.Context = unsafe { nil }
|
||||||
|
@ -239,7 +239,7 @@ fn (mut b Ball) update(dt f32) {
|
||||||
b.pos.y += b.vel.y * b.acc.y * dt
|
b.pos.y += b.vel.y * b.acc.y * dt
|
||||||
}
|
}
|
||||||
|
|
||||||
[heap]
|
@[heap]
|
||||||
struct Game {
|
struct Game {
|
||||||
mut:
|
mut:
|
||||||
app &App = unsafe { nil }
|
app &App = unsafe { nil }
|
||||||
|
|
|
@ -461,7 +461,7 @@ fn (mut app App) draw_footer() {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) inc_size() {
|
fn (mut app App) inc_size() {
|
||||||
if app.size < 30 {
|
if app.size < 30 {
|
||||||
app.size++
|
app.size++
|
||||||
|
@ -469,7 +469,7 @@ fn (mut app App) inc_size() {
|
||||||
app.show_msg('inc. size: ${app.size}', 1)
|
app.show_msg('inc. size: ${app.size}', 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) dec_size() {
|
fn (mut app App) dec_size() {
|
||||||
if app.size > 1 {
|
if app.size > 1 {
|
||||||
app.size--
|
app.size--
|
||||||
|
|
|
@ -256,7 +256,7 @@ fn (mut r Rat) randomize() {
|
||||||
r.app.height - block_size - buffer)
|
r.app.height - block_size - buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
[heap]
|
@[heap]
|
||||||
struct App {
|
struct App {
|
||||||
mut:
|
mut:
|
||||||
termui &termui.Context = unsafe { nil }
|
termui &termui.Context = unsafe { nil }
|
||||||
|
|
|
@ -126,7 +126,7 @@ fn remap(v f32, min f32, max f32, new_min f32, new_max f32) f32 {
|
||||||
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
||||||
}
|
}
|
||||||
|
|
||||||
[if showfps ?]
|
@[if showfps ?]
|
||||||
fn (mut 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
|
||||||
|
|
|
@ -127,7 +127,7 @@ fn remap(v f32, min f32, max f32, new_min f32, new_max f32) f32 {
|
||||||
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
||||||
}
|
}
|
||||||
|
|
||||||
[if showfps ?]
|
@[if showfps ?]
|
||||||
fn (mut 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
|
||||||
|
|
|
@ -72,12 +72,12 @@ pub mut:
|
||||||
* Utility functions
|
* Utility functions
|
||||||
*
|
*
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
[inline]
|
@[inline]
|
||||||
fn modulo(x int, n int) int {
|
fn modulo(x int, n int) int {
|
||||||
return (x % n + n) % n
|
return (x % n + n) % n
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn get_extension(x string) Item_type {
|
fn get_extension(x string) Item_type {
|
||||||
// 4 char extension check
|
// 4 char extension check
|
||||||
if x.len > 4 {
|
if x.len > 4 {
|
||||||
|
@ -110,7 +110,7 @@ fn get_extension(x string) Item_type {
|
||||||
return .file
|
return .file
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn is_image(x Item_type) bool {
|
fn is_image(x Item_type) bool {
|
||||||
if int(x) >= int(Item_type.bmp) {
|
if int(x) >= int(Item_type.bmp) {
|
||||||
return true
|
return true
|
||||||
|
@ -118,7 +118,7 @@ fn is_image(x Item_type) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn is_container(x Item_type) bool {
|
fn is_container(x Item_type) bool {
|
||||||
if x in [.zip, .folder] {
|
if x in [.zip, .folder] {
|
||||||
return true
|
return true
|
||||||
|
@ -126,7 +126,7 @@ fn is_container(x Item_type) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (item_list Item_list) is_inside_a_container() bool {
|
fn (item_list Item_list) is_inside_a_container() bool {
|
||||||
if item_list.lst.len <= 0 || item_list.n_item <= 0 {
|
if item_list.lst.len <= 0 || item_list.n_item <= 0 {
|
||||||
return false
|
return false
|
||||||
|
@ -134,7 +134,7 @@ fn (item_list Item_list) is_inside_a_container() bool {
|
||||||
return item_list.lst[item_list.item_index].need_extract
|
return item_list.lst[item_list.item_index].need_extract
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn (item_list Item_list) get_file_path() string {
|
fn (item_list Item_list) get_file_path() string {
|
||||||
if item_list.lst.len <= 0 || item_list.n_item <= 0 {
|
if item_list.lst.len <= 0 || item_list.n_item <= 0 {
|
||||||
return ''
|
return ''
|
||||||
|
@ -334,7 +334,7 @@ fn (mut item_list Item_list) go_to_next_container(in_inc int) {
|
||||||
* Other functions
|
* Other functions
|
||||||
*
|
*
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut item_list Item_list) rotate(in_inc int) {
|
fn (mut item_list Item_list) rotate(in_inc int) {
|
||||||
item_list.lst[item_list.item_index].rotation += in_inc
|
item_list.lst[item_list.item_index].rotation += in_inc
|
||||||
if item_list.lst[item_list.item_index].rotation >= 4 {
|
if item_list.lst[item_list.item_index].rotation >= 4 {
|
||||||
|
|
|
@ -166,7 +166,7 @@ fn update_text_texture(sg_img gfx.Image, w int, h int, buf &u8) {
|
||||||
* Memory buffer
|
* Memory buffer
|
||||||
*
|
*
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut app App) resize_buf_if_needed(in_size int) {
|
fn (mut app App) resize_buf_if_needed(in_size int) {
|
||||||
// manage the memory buffer
|
// manage the memory buffer
|
||||||
if app.mem_buf_size < in_size {
|
if app.mem_buf_size < in_size {
|
||||||
|
@ -191,7 +191,7 @@ fn (mut app App) resize_buf_if_needed(in_size int) {
|
||||||
*
|
*
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
// read_bytes from file in `path` in the memory buffer of app.
|
// read_bytes from file in `path` in the memory buffer of app.
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
fn (mut app App) read_bytes(path string) bool {
|
fn (mut app App) read_bytes(path string) bool {
|
||||||
mut fp := os.vfopen(path, 'rb') or {
|
mut fp := os.vfopen(path, 'rb') or {
|
||||||
eprintln('ERROR: Can not open the file [${path}].')
|
eprintln('ERROR: Can not open the file [${path}].')
|
||||||
|
@ -375,7 +375,7 @@ fn cleanup(mut app App) {
|
||||||
* Draw functions
|
* Draw functions
|
||||||
*
|
*
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
fn frame(mut app App) {
|
fn frame(mut app App) {
|
||||||
ws := gg.window_size_real_pixels()
|
ws := gg.window_size_real_pixels()
|
||||||
if ws.width <= 0 || ws.height <= 0 {
|
if ws.width <= 0 || ws.height <= 0 {
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub fn (mut app App) index() vweb.Result {
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
}
|
}
|
||||||
|
|
||||||
['/upload'; post]
|
@['/upload'; post]
|
||||||
pub fn (mut app App) upload() vweb.Result {
|
pub fn (mut app App) upload() vweb.Result {
|
||||||
fdata := app.files['upfile']
|
fdata := app.files['upfile']
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub fn (mut app App) index() vweb.Result {
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
}
|
}
|
||||||
|
|
||||||
['/upload'; post]
|
@['/upload'; post]
|
||||||
pub fn (mut app App) upload() vweb.Result {
|
pub fn (mut app App) upload() vweb.Result {
|
||||||
dump(app.form)
|
dump(app.form)
|
||||||
dump(app.files)
|
dump(app.files)
|
||||||
|
@ -29,7 +29,7 @@ pub fn (mut app App) upload() vweb.Result {
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
}
|
}
|
||||||
|
|
||||||
['/submit'; post]
|
@['/submit'; post]
|
||||||
pub fn (mut app App) submit() vweb.Result {
|
pub fn (mut app App) submit() vweb.Result {
|
||||||
dump(app.form)
|
dump(app.form)
|
||||||
form_data := app.form.clone()
|
form_data := app.form.clone()
|
||||||
|
|
|
@ -33,7 +33,7 @@ fn new_app() &App {
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
['/']
|
@['/']
|
||||||
pub fn (mut app App) index() vweb.Result {
|
pub fn (mut app App) index() vweb.Result {
|
||||||
println('Index page')
|
println('Index page')
|
||||||
title := 'Home Page'
|
title := 'Home Page'
|
||||||
|
@ -43,8 +43,8 @@ pub fn (mut app App) index() vweb.Result {
|
||||||
return app.html(base)
|
return app.html(base)
|
||||||
}
|
}
|
||||||
|
|
||||||
[middleware: check_auth]
|
@[middleware: check_auth]
|
||||||
['/admin/secrets']
|
@['/admin/secrets']
|
||||||
pub fn (mut app App) secrets() vweb.Result {
|
pub fn (mut app App) secrets() vweb.Result {
|
||||||
println('Secrets page')
|
println('Secrets page')
|
||||||
title := 'Secret Admin Page'
|
title := 'Secret Admin Page'
|
||||||
|
@ -54,7 +54,7 @@ pub fn (mut app App) secrets() vweb.Result {
|
||||||
return app.html(base)
|
return app.html(base)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/admin/:sub']
|
@['/admin/:sub']
|
||||||
pub fn (mut app App) dynamic(sub string) vweb.Result {
|
pub fn (mut app App) dynamic(sub string) vweb.Result {
|
||||||
println('Dynamic page')
|
println('Dynamic page')
|
||||||
title := 'Secret dynamic'
|
title := 'Secret dynamic'
|
||||||
|
@ -64,7 +64,7 @@ pub fn (mut app App) dynamic(sub string) vweb.Result {
|
||||||
return app.html(base)
|
return app.html(base)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/early']
|
@['/early']
|
||||||
pub fn (mut app App) early() vweb.Result {
|
pub fn (mut app App) early() vweb.Result {
|
||||||
println('Early page')
|
println('Early page')
|
||||||
title := 'Early Exit'
|
title := 'Early Exit'
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub fn (app &App) before_request() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
['/users/:user']
|
@['/users/:user']
|
||||||
pub fn (mut app App) user_endpoint(user string) vweb.Result {
|
pub fn (mut app App) user_endpoint(user string) vweb.Result {
|
||||||
id := rand.intn(100) or { 0 }
|
id := rand.intn(100) or { 0 }
|
||||||
return app.json({
|
return app.json({
|
||||||
|
@ -55,7 +55,7 @@ pub fn (mut app App) cookie() vweb.Result {
|
||||||
return app.text('Response Headers\n${app.header}')
|
return app.text('Response Headers\n${app.header}')
|
||||||
}
|
}
|
||||||
|
|
||||||
[post]
|
@[post]
|
||||||
pub fn (mut app App) post() vweb.Result {
|
pub fn (mut app App) post() vweb.Result {
|
||||||
return app.text('Post body: ${app.req.data}')
|
return app.text('Post body: ${app.req.data}')
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ module main
|
||||||
|
|
||||||
import vweb
|
import vweb
|
||||||
|
|
||||||
['/controller/auth'; post]
|
@['/controller/auth'; post]
|
||||||
pub fn (mut app App) controller_auth(username string, password string) vweb.Result {
|
pub fn (mut app App) controller_auth(username string, password string) vweb.Result {
|
||||||
response := app.service_auth(username, password) or {
|
response := app.service_auth(username, password) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
struct AuthRequestDto {
|
struct AuthRequestDto {
|
||||||
username string [nonull]
|
username string @[nonull]
|
||||||
password string [nonull]
|
password string @[nonull]
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import vweb
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
import json
|
import json
|
||||||
|
|
||||||
['/controller/products'; get]
|
@['/controller/products'; get]
|
||||||
pub fn (mut app App) controller_get_all_products() vweb.Result {
|
pub fn (mut app App) controller_get_all_products() vweb.Result {
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ pub fn (mut app App) controller_get_all_products() vweb.Result {
|
||||||
// return app.text('response')
|
// return app.text('response')
|
||||||
}
|
}
|
||||||
|
|
||||||
['/controller/product/create'; post]
|
@['/controller/product/create'; post]
|
||||||
pub fn (mut app App) controller_create_product(product_name string) vweb.Result {
|
pub fn (mut app App) controller_create_product(product_name string) vweb.Result {
|
||||||
if product_name == '' {
|
if product_name == '' {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
[table: 'products']
|
@[table: 'products']
|
||||||
struct Product {
|
struct Product {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
user_id int
|
user_id int
|
||||||
name string [sql_type: 'TEXT']
|
name string @[sql_type: 'TEXT']
|
||||||
created_at string [default: 'CURRENT_TIMESTAMP']
|
created_at string @[default: 'CURRENT_TIMESTAMP']
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ module main
|
||||||
|
|
||||||
import vweb
|
import vweb
|
||||||
|
|
||||||
['/products'; get]
|
@['/products'; get]
|
||||||
pub fn (mut app App) products() !vweb.Result {
|
pub fn (mut app App) products() !vweb.Result {
|
||||||
token := app.get_cookie('token') or {
|
token := app.get_cookie('token') or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
|
|
@ -4,7 +4,7 @@ import vweb
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
import json
|
import json
|
||||||
|
|
||||||
['/controller/users'; get]
|
@['/controller/users'; get]
|
||||||
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
// token := app.get_cookie('token') or { '' }
|
// token := app.get_cookie('token') or { '' }
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
@ -21,7 +21,7 @@ pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/controller/user'; get]
|
@['/controller/user'; get]
|
||||||
pub fn (mut app App) controller_get_user() vweb.Result {
|
pub fn (mut app App) controller_get_user() vweb.Result {
|
||||||
// token := app.get_cookie('token') or { '' }
|
// token := app.get_cookie('token') or { '' }
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
@ -47,7 +47,7 @@ pub fn (mut app App) controller_get_user() vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/controller/user/create'; post]
|
@['/controller/user/create'; post]
|
||||||
pub fn (mut app App) controller_create_user(username string, password string) vweb.Result {
|
pub fn (mut app App) controller_create_user(username string, password string) vweb.Result {
|
||||||
if username == '' {
|
if username == '' {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
[table: 'users']
|
@[table: 'users']
|
||||||
pub struct User {
|
pub struct User {
|
||||||
mut:
|
mut:
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
username string [sql_type: 'TEXT'; unique]
|
username string @[sql_type: 'TEXT'; unique]
|
||||||
password string [sql_type: 'TEXT']
|
password string @[sql_type: 'TEXT']
|
||||||
active bool
|
active bool
|
||||||
products []Product [fkey: 'user_id']
|
products []Product @[fkey: 'user_id']
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ module main
|
||||||
import vweb
|
import vweb
|
||||||
import json
|
import json
|
||||||
|
|
||||||
['/auth/login'; post]
|
@['/auth/login'; post]
|
||||||
pub fn (mut app App) controller_auth() vweb.Result {
|
pub fn (mut app App) controller_auth() vweb.Result {
|
||||||
body := json.decode(AuthRequestDto, app.req.data) or {
|
body := json.decode(AuthRequestDto, app.req.data) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
|
|
@ -3,6 +3,6 @@ module main
|
||||||
struct AuthRequestDto {
|
struct AuthRequestDto {
|
||||||
// Adding a [required] attribute will make decoding fail, if that field is not present in the input.
|
// Adding a [required] attribute will make decoding fail, if that field is not present in the input.
|
||||||
// If a field is not [required], but is missing, it will be assumed to have its default value, like 0 for numbers, or '' for strings, and decoding will not fail.
|
// If a field is not [required], but is missing, it will be assumed to have its default value, like 0 for numbers, or '' for strings, and decoding will not fail.
|
||||||
username string [required]
|
username string @[required]
|
||||||
password string [required]
|
password string @[required]
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ fn new_app() &App {
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
['/'; get]
|
@['/'; get]
|
||||||
pub fn (mut app App) ping() ?vweb.Result {
|
pub fn (mut app App) ping() ?vweb.Result {
|
||||||
return app.text('ping')
|
return app.text('ping')
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import vweb
|
||||||
import json
|
import json
|
||||||
import databases
|
import databases
|
||||||
|
|
||||||
['/user/:id/get'; get]
|
@['/user/:id/get'; get]
|
||||||
pub fn (mut app App) controller_get_user_by_id(id int) vweb.Result {
|
pub fn (mut app App) controller_get_user_by_id(id int) vweb.Result {
|
||||||
response := app.service_get_user_by_id(id) or {
|
response := app.service_get_user_by_id(id) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -13,7 +13,7 @@ pub fn (mut app App) controller_get_user_by_id(id int) vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/user/create'; post]
|
@['/user/create'; post]
|
||||||
pub fn (mut app App) controller_create_user() vweb.Result {
|
pub fn (mut app App) controller_create_user() vweb.Result {
|
||||||
body := json.decode(User, app.req.data) or {
|
body := json.decode(User, app.req.data) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -28,7 +28,7 @@ pub fn (mut app App) controller_create_user() vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/user/get_all'; get]
|
@['/user/get_all'; get]
|
||||||
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
token := app.get_header('token')
|
token := app.get_header('token')
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/user/get_by_username/:username'; get]
|
@['/user/get_by_username/:username'; get]
|
||||||
pub fn (mut app App) controller_get_by_username(username string) vweb.Result {
|
pub fn (mut app App) controller_get_by_username(username string) vweb.Result {
|
||||||
response := app.service_get_by_username(username) or {
|
response := app.service_get_by_username(username) or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
@ -53,7 +53,7 @@ pub fn (mut app App) controller_get_by_username(username string) vweb.Result {
|
||||||
return app.json(response)
|
return app.json(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
['/user/drop'; delete]
|
@['/user/drop'; delete]
|
||||||
pub fn (mut app App) delete() vweb.Result {
|
pub fn (mut app App) delete() vweb.Result {
|
||||||
mut db := databases.create_db_connection() or {
|
mut db := databases.create_db_connection() or {
|
||||||
app.set_status(400, '')
|
app.set_status(400, '')
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
[table: 'users']
|
@[table: 'users']
|
||||||
struct User {
|
struct User {
|
||||||
mut:
|
mut:
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
username string [required; sql_type: 'TEXT']
|
username string @[required; sql_type: 'TEXT']
|
||||||
password string [required; sql_type: 'TEXT']
|
password string @[required; sql_type: 'TEXT']
|
||||||
created_at string [default: 'CURRENT_TIMESTAMP']
|
created_at string @[default: 'CURRENT_TIMESTAMP']
|
||||||
updated_at string [default: 'CURRENT_TIMESTAMP']
|
updated_at string @[default: 'CURRENT_TIMESTAMP']
|
||||||
deleted_at string [default: 'CURRENT_TIMESTAMP']
|
deleted_at string @[default: 'CURRENT_TIMESTAMP']
|
||||||
active bool
|
active bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
struct Article {
|
struct Article {
|
||||||
id int [primary; sql: serial]
|
id int @[primary; sql: serial]
|
||||||
title string
|
title string
|
||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn (app &App) index_html() vweb.Result {
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
['/index']
|
@['/index']
|
||||||
pub fn (app &App) index() vweb.Result {
|
pub fn (app &App) index() vweb.Result {
|
||||||
articles := app.find_all_articles()
|
articles := app.find_all_articles()
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
|
@ -43,12 +43,12 @@ pub fn (mut app App) before_request() {
|
||||||
app.user_id = app.get_cookie('id') or { '0' }
|
app.user_id = app.get_cookie('id') or { '0' }
|
||||||
}
|
}
|
||||||
|
|
||||||
['/new']
|
@['/new']
|
||||||
pub fn (mut app App) new() vweb.Result {
|
pub fn (mut app App) new() vweb.Result {
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
}
|
}
|
||||||
|
|
||||||
['/new_article'; post]
|
@['/new_article'; post]
|
||||||
pub fn (mut app App) new_article(title string, text string) vweb.Result {
|
pub fn (mut app App) new_article(title string, text string) vweb.Result {
|
||||||
if title == '' || text == '' {
|
if title == '' || text == '' {
|
||||||
return app.text('Empty text/title')
|
return app.text('Empty text/title')
|
||||||
|
@ -66,7 +66,7 @@ pub fn (mut app App) new_article(title string, text string) vweb.Result {
|
||||||
return app.redirect('/')
|
return app.redirect('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
['/articles'; get]
|
@['/articles'; get]
|
||||||
pub fn (mut app App) articles() vweb.Result {
|
pub fn (mut app App) articles() vweb.Result {
|
||||||
articles := app.find_all_articles()
|
articles := app.find_all_articles()
|
||||||
json_result := json.encode(articles)
|
json_result := json.encode(articles)
|
||||||
|
|
|
@ -79,7 +79,7 @@ pub fn idx_max[T](array []T) !int {
|
||||||
|
|
||||||
// merge two sorted arrays (ascending) and maintain sorted order
|
// merge two sorted arrays (ascending) and maintain sorted order
|
||||||
// Example: arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8]
|
// Example: arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8]
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
pub fn merge[T](a []T, b []T) []T {
|
pub fn merge[T](a []T, b []T) []T {
|
||||||
mut m := []T{len: a.len + b.len}
|
mut m := []T{len: a.len + b.len}
|
||||||
mut ia := 0
|
mut ia := 0
|
||||||
|
@ -522,7 +522,7 @@ pub fn rotate_right[T](mut array []T, k int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn ptr_rotate[T](left_ int, mid &T, right_ int) {
|
fn ptr_rotate[T](left_ int, mid &T, right_ int) {
|
||||||
sz := usize(sizeof(T))
|
sz := usize(sizeof(T))
|
||||||
mut left := usize(left_)
|
mut left := usize(left_)
|
||||||
|
@ -597,7 +597,7 @@ fn raw_array_malloc_size[T]() isize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn memswap(x voidptr, y voidptr, len usize) {
|
fn memswap(x voidptr, y voidptr, len usize) {
|
||||||
block_size := isize(sizeof(Block))
|
block_size := isize(sizeof(Block))
|
||||||
|
|
||||||
|
@ -629,7 +629,7 @@ fn memswap(x voidptr, y voidptr, len usize) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn swap_nonoverlapping[T](x_ &T, y_ &T, count int) {
|
fn swap_nonoverlapping[T](x_ &T, y_ &T, count int) {
|
||||||
x := voidptr(x_)
|
x := voidptr(x_)
|
||||||
y := voidptr(y_)
|
y := voidptr(y_)
|
||||||
|
@ -672,7 +672,7 @@ fn can_copy_bits[T]() bool {
|
||||||
|
|
||||||
// carray_to_varray copies a C byte array into a V array of type `T`.
|
// carray_to_varray copies a C byte array into a V array of type `T`.
|
||||||
// See also: `cstring_to_vstring`
|
// See also: `cstring_to_vstring`
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn carray_to_varray[T](c_array_data voidptr, items int) []T {
|
pub fn carray_to_varray[T](c_array_data voidptr, items int) []T {
|
||||||
mut v_array := []T{len: items}
|
mut v_array := []T{len: items}
|
||||||
total_size := items * isize(sizeof(T))
|
total_size := items * isize(sizeof(T))
|
||||||
|
@ -713,7 +713,7 @@ pub fn find_last[T](array []T, predicate fn (elem T) bool) ?T {
|
||||||
|
|
||||||
// join_to_string takes in a custom transform function and joins all elements into a string with
|
// join_to_string takes in a custom transform function and joins all elements into a string with
|
||||||
// the specified separator
|
// the specified separator
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
pub fn join_to_string[T](array []T, separator string, transform fn (elem T) string) string {
|
pub fn join_to_string[T](array []T, separator string, transform fn (elem T) string) string {
|
||||||
mut sb := strings.new_builder(array.len * 2)
|
mut sb := strings.new_builder(array.len * 2)
|
||||||
defer {
|
defer {
|
||||||
|
|
|
@ -106,7 +106,7 @@ pub fn new(size int) BitField {
|
||||||
}
|
}
|
||||||
|
|
||||||
// frees the memory allocated for the bitfield instance
|
// frees the memory allocated for the bitfield instance
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (instance &BitField) free() {
|
pub fn (instance &BitField) free() {
|
||||||
unsafe {
|
unsafe {
|
||||||
instance.field.free()
|
instance.field.free()
|
||||||
|
@ -114,7 +114,7 @@ pub fn (instance &BitField) free() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_bit returns the value (0 or 1) of bit number 'bit_nr' (count from 0).
|
// get_bit returns the value (0 or 1) of bit number 'bit_nr' (count from 0).
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (instance BitField) get_bit(bitnr int) int {
|
pub fn (instance BitField) get_bit(bitnr int) int {
|
||||||
if bitnr >= instance.size {
|
if bitnr >= instance.size {
|
||||||
return 0
|
return 0
|
||||||
|
@ -123,7 +123,7 @@ pub fn (instance BitField) get_bit(bitnr int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// set_bit sets bit number 'bit_nr' to 1 (count from 0).
|
// set_bit sets bit number 'bit_nr' to 1 (count from 0).
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (mut instance BitField) set_bit(bitnr int) {
|
pub fn (mut instance BitField) set_bit(bitnr int) {
|
||||||
if bitnr >= instance.size {
|
if bitnr >= instance.size {
|
||||||
return
|
return
|
||||||
|
@ -132,7 +132,7 @@ pub fn (mut instance BitField) set_bit(bitnr int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0).
|
// clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0).
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (mut instance BitField) clear_bit(bitnr int) {
|
pub fn (mut instance BitField) clear_bit(bitnr int) {
|
||||||
if bitnr >= instance.size {
|
if bitnr >= instance.size {
|
||||||
return
|
return
|
||||||
|
@ -226,7 +226,7 @@ pub fn (mut instance BitField) clear_all() {
|
||||||
|
|
||||||
// toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit
|
// toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit
|
||||||
// number 'bit_nr'.
|
// number 'bit_nr'.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (mut instance BitField) toggle_bit(bitnr int) {
|
pub fn (mut instance BitField) toggle_bit(bitnr int) {
|
||||||
if bitnr >= instance.size {
|
if bitnr >= instance.size {
|
||||||
return
|
return
|
||||||
|
@ -328,7 +328,7 @@ pub fn join(input1 BitField, input2 BitField) BitField {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_size returns the number of bits the array can hold.
|
// get_size returns the number of bits the array can hold.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (instance BitField) get_size() int {
|
pub fn (instance BitField) get_size() int {
|
||||||
return instance.size
|
return instance.size
|
||||||
}
|
}
|
||||||
|
@ -378,7 +378,7 @@ pub fn (instance BitField) pop_count() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// hamming computes the Hamming distance between two bit arrays.
|
// hamming computes the Hamming distance between two bit arrays.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn hamming(input1 BitField, input2 BitField) int {
|
pub fn hamming(input1 BitField, input2 BitField) int {
|
||||||
input_xored := bf_xor(input1, input2)
|
input_xored := bf_xor(input1, input2)
|
||||||
return input_xored.pop_count()
|
return input_xored.pop_count()
|
||||||
|
@ -525,7 +525,7 @@ pub fn (instance BitField) rotate(offset int) BitField {
|
||||||
|
|
||||||
// Internal functions
|
// Internal functions
|
||||||
// clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
|
// clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
|
||||||
[inline]
|
@[inline]
|
||||||
fn (mut instance BitField) clear_tail() {
|
fn (mut instance BitField) clear_tail() {
|
||||||
tail := instance.size % bitfield.slot_size
|
tail := instance.size % bitfield.slot_size
|
||||||
if tail != 0 {
|
if tail != 0 {
|
||||||
|
@ -537,19 +537,19 @@ fn (mut instance BitField) clear_tail() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// bitmask is the bitmask needed to access a particular bit at offset bitnr
|
// bitmask is the bitmask needed to access a particular bit at offset bitnr
|
||||||
[inline]
|
@[inline]
|
||||||
fn bitmask(bitnr int) u32 {
|
fn bitmask(bitnr int) u32 {
|
||||||
return u32(u32(1) << u32(bitnr % bitfield.slot_size))
|
return u32(u32(1) << u32(bitnr % bitfield.slot_size))
|
||||||
}
|
}
|
||||||
|
|
||||||
// bitslot is the slot index (i.e. the integer) where a particular bit is located
|
// bitslot is the slot index (i.e. the integer) where a particular bit is located
|
||||||
[inline]
|
@[inline]
|
||||||
fn bitslot(size int) int {
|
fn bitslot(size int) int {
|
||||||
return size / bitfield.slot_size
|
return size / bitfield.slot_size
|
||||||
}
|
}
|
||||||
|
|
||||||
// min returns the minimum of 2 integers; it is here to avoid importing math just for that
|
// min returns the minimum of 2 integers; it is here to avoid importing math just for that
|
||||||
[inline]
|
@[inline]
|
||||||
fn min(input1 int, input2 int) int {
|
fn min(input1 int, input2 int) int {
|
||||||
if input1 < input2 {
|
if input1 < input2 {
|
||||||
return input1
|
return input1
|
||||||
|
@ -559,7 +559,7 @@ fn min(input1 int, input2 int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
|
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
|
||||||
[inline]
|
@[inline]
|
||||||
fn zbitnslots(length int) int {
|
fn zbitnslots(length int) int {
|
||||||
return (length - 1) / bitfield.slot_size + 1
|
return (length - 1) / bitfield.slot_size + 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub:
|
||||||
element_size int // size in bytes of one element in the array.
|
element_size int // size in bytes of one element in the array.
|
||||||
}
|
}
|
||||||
|
|
||||||
[flag]
|
@[flag]
|
||||||
pub enum ArrayFlags {
|
pub enum ArrayFlags {
|
||||||
noslices // when <<, `.noslices` will free the old data block immediately (you have to be sure, that there are *no slices* to that specific array). TODO: integrate with reference counting/compiler support for the static cases.
|
noslices // when <<, `.noslices` will free the old data block immediately (you have to be sure, that there are *no slices* to that specific array). TODO: integrate with reference counting/compiler support for the static cases.
|
||||||
noshrink // when `.noslices` and `.noshrink` are *both set*, .delete(x) will NOT allocate new memory and free the old. It will just move the elements in place, and adjust .len.
|
noshrink // when `.noslices` and `.noshrink` are *both set*, .delete(x) will NOT allocate new memory and free the old. It will just move the elements in place, and adjust .len.
|
||||||
|
@ -215,7 +215,7 @@ pub fn (a array) repeat(count int) array {
|
||||||
// multi-dimensional arrays.
|
// multi-dimensional arrays.
|
||||||
//
|
//
|
||||||
// It is `unsafe` to call directly because `depth` is not checked
|
// It is `unsafe` to call directly because `depth` is not checked
|
||||||
[direct_array_access; unsafe]
|
@[direct_array_access; unsafe]
|
||||||
pub fn (a array) repeat_to_depth(count int, depth int) array {
|
pub fn (a array) repeat_to_depth(count int, depth int) array {
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
panic('array.repeat: count is negative: ${count}')
|
panic('array.repeat: count is negative: ${count}')
|
||||||
|
@ -287,7 +287,7 @@ pub fn (mut a array) insert(i int, val voidptr) {
|
||||||
|
|
||||||
// insert_many is used internally to implement inserting many values
|
// insert_many is used internally to implement inserting many values
|
||||||
// into an the array beginning at `i`.
|
// into an the array beginning at `i`.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a array) insert_many(i int, val voidptr, size int) {
|
fn (mut a array) insert_many(i int, val voidptr, size int) {
|
||||||
$if !no_bounds_checking {
|
$if !no_bounds_checking {
|
||||||
if i < 0 || i > a.len {
|
if i < 0 || i > a.len {
|
||||||
|
@ -313,7 +313,7 @@ pub fn (mut a array) prepend(val voidptr) {
|
||||||
// prepend_many prepends another array to this array.
|
// prepend_many prepends another array to this array.
|
||||||
// NOTE: `.prepend` is probably all you need.
|
// NOTE: `.prepend` is probably all you need.
|
||||||
// NOTE: This code is never called in all of vlib
|
// NOTE: This code is never called in all of vlib
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a array) prepend_many(val voidptr, size int) {
|
fn (mut a array) prepend_many(val voidptr, size int) {
|
||||||
unsafe { a.insert_many(0, val, size) }
|
unsafe { a.insert_many(0, val, size) }
|
||||||
}
|
}
|
||||||
|
@ -394,7 +394,7 @@ pub fn (mut a array) clear() {
|
||||||
// is not safe, when your array contains more complex elements,
|
// is not safe, when your array contains more complex elements,
|
||||||
// like structs, maps, pointers etc, since setting them to 0,
|
// like structs, maps, pointers etc, since setting them to 0,
|
||||||
// can later lead to hard to find bugs.
|
// can later lead to hard to find bugs.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (mut a array) reset() {
|
pub fn (mut a array) reset() {
|
||||||
unsafe { vmemset(a.data, 0, a.len * a.element_size) }
|
unsafe { vmemset(a.data, 0, a.len * a.element_size) }
|
||||||
}
|
}
|
||||||
|
@ -431,7 +431,7 @@ pub fn (mut a array) drop(num int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// we manually inline this for single operations for performance without -prod
|
// we manually inline this for single operations for performance without -prod
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
fn (a array) get_unsafe(i int) voidptr {
|
fn (a array) get_unsafe(i int) voidptr {
|
||||||
unsafe {
|
unsafe {
|
||||||
return &u8(a.data) + u64(i) * u64(a.element_size)
|
return &u8(a.data) + u64(i) * u64(a.element_size)
|
||||||
|
@ -634,7 +634,7 @@ pub fn (a &array) clone() array {
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursively clone given array - `unsafe` when called directly because depth is not checked
|
// recursively clone given array - `unsafe` when called directly because depth is not checked
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (a &array) clone_to_depth(depth int) array {
|
pub fn (a &array) clone_to_depth(depth int) array {
|
||||||
mut arr := array{
|
mut arr := array{
|
||||||
element_size: a.element_size
|
element_size: a.element_size
|
||||||
|
@ -660,7 +660,7 @@ pub fn (a &array) clone_to_depth(depth int) array {
|
||||||
}
|
}
|
||||||
|
|
||||||
// we manually inline this for single operations for performance without -prod
|
// we manually inline this for single operations for performance without -prod
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
fn (mut a array) set_unsafe(i int, val voidptr) {
|
fn (mut a array) set_unsafe(i int, val voidptr) {
|
||||||
unsafe { vmemcpy(&u8(a.data) + u64(a.element_size) * u64(i), val, a.element_size) }
|
unsafe { vmemcpy(&u8(a.data) + u64(a.element_size) * u64(i), val, a.element_size) }
|
||||||
}
|
}
|
||||||
|
@ -685,7 +685,7 @@ fn (mut a array) push(val voidptr) {
|
||||||
|
|
||||||
// push_many implements the functionality for pushing another array.
|
// push_many implements the functionality for pushing another array.
|
||||||
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (mut a3 array) push_many(val voidptr, size int) {
|
pub fn (mut a3 array) push_many(val voidptr, size int) {
|
||||||
if size <= 0 || val == unsafe { nil } {
|
if size <= 0 || val == unsafe { nil } {
|
||||||
return
|
return
|
||||||
|
@ -741,7 +741,7 @@ pub fn (a array) reverse() array {
|
||||||
}
|
}
|
||||||
|
|
||||||
// free frees all memory occupied by the array.
|
// free frees all memory occupied by the array.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (a &array) free() {
|
pub fn (a &array) free() {
|
||||||
$if prealloc {
|
$if prealloc {
|
||||||
return
|
return
|
||||||
|
@ -894,7 +894,7 @@ pub fn (a array) contains(value voidptr) bool
|
||||||
// or `-1` if the value is not found.
|
// or `-1` if the value is not found.
|
||||||
pub fn (a array) index(value voidptr) int
|
pub fn (a array) index(value voidptr) int
|
||||||
|
|
||||||
[direct_array_access; unsafe]
|
@[direct_array_access; unsafe]
|
||||||
pub fn (mut a []string) free() {
|
pub fn (mut a []string) free() {
|
||||||
$if prealloc {
|
$if prealloc {
|
||||||
return
|
return
|
||||||
|
@ -910,7 +910,7 @@ pub fn (mut a []string) free() {
|
||||||
|
|
||||||
// str returns a string representation of an array of strings
|
// str returns a string representation of an array of strings
|
||||||
// Example: ['a', 'b', 'c'].str() // => "['a', 'b', 'c']".
|
// Example: ['a', 'b', 'c'].str() // => "['a', 'b', 'c']".
|
||||||
[direct_array_access; manualfree]
|
@[direct_array_access; manualfree]
|
||||||
pub fn (a []string) str() string {
|
pub fn (a []string) str() string {
|
||||||
mut sb_len := 4 // 2x" + 1x, + 1xspace
|
mut sb_len := 4 // 2x" + 1x, + 1xspace
|
||||||
if a.len > 0 {
|
if a.len > 0 {
|
||||||
|
@ -983,7 +983,7 @@ pub fn (mut a array) grow_cap(amount int) {
|
||||||
// Internally, it does this by copying the entire array to
|
// Internally, it does this by copying the entire array to
|
||||||
// a new memory location (creating a clone) unless the array.cap
|
// a new memory location (creating a clone) unless the array.cap
|
||||||
// is already large enough.
|
// is already large enough.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (mut a array) grow_len(amount int) {
|
pub fn (mut a array) grow_len(amount int) {
|
||||||
a.ensure_cap(a.len + amount)
|
a.ensure_cap(a.len + amount)
|
||||||
a.len += amount
|
a.len += amount
|
||||||
|
@ -991,7 +991,7 @@ pub fn (mut a array) grow_len(amount int) {
|
||||||
|
|
||||||
// pointers returns a new array, where each element
|
// pointers returns a new array, where each element
|
||||||
// is the address of the corresponding element in the array.
|
// is the address of the corresponding element in the array.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (a array) pointers() []voidptr {
|
pub fn (a array) pointers() []voidptr {
|
||||||
mut res := []voidptr{}
|
mut res := []voidptr{}
|
||||||
for i in 0 .. a.len {
|
for i in 0 .. a.len {
|
||||||
|
@ -1002,7 +1002,7 @@ pub fn (a array) pointers() []voidptr {
|
||||||
|
|
||||||
// vbytes on`voidptr` makes a V []u8 structure from a C style memory buffer.
|
// vbytes on`voidptr` makes a V []u8 structure from a C style memory buffer.
|
||||||
// NOTE: the data is reused, NOT copied!
|
// NOTE: the data is reused, NOT copied!
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (data voidptr) vbytes(len int) []u8 {
|
pub fn (data voidptr) vbytes(len int) []u8 {
|
||||||
res := array{
|
res := array{
|
||||||
element_size: 1
|
element_size: 1
|
||||||
|
@ -1015,7 +1015,7 @@ pub fn (data voidptr) vbytes(len int) []u8 {
|
||||||
|
|
||||||
// vbytes on `&u8` makes a V []u8 structure from a C style memory buffer.
|
// vbytes on `&u8` makes a V []u8 structure from a C style memory buffer.
|
||||||
// NOTE: the data is reused, NOT copied!
|
// NOTE: the data is reused, NOT copied!
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (data &u8) vbytes(len int) []u8 {
|
pub fn (data &u8) vbytes(len int) []u8 {
|
||||||
return unsafe { voidptr(data).vbytes(len) }
|
return unsafe { voidptr(data).vbytes(len) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@ fn (mut a array) ensure_cap_noscan(required int) {
|
||||||
|
|
||||||
// version of `repeat()` that handles multi dimensional arrays
|
// version of `repeat()` that handles multi dimensional arrays
|
||||||
// `unsafe` to call directly because `depth` is not checked
|
// `unsafe` to call directly because `depth` is not checked
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (a array) repeat_to_depth_noscan(count int, depth int) array {
|
fn (a array) repeat_to_depth_noscan(count int, depth int) array {
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
panic('array.repeat: count is negative: ${count}')
|
panic('array.repeat: count is negative: ${count}')
|
||||||
|
@ -165,7 +165,7 @@ fn (mut a array) insert_noscan(i int, val voidptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// insert_many inserts many values into the array from index `i`.
|
// insert_many inserts many values into the array from index `i`.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a array) insert_many_noscan(i int, val voidptr, size int) {
|
fn (mut a array) insert_many_noscan(i int, val voidptr, size int) {
|
||||||
$if !no_bounds_checking {
|
$if !no_bounds_checking {
|
||||||
if i < 0 || i > a.len {
|
if i < 0 || i > a.len {
|
||||||
|
@ -188,7 +188,7 @@ fn (mut a array) prepend_noscan(val voidptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepend_many prepends another array to this array.
|
// prepend_many prepends another array to this array.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a array) prepend_many_noscan(val voidptr, size int) {
|
fn (mut a array) prepend_many_noscan(val voidptr, size int) {
|
||||||
unsafe { a.insert_many_noscan(0, val, size) }
|
unsafe { a.insert_many_noscan(0, val, size) }
|
||||||
}
|
}
|
||||||
|
@ -217,7 +217,7 @@ fn (a array) clone_static_to_depth_noscan(depth int) array {
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursively clone given array - `unsafe` when called directly because depth is not checked
|
// recursively clone given array - `unsafe` when called directly because depth is not checked
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (a &array) clone_to_depth_noscan(depth int) array {
|
fn (a &array) clone_to_depth_noscan(depth int) array {
|
||||||
mut size := u64(a.cap) * u64(a.element_size)
|
mut size := u64(a.cap) * u64(a.element_size)
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
|
@ -254,7 +254,7 @@ fn (mut a array) push_noscan(val voidptr) {
|
||||||
|
|
||||||
// push_many implements the functionality for pushing another array.
|
// push_many implements the functionality for pushing another array.
|
||||||
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
// `val` is array.data and user facing usage is `a << [1,2,3]`
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a3 array) push_many_noscan(val voidptr, size int) {
|
fn (mut a3 array) push_many_noscan(val voidptr, size int) {
|
||||||
if size <= 0 || val == unsafe { nil } {
|
if size <= 0 || val == unsafe { nil } {
|
||||||
return
|
return
|
||||||
|
@ -298,7 +298,7 @@ fn (mut a array) grow_cap_noscan(amount int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// grow_len ensures that an array has a.len + amount of length
|
// grow_len ensures that an array has a.len + amount of length
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
fn (mut a array) grow_len_noscan(amount int) {
|
fn (mut a array) grow_len_noscan(amount int) {
|
||||||
a.ensure_cap_noscan(a.len + amount)
|
a.ensure_cap_noscan(a.len + amount)
|
||||||
a.len += amount
|
a.len += amount
|
||||||
|
|
|
@ -982,7 +982,7 @@ fn test_in_struct() {
|
||||||
assert baz.bar[0] == 3
|
assert baz.bar[0] == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn test_direct_modification() {
|
fn test_direct_modification() {
|
||||||
mut foo := [2, 0, 5]
|
mut foo := [2, 0, 5]
|
||||||
foo[1] = 3
|
foo[1] = 3
|
||||||
|
@ -1052,7 +1052,7 @@ fn test_trim() {
|
||||||
assert arr.last() == 2
|
assert arr.last() == 2
|
||||||
}
|
}
|
||||||
|
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
fn test_drop() {
|
fn test_drop() {
|
||||||
mut a := [1, 2]
|
mut a := [1, 2]
|
||||||
a << 3 // pushing assures reallocation; a.cap now should be bigger:
|
a << 3 // pushing assures reallocation; a.cap now should be bigger:
|
||||||
|
@ -1246,7 +1246,7 @@ fn test_array_last() {
|
||||||
assert s.last().val == 'a'
|
assert s.last().val == 'a'
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn test_direct_array_access() {
|
fn test_direct_array_access() {
|
||||||
mut a := [11, 22, 33, 44]
|
mut a := [11, 22, 33, 44]
|
||||||
assert a[0] == 11
|
assert a[0] == 11
|
||||||
|
@ -1259,7 +1259,7 @@ fn test_direct_array_access() {
|
||||||
assert a == [21, 24, 14, 20]
|
assert a == [21, 24, 14, 20]
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn test_direct_array_access_via_ptr() {
|
fn test_direct_array_access_via_ptr() {
|
||||||
mut b := [11, 22, 33, 44]
|
mut b := [11, 22, 33, 44]
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
[has_globals]
|
@[has_globals]
|
||||||
module builtin
|
module builtin
|
||||||
|
|
||||||
type FnExitCb = fn ()
|
type FnExitCb = fn ()
|
||||||
|
@ -6,12 +6,12 @@ type FnExitCb = fn ()
|
||||||
fn C.atexit(f FnExitCb) int
|
fn C.atexit(f FnExitCb) int
|
||||||
fn C.strerror(int) &char
|
fn C.strerror(int) &char
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn vhalt() {
|
fn vhalt() {
|
||||||
for {}
|
for {}
|
||||||
}
|
}
|
||||||
|
|
||||||
[markused]
|
@[markused]
|
||||||
fn v_segmentation_fault_handler(signal_number int) {
|
fn v_segmentation_fault_handler(signal_number int) {
|
||||||
$if freestanding {
|
$if freestanding {
|
||||||
eprintln('signal 11: segmentation fault')
|
eprintln('signal 11: segmentation fault')
|
||||||
|
@ -27,7 +27,7 @@ fn v_segmentation_fault_handler(signal_number int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// exit terminates execution immediately and returns exit `code` to the shell.
|
// exit terminates execution immediately and returns exit `code` to the shell.
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn exit(code int) {
|
pub fn exit(code int) {
|
||||||
C.exit(code)
|
C.exit(code)
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ pub fn exit(code int) {
|
||||||
// recent versions of tcc print nicer backtraces automatically
|
// recent versions of tcc print nicer backtraces automatically
|
||||||
// Note: the duplication here is because tcc_backtrace should be called directly
|
// Note: the duplication here is because tcc_backtrace should be called directly
|
||||||
// inside the panic functions.
|
// inside the panic functions.
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
|
fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
|
||||||
// Note: the order here is important for a stabler test output
|
// Note: the order here is important for a stabler test output
|
||||||
// module is less likely to change than function, etc...
|
// module is less likely to change than function, etc...
|
||||||
|
@ -83,21 +83,21 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
|
||||||
|
|
||||||
// panic_option_not_set is called by V, when you use option error propagation in your main function.
|
// panic_option_not_set is called by V, when you use option error propagation in your main function.
|
||||||
// It ends the program with a panic.
|
// It ends the program with a panic.
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn panic_option_not_set(s string) {
|
pub fn panic_option_not_set(s string) {
|
||||||
panic('option not set (${s})')
|
panic('option not set (${s})')
|
||||||
}
|
}
|
||||||
|
|
||||||
// panic_result_not_set is called by V, when you use result error propagation in your main function
|
// panic_result_not_set is called by V, when you use result error propagation in your main function
|
||||||
// It ends the program with a panic.
|
// It ends the program with a panic.
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn panic_result_not_set(s string) {
|
pub fn panic_result_not_set(s string) {
|
||||||
panic('result not set (${s})')
|
panic('result not set (${s})')
|
||||||
}
|
}
|
||||||
|
|
||||||
// panic prints a nice error message, then exits the process with exit code of 1.
|
// panic prints a nice error message, then exits the process with exit code of 1.
|
||||||
// It also shows a backtrace on most platforms.
|
// It also shows a backtrace on most platforms.
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn panic(s string) {
|
pub fn panic(s string) {
|
||||||
$if freestanding {
|
$if freestanding {
|
||||||
bare_panic(s)
|
bare_panic(s)
|
||||||
|
@ -153,7 +153,7 @@ pub fn c_error_number_str(errnum int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// panic with a C-API error message matching `errnum`
|
// panic with a C-API error message matching `errnum`
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn panic_error_number(basestr string, errnum int) {
|
pub fn panic_error_number(basestr string, errnum int) {
|
||||||
panic(basestr + c_error_number_str(errnum))
|
panic(basestr + c_error_number_str(errnum))
|
||||||
}
|
}
|
||||||
|
@ -224,7 +224,7 @@ pub fn flush_stderr() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// print prints a message to stdout. Note that unlike `eprint`, stdout is not automatically flushed.
|
// print prints a message to stdout. Note that unlike `eprint`, stdout is not automatically flushed.
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
pub fn print(s string) {
|
pub fn print(s string) {
|
||||||
$if android && !termux {
|
$if android && !termux {
|
||||||
C.android_print(C.stdout, c'%.*s\n', s.len, s.str)
|
C.android_print(C.stdout, c'%.*s\n', s.len, s.str)
|
||||||
|
@ -239,7 +239,7 @@ pub fn print(s string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// println prints a message with a line end, to stdout. Note that unlike `eprintln`, stdout is not automatically flushed.
|
// println prints a message with a line end, to stdout. Note that unlike `eprintln`, stdout is not automatically flushed.
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
pub fn println(s string) {
|
pub fn println(s string) {
|
||||||
if s.str == 0 {
|
if s.str == 0 {
|
||||||
println('println(NIL)')
|
println('println(NIL)')
|
||||||
|
@ -260,7 +260,7 @@ pub fn println(s string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
fn _writeln_to_fd(fd int, s string) {
|
fn _writeln_to_fd(fd int, s string) {
|
||||||
$if !builtin_writeln_should_write_at_once ? {
|
$if !builtin_writeln_should_write_at_once ? {
|
||||||
lf := u8(`\n`)
|
lf := u8(`\n`)
|
||||||
|
@ -280,7 +280,7 @@ fn _writeln_to_fd(fd int, s string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
fn _write_buf_to_fd(fd int, buf &u8, buf_len int) {
|
fn _write_buf_to_fd(fd int, buf &u8, buf_len int) {
|
||||||
if buf_len <= 0 {
|
if buf_len <= 0 {
|
||||||
return
|
return
|
||||||
|
@ -315,7 +315,7 @@ __global total_m = i64(0)
|
||||||
// malloc dynamically allocates a `n` bytes block of memory on the heap.
|
// malloc dynamically allocates a `n` bytes block of memory on the heap.
|
||||||
// malloc returns a `byteptr` pointing to the memory address of the allocated space.
|
// malloc returns a `byteptr` pointing to the memory address of the allocated space.
|
||||||
// unlike the `calloc` family of functions - malloc will not zero the memory block.
|
// unlike the `calloc` family of functions - malloc will not zero the memory block.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn malloc(n isize) &u8 {
|
pub fn malloc(n isize) &u8 {
|
||||||
$if trace_malloc ? {
|
$if trace_malloc ? {
|
||||||
total_m += n
|
total_m += n
|
||||||
|
@ -358,7 +358,7 @@ pub fn malloc(n isize) &u8 {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn malloc_noscan(n isize) &u8 {
|
pub fn malloc_noscan(n isize) &u8 {
|
||||||
$if trace_malloc ? {
|
$if trace_malloc ? {
|
||||||
total_m += n
|
total_m += n
|
||||||
|
@ -405,7 +405,7 @@ pub fn malloc_noscan(n isize) &u8 {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn __at_least_one(how_many u64) u64 {
|
fn __at_least_one(how_many u64) u64 {
|
||||||
// handle the case for allocating memory for empty structs, which have sizeof(EmptyStruct) == 0
|
// handle the case for allocating memory for empty structs, which have sizeof(EmptyStruct) == 0
|
||||||
// in this case, just allocate a single byte, avoiding the panic for malloc(0)
|
// in this case, just allocate a single byte, avoiding the panic for malloc(0)
|
||||||
|
@ -417,7 +417,7 @@ fn __at_least_one(how_many u64) u64 {
|
||||||
|
|
||||||
// malloc_uncollectable dynamically allocates a `n` bytes block of memory
|
// malloc_uncollectable dynamically allocates a `n` bytes block of memory
|
||||||
// on the heap, which will NOT be garbage-collected (but its contents will).
|
// on the heap, which will NOT be garbage-collected (but its contents will).
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn malloc_uncollectable(n isize) &u8 {
|
pub fn malloc_uncollectable(n isize) &u8 {
|
||||||
$if trace_malloc ? {
|
$if trace_malloc ? {
|
||||||
total_m += n
|
total_m += n
|
||||||
|
@ -462,7 +462,7 @@ pub fn malloc_uncollectable(n isize) &u8 {
|
||||||
// The `b byteptr` must be a pointer to an existing memory block
|
// The `b byteptr` must be a pointer to an existing memory block
|
||||||
// previously allocated with `malloc`, `v_calloc` or `vcalloc`.
|
// previously allocated with `malloc`, `v_calloc` or `vcalloc`.
|
||||||
// Please, see also realloc_data, and use it instead if possible.
|
// Please, see also realloc_data, and use it instead if possible.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn v_realloc(b &u8, n isize) &u8 {
|
pub fn v_realloc(b &u8, n isize) &u8 {
|
||||||
$if trace_realloc ? {
|
$if trace_realloc ? {
|
||||||
C.fprintf(C.stderr, c'v_realloc %6d\n', n)
|
C.fprintf(C.stderr, c'v_realloc %6d\n', n)
|
||||||
|
@ -493,7 +493,7 @@ pub fn v_realloc(b &u8, n isize) &u8 {
|
||||||
// instead of `v_realloc`, at least during development, because `realloc_data`
|
// instead of `v_realloc`, at least during development, because `realloc_data`
|
||||||
// can make debugging easier, when you compile your program with
|
// can make debugging easier, when you compile your program with
|
||||||
// `-d debug_realloc`.
|
// `-d debug_realloc`.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn realloc_data(old_data &u8, old_size int, new_size int) &u8 {
|
pub fn realloc_data(old_data &u8, old_size int, new_size int) &u8 {
|
||||||
$if trace_realloc ? {
|
$if trace_realloc ? {
|
||||||
C.fprintf(C.stderr, c'realloc_data old_size: %6d new_size: %6d\n', old_size, new_size)
|
C.fprintf(C.stderr, c'realloc_data old_size: %6d new_size: %6d\n', old_size, new_size)
|
||||||
|
@ -582,7 +582,7 @@ pub fn vcalloc_noscan(n isize) &u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// free allows for manually freeing memory allocated at the address `ptr`.
|
// free allows for manually freeing memory allocated at the address `ptr`.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn free(ptr voidptr) {
|
pub fn free(ptr voidptr) {
|
||||||
$if prealloc {
|
$if prealloc {
|
||||||
return
|
return
|
||||||
|
@ -604,7 +604,7 @@ pub fn free(ptr voidptr) {
|
||||||
// memdup then copies the contents of `src` into the allocated space and
|
// memdup then copies the contents of `src` into the allocated space and
|
||||||
// returns a pointer to the newly allocated space.
|
// returns a pointer to the newly allocated space.
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn memdup(src voidptr, sz isize) voidptr {
|
pub fn memdup(src voidptr, sz isize) voidptr {
|
||||||
$if trace_memdup ? {
|
$if trace_memdup ? {
|
||||||
C.fprintf(C.stderr, c'memdup size: %10d\n', sz)
|
C.fprintf(C.stderr, c'memdup size: %10d\n', sz)
|
||||||
|
@ -618,7 +618,7 @@ pub fn memdup(src voidptr, sz isize) voidptr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn memdup_noscan(src voidptr, sz isize) voidptr {
|
pub fn memdup_noscan(src voidptr, sz isize) voidptr {
|
||||||
$if trace_memdup ? {
|
$if trace_memdup ? {
|
||||||
C.fprintf(C.stderr, c'memdup_noscan size: %10d\n', sz)
|
C.fprintf(C.stderr, c'memdup_noscan size: %10d\n', sz)
|
||||||
|
@ -636,7 +636,7 @@ pub fn memdup_noscan(src voidptr, sz isize) voidptr {
|
||||||
// on the heap, which will NOT be garbage-collected (but its contents will).
|
// on the heap, which will NOT be garbage-collected (but its contents will).
|
||||||
// memdup_uncollectable then copies the contents of `src` into the allocated
|
// memdup_uncollectable then copies the contents of `src` into the allocated
|
||||||
// space and returns a pointer to the newly allocated space.
|
// space and returns a pointer to the newly allocated space.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn memdup_uncollectable(src voidptr, sz isize) voidptr {
|
pub fn memdup_uncollectable(src voidptr, sz isize) voidptr {
|
||||||
$if trace_memdup ? {
|
$if trace_memdup ? {
|
||||||
C.fprintf(C.stderr, c'memdup_uncollectable size: %10d\n', sz)
|
C.fprintf(C.stderr, c'memdup_uncollectable size: %10d\n', sz)
|
||||||
|
@ -680,7 +680,7 @@ pub fn gc_memory_use() usize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
fn v_fixed_index(i int, len int) int {
|
fn v_fixed_index(i int, len int) int {
|
||||||
$if !no_bounds_checking {
|
$if !no_bounds_checking {
|
||||||
if i < 0 || i >= len {
|
if i < 0 || i >= len {
|
||||||
|
@ -695,8 +695,8 @@ fn v_fixed_index(i int, len int) int {
|
||||||
// They are used internally by V's builtin; for user code, it is much
|
// They are used internally by V's builtin; for user code, it is much
|
||||||
// more convenient to just use `os.args` instead.
|
// more convenient to just use `os.args` instead.
|
||||||
|
|
||||||
[markused]
|
@[markused]
|
||||||
__global g_main_argc = int(0)
|
__global g_main_argc = int(0)
|
||||||
|
|
||||||
[markused]
|
@[markused]
|
||||||
__global g_main_argv = unsafe { nil }
|
__global g_main_argv = unsafe { nil }
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
[has_globals]
|
@[has_globals]
|
||||||
module builtin
|
module builtin
|
||||||
|
|
||||||
// isnil returns true if an object is nil (only for C objects).
|
// isnil returns true if an object is nil (only for C objects).
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn isnil(v voidptr) bool {
|
pub fn isnil(v voidptr) bool {
|
||||||
return v == 0
|
return v == 0
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ pub:
|
||||||
|
|
||||||
// free frees the memory occupied by the assertion meta data. It is called automatically by
|
// free frees the memory occupied by the assertion meta data. It is called automatically by
|
||||||
// the code, that V's test framework generates, after all other callbacks have been called.
|
// the code, that V's test framework generates, after all other callbacks have been called.
|
||||||
[manualfree; unsafe]
|
@[manualfree; unsafe]
|
||||||
pub fn (ami &VAssertMetaInfo) free() {
|
pub fn (ami &VAssertMetaInfo) free() {
|
||||||
unsafe {
|
unsafe {
|
||||||
ami.fpath.free()
|
ami.fpath.free()
|
||||||
|
|
|
@ -2,19 +2,19 @@ module builtin
|
||||||
|
|
||||||
// These are just dummy implementations to appease linking on musl/alpine
|
// These are just dummy implementations to appease linking on musl/alpine
|
||||||
|
|
||||||
[export: 'backtrace_symbols']
|
@[export: 'backtrace_symbols']
|
||||||
[weak]
|
@[weak]
|
||||||
fn vbacktrace_symbols(const_buffer &voidptr, size int) &&char {
|
fn vbacktrace_symbols(const_buffer &voidptr, size int) &&char {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
[export: 'backtrace']
|
@[export: 'backtrace']
|
||||||
[weak]
|
@[weak]
|
||||||
fn vbacktrace(buffer &voidptr, size int) int {
|
fn vbacktrace(buffer &voidptr, size int) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
[export: 'backtrace_symbols_fd']
|
@[export: 'backtrace_symbols_fd']
|
||||||
[weak]
|
@[weak]
|
||||||
fn vbacktrace_symbols_fd(const_buffer &voidptr, size int, fd int) {
|
fn vbacktrace_symbols_fd(const_buffer &voidptr, size int, fd int) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
[has_globals]
|
@[has_globals]
|
||||||
module builtin
|
module builtin
|
||||||
|
|
||||||
#flag -I@VEXEROOT/thirdparty/libbacktrace
|
#flag -I@VEXEROOT/thirdparty/libbacktrace
|
||||||
|
@ -85,7 +85,7 @@ fn bt_error_handler(data voidptr, msg &char, errnum int) {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
[noinline]
|
@[noinline]
|
||||||
fn print_libbacktrace(frames_to_skip int) {
|
fn print_libbacktrace(frames_to_skip int) {
|
||||||
$if no_backtrace ? {
|
$if no_backtrace ? {
|
||||||
return
|
return
|
||||||
|
@ -94,7 +94,7 @@ fn print_libbacktrace(frames_to_skip int) {
|
||||||
C.backtrace_full(bt_state, frames_to_skip, bt_print_callback, bt_error_callback, data)
|
C.backtrace_full(bt_state, frames_to_skip, bt_print_callback, bt_error_callback, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
[noinline]
|
@[noinline]
|
||||||
fn eprint_libbacktrace(frames_to_skip int) {
|
fn eprint_libbacktrace(frames_to_skip int) {
|
||||||
$if no_backtrace ? {
|
$if no_backtrace ? {
|
||||||
return
|
return
|
||||||
|
|
|
@ -14,7 +14,7 @@ fn break_if_debugger_attached() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[markused; noreturn]
|
@[markused; noreturn]
|
||||||
pub fn panic_lasterr(base string) {
|
pub fn panic_lasterr(base string) {
|
||||||
// TODO: use strerror_r and errno
|
// TODO: use strerror_r and errno
|
||||||
panic(base + ' unknown')
|
panic(base + ' unknown')
|
||||||
|
|
|
@ -3,6 +3,6 @@ module builtin
|
||||||
fn print_libbacktrace(frames_to_skip int) {
|
fn print_libbacktrace(frames_to_skip int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
[noinline]
|
@[noinline]
|
||||||
fn eprint_libbacktrace(frames_to_skip int) {
|
fn eprint_libbacktrace(frames_to_skip int) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ fn test_g_main_argv() {
|
||||||
assert first_arg.contains('builtin_test')
|
assert first_arg.contains('builtin_test')
|
||||||
}
|
}
|
||||||
|
|
||||||
[if windows]
|
@[if windows]
|
||||||
fn test_bool_size() {
|
fn test_bool_size() {
|
||||||
println(@LOCATION)
|
println(@LOCATION)
|
||||||
assert sizeof(C.BOOL) == 4
|
assert sizeof(C.BOOL) == 4
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
[has_globals]
|
@[has_globals]
|
||||||
module builtin
|
module builtin
|
||||||
|
|
||||||
// g_original_codepage - used to restore the original windows console code page when exiting
|
// g_original_codepage - used to restore the original windows console code page when exiting
|
||||||
|
@ -97,7 +97,7 @@ fn add_vectored_exception_handler(handler VectoredExceptionHandler) {
|
||||||
C.AddVectoredExceptionHandler(1, voidptr(handler))
|
C.AddVectoredExceptionHandler(1, voidptr(handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
[callconv: stdcall]
|
@[callconv: stdcall]
|
||||||
fn unhandled_exception_handler(e &ExceptionPointers) int {
|
fn unhandled_exception_handler(e &ExceptionPointers) int {
|
||||||
match e.exception_record.code {
|
match e.exception_record.code {
|
||||||
// These are 'used' by the backtrace printer
|
// These are 'used' by the backtrace printer
|
||||||
|
@ -164,7 +164,7 @@ pub fn winapi_lasterr_str() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// panic with an error message generated from WinAPI's `LastError`
|
// panic with an error message generated from WinAPI's `LastError`
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn panic_lasterr(base string) {
|
pub fn panic_lasterr(base string) {
|
||||||
panic(base + winapi_lasterr_str())
|
panic(base + winapi_lasterr_str())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module builtin
|
module builtin
|
||||||
|
|
||||||
[typedef]
|
@[typedef]
|
||||||
pub struct C.FILE {}
|
pub struct C.FILE {}
|
||||||
|
|
||||||
// <string.h>
|
// <string.h>
|
||||||
|
@ -12,7 +12,7 @@ fn C.memmove(dest voidptr, const_src voidptr, n usize) voidptr
|
||||||
|
|
||||||
fn C.memset(str voidptr, c int, n usize) voidptr
|
fn C.memset(str voidptr, c int, n usize) voidptr
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.calloc(int, int) &u8
|
fn C.calloc(int, int) &u8
|
||||||
|
|
||||||
fn C.atoi(&char) int
|
fn C.atoi(&char) int
|
||||||
|
@ -23,7 +23,7 @@ fn C.realloc(a &u8, b int) &u8
|
||||||
|
|
||||||
fn C.free(ptr voidptr)
|
fn C.free(ptr voidptr)
|
||||||
|
|
||||||
[noreturn; trusted]
|
@[noreturn; trusted]
|
||||||
fn C.exit(code int)
|
fn C.exit(code int)
|
||||||
|
|
||||||
fn C.qsort(base voidptr, items usize, item_size usize, cb C.qsort_callback_func)
|
fn C.qsort(base voidptr, items usize, item_size usize, cb C.qsort_callback_func)
|
||||||
|
@ -34,7 +34,7 @@ fn C.strlen(s &char) int
|
||||||
|
|
||||||
fn C.sscanf(&u8, &u8, ...&u8) int
|
fn C.sscanf(&u8, &u8, ...&u8) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.isdigit(c int) bool
|
fn C.isdigit(c int) bool
|
||||||
|
|
||||||
// stdio.h
|
// stdio.h
|
||||||
|
@ -78,7 +78,7 @@ fn C.strrchr(s &char, c int) &char
|
||||||
fn C.strchr(s &char, c int) &char
|
fn C.strchr(s &char, c int) &char
|
||||||
|
|
||||||
// process execution, os.process:
|
// process execution, os.process:
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.getpid() int
|
fn C.getpid() int
|
||||||
|
|
||||||
fn C.getuid() int
|
fn C.getuid() int
|
||||||
|
@ -101,14 +101,14 @@ fn C._execvp(cmd_path &char, args &&char) int
|
||||||
|
|
||||||
fn C.strcmp(s1 &char, s2 &char) int
|
fn C.strcmp(s1 &char, s2 &char) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.fork() int
|
fn C.fork() int
|
||||||
|
|
||||||
fn C.wait(status &int) int
|
fn C.wait(status &int) int
|
||||||
|
|
||||||
fn C.waitpid(pid int, status &int, options int) int
|
fn C.waitpid(pid int, status &int, options int) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.kill(pid int, sig int) int
|
fn C.kill(pid int, sig int) int
|
||||||
|
|
||||||
fn C.setenv(&char, &char, int) int
|
fn C.setenv(&char, &char, int) int
|
||||||
|
@ -135,24 +135,24 @@ fn C.rename(old_filename &char, new_filename &char) int
|
||||||
|
|
||||||
fn C.fgets(str &char, n int, stream &C.FILE) int
|
fn C.fgets(str &char, n int, stream &C.FILE) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.sigemptyset() int
|
fn C.sigemptyset() int
|
||||||
|
|
||||||
fn C.getcwd(buf &char, size usize) &char
|
fn C.getcwd(buf &char, size usize) &char
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.mktime() int
|
fn C.mktime() int
|
||||||
|
|
||||||
fn C.gettimeofday(tv &C.timeval, tz &C.timezone) int
|
fn C.gettimeofday(tv &C.timeval, tz &C.timezone) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.sleep(seconds u32) u32
|
fn C.sleep(seconds u32) u32
|
||||||
|
|
||||||
// fn C.usleep(usec useconds_t) int
|
// fn C.usleep(usec useconds_t) int
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.usleep(usec u32) int
|
fn C.usleep(usec u32) int
|
||||||
|
|
||||||
[typedef]
|
@[typedef]
|
||||||
pub struct C.DIR {
|
pub struct C.DIR {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,30 +164,30 @@ fn C.closedir(dirp &C.DIR) int
|
||||||
fn C.mkdir(path &char, mode u32) int
|
fn C.mkdir(path &char, mode u32) int
|
||||||
|
|
||||||
// C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive)
|
// C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive)
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.rand() int
|
fn C.rand() int
|
||||||
|
|
||||||
// C.srand seeds the internal PRNG with the given value.
|
// C.srand seeds the internal PRNG with the given value.
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.srand(seed u32)
|
fn C.srand(seed u32)
|
||||||
|
|
||||||
fn C.atof(str &char) f64
|
fn C.atof(str &char) f64
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.tolower(c int) int
|
fn C.tolower(c int) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.toupper(c int) int
|
fn C.toupper(c int) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.isspace(c int) int
|
fn C.isspace(c int) int
|
||||||
|
|
||||||
fn C.strchr(s &char, c int) &char
|
fn C.strchr(s &char, c int) &char
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.getchar() int
|
fn C.getchar() int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.putchar(int) int
|
fn C.putchar(int) int
|
||||||
|
|
||||||
fn C.strdup(s &char) &char
|
fn C.strdup(s &char) &char
|
||||||
|
@ -198,33 +198,33 @@ fn C.strcasecmp(s &char, s2 &char) int
|
||||||
|
|
||||||
fn C.strncmp(s &char, s2 &char, n int) int
|
fn C.strncmp(s &char, s2 &char, n int) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.strerror(int) &char
|
fn C.strerror(int) &char
|
||||||
|
|
||||||
fn C.snprintf(str &char, size usize, format &char, opt ...voidptr) int
|
fn C.snprintf(str &char, size usize, format &char, opt ...voidptr) int
|
||||||
|
|
||||||
fn C.fprintf(voidptr, &char, ...voidptr)
|
fn C.fprintf(voidptr, &char, ...voidptr)
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.WIFEXITED(status int) bool
|
fn C.WIFEXITED(status int) bool
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.WEXITSTATUS(status int) int
|
fn C.WEXITSTATUS(status int) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.WIFSIGNALED(status int) bool
|
fn C.WIFSIGNALED(status int) bool
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.WTERMSIG(status int) int
|
fn C.WTERMSIG(status int) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.isatty(fd int) int
|
fn C.isatty(fd int) int
|
||||||
|
|
||||||
fn C.syscall(number int, va ...voidptr) int
|
fn C.syscall(number int, va ...voidptr) int
|
||||||
|
|
||||||
fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr, newlen usize) int
|
fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr, newlen usize) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C._fileno(int) int
|
fn C._fileno(int) int
|
||||||
|
|
||||||
type C.intptr_t = voidptr
|
type C.intptr_t = voidptr
|
||||||
|
@ -251,7 +251,7 @@ fn C.GetComputerNameW(&u16, &u32) bool
|
||||||
|
|
||||||
fn C.GetUserNameW(&u16, &u32) bool
|
fn C.GetUserNameW(&u16, &u32) bool
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.SendMessageTimeout() isize
|
fn C.SendMessageTimeout() isize
|
||||||
|
|
||||||
fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) isize
|
fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) isize
|
||||||
|
@ -286,7 +286,7 @@ fn C.SetConsoleMode(voidptr, u32) bool
|
||||||
|
|
||||||
fn C.GetConsoleMode(voidptr, &u32) bool
|
fn C.GetConsoleMode(voidptr, &u32) bool
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.GetCurrentProcessId() u32
|
fn C.GetCurrentProcessId() u32
|
||||||
|
|
||||||
fn C.wprintf()
|
fn C.wprintf()
|
||||||
|
@ -336,7 +336,7 @@ fn C._fullpath() int
|
||||||
|
|
||||||
fn C.GetFullPathName(voidptr, u32, voidptr, voidptr) u32
|
fn C.GetFullPathName(voidptr, u32, voidptr, voidptr) u32
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.GetCommandLine() voidptr
|
fn C.GetCommandLine() voidptr
|
||||||
|
|
||||||
fn C.LocalFree()
|
fn C.LocalFree()
|
||||||
|
@ -358,15 +358,15 @@ fn C.CloseHandle(voidptr) int
|
||||||
|
|
||||||
fn C.GetExitCodeProcess(hProcess voidptr, lpExitCode &u32)
|
fn C.GetExitCodeProcess(hProcess voidptr, lpExitCode &u32)
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.GetTickCount() i64
|
fn C.GetTickCount() i64
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.Sleep(dwMilliseconds u32)
|
fn C.Sleep(dwMilliseconds u32)
|
||||||
|
|
||||||
fn C.WSAStartup(u16, &voidptr) int
|
fn C.WSAStartup(u16, &voidptr) int
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.WSAGetLastError() int
|
fn C.WSAGetLastError() int
|
||||||
|
|
||||||
fn C.closesocket(int) int
|
fn C.closesocket(int) int
|
||||||
|
@ -379,7 +379,7 @@ fn C.vschannel_cleanup(&C.TlsContext)
|
||||||
|
|
||||||
fn C.URLDownloadToFile(int, &u16, &u16, int, int)
|
fn C.URLDownloadToFile(int, &u16, &u16, int, int)
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.GetLastError() u32
|
fn C.GetLastError() u32
|
||||||
|
|
||||||
fn C.CreateDirectory(&u8, int) bool
|
fn C.CreateDirectory(&u8, int) bool
|
||||||
|
@ -464,14 +464,14 @@ fn C.sem_timedwait(voidptr, voidptr) int
|
||||||
fn C.sem_destroy(voidptr) int
|
fn C.sem_destroy(voidptr) int
|
||||||
|
|
||||||
// MacOS semaphore functions
|
// MacOS semaphore functions
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.dispatch_semaphore_create(i64) voidptr
|
fn C.dispatch_semaphore_create(i64) voidptr
|
||||||
|
|
||||||
fn C.dispatch_semaphore_signal(voidptr) i64
|
fn C.dispatch_semaphore_signal(voidptr) i64
|
||||||
|
|
||||||
fn C.dispatch_semaphore_wait(voidptr, u64) i64
|
fn C.dispatch_semaphore_wait(voidptr, u64) i64
|
||||||
|
|
||||||
[trusted]
|
@[trusted]
|
||||||
fn C.dispatch_time(u64, i64) u64
|
fn C.dispatch_time(u64, i64) u64
|
||||||
|
|
||||||
fn C.dispatch_release(voidptr)
|
fn C.dispatch_release(voidptr)
|
||||||
|
|
|
@ -2,14 +2,14 @@ module builtin
|
||||||
|
|
||||||
// vstrlen returns the V length of the C string `s` (0 terminator is not counted).
|
// vstrlen returns the V length of the C string `s` (0 terminator is not counted).
|
||||||
// The C string is expected to be a &byte pointer.
|
// The C string is expected to be a &byte pointer.
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
pub fn vstrlen(s &u8) int {
|
pub fn vstrlen(s &u8) int {
|
||||||
return unsafe { C.strlen(&char(s)) }
|
return unsafe { C.strlen(&char(s)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// vstrlen_char returns the V length of the C string `s` (0 terminator is not counted).
|
// vstrlen_char returns the V length of the C string `s` (0 terminator is not counted).
|
||||||
// The C string is expected to be a &char pointer.
|
// The C string is expected to be a &char pointer.
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
pub fn vstrlen_char(s &char) int {
|
pub fn vstrlen_char(s &char) int {
|
||||||
return unsafe { C.strlen(s) }
|
return unsafe { C.strlen(s) }
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ pub fn vstrlen_char(s &char) int {
|
||||||
// vmemcpy copies n bytes from memory area src to memory area dest.
|
// vmemcpy copies n bytes from memory area src to memory area dest.
|
||||||
// The memory areas *MUST NOT OVERLAP*. Use vmemmove, if the memory
|
// The memory areas *MUST NOT OVERLAP*. Use vmemmove, if the memory
|
||||||
// areas do overlap. vmemcpy returns a pointer to `dest`.
|
// areas do overlap. vmemcpy returns a pointer to `dest`.
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
pub fn vmemcpy(dest voidptr, const_src voidptr, n isize) voidptr {
|
pub fn vmemcpy(dest voidptr, const_src voidptr, n isize) voidptr {
|
||||||
$if trace_vmemcpy ? {
|
$if trace_vmemcpy ? {
|
||||||
C.fprintf(C.stderr, c'vmemcpy dest: %p src: %p n: %6ld\n', dest, const_src, n)
|
C.fprintf(C.stderr, c'vmemcpy dest: %p src: %p n: %6ld\n', dest, const_src, n)
|
||||||
|
@ -32,7 +32,7 @@ pub fn vmemcpy(dest voidptr, const_src voidptr, n isize) voidptr {
|
||||||
// in `src` are first copied into a temporary array that does not overlap
|
// in `src` are first copied into a temporary array that does not overlap
|
||||||
// `src` or `dest`, and the bytes are then copied from the temporary array
|
// `src` or `dest`, and the bytes are then copied from the temporary array
|
||||||
// to `dest`. vmemmove returns a pointer to `dest`.
|
// to `dest`. vmemmove returns a pointer to `dest`.
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
pub fn vmemmove(dest voidptr, const_src voidptr, n isize) voidptr {
|
pub fn vmemmove(dest voidptr, const_src voidptr, n isize) voidptr {
|
||||||
$if trace_vmemmove ? {
|
$if trace_vmemmove ? {
|
||||||
C.fprintf(C.stderr, c'vmemmove dest: %p src: %p n: %6ld\n', dest, const_src, n)
|
C.fprintf(C.stderr, c'vmemmove dest: %p src: %p n: %6ld\n', dest, const_src, n)
|
||||||
|
@ -54,7 +54,7 @@ pub fn vmemmove(dest voidptr, const_src voidptr, n isize) voidptr {
|
||||||
// secrets, because the required CPU time depends on the number of equal bytes.
|
// secrets, because the required CPU time depends on the number of equal bytes.
|
||||||
// You should use a function that performs comparisons in constant time for
|
// You should use a function that performs comparisons in constant time for
|
||||||
// this.
|
// this.
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
pub fn vmemcmp(const_s1 voidptr, const_s2 voidptr, n isize) int {
|
pub fn vmemcmp(const_s1 voidptr, const_s2 voidptr, n isize) int {
|
||||||
$if trace_vmemcmp ? {
|
$if trace_vmemcmp ? {
|
||||||
C.fprintf(C.stderr, c'vmemcmp s1: %p s2: %p n: %6ld\n', const_s1, const_s2, n)
|
C.fprintf(C.stderr, c'vmemcmp s1: %p s2: %p n: %6ld\n', const_s1, const_s2, n)
|
||||||
|
@ -66,7 +66,7 @@ pub fn vmemcmp(const_s1 voidptr, const_s2 voidptr, n isize) int {
|
||||||
|
|
||||||
// vmemset fills the first `n` bytes of the memory area pointed to by `s`,
|
// vmemset fills the first `n` bytes of the memory area pointed to by `s`,
|
||||||
// with the constant byte `c`. It returns a pointer to the memory area `s`.
|
// with the constant byte `c`. It returns a pointer to the memory area `s`.
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
pub fn vmemset(s voidptr, c int, n isize) voidptr {
|
pub fn vmemset(s voidptr, c int, n isize) voidptr {
|
||||||
$if trace_vmemset ? {
|
$if trace_vmemset ? {
|
||||||
C.fprintf(C.stderr, c'vmemset s: %p c: %d n: %6ld\n', s, c, n)
|
C.fprintf(C.stderr, c'vmemset s: %p c: %d n: %6ld\n', s, c, n)
|
||||||
|
@ -78,7 +78,7 @@ pub fn vmemset(s voidptr, c int, n isize) voidptr {
|
||||||
|
|
||||||
type FnSortCB = fn (const_a voidptr, const_b voidptr) int
|
type FnSortCB = fn (const_a voidptr, const_b voidptr) int
|
||||||
|
|
||||||
[inline; unsafe]
|
@[inline; unsafe]
|
||||||
fn vqsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
|
fn vqsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
|
||||||
C.qsort(base, nmemb, size, voidptr(sort_cb))
|
C.qsort(base, nmemb, size, voidptr(sort_cb))
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ $if !native {
|
||||||
----- f64 to string functions -----
|
----- f64 to string functions -----
|
||||||
*/
|
*/
|
||||||
// str return a `f64` as `string` in suitable notation.
|
// str return a `f64` as `string` in suitable notation.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f64) str() string {
|
pub fn (x f64) str() string {
|
||||||
unsafe {
|
unsafe {
|
||||||
f := strconv.Float64u{
|
f := strconv.Float64u{
|
||||||
|
@ -37,7 +37,7 @@ pub fn (x f64) str() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// strg return a `f64` as `string` in "g" printf format
|
// strg return a `f64` as `string` in "g" printf format
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f64) strg() string {
|
pub fn (x f64) strg() string {
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return '0.0'
|
return '0.0'
|
||||||
|
@ -51,14 +51,14 @@ pub fn (x f64) strg() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the value of the `float_literal` as a `string`.
|
// str returns the value of the `float_literal` as a `string`.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (d float_literal) str() string {
|
pub fn (d float_literal) str() string {
|
||||||
return f64(d).str()
|
return f64(d).str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// strsci returns the `f64` as a `string` in scientific notation with `digit_num` decimals displayed, max 17 digits.
|
// strsci returns the `f64` as a `string` in scientific notation with `digit_num` decimals displayed, max 17 digits.
|
||||||
// Example: assert f64(1.234).strsci(3) == '1.234e+00'
|
// Example: assert f64(1.234).strsci(3) == '1.234e+00'
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f64) strsci(digit_num int) string {
|
pub fn (x f64) strsci(digit_num int) string {
|
||||||
mut n_digit := digit_num
|
mut n_digit := digit_num
|
||||||
if n_digit < 1 {
|
if n_digit < 1 {
|
||||||
|
@ -71,7 +71,7 @@ pub fn (x f64) strsci(digit_num int) string {
|
||||||
|
|
||||||
// strlong returns a decimal notation of the `f64` as a `string`.
|
// strlong returns a decimal notation of the `f64` as a `string`.
|
||||||
// Example: assert f64(1.23456).strlong() == '1.23456'
|
// Example: assert f64(1.23456).strlong() == '1.23456'
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f64) strlong() string {
|
pub fn (x f64) strlong() string {
|
||||||
return strconv.f64_to_str_l(x)
|
return strconv.f64_to_str_l(x)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ pub fn (x f64) strlong() string {
|
||||||
----- f32 to string functions -----
|
----- f32 to string functions -----
|
||||||
*/
|
*/
|
||||||
// str returns a `f32` as `string` in suitable notation.
|
// str returns a `f32` as `string` in suitable notation.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f32) str() string {
|
pub fn (x f32) str() string {
|
||||||
unsafe {
|
unsafe {
|
||||||
f := strconv.Float32u{
|
f := strconv.Float32u{
|
||||||
|
@ -103,7 +103,7 @@ pub fn (x f32) str() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// strg return a `f32` as `string` in "g" printf format
|
// strg return a `f32` as `string` in "g" printf format
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f32) strg() string {
|
pub fn (x f32) strg() string {
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return '0.0'
|
return '0.0'
|
||||||
|
@ -118,7 +118,7 @@ pub fn (x f32) strg() string {
|
||||||
|
|
||||||
// strsci returns the `f32` as a `string` in scientific notation with `digit_num` decimals displayed, max 8 digits.
|
// strsci returns the `f32` as a `string` in scientific notation with `digit_num` decimals displayed, max 8 digits.
|
||||||
// Example: assert f32(1.234).strsci(3) == '1.234e+00'
|
// Example: assert f32(1.234).strsci(3) == '1.234e+00'
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f32) strsci(digit_num int) string {
|
pub fn (x f32) strsci(digit_num int) string {
|
||||||
mut n_digit := digit_num
|
mut n_digit := digit_num
|
||||||
if n_digit < 1 {
|
if n_digit < 1 {
|
||||||
|
@ -130,7 +130,7 @@ pub fn (x f32) strsci(digit_num int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// strlong returns a decimal notation of the `f32` as a `string`.
|
// strlong returns a decimal notation of the `f32` as a `string`.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (x f32) strlong() string {
|
pub fn (x f32) strlong() string {
|
||||||
return strconv.f32_to_str_l(x)
|
return strconv.f32_to_str_l(x)
|
||||||
}
|
}
|
||||||
|
@ -141,42 +141,42 @@ pub fn (x f32) strlong() string {
|
||||||
*/
|
*/
|
||||||
// f32_abs returns the absolute value of `a` as a `f32` value.
|
// f32_abs returns the absolute value of `a` as a `f32` value.
|
||||||
// Example: assert f32_abs(-2.0) == 2.0
|
// Example: assert f32_abs(-2.0) == 2.0
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn f32_abs(a f32) f32 {
|
pub fn f32_abs(a f32) f32 {
|
||||||
return if a < 0 { -a } else { a }
|
return if a < 0 { -a } else { a }
|
||||||
}
|
}
|
||||||
|
|
||||||
// f64_abs returns the absolute value of `a` as a `f64` value.
|
// f64_abs returns the absolute value of `a` as a `f64` value.
|
||||||
// Example: assert f64_abs(-2.0) == f64(2.0)
|
// Example: assert f64_abs(-2.0) == f64(2.0)
|
||||||
[inline]
|
@[inline]
|
||||||
fn f64_abs(a f64) f64 {
|
fn f64_abs(a f64) f64 {
|
||||||
return if a < 0 { -a } else { a }
|
return if a < 0 { -a } else { a }
|
||||||
}
|
}
|
||||||
|
|
||||||
// f32_max returns the largest `f32` of input `a` and `b`.
|
// f32_max returns the largest `f32` of input `a` and `b`.
|
||||||
// Example: assert f32_max(2.0,3.0) == 3.0
|
// Example: assert f32_max(2.0,3.0) == 3.0
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn f32_max(a f32, b f32) f32 {
|
pub fn f32_max(a f32, b f32) f32 {
|
||||||
return if a > b { a } else { b }
|
return if a > b { a } else { b }
|
||||||
}
|
}
|
||||||
|
|
||||||
// f32_min returns the smallest `f32` of input `a` and `b`.
|
// f32_min returns the smallest `f32` of input `a` and `b`.
|
||||||
// Example: assert f32_min(2.0,3.0) == 2.0
|
// Example: assert f32_min(2.0,3.0) == 2.0
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn f32_min(a f32, b f32) f32 {
|
pub fn f32_min(a f32, b f32) f32 {
|
||||||
return if a < b { a } else { b }
|
return if a < b { a } else { b }
|
||||||
}
|
}
|
||||||
|
|
||||||
// f64_max returns the largest `f64` of input `a` and `b`.
|
// f64_max returns the largest `f64` of input `a` and `b`.
|
||||||
// Example: assert f64_max(2.0,3.0) == 3.0
|
// Example: assert f64_max(2.0,3.0) == 3.0
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn f64_max(a f64, b f64) f64 {
|
pub fn f64_max(a f64, b f64) f64 {
|
||||||
return if a > b { a } else { b }
|
return if a > b { a } else { b }
|
||||||
}
|
}
|
||||||
|
|
||||||
// f64_min returns the smallest `f64` of input `a` and `b`.
|
// f64_min returns the smallest `f64` of input `a` and `b`.
|
||||||
// Example: assert f64_min(2.0,3.0) == 2.0
|
// Example: assert f64_min(2.0,3.0) == 2.0
|
||||||
[inline]
|
@[inline]
|
||||||
fn f64_min(a f64, b f64) f64 {
|
fn f64_min(a f64, b f64) f64 {
|
||||||
return if a < b { a } else { b }
|
return if a < b { a } else { b }
|
||||||
}
|
}
|
||||||
|
@ -184,7 +184,7 @@ fn f64_min(a f64, b f64) f64 {
|
||||||
// eq_epsilon returns true if the `f32` is equal to input `b`.
|
// eq_epsilon returns true if the `f32` is equal to input `b`.
|
||||||
// using an epsilon of typically 1E-5 or higher (backend/compiler dependent).
|
// using an epsilon of typically 1E-5 or higher (backend/compiler dependent).
|
||||||
// Example: assert f32(2.0).eq_epsilon(2.0)
|
// Example: assert f32(2.0).eq_epsilon(2.0)
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (a f32) eq_epsilon(b f32) bool {
|
pub fn (a f32) eq_epsilon(b f32) bool {
|
||||||
hi := f32_max(f32_abs(a), f32_abs(b))
|
hi := f32_max(f32_abs(a), f32_abs(b))
|
||||||
delta := f32_abs(a - b)
|
delta := f32_abs(a - b)
|
||||||
|
@ -206,7 +206,7 @@ pub fn (a f32) eq_epsilon(b f32) bool {
|
||||||
// eq_epsilon returns true if the `f64` is equal to input `b`.
|
// eq_epsilon returns true if the `f64` is equal to input `b`.
|
||||||
// using an epsilon of typically 1E-9 or higher (backend/compiler dependent).
|
// using an epsilon of typically 1E-9 or higher (backend/compiler dependent).
|
||||||
// Example: assert f64(2.0).eq_epsilon(2.0)
|
// Example: assert f64(2.0).eq_epsilon(2.0)
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (a f64) eq_epsilon(b f64) bool {
|
pub fn (a f64) eq_epsilon(b f64) bool {
|
||||||
hi := f64_max(f64_abs(a), f64_abs(b))
|
hi := f64_max(f64_abs(a), f64_abs(b))
|
||||||
delta := f64_abs(a - b)
|
delta := f64_abs(a - b)
|
||||||
|
|
|
@ -76,7 +76,7 @@ pub const (
|
||||||
|
|
||||||
// This implementation is the quickest with gcc -O2
|
// This implementation is the quickest with gcc -O2
|
||||||
// str_l returns the string representation of the integer nn with max chars.
|
// str_l returns the string representation of the integer nn with max chars.
|
||||||
[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
fn (nn int) str_l(max int) string {
|
fn (nn int) str_l(max int) string {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut n := i64(nn)
|
mut n := i64(nn)
|
||||||
|
@ -175,7 +175,7 @@ pub fn (n int) str() string {
|
||||||
|
|
||||||
// str returns the value of the `u32` as a `string`.
|
// str returns the value of the `u32` as a `string`.
|
||||||
// Example: assert u32(20000).str() == '20000'
|
// Example: assert u32(20000).str() == '20000'
|
||||||
[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
pub fn (nn u32) str() string {
|
pub fn (nn u32) str() string {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
|
@ -212,14 +212,14 @@ pub fn (nn u32) str() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the value of the `int_literal` as a `string`.
|
// str returns the value of the `int_literal` as a `string`.
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (n int_literal) str() string {
|
pub fn (n int_literal) str() string {
|
||||||
return i64(n).str()
|
return i64(n).str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the value of the `i64` as a `string`.
|
// str returns the value of the `i64` as a `string`.
|
||||||
// Example: assert i64(-200000).str() == '-200000'
|
// Example: assert i64(-200000).str() == '-200000'
|
||||||
[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
pub fn (nn i64) str() string {
|
pub fn (nn i64) str() string {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
|
@ -268,7 +268,7 @@ pub fn (nn i64) str() string {
|
||||||
|
|
||||||
// str returns the value of the `u64` as a `string`.
|
// str returns the value of the `u64` as a `string`.
|
||||||
// Example: assert u64(2000000).str() == '2000000'
|
// Example: assert u64(2000000).str() == '2000000'
|
||||||
[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
pub fn (nn u64) str() string {
|
pub fn (nn u64) str() string {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
|
@ -317,7 +317,7 @@ pub fn (b bool) str() string {
|
||||||
//
|
//
|
||||||
|
|
||||||
// u64_to_hex converts the number `nn` to a (zero padded if necessary) hexadecimal `string`.
|
// u64_to_hex converts the number `nn` to a (zero padded if necessary) hexadecimal `string`.
|
||||||
[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
fn u64_to_hex(nn u64, len u8) string {
|
fn u64_to_hex(nn u64, len u8) string {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
mut buf := [17]u8{}
|
mut buf := [17]u8{}
|
||||||
|
@ -332,7 +332,7 @@ fn u64_to_hex(nn u64, len u8) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// u64_to_hex_no_leading_zeros converts the number `nn` to hexadecimal `string`.
|
// u64_to_hex_no_leading_zeros converts the number `nn` to hexadecimal `string`.
|
||||||
[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
fn u64_to_hex_no_leading_zeros(nn u64, len u8) string {
|
fn u64_to_hex_no_leading_zeros(nn u64, len u8) string {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
mut buf := [17]u8{}
|
mut buf := [17]u8{}
|
||||||
|
@ -527,7 +527,7 @@ pub fn (b u8) ascii_str() string {
|
||||||
|
|
||||||
// str_escaped returns the contents of `byte` as an escaped `string`.
|
// str_escaped returns the contents of `byte` as an escaped `string`.
|
||||||
// Example: assert u8(0).str_escaped() == r'`\0`'
|
// Example: assert u8(0).str_escaped() == r'`\0`'
|
||||||
[manualfree]
|
@[manualfree]
|
||||||
pub fn (b u8) str_escaped() string {
|
pub fn (b u8) str_escaped() string {
|
||||||
str := match b {
|
str := match b {
|
||||||
0 {
|
0 {
|
||||||
|
@ -573,7 +573,7 @@ pub fn (b u8) str_escaped() string {
|
||||||
// is_capital returns `true`, if the byte is a Latin capital letter.
|
// is_capital returns `true`, if the byte is a Latin capital letter.
|
||||||
// Example: assert `H`.is_capital() == true
|
// Example: assert `H`.is_capital() == true
|
||||||
// Example: assert `h`.is_capital() == false
|
// Example: assert `h`.is_capital() == false
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_capital() bool {
|
pub fn (c u8) is_capital() bool {
|
||||||
return c >= `A` && c <= `Z`
|
return c >= `A` && c <= `Z`
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,7 +105,7 @@ pub fn (mut a array) trim(index int) {
|
||||||
#return result;
|
#return result;
|
||||||
#}
|
#}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (a array) repeat_to_depth(count int, depth int) array {
|
pub fn (a array) repeat_to_depth(count int, depth int) array {
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
panic('array.repeat: count is negative: ${count}')
|
panic('array.repeat: count is negative: ${count}')
|
||||||
|
@ -297,7 +297,7 @@ pub fn (mut a array) prepend(val voidptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepend_many prepends another array to this array.
|
// prepend_many prepends another array to this array.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (mut a array) prepend_many(val voidptr, size int) {
|
pub fn (mut a array) prepend_many(val voidptr, size int) {
|
||||||
unsafe { a.insert_many(0, val, size) }
|
unsafe { a.insert_many(0, val, size) }
|
||||||
}
|
}
|
||||||
|
@ -369,7 +369,7 @@ pub fn (mut a array) delete_last() {
|
||||||
#a.val.arr.arr.pop();
|
#a.val.arr.arr.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (a &array) free() {
|
pub fn (a &array) free() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -975,7 +975,7 @@ fn test_in_struct() {
|
||||||
assert baz.bar[0] == 3
|
assert baz.bar[0] == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn test_direct_modification() {
|
fn test_direct_modification() {
|
||||||
mut foo := [2, 0, 5]
|
mut foo := [2, 0, 5]
|
||||||
foo[1] = 3
|
foo[1] = 3
|
||||||
|
@ -1208,7 +1208,7 @@ fn test_array_last() {
|
||||||
assert s.last().val == 'a'
|
assert s.last().val == 'a'
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn test_direct_array_access() {
|
fn test_direct_array_access() {
|
||||||
mut a := [11, 22, 33, 44]
|
mut a := [11, 22, 33, 44]
|
||||||
assert a[0] == 11
|
assert a[0] == 11
|
||||||
|
@ -1221,7 +1221,7 @@ fn test_direct_array_access() {
|
||||||
assert a == [21, 24, 14, 20]
|
assert a == [21, 24, 14, 20]
|
||||||
}
|
}
|
||||||
|
|
||||||
[direct_array_access]
|
@[direct_array_access]
|
||||||
fn test_direct_array_access_via_ptr() {
|
fn test_direct_array_access_via_ptr() {
|
||||||
mut b := [11, 22, 33, 44]
|
mut b := [11, 22, 33, 44]
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -2,7 +2,7 @@ module builtin
|
||||||
|
|
||||||
// used to generate JS throw statements.
|
// used to generate JS throw statements.
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn js_throw(s any) {
|
pub fn js_throw(s any) {
|
||||||
#throw s
|
#throw s
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ pub fn eprint(s string) {
|
||||||
// Exits the process in node, and halts execution in the browser
|
// Exits the process in node, and halts execution in the browser
|
||||||
// because `process.exit` is undefined. Workaround for not having
|
// because `process.exit` is undefined. Workaround for not having
|
||||||
// a 'real' way to exit in the browser.
|
// a 'real' way to exit in the browser.
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn exit(c int) {
|
pub fn exit(c int) {
|
||||||
JS.process.exit(c)
|
JS.process.exit(c)
|
||||||
js_throw('exit(${c})')
|
js_throw('exit(${c})')
|
||||||
|
|
|
@ -6,7 +6,7 @@ module builtin
|
||||||
|
|
||||||
fn (a any) toString()
|
fn (a any) toString()
|
||||||
|
|
||||||
[noreturn]
|
@[noreturn]
|
||||||
pub fn panic(s string) {
|
pub fn panic(s string) {
|
||||||
eprintln('V panic: ${s}\n${js_stacktrace()}')
|
eprintln('V panic: ${s}\n${js_stacktrace()}')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
@ -127,7 +127,7 @@ fn trace_error(x string) {
|
||||||
|
|
||||||
// error returns a default error instance containing the error given in `message`.
|
// error returns a default error instance containing the error given in `message`.
|
||||||
// Example: if ouch { return error('an error occurred') }
|
// Example: if ouch { return error('an error occurred') }
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn error(message string) IError {
|
pub fn error(message string) IError {
|
||||||
// trace_error(message)
|
// trace_error(message)
|
||||||
return &MessageError{
|
return &MessageError{
|
||||||
|
@ -137,7 +137,7 @@ pub fn error(message string) IError {
|
||||||
|
|
||||||
// error_with_code returns a default error instance containing the given `message` and error `code`.
|
// error_with_code returns a default error instance containing the given `message` and error `code`.
|
||||||
// Example: if ouch { return error_with_code('an error occurred', 1) }
|
// Example: if ouch { return error_with_code('an error occurred', 1) }
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn error_with_code(message string, code int) IError {
|
pub fn error_with_code(message string, code int) IError {
|
||||||
// trace_error('$message | code: $code')
|
// trace_error('$message | code: $code')
|
||||||
return &MessageError{
|
return &MessageError{
|
||||||
|
@ -148,7 +148,7 @@ pub fn error_with_code(message string, code int) IError {
|
||||||
|
|
||||||
// free allows for manually freeing memory allocated at the address `ptr`.
|
// free allows for manually freeing memory allocated at the address `ptr`.
|
||||||
// However, this is a no-op on JS backend
|
// However, this is a no-op on JS backend
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn free(ptr voidptr) {
|
pub fn free(ptr voidptr) {
|
||||||
_ := ptr
|
_ := ptr
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,42 +30,42 @@ pub fn (c u8) repeat(count int) string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_digit() bool {
|
pub fn (c u8) is_digit() bool {
|
||||||
return c >= `0` && c <= `9`
|
return c >= `0` && c <= `9`
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_hex_digit returns `true` if the byte is either in range 0-9, a-f or A-F and `false` otherwise.
|
// is_hex_digit returns `true` if the byte is either in range 0-9, a-f or A-F and `false` otherwise.
|
||||||
// Example: assert u8(`F`) == true
|
// Example: assert u8(`F`) == true
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_hex_digit() bool {
|
pub fn (c u8) is_hex_digit() bool {
|
||||||
return (c >= `0` && c <= `9`) || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
return (c >= `0` && c <= `9`) || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
||||||
// Example: assert u8(`7`) == true
|
// Example: assert u8(`7`) == true
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_oct_digit() bool {
|
pub fn (c u8) is_oct_digit() bool {
|
||||||
return c >= `0` && c <= `7`
|
return c >= `0` && c <= `7`
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_bin_digit returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise.
|
// is_bin_digit returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise.
|
||||||
// Example: assert u8(`0`) == true
|
// Example: assert u8(`0`) == true
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_bin_digit() bool {
|
pub fn (c u8) is_bin_digit() bool {
|
||||||
return c == `0` || c == `1`
|
return c == `0` || c == `1`
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_letter returns `true` if the byte is in range a-z or A-Z and `false` otherwise.
|
// is_letter returns `true` if the byte is in range a-z or A-Z and `false` otherwise.
|
||||||
// Example: assert u8(`V`) == true
|
// Example: assert u8(`V`) == true
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_letter() bool {
|
pub fn (c u8) is_letter() bool {
|
||||||
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_alnum returns `true` if the byte is in range a-z, A-Z, 0-9 and `false` otherwise.
|
// is_alnum returns `true` if the byte is in range a-z, A-Z, 0-9 and `false` otherwise.
|
||||||
// Example: assert u8(`V`) == true
|
// Example: assert u8(`V`) == true
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_alnum() bool {
|
pub fn (c u8) is_alnum() bool {
|
||||||
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`)
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`)
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ pub fn (c u8) is_alnum() bool {
|
||||||
// is_capital returns `true`, if the byte is a Latin capital letter.
|
// is_capital returns `true`, if the byte is a Latin capital letter.
|
||||||
// Example: assert `H`.is_capital() == true
|
// Example: assert `H`.is_capital() == true
|
||||||
// Example: assert `h`.is_capital() == false
|
// Example: assert `h`.is_capital() == false
|
||||||
[inline]
|
@[inline]
|
||||||
pub fn (c u8) is_capital() bool {
|
pub fn (c u8) is_capital() bool {
|
||||||
return c >= `A` && c <= `Z`
|
return c >= `A` && c <= `Z`
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,17 +9,17 @@ module builtin
|
||||||
|
|
||||||
pub interface JS.Object {}
|
pub interface JS.Object {}
|
||||||
|
|
||||||
[single_impl]
|
@[single_impl]
|
||||||
pub interface JS.BigInt {
|
pub interface JS.BigInt {
|
||||||
JS.Any
|
JS.Any
|
||||||
}
|
}
|
||||||
|
|
||||||
[single_impl]
|
@[single_impl]
|
||||||
pub interface JS.Number {
|
pub interface JS.Number {
|
||||||
JS.Any
|
JS.Any
|
||||||
}
|
}
|
||||||
|
|
||||||
[single_impl]
|
@[single_impl]
|
||||||
pub interface JS.String {
|
pub interface JS.String {
|
||||||
JS.Any
|
JS.Any
|
||||||
length JS.Number
|
length JS.Number
|
||||||
|
@ -37,7 +37,7 @@ pub interface JS.String {
|
||||||
lastIndexOf(needle JS.String) JS.Number
|
lastIndexOf(needle JS.String) JS.Number
|
||||||
}
|
}
|
||||||
|
|
||||||
[single_impl]
|
@[single_impl]
|
||||||
pub interface JS.Boolean {
|
pub interface JS.Boolean {
|
||||||
JS.Any
|
JS.Any
|
||||||
length JS.Number
|
length JS.Number
|
||||||
|
|
|
@ -36,7 +36,7 @@ fn (mut m map) internal_get(key JS.Any) JS.Any {
|
||||||
#map.prototype.set = function(key,val) { map_internal_set(this,key,val); }
|
#map.prototype.set = function(key,val) { map_internal_set(this,key,val); }
|
||||||
#map.prototype.has = function (key) { if (typeof key != "string" && '$toJS' in key) { key = key.$toJS() } return key in this.map; }
|
#map.prototype.has = function (key) { if (typeof key != "string" && '$toJS' in key) { key = key.$toJS() } return key in this.map; }
|
||||||
// Removes the mapping of a particular key from the map.
|
// Removes the mapping of a particular key from the map.
|
||||||
[unsafe]
|
@[unsafe]
|
||||||
pub fn (mut m map) delete(key JS.Any) {
|
pub fn (mut m map) delete(key JS.Any) {
|
||||||
#let k = '$toJS' in key ? key.$toJS() : key;
|
#let k = '$toJS' in key ? key.$toJS() : key;
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ pub interface JS.Promise {
|
||||||
finally(callback JS.Any) JS.Promise
|
finally(callback JS.Any) JS.Promise
|
||||||
}
|
}
|
||||||
|
|
||||||
[use_new]
|
@[use_new]
|
||||||
pub fn JS.Promise.prototype.constructor(JS.Any) JS.Promise
|
pub fn JS.Promise.prototype.constructor(JS.Any) JS.Promise
|
||||||
pub fn JS.Promise.reject(JS.Any) JS.Promise
|
pub fn JS.Promise.reject(JS.Any) JS.Promise
|
||||||
pub fn JS.Promise.resolve(JS.Any) JS.Promise
|
pub fn JS.Promise.resolve(JS.Any) JS.Promise
|
||||||
|
@ -16,7 +16,7 @@ pub fn JS.Promise.race(JS.Array) JS.Promise
|
||||||
// of an asynchronous operation and its resulting value.
|
// of an asynchronous operation and its resulting value.
|
||||||
pub struct Promise[T] {
|
pub struct Promise[T] {
|
||||||
mut:
|
mut:
|
||||||
promise JS.Promise [noinit]
|
promise JS.Promise @[noinit]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn promise_new[T](executor fn (resolve fn (T), reject fn (JS.Any))) Promise[T] {
|
pub fn promise_new[T](executor fn (resolve fn (T), reject fn (JS.Any))) Promise[T] {
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue