rubydex 0.1.0.beta12-aarch64-linux

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 (109) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +23 -0
  3. data/README.md +125 -0
  4. data/THIRD_PARTY_LICENSES.html +4562 -0
  5. data/exe/rdx +47 -0
  6. data/ext/rubydex/declaration.c +453 -0
  7. data/ext/rubydex/declaration.h +23 -0
  8. data/ext/rubydex/definition.c +284 -0
  9. data/ext/rubydex/definition.h +28 -0
  10. data/ext/rubydex/diagnostic.c +6 -0
  11. data/ext/rubydex/diagnostic.h +11 -0
  12. data/ext/rubydex/document.c +97 -0
  13. data/ext/rubydex/document.h +10 -0
  14. data/ext/rubydex/extconf.rb +138 -0
  15. data/ext/rubydex/graph.c +681 -0
  16. data/ext/rubydex/graph.h +10 -0
  17. data/ext/rubydex/handle.h +44 -0
  18. data/ext/rubydex/location.c +22 -0
  19. data/ext/rubydex/location.h +15 -0
  20. data/ext/rubydex/reference.c +123 -0
  21. data/ext/rubydex/reference.h +15 -0
  22. data/ext/rubydex/rubydex.c +22 -0
  23. data/ext/rubydex/utils.c +108 -0
  24. data/ext/rubydex/utils.h +34 -0
  25. data/lib/rubydex/3.2/rubydex.so +0 -0
  26. data/lib/rubydex/3.3/rubydex.so +0 -0
  27. data/lib/rubydex/3.4/rubydex.so +0 -0
  28. data/lib/rubydex/4.0/rubydex.so +0 -0
  29. data/lib/rubydex/comment.rb +17 -0
  30. data/lib/rubydex/diagnostic.rb +21 -0
  31. data/lib/rubydex/failures.rb +15 -0
  32. data/lib/rubydex/graph.rb +98 -0
  33. data/lib/rubydex/keyword.rb +17 -0
  34. data/lib/rubydex/keyword_parameter.rb +13 -0
  35. data/lib/rubydex/librubydex_sys.so +0 -0
  36. data/lib/rubydex/location.rb +90 -0
  37. data/lib/rubydex/mixin.rb +22 -0
  38. data/lib/rubydex/version.rb +5 -0
  39. data/lib/rubydex.rb +23 -0
  40. data/rbi/rubydex.rbi +422 -0
  41. data/rust/Cargo.lock +1851 -0
  42. data/rust/Cargo.toml +29 -0
  43. data/rust/about.hbs +78 -0
  44. data/rust/about.toml +10 -0
  45. data/rust/rubydex/Cargo.toml +42 -0
  46. data/rust/rubydex/src/compile_assertions.rs +13 -0
  47. data/rust/rubydex/src/diagnostic.rs +110 -0
  48. data/rust/rubydex/src/errors.rs +28 -0
  49. data/rust/rubydex/src/indexing/local_graph.rs +224 -0
  50. data/rust/rubydex/src/indexing/rbs_indexer.rs +1551 -0
  51. data/rust/rubydex/src/indexing/ruby_indexer.rs +2329 -0
  52. data/rust/rubydex/src/indexing/ruby_indexer_tests.rs +4962 -0
  53. data/rust/rubydex/src/indexing.rs +210 -0
  54. data/rust/rubydex/src/integrity.rs +279 -0
  55. data/rust/rubydex/src/job_queue.rs +205 -0
  56. data/rust/rubydex/src/lib.rs +17 -0
  57. data/rust/rubydex/src/listing.rs +371 -0
  58. data/rust/rubydex/src/main.rs +160 -0
  59. data/rust/rubydex/src/model/built_in.rs +83 -0
  60. data/rust/rubydex/src/model/comment.rs +24 -0
  61. data/rust/rubydex/src/model/declaration.rs +671 -0
  62. data/rust/rubydex/src/model/definitions.rs +1682 -0
  63. data/rust/rubydex/src/model/document.rs +222 -0
  64. data/rust/rubydex/src/model/encoding.rs +22 -0
  65. data/rust/rubydex/src/model/graph.rs +3754 -0
  66. data/rust/rubydex/src/model/id.rs +110 -0
  67. data/rust/rubydex/src/model/identity_maps.rs +58 -0
  68. data/rust/rubydex/src/model/ids.rs +60 -0
  69. data/rust/rubydex/src/model/keywords.rs +256 -0
  70. data/rust/rubydex/src/model/name.rs +298 -0
  71. data/rust/rubydex/src/model/references.rs +111 -0
  72. data/rust/rubydex/src/model/string_ref.rs +50 -0
  73. data/rust/rubydex/src/model/visibility.rs +41 -0
  74. data/rust/rubydex/src/model.rs +15 -0
  75. data/rust/rubydex/src/offset.rs +147 -0
  76. data/rust/rubydex/src/position.rs +6 -0
  77. data/rust/rubydex/src/query.rs +1841 -0
  78. data/rust/rubydex/src/resolution.rs +6517 -0
  79. data/rust/rubydex/src/stats/memory.rs +71 -0
  80. data/rust/rubydex/src/stats/orphan_report.rs +264 -0
  81. data/rust/rubydex/src/stats/timer.rs +127 -0
  82. data/rust/rubydex/src/stats.rs +11 -0
  83. data/rust/rubydex/src/test_utils/context.rs +226 -0
  84. data/rust/rubydex/src/test_utils/graph_test.rs +730 -0
  85. data/rust/rubydex/src/test_utils/local_graph_test.rs +602 -0
  86. data/rust/rubydex/src/test_utils.rs +52 -0
  87. data/rust/rubydex/src/visualization/dot.rs +192 -0
  88. data/rust/rubydex/src/visualization.rs +6 -0
  89. data/rust/rubydex/tests/cli.rs +185 -0
  90. data/rust/rubydex-mcp/Cargo.toml +28 -0
  91. data/rust/rubydex-mcp/src/main.rs +48 -0
  92. data/rust/rubydex-mcp/src/server.rs +1145 -0
  93. data/rust/rubydex-mcp/src/tools.rs +49 -0
  94. data/rust/rubydex-mcp/tests/mcp.rs +302 -0
  95. data/rust/rubydex-sys/Cargo.toml +20 -0
  96. data/rust/rubydex-sys/build.rs +14 -0
  97. data/rust/rubydex-sys/cbindgen.toml +12 -0
  98. data/rust/rubydex-sys/src/declaration_api.rs +485 -0
  99. data/rust/rubydex-sys/src/definition_api.rs +443 -0
  100. data/rust/rubydex-sys/src/diagnostic_api.rs +99 -0
  101. data/rust/rubydex-sys/src/document_api.rs +85 -0
  102. data/rust/rubydex-sys/src/graph_api.rs +948 -0
  103. data/rust/rubydex-sys/src/lib.rs +79 -0
  104. data/rust/rubydex-sys/src/location_api.rs +79 -0
  105. data/rust/rubydex-sys/src/name_api.rs +135 -0
  106. data/rust/rubydex-sys/src/reference_api.rs +267 -0
  107. data/rust/rubydex-sys/src/utils.rs +70 -0
  108. data/rust/rustfmt.toml +2 -0
  109. metadata +159 -0
