rbs 4.1.0.pre.1 → 4.1.0

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 (130) hide show
  1. checksums.yaml +4 -4
  2. data/.dockerignore +37 -0
  3. data/.github/workflows/bundle-update.yml +1 -1
  4. data/.github/workflows/c-check.yml +14 -6
  5. data/.github/workflows/comments.yml +2 -2
  6. data/.github/workflows/dependabot.yml +2 -2
  7. data/.github/workflows/jruby.yml +79 -0
  8. data/.github/workflows/milestone.yml +13 -2
  9. data/.github/workflows/ruby.yml +16 -4
  10. data/.github/workflows/rust.yml +13 -8
  11. data/.github/workflows/truffleruby.yml +54 -0
  12. data/.github/workflows/typecheck.yml +5 -2
  13. data/.github/workflows/wasm.yml +55 -0
  14. data/.github/workflows/windows.yml +8 -2
  15. data/.gitignore +8 -0
  16. data/CHANGELOG.md +88 -0
  17. data/Dockerfile.jruby +53 -0
  18. data/README.md +3 -3
  19. data/Rakefile +103 -1
  20. data/Steepfile +9 -0
  21. data/core/array.rbs +243 -180
  22. data/core/builtin.rbs +6 -6
  23. data/core/enumerable.rbs +109 -109
  24. data/core/enumerator/product.rbs +5 -5
  25. data/core/enumerator.rbs +28 -28
  26. data/core/file.rbs +24 -1018
  27. data/core/file_constants.rbs +463 -0
  28. data/core/file_stat.rbs +534 -0
  29. data/core/float.rbs +0 -24
  30. data/core/hash.rbs +117 -101
  31. data/core/integer.rbs +21 -58
  32. data/core/io.rbs +25 -7
  33. data/core/match_data.rbs +1 -1
  34. data/core/module.rbs +88 -74
  35. data/core/numeric.rbs +3 -0
  36. data/core/object_space/weak_key_map.rbs +7 -7
  37. data/core/pathname.rbs +0 -10
  38. data/core/ractor.rbs +0 -10
  39. data/core/range.rbs +23 -23
  40. data/core/rbs/ops.rbs +154 -0
  41. data/core/rbs/unnamed/argf.rbs +3 -3
  42. data/core/rubygems/errors.rbs +4 -1
  43. data/core/rubygems/requirement.rbs +0 -10
  44. data/core/rubygems/rubygems.rbs +4 -1
  45. data/core/rubygems/specification.rbs +8 -0
  46. data/core/rubygems/version.rbs +0 -160
  47. data/core/set.rbs +3 -3
  48. data/core/struct.rbs +16 -16
  49. data/core/thread.rbs +9 -14
  50. data/docs/CONTRIBUTING.md +2 -1
  51. data/docs/inline.md +36 -6
  52. data/docs/rbs_by_example.md +20 -20
  53. data/docs/release.md +69 -0
  54. data/docs/syntax.md +2 -2
  55. data/docs/wasm_serialization.md +80 -0
  56. data/ext/rbs_extension/ast_translation.c +1294 -973
  57. data/ext/rbs_extension/ast_translation.h +4 -0
  58. data/ext/rbs_extension/legacy_location.c +11 -6
  59. data/ext/rbs_extension/main.c +139 -4
  60. data/include/rbs/ast.h +9 -1
  61. data/include/rbs/serialize.h +39 -0
  62. data/lib/rbs/ast/ruby/comment_block.rb +6 -4
  63. data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
  64. data/lib/rbs/ast/ruby/members.rb +12 -1
  65. data/lib/rbs/buffer.rb +48 -11
  66. data/lib/rbs/collection/sources/git.rb +6 -0
  67. data/lib/rbs/definition_builder/method_builder.rb +12 -6
  68. data/lib/rbs/environment.rb +4 -3
  69. data/lib/rbs/inline_parser.rb +5 -3
  70. data/lib/rbs/namespace.rb +47 -11
  71. data/lib/rbs/parser_aux.rb +4 -2
  72. data/lib/rbs/prototype/rbi.rb +193 -25
  73. data/lib/rbs/prototype/runtime.rb +2 -0
  74. data/lib/rbs/resolver/type_name_resolver.rb +12 -14
  75. data/lib/rbs/type_name.rb +33 -13
  76. data/lib/rbs/unit_test/type_assertions.rb +9 -0
  77. data/lib/rbs/version.rb +1 -1
  78. data/lib/rbs/wasm/deserializer.rb +213 -0
  79. data/lib/rbs/wasm/location.rb +61 -0
  80. data/lib/rbs/wasm/parser.rb +137 -0
  81. data/lib/rbs/wasm/runtime.rb +196 -0
  82. data/lib/rbs/wasm/serialization_schema.rb +110 -0
  83. data/lib/rbs.rb +13 -2
  84. data/lib/rbs_jars.rb +39 -0
  85. data/lib/rdoc_plugin/parser.rb +5 -0
  86. data/rbs.gemspec +32 -2
  87. data/sig/ast/ruby/members.rbs +6 -1
  88. data/sig/buffer.rbs +19 -1
  89. data/sig/namespace.rbs +20 -0
  90. data/sig/parser.rbs +10 -0
  91. data/sig/prototype/rbi.rbs +33 -4
  92. data/sig/resolver/type_name_resolver.rbs +2 -4
  93. data/sig/typename.rbs +15 -0
  94. data/sig/unit_test/type_assertions.rbs +4 -0
  95. data/sig/wasm/deserializer.rbs +66 -0
  96. data/sig/wasm/serialization_schema.rbs +13 -0
  97. data/src/ast.c +78 -78
  98. data/src/lexer.c +97 -93
  99. data/src/lexer.re +1 -1
  100. data/src/lexstate.c +9 -1
  101. data/src/serialize.c +958 -0
  102. data/src/util/rbs_allocator.c +14 -8
  103. data/stdlib/abbrev/0/array.rbs +1 -1
  104. data/stdlib/csv/0/csv.rbs +5 -5
  105. data/stdlib/delegate/0/delegator.rbs +2 -1
  106. data/stdlib/digest/0/digest.rbs +11 -5
  107. data/stdlib/erb/0/erb.rbs +1 -1
  108. data/stdlib/etc/0/etc.rbs +18 -4
  109. data/stdlib/fileutils/0/fileutils.rbs +21 -21
  110. data/stdlib/ipaddr/0/ipaddr.rbs +0 -5
  111. data/stdlib/json/0/json.rbs +6 -6
  112. data/stdlib/monitor/0/monitor.rbs +2 -2
  113. data/stdlib/openssl/0/openssl.rbs +44 -38
  114. data/stdlib/resolv/0/resolv.rbs +1 -1
  115. data/stdlib/shellwords/0/shellwords.rbs +1 -1
  116. data/stdlib/stringio/0/stringio.rbs +32 -10
  117. data/stdlib/strscan/0/string_scanner.rbs +74 -55
  118. data/stdlib/tempfile/0/manifest.yaml +3 -0
  119. data/stdlib/timeout/0/timeout.rbs +0 -5
  120. data/stdlib/tsort/0/cyclic.rbs +1 -1
  121. data/stdlib/tsort/0/interfaces.rbs +8 -8
  122. data/stdlib/tsort/0/tsort.rbs +9 -9
  123. data/stdlib/uri/0/generic.rbs +0 -5
  124. data/stdlib/zlib/0/gzip_reader.rbs +2 -2
  125. data/stdlib/zlib/0/zstream.rbs +0 -1
  126. data/wasm/README.md +60 -0
  127. data/wasm/rbs_wasm.c +423 -0
  128. metadata +26 -5
  129. data/.vscode/extensions.json +0 -5
  130. data/.vscode/settings.json +0 -19
