----------------------------------------------------------------------- Part 2: The Solution — `module my_helpers shared` Compile a loader script that `require`s the shared module. The module is promoted to the global registry as a dependency. After that, any compilation — even with a completely fresh FileAccess — can `require my_helpers` because it lives in the global registry. ---------------
| 153 | // can `require my_helpers` because it lives in the global registry. |
| 154 | // ----------------------------------------------------------------------- |
| 155 | static void part2_solution(void) { |
| 156 | printf("=== Part 2: The Solution -- module my_helpers shared ===\n\n"); |
| 157 | |
| 158 | // --- 2a: Compile the loader to promote my_helpers to the global registry --- |
| 159 | // |
| 160 | // Promotion happens when a shared module is compiled as a DEPENDENCY |
| 161 | // (via `require`). The loader script exists solely to trigger this. |
| 162 | { |
| 163 | das_text_writer * tout = das_text_make_printer(); |
| 164 | das_module_group * libgrp = das_modulegroup_make(); |
| 165 | das_file_access * fa = das_fileaccess_make_default(); |
| 166 | |
| 167 | das_fileaccess_introduce_file(fa, "my_helpers.das", MODULE_SHARED, 0); |
| 168 | das_fileaccess_introduce_file(fa, "loader.das", LOADER_SCRIPT, 0); |
| 169 | |
| 170 | das_program * program = das_program_compile("loader.das", fa, tout, libgrp); |
| 171 | int err_count = das_program_err_count(program); |
| 172 | if (err_count) { |
| 173 | printf("Part 2a: loader compilation FAILED (%d errors) -- unexpected\n", err_count); |
| 174 | for (int i = 0; i < err_count; i++) { |
| 175 | das_error * error = das_program_get_error(program, i); |
| 176 | char buf[1024]; |
| 177 | das_error_report(error, buf, sizeof(buf)); |
| 178 | printf(" error %d: %s\n", i, buf); |
| 179 | } |
| 180 | das_program_release(program); |
| 181 | das_fileaccess_release(fa); |
| 182 | das_modulegroup_release(libgrp); |
| 183 | das_text_release(tout); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | printf("Part 2a: loader compiled -- 'my_helpers' promoted to global module registry\n\n"); |
| 188 | |
| 189 | // Release everything. The promoted module survives because |
| 190 | // Module::promoteToBuiltin() transferred ownership to the |
| 191 | // global linked list (builtIn=true skips deletion). |
| 192 | das_program_release(program); |
| 193 | das_fileaccess_release(fa); |
| 194 | das_modulegroup_release(libgrp); |
| 195 | das_text_release(tout); |
| 196 | } |
| 197 | |
| 198 | // --- 2b: Fresh FileAccess, no module file — compilation succeeds --- |
| 199 | { |
| 200 | das_text_writer * tout = das_text_make_printer(); |
| 201 | das_module_group * libgrp = das_modulegroup_make(); |
| 202 | das_file_access * fa = das_fileaccess_make_default(); |
| 203 | |
| 204 | // Only introduce the user script — NOT the module. |
| 205 | // The compiler finds my_helpers in the global registry. |
| 206 | das_fileaccess_introduce_file(fa, "user_script.das", USER_SCRIPT, 0); |
| 207 | |
| 208 | das_program * program = das_program_compile("user_script.das", fa, tout, libgrp); |
| 209 | int err_count = das_program_err_count(program); |
| 210 | if (err_count) { |
| 211 | printf("Part 2b: compilation FAILED (%d errors) -- unexpected\n", err_count); |
| 212 | for (int i = 0; i < err_count; i++) { |
no test coverage detected