Freeze strings from `nil.to_s`, `true.to_s`, `false.to_s`.
This is an experimental changes in Ruby 2.7.
MODIFY
include/mruby.h
include/mruby.h
@@ -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);
#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
MRB_API char* mrb_utf8_from_locale(const char *p, int len);
MRB_API char* mrb_locale_from_utf8(const char *p, int len);

MODIFY
src/kernel.c
src/kernel.c
@@ -431,7 +431,7 @@ mrb_obj_extend_m(mrb_state *mrb, mrb_value 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)
{
if (!mrb_immediate_p(self)) {

MODIFY
src/object.c
src/object.c
@@ -83,13 +83,13 @@ mrb_true(mrb_state *mrb, mrb_value obj)
static mrb_value
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
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)
static mrb_value
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 */
@@ -257,7 +257,7 @@ false_or(mrb_state *mrb, mrb_value obj)
static mrb_value
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

MODIFY
src/string.c
src/string.c
@@ -235,6 +235,13 @@ mrb_str_new_static(mrb_state *mrb, const char *p, size_t len)
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
str_decref(mrb_state *mrb, mrb_shared_string *shared)
{

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

This is an experimental changes in Ruby 2.7.