Commit a668764f
a668764f7a40Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 9 files · +2 added · ~7 modified
Description
Merge branch 'fix-843-source-encoding'
Details
- Commit
- a668764f7a40
- Parent
- 95ae83abcecb
- Merge parent
- 10f0b8606aa3
lib/sp_runtime.hmodifiedView file
@@ -589,6 +589,18 @@
589589
}590590
591591
static void sp_oom_die(void);592+
593+
/* ---- Encoding runtime ----594+
Spinel currently assumes UTF-8 source and string data. Keep Encoding595+
as a tiny value type so `__ENCODING__` and `String#encoding` can596+
answer Ruby's Encoding-shaped protocol without carrying full597+
transcoding state. */598+
typedef struct{const char *name;}sp_Encoding;599+
static inline sp_Encoding sp_encoding_utf8(void){return(sp_Encoding){"UTF-8"};}600+
static inline const char*sp_encoding_name(sp_Encoding e){return e.name?e.name:sp_str_empty;}601+
static inline const char*sp_encoding_inspect(sp_Encoding e){return sp_sprintf("#<Encoding:%s>",sp_encoding_name(e));}602+
static inline mrb_bool sp_encoding_eq(sp_Encoding a,sp_Encoding b){const char*an=sp_encoding_name(a);const char*bn=sp_encoding_name(b);return strcmp(an,bn)==0;}603+
592604
static char *sp_str_alloc(size_t len) {593605
size_t total = sizeof(sp_str_hdr) + 1 + len + 1;594606
sp_str_hdr *h = (sp_str_hdr *)malloc(total);@@ -2251,6 +2263,9 @@
22512263
the boxed sp_Class's cls_id directly so unboxing is just a22522264
field read. */22532265
#define SP_TAG_CLASS 72266+
/* Encoding values boxed into a poly slot. v.s carries the canonical2267+
encoding name (`"UTF-8"` in Spinel's current runtime model). */2268+
#define SP_TAG_ENCODING 822542269
/* Negative cls_id values let SP_TAG_OBJ also carry built-in pointer22552270
types (IntArray, FloatArray, ...) — avoids minting a new SP_TAG_*22562271
per type. Non-negative cls_id stays an index into the user-class@@ -2286,6 +2301,7 @@
22862301
static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.cls_id = 0; r.v.i = (mrb_int)v; return r; }22872302
/* box a sp_Class into a poly slot. */22882303
static sp_RbVal sp_box_class(sp_Class c) { sp_RbVal r; r.tag = SP_TAG_CLASS; r.cls_id = (int)c.cls_id; r.v.i = c.cls_id; return r; }2304+
static sp_RbVal sp_box_encoding(sp_Encoding e) { sp_RbVal r; r.tag = SP_TAG_ENCODING; r.cls_id = 0; r.v.s = sp_encoding_name(e); return r; }22892305
22902306
/* every non-value-type sp_<C> starts22912307
with `mrb_int cls_id`. Read it back from a void* when@@ -2456,6 +2472,7 @@
24562472
case SP_TAG_BOOL: puts(v.v.b ? "true" : "false"); break;24572473
case SP_TAG_NIL: putchar('\n'); break;24582474
case SP_TAG_SYM: { const char *_ss = sp_sym_to_s((sp_sym)v.v.i); fputs(_ss, stdout); putchar('\n'); break; }2475+
case SP_TAG_ENCODING: { const char *_es = v.v.s ? v.v.s : sp_str_empty; fputs(_es, stdout); putchar('\n'); break; }24592476
case SP_TAG_OBJ: {24602477
/* MRI's `puts arr` iterates an Array, printing one element per24612478
line (using to_s on each); a non-Array OBJ falls back to@@ -2517,6 +2534,7 @@
25172534
case SP_TAG_NIL: return sp_str_empty;25182535
case SP_TAG_SYM: return sp_sym_to_s((sp_sym)v.v.i);25192536
case SP_TAG_CLASS: { sp_Class c = {v.v.i}; return sp_class_to_s(c); }2537+
case SP_TAG_ENCODING: return v.v.s ? v.v.s : sp_str_empty;25202538
case SP_TAG_OBJ:25212539
switch (v.cls_id) {25222540
case SP_BUILTIN_INT_ARRAY: return sp_IntArray_inspect((sp_IntArray *)v.v.p);@@ -2537,7 +2555,7 @@
25372555
static mrb_int sp_poly_to_i(sp_RbVal v) { if (v.tag == SP_TAG_INT || v.tag == SP_TAG_SYM) return v.v.i; if (v.tag == SP_TAG_STR) return (mrb_int)strtoll(v.v.s ? v.v.s : sp_str_empty, NULL, 10); if (v.tag == SP_TAG_FLT) return (mrb_int)v.v.f; if (v.tag == SP_TAG_BOOL) return v.v.b ? 1 : 0; return 0; }25382556
static mrb_float sp_poly_to_f(sp_RbVal v) { if (v.tag == SP_TAG_FLT) return v.v.f; if (v.tag == SP_TAG_INT || v.tag == SP_TAG_SYM) return (mrb_float)v.v.i; if (v.tag == SP_TAG_BOOL) return v.v.b ? 1.0 : 0.0; return 0.0; }25392557
static mrb_bool sp_poly_numeric_p(sp_RbVal v) { return v.tag == SP_TAG_INT || v.tag == SP_TAG_FLT; }2540-
static mrb_bool sp_poly_eq(sp_RbVal a, sp_RbVal b) { if (sp_poly_numeric_p(a) && sp_poly_numeric_p(b)) return sp_poly_to_f(a) == sp_poly_to_f(b); if (a.tag != b.tag) return FALSE; switch (a.tag) { case SP_TAG_INT: return a.v.i == b.v.i; case SP_TAG_STR: return (a.v.s == NULL || b.v.s == NULL) ? (a.v.s == b.v.s) : (strcmp(a.v.s, b.v.s) == 0); case SP_TAG_FLT: return a.v.f == b.v.f; case SP_TAG_BOOL: return a.v.b == b.v.b; case SP_TAG_NIL: return TRUE; case SP_TAG_SYM: return a.v.i == b.v.i; case SP_TAG_OBJ: return a.cls_id == b.cls_id && a.v.p == b.v.p; default: return FALSE; } }2558+
static mrb_bool sp_poly_eq(sp_RbVal a, sp_RbVal b) { if (sp_poly_numeric_p(a) && sp_poly_numeric_p(b)) return sp_poly_to_f(a) == sp_poly_to_f(b); if (a.tag != b.tag) return FALSE; switch (a.tag) { case SP_TAG_INT: return a.v.i == b.v.i; case SP_TAG_STR: return (a.v.s == NULL || b.v.s == NULL) ? (a.v.s == b.v.s) : (strcmp(a.v.s, b.v.s) == 0); case SP_TAG_FLT: return a.v.f == b.v.f; case SP_TAG_BOOL: return a.v.b == b.v.b; case SP_TAG_NIL: return TRUE; case SP_TAG_SYM: return a.v.i == b.v.i; case SP_TAG_ENCODING: return (a.v.s == NULL || b.v.s == NULL) ? (a.v.s == b.v.s) : (strcmp(a.v.s, b.v.s) == 0); case SP_TAG_OBJ: return a.cls_id == b.cls_id && a.v.p == b.v.p; default: return FALSE; } }25412559
static mrb_int sp_poly_cmp(sp_RbVal a, sp_RbVal b, mrb_bool *comparable) { if (sp_poly_numeric_p(a) && sp_poly_numeric_p(b)) { mrb_float af = sp_poly_to_f(a), bf = sp_poly_to_f(b); *comparable = TRUE; return (af > bf) - (af < bf); } if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR) { if (a.v.s == NULL || b.v.s == NULL) { *comparable = (a.v.s == b.v.s); return 0; } *comparable = TRUE; return strcmp(a.v.s, b.v.s); } if (a.tag == SP_TAG_SYM && b.tag == SP_TAG_SYM) { *comparable = TRUE; return (a.v.i > b.v.i) - (a.v.i < b.v.i); } *comparable = FALSE; return 0; }25422560
static mrb_bool sp_poly_lt(sp_RbVal a, sp_RbVal b) { mrb_bool comparable; mrb_int cmp = sp_poly_cmp(a, b, &comparable); return comparable ? (cmp < 0) : FALSE; }25432561
static mrb_bool sp_poly_le(sp_RbVal a, sp_RbVal b) { mrb_bool comparable; mrb_int cmp = sp_poly_cmp(a, b, &comparable); return comparable ? (cmp <= 0) : FALSE; }@@ -2705,6 +2723,7 @@
27052723
case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false");27062724
case SP_TAG_NIL: return SPL("nil");27072725
case SP_TAG_SYM: return sp_str_concat(SPL(":"), sp_sym_to_s((sp_sym)v.v.i));2726+
case SP_TAG_ENCODING: return sp_sprintf("#<Encoding:%s>", v.v.s ? v.v.s : "");27082727
case SP_TAG_OBJ:27092728
/* Built-in container / value-type tags get their typed inspect27102729
helper. Matches the dispatch shape in sp_poly_to_s above and the@@ -2837,6 +2856,8 @@
28372856
return (mrb_int)v.v.i;28382857
case SP_TAG_STR:28392858
return v.v.s ? (mrb_int)sp_str_hash(v.v.s) : 0;2859+
case SP_TAG_ENCODING:2860+
return v.v.s ? (mrb_int)sp_str_hash(v.v.s) : 0;28402861
case SP_TAG_FLT: { uint64_t b; memcpy(&b, &v.v.f, sizeof(b)); return (mrb_int)b; }28412862
case SP_TAG_OBJ:28422863
if (sp_obj_hash_hook) return sp_obj_hash_hook(v.cls_id, v.v.p);@@ -2853,6 +2874,10 @@
28532874
if (a.v.s == b.v.s) return TRUE;28542875
if (!a.v.s || !b.v.s) return FALSE;28552876
return strcmp(a.v.s, b.v.s) == 0;2877+
case SP_TAG_ENCODING:2878+
if (a.v.s == b.v.s) return TRUE;2879+
if (!a.v.s || !b.v.s) return FALSE;2880+
return strcmp(a.v.s, b.v.s) == 0;28562881
case SP_TAG_FLT:28572882
return a.v.f == b.v.f;28582883
case SP_TAG_OBJ:spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/bundle_sym.rbmodifiedView file
Loading diff…
test/source_encoding.rbaddedView file
Loading diff…
test/source_encoding.rb.expectedaddedView file
Loading diff…
test/string_encoding.rbmodifiedView file
Loading diff…
test/string_encoding.rb.expectedmodifiedView file
Loading diff…