code wiki / (root) / nx_import.nx

nx_import.nx

buildroot/runtime/nx_import.nx

52844 B1341 linesdepth 2pulls 3 transitivereach 173 importersview sourcekind librarytopic import
docsdependenciesstructsconstsfunctions

about

import.nx -- sovereign import preprocessor (replaces main.c logic). Responsibility: given a root source path, return a single null- terminated buffer containing every imported file's text spliced inline, recursively, with each file visited at most once. Output is exactly what parse_module consumes. Subsystem boundary: - syscalls.nx : file I/O (sys_read_file) - str helpers : cstr_len / cstr_eq / u8_copy (below; no external dep) - import.nx : path ops + import expansion (this file) - nxc.nx : CLI driver that owns argv and calls expand_imports Invariants (enforced, not hoped): I1 Each canonicalised absolute path is expanded at most once -- the `seen` path table dedupes. Repeat imports emit nothing. I2 Canonicalisation is pure textual (.. pops, . drops, forward-slashes unified). No realpath(3). Identical output on Windows and Linux/NishiOS. I3 Import syntax: exactly `import "relative.nx"` at start-of- line after optional whitespace. Nothing else parses as an import; a substring match mid-line passes through. I4 Buffer overruns fail loudly (negative return). Never silently truncate -- correctness over convenience. I5 Fixed capacity: MAX_IMPORTS distinct imports per tree; paths up to IMPORT_PATH_LEN; output buffer sized by caller. A build that needs more raises one constant in one place. Complexity: O(N * F) where N is total source bytes across all files and F is average imports-per-file. One read per file.

dependencies 2 imports · 41 importers

nx_syscalls.nx nx_linemap.nx nx_import.nx nx_compile_field_candidate_t280.nx nx_compile_wat.nx nx_compile_wat_call_operands_t143. nx_compile_wat_scalararm.nx nx_compile_x86.nx nx_dom_oracle_gate.nx nx_import_qualified_gate.nx nx_ir_substitute_pairs_candidate_t nx_ir_substitute_qualification_t20 nx_lsp.nx

diagram shows first 10 each side; +0 more imports, +31 more importers in the complete lists below.

imports: nx_syscalls.nxnx_linemap.nx

imported by: nx_compile_field_candidate_t280.nxnx_compile_wat.nxnx_compile_wat_call_operands_t143.nxnx_compile_wat_scalararm.nxnx_compile_x86.nxnx_dom_oracle_gate.nxnx_import_qualified_gate.nxnx_ir_substitute_pairs_candidate_t216.nxnx_ir_substitute_qualification_t206.nxnx_lsp.nxnx_nxc.nxnx_sbr_admission_candidate.nxnx_sbr_key_candidate.nxnx_sov_build_run.nxnx_sov_build_run_qualify_20260910.nxnx_vcc_color_wat.nxnx_vcc_probe_wat.nxnx_wgsl.nxnx_wgsl_authored_t55.nxnx_wgsl_before_front_t217.nxnx_wgsl_canonical_skin_t140.nxnx_wgsl_cast_control_t181.nxnx_wgsl_cast_lbs_t181.nxnx_wgsl_cast_material_t181.nxnx_wgsl_cloud_transport_t155.nxnx_wgsl_cumulus_release_t161.nxnx_wgsl_cumulus_t156.nxnx_wgsl_depth_candidate_t192.nxnx_wgsl_eye_t138.nxnx_wgsl_face_mask_t163.nxnx_wgsl_front_candidate_t217.nxnx_wgsl_inspection_t165.nxnx_wgsl_sand_baseline_t137.nxnx_wgsl_sand_private_t137.nxnx_wgsl_scene_light_candidate_t364.nxnx_wgsl_skin_t138.nxnx_wgsl_sky_light_t154.nxnx_wgsl_sky_private_20260910.nxnx_wgsl_sky_review_t154.nxnx_wgsl_static_t335.nxnx_wgsl_world_depth_t195.nx

structs

403struct ExpandCtx

consts

