Freeze strings from `nil.to_s`, `true.to_s`, `false.to_s`.

This is an experimental changes in Ruby 2.7.
parent 08eafe21
...@@ -1022,6 +1022,12 @@ MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*); ...@@ -1022,6 +1022,12 @@ MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, size_t len); MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, size_t len);
#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit)) #define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
MRB_API mrb_value mrb_obj_freeze(mrb_state*, mrb_value);
#define mrb_str_new_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new(mrb,p,len))
#define mrb_str_new_cstr_frozen(mrb,p) mrb_obj_freeze(mrb,mrb_str_new_cstr(mrb,p))
#define mrb_str_new_static_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new_static(mrb,p,len))
#define mrb_str_new_lit_frozen(mrb,lit) mrb_obj_freeze(mrb,mrb_str_new_lit(mrb,lit))
#ifdef _WIN32 #ifdef _WIN32
MRB_API char* mrb_utf8_from_locale(const char *p, int len); MRB_API char* mrb_utf8_from_locale(const char *p, int len);
MRB_API char* mrb_locale_from_utf8(const char *p, int len); MRB_API char* mrb_locale_from_utf8(const char *p, int len);
......
...@@ -431,7 +431,7 @@ mrb_obj_extend_m(mrb_state *mrb, mrb_value self) ...@@ -431,7 +431,7 @@ mrb_obj_extend_m(mrb_state *mrb, mrb_value self)
return mrb_obj_extend(mrb, argc, argv, self); return mrb_obj_extend(mrb, argc, argv, self);
} }
static mrb_value MRB_API mrb_value
mrb_obj_freeze(mrb_state *mrb, mrb_value self) mrb_obj_freeze(mrb_state *mrb, mrb_value self)
{ {
if (!mrb_immediate_p(self)) { if (!mrb_immediate_p(self)) {
......
...@@ -83,13 +83,13 @@ mrb_true(mrb_state *mrb, mrb_value obj) ...@@ -83,13 +83,13 @@ mrb_true(mrb_state *mrb, mrb_value obj)
static mrb_value static mrb_value
nil_to_s(mrb_state *mrb, mrb_value obj) nil_to_s(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new(mrb, 0, 0); return mrb_str_new_frozen(mrb, 0, 0);
} }
static mrb_value static mrb_value
nil_inspect(mrb_state *mrb, mrb_value obj) nil_inspect(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_lit(mrb, "nil"); return mrb_str_new_lit_frozen(mrb, "nil");
} }
/*********************************************************************** /***********************************************************************
...@@ -150,7 +150,7 @@ true_xor(mrb_state *mrb, mrb_value obj) ...@@ -150,7 +150,7 @@ true_xor(mrb_state *mrb, mrb_value obj)
static mrb_value static mrb_value
true_to_s(mrb_state *mrb, mrb_value obj) true_to_s(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_lit(mrb, "true"); return mrb_str_new_lit_frozen(mrb, "true");
} }
/* 15.2.5.3.4 */ /* 15.2.5.3.4 */
...@@ -257,7 +257,7 @@ false_or(mrb_state *mrb, mrb_value obj) ...@@ -257,7 +257,7 @@ false_or(mrb_state *mrb, mrb_value obj)
static mrb_value static mrb_value
false_to_s(mrb_state *mrb, mrb_value obj) false_to_s(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_lit(mrb, "false"); return mrb_str_new_lit_frozen(mrb, "false");
} }
void void
......
...@@ -235,6 +235,13 @@ mrb_str_new_static(mrb_state *mrb, const char *p, size_t len) ...@@ -235,6 +235,13 @@ mrb_str_new_static(mrb_state *mrb, const char *p, size_t len)
return mrb_obj_value(s); return mrb_obj_value(s);
} }
MRB_API mrb_value
mrb_str_freeze(mrb_state *mrb, const char *p, size_t len)
{
struct RString *s = str_new_static(mrb, p, len);
return mrb_obj_value(s);
}
static void static void
str_decref(mrb_state *mrb, mrb_shared_string *shared) str_decref(mrb_state *mrb, mrb_shared_string *shared)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment