rquickjs_sys/inlines/
common.rs

1pub use ::core::ffi::{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 unsafe fn JS_VALUE_HAS_REF_COUNT(v: JSValue) -> bool {
14    JS_VALUE_GET_TAG(v) as c_uint >= JS_TAG_FIRST as c_uint
15}
16
17#[inline]
18pub unsafe fn JS_IsNumber(v: JSValue) -> bool {
19    let tag = JS_VALUE_GET_TAG(v);
20    tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag)
21}
22
23#[inline]
24pub unsafe fn JS_IsInt(v: JSValue) -> bool {
25    let tag = JS_VALUE_GET_TAG(v);
26    tag == JS_TAG_INT
27}
28
29#[inline]
30pub unsafe fn JS_IsBigInt(v: JSValue) -> bool {
31    let tag = JS_VALUE_GET_TAG(v);
32    tag == JS_TAG_BIG_INT || tag == JS_TAG_SHORT_BIG_INT
33}
34
35#[inline]
36pub unsafe fn JS_IsBool(v: JSValue) -> bool {
37    let tag = JS_VALUE_GET_TAG(v);
38    tag == JS_TAG_BOOL
39}
40
41#[inline]
42pub unsafe fn JS_IsNull(v: JSValue) -> bool {
43    let tag = JS_VALUE_GET_TAG(v);
44    tag == JS_TAG_NULL
45}
46
47#[inline]
48pub unsafe fn JS_IsUndefined(v: JSValue) -> bool {
49    let tag = JS_VALUE_GET_TAG(v);
50    tag == JS_TAG_UNDEFINED
51}
52
53#[inline]
54pub unsafe fn JS_IsException(v: JSValue) -> bool {
55    let tag = JS_VALUE_GET_TAG(v);
56    tag == JS_TAG_EXCEPTION
57}
58
59#[inline]
60pub unsafe fn JS_IsUninitialized(v: JSValue) -> bool {
61    let tag = JS_VALUE_GET_TAG(v);
62    tag == JS_TAG_UNINITIALIZED
63}
64
65#[inline]
66pub unsafe fn JS_IsString(v: JSValue) -> bool {
67    let tag = JS_VALUE_GET_TAG(v);
68    tag == JS_TAG_STRING
69}
70
71#[inline]
72pub unsafe fn JS_IsSymbol(v: JSValue) -> bool {
73    let tag = JS_VALUE_GET_TAG(v);
74    tag == JS_TAG_SYMBOL
75}
76
77#[inline]
78pub unsafe fn JS_IsObject(v: JSValue) -> bool {
79    let tag = JS_VALUE_GET_TAG(v);
80    tag == JS_TAG_OBJECT
81}
82
83#[inline]
84pub unsafe fn JS_ToCString(ctx: *mut JSContext, val: JSValue) -> *const c_char {
85    // Type her can differ depending on architecture.
86    #[allow(clippy::useless_conversion)]
87    JS_ToCStringLen2(ctx, ptr::null_mut(), val, (false).into())
88}
89#[inline]
90pub unsafe fn JS_ToCStringLen(
91    ctx: *mut JSContext,
92    plen: *mut usize,
93    val: JSValue,
94) -> *const c_char {
95    // Type her can differ depending on architecture.
96    #[allow(clippy::useless_conversion)]
97    JS_ToCStringLen2(ctx, plen as _, val, (false).into())
98}
99
100#[inline]
101pub fn JS_NewFloat64(d: f64) -> JSValue {
102    union U {
103        d: f64,
104        u: u64,
105    }
106
107    let u = U { d };
108    let val = d as i32;
109    let t = U { d: val as f64 };
110    /* -0 cannot be represented as integer, so we compare the bit
111    representation */
112    if unsafe { u.u == t.u } {
113        JS_MKVAL(JS_TAG_INT, val)
114    } else {
115        __JS_NewFloat64(d)
116    }
117}