extism 1.21.0

Extism runtime and Rust SDK
Documentation
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
#![allow(clippy::missing_safety_doc)]

use std::{os::raw::c_char, ptr::null_mut};

use crate::*;

pub type ExtismMemoryHandle = u64;
pub type Size = u64;
pub struct ExtismFunction(std::cell::Cell<Option<Function>>);

/// The return code used to specify a successful plugin call
pub static EXTISM_SUCCESS: i32 = 0;

fn make_error_msg(s: String) -> Vec<u8> {
    let mut s = s.into_bytes();
    s.push(0);
    s
}

/// A union type for host function argument/return values
#[repr(C)]
pub union ValUnion {
    i32: i32,
    i64: i64,
    f32: f32,
    f64: f64,
    // TODO: v128, ExternRef, FuncRef
}

/// `ExtismVal` holds the type and value of a function argument/return
#[repr(C)]
pub struct ExtismVal {
    t: ValType,
    v: ValUnion,
}

/// Host function signature
pub type ExtismFunctionType = extern "C" fn(
    plugin: *mut CurrentPlugin,
    inputs: *const ExtismVal,
    n_inputs: Size,
    outputs: *mut ExtismVal,
    n_outputs: Size,
    data: *mut std::ffi::c_void,
);

/// Log drain callback
pub type ExtismLogDrainFunctionType = extern "C" fn(data: *const std::ffi::c_char, size: Size);

impl ExtismVal {
    fn from_val(value: &wasmtime::Val, ctx: impl AsContext) -> Result<Self, Error> {
        match value.ty(ctx)? {
            wasmtime::ValType::I32 => Ok(ExtismVal {
                t: ValType::I32,
                v: ValUnion {
                    i32: value.unwrap_i32(),
                },
            }),
            wasmtime::ValType::I64 => Ok(ExtismVal {
                t: ValType::I64,
                v: ValUnion {
                    i64: value.unwrap_i64(),
                },
            }),
            wasmtime::ValType::F32 => Ok(ExtismVal {
                t: ValType::F32,
                v: ValUnion {
                    f32: value.unwrap_f32(),
                },
            }),
            wasmtime::ValType::F64 => Ok(ExtismVal {
                t: ValType::F64,
                v: ValUnion {
                    f64: value.unwrap_f64(),
                },
            }),
            t => todo!("{}", t),
        }
    }
}

/// Get a plugin's ID, the returned bytes are a 16 byte buffer that represent a UUIDv4
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_id(plugin: *mut Plugin) -> *const u8 {
    if plugin.is_null() {
        return std::ptr::null_mut();
    }

    let plugin = &mut *plugin;
    plugin.id.as_bytes().as_ptr()
}

/// Get the current plugin's associated host context data. Returns null if call was made without
/// host context.
#[no_mangle]
pub unsafe extern "C" fn extism_current_plugin_host_context(
    plugin: *mut CurrentPlugin,
) -> *mut std::ffi::c_void {
    if plugin.is_null() {
        return std::ptr::null_mut();
    }

    let plugin = &mut *plugin;
    if let Ok(CVoidContainer(ptr)) = plugin.host_context::<CVoidContainer>() {
        *ptr
    } else {
        std::ptr::null_mut()
    }
}

/// Returns a pointer to the memory of the currently running plugin
/// NOTE: this should only be called from host functions.
#[no_mangle]
pub unsafe extern "C" fn extism_current_plugin_memory(plugin: *mut CurrentPlugin) -> *mut u8 {
    if plugin.is_null() {
        return std::ptr::null_mut();
    }

    let plugin = &mut *plugin;
    plugin.memory_ptr()
}

/// Allocate a memory block in the currently running plugin
/// NOTE: this should only be called from host functions.
#[no_mangle]
pub unsafe extern "C" fn extism_current_plugin_memory_alloc(
    plugin: *mut CurrentPlugin,
    n: Size,
) -> ExtismMemoryHandle {
    if plugin.is_null() {
        return 0;
    }

    let plugin = &mut *plugin;
    match plugin.memory_alloc(n) {
        Ok(x) => x.offset(),
        Err(_) => 0,
    }
}

