-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
863 lines (776 loc) · 25.9 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! This crate exposes a minimal C-interface for Jyafn. The objective here is just to get
//! functions running anywhere (while also providing debugging info).
extern crate jyafn as rust;
#[cfg(test)]
mod test;
use get_size::GetSize;
use rust::{
layout::{Layout, Struct},
Error, Function, Graph,
};
use std::borrow::Cow;
use std::ffi::{c_char, CStr, CString};
use std::panic::UnwindSafe;
use std::sync::atomic::{AtomicIsize, Ordering};
/// Counts the number of strings allocated via `new_c_str` and freed through `free_str`.
/// This is meant for debugging, to detect leakages.
static N_ALLOCATED_STRS: AtomicIsize = AtomicIsize::new(0);
/// Every time this function is called, there needs to be an accompaning `free_str` on the
/// other side.
fn new_c_str(s: String) -> *const c_char {
let c_str = CString::new(s)
.unwrap_or_else(|err| {
CString::new(String::from_utf8_lossy(&err.into_vec()).replace("\u{0}", ""))
.expect("nulls have already been removed")
})
.into_boxed_c_str();
N_ALLOCATED_STRS.fetch_add(1, Ordering::Relaxed);
Box::leak(c_str) as *mut CStr as *const c_char
}
/// # Safety
///
/// Expects `s` to be a pointer _allocated_ C-style string. Only use this function with
/// pointers returned by functions in this library.
#[no_mangle]
pub unsafe extern "C" fn free_str(s: *const c_char) {
let _c_str = Box::from_raw(s as *mut c_char);
N_ALLOCATED_STRS.fetch_add(-1, Ordering::Relaxed);
}
#[no_mangle]
pub extern "C" fn n_allocated_strs() -> isize {
N_ALLOCATED_STRS.load(Ordering::Relaxed)
}
#[no_mangle]
pub extern "C" fn transmute_as_str(s: *mut ()) -> *mut c_char {
s as *mut c_char
}
unsafe fn from_c_str<'a>(s: *const c_char) -> Cow<'a, str> {
let cstr = CStr::from_ptr(s);
cstr.to_string_lossy()
}
/// # Safety
///
/// Expects `bytes` to be a valid pointer to `bytes` data returned by a function in this
/// library.
pub unsafe extern "C" fn get_bytes_ptr(bytes: *const ()) -> *const u8 {
with_unchecked(bytes, |bytes: &Vec<u8>| bytes.as_ptr())
}
/// # Safety
///
/// Expects `bytes` to be a valid pointer to `bytes` data returned by a function in this
/// library.
pub unsafe extern "C" fn get_bytes_len(bytes: *const ()) -> usize {
with_unchecked(bytes, |bytes: &Vec<u8>| bytes.len())
}
/// # Safety
///
/// Expects `bytes` to be a valid pointer to `bytes` data returned by a function in this
/// library. The pointer becomes invalid after it is sent to this function.
pub unsafe extern "C" fn bytes_drop(bytes: *mut ()) {
let _: Box<Vec<u8>> = Box::from_raw(bytes as *mut Vec<u8>);
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct Outcome(*mut ());
impl Outcome {
fn from_result<T>(result: Result<T, Error>) -> Outcome {
match result {
Ok(ok) => {
let boxed = Box::new(ok);
let boxed_result = Box::new(Result::<*mut (), Error>::Ok(
Box::leak(boxed) as *mut T as *mut (),
));
Outcome(Box::leak(boxed_result) as *mut Result<*mut (), Error> as *mut ())
}
Err(error) => {
let boxed_result = Box::new(Result::<*mut (), Error>::Err(error));
Outcome(Box::leak(boxed_result) as *mut Result<*mut (), Error> as *mut ())
}
}
}
}
/// # Safety
///
/// Expects `outcome` to be a valid pointer to an outcome.
#[no_mangle]
pub unsafe extern "C" fn outcome_is_ok(outcome: Outcome) -> bool {
let outcome = Box::from_raw(outcome.0 as *mut Result<*mut (), Error>);
let is_ok = outcome.is_ok();
Box::leak(outcome);
is_ok
}
/// # Safety
///
/// Expects `outcome` to be a valid pointer to an outcome. The pointer becomes invalid
/// once it is passed to this function.
#[no_mangle]
pub unsafe extern "C" fn outcome_consume_ok(outcome: Outcome) -> *mut () {
let outcome = Box::from_raw(outcome.0 as *mut Result<*mut (), Error>);
outcome.expect("is supposed to be ok")
}
/// # Safety
///
/// Expects `outcome` to be a valid pointer to an outcome. The pointer becomes invalid
/// once it is passed to this function. Additionally, expects that the value of the "ok"
/// result is also a valid pointer, if this outcome is ok.
#[no_mangle]
pub unsafe extern "C" fn outcome_consume_ok_ptr(outcome: Outcome) -> *mut () {
let outcome = Box::from_raw(outcome.0 as *mut Result<*mut (), Error>);
let ok = outcome.expect("is supposed to be ok");
let boxed_ptr = Box::from_raw(ok as *mut *mut ());
*boxed_ptr
}
/// # Safety
///
/// Expects `outcome` to be a valid pointer to an outcome. The pointer becomes invalid
/// once it is passed to this function.
#[no_mangle]
pub unsafe extern "C" fn outcome_consume_err(outcome: Outcome) -> *const c_char {
let outcome = Box::from_raw(outcome.0 as *mut Result<*mut (), Error>);
let err = outcome.expect_err("is supposed to be err");
new_c_str(err.to_string())
}
fn panic_to_outcome<F, T>(f: F) -> Outcome
where
F: FnOnce() -> T + UnwindSafe,
{
match std::panic::catch_unwind(f) {
Ok(outcome) => Outcome::from_result::<T>(Ok(outcome)),
Err(_le_oops) => Outcome::from_result::<T>(Err(rust::Error::Other(
"operation panicked (see stderr)".to_string(),
))),
}
}
fn try_panic_to_outcome<F, T>(f: F) -> Outcome
where
F: FnOnce() -> Result<T, Error> + UnwindSafe,
{
match std::panic::catch_unwind(f) {
Ok(result) => Outcome::from_result::<T>(result),
Err(_le_oops) => Outcome::from_result::<T>(Err(rust::Error::Other(
"operation panicked (see stderr)".to_string(),
))),
}
}
/// # Safety
///
/// Expects the `s` and the `fmt` parameters to point to valid C-style strings.
#[no_mangle]
pub unsafe extern "C" fn parse_datetime(s: *const c_char, fmt: *const c_char) -> Outcome {
try_panic_to_outcome(|| {
rust::utils::parse_datetime(&from_c_str(s), &from_c_str(fmt))
.map(|dt| {
Box::leak(Box::new(i64::from(rust::utils::Timestamp::from(dt)))) as *mut i64
as *const i64
})
.map_err(|e| e.to_string().into())
})
}
/// # Safety
///
/// Expects `fmt` to be a valid pointer to a C-style string.
#[no_mangle]
pub unsafe extern "C" fn format_datetime(timestamp: i64, fmt: *const c_char) -> Outcome {
panic_to_outcome(|| new_c_str(rust::utils::format_datetime(timestamp, &from_c_str(fmt))))
}
/// # Safety
///
/// Expects the `ptr` parameter to point to a valid _allocated_ `i64` in the heap. Only
/// use with pointers returned by functions in this library. This pointer becomes invalid
/// after it is passed to this function.
#[no_mangle]
pub unsafe extern "C" fn consume_i64_ptr(ptr: *mut i64) -> i64 {
let boxed = Box::from_raw(ptr);
*boxed
}
unsafe fn with_unchecked<T, U, F>(thing: *const (), f: F) -> U
where
F: FnOnce(&T) -> U,
{
let boxed = Box::from_raw(thing as *mut T);
let outcome = f(&boxed);
Box::leak(boxed);
outcome
}
unsafe fn with<T, U, F>(thing: *const (), f: F) -> Outcome
where
F: FnOnce(&T) -> U + UnwindSafe,
{
panic_to_outcome(|| with_unchecked(thing, f))
}
unsafe fn try_with<T, U, F>(thing: *const (), f: F) -> Outcome
where
F: FnOnce(&T) -> Result<U, Error> + UnwindSafe,
{
try_panic_to_outcome(|| with_unchecked(thing, f))
}
#[allow(dead_code)]
unsafe fn with_mut_unchecked<T, U, F>(thing: *mut (), f: F) -> U
where
F: FnOnce(&mut T) -> U,
{
let mut boxed = Box::from_raw(thing as *mut T);
let outcome = f(&mut boxed);
Box::leak(boxed);
outcome
}
#[allow(dead_code)]
unsafe fn with_mut<T, U, F>(thing: *mut (), f: F) -> Outcome
where
F: FnOnce(&mut T) -> U + UnwindSafe,
{
panic_to_outcome(|| with_mut_unchecked(thing, f))
}
#[allow(dead_code)]
unsafe fn try_with_mut<T, F>(thing: *mut (), f: F) -> Outcome
where
F: FnOnce(&mut T) -> Result<T, Error> + UnwindSafe,
{
try_panic_to_outcome(|| with_mut_unchecked(thing, f))
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_name(graph: *const ()) -> *const c_char {
with_unchecked(graph, |graph: &Graph| new_c_str(graph.name().to_string()))
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph and `key` to be a valid pointer
/// to a C-style string.
#[no_mangle]
pub unsafe extern "C" fn graph_get_metadata(graph: *const (), key: *const c_char) -> *const c_char {
with_unchecked(graph, |graph: &Graph| {
if let Some(value) = graph.metadata().get(&*from_c_str(key)) {
new_c_str(value.to_string())
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_get_metadata_json(graph: *const ()) -> *const c_char {
with_unchecked(graph, |graph: &Graph| {
new_c_str(serde_json::to_string(graph.metadata()).expect("can always serialize json value"))
})
}
/// # Safety
///
/// Expects `bytes` to point to the beginning of a valid byte slice in memory with the size
/// of _at least_ `len`.
#[no_mangle]
pub unsafe extern "C" fn graph_load(bytes: *const u8, len: usize) -> Outcome {
try_panic_to_outcome(|| {
Graph::load(std::io::Cursor::new(std::slice::from_raw_parts(bytes, len)))
})
}
/// # Safety
///
/// Expects the `graph` parameter to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_dump(graph: *const ()) -> Outcome {
try_with(graph, |graph: &Graph| {
let mut buf = std::io::Cursor::new(vec![]);
graph.dump(&mut buf)?;
Ok(buf.into_inner())
})
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_to_json(graph: *const ()) -> *const c_char {
with_unchecked(graph, |graph: &Graph| new_c_str(graph.to_json()))
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_render(graph: *const ()) -> Outcome {
try_with(graph, |graph: &Graph| {
Ok(new_c_str(graph.render()?.to_string()))
})
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_compile(graph: *const ()) -> Outcome {
try_with(graph, |graph: &Graph| graph.compile())
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph.
#[no_mangle]
pub unsafe extern "C" fn graph_clone(graph: *const ()) -> *const () {
with_unchecked(graph, |graph: &Graph| {
let boxed = Box::new(graph.clone());
Box::leak(boxed) as *const Graph as *const ()
})
}
/// # Safety
///
/// Expects `graph` to be a valid pointer to a graph. The pointer becomes invalid
/// after it is passed to this function.
#[no_mangle]
pub unsafe extern "C" fn graph_drop(graph: *mut ()) {
let _ = Box::from_raw(graph as *mut Graph);
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_to_string(layout: *const ()) -> *const c_char {
with_unchecked(layout, |layout: &Layout| new_c_str(layout.to_string()))
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_to_json(layout: *const ()) -> *const c_char {
with_unchecked(layout, |layout: &Layout| {
new_c_str(serde_json::to_string(layout).expect("can always serialize"))
})
}
/// # Safety
///
/// Expects the `json` parameter to be a valid C-style string.
#[no_mangle]
pub unsafe extern "C" fn layout_from_json(json: *const c_char) -> Outcome {
let decode = || -> Result<Layout, Error> {
Ok(serde_json::Deserializer::from_str(&from_c_str(json))
.into_iter::<Layout>()
.next()
.ok_or_else(|| "empty string".to_string())?
.map_err(|err| err.to_string())?)
};
Outcome::from_result(decode())
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_size(layout: *const ()) -> usize {
with_unchecked(layout, |layout: &Layout| layout.size()).in_bytes()
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_unit(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| matches!(layout, Layout::Unit))
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_scalar(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| matches!(layout, Layout::Scalar))
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_bool(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| matches!(layout, Layout::Bool))
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.s
#[no_mangle]
pub unsafe extern "C" fn layout_is_datetime(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| {
matches!(layout, Layout::DateTime(_))
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_symbol(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| matches!(layout, Layout::Symbol))
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_struct(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| {
matches!(layout, Layout::Struct(_))
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_tuple(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| matches!(layout, Layout::Tuple(_)))
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_is_list(layout: *const ()) -> bool {
with_unchecked(layout, |layout: &Layout| {
matches!(layout, Layout::List(_, _))
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_datetime_format(layout: *const ()) -> *const c_char {
with_unchecked(layout, |layout: &Layout| {
if let Layout::DateTime(fmt) = layout {
new_c_str(fmt.clone())
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_as_struct(layout: *const ()) -> *const () {
with_unchecked(layout, |layout: &Layout| {
if let Layout::Struct(s) = layout {
s as *const Struct as *const ()
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects the `strct` parameter to be a valid pointer to a jyafn struct.
#[no_mangle]
pub unsafe extern "C" fn layout_tuple_size(layout: *const ()) -> usize {
with_unchecked(layout, |layout: &Layout| {
if let Layout::Tuple(t) = layout {
return t.len();
}
0
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_get_tuple_item(layout: *const (), index: usize) -> *const () {
with_unchecked(layout, |layout: &Layout| {
if let Layout::Tuple(t) = layout {
if index < t.len() {
return &t[index] as *const Layout as *const ();
}
}
std::ptr::null()
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_list_element(layout: *const ()) -> *const () {
with_unchecked(layout, |layout: &Layout| {
if let Layout::List(el, _) = layout {
el.as_ref() as *const Layout as *const ()
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_list_size(layout: *const ()) -> usize {
with_unchecked(layout, |layout: &Layout| {
if let &Layout::List(_, size) = layout {
size
} else {
0
}
})
}
/// # Safety
///
/// Expects the `layout` and the `other` parameters to be valid pointers to layouts.
#[no_mangle]
pub unsafe extern "C" fn layout_is_superset(layout: *mut (), other: *mut ()) -> bool {
with_unchecked(layout, |layout: &Layout| {
with_unchecked(other, |other: &Layout| layout.is_superset(other))
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout.
#[no_mangle]
pub unsafe extern "C" fn layout_clone(layout: *mut ()) -> *mut Layout {
with_unchecked(layout, |layout: &Layout| {
let boxed = Box::new(layout.clone());
Box::leak(boxed)
})
}
/// # Safety
///
/// Expects the `layout` parameter to be a valid pointer to a layout. The pointer becomes
/// invalid after it is passed to this function.
#[no_mangle]
pub unsafe extern "C" fn layout_drop(layout: *mut ()) {
let _ = Box::from_raw(layout as *mut Layout);
}
/// # Safety
///
/// Expects the `strct` parameter to be a valid pointer to a jyafn struct.
#[no_mangle]
pub unsafe extern "C" fn strct_size(strct: *const ()) -> usize {
with_unchecked(strct, |strct: &Struct| strct.0.len())
}
/// # Safety
///
/// Expects the `strct` parameter to be a valid pointer to a jyafn struct.
#[no_mangle]
pub unsafe extern "C" fn strct_get_item_name(strct: *const (), index: usize) -> *const c_char {
with_unchecked(strct, |strct: &Struct| {
// Remember, cannot panic, ever!
if index < strct.0.len() {
new_c_str(strct.0[index].0.clone())
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects the `strct` parameter to be a valid pointer to a jyafn struct.
#[no_mangle]
pub unsafe extern "C" fn strct_get_item_layout(strct: *const (), index: usize) -> *const () {
with_unchecked(strct, |strct: &Struct| {
// Remember, cannot panic, ever!
if index < strct.0.len() {
&strct.0[index].1 as *const Layout as *const ()
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_name(func: *const ()) -> *const c_char {
with_unchecked(func, |func: &Function| {
new_c_str(func.graph().name().to_string())
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_input_size(func: *const ()) -> usize {
with_unchecked(func, |func: &Function| func.input_size()).in_bytes()
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_output_size(func: *const ()) -> usize {
with_unchecked(func, |func: &Function| func.output_size()).in_bytes()
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_input_layout(func: *const ()) -> *const () {
with_unchecked(func, |func: &Function| {
func.input_layout() as *const Layout as *const ()
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_output_layout(func: *const ()) -> *const () {
with_unchecked(func, |func: &Function| {
func.output_layout() as *const Layout as *const ()
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_graph(func: *const ()) -> *const () {
with_unchecked(func, |func: &Function| {
func.graph() as *const Graph as *const ()
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function and `key` to
/// be a pointer to a C-style string.
#[no_mangle]
pub unsafe extern "C" fn function_get_metadata(
func: *const (),
key: *const c_char,
) -> *const c_char {
with_unchecked(func, |func: &Function| {
if let Some(value) = func.graph().metadata().get(&*from_c_str(key)) {
new_c_str(value.to_string())
} else {
std::ptr::null()
}
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_get_metadata_json(func: *const ()) -> *const c_char {
with_unchecked(func, |func: &Function| {
new_c_str(
serde_json::to_string(func.graph().metadata())
.expect("can always serialize json value"),
)
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_symbols_json(func: *const ()) -> *const c_char {
with_unchecked(func, |func: &Function| {
new_c_str(serde_json::to_string(func.graph().symbols()).expect("can always serialize"))
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_fn_ptr(
func: *const (),
) -> unsafe extern "C" fn(*const u8, *mut u8) -> *mut rust::FnError {
with_unchecked(func, |func: &Function| func.fn_ptr())
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function.
#[no_mangle]
pub unsafe extern "C" fn function_get_size(func: *const ()) -> usize {
with_unchecked(func, |func: &Function| func.get_size())
}
/// # Safety
///
/// Expects `bytes` to point to the beginning of a valid byte slice in memory with the size
/// of _at least_ `len`.
#[no_mangle]
pub unsafe extern "C" fn function_load(bytes: *const u8, len: usize) -> Outcome {
try_panic_to_outcome(|| {
Function::load(std::io::Cursor::new(std::slice::from_raw_parts(bytes, len)))
})
}
/// # Safety
///
/// Expects
/// 1. the `func` parameter to be a valid pointer to a jyafn function
/// 2. the `input` paramenter to be a valid pointer to a slice of size _at least_ the
/// function input size (given by `function_input_size`).
/// 3. the `output` paramenter to be a valid pointer to a slice of size _at least_ the
/// function output size (given by `function_output_size`).
#[no_mangle]
pub unsafe extern "C" fn function_call_raw(
func: *const (),
input: *const u8,
output: *mut u8,
) -> Outcome {
with_unchecked(func, |func: &Function| {
let outcome = std::panic::catch_unwind(|| {
let input = std::slice::from_raw_parts(input, func.input_size().in_bytes());
let output = std::slice::from_raw_parts_mut(output, func.output_size().in_bytes());
let fn_err = func.call_raw(input, output);
if !fn_err.is_null() {
let fn_err = Box::from_raw(fn_err).take();
return Outcome::from_result(Result::<(), Error>::Err(rust::Error::StatusRaised(
fn_err,
)));
}
Outcome::from_result(Result::<(), Error>::Ok(()))
});
match outcome {
Ok(status) => status,
Err(_le_oops) => Outcome::from_result(Result::<(), Error>::Err(
"function raw call panicked (see stderr)".to_string().into(),
)),
}
})
}
/// # Safety
///
/// Expects
/// 1. the `func` parameter to be a valid pointer to a jyafn function
/// 2. the `input` paramenter to be a valid pointer to a slice of size _at least_ the
/// function input size (given by `function_input_size`).
#[no_mangle]
pub unsafe extern "C" fn function_eval_raw(func: *const (), input: *const u8) -> Outcome {
with(func, |func: &Function| {
let input = std::slice::from_raw_parts(input, func.input_size().in_bytes());
Outcome::from_result(
func.eval_raw(input)
.map(|output| Box::leak(output) as *const [u8] as *const ()),
)
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function and `input` to
/// be a pointer to a C-style string.
#[no_mangle]
pub unsafe extern "C" fn function_eval_json(func: *const (), input: *mut c_char) -> Outcome {
try_with(func, |func: &Function| {
let input_cstr = CStr::from_ptr(input);
let input_str = input_cstr.to_string_lossy();
let input_value: serde_json::Value =
serde_json::from_str(input_str.trim()).map_err(|e| e.to_string())?;
let output_value: serde_json::Value = func.eval(&input_value)?;
let output_str = serde_json::to_string(&output_value).expect("can serialize");
let output_cstr = new_c_str(output_str);
Ok(output_cstr)
})
}
/// # Safety
///
/// Expects the `func` parameter to be a valid pointer to a jyafn function. The pointer
/// becomes invalid after it is passed to this function.
#[no_mangle]
pub unsafe extern "C" fn function_drop(func: *mut ()) {
let _ = Box::from_raw(func as *mut Function);
}
// #[no_mangle]
// pub extern "C" fn pfunc_inscribe(
// name: *const c_char,
// fn_ptr: *const (),
// signature: *const u8,
// signature_len: usize,
// returns: u8,
// ) -> Outcome {
// unsafe {
// try_panic_to_outcome(|| {
// from_ptr_result((|| {
// let name_cstr = CStr::from_ptr(name);
// let name_str = name_cstr.to_string_lossy();
// let signature = std::slice::from_raw_parts(signature, signature_len)
// .iter()
// .copied()
// .map(rust::Type::try_from)
// .collect::<Result<Vec<_>, _>>()?;
// let returns: rust::Type = returns.try_into()?;
// rust::pfunc::inscribe(&name_str, fn_ptr, &signature, returns)
// .map(|_| std::ptr::null::<()>())
// })())
// })
// }
// }