Commit 9ffad2a9
9ffad2a90bcdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #272 from ryanseys/rs-node-global-var-or-write
feat(parse,codegen): support GlobalVariableOrWriteNode ($x ||= val)
Details
- Commit
- 9ffad2a90bcd
- Parent
- 887d2bcb138e
- Merge parent
- 135e1a3a3938
spinel_codegen.rbmodifiedView file
@@ -24744,6 +24744,20 @@
2474424744
end2474524745
return2474624746
end24747+
if t == "GlobalVariableOrWriteNode"24748+
# `$x ||= val`. Short-circuit: only evaluate RHS when LHS is24749+
# falsy. compile_expr can emit prerequisite statements; running24750+
# it inside the C `if` block ensures those side effects only24751+
# fire on the assign branch. Note: in Ruby, an undefined $-var24752+
# reads as nil, which is falsy in C terms, so the `if (!cname)`24753+
# guard fires both for never-assigned and assigned-to-falsy.24754+
cname = sanitize_gvar(@nd_name[nid])24755+
emit(" if (!(" + cname + ")) {")24756+
val = compile_expr(@nd_expression[nid])24757+
emit(" " + cname + " = " + val + ";")24758+
emit(" }")24759+
return24760+
end2474724761
if t == "LocalVariableWriteNode"2474824762
lname = @nd_name[nid]2474924763
# Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/global_var_or_write.rbaddedView file
Loading diff…
test/global_var_or_write.rb.expectedaddedView file
Loading diff…