/// Get the length of an allocated block
/// NOTE: this should only be called from host functions.
#[no_mangle]
pub unsafe extern "C" fn extism_current_plugin_memory_length(
    plugin: *mut CurrentPlugin,
    n: ExtismMemoryHandle,
) -> Size {
    if plugin.is_null() {
        return 0;
    }

    let plugin = &mut *plugin;
    plugin.memory_length(n).unwrap_or_default()
}

/// Free an allocated memory block
/// NOTE: this should only be called from host functions.
#[no_mangle]
pub unsafe extern "C" fn extism_current_plugin_memory_free(
    plugin: *mut CurrentPlugin,
    ptr: ExtismMemoryHandle,
) {
    if plugin.is_null() {
        return;
    }

    let plugin = &mut *plugin;
    if let Some(handle) = plugin.memory_handle(ptr) {
        let _ = plugin.memory_free(handle);
    }
}

/// Create a new host function
///
/// Arguments
/// - `name`: function name, this should be valid UTF-8
/// - `inputs`: argument types
/// - `n_inputs`: number of argument types
/// - `outputs`: return types
/// - `n_outputs`: number of return types
/// - `func`: the function to call
/// - `user_data`: a pointer that will be passed to the function when it's called
///   this value should live as long as the function exists
/// - `free_user_data`: a callback to release the `user_data` value when the resulting
///   `ExtismFunction` is freed.
///
/// Returns a new `ExtismFunction` or `null` if the `name` argument is invalid.
#[no_mangle]
pub unsafe extern "C" fn extism_function_new(
    name: *const std::ffi::c_char,
    inputs: *const ValType,
    n_inputs: Size,
    outputs: *const ValType,
    n_outputs: Size,
    func: ExtismFunctionType,
    user_data: *mut std::ffi::c_void,
    free_user_data: Option<extern "C" fn(_: *mut std::ffi::c_void)>,
) -> *mut ExtismFunction {
    let name = match std::ffi::CStr::from_ptr(name).to_str() {
        Ok(x) => x.to_string(),
        Err(_) => {
            return std::ptr::null_mut();
        }
    };

    let inputs = if inputs.is_null() || n_inputs == 0 {
        &[]
    } else {
        std::slice::from_raw_parts(inputs, n_inputs as usize)
    }
    .to_vec();

    let output_types = if outputs.is_null() || n_outputs == 0 {
        &[]
    } else {
        std::slice::from_raw_parts(outputs, n_outputs as usize)
    }
    .to_vec();

    let user_data: UserData<()> = UserData::new_pointer(user_data, free_user_data);
    let f = Function::new(
        name,
        inputs,
        output_types.clone(),
        user_data,
        move |plugin, inputs, outputs, user_data| {
            let store = &*plugin.store;
            let inputs: Vec<_> = inputs
                .iter()
                .map(|x| ExtismVal::from_val(x, store).unwrap())
                .collect();
            let mut output_tmp: Vec<_> = output_types
                .iter()
                .map(|t| ExtismVal {
                    t: t.clone(),
                    v: ValUnion { i64: 0 },
                })
                .collect();

            // We cannot simply "get" the Vec's storage pointer because
            // the underlying storage might be invalid when the Vec is empty.
            // In that case, we return (null, 0).

            let (inputs_ptr, inputs_len) = if inputs.is_empty() {
                (core::ptr::null(), 0 as Size)
            } else {
                (inputs.as_ptr(), inputs.len() as Size)
            };

            let (output_ptr, output_len) = if output_tmp.is_empty() {
                (null_mut(), 0 as Size)
            } else {
                (output_tmp.as_mut_ptr(), output_tmp.len() as Size)
            };

            func(
                plugin,
                inputs_ptr,
                inputs_len,
                output_ptr,
                output_len,
                user_data.as_ptr(),
            );

            for (tmp, out) in output_tmp.iter().zip(outputs.iter_mut()) {
                match tmp.t {
                    ValType::I32 => *out = Val::I32(tmp.v.i32),
                    ValType::I64 => *out = Val::I64(tmp.v.i64),
                    ValType::F32 => *out = Val::F32(tmp.v.f32.to_bits()),
                    ValType::F64 => *out = Val::F64(tmp.v.f64.to_bits()),
                    _ => todo!(),
                }
            }
            Ok(())
        },
    );
    Box::into_raw(Box::new(ExtismFunction(std::cell::Cell::new(Some(f)))))
}

