rsciter 0.0.11

Unofficial Rust bindings for Sciter
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
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use crate::{args_as_raw_slice, bindings::*, utf, Error, EventGroups, Result, Value, ValueError};
use std::mem::MaybeUninit;

mod graphics;
mod request;

pub use graphics::*;
pub use request::*;

cfg_if::cfg_if! {
    if #[cfg(feature = "static")] {
        pub fn sapi() -> Result<Api<'static>> {
            let api: &ISciterAPI = unsafe { &*SciterAPI() };

            Ok(Api::from(api))
        }

        extern "C" {
            pub fn SciterAPI() -> *const ISciterAPI;
        }
    }
    else {
        pub fn sapi() -> Result<Api<'static>> {
            use std::sync::atomic::{AtomicPtr, Ordering};

            static API: AtomicPtr<ISciterAPI> = AtomicPtr::new(core::ptr::null_mut());

            let mut ptr = API.load(Ordering::Acquire);
            if ptr.is_null() {
                ptr = load_sciter_api()?;
                API.store(ptr, Ordering::Release);
            }

            Ok(Api::from(unsafe { &*ptr }))
        }

        fn load_sciter_api() -> Result<*mut ISciterAPI> {
            unsafe {
                let lib = if let Ok(bin) = std::env::var("SCITER_BIN_FOLDER") {
                    let full = format!("{}/{SCITER_DLL_NAME}", bin);
                    libloading::Library::new(full)
                } else {
                    libloading::Library::new(SCITER_DLL_NAME)
                };
                let lib = lib.map_err(|err| Error::library(SCITER_DLL_NAME, err))?;
                let func: libloading::Symbol<unsafe extern "system" fn() -> *mut ISciterAPI> = lib
                    .get(b"SciterAPI")
                    .map_err(|err| Error::symbol("SciterAPI", err))?;

                let api = func();
                std::mem::forget(lib); // leave loaded forever

                Ok(api)
            }
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Api<'api> {
    raw: &'api ISciterAPI,
}

impl<'api> From<&'api ISciterAPI> for Api<'api> {
    fn from(value: &'api ISciterAPI) -> Self {
        Self { raw: value }
    }
}

impl<'api> Api<'api> {
    /// API version
    pub fn version(&self) -> u32 {
        self.raw.version
    }

    pub fn class_name(&self) -> Result<Vec<u16>> {
        call_method!(self, SciterClassName as f, {
            Ok(utf::u16_ptr_to_slice(f()).to_vec())
        })
    }

    pub fn sciter_version(&self, kind: VersionKind) -> Result<u32> {
        call_method!(self, SciterVersion(kind.0))
    }

    /// This function is used in response to SCN_LOAD_DATA request.
    ///
    /// Returns `true` if Sciter accepts the data or `false` if error occured
    /// (for example this function was called outside of `SCN_LOAD_DATA` request)
    ///
    /// ### Warning:
    /// If used, call of this function **MUST** be done **ONLY** while handling
    /// `SCN_LOAD_DATA` request and in the same thread.
    /// For asynchronous resource loading use SciterDataReadyAsync
    pub fn data_ready(&self, hwnd: HWND, uri: LPCWSTR, data: &[u8]) -> Result<bool> {
        call_method!(
            self,
            SciterDataReady(hwnd, uri, data.as_ptr(), data.len() as u32) as bool
        )
    }

    /// Use this function outside of SCN_LOAD_DATA request.
    /// This function is needed when you have your own http client implemented in your application.
    ///
    /// Returns `true` if Sciter accepts the data or `false` if error occured
    pub fn data_ready_async(
        &self,
        hwnd: HWND,
        uri: &str,
        data: &[u8],
        request_id: Option<HREQUEST>,
    ) -> Result<bool> {
        let uri = utf::str_to_utf16(uri);
        let request_id = request_id.unwrap_or(std::ptr::null_mut());
        call_method!(
            self,
            SciterDataReadyAsync(
                hwnd,
                uri.as_ptr(),
                data.as_ptr(),
                data.len() as u32,
                request_id
            ) as bool
        )
    }

    pub fn sciter_proc(
        &self,
        hwnd: HWND,
        msg: UINT,
        wparam: WPARAM,
        lparam: LPARAM,
    ) -> Result<LRESULT> {
        call_method!(self, SciterProc(hwnd, msg, wparam, lparam))
    }

