/home/runner/work/panackelty/panackelty/src/vm/builtins.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "builtins.h" |
2 | | |
3 | | #include "builtins_internal.h" |
4 | | #include "host.h" |
5 | | #include "host_capabilities.h" |
6 | | #include "host_types.h" |
7 | | |
8 | | #include <stdlib.h> |
9 | | #include <string.h> |
10 | | |
11 | | /* One registry supplies arity, purity, and implementation selection. */ |
12 | | |
13 | | static Value *async_service_only(VM *vm, const char *name, Value **arguments) |
14 | 0 | { |
15 | 0 | (void)name; |
16 | 0 | (void)arguments; |
17 | 0 | vm->error = "VM trap: async service requires await"; |
18 | 0 | return NULL; |
19 | 0 | } |
20 | | |
21 | | bool builtin_is_async(const Builtin *entry) |
22 | 6.47M | { |
23 | 6.47M | return entry && entry->call == async_service_only; Branch (23:12): [True: 4.18M, False: 2.29M]
Branch (23:21): [True: 115, False: 4.18M]
|
24 | 6.47M | } |
25 | | |
26 | | static const Builtin BUILTINS[] = { |
27 | | {"async_fake_read", 1, false, async_service_only}, |
28 | | {"$tcp_exchange", 5, false, async_service_only}, |
29 | | {"$tcp_serve", 4, false, async_service_only}, |
30 | | {"fs_read", 2, false, host_capability_call}, |
31 | | {"fs_write", 2, false, host_capability_call}, |
32 | | {"fs_metadata", 1, false, host_capability_call}, |
33 | | {"fs_list", 1, false, host_capability_call}, |
34 | | {"fs_create_directory", 1, false, host_capability_call}, |
35 | | {"fs_remove_file", 1, false, host_capability_call}, |
36 | | {"fs_remove_directory", 1, false, host_capability_call}, |
37 | | {"fs_temp_file", 1, false, host_capability_call}, |
38 | | {"fs_temp_directory", 1, false, host_capability_call}, |
39 | | {"host_decode_utf8", 1, true, host_capability_call}, |
40 | | {"host_sleep", 1, false, host_capability_call}, |
41 | | {"process_run", 7, false, host_capability_call}, |
42 | | {"path_from_text", 1, true, host_type_call}, |
43 | | {"path_from_native", 1, true, host_type_call}, |
44 | | {"path_to_text", 1, true, host_type_call}, |
45 | | {"path_native_bytes", 1, true, host_type_call}, |
46 | | {"path_display", 1, true, host_type_call}, |
47 | | {"path_absolute", 1, true, host_type_call}, |
48 | | {"path_append", 2, true, host_type_call}, |
49 | | {"path_directory", 1, true, host_type_call}, |
50 | | {"path_filename", 1, true, host_type_call}, |
51 | | {"path_current", 0, true, host_type_call}, |
52 | | {"duration_nanoseconds", 1, true, host_type_call}, |
53 | | {"duration_ticks", 1, true, host_type_call}, |
54 | | {"duration_from_seconds", 1, true, host_type_call}, |
55 | | {"instant_now", 0, false, host_type_call}, |
56 | | {"instant_add", 2, true, host_type_call}, |
57 | | {"instant_difference", 2, true, host_type_call}, |
58 | | {"instant_before", 2, true, host_type_call}, |
59 | | |
60 | | {"$unit", 0, true, builtins_numeric_call}, |
61 | | {"nat", 1, true, builtins_numeric_call}, |
62 | | {"dec", 1, true, builtins_numeric_call}, |
63 | | {"quotient", 2, true, builtins_numeric_call}, |
64 | | {"print", 1, false, host_call}, |
65 | | {"read_line", 0, false, host_call}, |
66 | | {"read_file", 1, false, host_call}, |
67 | | {"write_file", 2, false, host_call}, |
68 | | {"len", 1, true, builtins_text_call}, |
69 | | {"append", 2, true, builtins_collections_call}, |
70 | | {"concat", 2, true, builtins_text_call}, |
71 | | {"slice", 3, true, builtins_text_call}, |
72 | | {"starts_with", 2, true, builtins_text_call}, |
73 | | {"starts_with_at", 3, true, builtins_text_call}, |
74 | | {"reverse", 1, true, builtins_text_call}, |
75 | | {"is_digit", 1, true, builtins_text_call}, |
76 | | {"is_letter", 1, true, builtins_text_call}, |
77 | | {"is_whitespace", 1, true, builtins_text_call}, |
78 | | {"map", 0, true, builtins_collections_call}, |
79 | | {"map_put", 3, true, builtins_collections_call}, |
80 | | {"map_has", 2, true, builtins_collections_call}, |
81 | | {"map_get", 2, true, builtins_collections_call}, |
82 | | {"$method_put", 3, true, builtins_collections_call}, |
83 | | {"$method_has", 2, true, builtins_collections_call}, |
84 | | {"$method_get", 2, true, builtins_collections_call}, |
85 | | {"set", 0, true, builtins_collections_call}, |
86 | | {"set_add", 2, true, builtins_collections_call}, |
87 | | {"set_has", 2, true, builtins_collections_call}, |
88 | | {"$method_add", 2, true, builtins_collections_call}, |
89 | | {"bytes", 0, true, builtins_collections_call}, |
90 | | {"byte_append", 2, true, builtins_text_call}, |
91 | | {"bytes_concat", 2, true, builtins_text_call}, |
92 | | {"byte_len", 1, true, builtins_text_call}, |
93 | | {"byte_get", 2, true, builtins_text_call}, |
94 | | {"utf8_encode", 1, true, builtins_text_call}, |
95 | | {"utf8_decode", 1, true, builtins_text_call}, |
96 | | {"read_bytes", 1, false, host_call}, |
97 | | {"write_bytes", 2, false, host_call}, |
98 | | {"nat_from_str", 1, true, builtins_text_call}, |
99 | | {"command_args", 0, false, host_call}, |
100 | | {"environment_has", 1, false, host_call}, |
101 | | {"environment_get", 1, false, host_call}, |
102 | | {"eprint", 1, false, host_call}, |
103 | | {"process_exit", 1, false, host_call}, |
104 | | {"path_resolve", 1, false, host_call}, |
105 | | {"path_parent", 1, true, host_call}, |
106 | | {"path_join", 2, true, host_call}, |
107 | | {"path_suffix", 1, true, host_call}, |
108 | | {"path_with_suffix", 2, true, host_call}, |
109 | | {"path_is_absolute", 1, true, host_call}, |
110 | | {"file_exists", 1, false, host_call}, |
111 | | {"run_bytecode", 1, false, builtins_vm_call}, |
112 | | {"run_bytecode_args", 2, false, builtins_vm_call}, |
113 | | }; |
114 | | |
115 | | const Builtin *builtin(const char *name) |
116 | 10.5M | { |
117 | 568M | for (size_t i = 0; i < sizeof(BUILTINS) / sizeof(*BUILTINS); i++) { Branch (117:24): [True: 565M, False: 2.29M]
|
118 | 565M | if (!strcmp(name, BUILTINS[i].name)) { Branch (118:13): [True: 8.25M, False: 557M]
|
119 | 8.25M | return &BUILTINS[i]; |
120 | 8.25M | } |
121 | 565M | } |
122 | 2.29M | return NULL; |
123 | 10.5M | } |
124 | | |
125 | | Value *builtin_call(VM *vm, const char *name, Value **arguments) |
126 | 4.07M | { |
127 | 4.07M | const Builtin *entry = builtin(name); |
128 | 4.07M | if (!entry) { Branch (128:9): [True: 1, False: 4.07M]
|
129 | 1 | vm->error = "VM trap: unknown builtin"; |
130 | 1 | return NULL; |
131 | 1 | } |
132 | 4.07M | return entry->call(vm, name, arguments); |
133 | 4.07M | } |