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
#![allow(non_snake_case)]

use crate::plugin::*;
use crate::{def_package, EvalAltResult, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;

#[cfg(any(
    not(feature = "no_float"),
    all(not(feature = "only_i32"), not(feature = "only_i64"))
))]
macro_rules! gen_cmp_functions {
    ($root:ident => $($arg_type:ident),+) => {
        mod $root { $(pub mod $arg_type {
            use super::super::*;

            #[export_module]
            pub mod functions {
                #[rhai_fn(name = "<")] pub fn lt(x: $arg_type, y: $arg_type) -> bool { x < y }
                #[rhai_fn(name = "<=")] pub fn lte(x: $arg_type, y: $arg_type) -> bool { x <= y }
                #[rhai_fn(name = ">")] pub fn gt(x: $arg_type, y: $arg_type) -> bool { x > y }
                #[rhai_fn(name = ">=")] pub fn gte(x: $arg_type, y: $arg_type) -> bool { x >= y }
                #[rhai_fn(name = "==")] pub fn eq(x: $arg_type, y: $arg_type) -> bool { x == y }
                #[rhai_fn(name = "!=")] pub fn ne(x: $arg_type, y: $arg_type) -> bool { x != y }
            }
        })* }
    };
}

#[cfg(any(
    not(feature = "no_float"),
    all(not(feature = "only_i32"), not(feature = "only_i64"))
))]
macro_rules! reg_functions {
    ($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
        combine_with_exported_module!($mod_name, "logic", $root::$arg_type::functions);
    )* }
}

def_package!(crate:LogicPackage:"Logical operators.", lib, {
    #[cfg(not(feature = "only_i32"))]
    #[cfg(not(feature = "only_i64"))]
    {
        reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64);

        #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
        reg_functions!(lib += num_128; i128, u128);
    }

    #[cfg(not(feature = "no_float"))]
    {
        #[cfg(not(feature = "f32_float"))]
        reg_functions!(lib += float; f32);
        combine_with_exported_module!(lib, "f32", f32_functions);

        #[cfg(feature = "f32_float")]
        reg_functions!(lib += float; f64);
        combine_with_exported_module!(lib, "f64", f64_functions);
    }

    set_exported_fn!(lib, "!", not);

    combine_with_exported_module!(lib, "bit_field", bit_field_functions);
});

// Logic operators
#[export_fn]
fn not(x: bool) -> bool {
    !x
}

#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
gen_cmp_functions!(numbers => i8, u8, i16, u16, i32, u32, u64);

#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
gen_cmp_functions!(num_128 => i128, u128);

#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "f32_float"))]
gen_cmp_functions!(float => f32);

#[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")]
gen_cmp_functions!(float => f64);

#[cfg(not(feature = "no_float"))]
#[export_module]
mod f32_functions {
    #[rhai_fn(name = "==")]
    pub fn eq_if(x: INT, y: f32) -> bool {
        (x as f32) == (y as f32)
    }
    #[rhai_fn(name = "==")]
    pub fn eq_fi(x: f32, y: INT) -> bool {
        (x as f32) == (y as f32)
    }
    #[rhai_fn(name = "!=")]
    pub fn neq_if(x: INT, y: f32) -> bool {
        (x as f32) != (y as f32)
    }
    #[rhai_fn(name = "!=")]
    pub fn neq_fi(x: f32, y: INT) -> bool {
        (x as f32) != (y as f32)
    }
    #[rhai_fn(name = ">")]
    pub fn gt_if(x: INT, y: f32) -> bool {
        (x as f32) > (y as f32)
    }
    #[rhai_fn(name = ">")]
    pub fn gt_fi(x: f32, y: INT) -> bool {
        (x as f32) > (y as f32)
    }
    #[rhai_fn(name = ">=")]
    pub fn gte_if(x: INT, y: f32) -> bool {
        (x as f32) >= (y as f32)
    }
    #[rhai_fn(name = ">=")]
    pub fn gte_fi(x: f32, y: INT) -> bool {
        (x as f32) >= (y as f32)
    }
    #[rhai_fn(name = "<")]
    pub fn lt_if(x: INT, y: f32) -> bool {
        (x as f32) < (y as f32)
    }
    #[rhai_fn(name = "<")]
    pub fn lt_fi(x: f32, y: INT) -> bool {
        (x as f32) < (y as f32)
    }
    #[rhai_fn(name = "<=")]
    pub fn lte_if(x: INT, y: f32) -> bool {
        (x as f32) <= (y as f32)
    }
    #[rhai_fn(name = "<=")]
    pub fn lte_fi(x: f32, y: INT) -> bool {
        (x as f32) <= (y as f32)
    }
}

