public: publish selfhost snapshot to public repo (SSOT using + AST merge + JSON VM fixes)

- SSOT using profiles (aliases/packages via nyash.toml), AST prelude merge
- Parser/member guards; Builder pin/PHI and instance→function rewrite (dev on)
- VM refactors (handlers split) and JSON roundtrip/nested stabilization
- CURRENT_TASK.md updated with scope and acceptance criteria

Notes: dev-only guards remain togglable via env; no default behavior changes for prod.
This commit is contained in:
nyash-codex
2025-09-26 14:34:42 +09:00
parent ecd46161b3
commit cdf826cbe7
44 changed files with 6264 additions and 576 deletions

View File

@ -91,6 +91,11 @@ impl MirBuilder {
method: String,
arguments: &[ASTNode],
) -> Result<ValueId, String> {
// Correctness-first: pin receiver so it has a block-local def and can safely
// flow across branches/merges when method calls are used in conditions.
let object_value = self
.pin_to_slot(object_value, "@recv")
.unwrap_or(object_value);
// Build argument values
let mut arg_values = Vec::new();
for arg in arguments {
@ -99,28 +104,58 @@ impl MirBuilder {
// If receiver is a user-defined box, lower to function call: "Box.method/(1+arity)"
let mut class_name_opt: Option<String> = None;
if let Some(cn) = self.value_origin_newbox.get(&object_value) { class_name_opt = Some(cn.clone()); }
// Heuristic guard: if this receiver equals the current function's 'me',
// prefer the enclosing box name parsed from the function signature.
if class_name_opt.is_none() {
if let Some(&me_vid) = self.variable_map.get("me") {
if me_vid == object_value {
if let Some(ref fun) = self.current_function {
if let Some(dot) = fun.signature.name.find('.') {
class_name_opt = Some(fun.signature.name[..dot].to_string());
}
}
}
}
}
if class_name_opt.is_none() {
if let Some(cn) = self.value_origin_newbox.get(&object_value) { class_name_opt = Some(cn.clone()); }
}
if class_name_opt.is_none() {
if let Some(t) = self.value_types.get(&object_value) {
if let MirType::Box(bn) = t { class_name_opt = Some(bn.clone()); }
}
}
// Optional dev/ci gate: enable builder-side instance→function rewrite by default
// in dev/ci profiles, keep OFF in prod. Allow explicit override via env:
// NYASH_BUILDER_REWRITE_INSTANCE={1|true|on} → force enable
// NYASH_BUILDER_REWRITE_INSTANCE={0|false|off} → force disable
let rewrite_enabled = {
match std::env::var("NYASH_BUILDER_REWRITE_INSTANCE").ok().as_deref().map(|v| v.to_ascii_lowercase()) {
Some(ref s) if s == "0" || s == "false" || s == "off" => false,
Some(ref s) if s == "1" || s == "true" || s == "on" => true,
_ => {
// Default: ON for dev/ci, OFF for prod
!crate::config::env::using_is_prod()
}
}
};
if rewrite_enabled {
if let Some(cls) = class_name_opt.clone() {
if self.user_defined_boxes.contains(&cls) {
let arity = arg_values.len(); // function name arity excludes 'me'
let fname = crate::mir::builder::calls::function_lowering::generate_method_function_name(&cls, &method, arity);
// Only use userbox path if such a function actually exists in the module
let has_fn = if let Some(ref module) = self.current_module {
// Gate: only rewrite when the lowered function actually exists (prevents false rewrites like JsonScanner.length/0)
let exists = if let Some(ref module) = self.current_module {
module.functions.contains_key(&fname)
} else { false };
if has_fn {
if exists {
if super::utils::builder_debug_enabled() || std::env::var("NYASH_BUILDER_DEBUG").ok().as_deref() == Some("1") {
super::utils::builder_debug_log(&format!("userbox method-call cls={} method={} fname={}", cls, method, fname));
}
let name_const = self.value_gen.next();
self.emit_instruction(MirInstruction::Const {
dst: name_const,
value: crate::mir::builder::ConstValue::String(fname),
value: crate::mir::builder::ConstValue::String(fname.clone()),
})?;
let mut call_args = Vec::with_capacity(arity + 1);
call_args.push(object_value); // 'me'
@ -133,12 +168,74 @@ impl MirBuilder {
args: call_args,
effects: crate::mir::EffectMask::READ.add(crate::mir::Effect::ReadHeap),
})?;
// Annotate return type/origin from lowered function signature
self.annotate_call_result_from_func_name(dst, &format!("{}.{}{}", cls, method, format!("/{}", arity)));
return Ok(dst);
} else {
// Special-case: treat toString as stringify when method not present
if method == "toString" && arity == 0 {
if let Some(ref module) = self.current_module {
let stringify_name = crate::mir::builder::calls::function_lowering::generate_method_function_name(&cls, "stringify", 0);
if module.functions.contains_key(&stringify_name) {
let name_const = self.value_gen.next();
self.emit_instruction(MirInstruction::Const {
dst: name_const,
value: crate::mir::builder::ConstValue::String(stringify_name.clone()),
})?;
let mut call_args = Vec::with_capacity(1);
call_args.push(object_value);
let dst = self.value_gen.next();
self.emit_instruction(MirInstruction::Call {
dst: Some(dst),
func: name_const,
callee: None,
args: call_args,
effects: crate::mir::EffectMask::READ.add(crate::mir::Effect::ReadHeap),
})?;
self.annotate_call_result_from_func_name(dst, &stringify_name);
return Ok(dst);
}
}
}
// Try alternate naming: <Class>Instance.method/Arity
let alt_cls = format!("{}Instance", cls);
let alt_fname = crate::mir::builder::calls::function_lowering::generate_method_function_name(&alt_cls, &method, arity);
let alt_exists = if let Some(ref module) = self.current_module {
module.functions.contains_key(&alt_fname)
} else { false };
if alt_exists {
if super::utils::builder_debug_enabled() || std::env::var("NYASH_BUILDER_DEBUG").ok().as_deref() == Some("1") {
super::utils::builder_debug_log(&format!("userbox method-call alt cls={} method={} fname={}", alt_cls, method, alt_fname));
}
let name_const = self.value_gen.next();
self.emit_instruction(MirInstruction::Const {
dst: name_const,
value: crate::mir::builder::ConstValue::String(alt_fname.clone()),
})?;
let mut call_args = Vec::with_capacity(arity + 1);
call_args.push(object_value); // 'me'
call_args.extend(arg_values.into_iter());
let dst = self.value_gen.next();
self.emit_instruction(MirInstruction::Call {
dst: Some(dst),
func: name_const,
callee: None,
args: call_args,
effects: crate::mir::EffectMask::READ.add(crate::mir::Effect::ReadHeap),
})?;
self.annotate_call_result_from_func_name(dst, &alt_fname);
return Ok(dst);
} else if super::utils::builder_debug_enabled() || std::env::var("NYASH_BUILDER_DEBUG").ok().as_deref() == Some("1") {
super::utils::builder_debug_log(&format!("skip rewrite (no fn): cls={} method={} fname={} alt={} (missing)", cls, method, fname, alt_fname));
}
}
}
}
}
// Fallback: if exactly one user-defined method matches by name/arity across module, resolve to that
// Fallback (narrowed): only when receiver class is known, and exactly one
// user-defined method matches by name/arity across module, resolve to that.
if rewrite_enabled && class_name_opt.is_some() {
if let Some(ref module) = self.current_module {
let tail = format!(".{}{}", method, format!("/{}", arg_values.len()));
let mut cands: Vec<String> = module
@ -155,7 +252,7 @@ impl MirBuilder {
let name_const = self.value_gen.next();
self.emit_instruction(MirInstruction::Const {
dst: name_const,
value: crate::mir::builder::ConstValue::String(fname),
value: crate::mir::builder::ConstValue::String(fname.clone()),
})?;
let mut call_args = Vec::with_capacity(arg_values.len() + 1);
call_args.push(object_value); // 'me'
@ -168,11 +265,14 @@ impl MirBuilder {
args: call_args,
effects: crate::mir::EffectMask::READ.add(crate::mir::Effect::ReadHeap),
})?;
// Annotate from signature if present
self.annotate_call_result_from_func_name(dst, &fname);
return Ok(dst);
}
}
}
}
}
// Else fall back to plugin/boxcall path
let result_id = self.value_gen.next();