Skip to content

Commit

Permalink
all: implement @VCURRENTHASH to replace C.V_CURRENT_COMMIT_HASH (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Spydr06 authored Oct 5, 2023
1 parent 1512486 commit 32bb8cf
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5533,6 +5533,9 @@ that are substituted at compile time:
- `@VEXEROOT` => will be substituted with the *folder*,
where the V executable is (as a string).
- `@VHASH` => replaced with the shortened commit hash of the V compiler (as a string).
- `@VCURRENTHASH` => Similar to `@VHASH`, but changes when the compiler is
recompiled on a different commit (after local modifications, or after
using git bisect etc).
- `@VMOD_FILE` => replaced with the contents of the nearest v.mod file (as a string).
- `@VMODROOT` => will be substituted with the *folder*,
where the nearest v.mod file is (as a string).
Expand Down
4 changes: 2 additions & 2 deletions vlib/builtin/builtin.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
eprintln(' function: ${fn_name}()')
eprintln(' message: ${s}')
eprintln(' file: ${file}:${line_no}')
eprintln(' v hash: ${@VHASH}')
eprintln(' v hash: ${@VHASH}') // TODO: use @VCURRENTHASH when bootstrapped
eprintln('=========================================')
$if native {
C.exit(1) // TODO: native backtraces
Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn panic(s string) {
} $else {
eprint('V panic: ')
eprintln(s)
eprintln('v hash: ${@VHASH}')
eprintln('v hash: ${@VHASH}') // TODO: use @VCURRENTHASH when bootstrapped
$if native {
C.exit(1) // TODO: native backtraces
} $else $if exit_after_panic_message ? {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ mut:
goto_labels map[string]ast.GotoLabel // to check for unused goto labels
enum_data_type ast.Type
fn_return_type ast.Type

v_current_commit_hash string // same as V_CURRENT_COMMIT_HASH
}

pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
Expand All @@ -145,6 +147,7 @@ pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
pref: pref_
timers: util.new_timers(should_print: timers_should_print, label: 'checker')
match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit
v_current_commit_hash: version.githash(pref_.building_v)
}
}

Expand Down Expand Up @@ -3368,6 +3371,9 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
.vhash {
node.val = version.vhash()
}
.v_current_hash {
node.val = c.v_current_commit_hash
}
.vmod_file {
// cache the vmod content, do not read it many times
if c.vmod_file_content.len == 0 {
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/eval/eval.v
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ fn (e Eval) error(msg string) {
}

fn (e Eval) panic(s string) {
commithash := unsafe { tos5(&char(C.V_CURRENT_COMMIT_HASH)) }
eprintln('V panic: ${s}')
eprintln('V hash: ${commithash}')
eprintln('V hash: ${@VCURRENTHASH}')
e.print_backtrace()
exit(1)
}
Expand Down
9 changes: 1 addition & 8 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import v.ast
import v.token
import v.pref
import v.util
import v.util.version
import v.depgraph
import encoding.base64
import v.gen.js.sourcemap
Expand Down Expand Up @@ -247,7 +246,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref_ &pref.Preferences) string
// deps_resolved := graph.resolve()
// nodes := deps_resolved.nodes

mut out := g.definitions.str() + g.hashes()
mut out := g.definitions.str()
if !g.pref.output_es5 {
out += '\nlet wasmExportObject;\n'

Expand Down Expand Up @@ -491,12 +490,6 @@ pub fn (mut g JsGen) init() {
g.definitions.writeln('function ReturnException(val) { this.val = val; }')
}

pub fn (g JsGen) hashes() string {
mut res := '// V_COMMIT_HASH ${version.vhash()}\n'
res += '// V_CURRENT_COMMIT_HASH ${version.githash(g.pref.building_v)}\n'
return res
}

[noreturn]
fn verror(msg string) {
eprintln('jsgen error: ${msg}')
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/gen/native/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import v.util

fn (mut g Gen) expr(node ast.Expr) {
match node {
ast.AtExpr {
g.allocate_string(g.comptime_at(node), 3, .rel32)
}
ast.ParExpr {
g.expr(node.expr)
}
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ fn (mut p Parser) at() ast.AtExpr {
'@FILE_LINE' { token.AtKind.file_path_line_nr }
'@LOCATION' { token.AtKind.location }
'@COLUMN' { token.AtKind.column_nr }
'@VCURRENTHASH' { token.AtKind.v_current_hash }
'@VHASH' { token.AtKind.vhash }
'@VMOD_FILE' { token.AtKind.vmod_file }
'@VEXE' { token.AtKind.vexe_path }
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/token/token.v
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub enum AtKind {
line_nr
column_nr
vhash
v_current_hash
vmod_file
vmodroot_path
vroot_path // obsolete
Expand All @@ -187,7 +188,8 @@ pub const (
.unsigned_right_shift_assign]

valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT',
'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE', '@FILE_LINE', '@LOCATION']
'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@FILE_LINE',
'@LOCATION']

token_str = build_token_str()

Expand Down

0 comments on commit 32bb8cf

Please sign in to comment.