Commit 72c29227

72c292271321Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified

Description

Implement JSON.generate / JSON.dump for hashes, arrays, scalars

JSON.generate(hash) silently returned the integer 0 — there was no JSON dispatch, so the call fell through to the unresolved-call "0" fallback and the analyzer typed it as int.

Add a recursive serializer:

  • runtime sp_json_str (JSON string escaping: control chars -> \uNNNN plus the " \ \n \t \r \b \f shorthands; UTF-8 bytes pass through) and sp_json_val (compact CRuby form, : separators, nil->null, symbol/int keys coerced to strings) covering scalars, every array variant, and the header-defined hash variants (str_int, str_str, int_str, str_poly, sym_poly, poly_poly). Accumulators are GC-rooted.
  • sym-keyed int/str hashes have codegen-emitted structs, so their serializers (sp_SymIntHash_json / sp_SymStrHash_json) are emitted alongside those structs and called directly from the dispatch.
  • codegen: a JSON.generate/JSON.dump arm in compile_constant_recv_ expr that boxes the arg and runs sp_json_val (or the sym serializer).
  • analyze: types JSON.generate/dump as String.

Output matches CRuby across str/sym keys, int/str/array/nil/bool values, nesting, and escaping. Known limits (rare): a sym-int/str hash nested as a boxed poly value renders as null, and int-keyed-int hashes (no poly cls_id) aren't covered.

Fixes #1053

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

Details

Commit
72c292271321
Parent
01b0dddd4084

lib/sp_runtime.hmodifiedView file

@@ -3080,6 +3080,77 @@
30803080 static sp_PolyPolyHash*sp_IntStrHash_invert(sp_IntStrHash*h){sp_PolyPolyHash*r=sp_PolyPolyHash_new();if(!h)return r;for(mrb_int i=0;i<h->len;i++)sp_PolyPolyHash_set(r,sp_box_str(sp_IntStrHash_get(h,h->order[i])),sp_box_int(h->order[i]));return r;}
30813081 static mrb_bool sp_PolyPolyHash_eq(sp_PolyPolyHash*a,sp_PolyPolyHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){sp_RbVal k=a->keys[a->order[i]];if(!sp_PolyPolyHash_has_key(b,k))return FALSE;if(!sp_poly_eq(sp_PolyPolyHash_get(a,k),sp_PolyPolyHash_get(b,k)))return FALSE;}return TRUE;}
30823082
3083+/* JSON.generate support. sp_json_str quotes + escapes a string per
3084+ JSON rules (control chars -> \uNNNN, plus the \" \\ \n \t \r \b \f
3085+ shorthands; UTF-8 bytes >= 0x80 pass through unescaped, matching
3086+ CRuby's default). sp_json_val recursively serializes any boxed
3087+ value -- scalars, arrays, and the typed hash variants -- in CRuby's
3088+ compact form (no spaces, `:` separator). Object keys are coerced to
3089+ JSON strings (symbol -> name, integer -> decimal). Raw container
3090+ element / value types are boxed and routed back through sp_json_val
3091+ so nesting works. The sp_String accumulator is GC-rooted so a
3092+ collection triggered by a nested allocation can't free it. */
3093+static const char *sp_json_str(const char *s) __attribute__((unused));
3094+static const char *sp_json_str(const char *s) {
3095+ sp_String *o = sp_String_new("\"");
3096+ SP_GC_ROOT(o);
3097+ if (s) { const char *p = s; while (*p) { unsigned char c = (unsigned char)*p;
3098+ if (c == '"') sp_String_append(o, "\\\"");
3099+ else if (c == '\\') sp_String_append(o, "\\\\");
3100+ else if (c == '\n') sp_String_append(o, "\\n");
3101+ else if (c == '\t') sp_String_append(o, "\\t");
3102+ else if (c == '\r') sp_String_append(o, "\\r");
3103+ else if (c == '\b') sp_String_append(o, "\\b");
3104+ else if (c == '\f') sp_String_append(o, "\\f");
3105+ else if (c < 0x20) { char b[8]; snprintf(b, sizeof b, "\\u%04x", (unsigned)c); sp_String_append(o, b); }
3106+ else { char ch[2]; ch[0] = (char)c; ch[1] = 0; sp_String_append(o, ch); }
3107+ p++; } }
3108+ sp_String_append(o, "\"");
3109+ return o->data;
3110+}
3111+static const char *sp_json_val(sp_RbVal v) __attribute__((unused));
3112+static const char *sp_json_key(sp_RbVal k) {
3113+ if (k.tag == SP_TAG_STR) return sp_json_str(k.v.s);
3114+ if (k.tag == SP_TAG_SYM) return sp_json_str(sp_sym_to_s((sp_sym)k.v.i));
3115+ if (k.tag == SP_TAG_INT) return sp_json_str(sp_int_to_s(k.v.i));
3116+ if (k.tag == SP_TAG_BOOL) return sp_json_str(k.v.b ? "true" : "false");
3117+ if (k.tag == SP_TAG_NIL) return sp_json_str("");
3118+ return sp_json_str("");
3119+}
3120+static const char *sp_json_val(sp_RbVal v) {
3121+ switch (v.tag) {
3122+ case SP_TAG_INT: return sp_int_to_s(v.v.i);
3123+ case SP_TAG_FLT: return sp_float_to_s(v.v.f);
3124+ case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false");
3125+ case SP_TAG_NIL: return SPL("null");
3126+ case SP_TAG_STR: return sp_json_str(v.v.s);
3127+ case SP_TAG_SYM: return sp_json_str(sp_sym_to_s((sp_sym)v.v.i));
3128+ case SP_TAG_OBJ: {
3129+ sp_String *o;
3130+ switch (v.cls_id) {
3131+ case SP_BUILTIN_INT_ARRAY: { sp_IntArray *a=(sp_IntArray*)v.v.p; o=sp_String_new("["); SP_GC_ROOT(o); if(a){for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_int_to_s(a->data[a->start+i]));}} sp_String_append(o,"]"); return o->data; }
3132+ case SP_BUILTIN_FLT_ARRAY: { sp_FloatArray *a=(sp_FloatArray*)v.v.p; o=sp_String_new("["); SP_GC_ROOT(o); if(a){for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_float_to_s(a->data[i]));}} sp_String_append(o,"]"); return o->data; }
3133+ case SP_BUILTIN_STR_ARRAY: { sp_StrArray *a=(sp_StrArray*)v.v.p; o=sp_String_new("["); SP_GC_ROOT(o); if(a){for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(a->data[i]));}} sp_String_append(o,"]"); return o->data; }
3134+ case SP_BUILTIN_SYM_ARRAY: { sp_IntArray *a=(sp_IntArray*)v.v.p; o=sp_String_new("["); SP_GC_ROOT(o); if(a){for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(sp_sym_to_s((sp_sym)a->data[a->start+i])));}} sp_String_append(o,"]"); return o->data; }
3135+ case SP_BUILTIN_POLY_ARRAY: { sp_PolyArray *a=(sp_PolyArray*)v.v.p; o=sp_String_new("["); SP_GC_ROOT(o); if(a){for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_val(a->data[i]));}} sp_String_append(o,"]"); return o->data; }
3136+ case SP_BUILTIN_STR_INT_HASH: { sp_StrIntHash *h=(sp_StrIntHash*)v.v.p; o=sp_String_new("{"); SP_GC_ROOT(o); if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(h->order[i]));sp_String_append(o,":");sp_String_append(o,sp_int_to_s(sp_StrIntHash_get(h,h->order[i])));}} sp_String_append(o,"}"); return o->data; }
3137+ case SP_BUILTIN_STR_STR_HASH: { sp_StrStrHash *h=(sp_StrStrHash*)v.v.p; o=sp_String_new("{"); SP_GC_ROOT(o); if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(h->order[i]));sp_String_append(o,":");sp_String_append(o,sp_json_str(sp_StrStrHash_get(h,h->order[i])));}} sp_String_append(o,"}"); return o->data; }
3138+ case SP_BUILTIN_INT_STR_HASH: { sp_IntStrHash *h=(sp_IntStrHash*)v.v.p; o=sp_String_new("{"); SP_GC_ROOT(o); if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(sp_int_to_s(h->order[i])));sp_String_append(o,":");sp_String_append(o,sp_json_str(sp_IntStrHash_get(h,h->order[i])));}} sp_String_append(o,"}"); return o->data; }
3139+ /* SYM_INT_HASH / SYM_STR_HASH structs are codegen-emitted (not
3140+ in this static header), so their serializers are emitted by
3141+ codegen as sp_SymIntHash_json / sp_SymStrHash_json and called
3142+ directly from the JSON dispatch. A nested sym-int/str hash
3143+ reaching here as a boxed poly value falls through to null. */
3144+ case SP_BUILTIN_STR_POLY_HASH: { sp_StrPolyHash *h=(sp_StrPolyHash*)v.v.p; o=sp_String_new("{"); SP_GC_ROOT(o); if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(h->order[i]));sp_String_append(o,":");sp_String_append(o,sp_json_val(sp_StrPolyHash_get(h,h->order[i])));}} sp_String_append(o,"}"); return o->data; }
3145+ case SP_BUILTIN_SYM_POLY_HASH: { sp_SymPolyHash *h=(sp_SymPolyHash*)v.v.p; o=sp_String_new("{"); SP_GC_ROOT(o); if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_str(sp_sym_to_s((sp_sym)h->order[i])));sp_String_append(o,":");sp_String_append(o,sp_json_val(sp_SymPolyHash_get(h,h->order[i])));}} sp_String_append(o,"}"); return o->data; }
3146+ case SP_BUILTIN_POLY_POLY_HASH: { sp_PolyPolyHash *h=(sp_PolyPolyHash*)v.v.p; o=sp_String_new("{"); SP_GC_ROOT(o); if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(o,",");sp_String_append(o,sp_json_key(h->keys[h->order[i]]));sp_String_append(o,":");sp_String_append(o,sp_json_val(h->vals[h->order[i]]));}} sp_String_append(o,"}"); return o->data; }
3147+ default: return SPL("null");
3148+ }
3149+ }
3150+ default: return SPL("null");
3151+ }
3152+}
3153+
30833154 #include <setjmp.h>
30843155 #define SP_EXC_STACK_MAX 64
30853156 static jmp_buf sp_exc_stack[SP_EXC_STACK_MAX];