1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Implemented shareable_constant_value

It does shallow freeze only for now.
This commit is contained in:
Nobuyoshi Nakada 2020-10-09 23:06:13 +09:00
parent b1bd223085
commit f43c71abe0
Notes: git 2020-12-14 19:19:48 +09:00
3 changed files with 36 additions and 1 deletions

16
parse.y
View file

@ -10925,19 +10925,33 @@ mark_lvar_used(struct parser_params *p, NODE *rhs)
}
}
static NODE *
shareable_constant_value(struct parser_params *p, NODE *value, const YYLTYPE *loc)
{
if (p->ctxt.shareable_constant_value) {
NODE *ractor = NEW_COLON3(rb_intern("Ractor"), loc);
value = NEW_CALL(ractor, rb_intern("make_shareable"),
NEW_LIST(value, loc), loc);
}
return value;
}
static NODE *
node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
{
if (!lhs) return 0;
switch (nd_type(lhs)) {
case NODE_CDECL:
rhs = shareable_constant_value(p, rhs, loc);
/* fallthru */
case NODE_GASGN:
case NODE_IASGN:
case NODE_LASGN:
case NODE_DASGN:
case NODE_DASGN_CURR:
case NODE_MASGN:
case NODE_CDECL:
case NODE_CVASGN:
lhs->nd_value = rhs;
nd_set_loc(lhs, loc);

View file

@ -51,6 +51,10 @@ class Ractor
}
end
def self.make_shareable(obj)
obj.freeze
end
# Multiplex multiple Ractor communications.
#
# r, obj = Ractor.select(r1, r2)

View file

@ -1178,6 +1178,23 @@ x = __ENCODING__
assert_warning(/invalid value/) do
assert_valid_syntax("# shareable_constant_value: invalid-option", verbose: true)
end
a = Class.new.class_eval("#{<<~"begin;"}\n#{<<~'end;'}")
begin;
# shareable_constant_value: true
A = []
end;
assert_predicate(a, :frozen?)
a, b = Class.new.class_eval("#{<<~"begin;"}\n#{<<~'end;'}")
begin;
# shareable_constant_value: false
class X # shareable_constant_value: true
A = []
end
B = []
[X::A, B]
end;
assert_predicate(a, :frozen?)
assert_not_predicate(b, :frozen?)
end
=begin