wardite 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +9 -1
  3. data/README.md +36 -4
  4. data/benchmark/README.md +8 -0
  5. data/benchmark/compare_mir_generation_builds.rb +66 -0
  6. data/benchmark/count_ruby_wasm_functions.rb +37 -0
  7. data/benchmark/generate_mir_generation_fixtures.rb +44 -0
  8. data/benchmark/measure_mir_cache_restore.rb +59 -0
  9. data/benchmark/measure_mir_generation.rb +52 -0
  10. data/benchmark/mir_copy_worker.rb +29 -0
  11. data/benchmark/mir_generation_worker.rb +50 -0
  12. data/benchmark/prepare_mir_generation_profile.rb +80 -0
  13. data/benchmark/profile_inline_snapshots.rb +63 -0
  14. data/benchmark/profile_mir_generation_allocations.rb +73 -0
  15. data/benchmark/results/efflorescence-mir2-function-restore.json +480 -0
  16. data/benchmark/results/mir-generation-adr0014.json +1779 -0
  17. data/benchmark/results/mir-generation-adr0015.json +3132 -0
  18. data/benchmark/results/mir-generation-allocations.json +236 -0
  19. data/benchmark/results/mir-generation-coarse.json +909 -0
  20. data/benchmark/results/mir-generation-control.json +463 -0
  21. data/benchmark/results/mir-generation-detail.json +387 -0
  22. data/benchmark/results/mir-generation-scaling.json +2945 -0
  23. data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +1 -1
  24. data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +1 -1
  25. data/docs/adr/0014-efflorescence-mir-build-validation-and-decoding.md +136 -0
  26. data/docs/adr/0015-efflorescence-selective-inline-snapshots.md +150 -0
  27. data/docs/adr/0016-efflorescence-generation-tagged-copy-propagation.md +163 -0
  28. data/docs/efflorescence-mir-build-validation-and-decoding-results.md +93 -0
  29. data/docs/efflorescence-mir-generation-implementation.md +77 -0
  30. data/docs/efflorescence-mir-generation-performance.md +184 -0
  31. data/docs/efflorescence-mir2-cache.md +31 -2
  32. data/docs/efflorescence-selective-inline-snapshots-results.md +92 -0
  33. data/docs/efflorescence-spinel-linear-memory.md +6 -4
  34. data/lib/wardite/cli/cli.rb +3 -1
  35. data/lib/wardite/efflorescence/ir.rb +131 -40
  36. data/lib/wardite/efflorescence/mir_cache.rb +165 -73
  37. data/lib/wardite/efflorescence/optimizer.rb +65 -15
  38. data/lib/wardite/efflorescence/register_validation.rb +162 -32
  39. data/lib/wardite/efflorescence/snapshot.rb +5 -2
  40. data/lib/wardite/efflorescence/vm.rb +4 -3
  41. data/lib/wardite/instruction.rb +42 -1
  42. data/lib/wardite/leb128.rb +2 -5
  43. data/lib/wardite/load.rb +154 -75
  44. data/lib/wardite/version.rb +1 -1
  45. data/lib/wardite.rb +1 -1
  46. data/scripts/build_spinel_zig.rb +121 -0
  47. data/sig/generated/wardite/efflorescence/ir.rbs +34 -3
  48. data/sig/generated/wardite/efflorescence/mir_cache.rbs +41 -14
  49. data/sig/generated/wardite/efflorescence/optimizer.rbs +27 -3
  50. data/sig/generated/wardite/efflorescence/register_validation.rbs +45 -4
  51. data/sig/generated/wardite/efflorescence/snapshot.rbs +2 -1
  52. data/sig/generated/wardite/instruction.rbs +23 -0
  53. data/sig/generated/wardite/load.rbs +63 -9
  54. data/sig/io_buffer.rbs +7 -0
  55. metadata +28 -3
  56. data/spinel/patches/0001-io-buffer-string-view.patch +0 -699
  57. data/spinel/patches/0002-io-buffer-allocation-error.patch +0 -47
