use quickjs::alloc::{self, ValueBox};
use quickjs::jsvalue::{
self, EXCEPTION, FALSE, JsValue, NULL, TAG_BOOL, TAG_EXCEPTION, TAG_FLOAT64, TAG_INT, TAG_NULL,
TAG_OBJECT, TAG_STRING, TAG_UNDEFINED, TRUE, UNDEFINED,
};
#[test]
fn constants_match_the_reference() {
assert_eq!(UNDEFINED.bits(), 0x0001_8000_0000_0000, "JS_UNDEFINED");
assert_eq!(NULL.bits(), 0x0001_0000_0000_0000, "JS_NULL");
assert_eq!(EXCEPTION.bits(), 0x0003_0000_0000_0000, "JS_EXCEPTION");
assert_eq!(FALSE.bits(), 0x0000_8000_0000_0000, "JS_FALSE");
assert_eq!(TRUE.bits(), FALSE.bits() | 1, "JS_TRUE is JS_FALSE | 1");
}
#[test]
fn integers_and_doubles_match_the_reference() {
assert_eq!(jsvalue::new_int32(42).bits(), 0x0000_0000_0000_002a);
assert_eq!(jsvalue::new_int32(-1).bits(), 0x0000_0000_ffff_ffff);
assert_eq!(jsvalue::new_float64(1.5).bits(), 0xbffc_0000_0000_0000);
assert_eq!(jsvalue::NAN, 0x7ffc_0000_0000_0000, "JS_NAN");
assert_eq!(
jsvalue::FLOAT64_ADDEND,
0x7ffc_0000_0000_0000,
"JS_FLOAT64_ADDEND"
);
}
#[test]
fn tags_match_the_reference() {
assert_eq!(TAG_OBJECT, -1);
assert_eq!(TAG_INT, 0);
assert_eq!(TAG_BOOL, 1);
assert_eq!(TAG_NULL, 2);
assert_eq!(TAG_UNDEFINED, 3);
assert_eq!(TAG_EXCEPTION, 6);
assert_eq!(TAG_FLOAT64, 8);
assert_eq!(TAG_STRING, -5);
assert_eq!(jsvalue::TAG_FIRST, -8);
assert_eq!(core::mem::size_of::<JsValue>(), 8);
assert_eq!(alloc::REF_COUNT_OFFSET, 4);
assert_eq!(core::mem::size_of::<i32>(), 4);
}
#[test]
fn a_value_is_undefined_after_the_default_constructor() {
assert_ne!(JsValue::new(0), UNDEFINED);
assert_eq!(JsValue::default(), UNDEFINED);
}
unsafe fn c_dup(payload: *mut ()) {
let count = unsafe { ValueBox::ref_count(payload) };
unsafe { ValueBox::set_ref_count(payload, count + 1) };
}
unsafe fn c_free(value: JsValue) {
if !value.has_ref_count() {
return;
}
let payload = value.as_ptr().expect("a refcounted value has a payload");
let count = unsafe { ValueBox::ref_count(payload) };
unsafe { ValueBox::set_ref_count(payload, count - 1) };
if count - 1 <= 0 {
unsafe { quickjs::__JS_FreeValue(core::ptr::null_mut(), value) };
}
}
fn engine_string(
ctx: &mut engine::Context<'_>,
text: &str,
) -> (engine::Value, engine::Gc<engine::JsString>) {
let value = ctx.new_string(text);
let engine::Value::String(handle) = value.clone() else {
panic!("new_string must answer a string");
};
(value, handle)
}
#[test]
fn ref_count_sits_four_bytes_before_the_payload() {
let runtime = engine::Runtime::new();
let mut ctx = engine::Context::new(&runtime);
let (value, _handle) = engine_string(&mut ctx, "hello");
let boxed = alloc::box_value(value);
let payload = boxed.as_ptr().expect("a string carries a count");
unsafe {
assert_eq!(ValueBox::ref_count(payload), 1);
let slot = payload.cast::<u8>().sub(4) as *mut i32;
slot.write(5);
assert_eq!(
ValueBox::ref_count(payload),
5,
"the count lives at payload - 4"
);
slot.write(1);
}
assert!(boxed.is_string(), "a boxed string keeps JS_TAG_STRING");
unsafe {
c_dup(payload);
c_dup(payload);
assert_eq!(ValueBox::ref_count(payload), 3);
c_free(boxed);
c_free(boxed);
assert_eq!(ValueBox::ref_count(payload), 1);
c_free(boxed);
}
}
#[test]
fn the_last_free_releases_the_engine_value() {
let runtime = engine::Runtime::new();
let mut ctx = engine::Context::new(&runtime);
let (value, handle) = engine_string(&mut ctx, "hello");
let baseline = handle.strong_count();
let boxed = alloc::box_value(value.clone());
assert_eq!(
handle.strong_count(),
baseline + 1,
"the box holds a reference"
);
unsafe { c_free(boxed) };
assert_eq!(
handle.strong_count(),
baseline,
"the box's reference is gone, and with it the engine value"
);
drop(value);
drop(handle);
drop(ctx);
let _ = &runtime;
}
#[test]
fn a_primitive_needs_no_allocation_at_all() {
let encoded = quickjs::convert::encode(&engine::Value::Int(7));
assert!(!encoded.has_ref_count());
assert_eq!(encoded.bits(), 7, "an int is the bare payload");
unsafe { quickjs::__JS_FreeValue(core::ptr::null_mut(), encoded) };
unsafe { quickjs::__JS_FreeValue(core::ptr::null_mut(), jsvalue::UNDEFINED) };
}
#[test]
fn js_mallocz_zeroes_and_js_free_releases() {
unsafe {
let bytes = quickjs::js_mallocz(core::ptr::null_mut(), 64).cast::<u8>();
assert!(!bytes.is_null());
for index in 0..64 {
assert_eq!(*bytes.add(index), 0, "byte {index} was not zeroed");
}
assert_eq!(
quickjs::js_malloc_usable_size(core::ptr::null_mut(), bytes.cast()),
64
);
quickjs::js_free(core::ptr::null_mut(), bytes.cast());
}
}
#[test]
fn js_malloc_payload_is_aligned_and_writable() {
unsafe {
let bytes = quickjs::js_malloc(core::ptr::null_mut(), 32).cast::<u8>();
assert!(!bytes.is_null());
assert_eq!(
bytes as usize % alloc::ALIGN,
0,
"payload must be 8-byte aligned"
);
bytes.cast::<u64>().write(0x1234_5678_9abc_def0);
assert_eq!(bytes.cast::<u64>().read(), 0x1234_5678_9abc_def0);
quickjs::js_free(core::ptr::null_mut(), bytes.cast());
}
}
#[test]
fn js_realloc_keeps_the_contents() {
unsafe {
let first = quickjs::js_malloc(core::ptr::null_mut(), 8).cast::<u8>();
first.write(b'x');
let grown = quickjs::js_realloc(core::ptr::null_mut(), first.cast(), 4096).cast::<u8>();
assert!(!grown.is_null());
assert_eq!(grown.read(), b'x', "contents survived the move");
assert_eq!(
quickjs::js_malloc_usable_size(core::ptr::null_mut(), grown.cast()),
4096,
"the header records the new size"
);
quickjs::js_free(core::ptr::null_mut(), grown.cast());
}
}
#[test]
fn a_zero_byte_allocation_is_still_freeable() {
unsafe {
let bytes = quickjs::js_mallocz(core::ptr::null_mut(), 0);
assert!(!bytes.is_null());
quickjs::js_free(core::ptr::null_mut(), bytes);
quickjs::js_free(core::ptr::null_mut(), core::ptr::null_mut());
quickjs::js_free_rt(core::ptr::null_mut(), core::ptr::null_mut());
}
}
#[test]
fn an_impossible_size_answers_null() {
unsafe {
assert!(quickjs::js_mallocz(core::ptr::null_mut(), usize::MAX).is_null());
assert!(quickjs::js_malloc(core::ptr::null_mut(), usize::MAX / 2).is_null());
}
}
#[test]
fn the_two_allocator_families_share_one_header() {
unsafe {
let bytes = quickjs::js_malloc(core::ptr::null_mut(), 16).cast::<u8>();
assert!(!bytes.is_null());
assert_eq!(
(bytes.sub(4) as *const i32).read(),
1,
"ref_count at payload - 4"
);
quickjs::js_free(core::ptr::null_mut(), bytes.cast());
}
}