data/wasm/rbs_wasm.c ADDED
@@ -0,0 +1,423 @@
1
+ /**
2
+ * @file rbs_wasm.c
3
+ *
4
+ * WebAssembly entry points for the RBS parser.
5
+ *
6
+ * The parser in `src/` is plain, self-contained C with no dependency on the
7
+ * Ruby C API, so it compiles to WebAssembly as-is. This file exposes a small,
8
+ * stable ABI so the parser can be driven from a WebAssembly host (a JVM-based
9
+ * runtime running under JRuby).
10
+ *
11
+ * The flow is: the host writes a UTF-8 source string into linear memory
12
+ * (`rbs_wasm_alloc`), calls one of the `rbs_wasm_parse_*` functions, and reads
13
+ * the result back out (`rbs_wasm_result_ptr` / `rbs_wasm_result_len`). On
14
+ * success the result is the serialized AST (see `rbs_serialize_node` and
15
+ * `docs/wasm_serialization.md`); on a parse error it is an error blob (see
16
+ * `set_error_result`). `RBS::WASM` on the Ruby side decodes both.
17
+ *
18
+ * Built as a "reactor": no `main`, and the host calls `_initialize` once before
19
+ * invoking any export.
20
+ */
21
+
22
+ #include <stdint.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+
26
+ #include "rbs/parser.h"
27
+ #include "rbs/serialize.h"
28
+ #include "rbs/string.h"
29
+ #include "rbs/util/rbs_buffer.h"
30
+ #include "rbs/util/rbs_encoding.h"
31
+
32
+ // The result of the most recent parse, living in linear memory until the next
33
+ // call replaces it. WebAssembly is little-endian, so the multi-byte integers
34
+ // written below match the little-endian format the Ruby decoder expects.
35
+ static char *result_buffer = NULL;
36
+ static int32_t result_length = 0;
37
+
38
+ // Replace the current result with a fresh `length`-byte buffer and return a
39
+ // pointer to it for the caller to fill in.
40
+ static char *allocate_result(size_t length) {
41
+ free(result_buffer);
42
+ result_buffer = (char *) malloc(length == 0 ? 1 : length);
43
+ result_length = (int32_t) length;
44
+ return result_buffer;
45
+ }
46
+
47
+ /**
48
+ * Allocate `size` bytes in linear memory and return the offset. The host uses
49
+ * this to reserve space for an input string before calling a parse function.
50
+ */
51
+ __attribute__((export_name("rbs_wasm_alloc"))) void *rbs_wasm_alloc(size_t size) {
52
+ return malloc(size);
53
+ }
54
+
55
+ /**
56
+ * Free a region previously returned by `rbs_wasm_alloc`.
57
+ */
58
+ __attribute__((export_name("rbs_wasm_free"))) void rbs_wasm_free(void *ptr) {
59
+ free(ptr);
60
+ }
61
+
62
+ /**
63
+ * Offset of the most recent parse result in linear memory.
64
+ */
65
+ __attribute__((export_name("rbs_wasm_result_ptr")))
66
+ int32_t
67
+ rbs_wasm_result_ptr(void) {
68
+ return (int32_t) (intptr_t) result_buffer;
69
+ }
70
+
71
+ /**
72
+ * Length, in bytes, of the most recent parse result.
73
+ */
74
+ __attribute__((export_name("rbs_wasm_result_len")))
75
+ int32_t
76
+ rbs_wasm_result_len(void) {
77
+ return result_length;
78
+ }
79
+
80
+ // Encode the parser's error into the result buffer:
81
+ //
82
+ // [i32 start_char][i32 end_char][u8 syntax_error]
83
+ // [u32 token_type_len][token_type bytes][u32 message_len][message bytes]
84
+ //
85
+ // Always returns 0, the failure status for the parse functions.
86
+ static int set_error_result(rbs_parser_t *parser) {
87
+ rbs_error_t *error = parser->error;
88
+ const char *token_type = rbs_token_type_str(error->token.type);
89
+ const char *message = error->message;
90
+ uint32_t token_type_len = (uint32_t) strlen(token_type);
91
+ uint32_t message_len = (uint32_t) strlen(message);
92
+
93
+ int32_t start_char = error->token.range.start.char_pos;
94
+ int32_t end_char = error->token.range.end.char_pos;
95
+ uint8_t syntax_error = error->syntax_error ? 1 : 0;
96
+
97
+ size_t total = 4 + 4 + 1 + 4 + token_type_len + 4 + message_len;
98
+ char *p = allocate_result(total);
99
+
100
+ memcpy(p, &start_char, 4);
101
+ p += 4;
102
+ memcpy(p, &end_char, 4);
103
+ p += 4;
104
+ *p++ = (char) syntax_error;
105
+ memcpy(p, &token_type_len, 4);
106
+ p += 4;
107
+ memcpy(p, token_type, token_type_len);
108
+ p += token_type_len;
109
+ memcpy(p, &message_len, 4);
110
+ p += 4;
111
+ memcpy(p, message, message_len);
112
+
113
+ return 0;
114
+ }
115
+
116
+ static int set_serialized_result(rbs_parser_t *parser, rbs_node_t *node) {
117
+ rbs_string_t bytes = rbs_serialize_node(parser->allocator, &parser->constant_pool, node);
118
+ size_t length = rbs_string_len(bytes);
119
+ memcpy(allocate_result(length), bytes.start, length);
120
+ return 1;
121
+ }
122
+
123
+ // A reversed or out-of-bounds range would make the lexer loop forever, which
124
+ // would hang the whole host. Hosts are expected to validate too (RBS::Parser
125
+ // raises on bad ranges), but guard here so a stray caller can never wedge the VM.
126
+ static bool range_is_valid(int start_pos, int end_pos, int length) {
127
+ return start_pos >= 0 && end_pos >= 0 && start_pos <= end_pos && end_pos <= length;
128
+ }
129
+
130
+ // Resolve a Ruby encoding name (e.g. "UTF-8", "EUC-JP") to an rbs encoding,
131
+ // falling back to UTF-8 when none is given or the name is not recognised.
132
+ static const rbs_encoding_t *resolve_encoding(const char *name, int name_length) {
133
+ if (name_length > 0) {
134
+ const rbs_encoding_t *encoding = rbs_encoding_find((const uint8_t *) name, (const uint8_t *) (name + name_length));
135
+ if (encoding != NULL) return encoding;
136
+ }
137
+ return RBS_ENCODING_UTF_8_ENTRY;
138
+ }
139
+
140
+ // Declare type variables from a buffer of newline-separated names. A negative
141
+ // length means "no variables given" (the parser keeps its default table).
142
+ static void declare_variables(rbs_parser_t *parser, const char *variables, int variables_length) {
143
+ if (variables_length < 0) return;
144
+
145
+ rbs_parser_push_typevar_table(parser, true);
146
+
147
+ const char *cursor = variables;
148
+ const char *end = variables + variables_length;
149
+ const char *name_start = cursor;
150
+
151
+ while (cursor <= end) {
152
+ if (cursor == end || *cursor == '\n') {
153
+ size_t name_length = (size_t) (cursor - name_start);
154
+ if (name_length > 0) {
155
+ uint8_t *copied = (uint8_t *) malloc(name_length);
156
+ memcpy(copied, name_start, name_length);
157
+ rbs_constant_id_t id = rbs_constant_pool_insert_owned(&parser->constant_pool, copied, name_length);
158
+ (void) rbs_parser_insert_typevar(parser, id);
159
+ }
160
+ name_start = cursor + 1;
161
+ }
162
+ cursor++;
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Parse an RBS signature from a source buffer.
168
+ *
169
+ * `source`/`length` is the whole buffer content; `encoding`/`encoding_length` is
170
+ * its Ruby encoding name; `start_pos`/`end_pos` are the character range within it
171
+ * to parse, so reported locations are absolute (this mirrors
172
+ * RBS::Parser._parse_signature).
173
+ *
174
+ * @return 1 on success (result is the serialized AST), 0 on a parse error
175
+ * (result is an error blob).
176
+ */
177
+ __attribute__((export_name("rbs_wasm_parse_signature"))) int rbs_wasm_parse_signature(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos) {
178
+ if (!range_is_valid(start_pos, end_pos, length)) {
179
+ allocate_result(0);
180
+ return 0;
181
+ }
182
+
183
+ rbs_string_t string = rbs_string_new(source, source + length);
184
+ rbs_parser_t *parser = rbs_parser_new(string, resolve_encoding(encoding, encoding_length), start_pos, end_pos);
185
+
186
+ rbs_signature_t *signature = NULL;
187
+ rbs_parse_signature(parser, &signature);
188
+
189
+ int status;
190
+ if (parser->error == NULL) {
191
+ status = set_serialized_result(parser, (rbs_node_t *) signature);
192
+ } else {
193
+ status = set_error_result(parser);
194
+ }
195
+
196
+ rbs_parser_free(parser);
197
+ return status;
198
+ }
199
+
200
+ /**
201
+ * Parse a single RBS type.
202
+ *
203
+ * @param variables Newline-separated type variable names (length < 0 for none).
204
+ * @return 1 on success, 0 on a parse error. On success with an empty result
205
+ * (`rbs_wasm_result_len` == 0), the input was empty (`nil`).
206
+ */
207
+ __attribute__((export_name("rbs_wasm_parse_type"))) int rbs_wasm_parse_type(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos, const char *variables, int variables_length, int require_eof, int void_allowed, int self_allowed, int classish_allowed) {
208
+ if (!range_is_valid(start_pos, end_pos, length)) {
209
+ allocate_result(0);
210
+ return 0;
211
+ }
212
+
213
+ rbs_string_t string = rbs_string_new(source, source + length);
214
+ rbs_parser_t *parser = rbs_parser_new(string, resolve_encoding(encoding, encoding_length), start_pos, end_pos);
215
+ declare_variables(parser, variables, variables_length);
216
+
217
+ int status;
218
+ if (parser->next_token.type == pEOF) {
219
+ allocate_result(0);
220
+ status = 1;
221
+ } else {
222
+ rbs_node_t *type = NULL;
223
+ rbs_parse_type(parser, &type, void_allowed != 0, self_allowed != 0, classish_allowed != 0);
224
+
225
+ if (parser->error == NULL && require_eof) {
226
+ rbs_parser_advance(parser);
227
+ if (parser->current_token.type != pEOF) {
228
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
229
+ }
230
+ }
231
+
232
+ status = parser->error == NULL ? set_serialized_result(parser, type) : set_error_result(parser);
233
+ }
234
+
235
+ rbs_parser_free(parser);
236
+ return status;
237
+ }
238
+
239
+ /**
240
+ * Parse a single RBS method type.
241
+ *
242
+ * @param variables Newline-separated type variable names (length < 0 for none).
243
+ * @return 1 on success, 0 on a parse error. On success with an empty result,
244
+ * the input was empty (`nil`).
245
+ */
246
+ __attribute__((export_name("rbs_wasm_parse_method_type"))) int rbs_wasm_parse_method_type(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos, const char *variables, int variables_length, int require_eof) {
247
+ if (!range_is_valid(start_pos, end_pos, length)) {
248
+ allocate_result(0);
249
+ return 0;
250
+ }
251
+
252
+ rbs_string_t string = rbs_string_new(source, source + length);
253
+ rbs_parser_t *parser = rbs_parser_new(string, resolve_encoding(encoding, encoding_length), start_pos, end_pos);
254
+ declare_variables(parser, variables, variables_length);
255
+
256
+ int status;
257
+ if (parser->next_token.type == pEOF) {
258
+ allocate_result(0);
259
+ status = 1;
260
+ } else {
261
+ rbs_method_type_t *method_type = NULL;
262
+ rbs_parse_method_type(parser, &method_type, require_eof != 0, true);
263
+
264
+ status = parser->error == NULL ? set_serialized_result(parser, (rbs_node_t *) method_type) : set_error_result(parser);
265
+ }
266
+
267
+ rbs_parser_free(parser);
268
+ return status;
269
+ }
270
+
271
+ /**
272
+ * Parse a type parameter list (e.g. `[T < Comparable]`). On success the result
273
+ * is a serialized node list; an empty result means the input was empty (`nil`).
274
+ */
275
+ __attribute__((export_name("rbs_wasm_parse_type_params"))) int rbs_wasm_parse_type_params(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos, int module_type_params) {
276
+ if (!range_is_valid(start_pos, end_pos, length)) {
277
+ allocate_result(0);
278
+ return 0;
279
+ }
280
+
281
+ rbs_string_t string = rbs_string_new(source, source + length);
282
+ rbs_parser_t *parser = rbs_parser_new(string, resolve_encoding(encoding, encoding_length), start_pos, end_pos);
283
+
284
+ int status;
285
+ if (parser->next_token.type == pEOF) {
286
+ allocate_result(0);
287
+ status = 1;
288
+ } else {
289
+ rbs_node_list_t *params = NULL;
290
+ rbs_parse_type_params(parser, module_type_params != 0, &params);
291
+
292
+ if (parser->error == NULL) {
293
+ rbs_string_t bytes = rbs_serialize_node_list(parser->allocator, &parser->constant_pool, params);
294
+ size_t n = rbs_string_len(bytes);
295
+ memcpy(allocate_result(n), bytes.start, n);
296
+ status = 1;
297
+ } else {
298
+ status = set_error_result(parser);
299
+ }
300
+ }
301
+
302
+ rbs_parser_free(parser);
303
+ return status;
304
+ }
305
+
306
+ // Shared body for the leading/trailing inline annotation parsers.
307
+ static int parse_inline_annotation(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos, const char *variables, int variables_length, bool leading) {
308
+ if (!range_is_valid(start_pos, end_pos, length)) {
309
+ allocate_result(0);
310
+ return 0;
311
+ }
312
+
313
+ rbs_string_t string = rbs_string_new(source, source + length);
314
+ rbs_parser_t *parser = rbs_parser_new(string, resolve_encoding(encoding, encoding_length), start_pos, end_pos);
315
+ declare_variables(parser, variables, variables_length);
316
+
317
+ rbs_ast_ruby_annotations_t *annotation = NULL;
318
+ bool success = leading ? rbs_parse_inline_leading_annotation(parser, &annotation) : rbs_parse_inline_trailing_annotation(parser, &annotation);
319
+
320
+ int status;
321
+ if (parser->error != NULL) {
322
+ status = set_error_result(parser);
323
+ } else if (!success || annotation == NULL) {
324
+ allocate_result(0);
325
+ status = 1;
326
+ } else {
327
+ status = set_serialized_result(parser, (rbs_node_t *) annotation);
328
+ }
329
+
330
+ rbs_parser_free(parser);
331
+ return status;
332
+ }
333
+
334
+ /**
335
+ * Parse an inline leading annotation. On success the result is a serialized
336
+ * node; an empty result means there was no annotation (`nil`).
337
+ */
338
+ __attribute__((export_name("rbs_wasm_parse_inline_leading_annotation"))) int rbs_wasm_parse_inline_leading_annotation(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos, const char *variables, int variables_length) {
339
+ return parse_inline_annotation(source, length, encoding, encoding_length, start_pos, end_pos, variables, variables_length, true);
340
+ }
341
+
342
+ /**
343
+ * Parse an inline trailing annotation. See rbs_wasm_parse_inline_leading_annotation.
344
+ */
345
+ __attribute__((export_name("rbs_wasm_parse_inline_trailing_annotation"))) int rbs_wasm_parse_inline_trailing_annotation(const char *source, int length, const char *encoding, int encoding_length, int start_pos, int end_pos, const char *variables, int variables_length) {
346
+ return parse_inline_annotation(source, length, encoding, encoding_length, start_pos, end_pos, variables, variables_length, false);
347
+ }
348
+
349
+ static void w_lex_u32(rbs_allocator_t *allocator, rbs_buffer_t *buffer, uint32_t value) {
350
+ unsigned char bytes[4] = {
351
+ (unsigned char) (value & 0xff),
352
+ (unsigned char) ((value >> 8) & 0xff),
353
+ (unsigned char) ((value >> 16) & 0xff),
354
+ (unsigned char) ((value >> 24) & 0xff),
355
+ };
356
+ rbs_buffer_append_string(allocator, buffer, (const char *) bytes, 4);
357
+ }
358
+
359
+ /**
360
+ * Lex the source into tokens. The result is a sequence of records, with no
361
+ * leading count (the host reads until the buffer is exhausted):
362
+ *
363
+ * [u32 type_name_len][type_name bytes][i32 start_char][i32 end_char]
364
+ *
365
+ * The final token is always pEOF, mirroring RBS::Parser._lex.
366
+ *
367
+ * @return 1 always (lexing does not report parse errors here).
368
+ */
369
+ __attribute__((export_name("rbs_wasm_lex"))) int rbs_wasm_lex(const char *source, int length, const char *encoding, int encoding_length, int end_pos) {
370
+ rbs_allocator_t *allocator = rbs_allocator_init();
371
+ rbs_lexer_t *lexer = rbs_lexer_new(allocator, rbs_string_new(source, source + length), resolve_encoding(encoding, encoding_length), 0, end_pos);
372
+
373
+ rbs_buffer_t buffer;
374
+ rbs_buffer_init(allocator, &buffer);
375
+
376
+ rbs_token_t token = NullToken;
377
+ while (token.type != pEOF) {
378
+ token = rbs_lexer_next_token(lexer);
379
+
380
+ const char *type_name = rbs_token_type_str(token.type);
381
+ uint32_t type_name_length = (uint32_t) strlen(type_name);
382
+ w_lex_u32(allocator, &buffer, type_name_length);
383
+ rbs_buffer_append_string(allocator, &buffer, type_name, type_name_length);
384
+ w_lex_u32(allocator, &buffer, (uint32_t) token.range.start.char_pos);
385
+ w_lex_u32(allocator, &buffer, (uint32_t) token.range.end.char_pos);
386
+ }
387
+
388
+ rbs_string_t bytes = rbs_buffer_to_string(&buffer);
389
+ size_t n = rbs_string_len(bytes);
390
+ memcpy(allocate_result(n), bytes.start, n);
391
+
392
+ rbs_allocator_free(allocator);
393
+ return 1;
394
+ }
395
+
396
+ /**
397
+ * Parse a small, fixed RBS document, used as a build smoke test
398
+ * (`wasmtime run --invoke rbs_wasm_selftest rbs_parser.wasm`).
399
+ *
400
+ * @return 1 if the sample parsed successfully, 0 otherwise.
401
+ */
402
+ // Internal: defined in rbs_allocator.c, not declared in the public header.
403
+ extern size_t rbs_allocator_normalize_page_size(long raw);
404
+
405
+ __attribute__((export_name("rbs_wasm_selftest"))) int rbs_wasm_selftest(void) {
406
+ // Regression test: normalize_page_size must return a safe value
407
+ // (>= sizeof(rbs_allocator_page_t)) for inputs that would underflow
408
+ // payload_size. On WASI in a Rust cdylib, sysconf(_SC_PAGESIZE)
409
+ // returns 0; the normalization must catch that.
410
+ if (rbs_allocator_normalize_page_size(-1) != 4096) return 0;
411
+ if (rbs_allocator_normalize_page_size(0) != 4096) return 0;
412
+ if (rbs_allocator_normalize_page_size(1) != 4096) return 0;
413
+ if (rbs_allocator_normalize_page_size(65536) != 65536) return 0;
414
+
415
+ static const char source[] =
416
+ "class User\n"
417
+ " attr_reader name: String\n"
418
+ " def initialize: (String name) -> void\n"
419
+ "end\n";
420
+
421
+ int length = (int) (sizeof(source) - 1);
422
+ return rbs_wasm_parse_signature(source, length, "UTF-8", 5, 0, length);
423
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0.pre.1
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 1980-01-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: logger
@@ -63,23 +63,26 @@ extra_rdoc_files: []
63
63
  files:
64
64
  - ".clang-format"
65
65
  - ".clangd"
66
+ - ".dockerignore"
66
67
  - ".github/dependabot.yml"
67
68
  - ".github/workflows/bundle-update.yml"
68
69
  - ".github/workflows/c-check.yml"
69
70
  - ".github/workflows/comments.yml"
70
71
  - ".github/workflows/dependabot.yml"
72
+ - ".github/workflows/jruby.yml"
71
73
  - ".github/workflows/milestone.yml"
72
74
  - ".github/workflows/ruby.yml"
73
75
  - ".github/workflows/rust.yml"
76
+ - ".github/workflows/truffleruby.yml"
74
77
  - ".github/workflows/typecheck.yml"
78
+ - ".github/workflows/wasm.yml"
75
79
  - ".github/workflows/windows.yml"
76
80
  - ".gitignore"
77
81
  - ".rubocop.yml"
78
- - ".vscode/extensions.json"
79
- - ".vscode/settings.json"
80
82
  - BSDL
81
83
  - CHANGELOG.md
82
84
  - COPYING
85
+ - Dockerfile.jruby
83
86
  - README.md
84
87
  - Rakefile
85
88
  - Steepfile
@@ -107,6 +110,8 @@ files:
107
110
  - core/fiber.rbs
108
111
  - core/fiber_error.rbs
109
112
  - core/file.rbs
113
+ - core/file_constants.rbs
114
+ - core/file_stat.rbs
110
115
  - core/file_test.rbs
111
116
  - core/float.rbs
112
117
  - core/gc.rbs
@@ -135,6 +140,7 @@ files:
135
140
  - core/range.rbs
136
141
  - core/rational.rbs
137
142
  - core/rb_config.rbs
143
+ - core/rbs/ops.rbs
138
144
  - core/rbs/unnamed/argf.rbs
139
145
  - core/rbs/unnamed/env_class.rbs
140
146
  - core/rbs/unnamed/main_class.rbs
@@ -180,6 +186,7 @@ files:
180
186
  - docs/gem.md
181
187
  - docs/inline.md
182
188
  - docs/rbs_by_example.md
189
+ - docs/release.md
183
190
  - docs/repo.md
184
191
  - docs/rust.md
185
192
  - docs/sigs.md
@@ -187,6 +194,7 @@ files:
187
194
  - docs/syntax.md
188
195
  - docs/tools.md
189
196
  - docs/type_fingerprint.md
197
+ - docs/wasm_serialization.md
190
198
  - exe/rbs
191
199
  - ext/rbs_extension/ast_translation.c
192
200
  - ext/rbs_extension/ast_translation.h
@@ -207,6 +215,7 @@ files:
207
215
  - include/rbs/lexer.h
208
216
  - include/rbs/location.h
209
217
  - include/rbs/parser.h
218
+ - include/rbs/serialize.h
210
219
  - include/rbs/string.h
211
220
  - include/rbs/util/rbs_allocator.h
212
221
  - include/rbs/util/rbs_assert.h
@@ -314,7 +323,13 @@ files:
314
323
  - lib/rbs/variance_calculator.rb
315
324
  - lib/rbs/vendorer.rb
316
325
  - lib/rbs/version.rb
326
+ - lib/rbs/wasm/deserializer.rb
327
+ - lib/rbs/wasm/location.rb
328
+ - lib/rbs/wasm/parser.rb
329
+ - lib/rbs/wasm/runtime.rb
330
+ - lib/rbs/wasm/serialization_schema.rb
317
331
  - lib/rbs/writer.rb
332
+ - lib/rbs_jars.rb
318
333
  - lib/rdoc/discover.rb
319
334
  - lib/rdoc_plugin/parser.rb
320
335
  - rbs.gemspec
@@ -417,6 +432,8 @@ files:
417
432
  - sig/vendorer.rbs
418
433
  - sig/version.rbs
419
434
  - sig/visitor.rbs
435
+ - sig/wasm/deserializer.rbs
436
+ - sig/wasm/serialization_schema.rbs
420
437
  - sig/writer.rbs
421
438
  - src/ast.c
422
439
  - src/lexer.c
@@ -424,6 +441,7 @@ files:
424
441
  - src/lexstate.c
425
442
  - src/location.c
426
443
  - src/parser.c
444
+ - src/serialize.c
427
445
  - src/string.c
428
446
  - src/util/rbs_allocator.c
429
447
  - src/util/rbs_assert.c
@@ -565,6 +583,7 @@ files:
565
583
  - stdlib/socket/0/unix_socket.rbs
566
584
  - stdlib/stringio/0/stringio.rbs
567
585
  - stdlib/strscan/0/string_scanner.rbs
586
+ - stdlib/tempfile/0/manifest.yaml
568
587
  - stdlib/tempfile/0/tempfile.rbs
569
588
  - stdlib/time/0/time.rbs
570
589
  - stdlib/timeout/0/timeout.rbs
@@ -606,6 +625,8 @@ files:
606
625
  - stdlib/zlib/0/version_error.rbs
607
626
  - stdlib/zlib/0/zlib.rbs
608
627
  - stdlib/zlib/0/zstream.rbs
628
+ - wasm/README.md
629
+ - wasm/rbs_wasm.c
609
630
  homepage: https://github.com/ruby/rbs
610
631
  licenses:
611
632
  - BSD-2-Clause
@@ -628,7 +649,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
628
649
  - !ruby/object:Gem::Version
629
650
  version: '0'
630
651
  requirements: []
631
- rubygems_version: 4.0.6
652
+ rubygems_version: 4.0.3
632
653
  specification_version: 4
633
654
  summary: Type signature for Ruby.
634
655
  test_files: []
@@ -1,5 +0,0 @@
1
- {
2
- "recommendations": [
3
- "llvm-vs-code-extensions.vscode-clangd"
4
- ]
5
- }
@@ -1,19 +0,0 @@
1
- {
2
- "clangd.onConfigChanged": "restart",
3
- "[c]": {
4
- "editor.formatOnSave": true,
5
- "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
6
- },
7
- "[cpp]": {
8
- "editor.formatOnSave": true,
9
- "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
10
- },
11
- "[h]": {
12
- "editor.formatOnSave": true,
13
- "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
14
- },
15
- "[hpp]": {
16
- "editor.formatOnSave": true,
17
- "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
18
- }
19
- }