    pub fn sciter_proc_nd(
        &self,
        hwnd: HWND,
        msg: UINT,
        wparam: WPARAM,
        lparam: LPARAM,
    ) -> Result<(bool, LRESULT)> {
        let mut handled: SBOOL = 0;

        let res = call_method!(self, SciterProcND(hwnd, msg, wparam, lparam, &mut handled))?;
        Ok((handled != 0, res))
    }

    /// Loads HTML file.
    ///
    /// Returns `true` if the text was parsed and loaded successfully, 'false' otherwise.
    pub fn load_file(&self, hwnd: HWND, path: impl AsRef<str>) -> Result<bool> {
        self.load_file_impl(hwnd, path.as_ref())
    }

    fn load_file_impl(&self, hwnd: HWND, path: &str) -> Result<bool> {
        let path = utf::str_to_utf16(path);
        call_method!(self, SciterLoadFile(hwnd, path.as_ptr()) as bool)
    }

    /// Loads an HTML document from memory.
    /// Returns `true` if the document was parsed and loaded successfully, 'false' otherwise.
    pub fn load_html(&self, hwnd: HWND, html: &[u8], base_url: Option<&str>) -> Result<bool> {
        let vec = base_url.map(utf::str_to_utf16).unwrap_or_default();
        let ptr = if vec.is_empty() {
            std::ptr::null()
        } else {
            vec.as_ptr()
        };

        call_method!(
            self,
            SciterLoadHtml(hwnd, html.as_ptr(), html.len() as u32, ptr) as bool
        )
    }

    pub fn set_callback(
        &self,
        hwnd: HWND,
        callback: LPSciterHostCallback,
        param: LPVOID,
    ) -> Result<()> {
        call_method!(self, SciterSetCallback(hwnd, callback, param))
    }

    pub fn set_master_css(&self, css: &str) -> Result<bool> {
        call_method!(
            self,
            SciterSetMasterCSS(css.as_ptr(), css.len() as u32) as bool
        )
    }

    pub fn append_master_css(&self, css: &str) -> Result<bool> {
        call_method!(
            self,
            SciterAppendMasterCSS(css.as_ptr(), css.len() as u32) as bool
        )
    }

    pub fn set_css(&self, hwnd: HWND, css: &str, base_url: &str, media_type: &str) -> Result<bool> {
        let base_utf16 = utf::str_to_utf16(base_url);
        let media_utf16 = utf::str_to_utf16(media_type);
        call_method!(
            self,
            SciterSetCSS(
                hwnd,
                css.as_ptr(),
                css.len() as u32,
                base_utf16.as_ptr(),
                media_utf16.as_ptr()
            ) as bool
        )
    }

    pub fn set_media_type(&self, hwnd: HWND, media_type: &str) -> Result<bool> {
        let media_utf16 = utf::str_to_utf16(media_type);
        call_method!(self, SciterSetMediaType(hwnd, media_utf16.as_ptr()) as bool)
    }

    pub fn set_media_vars(&self, hwnd: HWND, vars: &Value) -> Result<bool> {
        call_method!(self, SciterSetMediaVars(hwnd, &vars.0) as bool)
    }

    pub fn min_width(&self, hwnd: HWND) -> Result<u32> {
        call_method!(self, SciterGetMinWidth(hwnd))
    }

    pub fn min_height(&self, hwnd: HWND, width: u32) -> Result<u32> {
        call_method!(self, SciterGetMinHeight(hwnd, width))
    }

    pub fn call(&self, hwnd: HWND, name: &str, args: &[Value]) -> Result<Value> {
        let name: Vec<i8> = name
            .as_bytes()
            .iter()
            .cloned()
            .chain(std::iter::once(0))
            .map(|b| b as i8)
            .collect();
        let args = args_as_raw_slice(args);
        let mut script_res = crate::Value::new();
        let args_ptr = if args.is_empty() {
            std::ptr::null()
        } else {
            args.as_ptr()
        };
        let res = call_method!(
            self,
            SciterCall(
                hwnd,
                name.as_ptr(),
                args.len() as u32,
                args_ptr,
                &mut script_res.0
            ) as bool
        )?;

        if res {
            Ok(script_res)
        } else {
            Err(Error::EvalFailed)
        }
    }