@@ -0,0 +1,284 @@
1
+ #include "definition.h"
2
+ #include "graph.h"
3
+ #include "handle.h"
4
+ #include "location.h"
5
+ #include "reference.h"
6
+ #include "ruby/internal/scan_args.h"
7
+ #include "rustbindings.h"
8
+
9
+ static VALUE mRubydex;
10
+ static VALUE cInclude;
11
+ static VALUE cPrepend;
12
+ static VALUE cExtend;
13
+ VALUE cComment;
14
+ VALUE cDefinition;
15
+ VALUE cClassDefinition;
16
+ VALUE cSingletonClassDefinition;
17
+ VALUE cModuleDefinition;
18
+ VALUE cConstantDefinition;
19
+ VALUE cConstantAliasDefinition;
20
+ VALUE cConstantVisibilityDefinition;
21
+ VALUE cMethodVisibilityDefinition;
22
+ VALUE cMethodDefinition;
23
+ VALUE cAttrAccessorDefinition;
24
+ VALUE cAttrReaderDefinition;
25
+ VALUE cAttrWriterDefinition;
26
+ VALUE cGlobalVariableDefinition;
27
+ VALUE cInstanceVariableDefinition;
28
+ VALUE cClassVariableDefinition;
29
+ VALUE cMethodAliasDefinition;
30
+ VALUE cGlobalVariableAliasDefinition;
31
+
32
+ // Keep this in sync with definition.rs
33
+ VALUE rdxi_definition_class_for_kind(DefinitionKind kind) {
34
+ switch (kind) {
35
+ case DefinitionKind_Class:
36
+ return cClassDefinition;
37
+ case DefinitionKind_SingletonClass:
38
+ return cSingletonClassDefinition;
39
+ case DefinitionKind_Module:
40
+ return cModuleDefinition;
41
+ case DefinitionKind_Constant:
42
+ return cConstantDefinition;
43
+ case DefinitionKind_ConstantAlias:
44
+ return cConstantAliasDefinition;
45
+ case DefinitionKind_ConstantVisibility:
46
+ return cConstantVisibilityDefinition;
47
+ case DefinitionKind_MethodVisibility:
48
+ return cMethodVisibilityDefinition;
49
+ case DefinitionKind_Method:
50
+ return cMethodDefinition;
51
+ case DefinitionKind_AttrAccessor:
52
+ return cAttrAccessorDefinition;
53
+ case DefinitionKind_AttrReader:
54
+ return cAttrReaderDefinition;
55
+ case DefinitionKind_AttrWriter:
56
+ return cAttrWriterDefinition;
57
+ case DefinitionKind_GlobalVariable:
58
+ return cGlobalVariableDefinition;
59
+ case DefinitionKind_InstanceVariable:
60
+ return cInstanceVariableDefinition;
61
+ case DefinitionKind_ClassVariable:
62
+ return cClassVariableDefinition;
63
+ case DefinitionKind_MethodAlias:
64
+ return cMethodAliasDefinition;
65
+ case DefinitionKind_GlobalVariableAlias:
66
+ return cGlobalVariableAliasDefinition;
67
+ default:
68
+ rb_raise(rb_eRuntimeError, "Unknown DefinitionKind: %d", kind);
69
+ }
70
+ }
71
+
72
+ // Definition#location -> Rubydex::Location
73
+ static VALUE rdxr_definition_location(VALUE self) {
74
+ HandleData *data;
75
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
76
+
77
+ void *graph;
78
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
79
+
80
+ Location *loc = rdx_definition_location(graph, data->id);
81
+ VALUE location = rdxi_build_location_value(loc);
82
+ rdx_location_free(loc);
83
+
84
+ return location;
85
+ }
86
+
87
+ // Definition#comments -> [Rubydex::Comment]
88
+ static VALUE rdxr_definition_comments(VALUE self) {
89
+ HandleData *data;
90
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
91
+
92
+ void *graph;
93
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
94
+
95
+ CommentArray *arr = rdx_definition_comments(graph, data->id);
96
+ if (arr == NULL || arr->len == 0) {
97
+ if (arr != NULL) {
98
+ rdx_definition_comments_free(arr);
99
+ }
100
+ return rb_ary_new();
101
+ }
102
+
103
+ VALUE ary = rb_ary_new_capa((long)arr->len);
104
+ for (size_t i = 0; i < arr->len; i++) {
105
+ CommentEntry entry = arr->items[i];
106
+
107
+ VALUE string = rb_utf8_str_new_cstr(entry.string);
108
+
109
+ Location *loc = entry.location;
110
+ VALUE location = rdxi_build_location_value(loc);
111
+
112
+ VALUE comment_kwargs = rb_hash_new();
113
+ rb_hash_aset(comment_kwargs, ID2SYM(rb_intern("string")), string);
114
+ rb_hash_aset(comment_kwargs, ID2SYM(rb_intern("location")), location);
115
+ VALUE comment = rb_class_new_instance_kw(1, &comment_kwargs, cComment, RB_PASS_KEYWORDS);
116
+
117
+ rb_ary_push(ary, comment);
118
+ }
119
+
120
+ // Free the array and all inner allocations on the Rust side
121
+ rdx_definition_comments_free(arr);
122
+ return ary;
123
+ }
124
+
125
+ // Definition#name -> String
126
+ static VALUE rdxr_definition_name(VALUE self) {
127
+ HandleData *data;
128
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
129
+
130
+ void *graph;
131
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
132
+
133
+ const char *name = rdx_definition_name(graph, data->id);
134
+ if (name == NULL) {
135
+ return Qnil;
136
+ }
137
+ VALUE str = rb_utf8_str_new_cstr(name);
138
+ free_c_string(name);
139
+ return str;
140
+ }
141
+
142
+ // Definition#deprecated? -> bool
143
+ static VALUE rdxr_definition_deprecated(VALUE self) {
144
+ HandleData *data;
145
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
146
+
147
+ void *graph;
148
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
149
+
150
+ bool deprecated = rdx_definition_is_deprecated(graph, data->id);
151
+ return deprecated ? Qtrue : Qfalse;
152
+ }
153
+
154
+ // Definition#name_location -> Rubydex::Location or nil
155
+ // For class, module, and singleton class definitions, returns the location of just the name
156
+ // (e.g., "Bar" in "class Foo::Bar"). For other definition types, returns nil.
157
+ static VALUE rdxr_definition_name_location(VALUE self) {
158
+ HandleData *data;
159
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
160
+
161
+ void *graph;
162
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
163
+
164
+ Location *loc = rdx_definition_name_location(graph, data->id);
165
+ if (loc == NULL) {
166
+ return Qnil;
167
+ }
168
+ VALUE location = rdxi_build_location_value(loc);
169
+ rdx_location_free(loc);
170
+
171
+ return location;
172
+ }
173
+
174
+ static VALUE rdxi_build_constant_reference(VALUE graph_obj, const CConstantReference *cref) {
175
+ VALUE ref_class = (cref->declaration_id == 0)
176
+ ? cUnresolvedConstantReference
177
+ : cResolvedConstantReference;
178
+
179
+ VALUE argv[] = {graph_obj, ULL2NUM(cref->id)};
180
+ return rb_class_new_instance(2, argv, ref_class);
181
+ }
182
+
183
+ // ClassDefinition#superclass -> ConstantReference?
184
+ static VALUE rdxr_class_definition_superclass(VALUE self) {
185
+ HandleData *data;
186
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
187
+
188
+ void *graph;
189
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
190
+
191
+ const CConstantReference *ref = rdx_class_definition_superclass(graph, data->id);
192
+ if (ref == NULL) {
193
+ return Qnil;
194
+ }
195
+
196
+ VALUE result = rdxi_build_constant_reference(data->graph_obj, ref);
197
+ free_c_constant_reference(ref);
198
+ return result;
199
+ }
200
+
201
+ static VALUE rdxi_mixin_class_for_kind(MixinKind kind) {
202
+ switch (kind) {
203
+ case MixinKind_Include:
204
+ return cInclude;
205
+ case MixinKind_Prepend:
206
+ return cPrepend;
207
+ case MixinKind_Extend:
208
+ return cExtend;
209
+ default:
210
+ rb_raise(rb_eRuntimeError, "Unknown MixinKind: %d", kind);
211
+ }
212
+ }
213
+
214
+ // Definition#mixins -> [Rubydex::Mixin]
215
+ static VALUE rdxr_definition_mixins(VALUE self) {
216
+ HandleData *data;
217
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
218
+
219
+ void *graph;
220
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
221
+
222
+ MixinsIter *iter = rdx_definition_mixins(graph, data->id);
223
+ if (iter == NULL) {
224
+ rb_raise(rb_eRuntimeError, "Tried to get mixins for a definition that isn't a namespace");
225
+ }
226
+
227
+ size_t len = rdx_mixins_iter_len(iter);
228
+ VALUE ary = rb_ary_new_capa((long)len);
229
+
230
+ CMixin entry;
231
+ while (rdx_mixins_iter_next(iter, &entry)) {
232
+ VALUE constant_ref = rdxi_build_constant_reference(data->graph_obj, &entry.constant_reference);
233
+ VALUE mixin_class = rdxi_mixin_class_for_kind(entry.kind);
234
+ VALUE mixin = rb_class_new_instance(1, &constant_ref, mixin_class);
235
+ rb_ary_push(ary, mixin);
236
+ }
237
+
238
+ rdx_mixins_iter_free(iter);
239
+ return ary;
240
+ }
241
+
242
+ void rdxi_initialize_definition(VALUE mod) {
243
+ mRubydex = mod;
244
+
245
+ cInclude = rb_const_get(mRubydex, rb_intern("Include"));
246
+ cPrepend = rb_const_get(mRubydex, rb_intern("Prepend"));
247
+ cExtend = rb_const_get(mRubydex, rb_intern("Extend"));
248
+
249
+ cComment = rb_define_class_under(mRubydex, "Comment", rb_cObject);
250
+
251
+ cDefinition = rb_define_class_under(mRubydex, "Definition", rb_cObject);
252
+ rb_define_alloc_func(cDefinition, rdxr_handle_alloc);
253
+ rb_define_method(cDefinition, "initialize", rdxr_handle_initialize, 2);
254
+ rb_funcall(rb_singleton_class(cDefinition), rb_intern("private"), 1, ID2SYM(rb_intern("new")));
255
+ rb_define_method(cDefinition, "location", rdxr_definition_location, 0);
256
+ rb_define_method(cDefinition, "comments", rdxr_definition_comments, 0);
257
+ rb_define_method(cDefinition, "name", rdxr_definition_name, 0);
258
+ rb_define_method(cDefinition, "deprecated?", rdxr_definition_deprecated, 0);
259
+ rb_define_method(cDefinition, "name_location", rdxr_definition_name_location, 0);
260
+
261
+ cClassDefinition = rb_define_class_under(mRubydex, "ClassDefinition", cDefinition);
262
+ rb_define_method(cClassDefinition, "superclass", rdxr_class_definition_superclass, 0);
263
+ rb_define_method(cClassDefinition, "mixins", rdxr_definition_mixins, 0);
264
+
265
+ cSingletonClassDefinition = rb_define_class_under(mRubydex, "SingletonClassDefinition", cDefinition);
266
+ rb_define_method(cSingletonClassDefinition, "mixins", rdxr_definition_mixins, 0);
267
+
268
+ cModuleDefinition = rb_define_class_under(mRubydex, "ModuleDefinition", cDefinition);
269
+ rb_define_method(cModuleDefinition, "mixins", rdxr_definition_mixins, 0);
270
+
271
+ cConstantDefinition = rb_define_class_under(mRubydex, "ConstantDefinition", cDefinition);
272
+ cConstantAliasDefinition = rb_define_class_under(mRubydex, "ConstantAliasDefinition", cDefinition);
273
+ cConstantVisibilityDefinition = rb_define_class_under(mRubydex, "ConstantVisibilityDefinition", cDefinition);
274
+ cMethodVisibilityDefinition = rb_define_class_under(mRubydex, "MethodVisibilityDefinition", cDefinition);
275
+ cMethodDefinition = rb_define_class_under(mRubydex, "MethodDefinition", cDefinition);
276
+ cAttrAccessorDefinition = rb_define_class_under(mRubydex, "AttrAccessorDefinition", cDefinition);
277
+ cAttrReaderDefinition = rb_define_class_under(mRubydex, "AttrReaderDefinition", cDefinition);
278
+ cAttrWriterDefinition = rb_define_class_under(mRubydex, "AttrWriterDefinition", cDefinition);
279
+ cGlobalVariableDefinition = rb_define_class_under(mRubydex, "GlobalVariableDefinition", cDefinition);
280
+ cInstanceVariableDefinition = rb_define_class_under(mRubydex, "InstanceVariableDefinition", cDefinition);
281
+ cClassVariableDefinition = rb_define_class_under(mRubydex, "ClassVariableDefinition", cDefinition);
282
+ cMethodAliasDefinition = rb_define_class_under(mRubydex, "MethodAliasDefinition", cDefinition);
283
+ cGlobalVariableAliasDefinition = rb_define_class_under(mRubydex, "GlobalVariableAliasDefinition", cDefinition);
284
+ }
@@ -0,0 +1,28 @@
1
+ #ifndef RUBYDEX_DEFINITION_H
2
+ #define RUBYDEX_DEFINITION_H
3
+
4
+ #include "ruby.h"
5
+ #include "rustbindings.h"
6
+
7
+ extern VALUE cDefinition;
8
+ extern VALUE cClassDefinition;
9
+ extern VALUE cSingletonClassDefinition;
10
+ extern VALUE cModuleDefinition;
11
+ extern VALUE cConstantDefinition;
12
+ extern VALUE cConstantAliasDefinition;
13
+ extern VALUE cMethodDefinition;
14
+ extern VALUE cAttrAccessorDefinition;
15
+ extern VALUE cAttrReaderDefinition;
16
+ extern VALUE cAttrWriterDefinition;
17
+ extern VALUE cGlobalVariableDefinition;
18
+ extern VALUE cInstanceVariableDefinition;
19
+ extern VALUE cClassVariableDefinition;
20
+ extern VALUE cMethodAliasDefinition;
21
+ extern VALUE cGlobalVariableAliasDefinition;
22
+
23
+ void rdxi_initialize_definition(VALUE mRubydex);
24
+
25
+ // Returns the Ruby class for a given DefinitionKind without calling back into Rust
26
+ VALUE rdxi_definition_class_for_kind(DefinitionKind kind);
27
+
28
+ #endif // RUBYDEX_DEFINITION_H
@@ -0,0 +1,6 @@
1
+ #include "diagnostic.h"
2
+ #include "rustbindings.h"
3
+
4
+ VALUE cDiagnostic;
5
+
6
+ void rdxi_initialize_diagnostic(VALUE mRubydex) { cDiagnostic = rb_define_class_under(mRubydex, "Diagnostic", rb_cObject); }
@@ -0,0 +1,11 @@
1
+ #ifndef RUBYDEX_DIAGNOSTIC_H
2
+ #define RUBYDEX_DIAGNOSTIC_H
3
+
4
+ #include "ruby.h"
5
+ #include "rustbindings.h"
6
+
7
+ extern VALUE cDiagnostic;
8
+
9
+ void rdxi_initialize_diagnostic(VALUE mRubydex);
10
+
11
+ #endif // RUBYDEX_DIAGNOSTIC_H
@@ -0,0 +1,97 @@
1
+ #include "document.h"
2
+ #include "definition.h"
3
+ #include "graph.h"
4
+ #include "handle.h"
5
+ #include "rustbindings.h"
6
+
7
+ VALUE cDocument;
8
+
9
+ // Document#uri -> String
10
+ static VALUE rdxr_document_uri(VALUE self) {
11
+ HandleData *data;
12
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
13
+
14
+ void *graph;
15
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
16
+ const char *uri = rdx_document_uri(graph, data->id);
17
+
18
+ if (uri == NULL) {
19
+ return Qnil;
20
+ }
21
+
22
+ VALUE str = rb_utf8_str_new_cstr(uri);
23
+ free_c_string(uri);
24
+
25
+ return str;
26
+ }
27
+
28
+ // Body function for rb_ensure in Document#definitions
29
+ static VALUE document_definitions_yield(VALUE args) {
30
+ VALUE self = rb_ary_entry(args, 0);
31
+ void *iter = (void *)(uintptr_t)NUM2ULL(rb_ary_entry(args, 1));
32
+
33
+ HandleData *data;
34
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
35
+
36
+ CDefinition defn;
37
+ while (rdx_definitions_iter_next(iter, &defn)) {
38
+ VALUE argv[] = {data->graph_obj, ULL2NUM(defn.id)};
39
+ VALUE defn_class = rdxi_definition_class_for_kind(defn.kind);
40
+ VALUE handle = rb_class_new_instance(2, argv, defn_class);
41
+ rb_yield(handle);
42
+ }
43
+
44
+ return Qnil;
45
+ }
46
+
47
+ // Ensure function for rb_ensure in Document#definitions to always free the iterator
48
+ static VALUE document_definitions_ensure(VALUE args) {
49
+ void *iter = (void *)(uintptr_t)NUM2ULL(rb_ary_entry(args, 1));
50
+ rdx_definitions_iter_free(iter);
51
+
52
+ return Qnil;
53
+ }
54
+
55
+ // Size function for the Document#definitions enumerator
56
+ static VALUE document_definitions_size(VALUE self, VALUE _args, VALUE _eobj) {
57
+ HandleData *data;
58
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
59
+
60
+ void *graph;
61
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
62
+ struct DefinitionsIter *iter = rdx_document_definitions_iter_new(graph, data->id);
63
+ size_t len = rdx_definitions_iter_len(iter);
64
+ rdx_definitions_iter_free(iter);
65
+
66
+ return SIZET2NUM(len);
67
+ }
68
+
69
+ // Document#definitions: () -> Enumerator[Definition]
70
+ // Returns an enumerator that yields all definitions for this document lazily
71
+ static VALUE rdxr_document_definitions(VALUE self) {
72
+ if (!rb_block_given_p()) {
73
+ return rb_enumeratorize_with_size(self, rb_str_new2("definitions"), 0, NULL, document_definitions_size);
74
+ }
75
+
76
+ HandleData *data;
77
+ TypedData_Get_Struct(self, HandleData, &handle_type, data);
78
+
79
+ void *graph;
80
+ TypedData_Get_Struct(data->graph_obj, void *, &graph_type, graph);
81
+ void *iter = rdx_document_definitions_iter_new(graph, data->id);
82
+ VALUE args = rb_ary_new_from_args(2, self, ULL2NUM((uintptr_t)iter));
83
+ rb_ensure(document_definitions_yield, args, document_definitions_ensure, args);
84
+
85
+ return self;
86
+ }
87
+
88
+ void rdxi_initialize_document(VALUE mRubydex) {
89
+ cDocument = rb_define_class_under(mRubydex, "Document", rb_cObject);
90
+
91
+ rb_define_alloc_func(cDocument, rdxr_handle_alloc);
92
+ rb_define_method(cDocument, "initialize", rdxr_handle_initialize, 2);
93
+ rb_define_method(cDocument, "uri", rdxr_document_uri, 0);
94
+ rb_define_method(cDocument, "definitions", rdxr_document_definitions, 0);
95
+
96
+ rb_funcall(rb_singleton_class(cDocument), rb_intern("private"), 1, ID2SYM(rb_intern("new")));
97
+ }
@@ -0,0 +1,10 @@
1
+ #ifndef RUBYDEX_DOCUMENT_H
2
+ #define RUBYDEX_DOCUMENT_H
3
+
4
+ #include "ruby.h"
5
+
6
+ extern VALUE cDocument;
7
+
8
+ void rdxi_initialize_document(VALUE mRubydex);
9
+
10
+ #endif // RUBYDEX_DOCUMENT_H
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "pathname"
5
+
6
+ unless system("cargo", "--version", out: File::NULL, err: File::NULL)
7
+ abort "Installing Rubydex requires Cargo, the Rust package manager for platforms that we do not precompile binaries."
8
+ end
9
+
10
+ gem_dir = Pathname.new("../..").expand_path(__dir__)
11
+
12
+ # Use release mode for the compilation if:
13
+ # - The RELEASE environment variable is set
14
+ # - We're not working on Rubydex (BUNDLE_GEMFILE doesn't point to Rubydex's own Gemfile)
15
+ #
16
+ # We only need debug builds when working on Rubydex itself and on CI. This approach also lets people install Rubydex
17
+ # from the git source and get a release mode build
18
+
19
+ bundle_gemfile = ENV["BUNDLE_GEMFILE"]
20
+ developing_rubydex = bundle_gemfile && Pathname.new(bundle_gemfile).expand_path.dirname == gem_dir
21
+ release = ENV["RELEASE"] || !developing_rubydex
22
+
23
+ root_dir = gem_dir.join("rust")
24
+ target_dir = root_dir.join("target")
25
+ target_dir = target_dir.join("x86_64-pc-windows-gnu") if Gem.win_platform?
26
+ target_dir = target_dir.join(release ? "release" : "debug")
27
+
28
+ bindings_path = root_dir.join("rubydex-sys").join("rustbindings.h")
29
+
30
+ cargo_args = ["--manifest-path #{root_dir.join("Cargo.toml")}"]
31
+ cargo_args << "--release" if release
32
+
33
+ if Gem.win_platform?
34
+ cargo_args << "--target x86_64-pc-windows-gnu"
35
+ ENV["RUSTFLAGS"] = "-C target-feature=+crt-static"
36
+ end
37
+
38
+ append_cflags("-Werror=unused-but-set-variable")
39
+ append_cflags("-Werror=implicit-function-declaration")
40
+
41
+ if Gem.win_platform?
42
+ $LDFLAGS << " #{target_dir.join("librubydex_sys.a")}"
43
+
44
+ # On Windows, statically link system libraries to avoid having to distribute and load DLLs
45
+ #
46
+ # These libraries are the ones informed by `cargo rustc -- --print native-static-libs`, which displays the
47
+ # libraries necessary for statically linking the Rust code on the current platform
48
+ ["kernel32", "ntdll", "userenv", "ws2_32", "dbghelp", "msvcrt"].each do |lib|
49
+ $LDFLAGS << " -l#{lib}"
50
+ end
51
+ else
52
+ if RUBY_PLATFORM.include?("darwin")
53
+ # On the precompiled version of the gem, the `dylib` is one folder above the `.bundle/.so` file. For on machine
54
+ # compilation, they are at the same level
55
+ append_ldflags("-Wl,-rpath,@loader_path")
56
+ append_ldflags("-Wl,-rpath,@loader_path/..")
57
+ else
58
+ $LDFLAGS << " -Wl,-rpath,\\$$ORIGIN"
59
+ $LDFLAGS << " -Wl,-rpath,\\$$ORIGIN/.."
60
+ end
61
+
62
+ # We cannot use append_ldflags here because the Rust code is only compiled later. If it's not compiled yet, this will
63
+ # fail and the flag will not be added
64
+ $LDFLAGS << " -L#{target_dir} -lrubydex_sys"
65
+ end
66
+
67
+ create_makefile("rubydex/rubydex")
68
+
69
+ cargo_command = if ENV["SANITIZER"]
70
+ ENV["RUSTFLAGS"] = "-Zsanitizer=#{ENV["SANITIZER"]}"
71
+ "cargo +nightly build -Zbuild-std #{cargo_args.join(" ")}".strip
72
+ else
73
+ "cargo build #{cargo_args.join(" ")}".strip
74
+ end
75
+
76
+ lib_dir = gem_dir.join("lib").join("rubydex")
77
+
78
+ copy_dylib_commands = if Gem.win_platform?
79
+ ""
80
+ elsif RUBY_PLATFORM.include?("darwin")
81
+ src_dylib = target_dir.join("librubydex_sys.dylib")
82
+ "\t$(COPY) #{src_dylib} #{lib_dir}"
83
+ else
84
+ # Linux
85
+ src_dylib = target_dir.join("librubydex_sys.so")
86
+ "\t$(COPY) #{src_dylib} #{lib_dir}"
87
+ end
88
+
89
+ rust_srcs = Dir.glob("#{root_dir}/**/*.rs").reject { |path| path.include?("rust/target") }
90
+ makefile = File.read("Makefile")
91
+
92
+ new_makefile = makefile.gsub("$(OBJS): $(HDRS) $(ruby_headers)", <<~MAKEFILE.chomp)
93
+ .PHONY: compile_rust
94
+ RUST_SRCS = #{File.expand_path("Cargo.toml", root_dir)} #{File.expand_path("Cargo.lock", root_dir)} #{rust_srcs.join(" ")}
95
+
96
+ .rust_built: $(RUST_SRCS)
97
+ \t#{cargo_command} || (echo "Compiling Rust failed" && exit 1)
98
+ \t$(COPY) #{bindings_path} #{__dir__}
99
+ \ttouch $@
100
+
101
+ compile_rust: .rust_built
102
+
103
+ $(OBJS): $(HDRS) $(ruby_headers) .rust_built
104
+ MAKEFILE
105
+
106
+ new_makefile.gsub!(/(\$\(Q\) \$\(LDSHARED\) .*)/, <<~MAKEFILE.chomp)
107
+ \\1
108
+ #{copy_dylib_commands}
109
+ \t$(Q)$(RM) .rust_built
110
+ MAKEFILE
111
+
112
+ # Bundle all dependency licenses when building a release version of the gem. This only has to happen on CI where we
113
+ # precompile binaries. Oherwise, we're not redistributing the Rust dependencies as they are getting downloaded, compiled
114
+ # and linked on the user's machine
115
+ if release && system("cargo", "about", "--version", out: File::NULL, err: File::NULL)
116
+ licenses_file = root_dir.join("THIRD_PARTY_LICENSES.html")
117
+ about_config = root_dir.join("about.toml")
118
+ about_template = root_dir.join("about.hbs")
119
+
120
+ new_makefile.gsub!(".rust_built: $(RUST_SRCS)", <<~MAKEFILE.chomp)
121
+ #{licenses_file}: #{about_config} #{about_template}
122
+ \t$(Q)$(RM) #{licenses_file}
123
+ \tcargo about generate #{about_template} --manifest-path #{root_dir.join("Cargo.toml")} --workspace > #{licenses_file}
124
+ \t$(COPY) #{licenses_file} #{gem_dir}
125
+
126
+ .rust_built: $(RUST_SRCS) #{licenses_file}
127
+ MAKEFILE
128
+ end
129
+
130
+ File.write("Makefile", new_makefile)
131
+
132
+ begin
133
+ require "extconf_compile_commands_json"
134
+
135
+ ExtconfCompileCommandsJson.generate!
136
+ ExtconfCompileCommandsJson.symlink!
137
+ rescue LoadError # rubocop:disable Lint/SuppressedException
138
+ end