deft_rquickjs_sys/inlines/
common.rs

1pub use ::std::os::raw::{c_char, c_int, c_uint, c_void};
2
3pub type JSValueConst = JSValue;
4
5pub const JS_NULL: JSValue = JS_MKVAL(JS_TAG_NULL, 0);
6pub const JS_UNDEFINED: JSValue = JS_MKVAL(JS_TAG_UNDEFINED, 0);
7pub const JS_FALSE: JSValue = JS_MKVAL(JS_TAG_BOOL, 0);
8pub const JS_TRUE: JSValue = JS_MKVAL(JS_TAG_BOOL, 1);
9pub const JS_EXCEPTION: JSValue = JS_MKVAL(JS_TAG_EXCEPTION, 0);
10pub const JS_UNINITIALIZED: JSValue = JS_MKVAL(JS_TAG_UNINITIALIZED, 0);
11
12#[inline]
13pub fn JS_VALUE_HAS_REF_COUNT(v: JSValue) -> bool {
14    unsafe {
15        JS_VALUE_GET_TAG(v) as c_uint >= JS_TAG_FIRST as c_uint
16    }
17
18}
19
20#[inline]
21pub fn JS_IsNumber(v: JSValue) -> bool {
22    unsafe {
23        let tag = JS_VALUE_GET_TAG(v);
24        tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag)
25    }
26}
27
28#[inline]
29pub fn JS_IsFloat64(v: JSValue) -> bool {
30    unsafe {
31        let tag = JS_VALUE_GET_TAG(v);
32        JS_TAG_IS_FLOAT64(tag)
33    }
34}
35
36
37#[inline]
38pub fn JS_IsInt(v: JSValue) -> bool {
39    unsafe {
40        let tag = JS_VALUE_GET_TAG(v);
41        tag == JS_TAG_INT
42    }
43
44}
45
46#[inline]
47pub fn JS_IsBigInt(v: JSValue) -> bool {
48    unsafe {
49        let tag = JS_VALUE_GET_TAG(v);
50        tag == JS_TAG_BIG_INT
51    }
52}
53
54#[inline]
55pub fn JS_IsBool(v: JSValue) -> bool {
56    unsafe {
57        let tag = JS_VALUE_GET_TAG(v);
58        tag == JS_TAG_BOOL
59    }
60
61}
62
63#[inline]
64pub fn JS_IsNull(v: JSValue) -> bool {
65    unsafe {
66        let tag = JS_VALUE_GET_TAG(v);
67        tag == JS_TAG_NULL
68    }
69
70}
71
72#[inline]
73pub fn JS_IsUndefined(v: JSValue) -> bool {
74    unsafe {
75        let tag = JS_VALUE_GET_TAG(v);
76        tag == JS_TAG_UNDEFINED
77    }
78}
79
80#[inline]
81pub fn JS_IsException(v: JSValue) -> bool {
82    unsafe {
83        let tag = JS_VALUE_GET_TAG(v);
84        tag == JS_TAG_EXCEPTION
85    }
86}
87
88#[inline]
89pub fn JS_IsUninitialized(v: JSValue) -> bool {
90    unsafe {
91        let tag = JS_VALUE_GET_TAG(v);
92        tag == JS_TAG_UNINITIALIZED
93    }
94}
95
96#[inline]
97pub fn JS_IsString(v: JSValue) -> bool {
98    unsafe {
99        let tag = JS_VALUE_GET_TAG(v);
100        tag == JS_TAG_STRING
101    }
102
103}
104
105#[inline]
106pub fn JS_IsSymbol(v: JSValue) -> bool {
107    unsafe {
108        let tag = JS_VALUE_GET_TAG(v);
109        tag == JS_TAG_SYMBOL
110    }
111}
112
113#[inline]
114pub fn JS_IsObject(v: JSValue) -> bool {
115    unsafe {
116        let tag = JS_VALUE_GET_TAG(v);
117        tag == JS_TAG_OBJECT
118    }
119}
120
121#[inline]
122pub unsafe fn JS_ToCString(ctx: *mut JSContext, val: JSValue) -> *const c_char {
123    // Type her can differ depending on architecture.
124    #[allow(clippy::useless_conversion)]
125    JS_ToCStringLen2(ctx, ptr::null_mut(), val, (false).into())
126}
127#[inline]
128pub unsafe fn JS_ToCStringLen(
129    ctx: *mut JSContext,
130    plen: *mut usize,
131    val: JSValue,
132) -> *const c_char {
133    // Type her can differ depending on architecture.
134    #[allow(clippy::useless_conversion)]
135    JS_ToCStringLen2(ctx, plen as _, val, (false).into())
136}
137
138#[inline]
139pub fn JS_NewFloat64(d: f64) -> JSValue {
140    union U {
141        d: f64,
142        u: u64,
143    }
144
145    let u = U { d };
146    let val = d as i32;
147    let t = U { d: val as f64 };
148    /* -0 cannot be represented as integer, so we compare the bit
149    representation */
150    if unsafe { u.u == t.u } {
151        JS_MKVAL(JS_TAG_INT, val)
152    } else {
153        __JS_NewFloat64(d)
154    }
155}