    pub fn eval(&self, hwnd: HWND, code: &str) -> Result<Value> {
        let code = utf::str_to_utf16_no_trailing_zero(code);
        let mut script_res = crate::Value::new();
        let res = call_method!(
            self,
            SciterEval(hwnd, code.as_ptr(), code.len() as u32, &mut script_res.0) as bool
        )?;
        if res {
            Ok(script_res)
        } else {
            Err(Error::EvalFailed)
        }
    }

    pub fn update_window(&self, hwnd: HWND) -> Result<()> {
        call_method!(self, SciterUpdateWindow(hwnd))
    }

    pub fn translate_message(&self, msg: &mut MSG) -> Result<bool> {
        call_method!(self, SciterTranslateMessage(msg) as bool)
    }

    pub fn set_option(
        &self,
        hwnd: Option<HWND>,
        option: SCITER_RT_OPTIONS,
        value: UINT_PTR,
    ) -> Result<bool> {
        call_method!(
            self,
            SciterSetOption(hwnd.unwrap_or_default(), option as u32, value) as bool
        )
    }

    pub fn get_ppi(&self, hwnd: HWND) -> Result<(u32, u32)> {
        call_method!(self, SciterGetPPI as f, {
            let mut px = MaybeUninit::<u32>::uninit();
            let mut py = MaybeUninit::<u32>::uninit();

            f(hwnd, px.as_mut_ptr(), py.as_mut_ptr());

            Ok((px.assume_init(), py.assume_init()))
        })
    }

    pub fn get_view_expando(&self, hwnd: HWND) -> Result<bool> {
        let _ = hwnd;
        todo!("SciterGetViewExpando");
    }

    pub fn render_d2d(&self, hwnd: HWND, prt: *mut IUnknown) -> Result<bool> {
        // prt - ID2D1RenderTarget
        let _ = hwnd;
        let _ = prt;
        todo!("SciterRenderD2D");
    }

    pub fn d2d_factory(&self) -> Result<bool> {
        // ID2D1Factory
        todo!("SciterD2DFactory");
    }
    pub fn dw_factory(&self) -> Result<bool> {
        // IDWriteFactory
        todo!("SciterDWFactory");
    }

    pub fn graphics_caps(&self) -> Result<u32> {
        call_method!(self, SciterGraphicsCaps as f, {
            let mut caps = MaybeUninit::<u32>::uninit();
            if f(caps.as_mut_ptr()) != 0 {
                Ok(caps.assume_init())
            } else {
                Err(Error::ApiMethodFailed("SciterGraphicsCaps"))
            }
        })
    }

    pub fn set_home_url(&self, hwnd: HWND, base_url: &str) -> Result<bool> {
        let base_u16 = utf::str_to_utf16(base_url);
        call_method!(self, SciterSetHomeURL(hwnd, base_u16.as_ptr()) as bool)
    }

    pub fn create_nsview(&self, mut frame: RECT) -> Result<HWND> {
        call_method!(self, SciterCreateNSView(&mut frame))
    }

    pub fn create_widget(&self, mut frame: RECT) -> Result<HWND> {
        call_method!(self, SciterCreateWidget(&mut frame))
    }

    pub fn create_window(
        &self,
        flags: UINT,
        mut frame: Option<RECT>,
        parent: Option<HWND>,
        delegate: ::std::option::Option<
            unsafe extern "C" fn(
                hwnd: HWND,
                msg: UINT,
                wparam: WPARAM,
                lparam: LPARAM,
                dparam: LPVOID,
                handled: *mut SBOOL,
            ) -> LRESULT,
        >,
        delegate_param: LPVOID,
    ) -> Result<HWND> {
        let frame_ptr = frame
            .as_mut()
            .map(|r| {
                if (r.right - r.left) > 0 {
                    r
                } else {
                    std::ptr::null_mut()
                }
            })
            .unwrap_or(std::ptr::null_mut());

        call_method!(
            self,
            SciterCreateWindow(
                flags,
                frame_ptr,
                delegate,
                delegate_param,
                parent.unwrap_or_default()
            )
        )
    }

