Commit 08740b9d

08740b9d30b9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified

Description

fix(runtime,codegen,analyze): String#prepend / #insert via mutable_str widening (#740, #741)

s = "world"; s.prepend("hello ") used to emit the unresolved- call warning ("cannot resolve call to 'prepend' on string") -- spinel's string type is const char *, which is immutable.

Three pieces:

  • analyze.rb: scope walker now widens a local from string to mutable_str when it sees lv.prepend(...), lv.insert(...), lv.concat(...), or lv.replace(...) (same shape as the existing << widening). The literal s = "world" then declares lv_s as sp_String *.
  • lib/sp_runtime.h: sp_String_insert and sp_String_replace added alongside sp_String_prepend. Both honor the same realloc-on-NULL short-circuit pattern as append/prepend.
  • spinel_codegen.rb: mutable_str recv arm in compile_call routes prepend / << / concat / insert / replace to the runtime helpers via a (mutate, recv) comma-form so the expression value is the receiver (chaining works).

Closes #740 Closes #741

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
08740b9d30b9
Parent
2e5fed4870ee

lib/sp_runtime.hmodifiedView file

@@ -1371,6 +1371,11 @@
13711371 check the result and bail without mutating on failure. */
13721372 static inline void sp_String_append(sp_String*s,const char*t){if(!s||!t)return;int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memcpy(s->data+s->len,t,tl+1);s->len+=tl;}
13731373 static inline void sp_String_prepend(sp_String*s,const char*t){if(!s||!t)return;int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;}
1374+/* Issue #741: String#insert(idx, str) -- insert str at idx. Negative
1375+ idx is relative to len+1 (insert before tail). */
1376+static inline void sp_String_insert(sp_String*s,int64_t idx,const char*t){if(!s||!t)return;int64_t tl=(int64_t)strlen(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;}
1377+/* Issue #740/#741 sibling: String#replace(s) -- replace entire content. */
1378+static inline void sp_String_replace(sp_String*s,const char*t){if(!s||!t)return;int64_t tl=(int64_t)strlen(t);if(tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=tl*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;}
13741379 static inline const char*sp_String_cstr(sp_String*s){return s->data;}
13751380 static inline int64_t sp_String_length(sp_String*s){return s->len;}
13761381 static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}