Debian Codegraph @

doc/book/src/internals.md

raw
1 # Muon Internals
2
3 ## Error handling
4
5 All errors that can be checked, should be checked. If an error is detected, an
6 error message should be printed using `vm_error`, or if there is no source
7 code associated with the error, `LOG_E`. The error should be immediately
8 returned. Most functions returning a `bool` return `false` on error. The most
9 common other type of error returning function has the return type
10 `enum iteration_result`. These functions should return `ir_err` on error.
11
12 ## Meson functions
13
14 All meson functions are defined in `functions` with a separate file for each
15 object type on which the function is defined. Functions not defined on an
16 object are in `kernel.c`. If the function implementation is sufficiently large,
17 it may be broken up into a separate file under `functions/<object
18 type>/<function name>.c`.
19
20 When declaring a new function, you need to add it to the "impl\_tbl" at the
21 bottom of the file. All functions should call `pop_args()` before they do
22 anything, even if they don't take any arguments. `pop_args` takes 3 arrays,
23 conventionally named `an` (args, normal), `ao` (args, optional), and `akw`
24 (args, keyword). Any of these may be NULL. In particular, if they are all NULL
25 then the function takes no arguments, and `pop_args` will ensure this is the
26 case.
27
28 Arguments should specify what types they accept by bitwise or-ing `tc_`-prefixed
29 types together. `an` and `ao` argument arrays *must* be terminated by an
30 argument of type `ARG_TYPE_NULL`.
31
32 `TYPE_TAG_GLOB` can be or-d with the type of the last element of `an`, and will
33 store the remaining arguments in an `obj_array`. This is similar to e.g. `def
34 func(a, b, *c):` in python, where `c` is the "glob" argument.
35
36 You may also bitwise or any type with `TYPE_TAG_LISTIFY`. This "type" will
37 cause `pop_args` to do the following things:
38
39 1. coerce single elements to arrays
40 - `'hello' #=> ['hello']`
41 2. flatten arrays
42 - `['hello', [], [['world']]] #=> ['hello', 'world']`
43 3. typecheck all elements of the array
44 - given the "type" `TYPE_TAG_LISTIFY | obj_string`, the above examples
45 would pass, but `['a', false]` would not.
46
47 ## Workspace
48
49 The workspace is a structure that contains all the data for an entire build
50 setup, including all subprojects, AST, options, objects, strings. Most
51 interpreter related functions take a workspace as one of their arguments.
52
53 ## Objects
54
55 Meson objects are created with the `make_obj` function. See `object.h` for more
56 information. Conventionally, all objects are referred to by id of type obj
57 rather than a pointer.
58
59 ## Memory
60
61 You may be wondering, what about Garbage collection? Muon's current approach is
62 to cleanup only once at the very end. Meson is not a Turing-complete language,
63 so we don't need to worry about long running programs.