/// Free `ExtismFunction`
#[no_mangle]
pub unsafe extern "C" fn extism_function_free(f: *mut ExtismFunction) {
    if f.is_null() {
        return;
    }

    drop(Box::from_raw(f))
}

/// Set the namespace of an `ExtismFunction`
#[no_mangle]
pub unsafe extern "C" fn extism_function_set_namespace(
    ptr: *mut ExtismFunction,
    namespace: *const std::ffi::c_char,
) {
    let namespace = std::ffi::CStr::from_ptr(namespace);
    let f = &mut *ptr;
    if let Some(x) = f.0.get_mut() {
        x.set_namespace(namespace.to_string_lossy().to_string());
    } else {
        debug!("Trying to set namespace of already registered function")
    }
}

/// Pre-compile an Extism plugin
#[no_mangle]
pub unsafe extern "C" fn extism_compiled_plugin_new(
    wasm: *const u8,
    wasm_size: Size,
    functions: *mut *const ExtismFunction,
    n_functions: Size,
    with_wasi: bool,
    errmsg: *mut *mut std::ffi::c_char,
) -> *mut CompiledPlugin {
    trace!("Call to extism_plugin_new with wasm pointer {:?}", wasm);
    let data = std::slice::from_raw_parts(wasm, wasm_size as usize);

    let mut builder = PluginBuilder::new(data).with_wasi(with_wasi);

    if !functions.is_null() {
        let funcs = (0..n_functions)
            .map(|i| unsafe { *functions.add(i as usize) })
            .map(|ptr| {
                if ptr.is_null() {
                    return Err("Cannot pass null pointer");
                }

                let ExtismFunction(func) = &*ptr;
                let Some(func) = func.take() else {
                    return Err("Function cannot be registered with multiple different Plugins");
                };

                Ok(func)
            })
            .collect::<Result<Vec<_>, _>>()
            .unwrap_or_else(|e| {
                if !errmsg.is_null() {
                    let e = std::ffi::CString::new(e.to_string()).unwrap();
                    *errmsg = e.into_raw();
                }
                Vec::new()
            });

        if funcs.len() != n_functions as usize {
            return std::ptr::null_mut();
        }

        builder = builder.with_functions(funcs);
    }

    CompiledPlugin::new(builder)
        .map(|v| Box::into_raw(Box::new(v)))
        .unwrap_or_else(|e| {
            if !errmsg.is_null() {
                let e = std::ffi::CString::new(format!(
                    "Unable to compile Extism plugin: {}",
                    e.root_cause(),
                ))
                .unwrap();
                *errmsg = e.into_raw();
            }
            std::ptr::null_mut()
        })
}

