rustorb 0.7.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.
@@ -0,0 +1,448 @@
1
+ # frozen_string_literal: true
2
+
3
+ # CORBA::TypeCode: a native TypeCode, with what the marshaller needs kept on
4
+ # the Ruby side. TypeCodes made here (by the generated code, the ORB's
5
+ # create_*_tc) keep their member TypeCodes; those read from the wire ask the
6
+ # native one, lazily.
7
+ #
8
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
9
+
10
+ module CORBA
11
+ TK_NULL = 0
12
+ TK_VOID = 1
13
+ TK_SHORT = 2
14
+ TK_LONG = 3
15
+ TK_USHORT = 4
16
+ TK_ULONG = 5
17
+ TK_FLOAT = 6
18
+ TK_DOUBLE = 7
19
+ TK_BOOLEAN = 8
20
+ TK_CHAR = 9
21
+ TK_OCTET = 10
22
+ TK_ANY = 11
23
+ TK_TYPECODE = 12
24
+ TK_PRINCIPAL = 13
25
+ TK_OBJREF = 14
26
+ TK_STRUCT = 15
27
+ TK_UNION = 16
28
+ TK_ENUM = 17
29
+ TK_STRING = 18
30
+ TK_SEQUENCE = 19
31
+ TK_ARRAY = 20
32
+ TK_ALIAS = 21
33
+ TK_EXCEPT = 22
34
+ TK_LONGLONG = 23
35
+ TK_ULONGLONG = 24
36
+ TK_LONGDOUBLE = 25
37
+ TK_WCHAR = 26
38
+ TK_WSTRING = 27
39
+ TK_FIXED = 28
40
+ TK_VALUE = 29
41
+ TK_VALUE_BOX = 30
42
+ TK_NATIVE = 31
43
+ TK_ABSTRACT_INTERFACE = 32
44
+ TK_LOCAL_INTERFACE = 33
45
+ TK_COMPONENT = 34
46
+ TK_HOME = 35
47
+ TK_EVENT = 36
48
+
49
+ class TypeCode
50
+ class BadKind < UserException
51
+ ID = "IDL:omg.org/CORBA/TypeCode/BadKind:1.0"
52
+ MEMBERS = [].freeze
53
+ end
54
+
55
+ class Bounds < UserException
56
+ ID = "IDL:omg.org/CORBA/TypeCode/Bounds:1.0"
57
+ MEMBERS = [].freeze
58
+ end
59
+
60
+ Rustorb::Native::LOCAL_EXCEPTIONS[14] = ->(_) { BadKind.new }
61
+ Rustorb::Native::LOCAL_EXCEPTIONS[15] = ->(_) { Bounds.new }
62
+
63
+ attr_reader :native
64
+
65
+ # native: a Rustorb::Native::TC. members: [[name, TypeCode]] (struct,
66
+ # except), [[label, name, TypeCode]] (union, label :default), or names
67
+ # (enum); content: the TypeCode of a sequence, array or alias.
68
+ # extra: what the native TypeCode does not say, for the IDL 4 types
69
+ # sent as integers (rustorb): :int8 (an octet read as signed), or the
70
+ # class of a bitset (sent as the unsigned integer of the TypeCode).
71
+ def initialize(native, members: nil, content: nil, discriminator: nil, recursive_id: nil,
72
+ extra: nil)
73
+ @native = native
74
+ @kind = native&.kind
75
+ @members = members
76
+ @content = content
77
+ @discriminator = discriminator
78
+ @recursive_id = recursive_id
79
+ @extra = extra
80
+ end
81
+
82
+ def _extra
83
+ @extra
84
+ end
85
+
86
+ def self._wrap(native)
87
+ native && new(native)
88
+ end
89
+
90
+ # --- the queries of CORBA::TypeCode ---
91
+
92
+ def kind
93
+ return resolve.kind if @recursive_id
94
+
95
+ @kind
96
+ end
97
+
98
+ def id
99
+ @id ||= @recursive_id || native.id
100
+ end
101
+
102
+ def name
103
+ @name ||= native.name
104
+ end
105
+
106
+ def member_count
107
+ @member_count ||= native.member_count
108
+ end
109
+
110
+ def member_name(index)
111
+ native.member_name(index)
112
+ end
113
+
114
+ def member_type(index)
115
+ if @members && kind != TK_ENUM
116
+ m = @members[index] or raise Bounds
117
+ return kind == TK_UNION ? m[2] : m[1]
118
+ end
119
+ @member_types ||= {}
120
+ @member_types[index] ||= TypeCode._wrap(native.member_type(index))
121
+ end
122
+
123
+ # The label of a union member, as a Ruby value (:default for the
124
+ # default member).
125
+ def member_label(index)
126
+ raise BadKind unless kind == TK_UNION
127
+ return @members[index][0] if @members
128
+
129
+ return :default if index == default_index
130
+
131
+ CORBA::Any._from_native(native.member_label(index)).value
132
+ end
133
+
134
+ def discriminator_type
135
+ @discriminator ||= TypeCode._wrap(native.discriminator_type)
136
+ end
137
+
138
+ def default_index
139
+ @default_index ||= native.default_index
140
+ end
141
+
142
+ def length
143
+ @length ||= native.length
144
+ end
145
+
146
+ def content_type
147
+ @content ||= TypeCode._wrap(native.content_type)
148
+ end
149
+
150
+ # The concrete base valuetype of a valuetype (nil without one).
151
+ def concrete_base_type
152
+ return @concrete_base if defined?(@concrete_base)
153
+
154
+ @concrete_base = TypeCode._wrap(native.concrete_base_type)
155
+ end
156
+
157
+ def type_modifier
158
+ @type_modifier ||= native.type_modifier
159
+ end
160
+
161
+ def member_visibility(index)
162
+ native.member_visibility(index)
163
+ end
164
+
165
+ def fixed_digits
166
+ @fixed_digits ||= native.fixed_digits
167
+ end
168
+
169
+ def fixed_scale
170
+ @fixed_scale ||= native.fixed_scale
171
+ end
172
+
173
+ def equal?(other)
174
+ other.is_a?(TypeCode) && resolve.native.equal(other.resolve.native)
175
+ end
176
+
177
+ def equivalent?(other)
178
+ other.is_a?(TypeCode) && resolve.native.equivalent(other.resolve.native)
179
+ end
180
+
181
+ def ==(other)
182
+ equal?(other)
183
+ end
184
+
185
+ def get_compact_typecode
186
+ TypeCode._wrap(native.compact)
187
+ end
188
+
189
+ # The TypeCode without its aliases.
190
+ def unaliased
191
+ tc = resolve
192
+ tc = tc.content_type while tc.kind == TK_ALIAS
193
+ tc
194
+ end
195
+
196
+ # A placeholder of create_recursive_tc stands for the type with its id
197
+ # once that is made (Rustorb.find_tc).
198
+ def resolve
199
+ return self unless @recursive_id
200
+
201
+ @resolved ||= Rustorb.find_tc(@recursive_id) or
202
+ raise CORBA::BAD_TYPECODE.new("unresolved recursive TypeCode #{@recursive_id}")
203
+ end
204
+
205
+ # Enumerator names of an enum.
206
+ def _enumerators
207
+ @enumerators ||= @members || Array.new(member_count) { |i| member_name(i) }
208
+ end
209
+
210
+ # [label, name, TypeCode] of each union member.
211
+ def _union_members
212
+ @union_members ||= @members || Array.new(member_count) do |i|
213
+ [member_label(i), member_name(i), member_type(i)]
214
+ end
215
+ end
216
+
217
+ # [name, TypeCode] of each struct or exception member.
218
+ def _struct_members
219
+ @struct_members ||= @members || Array.new(member_count) { |i| [member_name(i), member_type(i)] }
220
+ end
221
+
222
+ def inspect
223
+ "#<CORBA::TypeCode kind=#{kind}#{@recursive_id || ([TK_STRUCT, TK_UNION, TK_ENUM, TK_ALIAS, TK_EXCEPT, TK_OBJREF].include?(kind) ? " #{id}" : '')}>"
224
+ end
225
+ alias to_s inspect
226
+
227
+ # --- constructors (ORB::create_*_tc; generated code) ---
228
+
229
+ SIMPLE = {}
230
+
231
+ def self.simple(kind)
232
+ SIMPLE[kind] ||= new(Rustorb::Native::TC.simple(kind))
233
+ end
234
+
235
+ def self.create_string_tc(bound)
236
+ bound.zero? ? CORBA._tc_string : new(Rustorb::Native::TC.string(false, bound))
237
+ end
238
+
239
+ def self.create_wstring_tc(bound)
240
+ bound.zero? ? CORBA._tc_wstring : new(Rustorb::Native::TC.string(true, bound))
241
+ end
242
+
243
+ def self.create_fixed_tc(digits, scale)
244
+ new(Rustorb::Native::TC.fixed(digits, scale))
245
+ end
246
+
247
+ def self.create_sequence_tc(bound, content)
248
+ new(Rustorb::Native::TC.sequence(content.resolve_for_build, bound), content: content)
249
+ end
250
+
251
+ def self.create_array_tc(length, content)
252
+ new(Rustorb::Native::TC.array(content.resolve_for_build, length), content: content)
253
+ end
254
+
255
+ def self.create_alias_tc(id, name, content)
256
+ new(Rustorb::Native::TC.alias(TK_ALIAS, id, name, content.resolve_for_build), content: content)
257
+ end
258
+
259
+ def self.create_interface_tc(id, name)
260
+ new(Rustorb::Native::TC.interface(TK_OBJREF, id, name))
261
+ end
262
+
263
+ def self.create_abstract_interface_tc(id, name)
264
+ new(Rustorb::Native::TC.interface(TK_ABSTRACT_INTERFACE, id, name))
265
+ end
266
+
267
+ def self.create_local_interface_tc(id, name)
268
+ new(Rustorb::Native::TC.interface(TK_LOCAL_INTERFACE, id, name))
269
+ end
270
+
271
+ def self.create_struct_tc(id, name, members)
272
+ make_struct(TK_STRUCT, id, name, members)
273
+ end
274
+
275
+ def self.create_exception_tc(id, name, members)
276
+ make_struct(TK_EXCEPT, id, name, members)
277
+ end
278
+
279
+ def self.make_struct(kind, id, name, members)
280
+ native = Rustorb::Native::TC.struct(kind, id, name, members.map(&:first),
281
+ members.map { |m| m[1].resolve_for_build })
282
+ Rustorb.register_tc(new(native, members: members))
283
+ end
284
+
285
+ # members: [[label, name, TypeCode]]; the label :default marks the
286
+ # default member.
287
+ def self.create_union_tc(id, name, discriminator, members)
288
+ default = members.index { |m| m[0] == :default } || -1
289
+ labels = members.map { |m| m[0] == :default ? 0 : Rustorb::Marshal.label_bits(m[0], discriminator) }
290
+ native = Rustorb::Native::TC.union(id, name, discriminator.resolve_for_build, labels, default,
291
+ members.map { |m| m[1] }, members.map { |m| m[2].resolve_for_build })
292
+ Rustorb.register_tc(new(native, members: members, discriminator: discriminator))
293
+ end
294
+
295
+ # members: [[name, TypeCode, visibility]] (CORBA::PRIVATE_MEMBER or
296
+ # PUBLIC_MEMBER); modifier CORBA::VM_NONE, VM_CUSTOM, VM_ABSTRACT or
297
+ # VM_TRUNCATABLE; base the concrete base type or nil.
298
+ def self.create_value_tc(id, name, modifier, base, members, kind: TK_VALUE)
299
+ native = Rustorb::Native::TC.value(kind, id, name, modifier, base&.resolve_for_build,
300
+ members.map(&:first), members.map { |m| m[1].resolve_for_build },
301
+ members.map { |m| m[2] })
302
+ tc = new(native, members: members.map { |m| m[0, 2] })
303
+ tc.instance_variable_set(:@concrete_base, base)
304
+ Rustorb.register_tc(tc)
305
+ end
306
+
307
+ def self.create_event_tc(id, name, modifier, base, members)
308
+ create_value_tc(id, name, modifier, base, members, kind: TK_EVENT)
309
+ end
310
+
311
+ def self.create_value_box_tc(id, name, boxed)
312
+ Rustorb.register_tc(new(Rustorb::Native::TC.alias(TK_VALUE_BOX, id, name, boxed.resolve_for_build),
313
+ content: boxed))
314
+ end
315
+
316
+ def self.create_enum_tc(id, name, names)
317
+ Rustorb.register_tc(new(Rustorb::Native::TC.enum(id, name, names), members: names))
318
+ end
319
+
320
+ # IDL 4 int8 (rustorb): the TypeCode of octet, read and written as a
321
+ # signed value.
322
+ def self.create_int8_tc
323
+ @int8_tc ||= new(Rustorb::Native::TC.simple(TK_OCTET), extra: :int8)
324
+ end
325
+
326
+ # An IDL 4 bitset (rustorb): the TypeCode of the unsigned integer kind
327
+ # that holds its bits; values are instances of klass (a Rustorb::Bitset).
328
+ def self._bitset_tc(kind, klass)
329
+ new(Rustorb::Native::TC.simple(kind), extra: klass)
330
+ end
331
+
332
+ def self.create_recursive_tc(id)
333
+ new(Rustorb::Native::TC.recursive(id), recursive_id: id)
334
+ end
335
+
336
+ # The native TypeCode to build a containing one with: a recursive
337
+ # placeholder stays one (the core replaces it).
338
+ def resolve_for_build
339
+ native
340
+ end
341
+ end
342
+
343
+ NULL_TC_KINDS = {
344
+ null: TK_NULL, void: TK_VOID, short: TK_SHORT, long: TK_LONG, ushort: TK_USHORT,
345
+ ulong: TK_ULONG, float: TK_FLOAT, double: TK_DOUBLE, boolean: TK_BOOLEAN, char: TK_CHAR,
346
+ octet: TK_OCTET, any: TK_ANY, TypeCode: TK_TYPECODE, longlong: TK_LONGLONG,
347
+ ulonglong: TK_ULONGLONG, longdouble: TK_LONGDOUBLE, wchar: TK_WCHAR, string: TK_STRING,
348
+ wstring: TK_WSTRING
349
+ }.freeze
350
+
351
+ NULL_TC_KINDS.each do |name, kind|
352
+ define_singleton_method("_tc_#{name}") { TypeCode.simple(kind) }
353
+ end
354
+
355
+ VM_NONE = 0
356
+ VM_CUSTOM = 1
357
+ VM_ABSTRACT = 2
358
+ VM_TRUNCATABLE = 3
359
+ PRIVATE_MEMBER = 0
360
+ PUBLIC_MEMBER = 1
361
+
362
+ def self._tc_Principal
363
+ TypeCode.simple(TK_PRINCIPAL)
364
+ end
365
+
366
+ def self._tc_int8
367
+ TypeCode.create_int8_tc
368
+ end
369
+
370
+ def self._tc_Object
371
+ @tc_object ||= TypeCode.create_interface_tc("IDL:omg.org/CORBA/Object:1.0", "Object")
372
+ end
373
+ end
374
+
375
+ require "monitor"
376
+
377
+ module Rustorb
378
+ @tcs = {}
379
+ @types = {}
380
+ @types_lock = Mutex.new
381
+ @tc_monitor = Monitor.new
382
+ @tc_building = []
383
+
384
+ # The version of the generated code this runtime reads (rustorb-idl
385
+ # -bruby writes Rustorb.check_generated_code(N)).
386
+ GENERATED_CODE_VERSION = 1
387
+
388
+ class << self
389
+ # Named TypeCodes by repository id: what a recursive placeholder stands
390
+ # for.
391
+ def register_tc(tc)
392
+ @types_lock.synchronize { @tcs[tc.id] = tc }
393
+ tc
394
+ end
395
+
396
+ def check_generated_code(version)
397
+ return if version == GENERATED_CODE_VERSION
398
+
399
+ raise LoadError, "code generated for rustorb's Ruby runtime #{version}, this runtime reads #{GENERATED_CODE_VERSION}: regenerate it with a matching rustorb-idl"
400
+ end
401
+
402
+ # The TypeCode of a generated type (holder: its class or module), made
403
+ # by the block on first use. A type met again while its own TypeCode is
404
+ # being made (a recursive struct or union) is a create_recursive_tc
405
+ # placeholder; a TypeCode holding a placeholder of an enclosing type is
406
+ # not kept. An alias (recursive: false) is never a placeholder: a cycle
407
+ # through it goes through a struct or union too.
408
+ def lazy_tc(holder, id, recursive: true)
409
+ tc = holder.instance_variable_get(:@_tc)
410
+ return tc if tc
411
+
412
+ @tc_monitor.synchronize do
413
+ tc = holder.instance_variable_get(:@_tc)
414
+ return tc if tc
415
+
416
+ i = recursive && @tc_building.index { |f| f[0] == id }
417
+ if i
418
+ ((i + 1)...@tc_building.size).each { |j| @tc_building[j][1] = true }
419
+ return CORBA::TypeCode.create_recursive_tc(id)
420
+ end
421
+
422
+ @tc_building.push([id, false])
423
+ begin
424
+ tc = yield
425
+ ensure
426
+ frame = @tc_building.pop
427
+ end
428
+ holder.instance_variable_set(:@_tc, tc) unless frame[1]
429
+ tc
430
+ end
431
+ end
432
+
433
+ def find_tc(id)
434
+ @types_lock.synchronize { @tcs[id] }
435
+ end
436
+
437
+ # The Ruby classes of named types (struct, union, exception), by
438
+ # repository id: what values read from the wire are made of.
439
+ def register_type(klass)
440
+ @types_lock.synchronize { @types[klass::ID] = klass }
441
+ klass
442
+ end
443
+
444
+ def find_type(id)
445
+ @types_lock.synchronize { @types[id] }
446
+ end
447
+ end
448
+ end