Commit 2775cd55
2775cd558d53Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #390 from OriPekelman/fix/env-fetch-and-module-cvars
Two small codegen additions: ENV.fetch(k,d) + module-scope @@cvars
Details
- Commit
- 2775cd558d53
- Parent
- 792c081aa546
- Merge parent
- 92cd8d792219
spinel_codegen.rbmodifiedView file
@@ -2539,6 +2539,16 @@
25392539
end25402540
return "int"25412541
end2542+
if t == "ClassVariableWriteNode"2543+
# `@@x = expr` as an expression returns the assigned value, so2544+
# the static type is the rhs's type. Without this case the2545+
# caller's `infer_type` falls through to the default (int) and2546+
# surrounding code -- e.g. a method whose body's last2547+
# expression is `@@x = v` -- gets `mrb_int` as the inferred2548+
# return, producing a const char* / mrb_int mismatch when v2549+
# is a string.2550+
return infer_type(@nd_expression[nid])2551+
end25422552
if t == "ConstantReadNode"25432553
if @nd_name[nid] == "ARGV"25442554
return "argv"@@ -4790,6 +4800,9 @@
47904800
if mname == "[]"47914801
return "string"47924802
end4803+
if mname == "fetch"4804+
return "string"4805+
end47934806
end47944807
if rcname == "Dir"47954808
if mname == "home"@@ -7016,29 +7029,23 @@
70167029
collect_class_with_prefix(nid, "")70177030
end70187031
7019-
# Walk every class body for class-var writes; register each7020-
# (class, name) pair so the static-declaration pass can emit7021-
# `static <type> cvar_<qname> = <default>;` ahead of the7022-
# functions that touch it.7032+
# Walk class bodies, module bodies, and the top-level statement7033+
# list for class-var writes; register each (class-or-Toplevel,7034+
# name) pair so the static-declaration pass can emit7035+
# `static <type> cvar_<qname> = <default>;` ahead of the functions7036+
# that touch it.7037+
#7038+
# Module-scope and top-level `@@x = ...` writes belong to the7039+
# `Toplevel` namespace -- spinel models cvars per-class, modules7040+
# don't have a cls_id of their own, and tying them to "Toplevel"7041+
# matches what the read/write codegen already emits when7042+
# `@current_class_idx == -1`.70237043
def collect_cvars70247044
root = @root_id70257045
if @nd_type[root] != "ProgramNode"70267046
return70277047
end7028-
stmts = get_body_stmts(root)7029-
stmts.each { |sid|7030-
if @nd_type[sid] == "ClassNode"7031-
cp = @nd_constant_path[sid]7032-
if cp >= 07033-
cname = const_ref_flat_name(cp)7034-
ci = find_class_idx(cname)7035-
if ci >= 07036-
body_id = @nd_body[sid]7037-
collect_cvars_in(body_id, ci)7038-
end7039-
end7040-
end7041-
}7048+
collect_cvars_in(root, -1)70427049
end70437050
70447051
# Recursively scan `nid`'s subtree for ClassVariable*WriteNode and@@ -7047,11 +7054,41 @@
70477054
# registered when their parent ClassVariableWriteNode is seen, OR70487055
# lazily during compile_stmt if no plain Write precedes them in70497056
# the same class.7057+
#7058+
# When the walk crosses into a nested ClassNode or ModuleNode the7059+
# context flips: nested ClassNode switches to that class's7060+
# @cls_names index, ModuleNode keeps the Toplevel namespace.70507061
def collect_cvars_in(nid, class_idx)70517062
if nid < 070527063
return70537064
end70547065
t = @nd_type[nid]7066+
if t == "ClassNode"7067+
cp = @nd_constant_path[nid]7068+
if cp >= 07069+
cname = const_ref_flat_name(cp)7070+
ci = find_class_idx(cname)7071+
if ci >= 07072+
collect_cvars_in(@nd_body[nid], ci)7073+
end7074+
end7075+
return7076+
end7077+
if t == "ModuleNode"7078+
collect_cvars_in(@nd_body[nid], -1)7079+
return7080+
end7081+
if t == "DefNode"7082+
# Method bodies aren't walked at collect-time. A `@@x = v`7083+
# inside a method writes during the call, but `v`'s type isn't7084+
# yet resolved (LocalVariableReadNode falls back to "int" when7085+
# the var-type table hasn't been populated), which would7086+
# spuriously widen a class-body literal's `string`/`float`7087+
# initialization to poly. The method-body write is registered7088+
# defensively at compile_stmt time anyway, when v's call-site-7089+
# resolved type is known.7090+
return7091+
end70557092
if t == "ClassVariableWriteNode"70567093
qname = cvar_qname(class_idx, @nd_name[nid])70577094
val_t = infer_type(@nd_expression[nid])@@ -27900,6 +27937,29 @@
2790027937
if mname == "[]"2790127938
return "sp_str_dup_external(getenv(" + compile_arg0(nid) + "))"2790227939
end27940+
if mname == "fetch"27941+
# Two-arg form: ENV.fetch(key, default). If the env var is27942+
# set, return its value; otherwise return the supplied27943+
# default. The single-arg form (`ENV.fetch("X")`) raises27944+
# KeyError in CRuby; spinel doesn't model that, so it27945+
# behaves like ENV[] -- callers wanting strict behaviour27946+
# should pass an explicit default.27947+
args_id = @nd_arguments[nid]27948+
if args_id >= 027949+
argz = get_args(args_id)27950+
if argz.length >= 227951+
key = compile_expr(argz[0])27952+
dfl = compile_expr_as_string(argz[1])27953+
tmp = new_temp27954+
return "({ const char *" + tmp + " = getenv(" + key + "); " +27955+
tmp + " ? sp_str_dup_external(" + tmp + ") : (" + dfl + "); })"27956+
end27957+
if argz.length == 127958+
return "sp_str_dup_external(getenv(" + compile_arg0(nid) + "))"27959+
end27960+
end27961+
return "(&(\"\\xff\")[1])"27962+
end2790327963
end2790427964
# Dir2790527965
if rcname == "Dir"test/env_fetch.rbaddedView file
Loading diff…
test/env_fetch.rb.expectedaddedView file
Loading diff…
test/module_cvars.rbaddedView file
Loading diff…
test/module_cvars.rb.expectedaddedView file
Loading diff…