/// Pre-compile an Extism plugin and set the number of instructions a plugin is allowed to execute
#[no_mangle]
pub unsafe extern "C" fn extism_compiled_plugin_new_with_fuel_limit(
    wasm: *const u8,
    wasm_size: Size,
    functions: *mut *const ExtismFunction,
    n_functions: Size,
    with_wasi: bool,
    fuel_limit: u64,
    errmsg: *mut *mut std::ffi::c_char,
) -> *mut CompiledPlugin {
    trace!("Call to extism_plugin_new with wasm pointer {:?}", wasm);
    let data = std::slice::from_raw_parts(wasm, wasm_size as usize);

    let mut builder = PluginBuilder::new(data)
        .with_wasi(with_wasi)
        .with_fuel_limit(fuel_limit);

    if !functions.is_null() {
        let funcs = (0..n_functions)
            .map(|i| unsafe { *functions.add(i as usize) })
            .map(|ptr| {
                if ptr.is_null() {
                    return Err("Cannot pass null pointer");
                }

                let ExtismFunction(func) = &*ptr;
                let Some(func) = func.take() else {
                    return Err("Function cannot be registered with multiple different Plugins");
                };

                Ok(func)
            })
            .collect::<Result<Vec<_>, _>>()
            .unwrap_or_else(|e| {
                if !errmsg.is_null() {
                    let e = std::ffi::CString::new(e.to_string()).unwrap();
                    *errmsg = e.into_raw();
                }
                Vec::new()
            });

        if funcs.len() != n_functions as usize {
            return std::ptr::null_mut();
        }

        builder = builder.with_functions(funcs);
    }

    CompiledPlugin::new(builder)
        .map(|v| Box::into_raw(Box::new(v)))
        .unwrap_or_else(|e| {
            if !errmsg.is_null() {
                let e = std::ffi::CString::new(format!(
                    "Unable to compile Extism plugin: {}",
                    e.root_cause(),
                ))
                .unwrap();
                *errmsg = e.into_raw();
            }
            std::ptr::null_mut()
        })
}

/// Free `ExtismCompiledPlugin`
#[no_mangle]
pub unsafe extern "C" fn extism_compiled_plugin_free(plugin: *mut CompiledPlugin) {
    if plugin.is_null() {
        return;
    }

    let plugin = Box::from_raw(plugin);
    trace!("called extism_compiled_plugin_free");
    drop(plugin)
}

/// Create a new plugin with host functions, the functions passed to this function no longer need to be manually freed using
///
/// `wasm`: is a WASM module (wat or wasm) or a JSON encoded manifest
/// `wasm_size`: the length of the `wasm` parameter
/// `functions`: an array of `ExtismFunction*`
/// `n_functions`: the number of functions provided
/// `with_wasi`: enables/disables WASI
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_new(
    wasm: *const u8,
    wasm_size: Size,
    functions: *mut *const ExtismFunction,
    n_functions: Size,
    with_wasi: bool,
    errmsg: *mut *mut std::ffi::c_char,
) -> *mut Plugin {
    let data = std::slice::from_raw_parts(wasm, wasm_size as usize);
    let funcs = if functions.is_null() {
        vec![]
    } else {
        let funcs = (0..n_functions)
            .map(|i| unsafe { *functions.add(i as usize) })
            .map(|ptr| {
                if ptr.is_null() {
                    return Err("Cannot pass null pointer");
                }

                let ExtismFunction(func) = &*ptr;
                let Some(func) = func.take() else {
                    return Err("Function cannot be registered with multiple different Plugins");
                };

                Ok(func)
            })
            .collect::<Result<Vec<_>, _>>()
            .unwrap_or_else(|e| {
                if !errmsg.is_null() {
                    let e = std::ffi::CString::new(e.to_string()).unwrap();
                    *errmsg = e.into_raw();
                }
                Vec::new()
            });

        if funcs.len() != n_functions as usize {
            return std::ptr::null_mut();
        }

        funcs
    };

    Plugin::new(data, funcs, with_wasi)
        .map(|v| Box::into_raw(Box::new(v)))
        .unwrap_or_else(|e| {
            if !errmsg.is_null() {
                let e = std::ffi::CString::new(format!(
                    "Unable to compile Extism plugin: {}",
                    e.root_cause(),
                ))
                .unwrap();
                *errmsg = e.into_raw();
            }
            std::ptr::null_mut()
        })
}

