builder/vm: stabilize json_lint_vm under unified calls

- Fix condition_fn resolution: Value call path + dev safety + stub injection
- VM bridge: handle Method::birth via BoxCall; ArrayBox push/get/length/set direct bridge
- Receiver safety: pin receiver in method_call_handlers to avoid undefined use across blocks
- Local vars: materialize on declaration (use init ValueId; void for uninit)
- Prefer legacy BoxCall for Array/Map/String/user boxes in emit_box_or_plugin_call (stability-first)
- Test runner: update LLVM hint to llvmlite harness (remove LLVM_SYS_180_PREFIX guidance)
- Docs/roadmap: update CURRENT_TASK with unified default-ON + guards

Note: NYASH_DEV_BIRTH_INJECT_BUILTINS=1 can re-enable builtin birth() injection during migration.
This commit is contained in:
nyash-codex
2025-09-28 12:19:49 +09:00
parent 41a46b433d
commit 510f4cf523
74 changed files with 2846 additions and 825 deletions

View File

@ -55,5 +55,131 @@ pub(super) fn apply_cli_directives_from_source(
}
// Lint: enforce fields at top-of-box (delegated)
super::pipeline::lint_fields_top(code, strict_fields, verbose)
super::pipeline::lint_fields_top(code, strict_fields, verbose)?;
// Dev-only guards (strict but opt-in via env)
// 1) ASI strict: disallow binary operator at end-of-line (line continuation)
if std::env::var("NYASH_ASI_STRICT").ok().as_deref() == Some("1") {
// operators to check (suffixes)
const OP2: [&str; 6] = ["==", "!=", "<=", ">=", "&&", "||"];
const OP1: [&str; 7] = ["+", "-", "*", "/", "%", "<", ">"];
for (i, line) in code.lines().enumerate() {
let l = line.trim_end();
if l.is_empty() { continue; }
let mut bad = false;
for op in OP2.iter() { if l.ends_with(op) { bad = true; break; } }
if !bad { for op in OP1.iter() { if l.ends_with(op) { bad = true; break; } } }
if bad {
return Err(format!("Parse error: Strict ASI violation — line {} ends with operator", i + 1));
}
}
}
// 2) '+' mixed types (String and Number) error (opt-in)
if std::env::var("NYASH_PLUS_MIX_ERROR").ok().as_deref() == Some("1") {
for (i, line) in code.lines().enumerate() {
if let Some((ltok, rtok)) = find_plus_operands(line) {
let left_is_num = is_number_literal(ltok);
let right_is_str = is_string_literal(rtok);
let left_is_str = is_string_literal(ltok);
let right_is_num = is_number_literal(rtok);
if (left_is_num && right_is_str) || (left_is_str && right_is_num) {
return Err(format!("Type error: '+' mixed String and Number at line {} (use str()/explicit conversion)", i + 1));
}
}
}
}
// 3) '==' on likely Box variables: emit guidance to use equals() (opt-in)
if std::env::var("NYASH_BOX_EQ_GUIDE_ERROR").ok().as_deref() == Some("1") {
for (i, line) in code.lines().enumerate() {
let l = line;
let mut idx = 0usize;
while let Some(pos) = l[idx..].find("==") {
let at = idx + pos;
// find left token end and right token start
let (left_ok, right_ok) = (peek_ident_left(l, at), peek_ident_right(l, at + 2));
if left_ok && right_ok {
return Err(format!("Type error: '==' on boxes — use equals() (line {})", i + 1));
}
idx = at + 2;
}
}
}
Ok(())
}
// ---- Helpers (very small, no external deps) ----
fn is_number_literal(s: &str) -> bool {
let t = s.trim();
!t.is_empty() && t.chars().all(|c| c.is_ascii_digit())
}
fn is_string_literal(s: &str) -> bool {
let t = s.trim();
t.starts_with('"')
}
fn find_plus_operands(line: &str) -> Option<(&str, &str)> {
let bytes = line.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
if bytes[i] as char == '+' {
// extract left token
let mut l = i;
while l > 0 && bytes[l - 1].is_ascii_whitespace() { l -= 1; }
let mut lstart = l;
while lstart > 0 {
let c = bytes[lstart - 1] as char;
if c.is_ascii_alphanumeric() || c == '_' || c == '"' { lstart -= 1; } else { break; }
}
let left = &line[lstart..l];
// extract right token
let mut r = i + 1;
while r < bytes.len() && bytes[r].is_ascii_whitespace() { r += 1; }
let mut rend = r;
while rend < bytes.len() {
let c = bytes[rend] as char;
if c.is_ascii_alphanumeric() || c == '_' || c == '"' { rend += 1; } else { break; }
}
if r <= rend { let right = &line[r..rend]; return Some((left, right)); }
return None;
}
i += 1;
}
None
}
fn peek_ident_left(s: &str, pos: usize) -> bool {
// scan left for first non-space token end, then back to token start
let bytes = s.as_bytes();
if pos == 0 { return false; }
let mut i = pos;
// skip spaces
while i > 0 && bytes[i - 1].is_ascii_whitespace() { i -= 1; }
if i == 0 { return false; }
// now consume identifier chars backwards
let mut j = i;
while j > 0 {
let c = bytes[j - 1] as char;
if c.is_ascii_alphanumeric() || c == '_' { j -= 1; } else { break; }
}
if j == i { return false; }
// ensure not starting with digit only (avoid numeric literal)
let tok = &s[j..i];
!tok.chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false)
}
fn peek_ident_right(s: &str, pos: usize) -> bool {
let bytes = s.as_bytes();
let mut i = pos;
while i < bytes.len() && bytes[i].is_ascii_whitespace() { i += 1; }
if i >= bytes.len() { return false; }
let mut j = i;
while j < bytes.len() {
let c = bytes[j] as char;
if c.is_ascii_alphanumeric() || c == '_' { j += 1; } else { break; }
}
if j == i { return false; }
let tok = &s[i..j];
!tok.chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false)
}