Skip to content

Commit 74f23a0

Browse files
committed
added: terminate extension hook on shutdown
1 parent 4cd8d25 commit 74f23a0

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

compiler/include/astnodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,7 @@ void track_resolution_for_symbol_info(Context *context, AstNode *original, AstNo
24102410

24112411
// Compiler Extensions
24122412
TypeMatch compiler_extension_start(Context *context, const char *name, const char *containing_filename, Entity *ent, i32 *out_extension_id);
2413+
void compiler_extension_terminate(Context *context, int extension_id);
24132414
TypeMatch compiler_extension_expand_macro(
24142415
Context *context,
24152416
int extension_id,

compiler/src/extensions.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ static i32 extension_recv(CompilerExtension *ext, void *buf, i32 maxlen) {
5757
return bytes_read;
5858
}
5959

60+
static void extension_wait(CompilerExtension *ext) {
61+
int status;
62+
waitpid(ext->pid, &status, 0);
63+
}
64+
6065
static void extension_kill(CompilerExtension *ext) {
6166
ext->alive = 0;
6267
kill(ext->pid, SIGKILL);
63-
int status;
64-
waitpid(ext->pid, &status, 0);
68+
extension_wait(ext);
6569
}
6670

6771
static b32 extension_spawn(CompilerExtension *ext, const char *path) {
@@ -119,6 +123,9 @@ static b32 extension_poll_recv(CompilerExtension *ext) {
119123
return 0;
120124
}
121125

126+
static void extension_wait(CompilerExtension *ext) {
127+
}
128+
122129
static void extension_kill(CompilerExtension *ext) {
123130
}
124131

@@ -262,13 +269,11 @@ b32 compiler_extension_negotiate_capabilities(Context *context, CompilerExtensio
262269
}
263270
}
264271

265-
{
266-
compiler_event_log(
267-
context,
268-
"Extension '%s' spawned with protocol version %d.",
269-
ext->name, extension_protocol_version
270-
);
271-
}
272+
compiler_event_log(
273+
context,
274+
"Extension '%s' spawned with protocol version %d.",
275+
ext->name, extension_protocol_version
276+
);
272277

273278
bh_arena_clear(&ext->arena);
274279
return 1;
@@ -493,3 +498,22 @@ TypeMatch compiler_extension_hook_stalled(Context *context, int extension_id) {
493498
}
494499
}
495500

501+
void compiler_extension_terminate(Context *context, int extension_id) {
502+
if (extension_id <= 0 || extension_id > bh_arr_length(context->extensions)) return;
503+
504+
CompilerExtension *ext = &context->extensions[extension_id - 1];
505+
506+
if (!ext->alive) return;
507+
if (ext->state != COMP_EXT_STATE_READY) return;
508+
509+
extension_send_int(ext, MSG_HOST_TERMINATE);
510+
511+
compiler_event_log(
512+
context,
513+
"Waiting for extension '%s' to exit...",
514+
ext->name
515+
);
516+
517+
extension_wait(ext);
518+
}
519+

compiler/src/library_main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ void onyx_context_free(onyx_context_t *ctx) {
170170
shfree((*pscope)->symbols);
171171
}
172172

173+
bh_arr_each(CompilerExtension, ext, context->extensions) {
174+
compiler_extension_terminate(context, ext->id);
175+
}
176+
173177
onyx_wasm_module_free(context->wasm_module);
174178
bh_arena_free(&context->ast_arena);
175179
bh_arr_free(context->loaded_files);

0 commit comments

Comments
 (0)