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 end
25402540 return "int"
25412541 end
2542+ if t == "ClassVariableWriteNode"
2543+ # `@@x = expr` as an expression returns the assigned value, so
2544+ # the static type is the rhs's type. Without this case the
2545+ # caller's `infer_type` falls through to the default (int) and
2546+ # surrounding code -- e.g. a method whose body's last
2547+ # expression is `@@x = v` -- gets `mrb_int` as the inferred
2548+ # return, producing a const char* / mrb_int mismatch when v
2549+ # is a string.
2550+ return infer_type(@nd_expression[nid])
2551+ end
25422552 if t == "ConstantReadNode"
25432553 if @nd_name[nid] == "ARGV"
25442554 return "argv"
@@ -4790,6 +4800,9 @@
47904800 if mname == "[]"
47914801 return "string"
47924802 end
4803+ if mname == "fetch"
4804+ return "string"
4805+ end
47934806 end
47944807 if rcname == "Dir"
47954808 if mname == "home"
@@ -7016,29 +7029,23 @@
70167029 collect_class_with_prefix(nid, "")
70177030 end
70187031
7019- # Walk every class body for class-var writes; register each
7020- # (class, name) pair so the static-declaration pass can emit
7021- # `static <type> cvar_<qname> = <default>;` ahead of the
7022- # functions that touch it.
7032+ # Walk class bodies, module bodies, and the top-level statement
7033+ # list for class-var writes; register each (class-or-Toplevel,
7034+ # name) pair so the static-declaration pass can emit
7035+ # `static <type> cvar_<qname> = <default>;` ahead of the functions
7036+ # that touch it.
7037+ #
7038+ # Module-scope and top-level `@@x = ...` writes belong to the
7039+ # `Toplevel` namespace -- spinel models cvars per-class, modules
7040+ # don't have a cls_id of their own, and tying them to "Toplevel"
7041+ # matches what the read/write codegen already emits when
7042+ # `@current_class_idx == -1`.
70237043 def collect_cvars
70247044 root = @root_id
70257045 if @nd_type[root] != "ProgramNode"
70267046 return
70277047 end
7028- stmts = get_body_stmts(root)
7029- stmts.each { |sid|
7030- if @nd_type[sid] == "ClassNode"
7031- cp = @nd_constant_path[sid]
7032- if cp >= 0
7033- cname = const_ref_flat_name(cp)
7034- ci = find_class_idx(cname)
7035- if ci >= 0
7036- body_id = @nd_body[sid]
7037- collect_cvars_in(body_id, ci)
7038- end
7039- end
7040- end
7041- }
7048+ collect_cvars_in(root, -1)
70427049 end
70437050
70447051 # Recursively scan `nid`'s subtree for ClassVariable*WriteNode and
@@ -7047,11 +7054,41 @@
70477054 # registered when their parent ClassVariableWriteNode is seen, OR
70487055 # lazily during compile_stmt if no plain Write precedes them in
70497056 # the same class.
7057+ #
7058+ # When the walk crosses into a nested ClassNode or ModuleNode the
7059+ # context flips: nested ClassNode switches to that class's
7060+ # @cls_names index, ModuleNode keeps the Toplevel namespace.
70507061 def collect_cvars_in(nid, class_idx)
70517062 if nid < 0
70527063 return
70537064 end
70547065 t = @nd_type[nid]
7066+ if t == "ClassNode"
7067+ cp = @nd_constant_path[nid]
7068+ if cp >= 0
7069+ cname = const_ref_flat_name(cp)
7070+ ci = find_class_idx(cname)
7071+ if ci >= 0
7072+ collect_cvars_in(@nd_body[nid], ci)
7073+ end
7074+ end
7075+ return
7076+ end
7077+ if t == "ModuleNode"
7078+ collect_cvars_in(@nd_body[nid], -1)
7079+ return
7080+ end
7081+ 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't
7084+ # yet resolved (LocalVariableReadNode falls back to "int" when
7085+ # the var-type table hasn't been populated), which would
7086+ # spuriously widen a class-body literal's `string`/`float`
7087+ # initialization to poly. The method-body write is registered
7088+ # defensively at compile_stmt time anyway, when v's call-site-
7089+ # resolved type is known.
7090+ return
7091+ end
70557092 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 end
27940+ if mname == "fetch"
27941+ # Two-arg form: ENV.fetch(key, default). If the env var is
27942+ # set, return its value; otherwise return the supplied
27943+ # default. The single-arg form (`ENV.fetch("X")`) raises
27944+ # KeyError in CRuby; spinel doesn't model that, so it
27945+ # behaves like ENV[] -- callers wanting strict behaviour
27946+ # should pass an explicit default.
27947+ args_id = @nd_arguments[nid]
27948+ if args_id >= 0
27949+ argz = get_args(args_id)
27950+ if argz.length >= 2
27951+ key = compile_expr(argz[0])
27952+ dfl = compile_expr_as_string(argz[1])
27953+ tmp = new_temp
27954+ return "({ const char *" + tmp + " = getenv(" + key + "); " +
27955+ tmp + " ? sp_str_dup_external(" + tmp + ") : (" + dfl + "); })"
27956+ end
27957+ if argz.length == 1
27958+ return "sp_str_dup_external(getenv(" + compile_arg0(nid) + "))"
27959+ end
27960+ end
27961+ return "(&(\"\\xff\")[1])"
27962+ end
2790327963 end
2790427964 # Dir
2790527965 if rcname == "Dir"