#[cfg(not(feature = "no_float"))]
#[export_module]
mod f64_functions {
    #[rhai_fn(name = "==")]
    pub fn eq_if(x: INT, y: f64) -> bool {
        (x as f64) == (y as f64)
    }
    #[rhai_fn(name = "==")]
    pub fn eq_fi(x: f64, y: INT) -> bool {
        (x as f64) == (y as f64)
    }
    #[rhai_fn(name = "!=")]
    pub fn neq_if(x: INT, y: f64) -> bool {
        (x as f64) != (y as f64)
    }
    #[rhai_fn(name = "!=")]
    pub fn neq_fi(x: f64, y: INT) -> bool {
        (x as f64) != (y as f64)
    }
    #[rhai_fn(name = ">")]
    pub fn gt_if(x: INT, y: f64) -> bool {
        (x as f64) > (y as f64)
    }
    #[rhai_fn(name = ">")]
    pub fn gt_fi(x: f64, y: INT) -> bool {
        (x as f64) > (y as f64)
    }
    #[rhai_fn(name = ">=")]
    pub fn gte_if(x: INT, y: f64) -> bool {
        (x as f64) >= (y as f64)
    }
    #[rhai_fn(name = ">=")]
    pub fn gte_fi(x: f64, y: INT) -> bool {
        (x as f64) >= (y as f64)
    }
    #[rhai_fn(name = "<")]
    pub fn lt_if(x: INT, y: f64) -> bool {
        (x as f64) < (y as f64)
    }
    #[rhai_fn(name = "<")]
    pub fn lt_fi(x: f64, y: INT) -> bool {
        (x as f64) < (y as f64)
    }
    #[rhai_fn(name = "<=")]
    pub fn lte_if(x: INT, y: f64) -> bool {
        (x as f64) <= (y as f64)
    }
    #[rhai_fn(name = "<=")]
    pub fn lte_fi(x: f64, y: INT) -> bool {
        (x as f64) <= (y as f64)
    }
}

#[export_module]
mod bit_field_functions {
    const BITS: usize = std::mem::size_of::<INT>() * 8;

    #[rhai_fn(return_raw)]
    pub fn get_bit(value: INT, index: INT) -> Result<bool, Box<EvalAltResult>> {
        if index >= 0 {
            let offset = index as usize;

            if offset >= BITS {
                EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into()
            } else {
                Ok((value & (1 << offset)) != 0)
            }
        } else if let Some(abs_index) = index.checked_abs() {
            let offset = abs_index as usize;

            // Count from end if negative
            if offset > BITS {
                EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into()
            } else {
                Ok((value & (1 << (BITS - offset))) != 0)
            }
        } else {
            EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into()
        }
    }
    #[rhai_fn(return_raw)]
    pub fn set_bit(value: &mut INT, index: INT, new_value: bool) -> Result<(), Box<EvalAltResult>> {
        if index >= 0 {
            let offset = index as usize;

            if offset >= BITS {
                EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into()
            } else {
                let mask = 1 << offset;
                if new_value {
                    *value |= mask;
                } else {
                    *value &= !mask;
                }
                Ok(())
            }
        } else if let Some(abs_index) = index.checked_abs() {
            let offset = abs_index as usize;

            // Count from end if negative
            if offset > BITS {
                EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into()
            } else {
                let mask = 1 << offset;
                if new_value {
                    *value |= mask;
                } else {
                    *value &= !mask;
                }
                Ok(())
            }
        } else {
            EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into()
        }
    }
    #[rhai_fn(return_raw)]
    pub fn get_bits(value: INT, index: INT, bits: INT) -> Result<INT, Box<EvalAltResult>> {
        if bits < 1 {
            return Ok(0);
        }

        let offset = if index >= 0 {
            let offset = index as usize;

            if offset >= BITS {
                return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into();
            }

            offset
        } else if let Some(abs_index) = index.checked_abs() {
            let offset = abs_index as usize;

            // Count from end if negative
            if offset > BITS {
                return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into();
            }
            BITS - offset
        } else {
            return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into();
        };

        let bits = if offset + bits as usize > BITS {
            BITS - offset
        } else {
            bits as usize
        };

        let mut base = 1;
        let mut mask = 0;

        for _ in 0..bits {
            mask |= base;
            base <<= 1;
        }

        Ok(((value & (mask << index)) >> index) & mask)
    }
    #[rhai_fn(return_raw)]
    pub fn set_bits(
        value: &mut INT,
        index: INT,
        bits: INT,
        new_value: INT,
    ) -> Result<(), Box<EvalAltResult>> {
        if bits < 1 {
            return Ok(());
        }

        let offset = if index >= 0 {
            let offset = index as usize;

            if offset >= BITS {
                return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into();
            }

            offset
        } else if let Some(abs_index) = index.checked_abs() {
            let offset = abs_index as usize;

            // Count from end if negative
            if offset > BITS {
                return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into();
            }
            BITS - offset
        } else {
            return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into();
        };

        let bits = if offset + bits as usize > BITS {
            BITS - offset
        } else {
            bits as usize
        };

        let mut base = 1;
        let mut mask = 0;

        for _ in 0..bits {
            mask |= base;
            base <<= 1;
        }

        *value &= !(mask << index);
        *value |= (new_value & mask) << index;

        Ok(())
    }
}