    pub fn setup_debug_output(
        &self,
        hwnd: Option<HWND>,
        param: LPVOID,
        proc: DEBUG_OUTPUT_PROC,
    ) -> Result<()> {
        call_method!(
            self,
            SciterSetupDebugOutput(hwnd.unwrap_or_default(), param, proc)
        )
    }
    // --------------- before this line all methods are in order

    pub fn window_attach_event_handler(
        &self,
        hwnd: HWND,
        pep: LPELEMENT_EVENT_PROC,
        tag: LPVOID,
        subscription: EventGroups,
    ) -> Result<()> {
        let res = call_method!(
            self,
            SciterWindowAttachEventHandler(hwnd, pep, tag, subscription.0 as u32)
        )?;

        match res as u32 {
            SCDOM_OK => Ok(()),
            _ => todo!(),
        }
    }

    // --------------- after this line all methods are in order

    /// Initialize VALUE storage
    /// This call has to be made before passing VALUE* to any other functions
    pub fn value_init(&self, value: &mut VALUE) -> Result<()> {
        call_method!(self, ValueInit(value) as VALUE_RESULT as Result<()>)
    }

    /// Clears the VALUE and deallocates all assosiated structures that are not used anywhere else.
    pub fn value_clear(&self, value: &mut VALUE) -> Result<()> {
        call_method!(self, ValueClear(value) as VALUE_RESULT as Result<()>)
    }

    /// Compares two values, returns true if val1 == val2.
    pub fn value_compare(&self, v1: &VALUE, v2: &VALUE) -> Result<bool> {
        call_method!(self, ValueCompare(v1, v2) as VALUE_RESULT as Result<bool>)
    }

    // Copies src VALUE to dst VALUE. dst VALUE must be in ValueInit state.
    pub fn value_copy(&self, dst: &mut VALUE, src: &VALUE) -> Result<()> {
        call_method!(self, ValueCopy(dst, src) as VALUE_RESULT as Result<()>)
    }

    /// Converts T_OBJECT value types to T_MAP or T_ARRAY.
    /// Use this method if you need to pass values between different threads.
    /// The fanction is applicable for the Sciter
    pub fn value_isolate(&self, value: &mut VALUE) -> Result<()> {
        call_method!(self, ValueIsolate(value) as VALUE_RESULT as Result<()>)
    }

    /// Returns VALUE_TYPE and VALUE_UNIT_TYPE flags of the VALUE
    pub fn value_type(&self, value: &VALUE) -> Result<(VALUE_TYPE, VALUE_UNIT_TYPE)> {
        call_method!(self, ValueType as f, {
            let mut atype = MaybeUninit::<UINT>::uninit();
            let mut units = MaybeUninit::<UINT>::uninit();
            let res = VALUE_RESULT(f(value, atype.as_mut_ptr(), units.as_mut_ptr()) as i32);

            match res {
                VALUE_RESULT::HV_OK | VALUE_RESULT::HV_OK_TRUE => {
                    let atype = VALUE_TYPE(atype.assume_init() as i32);
                    let units = VALUE_UNIT_TYPE(units.assume_init() as i32);
                    Ok((atype, units))
                }

                err => Err(Error::from(ValueError::from(err))),
            }
        })
    }

