1use std::ffi::{c_char, c_void};
10
11use rustorb_cdr::{ByteOrder, Decoder, Encoder, Encoding};
12use rustorb_core::{
13 Any, CompletionStatus, InStream, ObjectRef, Orb, OutStream, SystemExceptionKind, TCKind,
14 TypeCode, TypeCodeError,
15};
16
17use crate::host;
18use crate::stream::{In, Out, RorbIn, RorbOut};
19use crate::{
20 CALLBACK_FAILED, Exception, OK, Status, bad_param, guard, into_handle, local, release_handle,
21 set_local, set_system, str_arg, sys,
22};
23
24unsafe fn tc_error(exc: *mut Exception, e: TypeCodeError) -> Status {
27 match e {
28 TypeCodeError::BadKind => unsafe { set_local(exc, local::TC_BAD_KIND, 0) },
29 TypeCodeError::Bounds => unsafe { set_local(exc, local::TC_BOUNDS, 0) },
30 TypeCodeError::BadParam => unsafe { set_system(exc, &bad_param()) },
31 }
32}
33
34unsafe fn put<T>(
36 exc: *mut Exception,
37 out: *mut T,
38 f: impl FnOnce() -> Result<T, TypeCodeError>,
39) -> Status {
40 guard(exc, || match f() {
41 Ok(v) => {
42 unsafe { out.write(v) };
43 OK
44 }
45 Err(e) => unsafe { tc_error(exc, e) },
46 })
47}
48
49unsafe fn put_tc(
51 exc: *mut Exception,
52 out: *mut *mut TypeCode,
53 f: impl FnOnce() -> Result<TypeCode, TypeCodeError>,
54) -> Status {
55 unsafe { put(exc, out, || f().map(into_handle)) }
56}
57
58unsafe fn name_arg<'a>(p: *const c_char, len: usize) -> Result<&'a str, TypeCodeError> {
61 unsafe { str_arg(p, len) }.map_err(|_| TypeCodeError::BadParam)
62}
63
64unsafe fn tc_arg<'a>(tc: *const TypeCode) -> Result<&'a TypeCode, TypeCodeError> {
65 unsafe { tc.as_ref() }.ok_or(TypeCodeError::BadParam)
66}
67
68unsafe fn members<'a>(
70 count: usize,
71 names: *const *const c_char,
72 name_lens: *const usize,
73 types: *const *const TypeCode,
74) -> Result<Vec<(&'a str, TypeCode)>, TypeCodeError> {
75 let mut out = Vec::with_capacity(count);
76 for i in 0..count {
77 let name = unsafe { name_arg(*names.add(i), *name_lens.add(i)) }?;
78 let tc = unsafe { tc_arg(*types.add(i)) }?;
79 out.push((name, tc.clone()));
80 }
81 Ok(out)
82}
83
84#[unsafe(no_mangle)]
85pub unsafe extern "C" fn rorb_tc_clone(tc: *const TypeCode) -> *mut TypeCode {
86 match unsafe { tc.as_ref() } {
87 Some(tc) => into_handle(tc.clone()),
88 None => std::ptr::null_mut(),
89 }
90}
91
92#[unsafe(no_mangle)]
93pub unsafe extern "C" fn rorb_tc_release(tc: *mut TypeCode) {
94 unsafe { release_handle(tc) }
95}
96
97#[unsafe(no_mangle)]
98pub unsafe extern "C" fn rorb_tc_simple(
99 kind: u32,
100 out: *mut *mut TypeCode,
101 exc: *mut Exception,
102) -> Status {
103 unsafe {
104 put_tc(exc, out, || {
105 let kind = TCKind::from_u32(kind).ok_or(TypeCodeError::BadParam)?;
106 match kind {
107 TCKind::String => Ok(TypeCode::string(0)),
108 TCKind::WString => Ok(TypeCode::wstring(0)),
109 _ => TypeCode::simple(kind),
110 }
111 })
112 }
113}
114
115#[unsafe(no_mangle)]
116pub unsafe extern "C" fn rorb_tc_string(
117 wide: i32,
118 bound: u32,
119 out: *mut *mut TypeCode,
120 exc: *mut Exception,
121) -> Status {
122 unsafe {
123 put_tc(exc, out, || {
124 Ok(if wide != 0 {
125 TypeCode::wstring(bound)
126 } else {
127 TypeCode::string(bound)
128 })
129 })
130 }
131}
132
133#[unsafe(no_mangle)]
134pub unsafe extern "C" fn rorb_tc_fixed(
135 digits: u16,
136 scale: i16,
137 out: *mut *mut TypeCode,
138 exc: *mut Exception,
139) -> Status {
140 unsafe { put_tc(exc, out, || TypeCode::fixed(digits, scale)) }
141}
142
143#[unsafe(no_mangle)]
144pub unsafe extern "C" fn rorb_tc_sequence(
145 content: *const TypeCode,
146 bound: u32,
147 out: *mut *mut TypeCode,
148 exc: *mut Exception,
149) -> Status {
150 unsafe { put_tc(exc, out, || TypeCode::sequence(tc_arg(content)?, bound)) }
151}
152
153#[unsafe(no_mangle)]
154pub unsafe extern "C" fn rorb_tc_array(
155 content: *const TypeCode,
156 length: u32,
157 out: *mut *mut TypeCode,
158 exc: *mut Exception,
159) -> Status {
160 unsafe { put_tc(exc, out, || TypeCode::array(tc_arg(content)?, length)) }
161}
162
163#[unsafe(no_mangle)]
164pub unsafe extern "C" fn rorb_tc_alias(
165 kind: u32,
166 id: *const c_char,
167 id_len: usize,
168 name: *const c_char,
169 name_len: usize,
170 content: *const TypeCode,
171 out: *mut *mut TypeCode,
172 exc: *mut Exception,
173) -> Status {
174 unsafe {
175 put_tc(exc, out, || {
176 let id = name_arg(id, id_len)?;
177 let name = name_arg(name, name_len)?;
178 let content = tc_arg(content)?;
179 match TCKind::from_u32(kind) {
180 Some(TCKind::Alias) => TypeCode::alias(id, name, content),
181 Some(TCKind::ValueBox) => TypeCode::value_box(id, name, content),
182 _ => Err(TypeCodeError::BadParam),
183 }
184 })
185 }
186}
187
188#[unsafe(no_mangle)]
189pub unsafe extern "C" fn rorb_tc_interface(
190 kind: u32,
191 id: *const c_char,
192 id_len: usize,
193 name: *const c_char,
194 name_len: usize,
195 out: *mut *mut TypeCode,
196 exc: *mut Exception,
197) -> Status {
198 unsafe {
199 put_tc(exc, out, || {
200 let kind = TCKind::from_u32(kind).ok_or(TypeCodeError::BadParam)?;
201 TypeCode::interface(kind, name_arg(id, id_len)?, name_arg(name, name_len)?)
202 })
203 }
204}
205
206#[unsafe(no_mangle)]
207pub unsafe extern "C" fn rorb_tc_struct(
208 kind: u32,
209 id: *const c_char,
210 id_len: usize,
211 name: *const c_char,
212 name_len: usize,
213 count: usize,
214 names: *const *const c_char,
215 name_lens: *const usize,
216 types: *const *const TypeCode,
217 out: *mut *mut TypeCode,
218 exc: *mut Exception,
219) -> Status {
220 unsafe {
221 put_tc(exc, out, || {
222 let id = name_arg(id, id_len)?;
223 let name = name_arg(name, name_len)?;
224 let members = members(count, names, name_lens, types)?;
225 match TCKind::from_u32(kind) {
226 Some(TCKind::Struct) => TypeCode::struct_tc(id, name, &members),
227 Some(TCKind::Except) => TypeCode::exception(id, name, &members),
228 _ => Err(TypeCodeError::BadParam),
229 }
230 })
231 }
232}
233
234#[unsafe(no_mangle)]
235pub unsafe extern "C" fn rorb_tc_union(
236 id: *const c_char,
237 id_len: usize,
238 name: *const c_char,
239 name_len: usize,
240 discriminator: *const TypeCode,
241 count: usize,
242 labels: *const u64,
243 default_index: i32,
244 names: *const *const c_char,
245 name_lens: *const usize,
246 types: *const *const TypeCode,
247 out: *mut *mut TypeCode,
248 exc: *mut Exception,
249) -> Status {
250 unsafe {
251 put_tc(exc, out, || {
252 let id = name_arg(id, id_len)?;
253 let name = name_arg(name, name_len)?;
254 let disc = tc_arg(discriminator)?;
255 let unsigned = disc.unaliased().kind() == TCKind::ULongLong;
256 let members = members(count, names, name_lens, types)?
257 .into_iter()
258 .enumerate()
259 .map(|(i, (n, tc))| {
260 let bits = *labels.add(i);
261 let label = if i as i32 == default_index {
262 None
263 } else if unsigned {
264 Some(i128::from(bits))
265 } else {
266 Some(i128::from(bits as i64))
267 };
268 (label, n, tc)
269 })
270 .collect::<Vec<_>>();
271 TypeCode::union_tc(id, name, disc, &members)
272 })
273 }
274}
275
276#[unsafe(no_mangle)]
277pub unsafe extern "C" fn rorb_tc_enum(
278 id: *const c_char,
279 id_len: usize,
280 name: *const c_char,
281 name_len: usize,
282 count: usize,
283 names: *const *const c_char,
284 name_lens: *const usize,
285 out: *mut *mut TypeCode,
286 exc: *mut Exception,
287) -> Status {
288 unsafe {
289 put_tc(exc, out, || {
290 let id = name_arg(id, id_len)?;
291 let name = name_arg(name, name_len)?;
292 let names = (0..count)
293 .map(|i| name_arg(*names.add(i), *name_lens.add(i)))
294 .collect::<Result<Vec<_>, _>>()?;
295 TypeCode::enum_tc(id, name, &names)
296 })
297 }
298}
299
300#[unsafe(no_mangle)]
304pub unsafe extern "C" fn rorb_tc_value(
305 kind: u32,
306 id: *const c_char,
307 id_len: usize,
308 name: *const c_char,
309 name_len: usize,
310 modifier: i16,
311 base: *const TypeCode,
312 count: usize,
313 names: *const *const c_char,
314 name_lens: *const usize,
315 types: *const *const TypeCode,
316 visibilities: *const i16,
317 out: *mut *mut TypeCode,
318 exc: *mut Exception,
319) -> Status {
320 unsafe {
321 put_tc(exc, out, || {
322 let kind = TCKind::from_u32(kind).ok_or(TypeCodeError::BadParam)?;
323 let id = name_arg(id, id_len)?;
324 let name = name_arg(name, name_len)?;
325 let members: Vec<(&str, TypeCode, i16)> = members(count, names, name_lens, types)?
326 .into_iter()
327 .enumerate()
328 .map(|(i, (n, t))| (n, t, *visibilities.add(i)))
329 .collect();
330 TypeCode::value_tc(kind, id, name, modifier, base.as_ref(), &members)
331 })
332 }
333}
334
335#[unsafe(no_mangle)]
336pub unsafe extern "C" fn rorb_tc_recursive(
337 id: *const c_char,
338 id_len: usize,
339 out: *mut *mut TypeCode,
340 exc: *mut Exception,
341) -> Status {
342 unsafe { put_tc(exc, out, || Ok(TypeCode::recursive(name_arg(id, id_len)?))) }
343}
344
345#[unsafe(no_mangle)]
346pub unsafe extern "C" fn rorb_tc_kind(tc: *const TypeCode) -> u32 {
347 unsafe { &*tc }.kind().as_u32()
348}
349
350unsafe fn put_str(
352 exc: *mut Exception,
353 p: *mut *const c_char,
354 len: *mut usize,
355 f: impl FnOnce() -> Result<*const str, TypeCodeError>,
356) -> Status {
357 let mut s: *const str = "";
358 let status = unsafe { put(exc, &mut s, f) };
359 if status == OK {
360 let s = unsafe { &*s };
361 unsafe {
362 p.write(s.as_ptr() as *const c_char);
363 len.write(s.len());
364 }
365 }
366 status
367}
368
369#[unsafe(no_mangle)]
370pub unsafe extern "C" fn rorb_tc_id(
371 tc: *const TypeCode,
372 p: *mut *const c_char,
373 len: *mut usize,
374 exc: *mut Exception,
375) -> Status {
376 unsafe { put_str(exc, p, len, || (*tc).id().map(|s| s as *const str)) }
377}
378
379#[unsafe(no_mangle)]
380pub unsafe extern "C" fn rorb_tc_name(
381 tc: *const TypeCode,
382 p: *mut *const c_char,
383 len: *mut usize,
384 exc: *mut Exception,
385) -> Status {
386 unsafe { put_str(exc, p, len, || (*tc).name().map(|s| s as *const str)) }
387}
388
389#[unsafe(no_mangle)]
390pub unsafe extern "C" fn rorb_tc_member_count(
391 tc: *const TypeCode,
392 out: *mut u32,
393 exc: *mut Exception,
394) -> Status {
395 unsafe { put(exc, out, || (*tc).member_count()) }
396}
397
398#[unsafe(no_mangle)]
399pub unsafe extern "C" fn rorb_tc_member_name(
400 tc: *const TypeCode,
401 index: u32,
402 p: *mut *const c_char,
403 len: *mut usize,
404 exc: *mut Exception,
405) -> Status {
406 unsafe {
407 put_str(exc, p, len, || {
408 (*tc).member_name(index).map(|s| s as *const str)
409 })
410 }
411}
412
413#[unsafe(no_mangle)]
414pub unsafe extern "C" fn rorb_tc_member_type(
415 tc: *const TypeCode,
416 index: u32,
417 out: *mut *mut TypeCode,
418 exc: *mut Exception,
419) -> Status {
420 unsafe { put_tc(exc, out, || (*tc).member_type(index)) }
421}
422
423#[unsafe(no_mangle)]
424pub unsafe extern "C" fn rorb_tc_member_label(
425 tc: *const TypeCode,
426 index: u32,
427 out: *mut *mut Any,
428 exc: *mut Exception,
429) -> Status {
430 unsafe { put(exc, out, || (*tc).member_label(index).map(into_handle)) }
431}
432
433#[unsafe(no_mangle)]
434pub unsafe extern "C" fn rorb_tc_discriminator_type(
435 tc: *const TypeCode,
436 out: *mut *mut TypeCode,
437 exc: *mut Exception,
438) -> Status {
439 unsafe { put_tc(exc, out, || (*tc).discriminator_type()) }
440}
441
442#[unsafe(no_mangle)]
443pub unsafe extern "C" fn rorb_tc_default_index(
444 tc: *const TypeCode,
445 out: *mut i32,
446 exc: *mut Exception,
447) -> Status {
448 unsafe { put(exc, out, || (*tc).default_index()) }
449}
450
451#[unsafe(no_mangle)]
452pub unsafe extern "C" fn rorb_tc_length(
453 tc: *const TypeCode,
454 out: *mut u32,
455 exc: *mut Exception,
456) -> Status {
457 unsafe { put(exc, out, || (*tc).length()) }
458}
459
460#[unsafe(no_mangle)]
461pub unsafe extern "C" fn rorb_tc_content_type(
462 tc: *const TypeCode,
463 out: *mut *mut TypeCode,
464 exc: *mut Exception,
465) -> Status {
466 unsafe { put_tc(exc, out, || (*tc).content_type()) }
467}
468
469#[unsafe(no_mangle)]
470pub unsafe extern "C" fn rorb_tc_fixed_digits(
471 tc: *const TypeCode,
472 out: *mut u16,
473 exc: *mut Exception,
474) -> Status {
475 unsafe { put(exc, out, || (*tc).fixed_digits()) }
476}
477
478#[unsafe(no_mangle)]
479pub unsafe extern "C" fn rorb_tc_fixed_scale(
480 tc: *const TypeCode,
481 out: *mut i16,
482 exc: *mut Exception,
483) -> Status {
484 unsafe { put(exc, out, || (*tc).fixed_scale()) }
485}
486
487#[unsafe(no_mangle)]
488pub unsafe extern "C" fn rorb_tc_member_visibility(
489 tc: *const TypeCode,
490 index: u32,
491 out: *mut i16,
492 exc: *mut Exception,
493) -> Status {
494 unsafe { put(exc, out, || (*tc).member_visibility(index)) }
495}
496
497#[unsafe(no_mangle)]
498pub unsafe extern "C" fn rorb_tc_type_modifier(
499 tc: *const TypeCode,
500 out: *mut i16,
501 exc: *mut Exception,
502) -> Status {
503 unsafe { put(exc, out, || (*tc).type_modifier()) }
504}
505
506#[unsafe(no_mangle)]
508pub unsafe extern "C" fn rorb_tc_concrete_base_type(
509 tc: *const TypeCode,
510 out: *mut *mut TypeCode,
511 exc: *mut Exception,
512) -> Status {
513 unsafe {
514 put(exc, out, || {
515 (*tc)
516 .concrete_base_type()
517 .map(|b| b.map_or(std::ptr::null_mut(), into_handle))
518 })
519 }
520}
521
522#[unsafe(no_mangle)]
523pub unsafe extern "C" fn rorb_tc_equal(a: *const TypeCode, b: *const TypeCode) -> i32 {
524 i32::from(unsafe { (*a).equal(&*b) })
525}
526
527#[unsafe(no_mangle)]
528pub unsafe extern "C" fn rorb_tc_equivalent(a: *const TypeCode, b: *const TypeCode) -> i32 {
529 i32::from(unsafe { (*a).equivalent(&*b) })
530}
531
532#[unsafe(no_mangle)]
533pub unsafe extern "C" fn rorb_tc_compact(tc: *const TypeCode) -> *mut TypeCode {
534 into_handle(unsafe { &*tc }.compact())
535}
536
537#[unsafe(no_mangle)]
538pub unsafe extern "C" fn rorb_tc_unaliased(tc: *const TypeCode) -> *mut TypeCode {
539 into_handle(unsafe { &*tc }.unaliased())
540}
541
542pub type WriteFn = Option<unsafe extern "C" fn(*mut c_void, *mut RorbOut) -> Status>;
546pub type ReadFn = Option<unsafe extern "C" fn(*mut c_void, *mut RorbIn) -> Status>;
548
549#[unsafe(no_mangle)]
550pub extern "C" fn rorb_any_new() -> *mut Any {
551 into_handle(Any::default())
552}
553
554#[unsafe(no_mangle)]
558pub unsafe extern "C" fn rorb_any_typed(tc: *const TypeCode) -> *mut Any {
559 into_handle(Any::new(unsafe { &*tc }.clone(), rustorb_core::Value::Void))
560}
561
562#[unsafe(no_mangle)]
563pub unsafe extern "C" fn rorb_any_clone(any: *const Any) -> *mut Any {
564 match unsafe { any.as_ref() } {
565 Some(any) => into_handle(any.clone()),
566 None => std::ptr::null_mut(),
567 }
568}
569
570#[unsafe(no_mangle)]
571pub unsafe extern "C" fn rorb_any_release(any: *mut Any) {
572 if !any.is_null() {
576 rustorb_core::Value::Any(unsafe { Box::from_raw(any) }).release();
577 }
578}
579
580#[unsafe(no_mangle)]
581pub unsafe extern "C" fn rorb_any_type(any: *const Any) -> *mut TypeCode {
582 into_handle(unsafe { &*any }.typecode().clone())
583}
584
585#[unsafe(no_mangle)]
587pub unsafe extern "C" fn rorb_any_set_type(
588 any: *mut Any,
589 tc: *const TypeCode,
590 exc: *mut Exception,
591) -> Status {
592 guard(exc, || {
593 let any = unsafe { &mut *any };
594 let tc = unsafe { &*tc };
595 if !any.typecode().equivalent(tc) {
596 let e = sys(SystemExceptionKind::BAD_TYPECODE, 0, CompletionStatus::No);
597 return unsafe { set_system(exc, &e) };
598 }
599 let value = std::mem::take(any).into_parts().1;
600 *any = Any::new(tc.clone(), value);
601 OK
602 })
603}
604
605#[unsafe(no_mangle)]
608pub unsafe extern "C" fn rorb_any_equal(a: *const Any, b: *const Any) -> i32 {
609 let (a, b) = unsafe { (&*a, &*b) };
610 i32::from(a.typecode().equal(b.typecode()) && a.value() == b.value())
611}
612
613#[unsafe(no_mangle)]
616pub unsafe extern "C" fn rorb_any_label(any: *const Any, label: *mut u64) -> i32 {
617 match unsafe { &*any }.value().label_key() {
618 Some(v) => {
619 unsafe { label.write(v as u64) };
620 1
621 }
622 None => 0,
623 }
624}
625
626pub(crate) fn default_orb() -> Option<Orb> {
629 crate::orb::default_orb()
630}
631
632pub(crate) fn encoder() -> Encoder {
633 Encoder::new(ByteOrder::Big, Encoding::Giop1_2)
634}
635
636fn exception_id(tc: &TypeCode) -> Option<String> {
639 let tc = tc.unaliased();
640 (tc.kind() == TCKind::Except).then(|| tc.id().unwrap_or_default().to_owned())
641}
642
643#[unsafe(no_mangle)]
646pub unsafe extern "C" fn rorb_any_insert(
647 tc: *const TypeCode,
648 write: WriteFn,
649 context: *mut c_void,
650 out: *mut *mut Any,
651 exc: *mut Exception,
652) -> Status {
653 guard(exc, || {
654 let tc = unsafe { &*tc };
655 let mut enc = encoder();
656 let mut orb = None;
657 let written;
658 {
659 let mut stream = OutStream::standalone(&mut enc);
660 if let Some(id) = exception_id(tc) {
661 if let Err(e) = stream.write_string(&id) {
662 return unsafe { set_system(exc, &e) };
663 }
664 }
665 let mut out = Out::standalone(&mut stream);
666 out.written = Some(Vec::new());
667 if let Some(write) = write {
668 crate::stack::commit();
669 let status = unsafe { write(context, out.as_ptr()) };
670 if status != OK {
671 return CALLBACK_FAILED;
672 }
673 }
674 if let Some(e) = out.error.take() {
675 return unsafe { set_system(exc, &e) };
676 }
677 if let Some(o) = out.objects.first() {
678 orb = Some(o.orb().clone());
679 }
680 written = out.written.take().unwrap_or_default();
681 }
682 let orb = orb.or_else(default_orb);
683 let bytes = enc.into_bytes();
684 let mut input = InStream::standalone(
685 Decoder::new(&bytes, ByteOrder::Big, Encoding::Giop1_2),
686 orb.as_ref(),
687 );
688 for w in &written {
691 if let Some(v) = host::lookup(w.key) {
692 input.preset_value(w.slot, v);
693 }
694 }
695 match input.read_value(tc) {
696 Ok(v) => {
697 for w in written {
698 let Some(r) = input.value_at(w.slot) else {
699 continue;
700 };
701 if r.native().is_some() {
702 continue;
704 }
705 if let Some(h) = w.host {
706 let (ptr, release) = h.take();
707 host::attach(r, w.key, ptr, release);
708 }
709 }
710 unsafe { out.write(into_handle(Any::new(tc.clone(), v))) };
711 OK
712 }
713 Err(e) => unsafe { set_system(exc, &e) },
714 }
715 })
716}
717
718#[unsafe(no_mangle)]
721pub unsafe extern "C" fn rorb_any_extract(
722 any: *const Any,
723 read: ReadFn,
724 context: *mut c_void,
725 exc: *mut Exception,
726) -> Status {
727 guard(exc, || {
728 let any = unsafe { &*any };
729 let mut enc = encoder();
730 let bound;
731 {
732 let mut stream = OutStream::standalone(&mut enc);
733 stream.log_values();
734 if let Err(e) = stream.write_value(any.typecode(), any.value()) {
735 return unsafe { set_system(exc, &e) };
736 }
737 bound = stream.written_values();
738 }
739 let orb = first_object(any.value())
740 .map(|o| o.orb().clone())
741 .or_else(default_orb);
742 let bytes = enc.into_bytes();
743 let mut stream = InStream::standalone(
744 Decoder::new(&bytes, ByteOrder::Big, Encoding::Giop1_2),
745 orb.as_ref(),
746 );
747 if exception_id(any.typecode()).is_some() {
748 if let Err(e) = stream.read_string() {
749 return unsafe { set_system(exc, &e) };
750 }
751 }
752 let mut input = In::new(&mut stream, CompletionStatus::No);
753 input.bound = Some(bound);
754 if let Some(read) = read {
755 crate::stack::commit();
756 let status = unsafe { read(context, input.as_ptr()) };
757 if status != OK {
758 return CALLBACK_FAILED;
759 }
760 }
761 if let Some(e) = input.error.take() {
762 return unsafe { set_system(exc, &e) };
763 }
764 let bound = input.bound.take().unwrap_or_default();
767 for (held, v) in input.values.iter_mut().zip(&bound) {
768 let Some(held) = held else { continue };
769 let Some(key) = held.key else { continue };
770 if v.native().is_none() {
771 let (ptr, release) = held.take();
772 host::attach(v, key, ptr, release);
773 }
774 }
775 OK
776 })
777}
778
779pub(crate) fn first_object(v: &rustorb_core::Value) -> Option<&ObjectRef> {
781 use rustorb_core::Value;
782 match v {
783 Value::Objref(Some(o)) => Some(o),
784 Value::List(l) => l.iter().find_map(first_object),
785 Value::Union {
786 member: Some((_, m)),
787 ..
788 } => first_object(m),
789 Value::Any(a) => first_object(a.value()),
790 _ => None,
791 }
792}
793
794#[unsafe(no_mangle)]
795pub unsafe extern "C" fn rorb_out_write_typecode(out: *mut RorbOut, tc: *const TypeCode) {
796 unsafe { &mut *out }.with(|s| s.write_typecode(unsafe { &*tc }));
797}
798
799#[unsafe(no_mangle)]
800pub unsafe extern "C" fn rorb_out_write_any(out: *mut RorbOut, any: *const Any) {
801 unsafe { &mut *out }.with(|s| s.write_any(unsafe { &*any }));
802}
803
804#[unsafe(no_mangle)]
806pub unsafe extern "C" fn rorb_in_read_typecode(input: *mut RorbIn) -> *mut TypeCode {
807 match unsafe { &mut *input }.read(|s| s.read_typecode().map(Some)) {
808 Some(tc) => into_handle(tc),
809 None => std::ptr::null_mut(),
810 }
811}
812
813#[unsafe(no_mangle)]
815pub unsafe extern "C" fn rorb_in_read_any(input: *mut RorbIn) -> *mut Any {
816 match unsafe { &mut *input }.read(|s| s.read_any().map(Some)) {
817 Some(any) => into_handle(any),
818 None => std::ptr::null_mut(),
819 }
820}
821
822#[unsafe(no_mangle)]
824pub unsafe extern "C" fn rorb_out_write_any_value(out: *mut RorbOut, any: *const Any) {
825 let any = unsafe { &*any };
826 unsafe { &mut *out }.with(|s| s.write_value(any.typecode(), any.value()));
827}
828
829#[unsafe(no_mangle)]
831pub unsafe extern "C" fn rorb_in_read_any_value(
832 input: *mut RorbIn,
833 tc: *const TypeCode,
834) -> *mut Any {
835 let tc = unsafe { &*tc };
836 match unsafe { &mut *input }.read(|s| s.read_value(tc).map(Some)) {
837 Some(v) => into_handle(Any::new(tc.clone(), v)),
838 None => std::ptr::null_mut(),
839 }
840}