Commit d8316313
d83163135c73Merged
Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): ENV.fetch(key, default)
Pre-fix ENV.fetch(k, dflt) emitted "cannot resolve call to 'fetch'
on int" and segfaulted at runtime. ENV[k] was wired but the fetch
arm never landed.
Adds the type-inference and codegen arms for the two-arg form:
ENV.fetch(key, default) -> ({ const char *_t = getenv(key); _t ? sp_str_dup_external(_t) : (default); })
Single-arg ENV.fetch("X") falls through to the same path as
ENV["X"] -- spinel doesn't model KeyError, so the strict CRuby
behaviour isn't reproduced; callers wanting that should pass an
explicit default. Block form (ENV.fetch("X") { ... }) is not
handled.
Regression test: test/env_fetch.rb covers literal, computed, and concatenation defaults.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- d83163135c73
- Parent
- ae1a0f329f3d
spinel_codegen.rbmodifiedView file
if mname == "[]" return "string" end if mname == "fetch" return "string" end end if rcname == "Dir" if mname == "home" if mname == "[]" return "sp_str_dup_external(getenv(" + compile_arg0(nid) + "))" end if mname == "fetch" # Two-arg form: ENV.fetch(key, default). If the env var is # set, return its value; otherwise return the supplied # default. The single-arg form (`ENV.fetch("X")`) raises # KeyError in CRuby; spinel doesn't model that, so it # behaves like ENV[] -- callers wanting strict behaviour # should pass an explicit default. args_id = @nd_arguments[nid] if args_id >= 0 argz = get_args(args_id) if argz.length >= 2 key = compile_expr(argz[0]) dfl = compile_expr_as_string(argz[1]) tmp = new_temp return "({ const char *" + tmp + " = getenv(" + key + "); " + tmp + " ? sp_str_dup_external(" + tmp + ") : (" + dfl + "); })" end if argz.length == 1 return "sp_str_dup_external(getenv(" + compile_arg0(nid) + "))" end end return "(&(\"\\xff\")[1])" end end # Dir if rcname == "Dir"test/env_fetch.rbaddedView file
Loading diff…
test/env_fetch.rb.expectedaddedView file
Loading diff…