    /// Returns string data for T_STRING type
    /// For T_FUNCTION returns name of the fuction.
    pub fn value_string_data<'v>(&self, value: &'v VALUE) -> Result<&'v [u16]> {
        call_method!(self, ValueStringData as f, {
            let mut chars = MaybeUninit::<LPCWSTR>::uninit();
            let mut count = MaybeUninit::<UINT>::uninit();
            let res = VALUE_RESULT(f(value, chars.as_mut_ptr(), count.as_mut_ptr()) as i32);
            match res {
                VALUE_RESULT::HV_OK => Ok(std::slice::from_raw_parts(
                    chars.assume_init(),
                    count.assume_init() as usize,
                )),
                err => Err(Error::from(ValueError::from(err))),
            }
        })
    }

    /// Sets VALUE to T_STRING type and copies chars/numChars to
    /// internal refcounted buffer assosiated with the value.
    pub fn value_string_data_set(
        &self,
        value: &mut VALUE,
        data: &[u16],
        units: Option<VALUE_UNIT_TYPE>,
    ) -> Result<()> {
        call_method!(
            self,
            ValueStringDataSet(
                value,
                data.as_ptr(),
                data.len() as u32,
                units.unwrap_or(VALUE_UNIT_TYPE(0)).0 as u32
            ) as VALUE_RESULT as Result<()>
        )
    }

    /// Retrieves integer data of T_INT, T_LENGTH and T_BOOL types
    pub fn value_int_data(&self, value: &VALUE) -> Result<i32> {
        value_ret_val!(self, ValueIntData(value) as i32)
    }

    /// Sets VALUE integer data of T_INT and T_BOOL types
    /// Optionally sets units field too.
    pub fn value_int_data_set(
        &self,
        value: &mut VALUE,
        data: i32,
        atype: VALUE_TYPE,
        units: Option<VALUE_UNIT_TYPE>,
    ) -> Result<()> {
        // let units = units.unwrap_or(VALUE_UNIT_TYPE(0));
        // call_method!(self, ValueIntDataSet(value, data, atype.0 as u32, units.0 as u32) as VALUE_RESULT as Result<()>)
        value_impl_set!(self, value, ValueIntDataSet, data, atype, units)
    }

    /// Retrieve 64bit integer data of T_BIG_INT and T_DATE values.
    pub fn value_int64_data(&self, value: &VALUE) -> Result<i64> {
        value_ret_val!(self, ValueInt64Data(value) as i64)
    }

    /// Sets 64bit integer data of T_BIG_INT and T_DATE values.
    /// Optionally sets units field too.
    pub fn value_int64_data_set(
        &self,
        value: &mut VALUE,
        data: i64,
        atype: VALUE_TYPE,
        units: Option<VALUE_UNIT_TYPE>,
    ) -> Result<()> {
        value_impl_set!(self, value, ValueInt64DataSet, data, atype, units)
    }

    /// Retrieve FLOAT_VALUE (double) data of T_FLOAT and T_LENGTH values.
    pub fn value_float_data(&self, value: &VALUE) -> Result<f64> {
        value_ret_val!(self, ValueFloatData(value) as f64)
    }

    /// Sets FLOAT_VALUE data of T_FLOAT and T_LENGTH values.
    /// Optionally sets units field too.
    pub fn value_float_data_set(
        &self,
        value: &mut VALUE,
        data: f64,
        atype: VALUE_TYPE,
        units: Option<VALUE_UNIT_TYPE>,
    ) -> Result<()> {
        value_impl_set!(self, value, ValueFloatDataSet, data, atype, units)
    }

    /// Retrieve integer data of T_BYTES type
    pub fn value_binary_data<'v>(&self, value: &'v VALUE) -> Result<&'v [u8]> {
        call_method!(self, ValueBinaryData as f, {
            let mut bytes = MaybeUninit::<LPCBYTE>::uninit();
            let mut count = MaybeUninit::<UINT>::uninit();
            let res = VALUE_RESULT(f(value, bytes.as_mut_ptr(), count.as_mut_ptr()) as i32);
            match res {
                VALUE_RESULT::HV_OK => Ok(std::slice::from_raw_parts(
                    bytes.assume_init(),
                    count.assume_init() as usize,
                )),
                err => Err(Error::from(ValueError::from(err))),
            }
        })
    }

    /// Sets VALUE to sequence of bytes of type T_BYTES
    pub fn value_binary_data_set(
        &self,
        value: &mut VALUE,
        data: &[u8],
        atype: VALUE_TYPE,
        units: Option<VALUE_UNIT_TYPE>,
    ) -> Result<()> {
        call_method!(
            self,
            ValueBinaryDataSet(
                value,
                data.as_ptr(),
                data.len() as u32,
                atype.0 as u32,
                units.unwrap_or(VALUE_UNIT_TYPE(0)).0 as u32
            ) as VALUE_RESULT as Result<()>
        )
    }

    /// Retrieve number of sub-elements for:
    /// - T_ARRAY - number of elements in the array;
    /// - T_MAP - number of key/value pairs in the map;
    /// - T_FUNCTION - number of arguments in the function;
    pub fn value_elements_count(&self, value: &VALUE) -> Result<usize> {
        value_ret_val!(self, ValueElementsCount(value) as INT).map(|r| r as usize)
    }

    /// Retrieve value of sub-element at index n for:
    /// - T_ARRAY - nth element of the array;
    /// - T_MAP - value of nth key/value pair in the map;
    /// - T_FUNCTION - value of nth argument of the function;
    pub fn value_nth_element_value(&self, value: &VALUE, n: INT) -> Result<VALUE> {
        value_ret_val!(self, ValueNthElementValue(value, n))
    }

    /// sets value of sub-element at index n for:
    /// - T_ARRAY - nth element of the array;
    /// - T_MAP - value of nth key/value pair in the map;
    /// - T_FUNCTION - value of nth argument of the function;
    ///
    /// If the VALUE is not of one of types above then it makes it of type T_ARRAY with
    /// single element - 'val_to_set'.
    pub fn value_nth_element_value_set(&self, dst: &mut VALUE, n: INT, src: &VALUE) -> Result<()> {
        call_method!(
            self,
            ValueNthElementValueSet(dst, n, src) as VALUE_RESULT as Result<()>
        )
    }

    pub fn value_nth_element_key(&self, value: &VALUE, n: INT) -> Result<VALUE> {
        value_ret_val!(self, ValueNthElementKey(value, n))
    }

    pub fn value_enum_elements(
        &self,
        value: &VALUE,
        penum: unsafe extern "C" fn(arg1: LPVOID, arg2: *const VALUE, arg3: *const VALUE) -> SBOOL,
        param: LPVOID,
    ) -> Result<()> {
        call_method!(
            self,
            ValueEnumElements(value, Some(penum), param) as VALUE_RESULT as Result<()>
        )
    }

    pub fn value_set_value_to_key(
        &self,
        dst: &mut VALUE,
        key: &VALUE,
        value: &VALUE,
    ) -> Result<()> {
        call_method!(
            self,
            ValueSetValueToKey(dst, key, value) as VALUE_RESULT as Result<()>
        )
    }

    pub fn value_get_value_of_key(self, value: &VALUE, key: &VALUE) -> Result<VALUE> {
        value_ret_val!(self, ValueGetValueOfKey(value, key))
    }

    pub fn value_to_string(&self, value: &mut VALUE, how: VALUE_STRING_CVT_TYPE) -> Result<()> {
        call_method!(
            self,
            ValueToString(value, how.0 as u32) as VALUE_RESULT as Result<()>
        )
    }

    /// Returns Number of non-parsed characters in case of errors.
    /// Thus if string was parsed in full it returns 0 (success)
    pub fn value_from_string(
        &self,
        value: &mut VALUE,
        str: &[u16],
        how: VALUE_STRING_CVT_TYPE,
    ) -> Result<()> {
        let res = call_method!(
            self,
            ValueFromString(value, str.as_ptr(), str.len() as u32, how.0 as u32)
        )?;
        if res == 0 {
            Ok(())
        } else {
            Err(Error::from(ValueError::FromStringNonParsed(res)))
        }
    }

    pub fn value_invoke(
        &self,
        value: &VALUE,
        this: &mut VALUE,
        args: &[VALUE],
        ret_val: &mut VALUE,
        url: LPCWSTR,
    ) -> Result<()> {
        let args_ptr = if args.is_empty() {
            std::ptr::null()
        } else {
            args.as_ptr()
        };
        call_method!(
            self,
            ValueInvoke(value, this, args.len() as u32, args_ptr, ret_val, url as _) as VALUE_RESULT
                as Result<()>
        )
    }

    pub fn value_native_functor_set(
        &self,
        value: &mut VALUE,
        invoke: Option<
            unsafe extern "C" fn(
                tag: *mut ::std::os::raw::c_void,
                argc: UINT,
                argv: *const VALUE,
                retval: *mut VALUE,
            ),
        >,
        release: Option<unsafe extern "C" fn(tag: *mut ::std::os::raw::c_void)>,
        tag: LPVOID,
    ) -> Result<()> {
        call_method!(
            self,
            ValueNativeFunctorSet(value, invoke, release, tag) as VALUE_RESULT as Result<()>
        )
    }

    pub fn value_is_native_functor(&self, value: &VALUE) -> Result<bool> {
        call_method!(self, ValueIsNativeFunctor(value) as VALUE_RESULT).map(|res| res.0 != 0)
        // ValueIsNativeFunctor returns 1 for the native functor
    }

    ///////////////////
    pub fn open_archive(&self, data: &[u8]) -> Result<HSARCHIVE> {
        call_method!(self, SciterOpenArchive(data.as_ptr(), data.len() as u32))
    }

    pub fn get_archive_item<'data>(&self, har: HSARCHIVE, path: LPCWSTR) -> Result<&'data [u8]> {
        let mut data: LPCBYTE = std::ptr::null_mut();
        let mut len: UINT = 0;
        let res = call_method!(self, SciterGetArchiveItem(har, path, &mut data, &mut len))?;
        unsafe {
            match res {
                0 => Err(Error::ArchiveItemNotFound(utf::u16_ptr_to_string(path))),
                _ => Ok(std::slice::from_raw_parts(data, len as usize)),
            }
        }
    }

    pub fn close_archive(&self, har: HSARCHIVE) -> Result<bool> {
        call_method!(self, SciterCloseArchive(har) as bool)
    }

    // --------------- before this line methods are in order until the similar comment

    pub fn post_callback(
        &self,
        hwnd: HWND,
        wparam: UINT_PTR,
        lparam: UINT_PTR,
        timeoutms: UINT,
    ) -> Result<UINT_PTR> {
        call_method!(self, SciterPostCallback(hwnd, wparam, lparam, timeoutms))
    }

    pub fn graphics_api(&self) -> Result<GraphicsApi<'api>> {
        call_method!(self, GetSciterGraphicsAPI as f, {
            Ok(GraphicsApi::from(&*f()))
        })
    }

    pub fn request_api(&self) -> Result<RequestApi<'api>> {
        call_method!(self, GetSciterRequestAPI as f, {
            Ok(RequestApi::from(&*f()))
        })
    }

    pub fn atom_value(&self, name: &std::ffi::CStr) -> Result<UINT64> {
        call_method!(self, SciterAtomValue(name.as_ptr()))
    }

    pub fn atom_name_cb(
        &self,
        atomv: UINT64,
        rcv: Option<unsafe extern "C" fn(arg1: LPCSTR, arg2: UINT, arg3: LPVOID)>,
        rcv_param: LPVOID,
    ) -> Result<bool> {
        call_method!(self, SciterAtomNameCB(atomv, rcv, rcv_param) as bool)
    }

    pub fn set_global_asset(&self, pass: *mut som_asset_t) -> Result<bool> {
        call_method!(self, SciterSetGlobalAsset(pass) as bool)
    }

    pub fn release_global_asset(&self, pass: *mut som_asset_t) -> Result<bool> {
        call_method!(self, SciterReleaseGlobalAsset(pass) as bool)
    }

    pub fn exec(&self, app_cmd: SCITER_APP_CMD, p1: UINT_PTR, p2: UINT_PTR) -> Result<INT_PTR> {
        let cmd = app_cmd as i32 as UINT;
        call_method!(self, SciterExec(cmd, p1, p2))
    }

    pub fn window_exec(
        &self,
        hwnd: HWND,
        window_cmd: SCITER_WINDOW_CMD,
        p1: UINT_PTR,
        p2: UINT_PTR,
    ) -> Result<INT_PTR> {
        let cmd = window_cmd as i32 as UINT;
        call_method!(self, SciterWindowExec(hwnd, cmd, p1, p2))
    }
}

