ruby-changes:64155
From: Nobuyoshi <ko1@a...>
Date: Mon, 14 Dec 2020 19:29:53 +0900 (JST)
Subject: [ruby-changes:64155] f43c71abe0 (master): Implemented shareable_constant_value
https://git.ruby-lang.org/ruby.git/commit/?id=f43c71abe0 From f43c71abe07b4b2b0f8f9a69c567fcd0f38faef6 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 9 Oct 2020 23:06:13 +0900 Subject: Implemented shareable_constant_value It does shallow freeze only for now. diff --git a/parse.y b/parse.y index 3c9b9e0..da66c56 100644 --- a/parse.y +++ b/parse.y @@ -10926,18 +10926,32 @@ mark_lvar_used(struct parser_params *p, NODE *rhs) https://github.com/ruby/ruby/blob/trunk/parse.y#L10926 } 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); diff --git a/ractor.rb b/ractor.rb index ca09aee..0867f1a 100644 --- a/ractor.rb +++ b/ractor.rb @@ -51,6 +51,10 @@ class Ractor https://github.com/ruby/ruby/blob/trunk/ractor.rb#L51 } end + def self.make_shareable(obj) + obj.freeze + end + # Multiplex multiple Ractor communications. # # r, obj = Ractor.select(r1, r2) diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 1ed92d5..f12b610 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -1178,6 +1178,23 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L1178 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 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/