rbs 4.0.3 → 4.1.0.pre.2
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 +4 -4
- data/.github/workflows/c-check.yml +3 -0
- data/.github/workflows/dependabot.yml +1 -1
- data/.github/workflows/jruby.yml +67 -0
- data/.github/workflows/milestone.yml +4 -1
- data/.github/workflows/ruby.yml +40 -0
- data/.github/workflows/rust.yml +3 -2
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +3 -0
- data/.github/workflows/wasm.yml +53 -0
- data/.github/workflows/windows.yml +3 -0
- data/.gitignore +7 -0
- data/CHANGELOG.md +0 -8
- data/README.md +3 -3
- data/Rakefile +118 -3
- data/Steepfile +7 -0
- data/config.yml +24 -0
- data/core/array.rbs +144 -144
- data/core/builtin.rbs +7 -6
- data/core/class.rbs +5 -3
- data/core/enumerable.rbs +109 -109
- data/core/enumerator/product.rbs +5 -5
- data/core/enumerator.rbs +28 -28
- data/core/file.rbs +24 -1018
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/hash.rbs +117 -101
- data/core/integer.rbs +21 -58
- data/core/io.rbs +25 -7
- data/core/kernel.rbs +26 -11
- data/core/module.rbs +88 -74
- data/core/numeric.rbs +3 -0
- data/core/object_space/weak_key_map.rbs +7 -7
- data/core/range.rbs +23 -23
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +3 -3
- data/core/ruby_vm.rbs +40 -0
- data/core/set.rbs +3 -3
- data/core/struct.rbs +16 -16
- data/core/thread.rbs +6 -6
- data/docs/CONTRIBUTING.md +2 -1
- data/docs/inline.md +65 -7
- data/docs/rbs_by_example.md +20 -20
- data/docs/syntax.md +2 -2
- data/docs/wasm_serialization.md +80 -0
- data/ext/rbs_extension/ast_translation.c +1298 -956
- data/ext/rbs_extension/ast_translation.h +4 -0
- data/ext/rbs_extension/class_constants.c +2 -0
- data/ext/rbs_extension/class_constants.h +1 -0
- data/ext/rbs_extension/extconf.rb +1 -0
- data/ext/rbs_extension/main.c +139 -4
- data/include/rbs/ast.h +323 -298
- data/include/rbs/defines.h +13 -0
- data/include/rbs/lexer.h +1 -0
- data/include/rbs/serialize.h +39 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +27 -31
- data/lib/rbs/ast/ruby/annotations.rb +42 -0
- data/lib/rbs/ast/ruby/comment_block.rb +6 -4
- data/lib/rbs/ast/ruby/declarations.rb +11 -1
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
- data/lib/rbs/ast/ruby/members.rb +40 -1
- data/lib/rbs/buffer.rb +48 -11
- data/lib/rbs/cli.rb +3 -5
- data/lib/rbs/collection/config/lockfile_generator.rb +14 -1
- data/lib/rbs/collection/sources/git.rb +6 -0
- data/lib/rbs/definition_builder/method_builder.rb +12 -6
- data/lib/rbs/environment.rb +10 -3
- data/lib/rbs/inline_parser.rb +54 -28
- data/lib/rbs/namespace.rb +47 -11
- data/lib/rbs/prototype/runtime.rb +2 -0
- data/lib/rbs/resolver/type_name_resolver.rb +12 -14
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/test/type_check.rb +6 -1
- data/lib/rbs/type_name.rb +33 -13
- data/lib/rbs/unit_test/type_assertions.rb +9 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/runtime.rb +217 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs.rb +14 -2
- data/rbs.gemspec +19 -2
- data/sig/annotate/rdoc_annotater.rbs +12 -9
- data/sig/ast/ruby/annotations.rbs +49 -0
- data/sig/ast/ruby/members.rbs +21 -1
- data/sig/buffer.rbs +19 -1
- data/sig/collection/config/lockfile_generator.rbs +2 -0
- data/sig/inline_parser.rbs +2 -0
- data/sig/manifest.yaml +0 -1
- data/sig/namespace.rbs +20 -0
- data/sig/parser.rbs +10 -0
- data/sig/resolver/type_name_resolver.rbs +2 -4
- data/sig/rewriter.rbs +45 -0
- data/sig/typename.rbs +15 -0
- data/sig/unit_test/type_assertions.rbs +4 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +186 -162
- data/src/lexer.c +137 -114
- data/src/lexer.re +1 -0
- data/src/lexstate.c +6 -1
- data/src/parser.c +55 -5
- data/src/serialize.c +958 -0
- data/src/util/rbs_allocator.c +1 -4
- data/stdlib/abbrev/0/array.rbs +1 -1
- data/stdlib/csv/0/csv.rbs +5 -5
- data/stdlib/digest/0/digest.rbs +1 -1
- data/stdlib/etc/0/etc.rbs +18 -4
- data/stdlib/fileutils/0/fileutils.rbs +21 -21
- data/stdlib/json/0/json.rbs +6 -6
- data/stdlib/openssl/0/openssl.rbs +7 -7
- data/stdlib/resolv/0/resolv.rbs +1 -1
- data/stdlib/shellwords/0/shellwords.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +32 -10
- data/stdlib/strscan/0/string_scanner.rbs +74 -55
- data/stdlib/tsort/0/cyclic.rbs +1 -1
- data/stdlib/tsort/0/interfaces.rbs +8 -8
- data/stdlib/tsort/0/tsort.rbs +9 -9
- data/stdlib/zlib/0/gzip_reader.rbs +2 -2
- data/wasm/README.md +59 -0
- data/wasm/rbs_wasm.c +411 -0
- metadata +23 -5
- data/.vscode/extensions.json +0 -5
- data/.vscode/settings.json +0 -19
|
@@ -23,6 +23,10 @@ typedef struct rbs_translation_context {
|
|
|
23
23
|
rbs_constant_pool_t *constant_pool;
|
|
24
24
|
VALUE buffer;
|
|
25
25
|
rb_encoding *encoding;
|
|
26
|
+
|
|
27
|
+
/// A reusable Hash used for calling `rb_class_new_instance_kw()` without allocating a new Hash each time.
|
|
28
|
+
/// It's vital that the various Nodes' `#initialize` methods take the kwarg values, but don't retain this reusable Hash.
|
|
29
|
+
VALUE reusable_kwargs_hash;
|
|
26
30
|
} rbs_translation_context_t;
|
|
27
31
|
|
|
28
32
|
rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, rb_encoding *ruby_encoding);
|
|
@@ -54,6 +54,7 @@ VALUE RBS_AST_Ruby_Annotations_DoubleSplatParamTypeAnnotation;
|
|
|
54
54
|
VALUE RBS_AST_Ruby_Annotations_InstanceVariableAnnotation;
|
|
55
55
|
VALUE RBS_AST_Ruby_Annotations_MethodTypesAnnotation;
|
|
56
56
|
VALUE RBS_AST_Ruby_Annotations_ModuleAliasAnnotation;
|
|
57
|
+
VALUE RBS_AST_Ruby_Annotations_ModuleSelfAnnotation;
|
|
57
58
|
VALUE RBS_AST_Ruby_Annotations_NodeTypeAssertion;
|
|
58
59
|
VALUE RBS_AST_Ruby_Annotations_ParamTypeAnnotation;
|
|
59
60
|
VALUE RBS_AST_Ruby_Annotations_ReturnTypeAnnotation;
|
|
@@ -147,6 +148,7 @@ void rbs__init_constants(void) {
|
|
|
147
148
|
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_InstanceVariableAnnotation, RBS_AST_Ruby_Annotations, "InstanceVariableAnnotation");
|
|
148
149
|
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_MethodTypesAnnotation, RBS_AST_Ruby_Annotations, "MethodTypesAnnotation");
|
|
149
150
|
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ModuleAliasAnnotation, RBS_AST_Ruby_Annotations, "ModuleAliasAnnotation");
|
|
151
|
+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ModuleSelfAnnotation, RBS_AST_Ruby_Annotations, "ModuleSelfAnnotation");
|
|
150
152
|
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_NodeTypeAssertion, RBS_AST_Ruby_Annotations, "NodeTypeAssertion");
|
|
151
153
|
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ParamTypeAnnotation, RBS_AST_Ruby_Annotations, "ParamTypeAnnotation");
|
|
152
154
|
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ReturnTypeAnnotation, RBS_AST_Ruby_Annotations, "ReturnTypeAnnotation");
|
|
@@ -62,6 +62,7 @@ extern VALUE RBS_AST_Ruby_Annotations_DoubleSplatParamTypeAnnotation;
|
|
|
62
62
|
extern VALUE RBS_AST_Ruby_Annotations_InstanceVariableAnnotation;
|
|
63
63
|
extern VALUE RBS_AST_Ruby_Annotations_MethodTypesAnnotation;
|
|
64
64
|
extern VALUE RBS_AST_Ruby_Annotations_ModuleAliasAnnotation;
|
|
65
|
+
extern VALUE RBS_AST_Ruby_Annotations_ModuleSelfAnnotation;
|
|
65
66
|
extern VALUE RBS_AST_Ruby_Annotations_NodeTypeAssertion;
|
|
66
67
|
extern VALUE RBS_AST_Ruby_Annotations_ParamTypeAnnotation;
|
|
67
68
|
extern VALUE RBS_AST_Ruby_Annotations_ReturnTypeAnnotation;
|
data/ext/rbs_extension/main.c
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#include "rbs/util/rbs_assert.h"
|
|
3
3
|
#include "rbs/util/rbs_allocator.h"
|
|
4
4
|
#include "rbs/util/rbs_constant_pool.h"
|
|
5
|
+
#include "rbs/serialize.h"
|
|
5
6
|
#include "ast_translation.h"
|
|
6
7
|
#include "legacy_location.h"
|
|
7
8
|
#include "rbs_string_bridging.h"
|
|
@@ -145,10 +146,17 @@ static VALUE parse_type_try(VALUE a) {
|
|
|
145
146
|
return rbs_struct_to_ruby_value(ctx, type);
|
|
146
147
|
}
|
|
147
148
|
|
|
148
|
-
static
|
|
149
|
+
static void validate_position_range(int start_pos, int end_pos) {
|
|
149
150
|
if (start_pos < 0 || end_pos < 0) {
|
|
150
151
|
rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
|
|
151
152
|
}
|
|
153
|
+
if (start_pos > end_pos) {
|
|
154
|
+
rb_raise(rb_eArgError, "invalid position range: %d...%d", start_pos, end_pos);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) {
|
|
159
|
+
validate_position_range(start_pos, end_pos);
|
|
152
160
|
|
|
153
161
|
const char *encoding_name = rb_enc_name(encoding);
|
|
154
162
|
|
|
@@ -162,9 +170,7 @@ static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE st
|
|
|
162
170
|
}
|
|
163
171
|
|
|
164
172
|
static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) {
|
|
165
|
-
|
|
166
|
-
rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
|
|
167
|
-
}
|
|
173
|
+
validate_position_range(start_pos, end_pos);
|
|
168
174
|
|
|
169
175
|
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
170
176
|
StringValue(string);
|
|
@@ -285,6 +291,132 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos
|
|
|
285
291
|
return result;
|
|
286
292
|
}
|
|
287
293
|
|
|
294
|
+
// Serialize a parsed node into a binary Ruby string using the same encoder the
|
|
295
|
+
// WebAssembly build uses. These `_*_to_bytes` entry points exist so the
|
|
296
|
+
// round-trip (parse -> serialize -> deserialize) can be exercised on CRuby,
|
|
297
|
+
// where it can be compared against the direct C -> Ruby translation.
|
|
298
|
+
static VALUE serialized_node_to_string(rbs_parser_t *parser, rbs_node_t *node) {
|
|
299
|
+
rbs_string_t bytes = rbs_serialize_node(parser->allocator, &parser->constant_pool, node);
|
|
300
|
+
return rb_str_new(bytes.start, (long) rbs_string_len(bytes));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
static VALUE parse_type_to_bytes_try(VALUE a) {
|
|
304
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
|
305
|
+
rbs_parser_t *parser = arg->parser;
|
|
306
|
+
|
|
307
|
+
if (parser->next_token.type == pEOF) {
|
|
308
|
+
return Qnil;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
rbs_node_t *type;
|
|
312
|
+
rbs_parse_type(parser, &type, RTEST(arg->void_allowed), RTEST(arg->self_allowed), RTEST(arg->classish_allowed));
|
|
313
|
+
|
|
314
|
+
raise_error_if_any(parser, arg->buffer);
|
|
315
|
+
|
|
316
|
+
if (RB_TEST(arg->require_eof)) {
|
|
317
|
+
rbs_parser_advance(parser);
|
|
318
|
+
if (parser->current_token.type != pEOF) {
|
|
319
|
+
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
|
|
320
|
+
raise_error(parser->error, arg->buffer);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return serialized_node_to_string(parser, type);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
static VALUE rbsparser_parse_type_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE void_allowed, VALUE self_allowed, VALUE classish_allowed) {
|
|
328
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
329
|
+
StringValue(string);
|
|
330
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
331
|
+
|
|
332
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
333
|
+
declare_type_variables(parser, variables, buffer);
|
|
334
|
+
struct parse_type_arg arg = {
|
|
335
|
+
.buffer = buffer,
|
|
336
|
+
.encoding = encoding,
|
|
337
|
+
.parser = parser,
|
|
338
|
+
.require_eof = require_eof,
|
|
339
|
+
.void_allowed = void_allowed,
|
|
340
|
+
.self_allowed = self_allowed,
|
|
341
|
+
.classish_allowed = classish_allowed
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
VALUE result = rb_ensure(parse_type_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
345
|
+
|
|
346
|
+
RB_GC_GUARD(string);
|
|
347
|
+
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static VALUE parse_method_type_to_bytes_try(VALUE a) {
|
|
352
|
+
struct parse_method_type_arg *arg = (struct parse_method_type_arg *) a;
|
|
353
|
+
rbs_parser_t *parser = arg->parser;
|
|
354
|
+
|
|
355
|
+
if (parser->next_token.type == pEOF) {
|
|
356
|
+
return Qnil;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
rbs_method_type_t *method_type = NULL;
|
|
360
|
+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);
|
|
361
|
+
|
|
362
|
+
raise_error_if_any(parser, arg->buffer);
|
|
363
|
+
|
|
364
|
+
return serialized_node_to_string(parser, (rbs_node_t *) method_type);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
static VALUE rbsparser_parse_method_type_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
|
|
368
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
369
|
+
StringValue(string);
|
|
370
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
371
|
+
|
|
372
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
373
|
+
declare_type_variables(parser, variables, buffer);
|
|
374
|
+
struct parse_method_type_arg arg = {
|
|
375
|
+
.buffer = buffer,
|
|
376
|
+
.encoding = encoding,
|
|
377
|
+
.parser = parser,
|
|
378
|
+
.require_eof = require_eof
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
VALUE result = rb_ensure(parse_method_type_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
382
|
+
|
|
383
|
+
RB_GC_GUARD(string);
|
|
384
|
+
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
static VALUE parse_signature_to_bytes_try(VALUE a) {
|
|
389
|
+
struct parse_signature_arg *arg = (struct parse_signature_arg *) a;
|
|
390
|
+
rbs_parser_t *parser = arg->parser;
|
|
391
|
+
|
|
392
|
+
rbs_signature_t *signature = NULL;
|
|
393
|
+
rbs_parse_signature(parser, &signature);
|
|
394
|
+
|
|
395
|
+
raise_error_if_any(parser, arg->buffer);
|
|
396
|
+
|
|
397
|
+
return serialized_node_to_string(parser, (rbs_node_t *) signature);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
static VALUE rbsparser_parse_signature_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
|
|
401
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
402
|
+
StringValue(string);
|
|
403
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
404
|
+
|
|
405
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
406
|
+
struct parse_signature_arg arg = {
|
|
407
|
+
.buffer = buffer,
|
|
408
|
+
.encoding = encoding,
|
|
409
|
+
.parser = parser,
|
|
410
|
+
.require_eof = false
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
VALUE result = rb_ensure(parse_signature_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
414
|
+
|
|
415
|
+
RB_GC_GUARD(string);
|
|
416
|
+
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
419
|
+
|
|
288
420
|
struct parse_type_params_arg {
|
|
289
421
|
VALUE buffer;
|
|
290
422
|
rb_encoding *encoding;
|
|
@@ -457,6 +589,9 @@ void rbs__init_parser(void) {
|
|
|
457
589
|
rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 8);
|
|
458
590
|
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
|
|
459
591
|
rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
|
|
592
|
+
rb_define_singleton_method(RBS_Parser, "_parse_type_to_bytes", rbsparser_parse_type_to_bytes, 8);
|
|
593
|
+
rb_define_singleton_method(RBS_Parser, "_parse_method_type_to_bytes", rbsparser_parse_method_type_to_bytes, 5);
|
|
594
|
+
rb_define_singleton_method(RBS_Parser, "_parse_signature_to_bytes", rbsparser_parse_signature_to_bytes, 3);
|
|
460
595
|
rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
|
|
461
596
|
rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
|
|
462
597
|
rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
|