// user friendly additions
impl<'api> Api<'api> {
    pub fn class_name_as_string(&self) -> Result<String> {
        call_method!(self, SciterClassName as f, {
            let name = utf::u16_ptr_to_slice(f());
            Ok(String::from_utf16_lossy(name))
        })
    }

    /// This function is used in response to SCN_LOAD_DATA request.
    ///
    /// Returns `true` if Sciter accepts the data or `false` if error occured
    /// (for example this function was called outside of `SCN_LOAD_DATA` request)
    ///
    /// ### Warning:
    /// If used, call of this function **MUST** be done **ONLY** while handling
    /// `SCN_LOAD_DATA` request and in the same thread.
    /// For asynchronous resource loading use SciterDataReadyAsync
    pub fn data_ready_str(&self, hwnd: HWND, uri: impl AsRef<str>, data: &[u8]) -> Result<bool> {
        let uri = utf::str_to_utf16(uri.as_ref());
        self.data_ready(hwnd, uri.as_ptr(), data)
    }

    pub fn get_archive_item_str<'data>(
        &self,
        har: HSARCHIVE,
        path: impl AsRef<str>,
    ) -> Result<&'data [u8]> {
        let path = utf::str_to_utf16(path.as_ref());
        self.get_archive_item(har, path.as_ptr())
    }
}

