Commit b31dd3e2

b31dd3e289c6Merged

Sam Ruby <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen,runtime): Array#join on mutable_str_ptr_array (#526)

#522 added the mutable_str_ptr_array tag (sp_PtrArray of sp_String*) for .map { String.new } blocks, but the is_ptr_array_type recv block in compile_call_expr had no join arm — xs.map { String.new }.join(",") fell through to the unresolved-call warning and emitted 0.

Add a sibling helper sp_PtrArray_str_join (mirror of sp_StrArray_join but reads each entry's bytes via the sp_String data/len pair, preserving embedded NULs) and a dispatch arm gated on elem_type == "mutable_str".

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b31dd3e289c6
Parent
992c82eada39

lib/sp_runtime.hmodifiedView file

@@ -884,6 +884,11 @@
884884 static const char*sp_FloatArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_FloatArray_inspect((sp_FloatArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
885885 static const char*sp_StrArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_StrArray_inspect((sp_StrArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
886886 static const char*sp_SymArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_SymArray_inspect((sp_IntArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
887+/* issue #526: join for a sp_PtrArray of sp_String* (mutable_str_ptr_array).
888+ Sibling to sp_StrArray_join — same shape, but reads each element's
889+ bytes from the sp_String data/len pair (preserves embedded NULs)
890+ rather than strlen on a const char*. */
891+static const char*sp_PtrArray_str_join(sp_PtrArray*a,const char*sep){size_t sl=strlen(sep),cap=256;char*buf=(char*)malloc(cap);size_t len=0;for(mrb_int i=0;i<a->len;i++){if(i>0){if(len+sl>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,sep,sl);len+=sl;}sp_String*s=(sp_String*)a->data[i];size_t el=s?(size_t)s->len:0;if(len+el>=cap){cap=(len+el)*2+1;buf=(char*)realloc(buf,cap);}if(el){memcpy(buf+len,s->data,el);len+=el;}}buf[len]=0;char*r=sp_str_alloc(len);memcpy(r,buf,len);free(buf);return r;}
887892
888893 #ifdef __FreeBSD__
889894