@@ -1,699 +0,0 @@
1
- From 255955a06a0311df92f6b422c69d65ff2f39fe6e Mon Sep 17 00:00:00 2001
2
- From: Uchio Kondo <udzura@udzura.jp>
3
- Date: Thu, 12 Nov 2026 00:48:39 +0900
4
- Subject: [PATCH] Borrow String bytes in IO::Buffer.for blocks
5
-
6
- Lock mutable String storage during the yield and release it on unwind.
7
- Compare write-through, readonly views, and cleanup with CRuby.
8
-
9
- Co-authored-by: Codex <codex@openai.com>
10
- ---
11
- lib/sp_alloc.h | 9 ++++-
12
- lib/sp_cext_bridge.c | 2 +-
13
- lib/sp_iobuffer.c | 48 +++++++++++++++++++++-
14
- lib/sp_iobuffer.h | 6 ++-
15
- lib/sp_poly_cold.c | 2 +-
16
- lib/sp_str.c | 5 ++-
17
- lib/sp_string.c | 6 +--
18
- lib/sp_string.h | 17 ++++----
19
- lib/sp_types.h | 2 +-
20
- lib/spinel_rt.h | 19 +++++----
21
- packages/io/io/buffer.rb | 21 ++++++----
22
- src/codegen_call_io.c | 2 +-
23
- src/codegen_call_recv.c | 24 +++++------
24
- src/codegen_stmt.c | 10 ++---
25
- test/io_buffer_for_block.rb | 59 ++++++++++++++++++++++++++++
26
- test/io_buffer_for_block.rb.expected | 15 +++++++
27
- 16 files changed, 196 insertions(+), 51 deletions(-)
28
- create mode 100644 test/io_buffer_for_block.rb
29
- create mode 100644 test/io_buffer_for_block.rb.expected
30
-
31
- diff --git a/lib/sp_alloc.h b/lib/sp_alloc.h
32
- index fec103dbf..5e065e86a 100644
33
- --- a/lib/sp_alloc.h
34
- +++ b/lib/sp_alloc.h
35
- @@ -295,7 +295,7 @@ static SP_ALWAYS_INLINE inline char *sp_str_alloc_born(size_t len, int nogc) {
36
- char *body = (char *)(h + 1);
37
- body[0] = (char)0xfe;
38
- body[1 + len] = 0;
39
- - r->data = body + 1; r->len = (int64_t)len; r->cap = (int64_t)len; r->binary = 0;
40
- + r->data = body + 1; r->len = (int64_t)len; r->cap = (int64_t)len; r->binary = 0; r->temporarily_locked = 0;
41
- if (sp_alloc_report_on) sp_alloc_report_str(len);
42
- return body + 1;
43
- }
44
- @@ -1116,7 +1116,14 @@ static inline sp_bool sp_str_is_frozen_val(const char *s) {
45
- if (SP_UNLIKELY(m == 0xfd)) return sp_str_fd_owner_frozen(s);
46
- return TRUE;
47
- }
48
- +static inline sp_bool sp_str_is_locked(const char *s) {
49
- + if (!s || ((const unsigned char *)s)[-1] != 0xfd) return FALSE;
50
- + const struct sp_String_s *owner = (const void *)(((const sp_str_hdr *)(s - 1)) - 1)->next;
51
- + return owner && owner->temporarily_locked;
52
- +}
53
- +SP_NORETURN void sp_raise_locked_str(void);
54
- static inline void sp_str_check_mutable(const char *s) {
55
- + if (sp_str_is_locked(s)) sp_raise_locked_str();
56
- if (sp_str_is_frozen_val(s)) sp_raise_frozen_str(s);
57
- }
58
- /* A String a runtime object keeps past the call (a File's path, a
59
- diff --git a/lib/sp_cext_bridge.c b/lib/sp_cext_bridge.c
60
- index fd991c91b..b7221836a 100644
61
- --- a/lib/sp_cext_bridge.c
62
- +++ b/lib/sp_cext_bridge.c
63
- @@ -564,7 +564,7 @@ VALUE rb_str_export_to_enc(VALUE value,rb_encoding *to) { return string_in_encod
64
- static sp_String *string(VALUE value) {
65
- sp_RbVal r=sp_cext_rbval(value);
66
- if(r.tag==SP_TAG_OBJ && r.cls_id==SP_BUILTIN_STRBUF)return r.v.p;
67
- - if(r.tag==SP_TAG_STR && sp_str_is_frozen_val(r.v.s))sp_raise_frozen_str(r.v.s);
68
- + if(r.tag==SP_TAG_STR)sp_str_check_mutable(r.v.s);
69
- rb_raise(rb_eTypeError,"expected a shared String");
70
- }
71
- VALUE rb_str_freeze(VALUE v) {
72
- diff --git a/lib/sp_iobuffer.c b/lib/sp_iobuffer.c
73
- index 347030354..3cdf111aa 100644
74
- --- a/lib/sp_iobuffer.c
75
- +++ b/lib/sp_iobuffer.c
76
- @@ -9,6 +9,7 @@
77
- range plus Bignums up to the unsigned bound. Messages mirror CRuby's,
78
- including the "integer ... to" (fixnum) / "bignum ... into" wording. */
79
- #include "sp_iobuffer.h"
80
- +#include "sp_string.h"
81
- #include "sp_io.h" /* the IO integration: sp_File, the readiness parks */
82
- #include <stdlib.h>
83
- #include <string.h>
84
- @@ -53,8 +54,13 @@ static int iob_slice_invalid(sp_IOBuffer *b);
85
- static void iob_readonly(sp_IOBuffer *b) {
86
- if (b->flags & SP_IOB_READONLY) sp_raise_cls("IO::Buffer::AccessError", "Buffer is not writable!");
87
- }
88
- +static void iob_string_changed(sp_IOBuffer *b) {
89
- + if (b->source && b->source->borrowed_string)
90
- + sp_fd_publish(b->source->borrowed_string);
91
- +}
92
- static void iob_writable_slice(sp_IOBuffer *b) {
93
- if (iob_slice_invalid(b)) sp_raise_cls("IO::Buffer::InvalidatedError", "Buffer is invalid!");
94
- + iob_string_changed(b);
95
- }
96
- static void iob_writable(sp_IOBuffer *b) {
97
- iob_readonly(b);
98
- @@ -327,6 +333,7 @@ static sp_int iob_set_core(sp_IOBuffer *b, int ty, int64_t off, sp_RbVal val) {
99
- static void iob_scan(void *p) {
100
- sp_IOBuffer *b = (sp_IOBuffer *)p;
101
- if (b->source) sp_gc_mark(b->source);
102
- + if (b->borrowed_string) sp_gc_mark(b->borrowed_string);
103
- }
104
- /* The bytes this buffer's own malloc holds: a slice's data is NULL, a
105
- mapping is the file's pages, not the heap's. They are counted to the
106
- @@ -334,13 +341,13 @@ static void iob_scan(void *p) {
107
- a buffer weighed its header alone, so a program churning large buffers
108
- grew by gigabytes without a collection. */
109
- static size_t iob_owned(sp_IOBuffer *b) {
110
- - return b->data && !b->map_base ? (size_t)b->size : 0;
111
- + return b->data && !b->map_base && !b->borrowed_string ? (size_t)b->size : 0;
112
- }
113
- /* Give the allocation back: munmap for a mapped file, free otherwise. */
114
- static void iob_release(sp_IOBuffer *b) {
115
- sp_gc_payload_resize(b, iob_owned(b), 0);
116
- if (b->map_base) { munmap(b->map_base, b->map_len); b->map_base = NULL; b->map_len = 0; }
117
- - else free(b->data);
118
- + else if (!b->borrowed_string) free(b->data);
119
- b->data = NULL;
120
- }
121
- void sp_IOBuffer_fin(void *p) {
122
- @@ -406,6 +413,42 @@ sp_IOBuffer *sp_IOBuffer_become_for(sp_IOBuffer *b, const char *s) {
123
- return b;
124
- }
125
-
126
- +/* The private owner lives through the entire block, even if the yielded
127
- + buffer is freed or transferred. Derived views resolve through it and
128
- + become invalid when the block ends. */
129
- +sp_IOBuffer *sp_IOBuffer_borrow_string(sp_IOBuffer *owner, const char *s) {
130
- + SP_GC_ROOT(owner);
131
- + SP_GC_ROOT_STR(s);
132
- + if (owner->data || owner->source) iob_arg("Borrow owner is already in use");
133
- + sp_String *string = sp_String_new_shared(s);
134
- + SP_GC_ROOT(string);
135
- + if (string->temporarily_locked) sp_raise_locked_str();
136
- + sp_IOBuffer *view = iob_alloc_obj(owner->cls_id);
137
- + const int readonly = sp_String_is_frozen(string);
138
- + if (!readonly) string->temporarily_locked = 1;
139
- + sp_gc_wb(owner);
140
- + owner->borrowed_string = string;
141
- + owner->data = (uint8_t *)string->data;
142
- + owner->size = string->len;
143
- + view->source = owner;
144
- + view->size = owner->size;
145
- + view->flags = SP_IOB_EXTERNAL | (readonly ? SP_IOB_READONLY : 0);
146
- + return view;
147
- +}
148
- +
149
- +sp_IOBuffer *sp_IOBuffer_end_borrow(sp_IOBuffer *owner) {
150
- + sp_String *string = owner->borrowed_string;
151
- + if (!string) return owner;
152
- + if (string->temporarily_locked) {
153
- + sp_fd_publish(string);
154
- + string->temporarily_locked = 0;
155
- + }
156
- + owner->borrowed_string = NULL;
157
- + owner->data = NULL;
158
- + owner->size = 0;
159
- + return owner;
160
- +}
161
- +
162
- /* ---- generic + typed accessors ---- */
163
-
164
- sp_RbVal sp_IOBuffer_get_value(sp_IOBuffer *b, sp_RbVal type, sp_int off) {
165
- @@ -991,6 +1034,7 @@ static uint8_t *iob_io_span(sp_IOBuffer *b, sp_int *length, sp_int offset, int w
166
- *length = b->size - offset;
167
- if (writing && (b->flags & SP_IOB_READONLY)) sp_raise_cls("IO::Buffer::AccessError", "Buffer is not writable!");
168
- if (!base) sp_raise_cls("IO::Buffer::AccessError", "Buffer is not allocated!");
169
- + if (writing) iob_string_changed(b);
170
- return base + offset;
171
- }
172
- sp_int sp_IOBuffer_read_io(sp_IOBuffer *b, sp_RbVal io, sp_int length, sp_int offset) {
173
- diff --git a/lib/sp_iobuffer.h b/lib/sp_iobuffer.h
174
- index ff795f261..120bc96a8 100644
175
- --- a/lib/sp_iobuffer.h
176
- +++ b/lib/sp_iobuffer.h
177
- @@ -40,6 +40,7 @@ struct sp_IOBuffer_s {
178
- uint32_t flags;
179
- void *map_base; /* IO::Buffer.map: the mmap'd region `data` points into
180
- (page-aligned), NULL for a malloc'd allocation */
181
- + struct sp_String_s *borrowed_string; /* kept and unlocked by the block owner */
182
- size_t map_len; /* ...and its length, for munmap */
183
- };
184
-
185
- @@ -204,7 +205,8 @@ static inline sp_int sp_iob_put_f(uint8_t *d, sp_int wl, sp_int ty, sp_int off,
186
- /* how many of a view's len bytes a write may store into: none of a
187
- read-only buffer's (a slice carries its root's READONLY) */
188
- static inline sp_int sp_iob_view_wlen(sp_IOBuffer *b, sp_int len) {
189
- - return b && !(b->flags & SP_IOB_READONLY) ? len : 0;
190
- + return b && !(b->flags & SP_IOB_READONLY) &&
191
- + !(b->source && b->source->borrowed_string) ? len : 0;
192
- }
193
- /* an owned buffer's bytes: a slice's data is NULL (see the struct), so a
194
- slice takes the out-of-line write */
195
- @@ -244,6 +246,8 @@ sp_IOBuffer *sp_IOBuffer_transfer(sp_IOBuffer *b);
196
- sp_IOBuffer *sp_IOBuffer_free_m(sp_IOBuffer *b);
197
- sp_IOBuffer *sp_IOBuffer_dup_m(sp_IOBuffer *b);
198
- sp_IOBuffer *sp_IOBuffer_become_for(sp_IOBuffer *b, const char *s);
199
- +sp_IOBuffer *sp_IOBuffer_borrow_string(sp_IOBuffer *owner, const char *s);
200
- +sp_IOBuffer *sp_IOBuffer_end_borrow(sp_IOBuffer *owner);
201
-
202
- /* comparison */
203
- sp_int sp_IOBuffer_cmp(sp_IOBuffer *b, sp_RbVal other);
204
- diff --git a/lib/sp_poly_cold.c b/lib/sp_poly_cold.c
205
- index 7126be57c..f0764c6c2 100644
206
- --- a/lib/sp_poly_cold.c
207
- +++ b/lib/sp_poly_cold.c
208
- @@ -833,7 +833,7 @@ sp_RbVal sp_poly_clear(sp_RbVal v)
209
- case SP_BUILTIN_PTR_ARRAY: ((sp_PtrArray *)v.v.p)->len = 0; break;
210
- case SP_BUILTIN_STRBUF: {
211
- sp_String *_m = (sp_String *)v.v.p;
212
- - if (sp_String_is_frozen(_m)) { sp_raise_frozen_str(_m->data); break; }
213
- + if (sp_String_is_immutable(_m)) { sp_raise_frozen_str(_m->data); break; }
214
- _m->len = 0; _m->data[0] = 0; sp_fd_publish(_m);
215
- break;
216
- }
217
- diff --git a/lib/sp_str.c b/lib/sp_str.c
218
- index 735ca9982..fbaf2df7e 100644
219
- --- a/lib/sp_str.c
220
- +++ b/lib/sp_str.c
221
- @@ -228,7 +228,10 @@ const char *sp_str_plus_lit(const char *a, const char *lit, size_t ll) {
222
- /* FrozenError naming the receiver, matching CRuby's
223
- "can't modify frozen String: \"abc\"" message shape. */
224
- void sp_exc_stage_recv(sp_RbVal v); /* generated TU: stages FrozenError#receiver */
225
- -void sp_raise_frozen_str(const char*s){SP_GC_ROOT_STR(s);const char*ins=sp_str_inspect(s);SP_GC_ROOT_STR(ins);const char*msg=sp_str_concat(&("\xff" "can't modify frozen String: ")[1],ins);SP_GC_ROOT_STR(msg);sp_exc_stage_recv(sp_box_str(s));sp_raise_cls("FrozenError",msg);}
226
- +void sp_raise_locked_str(void) {
227
- + sp_raise_cls("RuntimeError", "can't modify string; temporarily locked");
228
- +}
229
- +void sp_raise_frozen_str(const char*s){if(sp_str_is_locked(s))sp_raise_locked_str();SP_GC_ROOT_STR(s);const char*ins=sp_str_inspect(s);SP_GC_ROOT_STR(ins);const char*msg=sp_str_concat(&("\xff" "can't modify frozen String: ")[1],ins);SP_GC_ROOT_STR(msg);sp_exc_stage_recv(sp_box_str(s));sp_raise_cls("FrozenError",msg);}
230
- /* Simple Unicode case mapping for ASCII + Latin-1 Supplement (U+00C0-00FF)
231
- + Latin Extended-A (U+0100-017F), the ranges the ruby/spec string case
232
- specs exercise. Full Unicode is out of scope (one internal UTF-8 rep). */
233
- diff --git a/lib/sp_string.c b/lib/sp_string.c
234
- index f9c2c74a1..8547a4696 100644
235
- --- a/lib/sp_string.c
236
- +++ b/lib/sp_string.c
237
- @@ -40,11 +40,11 @@ void sp_String_mark_literals(void) {
238
- for (sp_StringLiteral *p = sp_string_literals; p; p = p->next) sp_gc_mark(p->value);
239
- }
240
-
241
- -void sp_String_prepend(sp_String*s,const char*t){SP_GC_ROOT(s);SP_GC_ROOT_STR(t);if (SP_UNLIKELY(!s)) sp_nil_recv("prepend"); if (!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}int64_t tl=(int64_t)strlen(t);if(!sp_fd_grow(s,s->len+tl))return;memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;sp_fd_publish(s);}
242
- +void sp_String_prepend(sp_String*s,const char*t){SP_GC_ROOT(s);SP_GC_ROOT_STR(t);if (SP_UNLIKELY(!s)) sp_nil_recv("prepend"); if (!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}int64_t tl=(int64_t)strlen(t);if(!sp_fd_grow(s,s->len+tl))return;memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;sp_fd_publish(s);}
243
- /* String#insert(idx, str): insert at idx; negative idx is relative to len+1. */
244
- -void sp_String_insert(sp_String*s,int64_t idx,const char*t){SP_GC_ROOT(s);SP_GC_ROOT_STR(t);if (SP_UNLIKELY(!s)) sp_nil_recv("insert"); if (!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}int64_t tl=(int64_t)strlen(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(!sp_fd_grow(s,s->len+tl))return;memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;sp_fd_publish(s);}
245
- +void sp_String_insert(sp_String*s,int64_t idx,const char*t){SP_GC_ROOT(s);SP_GC_ROOT_STR(t);if (SP_UNLIKELY(!s)) sp_nil_recv("insert"); if (!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}int64_t tl=(int64_t)strlen(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(!sp_fd_grow(s,s->len+tl))return;memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;sp_fd_publish(s);}
246
- /* String#replace(s): replace entire content. */
247
- -void sp_String_replace(sp_String*s,const char*t){SP_GC_ROOT(s);SP_GC_ROOT_STR(t);if (SP_UNLIKELY(!s)) sp_nil_recv("replace"); if (!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}int64_t tl=(int64_t)strlen(t);if(!sp_fd_grow(s,tl))return;memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;sp_fd_publish(s);}
248
- +void sp_String_replace(sp_String*s,const char*t){SP_GC_ROOT(s);SP_GC_ROOT_STR(t);if (SP_UNLIKELY(!s)) sp_nil_recv("replace"); if (!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}int64_t tl=(int64_t)strlen(t);if(!sp_fd_grow(s,tl))return;memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;sp_fd_publish(s);}
249
- sp_String*sp_String_dup(sp_String*s){SP_GC_ROOT(s);return sp_String_new(s->data);}
250
- /* The first growth past an inline payload (sp_String_new_inline_len) moves
251
- it to a malloc'd block, which the object now owns: its bytes are counted
252
- diff --git a/lib/sp_string.h b/lib/sp_string.h
253
- index 62a794d82..5f8682d1f 100644
254
- --- a/lib/sp_string.h
255
- +++ b/lib/sp_string.h
256
- @@ -51,7 +51,10 @@ static inline const void *sp_String_identity(sp_String *h) {
257
-
258
- /* Per-mutable-string freeze flag rides in the GC header alongside `marked`. */
259
- static inline sp_bool sp_String_is_frozen(sp_String*s){if(!s)return TRUE;sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));return h->frozen;}
260
- -static inline sp_String*sp_String_freeze(sp_String*s){if(s){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));h->frozen=1;}return s;}
261
- +static inline sp_bool sp_String_is_immutable(sp_String *s) {
262
- + return sp_String_is_frozen(s) || (s && s->temporarily_locked);
263
- +}
264
- +static inline sp_String*sp_String_freeze(sp_String*s){if(s){if(s->temporarily_locked)sp_raise_locked_str();sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));h->frozen=1;}return s;}
265
-
266
- /* A mutable String's payload carries the same length-bearing sp_str_hdr that
267
- 0xfe/0xfc heap strings use, so an escaped const char* is binary-safe (the
268
- @@ -177,12 +180,12 @@ static inline void sp_fd_append_len(sp_String*s,const char*t,int64_t tl){
269
- #else
270
- static inline void sp_fd_append_len(sp_String*s,const char*t,int64_t tl){if(!sp_fd_grow(s,s->len+tl))return;memcpy(s->data+s->len,t,tl);s->len+=tl;s->data[s->len]=0;sp_fd_publish(s);}
271
- #endif
272
- -static inline void sp_String_append(sp_String*s,const char*t){if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}sp_fd_append_len(s,t,(int64_t)strlen(t));}
273
- +static inline void sp_String_append(sp_String*s,const char*t){if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}sp_fd_append_len(s,t,(int64_t)strlen(t));}
274
- /* Binary-safe append: sizes the operand with the header length so an embedded
275
- NUL is preserved (Ruby String#<< / concat on a marked spinel string). */
276
- /* Replace the buffer contents in place (the handle stays stable, so every
277
- alias and container holding it observes the new value; #3227). */
278
- -static inline void sp_String_set_bin(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}s->len=0;sp_fd_append_len(s,t,(int64_t)sp_str_byte_len(t));}
279
- +static inline void sp_String_set_bin(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}s->len=0;sp_fd_append_len(s,t,(int64_t)sp_str_byte_len(t));}
280
- /* The bytes a read put into its (len, outbuf) buffer: binary-safe, and the
281
- buffer becomes ASCII-8BIT, as CRuby's readpartial/sysread/read_nonblock
282
- leave it. */
283
- @@ -212,12 +215,12 @@ static inline void sp_String_append_enc(sp_String*s,const char*t,size_t tl){
284
- }
285
- }
286
- /* the first tl bytes of t: the append form of an interpolation (emit_interp_append) */
287
- -static inline void sp_String_append_n(sp_String*s,const char*t,size_t tl){if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}sp_String_append_enc(s,t,tl);sp_fd_append_len(s,t,(int64_t)tl);}
288
- +static inline void sp_String_append_n(sp_String*s,const char*t,size_t tl){if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}sp_String_append_enc(s,t,tl);sp_fd_append_len(s,t,(int64_t)tl);}
289
- /* append_as_bytes preserves the handle's encoding as well as embedded NULs. */
290
- -static inline void sp_String_append_bytes(sp_String*s,const char*t){if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}sp_fd_append_len(s,t,(int64_t)sp_str_byte_len(t));}
291
- +static inline void sp_String_append_bytes(sp_String*s,const char*t){if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}sp_fd_append_len(s,t,(int64_t)sp_str_byte_len(t));}
292
- static inline void sp_String_append_bin(sp_String*s,const char*t){
293
- if (SP_UNLIKELY(!s)) sp_nil_recv("<<"); if (!t)return;
294
- - if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return;}
295
- + if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return;}
296
- size_t tl=sp_str_byte_len(t);
297
- sp_String_append_enc(s,t,tl);
298
- sp_fd_append_len(s,t,(int64_t)tl);
299
- @@ -405,7 +408,7 @@ static inline sp_String*sp_String_new_unfrozen(const char*s){
300
- into the bytes now; mode 1 sets it, 0 clears it, -1 (another encoding)
301
- leaves it. A frozen String refuses either way, as CRuby does (#3334). */
302
- static inline sp_String*sp_String_force_encoding(sp_String*s,int mode){
303
- - if(sp_String_is_frozen(s)){sp_raise_frozen_str(s->data);return s;}
304
- + if(sp_String_is_immutable(s)){sp_raise_frozen_str(s->data);return s;}
305
- if(mode<0)return s;
306
- s->binary=(unsigned)mode;
307
- sp_str_hdr*h=(sp_str_hdr*)sp_fd_base(s->data);
308
- diff --git a/lib/sp_types.h b/lib/sp_types.h
309
- index d09318115..b8ac7fc2e 100644
310
- --- a/lib/sp_types.h
311
- +++ b/lib/sp_types.h
312
- @@ -328,7 +328,7 @@ typedef struct{void**data;sp_int len;sp_int cap;void(*scan_elem)(void*);int32_t
313
- /* the element of a String Array (and a String Hash's value): the handle
314
- under SP_ONE_STRING, the bytes otherwise (sp_string.h's SP_SEB / SP_SEF) */
315
- /* the shared String handle (sp_string.h) */
316
- -struct sp_String_s { char *data; int64_t len; int64_t cap; unsigned binary; };
317
- +struct sp_String_s { char *data; int64_t len; int64_t cap; unsigned binary; unsigned temporarily_locked; };
318
- #ifdef SP_ONE_STRING
319
- typedef struct sp_String_s *sp_strelem;
320
- #else
321
- diff --git a/lib/spinel_rt.h b/lib/spinel_rt.h
322
- index 1f02f86a6..e866c28d4 100644
323
- --- a/lib/spinel_rt.h
324
- +++ b/lib/spinel_rt.h
325
- @@ -1080,6 +1080,7 @@ static const char *sp_str_frozen_name(const char *s) {
326
- }
327
- static inline const char *sp_str_freeze_val(const char *s) {
328
- if (!s) return s;
329
- + if (sp_str_is_locked(s)) sp_raise_locked_str();
330
- unsigned char m = ((const unsigned char *)s)[-1];
331
- if (m == 0xfe || m == 0xfc) {
332
- /* a frozen heap string is immortal, as a literal is: the sweep is told */
333
- @@ -7966,7 +7967,7 @@ static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) {
334
- }
335
- /* a frozen String refuses, as the typed paths do: a literal is frozen
336
- (frozen_string_literal is always on) and so is a `.freeze`d one (#6328) */
337
- - if (a.tag == SP_TAG_STR && a.v.s && sp_str_is_frozen_val(a.v.s)) sp_raise_frozen_str(a.v.s);
338
- + if (a.tag == SP_TAG_STR && a.v.s && (sp_str_is_frozen_val(a.v.s) || sp_str_is_locked(a.v.s))) sp_raise_frozen_str(a.v.s);
339
- /* String#<< appends (sp_str_concat treats NULL as the empty string) */
340
- if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR)
341
- return sp_box_str_new(sp_str_concat(a.v.s, b.v.s));
342
- @@ -8791,7 +8792,7 @@ static sp_RbVal sp_poly_splice(sp_RbVal recv, sp_int start, sp_int len, sp_RbVal
343
- store back, while a shared handle absorbs it in place -- which is the only
344
- form an element receiver can use, and the write was silently dropped
345
- before (#3940). */
346
- - if (recv.tag == SP_TAG_STR && recv.v.s && sp_str_is_frozen_val(recv.v.s)) sp_raise_frozen_str(recv.v.s); /* #6328 */
347
- + if (recv.tag == SP_TAG_STR && recv.v.s && (sp_str_is_frozen_val(recv.v.s) || sp_str_is_locked(recv.v.s))) sp_raise_frozen_str(recv.v.s); /* #6328 */
348
- if (recv.tag == SP_TAG_STR || sp_poly_is_strbuf(recv)) {
349
- const char *cur = (recv.tag == SP_TAG_STR) ? (recv.v.s ? recv.v.s : sp_str_empty)
350
- : sp_String_cstr((sp_String *)recv.v.p);
351
- @@ -11675,7 +11676,7 @@ static SP_NOINLINE const char *sp_io_read_splat(sp_File *f, sp_PolyArray *a) {
352
- sp_int ln = sp_poly_nil_p(len) ? 0 : sp_poly_arg_int_chk(len);
353
- if (!sp_poly_nil_p(buf)) {
354
- if (sp_poly_is_strbuf(buf)) {
355
- - if (sp_String_is_frozen((sp_String *)buf.v.p)) sp_raise_frozen_str(sp_String_cstr((sp_String *)buf.v.p));
356
- + if (sp_String_is_immutable((sp_String *)buf.v.p)) sp_raise_frozen_str(sp_String_cstr((sp_String *)buf.v.p));
357
- }
358
- else {
359
- sp_str_check_mutable(sp_poly_arg_str_chk(buf));
360
- @@ -11697,7 +11698,7 @@ static const char *sp_io_read_nonblock_buffer(sp_File *f, sp_int n, sp_RbVal *bu
361
- SP_GC_ROOT_RBVAL(v);
362
- if (n < 0) sp_raise_cls("ArgumentError", sp_sprintf("negative length %lld given", (long long)n));
363
- const char *s = sp_poly_nil_p(v) ? NULL : sp_poly_arg_str_chk(v);
364
- - if (s && sp_poly_is_strbuf(v) && sp_String_is_frozen((sp_String *)v.v.p)) sp_raise_frozen_str(s);
365
- + if (s && sp_poly_is_strbuf(v) && sp_String_is_immutable((sp_String *)v.v.p)) sp_raise_frozen_str(s);
366
- if (s) sp_str_check_mutable(s);
367
- if (exception && exception->tag != SP_TAG_BOOL)
368
- sp_raise_cls("ArgumentError", sp_sprintf("expected true or false as exception: %s", sp_poly_inspect(*exception)));
369
- @@ -13259,6 +13260,10 @@ static inline sp_RbVal sp_poly_clone_freeze(sp_RbVal v, int fz) {
370
- (sp_gc_is_frozen reports it back), heap strings flip the string marker,
371
- every immutable tag (int/float/sym/nil/bool/...) is already frozen. */
372
- static inline sp_RbVal sp_poly_freeze(sp_RbVal v) {
373
- + if (sp_poly_is_strbuf(v)) {
374
- + sp_String_freeze((sp_String *)v.v.p);
375
- + return v;
376
- + }
377
- if (v.tag == SP_TAG_OBJ) {
378
- sp_gc_freeze(v.v.p);
379
- /* the array mutators test the array's own field, not the header bit, so
380
- @@ -14139,7 +14144,7 @@ static sp_RbVal sp_poly_arr_widen_and_set(sp_RbVal v, sp_int idx, sp_RbVal val)
381
- caller to reassign to the poly slot (sp_poly_arr_set is a no-op on a
382
- string, silently dropping the mutation) (#3172). */
383
- if (v.tag == SP_TAG_STR) {
384
- - if (v.v.s && sp_str_is_frozen_val(v.v.s)) sp_raise_frozen_str(v.v.s); /* #6328 */
385
- + if (v.v.s && (sp_str_is_frozen_val(v.v.s) || sp_str_is_locked(v.v.s))) sp_raise_frozen_str(v.v.s); /* #6328 */
386
- const char *rep = sp_str_aset_rep(val);
387
- return sp_box_str_new(sp_str_splice_at(v.v.s ? v.v.s : sp_str_empty, idx, 1, rep, 0));
388
- }
389
- @@ -14181,7 +14186,7 @@ static sp_RbVal sp_poly_str_aset_key(sp_RbVal v, sp_RbVal key, sp_RbVal val) {
390
- else sp_poly_set_poly(v, key, val);
391
- return v;
392
- }
393
- - if (v.tag == SP_TAG_STR && v.v.s && sp_str_is_frozen_val(v.v.s)) sp_raise_frozen_str(v.v.s);
394
- + if (v.tag == SP_TAG_STR && v.v.s && (sp_str_is_frozen_val(v.v.s) || sp_str_is_locked(v.v.s))) sp_raise_frozen_str(v.v.s);
395
- /* refused before anything is rooted: a raise past a root would leave it
396
- dangling under an inline rescue */
397
- sp_str_aset_refuse(val);
398
- @@ -22338,7 +22343,7 @@ static sp_RbVal sp_poly_pack_buffer(sp_RbVal recv, sp_RbVal format, sp_RbVal buf
399
- if (buffer.tag != SP_TAG_STR && !sp_poly_is_strbuf(buffer))
400
- sp_raise_cls("TypeError", sp_sprintf("buffer must be String, not %s", sp_poly_class_name(buffer)));
401
- initial = sp_poly_to_s(buffer);
402
- - if (sp_poly_is_strbuf(buffer) && sp_String_is_frozen((sp_String *)buffer.v.p)) sp_raise_frozen_str(initial);
403
- + if (sp_poly_is_strbuf(buffer) && sp_String_is_immutable((sp_String *)buffer.v.p)) sp_raise_frozen_str(initial);
404
- sp_str_check_mutable(initial);
405
- }
406
- SP_GC_ROOT_STR(initial);
407
- diff --git a/packages/io/io/buffer.rb b/packages/io/io/buffer.rb
408
- index 661d3a936..fc50eaa9e 100644
409
- --- a/packages/io/io/buffer.rb
410
- +++ b/packages/io/io/buffer.rb
411
- @@ -81,6 +81,8 @@ module IOBufferPackage
412
- native_method :__lock, [], :self, "sp_IOBuffer_lock"
413
- native_method :__unlock, [], :self, "sp_IOBuffer_unlock"
414
- native_method :__become_for, [:string], :self, "sp_IOBuffer_become_for"
415
- + native_method :__borrow_string, [:string], :self, "sp_IOBuffer_borrow_string"
416
- + native_method :__end_borrow, [], :self, "sp_IOBuffer_end_borrow"
417
- # The IO integration: the Ruby-side read/write/pread/pwrite below hold the
418
- # lock around these; a negative length or size stands for nil.
419
- native_method :__read_io, [:any, :int, :int], :int, "sp_IOBuffer_read_io"
420
- @@ -109,16 +111,19 @@ class IO::Buffer
421
- DEFAULT_SIZE = 65536
422
- PAGE_SIZE = IOBufferPackage.__page_size
423
-
424
- - # IO::Buffer.for(string) answers a READONLY copy of the string's bytes.
425
- - # CRuby's is a zero-copy view; Spinel strings are immutable, so a copy is
426
- - # observably the same -- except the BLOCK form, whose whole point is
427
- - # writing through into the String. That cannot be represented here, so it
428
- - # refuses loudly rather than silently dropping the writes.
429
- + # With a block, lend the String's current bytes for the duration of
430
- + # the yield. The non-block form keeps its read-only snapshot.
431
- def self.for(string)
432
- - if block_given?
433
- - raise NotImplementedError, "IO::Buffer.for with a block (writing through into the String) is not supported"
434
- + return new(0).__become_for(string) unless block_given?
435
- +
436
- + owner = new(0)
437
- + buffer = owner.__borrow_string(string)
438
- + begin
439
- + yield buffer
440
- + ensure
441
- + owner.__end_borrow
442
- + buffer.free
443
- end
444
- - new(0).__become_for(string)
445
- end
446
-
447
- # IO::Buffer.map(file, size = nil, offset = 0, flags = 0): a view of the
448
- diff --git a/src/codegen_call_io.c b/src/codegen_call_io.c
449
- index 4e668a61e..b124ec5c2 100644
450
- --- a/src/codegen_call_io.c
451
- +++ b/src/codegen_call_io.c
452
- @@ -399,7 +399,7 @@ static void emit_io_read_nonblock_buffer(Compiler *c, int id, int handle, Buf *b
453
- if (shared) buf_printf(b, "_t%d = sp_poly_strbuf_lift(_t%d); ", held[1], held[1]);
454
- buf_printf(b, "const char *_t%d = sp_poly_nil_p(_t%d) ? NULL : sp_poly_arg_str_chk(_t%d); "
455
- "SP_GC_ROOT_STR(_t%d); if (_t%d && sp_poly_is_strbuf(_t%d) && "
456
- - "sp_String_is_frozen((sp_String *)_t%d.v.p)) sp_raise_frozen_str(_t%d); "
457
- + "sp_String_is_immutable((sp_String *)_t%d.v.p)) sp_raise_frozen_str(_t%d); "
458
- "if (_t%d) sp_str_check_mutable(_t%d); ",
459
- ts, held[1], held[1], ts, ts, held[1], held[1], ts, ts, ts);
460
- if (argc == 3) {
461
- diff --git a/src/codegen_call_recv.c b/src/codegen_call_recv.c
462
- index a5edfa2e8..ae9c5bbf1 100644
463
- --- a/src/codegen_call_recv.c
464
- +++ b/src/codegen_call_recv.c
465
- @@ -773,7 +773,7 @@ static int sb_iv_expr_shim(Compiler *c, int id, int recvS, Buf *b,
466
- if (!handled) { free(armb.p); free(pre.p); free(srefI.p); return 0; }
467
- TyKind resty = repr_of(c, id).as_ty;
468
- buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s);%s"
469
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
470
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
471
- " const char *lv__sb%d = sp_str_concat(sp_String_cstr(_t%d), (&(\"\\xff\")[1]));"
472
- " SP_GC_ROOT(lv__sb%d); ",
473
- tH, srefI.p, pre.p ? pre.p : "", tH, tH, tH, tH, tH);
474
- @@ -802,7 +802,7 @@ static int sb_reader_expr_shim(Compiler *c, int id, int recvS, Buf *b,
475
- if (!handled) { free(armb.p); free(pre.p); free(srefR.p); return 0; }
476
- TyKind resty = repr_of(c, id).as_ty;
477
- buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s);%s"
478
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
479
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
480
- " const char *lv__sb%d = sp_str_concat(sp_String_cstr(_t%d), (&(\"\\xff\")[1]));"
481
- " SP_GC_ROOT(lv__sb%d); ",
482
- tH, srefR.p, pre.p ? pre.p : "", tH, tH, tH, tH, tH);
483
- @@ -854,7 +854,7 @@ static int sb_route_expr_shim(Compiler *c, int id, int recvS, Buf *b,
484
- allocate */
485
- /* (one representation: the receiver's text may be a view of the handle; sp_String_new_fresh finds it) */
486
- buf_printf(b, "({ sp_String *_t%d = %s%s%s; SP_GC_ROOT(_t%d);%s"
487
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
488
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
489
- " const char *lv__sb%d = sp_str_concat(sp_String_cstr(_t%d), (&(\"\\xff\")[1]));"
490
- " SP_GC_ROOT(lv__sb%d); ",
491
- tH, "SP_STR_HDL(", hb.p, ")", tH, pre.p ? pre.p : "", tH, tH, tH, tH, tH);
492
- @@ -4881,7 +4881,7 @@ static int emit_str_mutator_call(Compiler *c, int id, Buf *b, const NodeTable *n
493
- mutability check reads a NULL as */
494
- if (!lvw) buf_printf(b, "if (!_t%d) sp_nil_recv(\"%s\"); ", to, sb_bang);
495
- /* an in-place mutator on a frozen string raises FrozenError (#3003) */
496
- - buf_printf(b, "if (sp_str_is_frozen_val(_t%d)) sp_raise_frozen_str(_t%d); ", to, to);
497
- + buf_printf(b, "sp_str_check_mutable(_t%d); ", to);
498
- int subm2 = sb_sub; /* gsub!/sub!: nil means no substitution, see above */
499
- if (subm2) buf_puts(g_pre ? g_pre : b, "sp_re_sub_matched = 0; ");
500
- char *sv_nm = nt_node_swap_str((NodeTable *)nt, id, "name", sb_plain);
501
- @@ -5094,7 +5094,7 @@ static int emit_str_mutator_call(Compiler *c, int id, Buf *b, const NodeTable *n
502
- Buf lb0; memset(&lb0, 0, sizeof lb0);
503
- if (!strbuf_recv_handle_any(c, id, recv, &sref0, &lb0)) return 0;
504
- int t0 = ++g_tmp;
505
- - buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s); if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
506
- + buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s); if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
507
- " sp_strbuf_read_pub(_t%d); })", t0, lb0.p ? lb0.p : sref0.p, t0, t0, t0);
508
- free(lb0.p); free(sref0.p);
509
- { *out = 1; return 1; }
510
- @@ -5419,7 +5419,7 @@ int emit_array_call(Compiler *c, int id, Buf *b) {
511
- else {
512
- TyKind resty = repr_of(c, id).as_ty;
513
- buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s);%s"
514
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
515
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
516
- " const char *lv__sb%d = sp_str_concat(sp_String_cstr(_t%d), (&(\"\\xff\")[1]));"
517
- " SP_GC_ROOT(lv__sb%d); ",
518
- tH, srefL.p, pre.p ? pre.p : "", tH, tH, tH, tH, tH);
519
- @@ -10975,7 +10975,7 @@ static int emit_scalar_call_arms(Compiler *c, int id, Buf *b) {
520
- emit_int_expr(c, avS[1], b);
521
- /* (one representation: the handle's own payload is written in
522
- place, sp_String_setbyte) */
523
- - buf_printf(b, "; if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
524
- + buf_printf(b, "; if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
525
- " sp_String_setbyte(_t%d, _i%d, _v%d); })", tH, tH, tH, tH, tH);
526
- return 1;
527
- }
528
- @@ -10989,14 +10989,14 @@ static int emit_scalar_call_arms(Compiler *c, int id, Buf *b) {
529
- emit_int_expr(c, avS[0], b);
530
- buf_printf(b, "; sp_int _v%d = ", tH);
531
- emit_int_expr(c, avS[1], b);
532
- - buf_printf(b, "; if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
533
- + buf_printf(b, "; if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
534
- " sp_String_setbyte(_t%d, _i%d, _v%d); })", tH, tH, tH, tH, tH);
535
- return 1;
536
- }
537
- if (haveB) {
538
- int tH = ++g_tmp;
539
- buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s);"
540
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
541
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
542
- " const char *_p%d = sp_String_cstr(_t%d); sp_int _v%d = ",
543
- tH, srefB.p, tH, tH, tH, tH, tH);
544
- free(srefB.p);
545
- @@ -11036,7 +11036,7 @@ static int emit_scalar_call_arms(Compiler *c, int id, Buf *b) {
546
- if (!handled) { free(armb.p); free(srefL.p); }
547
- else {
548
- buf_printf(b, "({ sp_String *_t%d = SP_STR_HDL(%s);"
549
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
550
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
551
- " const char *lv__sb%d = sp_str_concat(sp_String_cstr(_t%d), (&(\"\\xff\")[1]));"
552
- " SP_GC_ROOT(lv__sb%d);"
553
- " sp_int _res%d = %s;"
554
- @@ -13594,7 +13594,7 @@ static void emit_strbuf_encode_bang(Compiler *c, const char *ref, int hr, const
555
- emit_route_recv_root(c, hr, th, 0, NULL, TY_STRING, b);
556
- int ta = encode_args_hold(c, argv, argc, &npos, b);
557
- buf_printf(b, " if (!_t%d) sp_nil_recv(\"encode!\");"
558
- - " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);"
559
- + " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);"
560
- " sp_String_encode_into(_t%d, sp_str_encode(sp_String_cstr(_t%d), ", th, th, th, th, th);
561
- emit_encode_args(ta, npos, b);
562
- buf_printf(b, ")); sp_strbuf_read_pub(_t%d); })", th);
563
- @@ -18369,7 +18369,7 @@ static void emit_poly_encoding_mutator(Compiler *c, const char *name, int recv,
564
- int ta = encode_hold(c, "sp_RbVal", "SP_GC_ROOT_RBVAL", rv.p ? rv.p : "", recv, argv, argc, tv, &npos, b);
565
- free(rv.p);
566
- buf_printf(b, " if (sp_poly_is_strbuf(_t%d)) { sp_String *_h%d = (sp_String *)_t%d.v.p;"
567
- - " if (sp_String_is_frozen(_h%d)) sp_raise_frozen_str(_h%d->data);"
568
- + " if (sp_String_is_immutable(_h%d)) sp_raise_frozen_str(_h%d->data);"
569
- " sp_String_encode_into(_h%d, sp_str_encode(sp_String_cstr(_h%d), ",
570
- tv, tv, tv, tv, tv, tv, tv);
571
- emit_encode_args(ta, npos, b);
572
- diff --git a/src/codegen_stmt.c b/src/codegen_stmt.c
573
- index d628830ed..c8812f613 100644
574
- --- a/src/codegen_stmt.c
575
- +++ b/src/codegen_stmt.c
576
- @@ -19751,8 +19751,8 @@ static int emit_call_stmt(Compiler *c, int id, Buf *b, int indent, const NodeTab
577
- if (repr_of_slot(c, llv).kind == RK_STRBUF) {
578
- emit_indent(b, indent);
579
- /* (a local a block captures lives in its cell) */
580
- - if (llv->is_cell) buf_printf(b, "sp_gc_freeze((void *)(*_cell_%s));\n", rename_local(lnm));
581
- - else buf_printf(b, "sp_gc_freeze((void *)lv_%s);\n", rename_local(lnm));
582
- + if (llv->is_cell) buf_printf(b, "sp_String_freeze(*_cell_%s);\n", rename_local(lnm));
583
- + else buf_printf(b, "sp_String_freeze(lv_%s);\n", rename_local(lnm));
584
- return 1;
585
- }
586
- }
587
- @@ -23435,7 +23435,7 @@ static void emit_sb_shim_swap(Buf *b, int indent, int tH, Buf *pre, char *arm) {
588
- buf_puts(b, pre->p ? pre->p : "");
589
- free(pre->p);
590
- emit_indent(b, indent + 1);
591
- - buf_printf(b, "if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);\n", tH, tH);
592
- + buf_printf(b, "if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);\n", tH, tH);
593
- emit_indent(b, indent + 1);
594
- buf_printf(b, "const char *lv__sb%d = sp_str_concat(sp_String_cstr(_t%d), (&(\"\\xff\")[1]));\n", tH, tH);
595
- emit_indent(b, indent + 1);
596
- @@ -23931,7 +23931,7 @@ static int str_mutate_reassign_arms(Compiler *c, Buf *b, int indent, const NodeT
597
- buf_printf(b, " sp_int _t%d = ", ts); emit_int_expr(c, argv[0], b);
598
- buf_printf(b, "; sp_int _t%d = ", tl); emit_int_expr(c, argv[1], b); buf_puts(b, ";");
599
- char *v = emit_str_splice_value(c, recv, argv[2], 0, ts, b);
600
- - buf_printf(b, " if (sp_String_is_frozen(_t%d)) sp_raise_frozen_str(_t%d->data);", th, th);
601
- + buf_printf(b, " if (sp_String_is_immutable(_t%d)) sp_raise_frozen_str(_t%d->data);", th, th);
602
- buf_printf(b, " sp_String_set_bin(_t%d, sp_str_splice_at(sp_String_cstr(_t%d), _t%d, _t%d, %s, 0)); }\n",
603
- th, th, ts, tl, v ? v : "NULL");
604
- free(v);
605
- @@ -24096,7 +24096,7 @@ void emit_str_concat_handle(Compiler *c, const char *sref, int argc, const int *
606
- }
607
- g_one_held = held0;
608
- free(rt.p);
609
- - buf_printf(b, " if (sp_String_is_frozen(%s)) sp_raise_frozen_str((%s)->data);\n", sref, sref);
610
- + buf_printf(b, " if (sp_String_is_immutable(%s)) sp_raise_frozen_str((%s)->data);\n", sref, sref);
611
- for (int a = 0; a < argc; a++) {
612
- emit_indent(b, indent + 1);
613
- buf_printf(b, "sp_String_append_bin(%s, _t%d);\n", sref, base + a);
614
- diff --git a/test/io_buffer_for_block.rb b/test/io_buffer_for_block.rb
615
- new file mode 100644
616
- index 000000000..aaa5197dc
617
- --- /dev/null
618
- +++ b/test/io_buffer_for_block.rb
619
- @@ -0,0 +1,59 @@
620
- +s = +"abcdef"
621
- +alias_of_s = s
622
- +before = s.hash
623
- +escaped = nil
624
- +result = IO::Buffer.for(s) do |buffer|
625
- + escaped = buffer
626
- + p [buffer.external?, buffer.readonly?, buffer.size]
627
- + buffer.clear(90, 1, 2)
628
- + p alias_of_s
629
- + p before != s.hash
630
- + buffer.slice(3, 2).set_string("XY")
631
- + p s
632
- + begin
633
- + buffer.clear(1, 5, 2)
634
- + rescue ArgumentError => error
635
- + puts error.message
636
- + end
637
- + begin
638
- + alias_of_s << "extra"
639
- + rescue RuntimeError => error
640
- + puts error.message
641
- + end
642
- + begin
643
- + s.setbyte(0, 0)
644
- + rescue RuntimeError => error
645
- + puts error.message
646
- + end
647
- + begin
648
- + s.freeze
649
- + rescue RuntimeError => error
650
- + puts error.message
651
- + end
652
- + :answer
653
- +end
654
- +p [result, escaped.null?, escaped.size, s.frozen?]
655
- +s << "!"
656
- +p s
657
- +
658
- +begin
659
- + IO::Buffer.for(s) do |buffer|
660
- + buffer.set_value(:U8, 0, 65)
661
- + raise "from block"
662
- + end
663
- +rescue RuntimeError => error
664
- + puts error.message
665
- +end
666
- +s.setbyte(1, 66)
667
- +p s
668
- +
669
- +frozen = "xyz".freeze
670
- +p IO::Buffer.for(frozen) { |buffer| [buffer.readonly?, buffer.get_string] }
671
- +begin
672
- + IO::Buffer.for(frozen) { |buffer| buffer.clear(0) }
673
- +rescue IO::Buffer::AccessError => error
674
- + puts error.message
675
- +end
676
- +
677
- +empty = +""
678
- +IO::Buffer.for(empty) { |buffer| p [buffer.null?, buffer.size, buffer.get_string] }
679
- diff --git a/test/io_buffer_for_block.rb.expected b/test/io_buffer_for_block.rb.expected
680
- new file mode 100644
681
- index 000000000..439856c6e
682
- --- /dev/null
683
- +++ b/test/io_buffer_for_block.rb.expected
684
- @@ -0,0 +1,15 @@
685
- +[true, false, 6]
686
- +"aZZdef"
687
- +true
688
- +"aZZXYf"
689
- +Specified offset+length is bigger than the buffer size!
690
- +can't modify string; temporarily locked
691
- +can't modify string; temporarily locked
692
- +can't modify string; temporarily locked
693
- +[:answer, true, 0, false]
694
- +"aZZXYf!"
695
- +from block
696
- +"ABZXYf!"
697
- +[true, "xyz"]
698
- +Buffer is not writable!
699
- +[false, 0, ""]