pub struct VersionKind(u32);
impl VersionKind {
    pub const MAJOR: VersionKind = VersionKind(0);
    pub const MINOR: VersionKind = VersionKind(1); // ?? Not sure this names are correct
    pub const UPDATE: VersionKind = VersionKind(2); // ??
    pub const BUILD: VersionKind = VersionKind(3); // ??
    pub const REVISION: VersionKind = VersionKind(4);
}

macro_rules! call_method {
    ($self:ident, $name:ident as $f:ident, $body: block) => {
        $self.raw.$name.ok_or(crate::Error::ApiMethod(stringify!($name))).and_then(|$f| unsafe { $body })
    };

    ($self:ident, $name:ident($( $arg:expr ),*)) => {
        call_method!($self, $name as method, {
            Ok(method($($arg),*))
        })
    };

    ($self:ident, $name:ident($( $arg:expr ),*) as bool) => {
        call_method!($self, $name($($arg),*)).map(|res| res != 0)
    };

    ($self:ident, $name:ident($( $arg:expr ),*) as VALUE_RESULT) => {
        call_method!($self, $name($($arg),*)).map(|res| crate::bindings::VALUE_RESULT(res as i32))
    };

    ($self:ident, $name:ident($( $arg:expr ),*) as VALUE_RESULT as Result<()>) => {
        match call_method!($self, $name($($arg),*)).map(|res| crate::bindings::VALUE_RESULT(res as i32))? {
            crate::bindings::VALUE_RESULT::HV_OK | crate::bindings::VALUE_RESULT::HV_OK_TRUE => Ok(()),
            err => Err(crate::Error::from(crate::ValueError::from(err)))
        }
    };

    ($self:ident, $name:ident($( $arg:expr ),*) as VALUE_RESULT as Result<bool>) => {
        match call_method!($self, $name($($arg),*)).map(|res| crate::bindings::VALUE_RESULT(res as i32))? {
            crate::bindings::VALUE_RESULT::HV_OK => Ok(false),
            crate::bindings::VALUE_RESULT::HV_OK_TRUE => Ok(true),
            err => Err(crate::Error::from(crate::ValueError::from(err)))
        }
    };

}

