-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathscript_main.c
More file actions
412 lines (394 loc) · 13.8 KB
/
Copy pathscript_main.c
File metadata and controls
412 lines (394 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* SPEC: PJ2026-0501 V2 kernel v0.3; host script runner. */
#ifndef PIKA_SCRIPT_MAIN_ENABLE
#define PIKA_SCRIPT_MAIN_ENABLE 1
#endif
#if defined(__linux__) && PIKA_SCRIPT_MAIN_ENABLE
#include "pika_frontend.h"
#include "pika_kernel.h"
#ifdef PIKA_SCRIPT_BINDING_REGISTER
extern PikaStatus PIKA_SCRIPT_BINDING_REGISTER(
PikaBindingRegistry* registry);
#endif
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
typedef struct {
const char* entry_path;
const char* entry_module;
char* directory;
} ScriptSourceContext;
static PikaStatus write_stdout(void* context,
const char* bytes,
uint32_t length) {
FILE* stream = (FILE*)context;
return fwrite(bytes, 1u, length, stream) == length
? PIKA_STATUS_OK
: PIKA_STATUS_OUTPUT_ERROR;
}
static char* resolve_script_path(const char* input, int* is_directory) {
static const char suffix[] = "/main.py";
struct stat information;
size_t length;
char* path;
if (stat(input, &information) != 0) {
fprintf(stderr, "io_error: %s: %s\n", input, strerror(errno));
return NULL;
}
if (!S_ISDIR(information.st_mode)) {
*is_directory = 0;
length = strlen(input);
if (length < 3u || strcmp(&input[length - 3u], ".py") != 0) {
fprintf(stderr, "io_error: expected .py file: %s\n", input);
return NULL;
}
path = (char*)malloc(length + 1u);
if (path != NULL) {
memcpy(path, input, length + 1u);
}
return path;
}
*is_directory = 1;
length = strlen(input);
if (length > SIZE_MAX - sizeof(suffix)) {
return NULL;
}
path = (char*)malloc(length + sizeof(suffix));
if (path == NULL) {
return NULL;
}
memcpy(path, input, length);
memcpy(&path[length], suffix, sizeof(suffix));
if (stat(path, &information) != 0 || S_ISDIR(information.st_mode)) {
fprintf(stderr, "io_error: directory requires main.py: %s\n",
input);
free(path);
return NULL;
}
return path;
}
static PikaStatus read_module_source(const char* path,
const char** result,
size_t* length) {
FILE* file = fopen(path, "rb");
long end;
char* source;
size_t read_count;
if (file == NULL) {
return errno == ENOENT ? PIKA_STATUS_MODULE_NOT_FOUND
: PIKA_STATUS_IO_ERROR;
}
if (fseek(file, 0L, SEEK_END) != 0 ||
(end = ftell(file)) < 0L ||
fseek(file, 0L, SEEK_SET) != 0) {
fclose(file);
return PIKA_STATUS_IO_ERROR;
}
if ((unsigned long)end > SIZE_MAX - 1u) {
fclose(file);
return PIKA_STATUS_FRONTEND_LIMIT;
}
*length = (size_t)end;
if (*length > PIKA_FRONTEND_SOURCE_BYTE_LIMIT) {
fclose(file);
return PIKA_STATUS_FRONTEND_LIMIT;
}
source = (char*)malloc(*length + 1u);
if (source == NULL) {
fclose(file);
return PIKA_STATUS_FRONTEND_LIMIT;
}
read_count = fread(source, 1u, *length, file);
if (read_count != *length || fclose(file) != 0) {
free(source);
return PIKA_STATUS_IO_ERROR;
}
source[*length] = '\0';
*result = source;
return PIKA_STATUS_OK;
}
static char* script_directory(const char* path) {
const char* separator = strrchr(path, '/');
size_t length;
char* directory;
if (separator == NULL) {
directory = (char*)malloc(2u);
if (directory != NULL) {
directory[0] = '.';
directory[1] = '\0';
}
return directory;
}
length = separator == path ? 1u : (size_t)(separator - path);
directory = (char*)malloc(length + 1u);
if (directory != NULL) {
memcpy(directory, path, length);
directory[length] = '\0';
}
return directory;
}
static PikaStatus script_source_load(void* context,
const char* module_name,
const char** source,
size_t* length) {
ScriptSourceContext* source_context =
(ScriptSourceContext*)context;
const char suffix[] = ".py";
const char* path = source_context->entry_path;
char* module_path = NULL;
size_t directory_length;
size_t name_length;
PikaStatus status;
if (strcmp(module_name, source_context->entry_module) != 0) {
directory_length = strlen(source_context->directory);
name_length = strlen(module_name);
if (directory_length > SIZE_MAX - name_length -
sizeof(suffix) - 1u) {
return PIKA_STATUS_FRONTEND_LIMIT;
}
module_path = (char*)malloc(
directory_length + name_length + sizeof(suffix) + 1u);
if (module_path == NULL) return PIKA_STATUS_FRONTEND_LIMIT;
memcpy(module_path, source_context->directory,
directory_length);
module_path[directory_length] = '/';
memcpy(&module_path[directory_length + 1u], module_name,
name_length);
memcpy(&module_path[directory_length + name_length + 1u],
suffix, sizeof(suffix));
path = module_path;
}
status = read_module_source(path, source, length);
free(module_path);
return status;
}
static void script_source_release(void* context, const char* source) {
(void)context;
free((void*)source);
}
static void write_frontend_diagnostic(
PikaStatus status,
const PikaDiagnostic* diagnostic) {
const char* status_name = pika_status_name(status);
if (diagnostic->reason ==
PIKA_DIAGNOSTIC_REASON_LIMIT_EXCEEDED &&
diagnostic->bound != PIKA_FRONTEND_BOUND_NONE) {
fprintf(
stderr,
"%s:%s:%s:configured=%u:observed=%u:"
"line=%u:column=%u:offset=%u\n",
status_name,
pika_frontend_diagnostic_reason_name(
diagnostic->reason),
pika_frontend_bound_macro(diagnostic->bound),
diagnostic->configured, diagnostic->observed,
diagnostic->line, diagnostic->column,
diagnostic->offset);
return;
}
if (diagnostic->reason != PIKA_DIAGNOSTIC_REASON_NONE) {
fprintf(
stderr, "%s:%s:line=%u:column=%u:offset=%u\n",
status_name,
pika_frontend_diagnostic_reason_name(
diagnostic->reason),
diagnostic->line, diagnostic->column,
diagnostic->offset);
return;
}
fprintf(
stderr, "%s:%u:%u\n", status_name,
diagnostic->line, diagnostic->column);
}
static int execute_script(ScriptSourceContext* source_context) {
PikaCompiledModule* module = NULL;
PikaDiagnostic diagnostic = {0};
PikaModuleProvider provider = {
script_source_load,
script_source_release,
source_context,
};
PikaProgramRequirements requirements;
PikaArguments arguments = {NULL, 0u};
PikaStorage storage;
PikaOutput output = {write_stdout, stdout};
PikaResult result;
PikaMetrics metrics;
PikaStatus status;
uint32_t runtime_instruction = UINT32_MAX;
void* frames = NULL;
int64_t* values = NULL;
PikaRuntimeValue* typed_values = NULL;
PikaObjectSlot* object_slots = NULL;
uint8_t* object_arena = NULL;
PikaObjectStorage object_storage;
PikaObjectStorage* object_storage_pointer = NULL;
PikaFrontendConfig frontend_config;
#ifdef PIKA_SCRIPT_BINDING_REGISTER
PikaBindingRegistry registry;
PikaBindingStorage binding_storage;
PikaBindingStorage* binding_storage_pointer = NULL;
PikaBindingValue* binding_values = NULL;
PikaBindingObject* binding_objects = NULL;
status = PIKA_SCRIPT_BINDING_REGISTER(®istry);
if (status != PIKA_STATUS_OK) {
fprintf(stderr, "%s\n", pika_status_name(status));
return 1;
}
frontend_config.enabled =
PIKA_CAPABILITY_BINDING_DEFAULT |
(PIKA_CAPABILITY_OBJECT_CLASS & PIKA_CAPABILITY_PROVIDED);
frontend_config.bindings = ®istry;
#else
frontend_config.enabled =
PIKA_CAPABILITY_APPLICATION_DEFAULT |
(PIKA_CAPABILITY_OBJECT_CLASS & PIKA_CAPABILITY_PROVIDED);
frontend_config.bindings = NULL;
#endif
status = pika_module_graph_compile(
source_context->entry_module, &provider, &frontend_config,
&module, &diagnostic);
if (status != PIKA_STATUS_OK) {
write_frontend_diagnostic(status, &diagnostic);
return 1;
}
status = pika_program_storage_requirements(
pika_compiled_module_program(module), &requirements);
if (status == PIKA_STATUS_OK) {
frames = malloc(requirements.frame_bytes);
if (requirements.value_count > 0u) {
values = (int64_t*)calloc(
requirements.value_count, sizeof(int64_t));
}
if (requirements.typed_value_count > 0u) {
typed_values = (PikaRuntimeValue*)calloc(
requirements.typed_value_count, sizeof(PikaRuntimeValue));
}
if (requirements.object_slot_count > 0u) {
object_slots = (PikaObjectSlot*)calloc(
requirements.object_slot_count, sizeof(PikaObjectSlot));
if (requirements.object_bytes > 0u) {
object_arena = (uint8_t*)calloc(
requirements.object_bytes, sizeof(uint8_t));
}
if (object_slots != NULL &&
(requirements.object_bytes == 0u || object_arena != NULL)) {
memset(&object_storage, 0, sizeof(object_storage));
object_storage.slots = object_slots;
object_storage.capacity = requirements.object_slot_count;
object_storage.arena = object_arena;
object_storage.arena_capacity = requirements.object_bytes;
object_storage_pointer = &object_storage;
}
}
if (frames == NULL ||
(requirements.value_count > 0u && values == NULL) ||
(requirements.typed_value_count > 0u && typed_values == NULL) ||
(requirements.object_slot_count > 0u &&
object_storage_pointer == NULL)) {
status = PIKA_STATUS_STORAGE_TOO_SMALL;
}
#ifdef PIKA_SCRIPT_BINDING_REGISTER
if (requirements.binding_value_count > 0u) {
binding_values = (PikaBindingValue*)calloc(
requirements.binding_value_count,
sizeof(PikaBindingValue));
}
if (requirements.binding_object_count > 0u) {
binding_objects = (PikaBindingObject*)calloc(
requirements.binding_object_count,
sizeof(PikaBindingObject));
}
if ((requirements.binding_value_count > 0u &&
binding_values == NULL) ||
(requirements.binding_object_count > 0u &&
binding_objects == NULL)) {
status = PIKA_STATUS_STORAGE_TOO_SMALL;
} else if (requirements.binding_value_count > 0u ||
requirements.binding_object_count > 0u) {
binding_storage.values = binding_values;
binding_storage.value_capacity =
requirements.binding_value_count;
binding_storage.objects = binding_objects;
binding_storage.object_capacity =
requirements.binding_object_count;
binding_storage_pointer = &binding_storage;
}
#endif
}
if (status == PIKA_STATUS_OK) {
storage.frames = frames;
storage.frame_bytes = requirements.frame_bytes;
storage.values = values;
storage.value_capacity = requirements.value_count;
storage.typed_values = typed_values;
storage.typed_value_capacity = requirements.typed_value_count;
storage.objects = object_storage_pointer;
#ifdef PIKA_SCRIPT_BINDING_REGISTER
status = pika_program_execute_bound(
pika_compiled_module_program(module), &arguments, &storage,
binding_storage_pointer, &output, &result, &metrics);
#else
status = pika_program_execute(
pika_compiled_module_program(module), &arguments, &storage,
&output, &result, &metrics);
#endif
runtime_instruction = result.instruction_index;
}
#ifdef PIKA_SCRIPT_BINDING_REGISTER
free(binding_objects);
free(binding_values);
#endif
if (object_storage_pointer != NULL) {
pika_object_storage_release(object_storage_pointer);
}
free(object_arena);
free(object_slots);
free(typed_values);
free(values);
free(frames);
pika_compiled_module_destroy(module);
if (status != PIKA_STATUS_OK) {
if (runtime_instruction != UINT32_MAX) {
fprintf(
stderr, "%s:instruction=%u\n",
pika_status_name(status), runtime_instruction);
} else {
fprintf(stderr, "%s\n", pika_status_name(status));
}
return 1;
}
return 0;
}
int main(int argc, char** argv) {
char* path;
ScriptSourceContext source_context;
int is_directory = 0;
int result;
if (argc != 2) {
fprintf(stderr, "usage: pika_script <file.py|directory>\n");
return 2;
}
path = resolve_script_path(argv[1], &is_directory);
if (path == NULL) {
return 1;
}
source_context.entry_path = path;
source_context.entry_module = is_directory ? "main" : "__entry";
source_context.directory = script_directory(path);
if (source_context.directory == NULL) {
free(path);
fprintf(stderr, "frontend_limit\n");
return 1;
}
result = execute_script(&source_context);
free(source_context.directory);
free(path);
return result;
}
#else
typedef unsigned char PikaScriptMainDisabledTranslationUnit;
#endif