Commit e8713484
e87134841bdfMerged
Ryan Seys <[email protected]> · 5 months ago · 6 files · +2 added · ~4 modified
Description
codegen,runtime: bind missing block and proc params to nil
A block or proc invoked with fewer arguments than it declares left the
unsupplied params holding the typed zero (0 for an int slot, a stale
slot for a poly), so proc { |a, b| p b }.call(1) printed 0 where
CRuby yields nil. Guard each proc-body param binding on argc: a
param past the supplied count takes its slot's nil sentinel — SP_INT_NIL
for an int or bool, sp_float_nil() for a float, ((sp_sym)-1) for a
symbol, NULL for a heap-pointer slot, and sp_box_nil() for a poly
(which also fixes a stale read of the reused static side-channel). The
pointer and symbol guards also keep a missing param from binding a
stale garbage pointer or symbol id.
The proc body already renders these sentinels as nil through .inspect,
.nil?, and || default, but string interpolation of an int slot
emitted a bare %lld and printed the raw sentinel. Route int
interpolation through sp_int_interp, which renders the nil sentinel as
the empty string (CRuby interpolates nil as "") and every other value as
its decimal. The empty case returns sp_str_empty, whose controlled
0xff marker byte keeps the runtime's s[-1] reads in bounds.
Regenerate bundle_tiny_misc's expected output, which had baked the old
zero-fill behavior and was masking this gap. Inspecting a nil int inside
a typed array (p [a, b, c]) still renders the sentinel and is left to
the broader nilable-scalar rendering work.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e87134841bdf
- Parent
- 3d4efd9a07a7
lib/sp_runtime.hmodifiedView file
/* Issue #760: NULL entries treated as empty strings. */static const char*sp_str_concat_arr(const char *const *parts,int n){size_t total=0;for(int i=0;i<n;i++)total+=sp_str_byte_len(parts[i]?parts[i]:"");char*r=sp_str_alloc(total);char*p=r;for(int i=0;i<n;i++){const char*s=parts[i]?parts[i]:"";size_t sl=sp_str_byte_len(s);memcpy(p,s,sl);p+=sl;}return r;}static const char*sp_int_to_s(mrb_int n){char*b=sp_str_alloc_raw(32);int len=snprintf(b,32,"%lld",(long long)n);if(len<0)len=0;sp_str_set_len(b,(size_t)len);return b;}/* String-interpolation of an int slot: a nil sentinel renders as the empty string (CRuby interpolates nil as ""), every other value as its decimal. */static const char*sp_int_interp(mrb_int n){return n==SP_INT_NIL?sp_str_empty:sp_int_to_s(n);}static const char*sp_int_to_s_base(mrb_int n,mrb_int base){if(base<2||base>36)base=10;char*b=sp_str_alloc_raw(72);char tmp[72];int i=0;int neg=0;uint64_t u;if(n<0){neg=1;u=(uint64_t)(-(n+1))+1;}else{u=(uint64_t)n;}if(u==0){tmp[i++]='0';}else{while(u>0){mrb_int d=u%base;tmp[i++]=d<10?'0'+d:'a'+d-10;u/=base;}}int j=0;if(neg)b[j++]='-';while(i>0)b[j++]=tmp[--i];b[j]=0;sp_str_set_len(b,(size_t)j);return b;}/* Float#to_s (Ruby semantics): produce the shortest decimal that round-trips back to the same double, formatted per CRuby — fixedsrc/codegen.cmodifiedView file
Loading diff…
src/codegen_expr.cmodifiedView file
Loading diff…
test/bundle_tiny_misc.rb.expectedmodifiedView file
Loading diff…
test/proc_arity_nil.rbaddedView file
Loading diff…
test/proc_arity_nil.rb.expectedaddedView file
Loading diff…