macro_rules! value_impl_set {
    ($self:ident, $value:ident, $name:ident, $data:ident, $atype: expr, $units: expr) => {
        call_method!(
            $self,
            $name(
                $value,
                $data,
                $atype.0 as u32,
                $units.unwrap_or(VALUE_UNIT_TYPE(0)).0 as u32
            ) as VALUE_RESULT as Result<()>
        )
    };
}

macro_rules! value_ret_val {
    ($self:ident, $name:ident ( $( $arg1:expr ),* ; $( $arg2:expr ),* ) as $type:ty) => {
        call_method!($self, $name as f, {
            let mut ret_val = MaybeUninit::<$type>::zeroed();
            let res = VALUE_RESULT(f($($arg1),*, ret_val.as_mut_ptr(), $($arg2),*) as i32);
            match res {
                VALUE_RESULT::HV_OK | VALUE_RESULT::HV_OK_TRUE => Ok(ret_val.assume_init()),
                err => Err(Error::from(ValueError::from(err))),
            }
        })
    };

    ($self:ident, $name:ident ( $( $arg1:expr ),* ; $( $arg2:expr ),* ) ) => {
        value_ret_val!($self, $name ($($arg1),* ; $($arg2),*) as VALUE)
    };

    ($self:ident, $name:ident ( $( $arg:expr ),* ) ) => {
        value_ret_val!($self, $name ($($arg),* ; ) as VALUE)
    };

    ($self:ident, $name:ident ( $( $arg:expr ),* ) as $type:ty ) => {
        value_ret_val!($self, $name ($($arg),* ; ) as $type)
    };
}

// to make visible in submodules and above definitions
use call_method;
use value_impl_set;
use value_ret_val;