/home/runner/work/panackelty/panackelty/src/vm/builtins.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "http_transport.h" |
2 | | #include "builtins.h" |
3 | | #include "console_write.h" |
4 | | #include "console_read.h" |
5 | | |
6 | | #include "builtins_internal.h" |
7 | | #include "host.h" |
8 | | #include "host_capabilities.h" |
9 | | #include "filesystem_stage.h" |
10 | | #include "gzip_stream.h" |
11 | | #include "digest.h" |
12 | | #include "host_types.h" |
13 | | |
14 | | #include <stdlib.h> |
15 | | #include <string.h> |
16 | | |
17 | | /* One registry supplies arity, purity, and implementation selection. */ |
18 | | |
19 | | static Value *function_reference(VM *vm, const char *name, Value **arguments) |
20 | 7.73k | { |
21 | 7.73k | (void)name; |
22 | 7.73k | Value *target = arguments[0]; |
23 | 7.73k | if (target->kind != V_STR || memchr(target->as.bytes.data, 0, target->as.bytes.length)) { Branch (23:9): [True: 0, False: 7.73k]
Branch (23:34): [True: 0, False: 7.73k]
|
24 | 0 | vm->error = "VM trap: invalid function reference"; |
25 | 0 | return NULL; |
26 | 0 | } |
27 | 7.73k | const char *symbol = (const char *)target->as.bytes.data; |
28 | 7.73k | if (!program_function(vm->program, symbol) || builtin(symbol)) { Branch (28:9): [True: 0, False: 7.73k]
Branch (28:51): [True: 0, False: 7.73k]
|
29 | 0 | vm->error = "VM trap: function reference target was not found"; |
30 | 0 | return NULL; |
31 | 0 | } |
32 | 7.73k | CaptureCheck status; |
33 | 7.73k | Value *result = value_closure(symbol, NULL, 0, &status); |
34 | 7.73k | if (!result) vm->error = "native VM out of memory"; Branch (34:9): [True: 8, False: 7.72k]
|
35 | 7.73k | return result; |
36 | 7.73k | } |
37 | | |
38 | | static Value *closure_create(VM *vm, const char *name, Value **arguments) |
39 | 412 | { |
40 | 412 | (void)name; |
41 | 412 | Value *target = arguments[0], *environment = arguments[1]; |
42 | 412 | size_t arity; |
43 | 412 | bool reference = target->kind == V_CLOSURE && target->as.named.count == 0; Branch (43:22): [True: 0, False: 206]
Branch (43:51): [True: 0, False: 0]
Branch (43:22): [True: 0, False: 206]
Branch (43:51): [True: 0, False: 0]
|
44 | 412 | bool legacy = target->kind == V_STR && Branch (44:19): [True: 206, False: 0]
Branch (44:19): [True: 206, False: 0]
|
45 | 412 | !memchr(target->as.bytes.data, 0, target->as.bytes.length); Branch (45:9): [True: 176, False: 30]
Branch (45:9): [True: 176, False: 30]
|
46 | 412 | if ((!reference && !legacy) || environment->kind != V_ARRAY || Branch (46:10): [True: 206, False: 0]
Branch (46:24): [True: 30, False: 176]
Branch (46:36): [True: 0, False: 176]
Branch (46:10): [True: 206, False: 0]
Branch (46:24): [True: 30, False: 176]
Branch (46:36): [True: 0, False: 176]
|
47 | 412 | arguments[2]->kind != V_NAT || !value_index(arguments[2], &arity)) { Branch (47:9): [True: 0, False: 176]
Branch (47:40): [True: 0, False: 176]
Branch (47:9): [True: 0, False: 176]
Branch (47:40): [True: 0, False: 176]
|
48 | 60 | vm->error = "VM trap: invalid closure construction"; |
49 | 60 | return NULL; |
50 | 60 | } |
51 | 352 | char *symbol = copy_text(reference ? target->as.named.name : (const char *)target->as.bytes.data); Branch (51:30): [True: 0, False: 176]
Branch (51:30): [True: 0, False: 176]
|
52 | 352 | if (!symbol) { vm->error = "native VM out of memory"; return NULL; } Branch (52:9): [True: 4, False: 172]
Branch (52:9): [True: 4, False: 172]
|
53 | 344 | Function *function = program_function(vm->program, symbol); |
54 | 344 | size_t count = environment->as.sequence.count; |
55 | 344 | if (!function || builtin(symbol)) { Branch (55:9): [True: 30, False: 142]
Branch (55:22): [True: 0, False: 142]
Branch (55:9): [True: 30, False: 142]
Branch (55:22): [True: 0, False: 142]
|
56 | 60 | vm->error = "VM trap: closure target was not found"; |
57 | 284 | } else if (count > function->param_count || arity != function->param_count - count) { Branch (57:16): [True: 0, False: 142]
Branch (57:49): [True: 30, False: 112]
Branch (57:16): [True: 0, False: 142]
Branch (57:49): [True: 30, False: 112]
|
58 | 60 | vm->error = "VM trap: closure capture layout does not match target arity"; |
59 | 60 | } |
60 | 344 | Value *result = NULL; |
61 | 344 | if (!vm->error) { Branch (61:9): [True: 112, False: 60]
Branch (61:9): [True: 112, False: 60]
|
62 | 224 | CaptureCheck status; |
63 | 224 | result = value_closure(symbol, environment->as.sequence.items, count, &status); |
64 | 224 | if (!result) vm->error = status == CAPTURE_OUT_OF_MEMORY Branch (64:13): [True: 74, False: 38]
Branch (64:34): [True: 44, False: 30]
Branch (64:13): [True: 74, False: 38]
Branch (64:34): [True: 44, False: 30]
|
65 | 148 | ? "native VM out of memory" : "VM trap: closure cannot capture a resource-bearing value"; |
66 | 224 | } |
67 | 344 | free(symbol); |
68 | 344 | return result; |
69 | 352 | } builtins.c:closure_create Line | Count | Source | 39 | 206 | { | 40 | 206 | (void)name; | 41 | 206 | Value *target = arguments[0], *environment = arguments[1]; | 42 | 206 | size_t arity; | 43 | 206 | bool reference = target->kind == V_CLOSURE && target->as.named.count == 0; Branch (43:22): [True: 0, False: 206]
Branch (43:51): [True: 0, False: 0]
| 44 | 206 | bool legacy = target->kind == V_STR && Branch (44:19): [True: 206, False: 0]
| 45 | 206 | !memchr(target->as.bytes.data, 0, target->as.bytes.length); Branch (45:9): [True: 176, False: 30]
| 46 | 206 | if ((!reference && !legacy) || environment->kind != V_ARRAY || Branch (46:10): [True: 206, False: 0]
Branch (46:24): [True: 30, False: 176]
Branch (46:36): [True: 0, False: 176]
| 47 | 206 | arguments[2]->kind != V_NAT || !value_index(arguments[2], &arity)) { Branch (47:9): [True: 0, False: 176]
Branch (47:40): [True: 0, False: 176]
| 48 | 30 | vm->error = "VM trap: invalid closure construction"; | 49 | 30 | return NULL; | 50 | 30 | } | 51 | 176 | char *symbol = copy_text(reference ? target->as.named.name : (const char *)target->as.bytes.data); Branch (51:30): [True: 0, False: 176]
| 52 | 176 | if (!symbol) { vm->error = "native VM out of memory"; return NULL; } Branch (52:9): [True: 4, False: 172]
| 53 | 172 | Function *function = program_function(vm->program, symbol); | 54 | 172 | size_t count = environment->as.sequence.count; | 55 | 172 | if (!function || builtin(symbol)) { Branch (55:9): [True: 30, False: 142]
Branch (55:22): [True: 0, False: 142]
| 56 | 30 | vm->error = "VM trap: closure target was not found"; | 57 | 142 | } else if (count > function->param_count || arity != function->param_count - count) { Branch (57:16): [True: 0, False: 142]
Branch (57:49): [True: 30, False: 112]
| 58 | 30 | vm->error = "VM trap: closure capture layout does not match target arity"; | 59 | 30 | } | 60 | 172 | Value *result = NULL; | 61 | 172 | if (!vm->error) { Branch (61:9): [True: 112, False: 60]
| 62 | 112 | CaptureCheck status; | 63 | 112 | result = value_closure(symbol, environment->as.sequence.items, count, &status); | 64 | 112 | if (!result) vm->error = status == CAPTURE_OUT_OF_MEMORY Branch (64:13): [True: 74, False: 38]
Branch (64:34): [True: 44, False: 30]
| 65 | 74 | ? "native VM out of memory" : "VM trap: closure cannot capture a resource-bearing value"; | 66 | 112 | } | 67 | 172 | free(symbol); | 68 | 172 | return result; | 69 | 176 | } |
builtins.c:closure_create Line | Count | Source | 39 | 206 | { | 40 | 206 | (void)name; | 41 | 206 | Value *target = arguments[0], *environment = arguments[1]; | 42 | 206 | size_t arity; | 43 | 206 | bool reference = target->kind == V_CLOSURE && target->as.named.count == 0; Branch (43:22): [True: 0, False: 206]
Branch (43:51): [True: 0, False: 0]
| 44 | 206 | bool legacy = target->kind == V_STR && Branch (44:19): [True: 206, False: 0]
| 45 | 206 | !memchr(target->as.bytes.data, 0, target->as.bytes.length); Branch (45:9): [True: 176, False: 30]
| 46 | 206 | if ((!reference && !legacy) || environment->kind != V_ARRAY || Branch (46:10): [True: 206, False: 0]
Branch (46:24): [True: 30, False: 176]
Branch (46:36): [True: 0, False: 176]
| 47 | 206 | arguments[2]->kind != V_NAT || !value_index(arguments[2], &arity)) { Branch (47:9): [True: 0, False: 176]
Branch (47:40): [True: 0, False: 176]
| 48 | 30 | vm->error = "VM trap: invalid closure construction"; | 49 | 30 | return NULL; | 50 | 30 | } | 51 | 176 | char *symbol = copy_text(reference ? target->as.named.name : (const char *)target->as.bytes.data); Branch (51:30): [True: 0, False: 176]
| 52 | 176 | if (!symbol) { vm->error = "native VM out of memory"; return NULL; } Branch (52:9): [True: 4, False: 172]
| 53 | 172 | Function *function = program_function(vm->program, symbol); | 54 | 172 | size_t count = environment->as.sequence.count; | 55 | 172 | if (!function || builtin(symbol)) { Branch (55:9): [True: 30, False: 142]
Branch (55:22): [True: 0, False: 142]
| 56 | 30 | vm->error = "VM trap: closure target was not found"; | 57 | 142 | } else if (count > function->param_count || arity != function->param_count - count) { Branch (57:16): [True: 0, False: 142]
Branch (57:49): [True: 30, False: 112]
| 58 | 30 | vm->error = "VM trap: closure capture layout does not match target arity"; | 59 | 30 | } | 60 | 172 | Value *result = NULL; | 61 | 172 | if (!vm->error) { Branch (61:9): [True: 112, False: 60]
| 62 | 112 | CaptureCheck status; | 63 | 112 | result = value_closure(symbol, environment->as.sequence.items, count, &status); | 64 | 112 | if (!result) vm->error = status == CAPTURE_OUT_OF_MEMORY Branch (64:13): [True: 74, False: 38]
Branch (64:34): [True: 44, False: 30]
| 65 | 74 | ? "native VM out of memory" : "VM trap: closure cannot capture a resource-bearing value"; | 66 | 112 | } | 67 | 172 | free(symbol); | 68 | 172 | return result; | 69 | 176 | } |
|
70 | | |
71 | | static Value *async_service_only(VM *vm, const char *name, Value **arguments) |
72 | 0 | { |
73 | 0 | (void)name; |
74 | 0 | (void)arguments; |
75 | 0 | vm->error = "VM trap: async service requires await"; |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | | bool builtin_is_async(const Builtin *entry) |
80 | 60.0M | { |
81 | 60.0M | return entry && entry->call == async_service_only; Branch (81:12): [True: 36.8M, False: 23.2M]
Branch (81:21): [True: 950, False: 36.8M]
|
82 | 60.0M | } |
83 | | |
84 | | bool builtin_is_local_state(const Builtin *entry) |
85 | 60.0M | { |
86 | 60.0M | return entry && (entry->call == builtins_http_state_call || Branch (86:12): [True: 36.8M, False: 23.2M]
Branch (86:22): [True: 0, False: 36.8M]
|
87 | 36.8M | entry->call == builtins_http_writer_call || Branch (87:22): [True: 4, False: 36.8M]
|
88 | 36.8M | entry->call == builtins_http_connection_call); Branch (88:22): [True: 136, False: 36.8M]
|
89 | 60.0M | } |
90 | | |
91 | | static Value *state_execution_only(VM *vm, const char *name, Value **arguments) |
92 | 0 | { |
93 | 0 | (void)name; |
94 | 0 | (void)arguments; |
95 | 0 | vm->error = "VM trap: State operation requires an execution"; |
96 | 0 | return NULL; |
97 | 0 | } |
98 | | |
99 | | static const Builtin BUILTINS[] = { |
100 | | {"state_create", 1, false, BUILTIN_STATE_CREATE, state_execution_only}, |
101 | | {"state_read", 1, false, BUILTIN_STATE_READ, state_execution_only}, |
102 | | {"state_update", 2, false, BUILTIN_STATE_UPDATE, state_execution_only}, |
103 | | {"state_create_async", 1, false, BUILTIN_STATE_CREATE, async_service_only}, |
104 | | {"state_read_async", 1, false, BUILTIN_STATE_READ, async_service_only}, |
105 | | {"state_update_async", 2, false, BUILTIN_STATE_UPDATE, async_service_only}, |
106 | | {"$intrinsic_function", 1, true, BUILTIN_ORDINARY, function_reference}, |
107 | | {"$intrinsic_closure", 3, true, BUILTIN_ORDINARY, closure_create}, |
108 | | {"http_bytes_slice", 3, true, BUILTIN_ORDINARY, builtins_http_connection_call}, |
109 | | {"http_body", 1, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
110 | | {"http_body_size", 1, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
111 | | {"http_body_append", 4, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
112 | | {"http_connection_continue", 1, false, BUILTIN_HTTP_CONNECTION_CONTINUE, async_service_only}, |
113 | | {"http_listen_ordinary", 2, false, BUILTIN_HTTP_LISTEN_ORDINARY, async_service_only}, |
114 | | {"http_listen_contextual", 2, false, BUILTIN_HTTP_LISTEN_CONTEXTUAL, async_service_only}, |
115 | | {"http_close", 1, false, BUILTIN_HTTP_CLOSE, async_service_only}, |
116 | | {"http_destroy", 1, false, BUILTIN_HTTP_DESTROY, async_service_only}, |
117 | | {"http_connection_invoke", 4, false, BUILTIN_HTTP_CONNECTION_INVOKE, async_service_only}, |
118 | | {"http_connection_writer", 1, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
119 | | {"http_connection_routes", 1, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
120 | | {"http_connection_fallback", 1, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
121 | | {"http_connection_admit", 1, false, BUILTIN_ORDINARY, builtins_http_connection_call}, |
122 | | |
123 | | {"http_writer_get_status", 1, false, BUILTIN_ORDINARY, builtins_http_writer_call}, |
124 | | {"http_writer_get_headers", 1, false, BUILTIN_ORDINARY, builtins_http_writer_call}, |
125 | | {"http_writer_reject", 1, false, BUILTIN_ORDINARY, builtins_http_writer_call}, |
126 | | {"http_writer_status", 2, false, BUILTIN_ORDINARY, builtins_http_writer_call}, |
127 | | {"http_writer_headers", 2, false, BUILTIN_ORDINARY, builtins_http_writer_call}, |
128 | | {"http_writer_commit", 2, false, BUILTIN_ORDINARY, builtins_http_writer_call}, |
129 | | |
130 | | {"http_state_create", 3, false, BUILTIN_ORDINARY, builtins_http_state_call}, |
131 | | {"http_state_context", 1, true, BUILTIN_ORDINARY, builtins_http_state_call}, |
132 | | {"http_state_setup_error", 1, false, BUILTIN_ORDINARY, builtins_http_state_call}, |
133 | | {"http_state_fail_setup", 1, false, BUILTIN_ORDINARY, builtins_http_state_call}, |
134 | | {"http_state_register", 4, false, BUILTIN_ORDINARY, builtins_http_state_call}, |
135 | | {"tcp_service", 6, false, BUILTIN_TCP_SERVICE, async_service_only}, |
136 | | {"process_run_async", 7, false, BUILTIN_PROCESS_RUN_ASYNC, async_service_only}, |
137 | | {"process_run_isolated_async", 7, false, BUILTIN_PROCESS_RUN_ASYNC, async_service_only}, |
138 | | {"console_input", 1, false, BUILTIN_ORDINARY, console_input_call}, |
139 | | {"console_input_async", 1, false, BUILTIN_CONSOLE_INPUT_ASYNC, async_service_only}, |
140 | | {"console_output", 3, false, BUILTIN_ORDINARY, console_output_call}, |
141 | | {"console_output_async", 3, false, BUILTIN_CONSOLE_OUTPUT_ASYNC, async_service_only}, |
142 | | {"host_sleep_async", 1, false, BUILTIN_HOST_SLEEP_ASYNC, async_service_only}, |
143 | | {"instant_now_async", 0, false, BUILTIN_INSTANT_NOW_ASYNC, async_service_only}, |
144 | | {"environment_lookup_async", 1, false, BUILTIN_ENVIRONMENT_LOOKUP_ASYNC, async_service_only}, |
145 | | {"host_arguments_async", 0, false, BUILTIN_HOST_ARGUMENTS_ASYNC, async_service_only}, |
146 | | {"host_stdout_async", 1, false, BUILTIN_HOST_STDOUT_ASYNC, async_service_only}, |
147 | | {"host_stderr_async", 1, false, BUILTIN_HOST_STDERR_ASYNC, async_service_only}, |
148 | | {"host_exit_async", 1, false, BUILTIN_HOST_EXIT_ASYNC, async_service_only}, |
149 | | {"async_fake_read", 1, false, BUILTIN_ASYNC_FAKE_READ, async_service_only}, |
150 | | {"$tcp_exchange", 5, false, BUILTIN_TCP_EXCHANGE, async_service_only}, |
151 | | {"$http_transport_exchange", 6, false, BUILTIN_HTTP_TRANSPORT_EXCHANGE, async_service_only}, |
152 | | {"$tcp_serve", 4, false, BUILTIN_TCP_SERVE, async_service_only}, |
153 | | {"$tcp_serve_stream", 5, false, BUILTIN_TCP_SERVE_STREAM, async_service_only}, |
154 | | {"$tcp_server_read_chunk", 1, false, BUILTIN_TCP_SERVER_READ_CHUNK, async_service_only}, |
155 | | {"fs_metadata_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
156 | | {"fs_read_chunk_async", 3, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
157 | | {"fs_atomic_write_async", 2, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
158 | | {"fs_read_async", 2, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
159 | | {"fs_write_async", 2, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
160 | | {"fs_list_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
161 | | {"fs_remove_file_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
162 | | {"fs_remove_directory_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
163 | | {"fs_create_directory_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
164 | | {"fs_temp_directory_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
165 | | {"fs_temp_file_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
166 | | {"fs_directories_overlap_async", 2, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
167 | | {"fs_root_identity", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
168 | | {"fs_read_link_async", 2, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
169 | | {"fs_root_identity_async", 1, false, BUILTIN_FS_ROOT_IDENTITY_ASYNC, async_service_only}, |
170 | | {"fs_read_beneath_async", 3, false, BUILTIN_FS_READ_BENEATH_ASYNC, async_service_only}, |
171 | | {"fs_read_beneath", 3, false, BUILTIN_ORDINARY, host_capability_call}, |
172 | | {"fs_directories_overlap", 2, false, BUILTIN_ORDINARY, host_capability_call}, |
173 | | {"fs_read", 2, false, BUILTIN_ORDINARY, host_capability_call}, |
174 | | {"fs_write", 2, false, BUILTIN_ORDINARY, host_capability_call}, |
175 | | {"fs_metadata", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
176 | | {"fs_list", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
177 | | {"fs_create_directory", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
178 | | {"fs_remove_file", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
179 | | {"fs_remove_directory", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
180 | | {"stage_create_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
181 | | {"stage_path_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
182 | | {"stage_directory_async", 2, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
183 | | {"stage_write_async", 4, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
184 | | {"stage_symlink_async", 3, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
185 | | {"stage_publish_async", 3, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
186 | | {"stage_discard_async", 1, false, BUILTIN_FILESYSTEM_ASYNC, async_service_only}, |
187 | | {"stage_create", 1, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
188 | | {"stage_path", 1, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
189 | | {"stage_directory", 2, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
190 | | {"stage_write", 4, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
191 | | {"stage_symlink", 3, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
192 | | {"stage_publish", 3, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
193 | | {"stage_discard", 1, false, BUILTIN_ORDINARY, filesystem_stage_resource_call}, |
194 | | {"fs_stage_begin", 1, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
195 | | {"fs_stage_path", 1, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
196 | | {"fs_stage_directory", 2, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
197 | | {"fs_stage_write", 4, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
198 | | {"fs_stage_symlink", 3, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
199 | | {"fs_read_link", 2, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
200 | | {"fs_stage_publish", 3, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
201 | | {"fs_stage_close", 1, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
202 | | {"digest_sha256_open", 0, false, BUILTIN_ORDINARY, digest_call}, |
203 | | {"digest_update", 2, false, BUILTIN_ORDINARY, digest_call}, |
204 | | {"digest_finish", 1, false, BUILTIN_ORDINARY, digest_call}, |
205 | | {"digest_close", 1, false, BUILTIN_ORDINARY, digest_call}, |
206 | | {"sha256_hash", 1, true, BUILTIN_ORDINARY, digest_hash_call}, |
207 | | {"sha256_create", 0, false, BUILTIN_ORDINARY, digest_resource_call}, |
208 | | {"sha256_create_async", 0, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
209 | | {"sha256_update", 2, false, BUILTIN_ORDINARY, digest_resource_call}, |
210 | | {"sha256_update_async", 2, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
211 | | {"sha256_finish", 1, false, BUILTIN_ORDINARY, digest_resource_call}, |
212 | | {"sha256_finish_async", 1, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
213 | | {"sha256_close", 1, false, BUILTIN_ORDINARY, digest_resource_call}, |
214 | | {"sha256_close_async", 1, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
215 | | {"gzip_decoder_create", 1, false, BUILTIN_ORDINARY, gzip_resource_call}, |
216 | | {"gzip_decoder_create_async", 1, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
217 | | {"gzip_decoder_decode", 3, false, BUILTIN_ORDINARY, gzip_resource_call}, |
218 | | {"gzip_decoder_decode_async", 3, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
219 | | {"gzip_decoder_close", 1, false, BUILTIN_ORDINARY, gzip_resource_call}, |
220 | | {"gzip_decoder_close_async", 1, false, BUILTIN_CPU_RESOURCE_ASYNC, async_service_only}, |
221 | | {"http_stream_open_typed", 6, false, BUILTIN_ORDINARY, http_stream_typed_call}, |
222 | | {"http_stream_read_typed", 1, false, BUILTIN_ORDINARY, http_stream_typed_call}, |
223 | | {"http_stream_close_typed", 1, false, BUILTIN_ORDINARY, http_stream_typed_call}, |
224 | | {"http_stream_open_async", 6, false, BUILTIN_HTTP_STREAM_OPEN, async_service_only}, |
225 | | {"http_stream_read_async", 1, false, BUILTIN_HTTP_STREAM_READ, async_service_only}, |
226 | | {"http_stream_close_async", 1, false, BUILTIN_HTTP_STREAM_CLOSE, async_service_only}, |
227 | | {"http_stream_open", 6, false, BUILTIN_ORDINARY, http_stream_call}, |
228 | | {"http_stream_read", 1, false, BUILTIN_ORDINARY, http_stream_call}, |
229 | | {"http_stream_close", 1, false, BUILTIN_ORDINARY, http_stream_call}, |
230 | | {"gzip_open", 1, false, BUILTIN_ORDINARY, gzip_stream_call}, |
231 | | {"gzip_step", 3, false, BUILTIN_ORDINARY, gzip_stream_call}, |
232 | | {"gzip_close", 1, false, BUILTIN_ORDINARY, gzip_stream_call}, |
233 | | {"fs_read_chunk", 3, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
234 | | {"fs_atomic_write", 2, false, BUILTIN_ORDINARY, filesystem_stage_call}, |
235 | | {"fs_temp_file", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
236 | | {"fs_temp_directory", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
237 | | {"host_decode_utf8", 1, true, BUILTIN_ORDINARY, host_capability_call}, |
238 | | {"host_sleep", 1, false, BUILTIN_ORDINARY, host_capability_call}, |
239 | | {"process_run", 7, false, BUILTIN_PROCESS_RUN, host_capability_call}, |
240 | | {"process_run_isolated", 7, false, BUILTIN_ORDINARY, host_capability_call}, |
241 | | {"path_from_text", 1, true, BUILTIN_ORDINARY, host_type_call}, |
242 | | {"path_from_native", 1, true, BUILTIN_ORDINARY, host_type_call}, |
243 | | {"path_to_text", 1, true, BUILTIN_ORDINARY, host_type_call}, |
244 | | {"path_native_bytes", 1, true, BUILTIN_ORDINARY, host_type_call}, |
245 | | {"path_display", 1, true, BUILTIN_ORDINARY, host_type_call}, |
246 | | {"path_absolute", 1, true, BUILTIN_ORDINARY, host_type_call}, |
247 | | {"path_append", 2, true, BUILTIN_ORDINARY, host_type_call}, |
248 | | {"path_directory", 1, true, BUILTIN_ORDINARY, host_type_call}, |
249 | | {"path_filename", 1, true, BUILTIN_ORDINARY, host_type_call}, |
250 | | {"path_current", 0, true, BUILTIN_ORDINARY, host_type_call}, |
251 | | {"duration_nanoseconds", 1, true, BUILTIN_ORDINARY, host_type_call}, |
252 | | {"duration_ticks", 1, true, BUILTIN_ORDINARY, host_type_call}, |
253 | | {"duration_from_seconds", 1, true, BUILTIN_ORDINARY, host_type_call}, |
254 | | {"instant_now", 0, false, BUILTIN_ORDINARY, host_type_call}, |
255 | | {"instant_add", 2, true, BUILTIN_ORDINARY, host_type_call}, |
256 | | {"instant_difference", 2, true, BUILTIN_ORDINARY, host_type_call}, |
257 | | {"instant_before", 2, true, BUILTIN_ORDINARY, host_type_call}, |
258 | | |
259 | | {"$unit", 0, true, BUILTIN_ORDINARY, builtins_numeric_call}, |
260 | | {"nat", 1, true, BUILTIN_ORDINARY, builtins_numeric_call}, |
261 | | {"dec", 1, true, BUILTIN_ORDINARY, builtins_numeric_call}, |
262 | | {"quotient", 2, true, BUILTIN_ORDINARY, builtins_numeric_call}, |
263 | | {"print", 1, false, BUILTIN_PRINT, host_call}, |
264 | | {"read_line", 0, false, BUILTIN_ORDINARY, host_call}, |
265 | | {"read_file", 1, false, BUILTIN_ORDINARY, host_call}, |
266 | | {"write_file", 2, false, BUILTIN_ORDINARY, host_call}, |
267 | | {"len", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
268 | | {"append", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
269 | | {"concat", 2, true, BUILTIN_ORDINARY, builtins_text_call}, |
270 | | {"slice", 3, true, BUILTIN_ORDINARY, builtins_text_call}, |
271 | | {"starts_with", 2, true, BUILTIN_ORDINARY, builtins_text_call}, |
272 | | {"starts_with_at", 3, true, BUILTIN_ORDINARY, builtins_text_call}, |
273 | | {"reverse", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
274 | | {"is_digit", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
275 | | {"is_letter", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
276 | | {"is_whitespace", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
277 | | {"map", 0, true, BUILTIN_ORDINARY, builtins_collections_call}, |
278 | | {"map_put", 3, true, BUILTIN_ORDINARY, builtins_collections_call}, |
279 | | {"map_has", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
280 | | {"map_get", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
281 | | {"$method_put", 3, true, BUILTIN_ORDINARY, builtins_collections_call}, |
282 | | {"$method_has", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
283 | | {"$method_get", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
284 | | {"set", 0, true, BUILTIN_ORDINARY, builtins_collections_call}, |
285 | | {"set_add", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
286 | | {"set_has", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
287 | | {"$method_add", 2, true, BUILTIN_ORDINARY, builtins_collections_call}, |
288 | | {"bytes", 0, true, BUILTIN_ORDINARY, builtins_collections_call}, |
289 | | {"byte_append", 2, true, BUILTIN_ORDINARY, builtins_text_call}, |
290 | | {"bytes_concat", 2, true, BUILTIN_ORDINARY, builtins_text_call}, |
291 | | {"byte_len", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
292 | | {"byte_get", 2, true, BUILTIN_ORDINARY, builtins_text_call}, |
293 | | {"utf8_encode", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
294 | | {"utf8_decode", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
295 | | {"read_bytes", 1, false, BUILTIN_ORDINARY, host_call}, |
296 | | {"write_bytes", 2, false, BUILTIN_ORDINARY, host_call}, |
297 | | {"nat_from_str", 1, true, BUILTIN_ORDINARY, builtins_text_call}, |
298 | | {"command_args", 0, false, BUILTIN_ORDINARY, host_call}, |
299 | | {"environment_has", 1, false, BUILTIN_ORDINARY, host_call}, |
300 | | {"environment_get", 1, false, BUILTIN_ORDINARY, host_call}, |
301 | | {"eprint", 1, false, BUILTIN_ORDINARY, host_call}, |
302 | | {"process_exit", 1, false, BUILTIN_PROCESS_EXIT, host_call}, |
303 | | {"path_resolve", 1, false, BUILTIN_ORDINARY, host_call}, |
304 | | {"path_parent", 1, true, BUILTIN_ORDINARY, host_call}, |
305 | | {"path_join", 2, true, BUILTIN_ORDINARY, host_call}, |
306 | | {"path_suffix", 1, true, BUILTIN_ORDINARY, host_call}, |
307 | | {"path_with_suffix", 2, true, BUILTIN_ORDINARY, host_call}, |
308 | | {"path_is_absolute", 1, true, BUILTIN_ORDINARY, host_call}, |
309 | | {"file_exists", 1, false, BUILTIN_ORDINARY, host_call}, |
310 | | {"run_bytecode", 1, false, BUILTIN_RUN_BYTECODE, builtins_vm_call}, |
311 | | {"run_bytecode_args", 2, false, BUILTIN_RUN_BYTECODE_ARGS, builtins_vm_call}, |
312 | | }; |
313 | | |
314 | | const Builtin *builtin(const char *name) |
315 | 1.03M | { |
316 | 200M | for (size_t i = 0; i < sizeof(BUILTINS) / sizeof(*BUILTINS); i++) { Branch (316:24): [True: 200M, False: 627k]
|
317 | 200M | if (!strcmp(name, BUILTINS[i].name)) { Branch (317:13): [True: 405k, False: 199M]
|
318 | 405k | return &BUILTINS[i]; |
319 | 405k | } |
320 | 200M | } |
321 | 627k | return NULL; |
322 | 1.03M | } |
323 | | |
324 | | Value *builtin_call(VM *vm, const char *name, Value **arguments) |
325 | 1.23k | { |
326 | 1.23k | const Builtin *entry = builtin(name); |
327 | 1.23k | if (!entry) { Branch (327:9): [True: 1, False: 1.23k]
|
328 | 1 | vm->error = "VM trap: unknown builtin"; |
329 | 1 | return NULL; |
330 | 1 | } |
331 | 1.23k | return entry->call(vm, name, arguments); |
332 | 1.23k | } |