From d8da21a406f449d277c42b91d4c9bf8b20bd0dcc Mon Sep 17 00:00:00 2001 From: zinc Date: Wed, 14 Oct 2026 07:55:08 +0000 Subject: embed: tests, editor support and docs; js vm file access; test fixes embed declarations get accept and reject fixtures, a run-only image check through zc0, an lsp keyword hover and the grammar keyword. errors point at the declaration and still declare the name, so its uses are not reported again. zc0 tracks byte-string globals and lays embedded arrays out one word per element, the way its own indexing reads them. the command-line javascript vm reaches node's file system for the file syscalls, so include and embed work there; other embedders opt in. the web build is self-contained in build/web with its examples. README, BORROW and BOOTSTRAP-OWNERSHIP describe the new build and ownership rules, and ONBOARD.md is a reading and verification order for the code. the suite passes the map header to mklayout, the wasm fuzz program uses the include format, and lsp drivers scale their deadlines on hosts without the arm64 jit (ZC_TEST_TIMEOUT_SCALE). LLVM_CONFIG selects an llvm with headers for the llvm section. --- src/compiler/compiler.zc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/compiler.zc b/src/compiler/compiler.zc index 244efc5..c6e1ca4 100644 --- a/src/compiler/compiler.zc +++ b/src/compiler/compiler.zc @@ -4737,7 +4737,7 @@ int kw_embed() { off = toff; n = name(); expect(s_semi); data = m*embed_open(path); - if (!data) { int sv = tpack(); tunpack(pp); serr("can't read the file named by embed"); tunpack(sv); data = chk_resize(0, 0, 1); } + if (!data) { embed_fail(pp, "can't read the file named by embed"); data = chk_resize(0, 0, 1); } sz = data[0]; if (!t) t = tbstr; // in a function the data sits in the code stream, so the code jumps over it @@ -4750,10 +4750,10 @@ int kw_embed() { } if (t[0] == TY_ARR) { w = embed_width(t[4]); - if (!w) { serr("embed declares a bstr, a char*, a number, or an array of numbers"); if (skip >= 0) setlit(skip, here); return 0; } - if (sz % w) { serr("the embedded file's size is not a whole number of array elements"); if (skip >= 0) setlit(skip, here); return 0; } + if (!w) { embed_fail(pp, "embed declares a bstr, a char*, a number, or an array of numbers"); if (skip >= 0) setlit(skip, here); return embed_bind(n, off, tword, 0, 2); } + if (sz % w) { embed_fail(pp, "the embedded file's size is not a whole number of array elements"); if (skip >= 0) setlit(skip, here); return embed_bind(n, off, tword, 0, 2); } count = sz / w; - if (t[3] >= 0 && count > t[3]) { serr("the embedded file is larger than the array it is declared as"); if (skip >= 0) setlit(skip, here); return 0; } + if (t[3] >= 0 && count > t[3]) { embed_fail(pp, "the embedded file is larger than the array it is declared as"); if (skip >= 0) setlit(skip, here); return embed_bind(n, off, tword, 0, 2); } if (t[3] < 0) t = tarr(t[4], count); k = tsz(t); reserve_code(k); a = here; i = 0; if (t[4][0] == TY_CHAR) { while (i < k) { if (i < sz) emit(data[1 + i]); else emit(0); i++; } } @@ -4768,15 +4768,22 @@ int kw_embed() { return embed_bind(n, off, t, a, 1); } w = embed_width(t); - if (!w) { serr("embed declares a bstr, a char*, a number, or an array of numbers"); return 0; } - if (sz > w) { serr("the embedded file is larger than the type it is declared as"); return 0; } - if (isfl(t) && sz != w) { serr("an embedded float takes exactly as many bytes as its type"); return 0; } + if (!w) { embed_fail(pp, "embed declares a bstr, a char*, a number, or an array of numbers"); return embed_bind(n, off, tword, 0, 2); } + if (sz > w) { embed_fail(pp, "the embedded file is larger than the type it is declared as"); return embed_bind(n, off, tword, 0, 2); } + if (isfl(t) && sz != w) { embed_fail(pp, "an embedded float takes exactly as many bytes as its type"); return embed_bind(n, off, tword, 0, 2); } v = 0; i = 0; while (i < sz) { v = v | (data[1 + i] << (i * 8)); i++; } if (isfl(t) && w == 4) v = rt.s2f(v); else v = cnorm(t, v); return embed_bind(n, off, t, v, 2); } +// report msg at the embed declaration that starts at the token packed in pp. the +// caller still declares the name, as a plain 0, so its uses are not reported too. +int embed_fail(int pp, char* msg) { + int sv = tpack(); + tunpack(pp); serr(msg); tunpack(sv); + return 0; +} // the bytes one element of type t takes in an embedded file: the packed size of a // sized type or f32, 1 for char, 8 for the other number types, 0 when t is not a number. int embed_width(word t) { -- cgit v1.2.3