/// Create a new plugin from an `ExtismCompiledPlugin`
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_new_from_compiled(
    compiled: *const CompiledPlugin,
    errmsg: *mut *mut std::ffi::c_char,
) -> *mut Plugin {
    let plugin = Plugin::new_from_compiled(&*compiled);
    match plugin {
        Err(e) => {
            if !errmsg.is_null() {
                let e = std::ffi::CString::new(format!(
                    "Unable to create Extism plugin: {}",
                    e.root_cause(),
                ))
                .unwrap();
                *errmsg = e.into_raw();
            }
            std::ptr::null_mut()
        }
        Ok(p) => Box::into_raw(Box::new(p)),
    }
}

/// Create a new plugin and set the number of instructions a plugin is allowed to execute
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_new_with_fuel_limit(
    wasm: *const u8,
    wasm_size: Size,
    functions: *mut *const ExtismFunction,
    n_functions: Size,
    with_wasi: bool,
    fuel_limit: u64,
    errmsg: *mut *mut std::ffi::c_char,
) -> *mut Plugin {
    trace!(
        "Call to extism_plugin_new_with_fuel_limit with wasm pointer {:?}",
        wasm
    );
    let data = std::slice::from_raw_parts(wasm, wasm_size as usize);
    let funcs = if functions.is_null() {
        vec![]
    } else {
        let funcs = (0..n_functions)
            .map(|i| unsafe { *functions.add(i as usize) })
            .map(|ptr| {
                if ptr.is_null() {
                    return Err("Cannot pass null pointer");
                }

                let ExtismFunction(func) = &*ptr;
                let Some(func) = func.take() else {
                    return Err("Function cannot be registered with multiple different Plugins");
                };

                Ok(func)
            })
            .collect::<Result<Vec<_>, _>>()
            .unwrap_or_else(|e| {
                if !errmsg.is_null() {
                    let e = std::ffi::CString::new(e.to_string()).unwrap();
                    *errmsg = e.into_raw();
                }
                Vec::new()
            });

        if funcs.len() != n_functions as usize {
            return std::ptr::null_mut();
        }

        funcs
    };

    let compiled = match CompiledPlugin::new(
        PluginBuilder::new(data)
            .with_functions(funcs)
            .with_wasi(with_wasi)
            .with_fuel_limit(fuel_limit),
    ) {
        Ok(x) => x,
        Err(e) => {
            if !errmsg.is_null() {
                let e = std::ffi::CString::new(format!(
                    "Unable to compile Extism plugin: {}",
                    e.root_cause(),
                ))
                .unwrap();
                *errmsg = e.into_raw();
            }
            return std::ptr::null_mut();
        }
    };

    let plugin = Plugin::new_from_compiled(&compiled);

    match plugin {
        Err(e) => {
            if !errmsg.is_null() {
                let e = std::ffi::CString::new(format!(
                    "Unable to create Extism plugin: {}",
                    e.root_cause(),
                ))
                .unwrap();
                *errmsg = e.into_raw();
            }
            std::ptr::null_mut()
        }
        Ok(p) => Box::into_raw(Box::new(p)),
    }
}

/// Enable HTTP response headers in plugins using `extism:host/env::http_request`
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_allow_http_response_headers(plugin: *mut Plugin) {
    let plugin = &mut *plugin;
    plugin.store.data_mut().http_headers = Some(BTreeMap::new());
}

/// Free the error returned by `extism_plugin_new`, errors returned from `extism_plugin_error` don't need to be freed
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_new_error_free(err: *mut std::ffi::c_char) {
    if err.is_null() {
        return;
    }
    drop(std::ffi::CString::from_raw(err))
}

/// Free `ExtismPlugin`
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_free(plugin: *mut Plugin) {
    if plugin.is_null() {
        return;
    }

    let plugin = Box::from_raw(plugin);
    trace!(plugin = plugin.id.to_string(), "called extism_plugin_free");
    drop(plugin)
}

