phlex-native 0.1.0-aarch64-linux-gnu
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.
- checksums.yaml +7 -0
- data/README.md +43 -0
- data/ext/phlex_native/extconf.rb +13 -0
- data/ext/phlex_native/phlex_native.c +624 -0
- data/lib/phlex-native.rb +3 -0
- data/lib/phlex_native/phlex_native.so +0 -0
- data/lib/phlex_native.rb +8 -0
- metadata +65 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4e0724f5bc31c536e33ba3bf98ca229ff1f1116df452036c6e82214ba5d2e056
|
|
4
|
+
data.tar.gz: ef17bd64c8565e1d79cbf3f092cf4362c7582feba56464ade555658e2b71e408
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ca6b901c3e796d8a4d01688fa87413bb819f90426fcf672a04c0c04e844e10e6e2e5ae0999a3575f45211c19002e62db7111b21d594a702169c212db56c00c10
|
|
7
|
+
data.tar.gz: a63e98d7ce351b0c15da7a3a170c06e00a31c8de2ea8847ba2e465553489a068e801bdfc25edc3b0ac27ea7eabbd2c9700c5e9643b1ce297ab49254b04338676
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# phlex-native
|
|
2
|
+
|
|
3
|
+
A C extension that serialises Phlex attributes. It needs the latest stable Ruby (currently 4.0). Add it to your Gemfile alongside `phlex` and Phlex will use it automatically:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
gem "phlex"
|
|
7
|
+
gem "phlex-native"
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
It produces exactly the same output, and raises exactly the same errors, as Phlex’s Ruby implementation. Anything it doesn’t handle natively (Sets, Dates, Times, subclasses of core types, objects with singleton methods, and so on) is handed back to Ruby. Each release depends on exactly one version of Phlex, the one whose Ruby it mirrors.
|
|
11
|
+
|
|
12
|
+
With it loaded, Phlex no longer caches serialised attributes, because generating them natively is faster than looking them up.
|
|
13
|
+
|
|
14
|
+
It assumes core methods such as `String#gsub`, `Symbol#name`, `Integer#to_s`, `Float#to_s` and `Hash#each` haven’t been redefined globally, and that `Phlex::SGML::Attributes` and its methods haven’t been replaced or overridden.
|
|
15
|
+
|
|
16
|
+
## Development
|
|
17
|
+
|
|
18
|
+
From the repository root:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
mise run setup # installs phlex-native's build dependencies too
|
|
22
|
+
mise run native:test # compiles phlex-native, then runs every test with it loaded
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
To build with AddressSanitizer and UndefinedBehaviorSanitizer, and run the differential tests while the GC compacts and runs constantly, use a Ruby built with AddressSanitizer, as the `native-sanitizers` CI job does:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
(cd phlex-native && PHLEX_NATIVE_SANITIZE=true bundle exec rake compile)
|
|
29
|
+
PHLEX_NATIVE=true PHLEX_NATIVE_STRESS=true bundle exec qt ./phlex-native/quickdraw/attributes.test.rb
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Releasing
|
|
33
|
+
|
|
34
|
+
1. Release Phlex first. This gem depends on exactly the version in `lib/phlex/version.rb`.
|
|
35
|
+
2. Bump `spec.version` in `phlex-native.gemspec`.
|
|
36
|
+
3. With Docker running (OrbStack works), from the repository root:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
mise run native:package # phlex-native/pkg: the source gem, and a precompiled gem for each platform
|
|
40
|
+
mise run native:release # gem push each of them
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The precompiled gems are cross-compiled with [rake-compiler-dock](https://github.com/rake-compiler/rake-compiler-dock), one platform at a time. The source gem compiles on install, for platforms without one.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
|
|
5
|
+
append_cflags(["-O3", "-Wall", "-Wextra", "-Wno-unused-parameter"])
|
|
6
|
+
|
|
7
|
+
if ENV["PHLEX_NATIVE_SANITIZE"] == "true"
|
|
8
|
+
sanitizers = "-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer"
|
|
9
|
+
$CFLAGS << " #{sanitizers} -O1"
|
|
10
|
+
$LDFLAGS << " #{sanitizers}"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
create_makefile("phlex_native/phlex_native")
|
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
// Native attribute serialisation for Phlex.
|
|
2
|
+
//
|
|
3
|
+
// Every function here mirrors a method in lib/phlex/sgml/attributes.rb and
|
|
4
|
+
// must produce byte-identical output. Anything outside the common path (Set,
|
|
5
|
+
// Date, Time, SafeObject, :style, subclasses, objects with singleton classes,
|
|
6
|
+
// non-ASCII top-level names, broken encodings, or anything the Ruby version
|
|
7
|
+
// would raise on) makes the whole call fall back to the Ruby implementation.
|
|
8
|
+
|
|
9
|
+
#include <ruby.h>
|
|
10
|
+
#include <ruby/encoding.h>
|
|
11
|
+
#include <ruby/ractor.h>
|
|
12
|
+
#include <limits.h>
|
|
13
|
+
#include <string.h>
|
|
14
|
+
|
|
15
|
+
#define MAX_DEPTH 32
|
|
16
|
+
#define PREFIX_CAPACITY 1024
|
|
17
|
+
#define NORMALIZED_CAPACITY 16
|
|
18
|
+
|
|
19
|
+
static ID id_to_s, id_unsafe_reference_p, id_Phlex, id_SGML, id_Attributes, id_SafeObject, id_SafeValue;
|
|
20
|
+
static VALUE sym_style, sym_id, sym_underscore;
|
|
21
|
+
static VALUE empty_string;
|
|
22
|
+
static int utf8_index, usascii_index;
|
|
23
|
+
|
|
24
|
+
// Set once by native_prepended, before any of them are read.
|
|
25
|
+
static VALUE attributes_module = Qnil, safe_object_module = Qnil, safe_value_class = Qnil;
|
|
26
|
+
|
|
27
|
+
typedef struct {
|
|
28
|
+
VALUE receiver;
|
|
29
|
+
VALUE string;
|
|
30
|
+
char *ptr;
|
|
31
|
+
long length;
|
|
32
|
+
long capacity;
|
|
33
|
+
int depth;
|
|
34
|
+
char prefix[PREFIX_CAPACITY];
|
|
35
|
+
} output_t;
|
|
36
|
+
|
|
37
|
+
typedef struct {
|
|
38
|
+
output_t *output;
|
|
39
|
+
long prefix_length;
|
|
40
|
+
int fallback;
|
|
41
|
+
} nested_t;
|
|
42
|
+
|
|
43
|
+
// The string is hidden from ObjectSpace until it's finished, so nothing can
|
|
44
|
+
// change it behind the cached pointer while unsafe_reference? runs.
|
|
45
|
+
static void output_init(output_t *o, VALUE receiver) {
|
|
46
|
+
o->receiver = receiver;
|
|
47
|
+
o->string = rb_str_buf_new(128);
|
|
48
|
+
rb_obj_hide(o->string);
|
|
49
|
+
rb_enc_associate_index(o->string, utf8_index);
|
|
50
|
+
o->ptr = RSTRING_PTR(o->string);
|
|
51
|
+
o->length = 0;
|
|
52
|
+
o->capacity = (long)rb_str_capacity(o->string);
|
|
53
|
+
o->depth = 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static VALUE output_finish(output_t *o) {
|
|
57
|
+
rb_str_set_len(o->string, o->length);
|
|
58
|
+
ENC_CODERANGE_CLEAR(o->string);
|
|
59
|
+
return rb_obj_reveal(o->string, rb_cString);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Ruby raises the same error when a string would outgrow a long, which is 32 bits on Windows.
|
|
63
|
+
#define RAISE_TOO_BIG() rb_raise(rb_eArgError, "string size too big")
|
|
64
|
+
|
|
65
|
+
static void reserve(output_t *o, long n) {
|
|
66
|
+
if (n > LONG_MAX - o->length) RAISE_TOO_BIG();
|
|
67
|
+
if (o->length + n <= o->capacity) return;
|
|
68
|
+
rb_str_set_len(o->string, o->length);
|
|
69
|
+
rb_str_modify_expand(o->string, (o->length > n && o->length <= LONG_MAX / 2) ? o->length : n);
|
|
70
|
+
o->ptr = RSTRING_PTR(o->string);
|
|
71
|
+
o->capacity = (long)rb_str_capacity(o->string);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static inline void put(output_t *o, const char *p, long n) {
|
|
75
|
+
reserve(o, n);
|
|
76
|
+
memcpy(o->ptr + o->length, p, n);
|
|
77
|
+
o->length += n;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#define PUT_LITERAL(o, literal) put(o, literal, sizeof(literal) - 1)
|
|
81
|
+
|
|
82
|
+
static inline void put_char(output_t *o, char c) {
|
|
83
|
+
reserve(o, 1);
|
|
84
|
+
o->ptr[o->length++] = c;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Appends with `"` replaced by `"`, and `_` by `-` when dasherizing.
|
|
88
|
+
// Space is reserved before reading, so nothing can allocate mid-copy.
|
|
89
|
+
static void put_value(output_t *o, const char *p, long n, int dasherize) {
|
|
90
|
+
long quotes = 0;
|
|
91
|
+
for (const char *q = p, *end = p + n; (q = memchr(q, '"', end - q)); q++) quotes++;
|
|
92
|
+
|
|
93
|
+
if (!quotes && !(dasherize && memchr(p, '_', n))) {
|
|
94
|
+
put(o, p, n);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (quotes > (LONG_MAX - n) / 5) RAISE_TOO_BIG();
|
|
99
|
+
reserve(o, n + quotes * 5);
|
|
100
|
+
char *out = o->ptr + o->length;
|
|
101
|
+
for (long i = 0; i < n; i++) {
|
|
102
|
+
char c = p[i];
|
|
103
|
+
if (c == '"') {
|
|
104
|
+
memcpy(out, """, 6);
|
|
105
|
+
out += 6;
|
|
106
|
+
} else {
|
|
107
|
+
*out++ = (dasherize && c == '_') ? '-' : c;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
o->length = out - o->ptr;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// A String the Ruby version would append to its UTF-8 buffer without raising
|
|
114
|
+
// or changing the buffer's encoding.
|
|
115
|
+
static inline int compatible_string(VALUE s) {
|
|
116
|
+
int index = ENCODING_GET(s);
|
|
117
|
+
int coderange = rb_enc_str_coderange(s);
|
|
118
|
+
if (coderange == ENC_CODERANGE_BROKEN) return 0;
|
|
119
|
+
if (index == utf8_index || index == usascii_index) return 1;
|
|
120
|
+
return coderange == ENC_CODERANGE_7BIT && rb_enc_asciicompat(rb_enc_from_index(index));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static inline int plain_string_p(VALUE v) {
|
|
124
|
+
return RB_TYPE_P(v, T_STRING) && RBASIC_CLASS(v) == rb_cString;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static inline int plain_hash_p(VALUE v) {
|
|
128
|
+
return RB_TYPE_P(v, T_HASH) && RBASIC_CLASS(v) == rb_cHash;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static inline int plain_array_p(VALUE v) {
|
|
132
|
+
return RB_TYPE_P(v, T_ARRAY) && RBASIC_CLASS(v) == rb_cArray;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// The bytes of a String or Symbol key, and whether `_` becomes `-`.
|
|
136
|
+
static int key_bytes(VALUE k, const char **p, long *n, int *dasherize) {
|
|
137
|
+
VALUE s;
|
|
138
|
+
if (SYMBOL_P(k)) {
|
|
139
|
+
s = rb_sym2str(k);
|
|
140
|
+
*dasherize = 1;
|
|
141
|
+
} else if (plain_string_p(k)) {
|
|
142
|
+
s = k;
|
|
143
|
+
*dasherize = 0;
|
|
144
|
+
} else {
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
if (!compatible_string(s)) return 0;
|
|
148
|
+
*p = RSTRING_PTR(s);
|
|
149
|
+
*n = RSTRING_LEN(s);
|
|
150
|
+
return 1;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static inline int unsafe_name_char_p(unsigned char c) {
|
|
154
|
+
switch (c) {
|
|
155
|
+
case '<': case '>': case '&': case '"': case '\'': case '/': case '=':
|
|
156
|
+
case ' ': case '\t': case '\n': case '\v': case '\f': case '\r': case 0:
|
|
157
|
+
return 1;
|
|
158
|
+
default:
|
|
159
|
+
return 0;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Integer and Float values. Strings and Symbols are handled by the callers
|
|
164
|
+
// because the two differ in whether `_` is dasherized.
|
|
165
|
+
static int put_number(output_t *o, VALUE v) {
|
|
166
|
+
if (FIXNUM_P(v)) {
|
|
167
|
+
char digits[24];
|
|
168
|
+
int n = snprintf(digits, sizeof digits, "%ld", FIX2LONG(v));
|
|
169
|
+
put(o, digits, n);
|
|
170
|
+
return 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
VALUE s;
|
|
174
|
+
if (RB_TYPE_P(v, T_BIGNUM) && RBASIC_CLASS(v) == rb_cInteger) {
|
|
175
|
+
s = rb_big2str(v, 10);
|
|
176
|
+
} else if (RB_FLOAT_TYPE_P(v)) {
|
|
177
|
+
s = rb_funcall(v, id_to_s, 0);
|
|
178
|
+
if (!plain_string_p(s) || !compatible_string(s) || memchr(RSTRING_PTR(s), '"', RSTRING_LEN(s))) return 0;
|
|
179
|
+
} else {
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
put(o, RSTRING_PTR(s), RSTRING_LEN(s));
|
|
183
|
+
RB_GC_GUARD(s);
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static inline int safe_value_p(VALUE v) {
|
|
188
|
+
return RB_TYPE_P(v, T_OBJECT) && RBASIC_CLASS(v) == safe_value_class;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static int put_scalar(output_t *o, VALUE v) {
|
|
192
|
+
if (safe_value_p(v)) {
|
|
193
|
+
VALUE s = rb_funcall(v, id_to_s, 0);
|
|
194
|
+
if (!plain_string_p(s) || !compatible_string(s)) return 0;
|
|
195
|
+
put_value(o, RSTRING_PTR(s), RSTRING_LEN(s), 0);
|
|
196
|
+
RB_GC_GUARD(s);
|
|
197
|
+
return 1;
|
|
198
|
+
}
|
|
199
|
+
if (plain_string_p(v)) {
|
|
200
|
+
if (!compatible_string(v)) return 0;
|
|
201
|
+
put_value(o, RSTRING_PTR(v), RSTRING_LEN(v), 0);
|
|
202
|
+
RB_GC_GUARD(v);
|
|
203
|
+
return 1;
|
|
204
|
+
}
|
|
205
|
+
if (SYMBOL_P(v)) {
|
|
206
|
+
VALUE s = rb_sym2str(v);
|
|
207
|
+
if (!compatible_string(s)) return 0;
|
|
208
|
+
put_value(o, RSTRING_PTR(s), RSTRING_LEN(s), 1);
|
|
209
|
+
RB_GC_GUARD(s);
|
|
210
|
+
return 1;
|
|
211
|
+
}
|
|
212
|
+
return put_number(o, v);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// generate_nested_tokens, written from `start`. Emptiness is relative to `start`.
|
|
216
|
+
static int put_tokens(output_t *o, VALUE tokens, long start) {
|
|
217
|
+
if (++o->depth > MAX_DEPTH) return 0;
|
|
218
|
+
|
|
219
|
+
for (long i = 0, length = RARRAY_LEN(tokens); i < length; i++) {
|
|
220
|
+
VALUE token = rb_ary_entry(tokens, i);
|
|
221
|
+
if (!RTEST(token)) continue;
|
|
222
|
+
|
|
223
|
+
if (plain_array_p(token)) {
|
|
224
|
+
if (RARRAY_LEN(token) == 0) continue;
|
|
225
|
+
long mark = o->length;
|
|
226
|
+
if (mark > start) put_char(o, ' ');
|
|
227
|
+
long inner = o->length;
|
|
228
|
+
if (!put_tokens(o, token, inner)) return 0;
|
|
229
|
+
if (o->length == inner) o->length = mark;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (o->length > start) put_char(o, ' ');
|
|
234
|
+
if (!put_scalar(o, token)) return 0;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
o->depth--;
|
|
238
|
+
return 1;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static int put_nested(output_t *o, VALUE hash, long prefix_length);
|
|
242
|
+
|
|
243
|
+
static int nested_each(VALUE k, VALUE v, VALUE arg) {
|
|
244
|
+
nested_t *nested = (nested_t *)arg;
|
|
245
|
+
output_t *o = nested->output;
|
|
246
|
+
if (!RTEST(v)) return ST_CONTINUE;
|
|
247
|
+
|
|
248
|
+
long length;
|
|
249
|
+
if (k == sym_underscore) {
|
|
250
|
+
length = nested->prefix_length - 1;
|
|
251
|
+
} else {
|
|
252
|
+
const char *p;
|
|
253
|
+
long n;
|
|
254
|
+
int dasherize;
|
|
255
|
+
if (!key_bytes(k, &p, &n, &dasherize)) goto fallback;
|
|
256
|
+
if (n > PREFIX_CAPACITY - 1 - nested->prefix_length) goto fallback;
|
|
257
|
+
char *name = o->prefix + nested->prefix_length;
|
|
258
|
+
for (long i = 0; i < n; i++) {
|
|
259
|
+
if (unsafe_name_char_p(p[i])) goto fallback;
|
|
260
|
+
name[i] = (dasherize && p[i] == '_') ? '-' : p[i];
|
|
261
|
+
}
|
|
262
|
+
length = nested->prefix_length + n;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (v == Qtrue) {
|
|
266
|
+
put_char(o, ' ');
|
|
267
|
+
put(o, o->prefix, length);
|
|
268
|
+
} else if (plain_hash_p(v)) {
|
|
269
|
+
o->prefix[length] = '-';
|
|
270
|
+
if (!put_nested(o, v, length + 1)) goto fallback;
|
|
271
|
+
} else if (plain_array_p(v)) {
|
|
272
|
+
long mark = o->length;
|
|
273
|
+
put_char(o, ' ');
|
|
274
|
+
put(o, o->prefix, length);
|
|
275
|
+
PUT_LITERAL(o, "=\"");
|
|
276
|
+
long start = o->length;
|
|
277
|
+
if (!put_tokens(o, v, start)) goto fallback;
|
|
278
|
+
if (o->length == start) o->length = mark;
|
|
279
|
+
else put_char(o, '"');
|
|
280
|
+
} else {
|
|
281
|
+
put_char(o, ' ');
|
|
282
|
+
put(o, o->prefix, length);
|
|
283
|
+
PUT_LITERAL(o, "=\"");
|
|
284
|
+
if (!put_scalar(o, v)) goto fallback;
|
|
285
|
+
put_char(o, '"');
|
|
286
|
+
}
|
|
287
|
+
return ST_CONTINUE;
|
|
288
|
+
|
|
289
|
+
fallback:
|
|
290
|
+
nested->fallback = 1;
|
|
291
|
+
return ST_STOP;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
static int put_nested(output_t *o, VALUE hash, long prefix_length) {
|
|
295
|
+
if (++o->depth > MAX_DEPTH) return 0;
|
|
296
|
+
nested_t nested = { o, prefix_length, 0 };
|
|
297
|
+
rb_hash_foreach(hash, nested_each, (VALUE)&nested);
|
|
298
|
+
o->depth--;
|
|
299
|
+
return !nested.fallback;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
typedef struct {
|
|
303
|
+
output_t *output;
|
|
304
|
+
long start;
|
|
305
|
+
int fallback;
|
|
306
|
+
} styles_t;
|
|
307
|
+
|
|
308
|
+
// One `property: value;` from generate_styles with a Hash. Ruby checks the
|
|
309
|
+
// key before it skips a nil value, so the key is checked first here too.
|
|
310
|
+
static int style_each(VALUE k, VALUE v, VALUE arg) {
|
|
311
|
+
styles_t *styles = (styles_t *)arg;
|
|
312
|
+
output_t *o = styles->output;
|
|
313
|
+
|
|
314
|
+
VALUE property;
|
|
315
|
+
int dasherize;
|
|
316
|
+
if (SYMBOL_P(k)) {
|
|
317
|
+
property = rb_sym2str(k);
|
|
318
|
+
dasherize = 1;
|
|
319
|
+
} else if (plain_string_p(k)) {
|
|
320
|
+
property = k;
|
|
321
|
+
dasherize = 0;
|
|
322
|
+
} else {
|
|
323
|
+
goto fallback;
|
|
324
|
+
}
|
|
325
|
+
if (!compatible_string(property)) goto fallback;
|
|
326
|
+
|
|
327
|
+
if (!RTEST(v)) return ST_CONTINUE;
|
|
328
|
+
|
|
329
|
+
if (o->length > styles->start) put_char(o, ' ');
|
|
330
|
+
put_value(o, RSTRING_PTR(property), RSTRING_LEN(property), dasherize);
|
|
331
|
+
RB_GC_GUARD(property);
|
|
332
|
+
PUT_LITERAL(o, ": ");
|
|
333
|
+
if (!put_scalar(o, v)) goto fallback;
|
|
334
|
+
put_char(o, ';');
|
|
335
|
+
return ST_CONTINUE;
|
|
336
|
+
|
|
337
|
+
fallback:
|
|
338
|
+
styles->fallback = 1;
|
|
339
|
+
return ST_STOP;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// generate_styles with a Hash, written from `start`.
|
|
343
|
+
static int put_style_hash(output_t *o, VALUE hash, long start) {
|
|
344
|
+
styles_t styles = { o, start, 0 };
|
|
345
|
+
rb_hash_foreach(hash, style_each, (VALUE)&styles);
|
|
346
|
+
return !styles.fallback;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// A String or safe value from a style Array, with `;` added unless it ends with one.
|
|
350
|
+
static void put_declaration(output_t *o, VALUE declaration, long start) {
|
|
351
|
+
const char *p = RSTRING_PTR(declaration);
|
|
352
|
+
long n = RSTRING_LEN(declaration);
|
|
353
|
+
int terminated = n > 0 && p[n - 1] == ';';
|
|
354
|
+
|
|
355
|
+
if (o->length > start) put_char(o, ' ');
|
|
356
|
+
put_value(o, p, n, 0);
|
|
357
|
+
RB_GC_GUARD(declaration);
|
|
358
|
+
if (!terminated) put_char(o, ';');
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// generate_styles with an Array, written from `start`. Like Array#each, the
|
|
362
|
+
// length is read on every iteration.
|
|
363
|
+
static int put_style_array(output_t *o, VALUE declarations, long start) {
|
|
364
|
+
for (long i = 0; i < RARRAY_LEN(declarations); i++) {
|
|
365
|
+
VALUE declaration = RARRAY_AREF(declarations, i);
|
|
366
|
+
if (!RTEST(declaration)) continue;
|
|
367
|
+
|
|
368
|
+
if (plain_string_p(declaration)) {
|
|
369
|
+
if (!compatible_string(declaration)) return 0;
|
|
370
|
+
if (RSTRING_LEN(declaration) == 0) continue;
|
|
371
|
+
put_declaration(o, declaration, start);
|
|
372
|
+
} else if (safe_value_p(declaration)) {
|
|
373
|
+
VALUE value = rb_funcall(declaration, id_to_s, 0);
|
|
374
|
+
if (!plain_string_p(value) || !compatible_string(value)) return 0;
|
|
375
|
+
put_declaration(o, value, start);
|
|
376
|
+
} else if (plain_hash_p(declaration)) {
|
|
377
|
+
long mark = o->length;
|
|
378
|
+
if (mark > start) put_char(o, ' ');
|
|
379
|
+
long inner = o->length;
|
|
380
|
+
if (!put_style_hash(o, declaration, inner)) return 0;
|
|
381
|
+
if (o->length == inner) o->length = mark;
|
|
382
|
+
} else {
|
|
383
|
+
return 0;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return 1;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
static int ref_attribute_p(const char *p, long n) {
|
|
390
|
+
switch (n) {
|
|
391
|
+
case 3: return memcmp(p, "src", 3) == 0;
|
|
392
|
+
case 4: return memcmp(p, "href", 4) == 0 || memcmp(p, "ping", 4) == 0;
|
|
393
|
+
case 6: return memcmp(p, "action", 6) == 0 || memcmp(p, "lowsrc", 6) == 0 || memcmp(p, "dynsrc", 6) == 0;
|
|
394
|
+
case 9: return memcmp(p, "xlinkhref", 9) == 0;
|
|
395
|
+
case 10: return memcmp(p, "formaction", 10) == 0 || memcmp(p, "background", 10) == 0;
|
|
396
|
+
default: return 0;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
static int unsafe_attribute_p(const char *p, long n) {
|
|
401
|
+
switch (n) {
|
|
402
|
+
case 6: return memcmp(p, "srcdoc", 6) == 0;
|
|
403
|
+
case 7: return memcmp(p, "sandbox", 7) == 0;
|
|
404
|
+
case 10: return memcmp(p, "http-equiv", 10) == 0;
|
|
405
|
+
default: return 0;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// unsafe_reference? on the value written from `start`. The cheap check is the
|
|
410
|
+
// same one the Ruby version makes; anything it can't rule out goes to Ruby.
|
|
411
|
+
static int unsafe_reference_p(output_t *o, long start) {
|
|
412
|
+
const char *p = o->ptr + start;
|
|
413
|
+
long n = o->length - start;
|
|
414
|
+
if (!memchr(p, '&', n) && !(memchr(p, ':', n) && (memchr(p, 'j', n) || memchr(p, 'J', n)))) return 0;
|
|
415
|
+
return RTEST(rb_funcall(o->receiver, id_unsafe_reference_p, 1, rb_utf8_str_new(p, n)));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// One attribute whose name has passed the top-level checks.
|
|
419
|
+
static int put_attribute(output_t *o, VALUE k, const char *name, long name_length, VALUE v, int reference) {
|
|
420
|
+
if (reference && safe_value_p(v)) reference = 0;
|
|
421
|
+
|
|
422
|
+
if (v == Qtrue) {
|
|
423
|
+
put_char(o, ' ');
|
|
424
|
+
put(o, name, name_length);
|
|
425
|
+
return 1;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
int hash = plain_hash_p(v);
|
|
429
|
+
|
|
430
|
+
if (k == sym_style && !reference && (hash || plain_array_p(v))) {
|
|
431
|
+
long mark = o->length;
|
|
432
|
+
put_char(o, ' ');
|
|
433
|
+
put(o, name, name_length);
|
|
434
|
+
PUT_LITERAL(o, "=\"");
|
|
435
|
+
long start = o->length;
|
|
436
|
+
if (!(hash ? put_style_hash(o, v, start) : put_style_array(o, v, start))) return 0;
|
|
437
|
+
// generate_styles returns nil when there are no declarations.
|
|
438
|
+
if (o->length == start) o->length = mark;
|
|
439
|
+
else put_char(o, '"');
|
|
440
|
+
return 1;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (hash) {
|
|
444
|
+
if (k == sym_style || reference || name_length + 1 > PREFIX_CAPACITY) return 0;
|
|
445
|
+
memcpy(o->prefix, name, name_length);
|
|
446
|
+
o->prefix[name_length] = '-';
|
|
447
|
+
return put_nested(o, v, name_length + 1);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
long mark = o->length;
|
|
451
|
+
put_char(o, ' ');
|
|
452
|
+
put(o, name, name_length);
|
|
453
|
+
PUT_LITERAL(o, "=\"");
|
|
454
|
+
long start = o->length;
|
|
455
|
+
|
|
456
|
+
if (plain_array_p(v)) {
|
|
457
|
+
if (k == sym_style || !put_tokens(o, v, start)) return 0;
|
|
458
|
+
if (o->length == start) {
|
|
459
|
+
// drop_reference? raises on the nil an empty token list produces.
|
|
460
|
+
if (reference) return 0;
|
|
461
|
+
o->length = mark;
|
|
462
|
+
return 1;
|
|
463
|
+
}
|
|
464
|
+
} else if (!put_scalar(o, v)) {
|
|
465
|
+
return 0;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (reference && unsafe_reference_p(o, start)) {
|
|
469
|
+
// Safe objects are never dropped, and anything can be made one.
|
|
470
|
+
if (RTEST(rb_obj_is_kind_of(v, safe_object_module))) return 0;
|
|
471
|
+
o->length = mark;
|
|
472
|
+
return 1;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
put_char(o, '"');
|
|
476
|
+
return 1;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
static int top_level_each(VALUE k, VALUE v, VALUE arg) {
|
|
480
|
+
nested_t *top = (nested_t *)arg;
|
|
481
|
+
output_t *o = top->output;
|
|
482
|
+
if (!RTEST(v)) return ST_CONTINUE;
|
|
483
|
+
|
|
484
|
+
const char *p;
|
|
485
|
+
long n;
|
|
486
|
+
int dasherize;
|
|
487
|
+
if (!key_bytes(k, &p, &n, &dasherize)) goto fallback;
|
|
488
|
+
|
|
489
|
+
// The name is copied out of the key so it can't move while the value is written.
|
|
490
|
+
char stack_name[256];
|
|
491
|
+
if (n > (long)sizeof stack_name) goto fallback;
|
|
492
|
+
char *name = stack_name;
|
|
493
|
+
|
|
494
|
+
char normalized[NORMALIZED_CAPACITY];
|
|
495
|
+
long normalized_length = 0;
|
|
496
|
+
int simple = n > 0, unsafe_char = 0, has_dash = 0;
|
|
497
|
+
|
|
498
|
+
for (long i = 0; i < n; i++) {
|
|
499
|
+
unsigned char c = (dasherize && p[i] == '_') ? '-' : (unsigned char)p[i];
|
|
500
|
+
if (c >= 0x80) goto fallback;
|
|
501
|
+
name[i] = c;
|
|
502
|
+
if (!((c >= 'a' && c <= 'z') || c == '-')) simple = 0;
|
|
503
|
+
if (unsafe_name_char_p(c)) unsafe_char = 1;
|
|
504
|
+
unsigned char lower = (c >= 'A' && c <= 'Z') ? c + 32 : c;
|
|
505
|
+
if ((lower >= 'a' && lower <= 'z') || lower == '-') {
|
|
506
|
+
if (lower == '-') has_dash = 1;
|
|
507
|
+
if (normalized_length < NORMALIZED_CAPACITY) normalized[normalized_length] = lower;
|
|
508
|
+
normalized_length++;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Safe values may use event handler and other unsafe attribute names.
|
|
513
|
+
if (!safe_value_p(v)) {
|
|
514
|
+
if (normalized_length > 2 && normalized[0] == 'o' && normalized[1] == 'n' && !has_dash) goto fallback;
|
|
515
|
+
if (normalized_length <= NORMALIZED_CAPACITY && unsafe_attribute_p(normalized, normalized_length)) goto fallback;
|
|
516
|
+
}
|
|
517
|
+
if (!simple && unsafe_char) goto fallback;
|
|
518
|
+
if (n == 2 && (name[0] | 32) == 'i' && (name[1] | 32) == 'd' && k != sym_id) goto fallback;
|
|
519
|
+
|
|
520
|
+
int reference = normalized_length <= NORMALIZED_CAPACITY && ref_attribute_p(normalized, normalized_length);
|
|
521
|
+
if (!put_attribute(o, k, name, n, v, reference)) goto fallback;
|
|
522
|
+
return ST_CONTINUE;
|
|
523
|
+
|
|
524
|
+
fallback:
|
|
525
|
+
top->fallback = 1;
|
|
526
|
+
return ST_STOP;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// The serialised attributes, or Qundef to fall back.
|
|
530
|
+
static VALUE generate(VALUE receiver, VALUE attributes) {
|
|
531
|
+
if (!plain_hash_p(attributes)) return Qundef;
|
|
532
|
+
output_t o;
|
|
533
|
+
output_init(&o, receiver);
|
|
534
|
+
nested_t top = { &o, 0, 0 };
|
|
535
|
+
rb_hash_foreach(attributes, top_level_each, (VALUE)&top);
|
|
536
|
+
return top.fallback ? Qundef : output_finish(&o);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Phlex::Native::SerializedAttributes.fetch(attributes) { generate in Ruby }
|
|
540
|
+
// generate_attributes itself isn't prepended, so a fallback doesn't run the C twice.
|
|
541
|
+
static VALUE native_fetch(VALUE self, VALUE attributes) {
|
|
542
|
+
// The block loads Phlex::SGML::Attributes, which prepends Phlex::Native::Attributes.
|
|
543
|
+
if (NIL_P(attributes_module)) return rb_yield_values(0);
|
|
544
|
+
|
|
545
|
+
VALUE result = generate(attributes_module, attributes);
|
|
546
|
+
return result == Qundef ? rb_yield_values(0) : result;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Phlex::Native::Attributes.prepended(base), called once when attributes.rb
|
|
550
|
+
// loads, so the constants are set before anything reads them and no Ractor
|
|
551
|
+
// ever writes them.
|
|
552
|
+
static VALUE native_prepended(VALUE self, VALUE base) {
|
|
553
|
+
VALUE sgml = rb_const_get(rb_const_get(rb_cObject, id_Phlex), id_SGML);
|
|
554
|
+
safe_object_module = rb_const_get(sgml, id_SafeObject);
|
|
555
|
+
safe_value_class = rb_const_get(sgml, id_SafeValue);
|
|
556
|
+
rb_gc_register_mark_object(safe_object_module);
|
|
557
|
+
rb_gc_register_mark_object(safe_value_class);
|
|
558
|
+
|
|
559
|
+
VALUE attributes = rb_const_get(sgml, id_Attributes);
|
|
560
|
+
rb_gc_register_mark_object(attributes);
|
|
561
|
+
attributes_module = attributes;
|
|
562
|
+
|
|
563
|
+
return rb_call_super(1, &base);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
static VALUE serialize_validated(VALUE self, int argc, VALUE *argv, int reference) {
|
|
567
|
+
rb_check_arity(argc, 3, 3);
|
|
568
|
+
VALUE k = argv[0], name = argv[1], v = argv[2];
|
|
569
|
+
if (!RTEST(v)) return empty_string;
|
|
570
|
+
if (!plain_string_p(name) || !compatible_string(name)) return rb_call_super(argc, argv);
|
|
571
|
+
|
|
572
|
+
output_t o;
|
|
573
|
+
output_init(&o, self);
|
|
574
|
+
char stack_name[256];
|
|
575
|
+
long n = RSTRING_LEN(name);
|
|
576
|
+
if (n > (long)sizeof stack_name) return rb_call_super(argc, argv);
|
|
577
|
+
memcpy(stack_name, RSTRING_PTR(name), n);
|
|
578
|
+
|
|
579
|
+
if (!put_attribute(&o, k, stack_name, n, v, reference)) return rb_call_super(argc, argv);
|
|
580
|
+
// A dropped reference returns the literal "" in Ruby.
|
|
581
|
+
if (reference && o.length == 0) return empty_string;
|
|
582
|
+
return output_finish(&o);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// Phlex::SGML::Attributes.attribute(k, name, v)
|
|
586
|
+
static VALUE native_attribute(int argc, VALUE *argv, VALUE self) {
|
|
587
|
+
return serialize_validated(self, argc, argv, 0);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Phlex::SGML::Attributes.reference_attribute(k, name, v)
|
|
591
|
+
static VALUE native_reference_attribute(int argc, VALUE *argv, VALUE self) {
|
|
592
|
+
return serialize_validated(self, argc, argv, 1);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
void Init_phlex_native(void) {
|
|
596
|
+
rb_ext_ractor_safe(true);
|
|
597
|
+
|
|
598
|
+
id_to_s = rb_intern("to_s");
|
|
599
|
+
id_unsafe_reference_p = rb_intern("unsafe_reference?");
|
|
600
|
+
id_Phlex = rb_intern("Phlex");
|
|
601
|
+
id_SGML = rb_intern("SGML");
|
|
602
|
+
id_Attributes = rb_intern("Attributes");
|
|
603
|
+
id_SafeObject = rb_intern("SafeObject");
|
|
604
|
+
id_SafeValue = rb_intern("SafeValue");
|
|
605
|
+
sym_style = ID2SYM(rb_intern("style"));
|
|
606
|
+
sym_id = ID2SYM(rb_intern("id"));
|
|
607
|
+
sym_underscore = ID2SYM(rb_intern("_"));
|
|
608
|
+
utf8_index = rb_utf8_encindex();
|
|
609
|
+
usascii_index = rb_usascii_encindex();
|
|
610
|
+
|
|
611
|
+
empty_string = rb_ractor_make_shareable(rb_utf8_str_new("", 0));
|
|
612
|
+
rb_gc_register_mark_object(empty_string);
|
|
613
|
+
|
|
614
|
+
VALUE mNative = rb_define_module_under(rb_define_module("Phlex"), "Native");
|
|
615
|
+
|
|
616
|
+
// Prepended to Phlex::SGML::Attributes' singleton class.
|
|
617
|
+
VALUE mNativeAttributes = rb_define_module_under(mNative, "Attributes");
|
|
618
|
+
rb_define_method(mNativeAttributes, "attribute", native_attribute, -1);
|
|
619
|
+
rb_define_method(mNativeAttributes, "reference_attribute", native_reference_attribute, -1);
|
|
620
|
+
rb_define_singleton_method(mNativeAttributes, "prepended", native_prepended, 1);
|
|
621
|
+
|
|
622
|
+
VALUE mSerializedAttributes = rb_define_module_under(mNative, "SerializedAttributes");
|
|
623
|
+
rb_define_singleton_method(mSerializedAttributes, "fetch", native_fetch, 1);
|
|
624
|
+
}
|
data/lib/phlex-native.rb
ADDED
|
Binary file
|
data/lib/phlex_native.rb
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A gem precompiled for several Ruby versions has a binary for each; otherwise there's just one.
|
|
4
|
+
begin
|
|
5
|
+
require "phlex_native/#{RUBY_VERSION[/\A\d+\.\d+/]}/phlex_native"
|
|
6
|
+
rescue LoadError
|
|
7
|
+
require "phlex_native/phlex_native"
|
|
8
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: phlex-native
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: aarch64-linux-gnu
|
|
6
|
+
authors:
|
|
7
|
+
- Joel Drapper
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: phlex
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - '='
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.4.1
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - '='
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 2.4.1
|
|
26
|
+
description: Serialises Phlex attributes in C, falling back to Ruby for anything uncommon.
|
|
27
|
+
email:
|
|
28
|
+
- joel@drapper.me
|
|
29
|
+
executables: []
|
|
30
|
+
extensions: []
|
|
31
|
+
extra_rdoc_files: []
|
|
32
|
+
files:
|
|
33
|
+
- README.md
|
|
34
|
+
- ext/phlex_native/extconf.rb
|
|
35
|
+
- ext/phlex_native/phlex_native.c
|
|
36
|
+
- lib/phlex-native.rb
|
|
37
|
+
- lib/phlex_native.rb
|
|
38
|
+
- lib/phlex_native/phlex_native.so
|
|
39
|
+
homepage: https://www.phlex.fun
|
|
40
|
+
licenses:
|
|
41
|
+
- MIT
|
|
42
|
+
metadata:
|
|
43
|
+
source_code_uri: https://github.com/phlex-ruby/phlex
|
|
44
|
+
rubygems_mfa_required: 'true'
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '4.0'
|
|
53
|
+
- - "<"
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: 4.1.dev
|
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 3.3.22
|
|
61
|
+
requirements: []
|
|
62
|
+
rubygems_version: 4.0.6
|
|
63
|
+
specification_version: 4
|
|
64
|
+
summary: A native extension that speeds up Phlex.
|
|
65
|
+
test_files: []
|