Commit e3d9ebf8
e3d9ebf8ae06Merged
NHH <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix: h[k] &&=/||= on int-valued hashes uses has_key, not _get return
C treats 0 as falsy but Ruby treats 0 as truthy, so using sp_*IntHash_get as the boolean guard for &&= and ||= was wrong:
h = {"a" => 0}; h["a"] &&= 99 => kept 0 (C: if(0) skips) h = {"a" => 0}; h["a"] ||= 99 => wrote 99 (C: if(!0) fires)
CRuby: &&= updates to 99 (0 is truthy), ||= keeps 0 (0 is truthy).
Switch int-valued hash guards to _has_key() which checks existence rather than value. For int/float arrays, use bounds check instead of _get since all in-bounds values are truthy in Ruby.
Details
- Commit
- e3d9ebf8ae06
- Parent
- 7780fff2c6e8
spinel_codegen.rbmodifiedView file
@@ -38272,7 +38272,7 @@
3827238272
tt = new_temp3827338273
ti = new_temp3827438274
emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx_int_ia + ";")38275-
emit(" if (sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")38275+
emit(" if (" + ti + " >= 0 && " + ti + " < " + tt + "->len) {")3827638276
val = compile_expr(@nd_expression[nid])3827738277
emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")3827838278
emit(" }")@@ -38284,7 +38284,7 @@
3828438284
ti = new_temp3828538285
idx_s = compile_expr_as_string(arg_ids[0])3828638286
emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")38287-
emit(" if (sp_StrIntHash_get(" + tt + ", " + ti + ")) {")38287+
emit(" if (sp_StrIntHash_has_key(" + tt + ", " + ti + ")) {")3828838288
val = compile_expr(@nd_expression[nid])3828938289
emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")3829038290
emit(" }")@@ -38295,7 +38295,7 @@
3829538295
tt = new_temp3829638296
ti = new_temp3829738297
emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")38298-
emit(" if (sp_IntStrHash_get(" + tt + ", " + ti + ")) {")38298+
emit(" if (sp_IntStrHash_has_key(" + tt + ", " + ti + ")) {")3829938299
val = compile_expr(@nd_expression[nid])3830038300
emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")3830138301
emit(" }")@@ -38306,7 +38306,7 @@
3830638306
tt = new_temp3830738307
ti = new_temp3830838308
emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")38309-
emit(" if (sp_SymIntHash_get(" + tt + ", " + ti + ")) {")38309+
emit(" if (sp_SymIntHash_has_key(" + tt + ", " + ti + ")) {")3831038310
val = compile_expr(@nd_expression[nid])3831138311
emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")3831238312
emit(" }")@@ -38342,7 +38342,7 @@
3834238342
tt = new_temp3834338343
ti = new_temp3834438344
emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx_int_or + ";")38345-
emit(" if (!sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")38345+
emit(" if (" + ti + " < 0 || " + ti + " >= " + tt + "->len) {")3834638346
val = compile_expr(@nd_expression[nid])3834738347
emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")3834838348
emit(" }")@@ -38354,7 +38354,7 @@
3835438354
ti = new_temp3835538355
idx_s = compile_expr_as_string(arg_ids[0])3835638356
emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")38357-
emit(" if (!sp_StrIntHash_get(" + tt + ", " + ti + ")) {")38357+
emit(" if (!sp_StrIntHash_has_key(" + tt + ", " + ti + ")) {")3835838358
val = compile_expr(@nd_expression[nid])3835938359
emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")3836038360
emit(" }")@@ -38365,7 +38365,7 @@
3836538365
tt = new_temp3836638366
ti = new_temp3836738367
emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")38368-
emit(" if (!sp_IntStrHash_get(" + tt + ", " + ti + ")) {")38368+
emit(" if (!sp_IntStrHash_has_key(" + tt + ", " + ti + ")) {")3836938369
val = compile_expr(@nd_expression[nid])3837038370
emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")3837138371
emit(" }")@@ -38376,7 +38376,7 @@
3837638376
tt = new_temp3837738377
ti = new_temp3837838378
emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")38379-
emit(" if (!sp_SymIntHash_get(" + tt + ", " + ti + ")) {")38379+
emit(" if (!sp_SymIntHash_has_key(" + tt + ", " + ti + ")) {")3838038380
val = compile_expr(@nd_expression[nid])3838138381
emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")3838238382
emit(" }")test/hash_int_and_or_assign.rbaddedView file
Loading diff…
test/hash_int_and_or_assign.rb.expectedaddedView file
Loading diff…