/// Get handle for plugin cancellation
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_cancel_handle(plugin: *const Plugin) -> *const CancelHandle {
    if plugin.is_null() {
        return std::ptr::null();
    }
    let plugin = &*plugin;
    trace!(
        plugin = plugin.id.to_string(),
        "called extism_plugin_cancel_handle"
    );
    &plugin.cancel_handle as *const _
}

/// Cancel a running plugin
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_cancel(handle: *const CancelHandle) -> bool {
    let handle = &*handle;
    trace!(
        plugin = handle.id.to_string(),
        "called extism_plugin_cancel"
    );
    handle.cancel().is_ok()
}

/// Update plugin config values.
//
// This will merge with the existing values, if an existing value is set to `null` it will
// be removed
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_config(
    plugin: *mut Plugin,
    json: *const u8,
    json_size: Size,
) -> bool {
    if plugin.is_null() {
        return false;
    }
    let plugin = &mut *plugin;

    trace!(
        plugin = plugin.id.to_string(),
        "call to extism_plugin_config with pointer {:?}",
        json
    );
    let data = std::slice::from_raw_parts(json, json_size as usize);
    let json: std::collections::BTreeMap<String, Option<String>> =
        match serde_json::from_slice(data) {
            Ok(x) => x,
            Err(_) => {
                return false;
            }
        };

    let id = plugin.id;
    let config = &mut plugin.current_plugin_mut().manifest.config;
    for (k, v) in json.into_iter() {
        match v {
            Some(v) => {
                trace!(plugin = id.to_string(), "config, adding {k}");
                config.insert(k, v);
            }
            None => {
                trace!(plugin = id.to_string(), "config, removing {k}");
                config.remove(&k);
            }
        }
    }

    let _ = plugin.clear_error();
    true
}

/// Returns true if `func_name` exists
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_function_exists(
    plugin: *mut Plugin,
    func_name: *const c_char,
) -> bool {
    if plugin.is_null() {
        return false;
    }
    let plugin = &mut *plugin;
    let name = std::ffi::CStr::from_ptr(func_name);
    trace!(
        plugin = plugin.id.to_string(),
        "extism_plugin_function_exists: {:?}",
        name
    );

    let name = match name.to_str() {
        Ok(x) => x,
        Err(_) => {
            return false;
        }
    };

    let _ = plugin.clear_error();
    plugin.function_exists(name)
}

/// Call a function
///
/// `func_name`: is the function to call
/// `data`: is the input data
/// `data_len`: is the length of `data`
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_call(
    plugin: *mut Plugin,
    func_name: *const c_char,
    data: *const u8,
    data_len: Size,
) -> i32 {
    extism_plugin_call_with_host_context(plugin, func_name, data, data_len, std::ptr::null_mut())
}

#[derive(Clone)]
#[repr(transparent)]
struct CVoidContainer(*mut std::ffi::c_void);

// "You break it, you buy it."
unsafe impl Send for CVoidContainer {}
unsafe impl Sync for CVoidContainer {}

/// Call a function with host context.
///
/// `func_name`: is the function to call
/// `data`: is the input data
/// `data_len`: is the length of `data`
/// `host_context`: a pointer to context data that will be available in host functions
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_call_with_host_context(
    plugin: *mut Plugin,
    func_name: *const c_char,
    data: *const u8,
    data_len: Size,
    host_context: *mut std::ffi::c_void,
) -> i32 {
    if plugin.is_null() {
        return -1;
    }
    let plugin = &mut *plugin;
    let lock = plugin.instance.clone();
    let mut lock = lock.lock().unwrap();

    // Get function name
    let name = std::ffi::CStr::from_ptr(func_name);
    let name = match name.to_str() {
        Ok(name) => name,
        Err(e) => {
            plugin.error_msg = Some(make_error_msg(e.to_string()));
            return -1;
        }
    };

    trace!(
        plugin = plugin.id.to_string(),
        "calling function {} using extism_plugin_call",
        name
    );
    let input = std::slice::from_raw_parts(data, data_len as usize);
    let r = if host_context.is_null() {
        None
    } else {
        Some(CVoidContainer(host_context))
    };
    let res = plugin.raw_call(&mut lock, name, input, r);
    match res {
        Err((e, rc)) => {
            plugin.error_msg = Some(make_error_msg(e.to_string()));
            rc
        }
        Ok(x) => x,
    }
}

