v/vlib/builtin/builtin.v

113 lines
3.1 KiB
V
Raw Normal View History

// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
2019-06-23 04:21:30 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 20:20:28 +02:00
module builtin
__global (
g_m2_buf byteptr
g_m2_ptr byteptr
)
2019-12-11 16:41:25 +01:00
2019-06-22 20:20:28 +02:00
// isnil returns true if an object is nil (only for C objects).
pub fn isnil(v voidptr) bool {
return v == 0
}
/*
2019-12-19 21:52:45 +01:00
fn on_panic(f fn(int)int) {
2019-06-22 20:20:28 +02:00
// TODO
}
*/
2019-06-22 20:20:28 +02:00
// print_backtrace shows a backtrace of the current call stack on stdout
2019-12-19 21:52:45 +01:00
pub fn print_backtrace() {
// at the time of backtrace_symbols_fd call, the C stack would look something like this:
// 1 frame for print_backtrace_skipping_top_frames
// 1 frame for print_backtrace itself
// ... print the rest of the backtrace frames ...
// => top 2 frames should be skipped, since they will not be informative to the developer
print_backtrace_skipping_top_frames(2)
2019-06-22 20:20:28 +02:00
}
struct VCastTypeIndexName {
tindex int
tname string
}
__global (
total_m = i64(0)
nr_mallocs = int(0)
// will be filled in cgen
as_cast_type_indexes []VCastTypeIndexName
)
fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr {
2020-04-25 08:36:53 +02:00
if obj_type != expected_type {
mut obj_name := as_cast_type_indexes[0].tname
mut expected_name := as_cast_type_indexes[0].tname
for x in as_cast_type_indexes {
if x.tindex == obj_type {
obj_name = x.tname
}
if x.tindex == expected_type {
expected_name = x.tname
}
}
panic('as cast: cannot cast `$obj_name` to `$expected_name`')
2020-04-25 08:36:53 +02:00
}
return obj
}
// VAssertMetaInfo is used during assertions. An instance of it
// is filled in by compile time generated code, when an assertion fails.
pub struct VAssertMetaInfo {
pub:
fpath string // the source file path of the assertion
line_nr int // the line number of the assertion
fn_name string // the function name in which the assertion is
src string // the actual source line of the assertion
op string // the operation of the assertion, i.e. '==', '<', 'call', etc ...
llabel string // the left side of the infix expressions as source
rlabel string // the right side of the infix expressions as source
lvalue string // the stringified *actual value* of the left side of a failed assertion
rvalue string // the stringified *actual value* of the right side of a failed assertion
}
fn __print_assert_failure(i &VAssertMetaInfo) {
eprintln('$i.fpath:${i.line_nr + 1}: FAIL: fn $i.fn_name: assert $i.src')
2020-06-13 16:20:45 +02:00
if i.op.len > 0 && i.op != 'call' {
eprintln(' left value: $i.llabel = $i.lvalue')
if i.rlabel == i.rvalue {
eprintln(' right value: $i.rlabel')
} else {
eprintln(' right value: $i.rlabel = $i.rvalue')
}
}
}
// MethodArgs holds type information for function and/or method arguments.
pub struct MethodArgs {
pub:
typ int
}
// FunctionData holds information about a parsed function.
pub struct FunctionData {
pub:
name string
attrs []string
args []MethodArgs
return_type int
typ int
}
// FieldData holds information about a field. Fields reside on structs.
pub struct FieldData {
pub:
name string
attrs []string
is_pub bool
is_mut bool
typ int
}