Commit 10f0b860
10f0b8606aa3Merged
ydah <[email protected]> · 6 months ago · 9 files · +2 added · ~7 modified
Description
parse,runtime,analyze,codegen: ENCODING returns Encoding
Details
- Commit
- 10f0b8606aa3
- Parent
- e7b577b37307
lib/sp_runtime.hmodifiedView file
@@ -508,6 +508,18 @@
508508
}509509
510510
static void sp_oom_die(void);511+
512+
/* ---- Encoding runtime ----513+
Spinel currently assumes UTF-8 source and string data. Keep Encoding514+
as a tiny value type so `__ENCODING__` and `String#encoding` can515+
answer Ruby's Encoding-shaped protocol without carrying full516+
transcoding state. */517+
typedef struct{const char *name;}sp_Encoding;518+
static inline sp_Encoding sp_encoding_utf8(void){return(sp_Encoding){"UTF-8"};}519+
static inline const char*sp_encoding_name(sp_Encoding e){return e.name?e.name:sp_str_empty;}520+
static inline const char*sp_encoding_inspect(sp_Encoding e){return sp_sprintf("#<Encoding:%s>",sp_encoding_name(e));}521+
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;}522+
511523
static char *sp_str_alloc(size_t len) {512524
size_t total = sizeof(sp_str_hdr) + 1 + len + 1;513525
sp_str_hdr *h = (sp_str_hdr *)malloc(total);@@ -1925,6 +1937,9 @@
19251937
the boxed sp_Class's cls_id directly so unboxing is just a19261938
field read. */19271939
#define SP_TAG_CLASS 71940+
/* Encoding values boxed into a poly slot. v.s carries the canonical1941+
encoding name (`"UTF-8"` in Spinel's current runtime model). */1942+
#define SP_TAG_ENCODING 819281943
/* Negative cls_id values let SP_TAG_OBJ also carry built-in pointer19291944
types (IntArray, FloatArray, ...) — avoids minting a new SP_TAG_*19301945
per type. Non-negative cls_id stays an index into the user-class@@ -1960,6 +1975,7 @@
19601975
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; }19611976
/* box a sp_Class into a poly slot. */19621977
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; }1978+
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; }19631979
19641980
/* every non-value-type sp_<C> starts19651981
with `mrb_int cls_id`. Read it back from a void* when@@ -2130,6 +2146,7 @@
21302146
case SP_TAG_BOOL: puts(v.v.b ? "true" : "false"); break;21312147
case SP_TAG_NIL: putchar('\n'); break;21322148
case SP_TAG_SYM: { const char *_ss = sp_sym_to_s((sp_sym)v.v.i); fputs(_ss, stdout); putchar('\n'); break; }2149+
case SP_TAG_ENCODING: { const char *_es = v.v.s ? v.v.s : sp_str_empty; fputs(_es, stdout); putchar('\n'); break; }21332150
case SP_TAG_OBJ: {21342151
/* MRI's `puts arr` iterates an Array, printing one element per21352152
line (using to_s on each); a non-Array OBJ falls back to@@ -2191,6 +2208,7 @@
21912208
case SP_TAG_NIL: return sp_str_empty;21922209
case SP_TAG_SYM: return sp_sym_to_s((sp_sym)v.v.i);21932210
case SP_TAG_CLASS: { sp_Class c = {v.v.i}; return sp_class_to_s(c); }2211+
case SP_TAG_ENCODING: return v.v.s ? v.v.s : sp_str_empty;21942212
case SP_TAG_OBJ:21952213
switch (v.cls_id) {21962214
case SP_BUILTIN_INT_ARRAY: return sp_IntArray_inspect((sp_IntArray *)v.v.p);@@ -2211,7 +2229,7 @@
22112229
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; }22122230
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; }22132231
static mrb_bool sp_poly_numeric_p(sp_RbVal v) { return v.tag == SP_TAG_INT || v.tag == SP_TAG_FLT; }2214-
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; } }2232+
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; } }22152233
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; }22162234
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; }22172235
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; }@@ -2332,6 +2350,7 @@
23322350
case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false");23332351
case SP_TAG_NIL: return SPL("nil");23342352
case SP_TAG_SYM: return sp_str_concat(SPL(":"), sp_sym_to_s((sp_sym)v.v.i));2353+
case SP_TAG_ENCODING: return sp_sprintf("#<Encoding:%s>", v.v.s ? v.v.s : "");23352354
case SP_TAG_OBJ:23362355
/* Built-in container / value-type tags get their typed inspect23372356
helper. Matches the dispatch shape in sp_poly_to_s above and the@@ -2459,6 +2478,8 @@
24592478
return (mrb_int)v.v.i;24602479
case SP_TAG_STR:24612480
return v.v.s ? (mrb_int)sp_str_hash(v.v.s) : 0;2481+
case SP_TAG_ENCODING:2482+
return v.v.s ? (mrb_int)sp_str_hash(v.v.s) : 0;24622483
case SP_TAG_FLT: { uint64_t b; memcpy(&b, &v.v.f, sizeof(b)); return (mrb_int)b; }24632484
case SP_TAG_OBJ:24642485
if (sp_obj_hash_hook) return sp_obj_hash_hook(v.cls_id, v.v.p);@@ -2475,6 +2496,10 @@
24752496
if (a.v.s == b.v.s) return TRUE;24762497
if (!a.v.s || !b.v.s) return FALSE;24772498
return strcmp(a.v.s, b.v.s) == 0;2499+
case SP_TAG_ENCODING:2500+
if (a.v.s == b.v.s) return TRUE;2501+
if (!a.v.s || !b.v.s) return FALSE;2502+
return strcmp(a.v.s, b.v.s) == 0;24782503
case SP_TAG_FLT:24792504
return a.v.f == b.v.f;24802505
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…