/// Get the error associated with a `Plugin`
#[no_mangle]
#[deprecated]
pub unsafe extern "C" fn extism_error(plugin: *mut Plugin) -> *const c_char {
    extism_plugin_error(plugin)
}

/// Get the error associated with a `Plugin`
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_error(plugin: *mut Plugin) -> *const c_char {
    if plugin.is_null() {
        return std::ptr::null();
    }
    let plugin = &mut *plugin;
    let _lock = plugin.instance.clone();
    let _lock = _lock.lock().unwrap();

    if plugin.output.error_offset == 0 {
        if let Some(err) = &plugin.error_msg {
            return err.as_ptr() as *const _;
        }
        trace!(plugin = plugin.id.to_string(), "error is NULL");
        return std::ptr::null();
    }

    let offs = plugin.output.error_offset;

    let ptr = plugin.current_plugin_mut().memory_ptr().add(offs as usize) as *const _;

    let len = plugin
        .current_plugin_mut()
        .memory_length(offs)
        .unwrap_or_default();

    let mut data = std::slice::from_raw_parts(ptr, len as usize).to_vec();
    data.push(0);
    plugin.error_msg = Some(data);
    plugin.error_msg.as_ref().unwrap().as_ptr() as *const _
}

/// Get the length of a plugin's output data
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_output_length(plugin: *mut Plugin) -> Size {
    if plugin.is_null() {
        return 0;
    }
    let plugin = &mut *plugin;
    let _lock = plugin.instance.clone();
    let _lock = _lock.lock().unwrap();
    plugin.output.length
}

/// Get a pointer to the output data
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_output_data(plugin: *mut Plugin) -> *const u8 {
    if plugin.is_null() {
        return std::ptr::null();
    }
    let plugin = &mut *plugin;
    let _lock = plugin.instance.clone();
    let _lock = _lock.lock().unwrap();
    trace!(
        plugin = plugin.id.to_string(),
        "extism_plugin_output_data: offset={}, length={}",
        plugin.output.offset,
        plugin.output.length
    );

    let ptr = plugin.current_plugin_mut().memory_ptr();
    ptr.add(plugin.output.offset as usize)
}

/// Set log file and level.
/// The log level can be either one of: info, error, trace, debug, warn or a more
/// complex filter like `extism=trace,cranelift=debug`
/// The file will be created if it doesn't exist.
#[no_mangle]
pub unsafe extern "C" fn extism_log_file(
    filename: *const c_char,
    log_level: *const c_char,
) -> bool {
    let file = if !filename.is_null() {
        let file = std::ffi::CStr::from_ptr(filename);
        match file.to_str() {
            Ok(x) => x,
            Err(_) => {
                return false;
            }
        }
    } else {
        "stderr"
    };

    let level = if !log_level.is_null() {
        let level = std::ffi::CStr::from_ptr(log_level);
        match level.to_str() {
            Ok(x) => x,
            Err(_) => {
                return false;
            }
        }
    } else {
        "error"
    };

    set_log_file(file, level).is_ok()
}

// Set the log file Extism will use, this is a global configuration
fn set_log_file(log_file: impl Into<std::path::PathBuf>, filter: &str) -> Result<(), Error> {
    let log_file = log_file.into();
    let s = log_file.to_str();
    let is_level = tracing::Level::from_str(filter).is_ok();
    let cfg = tracing_subscriber::FmtSubscriber::builder().with_env_filter({
        let x = tracing_subscriber::EnvFilter::builder()
            .with_default_directive(tracing::Level::ERROR.into());
        if is_level {
            x.parse_lossy(format!("extism={filter}"))
        } else {
            x.parse_lossy(filter)
        }
    });

    let res = if s == Some("-") || s == Some("stderr") {
        cfg.with_ansi(true).with_writer(std::io::stderr).try_init()
    } else if s == Some("stdout") {
        cfg.with_ansi(true).with_writer(std::io::stdout).try_init()
    } else {
        let log_file = log_file.to_path_buf();
        let f = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(log_file)
            .expect("Open log file");
        cfg.with_ansi(false)
            .with_writer(move || f.try_clone().unwrap())
            .try_init()
    };

    if let Err(e) = res {
        return Err(Error::msg(e.to_string()));
    }
    Ok(())
}

