Function hook example

Ano script supports function hooks, which is like constructor and destructor of a function. Also main block can have finalize block which runs when main program terminates.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh function_hook

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; ------------------------------------------------------------------------------
; function_hook.ano
;
; To compile:
;
; $ ./build/ano ./examples/function_hook/function_hook.ano > \
;     engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		function_hook
@ANO_SCRIPT_VERSION		0.0.1
@ANO_SCRIPT_DESCRIPTION	Simple function hook example
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; Ano script supports function hooks, which is like constructor and destructor
; of a function. Also main block can have finalize block which runs when main
; program terminates.
;

main [exit: 0] {
	&my_func (1, 2)
}

finalize {
	; This hook is called when main program calls exit().
	;
	print "\n" \
		"Finalize block is started automatically when exit function" \
		" is called.\nThis block does not pass to control to main" \
		" block anymore.\n"
}

function my_func [entry: my_func_entry, return: my_func_ret] (_var1, _var2) {
	; Entry hook is called here.
	;

	; This is what this function is for:
	;
	print "This is function with hooks.\n"

	; Return hook is called at the end of the function, which is here.
	;
}

hook my_func_entry {
	; Hook has access to function parameters:
	;
	print " * function is about to start, _var1 value is " . _var1 . ".\n"
}

hook my_func_ret {
	; Hook has access to function parameters:
	;
	print " * function ends, _var2 value is " . _var2 . ".\n"
}
; ------------------------------------------------------------------------------
; function_hook.w2c
;
; To compile:
;
; $ ./build/widget ./examples/function_hook/function_hook.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; function_hook.m2c
;
; To compile:
;
; $ ./build/menu ./examples/function_hook/function_hook.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.