46const MAX_IMPORTS: i64 = 256
47const IMPORT_PATH_LEN: i64 = 1024
50const ERR_IMPORTS_FULL: i64 = -1
51const ERR_READ_FAILED: i64 = -2
52const ERR_OUT_OVERFLOW: i64 = -3
53const ERR_BAD_IMPORT: i64 = -4
61const MAX_ALIASES: i64 = 256
64const AL_FIELDS: i64 = 6
65const AL_F_NAMEOFF: i64 = 0 // byte offset of the alias spelling in al_names
66const AL_F_EXPADDR: i64 = 1 // *i64: absolute address of each export name
67const AL_F_EXPLEN: i64 = 2 // *i64: length of each export name
68const AL_F_EXPN: i64 = 3 // export count
69const AL_F_RWBUF: i64 = 4 // *u8: this module's line-rewrite scratch
70const AL_F_RWCAP: i64 = 5 // bytes in AL_F_RWBUF
77const EXP_MIN_DECL_BYTES: i64 = 8
86const RW_GROWTH_SLACK: i64 = 3
93const EXPAND_CTX_BYTES: i64 = 4096
97const ERR_ALIAS_FULL: i64 = -5
98const ERR_ALIAS_CONFLICT: i64 = -6
99const ERR_ALIAS_UNKNOWN: i64 = -7
100const ERR_ALIAS_NOEXPORT: i64 = -8
101const ERR_ALIAS_BADNAME: i64 = -9
102const ERR_ALIAS_LINE_CAP: i64 = -10

functions

106func cstr_len(s: *u8) -> i64
112func cstr_eq(a: *u8, b: *u8) -> i64
123func u8_copy(dst: *u8, src: *u8, n: i64) -> i64
130func ptr_at(p: *u8, delta: i64) -> *u8
141func imp_ident_ch(b: i64) -> i64
151func imp_kw_at(src: *u8, p: i64, kw: *u8) -> i64
165func imp_is_main(src: *u8, s: i64, n: i64) -> i64
177func imp_err(s: *u8) -> i64
184func imp_err_span(p: *u8, n: i64) -> i64
193func imp_has_qual(src: *u8, s: i64, e: i64) -> i64
211func imp_path_index(paths: *u8, n: i64, path: *u8) -> i64
221func import_already(paths: *u8, n: i64, path: *u8) -> i64
calls 1: imp_path_index
226func import_add(paths: *u8, n: i64, path: *u8) -> i64
240func path_dir(path: *u8, out: *u8) -> i64
257func join_path(dir: *u8, name: *u8, out: *u8) -> i64
289func is_sep(b: i64) -> i64
called by 1: canonicalise_path
299func canonicalise_path(path: *u8, out: *u8, scratch: *u8) -> i64
385func starts_with_import(src: *u8, p: i64) -> i64
430func expand_ctx_new(out: *u8, out_cap: i64) -> *ExpandCtx
called by 17: mainmainmainmainmainmain+11 calls 1: sys_mmap
466func expand_ctx_enable_linemap(ctx: *ExpandCtx) -> *LineMap
called by 3: mainmainlsp_child_analyze calls 1: lm_new
473func out_push(ctx: *ExpandCtx, b: i64) -> i64
491func out_append_line(ctx: *ExpandCtx, src: *u8, n: i64) -> i64
called by 1: expand_imports_inner calls 2: u8_copyptr_at
501func out_append(ctx: *ExpandCtx, src: *u8, n: i64) -> i64
calls 2: u8_copyptr_at
555func imp_alias_init(ctx: *ExpandCtx) -> i64
called by 1: imp_alias_add calls 1: sys_mmap
563func imp_alias_slot(ctx: *ExpandCtx, k: i64, f: i64) -> i64
567func imp_alias_set(ctx: *ExpandCtx, k: i64, f: i64, v: i64) -> i64
572func imp_alias_name(ctx: *ExpandCtx, k: i64) -> *u8
577func imp_alias_find(ctx: *ExpandCtx, src: *u8, s: i64, n: i64) -> i64
596func imp_alias_add(ctx: *ExpandCtx, src: *u8, s: i64, n: i64) -> i64
621func imp_alias_scan_exports(ctx: *ExpandCtx, k: i64, src: *u8, n: i64) -> i64
691func imp_alias_exports(ctx: *ExpandCtx, k: i64, src: *u8, s: i64, n: i64) -> i64
720func imp_qualified_name(ctx: *ExpandCtx, src: *u8, s: i64, e: i64, out: *u8, ocap: i64) -> i64
767func imp_rw_line(ctx: *ExpandCtx, src: *u8, s: i64, e: i64, alias_idx: i64,
892func is_func_main_line(src: *u8, p: i64) -> i64
919func skip_func_main(src: *u8, p: i64) -> i64
967func expand_imports_inner(ctx: *ExpandCtx, path: *u8, is_root: i64, alias_idx: i64) -> i64;
980func file_exists(path: *u8) -> i64
987func try_subroot(dir: *u8, subroot: *u8, ipath: *u8,
1015func try_resolve_import(dir: *u8, ipath: *u8, out: *u8) -> i64
1094func expand_imports(ctx: *ExpandCtx, path: *u8) -> i64
1098func expand_imports_inner(ctx: *ExpandCtx, path: *u8, is_root: i64, alias_idx: i64) -> i64