Commit bf136cbb
bf136cbb56c2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
fix(codegen): support Object.new as a sentinel allocation
Object.new.freeze constants used as identity sentinels referred to
a sp_Object C type that didn't exist in the runtime, and the
.freeze method call on a non-string receiver fell through every
dispatcher to the "0" fallback — both compile errors and a wrong
runtime answer.
Three small additions:
lib/sp_runtime.h: definesp_Object(a single-byte struct so every allocation occupies a distinct address) andsp_Object_new(a freshsp_gc_allocper call).spinel_codegen.rbconstructor dispatch: recognizeObjectnext toArray/Hash/Proc/StringIOand emitsp_Object_new(). The existingobj_Objecttype inference already produces the correct C type for the result.compile_introspection_expr: add a genericfreezearm that returns the receiver unchanged. Strings/mutable strings handle their ownfreezeearlier in the dispatcher; this arm covers obj_*, arrays, sp_Object *, etc., soFoo.new.freezeand the issue-#63 const-init shape work end-to-end.
test/object_new_sentinel.rb covers the const-sentinel idiom and
identity comparisons (== between distinct Object.new returns
false; the same constant compares equal to itself).
Closes #63
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- bf136cbb56c2
- Parent
- 878007c9e39b
lib/sp_runtime.hmodifiedView file
@@ -175,6 +175,12 @@
175175
void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}}size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}176176
void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}177177
178+
/* `Object.new` — a sentinel object whose only meaningful property is179+
identity. Each call returns a fresh GC-managed allocation, so two180+
`Object.new` results compare as `!=` via their pointer addresses. */181+
typedef struct sp_Object_s { uint8_t _pad; } sp_Object;182+
static sp_Object *sp_Object_new(void){return(sp_Object*)sp_gc_alloc(sizeof(sp_Object),NULL,NULL);}183+
178184
typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;}sp_IntArray;179185
static void sp_IntArray_fin(void*p){free(((sp_IntArray*)p)->data);}180186
static sp_IntArray*sp_IntArray_new(void){sp_IntArray*a=(sp_IntArray*)sp_gc_alloc(sizeof(sp_IntArray),sp_IntArray_fin,NULL);a->cap=16;a->data=(mrb_int*)malloc(sizeof(mrb_int)*a->cap);a->start=0;a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}return a;}spinel_codegen.rbmodifiedView file
Loading diff…
test/object_new_sentinel.rbaddedView file
Loading diff…