static LOG_BUFFER: std::sync::Mutex<Option<LogBuffer>> = std::sync::Mutex::new(None);

/// Enable a custom log handler, this will buffer logs until `extism_log_drain` is called
/// Log level should be one of: info, error, trace, debug, warn
#[no_mangle]
pub unsafe extern "C" fn extism_log_custom(log_level: *const c_char) -> bool {
    let level = if !log_level.is_null() {
        let level = std::ffi::CStr::from_ptr(log_level);
        match level.to_str() {
            Ok(x) => x,
            Err(_) => {
                return false;
            }
        }
    } else {
        "error"
    };

    set_log_buffer(level).is_ok()
}

unsafe fn set_log_buffer(filter: &str) -> Result<(), Error> {
    let is_level = tracing::Level::from_str(filter).is_ok();
    let cfg = tracing_subscriber::FmtSubscriber::builder().with_env_filter({
        let x = tracing_subscriber::EnvFilter::builder()
            .with_default_directive(tracing::Level::ERROR.into());
        if is_level {
            x.parse_lossy(format!("extism={filter}"))
        } else {
            x.parse_lossy(filter)
        }
    });
    *LOG_BUFFER.lock().unwrap() = Some(LogBuffer::default());
    let buf = LOG_BUFFER.lock().unwrap().clone().unwrap();
    cfg.with_ansi(false)
        .with_writer(move || buf.clone())
        .try_init()
        .map_err(|x| Error::msg(x.to_string()))?;
    Ok(())
}

#[no_mangle]
/// Calls the provided callback function for each buffered log line.
/// This is only needed when `extism_log_custom` is used.
pub unsafe extern "C" fn extism_log_drain(handler: ExtismLogDrainFunctionType) {
    if let Some(buf) = LOG_BUFFER.lock().unwrap().as_mut() {
        if let Ok(mut buf) = buf.buffer.lock() {
            for (line, len) in buf.drain(..) {
                handler(line.as_ptr(), len as u64);
            }
        }
    }
}

#[derive(Default, Clone)]
struct LogBuffer {
    buffer:
        std::sync::Arc<std::sync::Mutex<std::collections::VecDeque<(std::ffi::CString, usize)>>>,
}

unsafe impl Send for LogBuffer {}
unsafe impl Sync for LogBuffer {}

impl std::io::Write for LogBuffer {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if let Ok(s) = std::str::from_utf8(buf) {
            if let Ok(mut buf) = self.buffer.lock() {
                buf.push_back((std::ffi::CString::new(s)?, s.len()));
            }
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// Reset the Extism runtime, this will invalidate all allocated memory
#[no_mangle]
pub unsafe extern "C" fn extism_plugin_reset(plugin: *mut Plugin) -> bool {
    let plugin = &mut *plugin;

    if let Err(e) = plugin.reset() {
        error!(
            plugin = plugin.id.to_string(),
            "unable to reset plugin: {}",
            e.to_string()
        );
        if let Err(e) = plugin.current_plugin_mut().set_error(e.to_string()) {
            error!(
                plugin = plugin.id.to_string(),
                "unable to set error after failed plugin reset: {}",
                e.to_string()
            );
        }
        false
    } else {
        true
    }
}

/// Get the Extism version string
#[no_mangle]
pub unsafe extern "C" fn extism_version() -> *const c_char {
    VERSION.as_ptr() as *const _
}