sciter/capi/
scom.rs

1/*! Sciter Object Model (SOM passport), native C interface.
2
3See https://sciter.com/native-code-exposure-to-script/
4and https://sciter.com/developers/for-native-gui-programmers/sciter-object-model/.
5
6*/
7
8#![allow(non_snake_case, non_camel_case_types)]
9#![allow(dead_code)]
10
11use capi::sctypes::*;
12use capi::scvalue::VALUE;
13
14/// An atom value that uniquely identifies the name being registered.
15pub type som_atom_t = u64;
16
17
18/// `som_asset_t` is a structure that a custom native object must be derived from.
19#[repr(C)]
20#[derive(Debug)]
21pub struct som_asset_t {
22	pub(crate) isa: &'static som_asset_class_t,
23}
24
25impl som_asset_t {
26	pub(crate) fn get_passport(&self) -> *const som_passport_t {
27		(self.isa.get_passport)(self as *const _ as *mut _)
28	}
29}
30
31/// Is a pack of 4 pointers to functions that define the life time of an asset.
32#[repr(C)]
33#[derive(Debug)]
34pub(crate) struct som_asset_class_t {
35	/// Increments the reference count for an interface on an object.
36	pub add_ref: extern "C" fn(thing: *mut som_asset_t) -> i32,
37
38	/// Decrements the reference count for an interface on an object.
39	pub release: extern "C" fn(thing: *mut som_asset_t) -> i32,
40
41	/// Retrieves a pointer to a supported interface of an object.
42	pub get_interface: extern "C" fn(thing: *mut som_asset_t, name: LPCSTR, out: *mut *mut som_asset_t) -> bool,
43
44	/// Retrieves a pointer to the passport declaration of an object.
45	pub get_passport: extern "C" fn(thing: *mut som_asset_t) -> *const som_passport_t,
46}
47
48
49/// Defines properties and methods of an asset.
50#[repr(C)]
51pub struct som_passport_t {
52	/// Flags of an asset, see [`som_passport_flags`](enum.som_passport_flags.html).
53	pub flags: u64,
54
55	/// The name of the class (asset type).
56	pub name: som_atom_t,
57
58	/// Properties: `asset.prop`.
59	///
60	/// Must be a pointer to an array of structures:
61	///
62	/// ```rust,no_run
63	/// # use sciter::om::*;
64	/// let mut pst = Box::new(som_passport_t::default());
65	///
66	/// type ObjectProps = [som_property_def_t; 2];
67	/// let mut props = Box::new(ObjectProps::default());
68	///
69	/// let mut prop1 = &mut props[0];
70	/// prop1.name = atom("age");
71	///
72	/// let mut prop2 = &mut props[1];
73	/// prop2.name = atom("name");
74	///
75	/// pst.n_properties = 2;
76	/// pst.properties = Box::into_raw(props) as *const _;
77	/// ```
78
79	pub properties: *const som_property_def_t,
80
81	/// Properties count.
82	pub n_properties: usize,
83
84	/// Methods: `asset.func()`
85	///
86	/// Must be a pointer to an array of structures,
87	/// see [`properties`](struct.som_passport_t.html#structfield.properties) for an example.
88	pub methods: *const som_method_def_t,
89
90	/// Methods count.
91	pub n_methods: usize,
92
93	/// Index access: `var item = asset[key]`.
94	pub item_getter: Option<som_item_getter_t>,
95
96	/// Index access: `asset[key] = item`.
97	pub item_setter: Option<som_item_setter_t>,
98
99	/// Enumeration: `for(var item in asset)`.
100	pub item_next: Option<som_item_next_t>,
101
102	/// Property access interceptor: `var val = asset.prop`.
103	pub prop_getter: Option<som_any_prop_getter_t>,
104
105	/// Property set interceptor: `asset.prop = val`.
106	pub prop_setter: Option<som_any_prop_setter_t>,
107}
108
109/// Empty passport.
110impl Default for som_passport_t {
111	fn default() -> Self {
112		use std::ptr;
113		Self {
114			flags: 0,
115			name: 0,
116
117			prop_getter: None,
118			prop_setter: None,
119
120			item_getter: None,
121			item_setter: None,
122			item_next: None,
123
124			properties: ptr::null(),
125			n_properties: 0,
126
127			methods: ptr::null(),
128			n_methods: 0,
129		}
130	}
131}
132
133
134/// [`som_passport_t`](struct.som_passport_t.html#structfield.flags) flags.
135#[repr(u64)]
136#[derive(Debug, PartialOrd, PartialEq)]
137pub enum som_passport_flags {
138	/// Not extendable.
139	SEALED = 0,
140
141	/// Extendable.
142	///
143	/// An asset may have new properties added by script.
144	EXTENDABLE = 1,
145}
146
147
148/// Property of an asset.
149#[repr(C)]
150pub struct som_property_def_t {
151	pub reserved: LPVOID,
152
153	/// Property name.
154	pub name: som_atom_t,
155
156	/// Property getter: `var val = asset.prop`.
157	pub getter: Option<som_prop_getter_t>,
158
159	/// Property setter: `asset.prop = val`.
160	pub setter: Option<som_prop_setter_t>,
161}
162
163/// Empty property.
164impl Default for som_property_def_t {
165	fn default() -> Self {
166		Self {
167			reserved: std::ptr::null_mut(),
168			name: 0,
169			getter: None,
170			setter: None,
171		}
172	}
173}
174
175/// Method of an asset.
176#[repr(C)]
177pub struct som_method_def_t {
178	pub reserved: LPVOID,
179
180	/// Method name.
181	pub name: som_atom_t,
182
183	/// Parameters count.
184	///
185	/// The actual arguments count can be lesser then specified here:
186	///
187	/// ```tiscript,ignore
188	/// function asset.func(a,b,c);  // native asset method accepts 3 parameters
189	///
190	/// asset.func("one"); // call with only one parameter.
191	/// ```
192	pub params: usize,
193
194	/// Method body.
195	pub func: Option<som_method_t>,
196}
197
198/// Empty method.
199impl Default for som_method_def_t {
200	fn default() -> Self {
201		Self {
202			reserved: std::ptr::null_mut(),
203			name: 0,
204			params: 0,
205			func: None,
206		}
207	}
208}
209
210type som_dispose_t = extern "C" fn(thing: *mut som_asset_t);
211
212type som_prop_getter_t = extern "C" fn(thing: *mut som_asset_t, p_value: &mut VALUE) -> BOOL;
213type som_prop_setter_t = extern "C" fn(thing: *mut som_asset_t, p_value: &VALUE) -> BOOL;
214
215type som_any_prop_getter_t = extern "C" fn(thing: *mut som_asset_t, propSymbol: som_atom_t, p_value: &mut VALUE) -> BOOL;
216type som_any_prop_setter_t = extern "C" fn(thing: *mut som_asset_t, propSymbol: som_atom_t, p_value: &VALUE) -> BOOL;
217
218type som_item_getter_t = extern "C" fn(thing: *mut som_asset_t, p_key: &VALUE, p_value: &mut VALUE) -> BOOL;
219type som_item_setter_t = extern "C" fn(thing: *mut som_asset_t, p_key: &VALUE, p_value: &VALUE) -> BOOL;
220
221type som_item_next_t = extern "C" fn(thing: *mut som_asset_t, p_idx: &mut VALUE, p_value: &mut VALUE) -> BOOL;
222
223type som_method_t = extern "C" fn(thing: *mut som_asset_t, argc: u32, argv: *const VALUE, p_result: &mut VALUE) -> BOOL;