use core::ffi::{c_char, c_int, c_void};
use crate::jsvalue::JsValue;
pub const JS_CALL_FLAG_CONSTRUCTOR: c_int = 1 << 0;
pub type ClassId = u32;
pub type Bool = c_int;
pub mod prop {
pub const CONFIGURABLE: u8 = 1 << 0;
pub const WRITABLE: u8 = 1 << 1;
pub const ENUMERABLE: u8 = 1 << 2;
pub const HAS_GET: u8 = 1 << 5;
pub const HAS_SET: u8 = 1 << 6;
pub const C_W_E: u8 = CONFIGURABLE | WRITABLE | ENUMERABLE;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum CFunctionEnum {
Generic = 0,
GenericMagic = 1,
Constructor = 2,
ConstructorMagic = 3,
ConstructorOrFunc = 4,
ConstructorOrFuncMagic = 5,
ConstructorOrFuncFlags = 6,
FF = 7,
FFF = 8,
Getter = 9,
Setter = 10,
GetterMagic = 11,
SetterMagic = 12,
IteratorNext = 13,
}
impl CFunctionEnum {
pub fn from_raw(raw: u8) -> Option<CFunctionEnum> {
Some(match raw {
0 => CFunctionEnum::Generic,
1 => CFunctionEnum::GenericMagic,
2 => CFunctionEnum::Constructor,
3 => CFunctionEnum::ConstructorMagic,
4 => CFunctionEnum::ConstructorOrFunc,
5 => CFunctionEnum::ConstructorOrFuncMagic,
6 => CFunctionEnum::ConstructorOrFuncFlags,
7 => CFunctionEnum::FF,
8 => CFunctionEnum::FFF,
9 => CFunctionEnum::Getter,
10 => CFunctionEnum::Setter,
11 => CFunctionEnum::GetterMagic,
12 => CFunctionEnum::SetterMagic,
13 => CFunctionEnum::IteratorNext,
_ => return None,
})
}
pub fn is_constructor(self) -> bool {
matches!(
self,
CFunctionEnum::Constructor
| CFunctionEnum::ConstructorMagic
| CFunctionEnum::ConstructorOrFunc
| CFunctionEnum::ConstructorOrFuncMagic
| CFunctionEnum::ConstructorOrFuncFlags
)
}
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct JSCFunctionType {
pub pointer: *const c_void,
}
impl JSCFunctionType {
pub fn as_ptr(self) -> *const c_void {
self.pointer
}
pub fn is_null(self) -> bool {
self.pointer.is_null()
}
}
pub const JS_DEF_CFUNC: u8 = 0;
pub const JS_DEF_CGETSET: u8 = 1;
pub const JS_DEF_CGETSET_MAGIC: u8 = 2;
pub const JS_DEF_PROP_STRING: u8 = 3;
pub const JS_DEF_PROP_INT32: u8 = 4;
pub const JS_DEF_PROP_INT64: u8 = 5;
pub const JS_DEF_PROP_DOUBLE: u8 = 6;
pub const JS_DEF_PROP_UNDEFINED: u8 = 7;
pub const JS_DEF_OBJECT: u8 = 8;
pub const JS_DEF_ALIAS: u8 = 9;
pub const JS_DEF_PROP_ATOM: u8 = 10;
pub const JS_DEF_PROP_BOOL: u8 = 11;
#[derive(Clone, Copy)]
#[repr(C)]
pub struct FunctionEntry {
pub length: u8,
pub cproto: u8,
pub _padding: [u8; 6],
pub cfunc: JSCFunctionType,
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct GetSetEntry {
pub get: JSCFunctionType,
pub set: JSCFunctionType,
}
#[derive(Clone, Copy)]
#[repr(C)]
pub union FunctionListUnion {
pub func: core::mem::ManuallyDrop<FunctionEntry>,
pub getset: core::mem::ManuallyDrop<GetSetEntry>,
pub alias_name: *const c_char,
pub alias_base: c_int,
pub prop_list_tab: *const JSCFunctionListEntry,
pub prop_list_len: c_int,
pub str_: *const c_char,
pub i32: i32,
pub i64: i64,
pub f64: f64,
pub raw: u64,
}
#[repr(C)]
pub struct JSCFunctionListEntry {
pub name: *const c_char,
pub prop_flags: u8,
pub def_type: u8,
pub magic: i16,
pub _padding: [u8; 4],
pub u: FunctionListUnion,
}
pub type ClassFinalizer = Option<unsafe extern "C" fn(*mut c_void, JsValue)>;
pub type ClassGCMark = Option<unsafe extern "C" fn(*mut c_void, JsValue, *mut c_void)>;
pub type ClassCall = Option<
unsafe extern "C" fn(*mut c_void, JsValue, JsValue, c_int, *const JsValue, c_int) -> JsValue,
>;
#[repr(C)]
pub struct JSClassExoticMethods {
pub get_own_property: Option<
unsafe extern "C" fn(*mut c_void, *mut JsValue, JsValue, c_int, *const JsValue) -> c_int,
>,
pub define_own_property: Option<
unsafe extern "C" fn(
*mut c_void,
JsValue,
c_int,
*const JsValue,
JsValue,
*mut c_int,
) -> c_int,
>,
pub has_property:
Option<unsafe extern "C" fn(*mut c_void, JsValue, c_int, *const JsValue) -> c_int>,
pub delete_property:
Option<unsafe extern "C" fn(*mut c_void, JsValue, c_int, *const JsValue) -> c_int>,
pub get_prototype: Option<unsafe extern "C" fn(*mut c_void, JsValue) -> JsValue>,
pub set_prototype: Option<unsafe extern "C" fn(*mut c_void, JsValue, JsValue) -> c_int>,
pub is_extensible: Option<unsafe extern "C" fn(*mut c_void, JsValue) -> c_int>,
pub prevent_extensions: Option<unsafe extern "C" fn(*mut c_void, JsValue) -> c_int>,
}
#[repr(C)]
pub struct JSClassDef {
pub class_name: *const c_char,
pub finalizer: ClassFinalizer,
pub gc_mark: ClassGCMark,
pub call: ClassCall,
pub exotic: *const JSClassExoticMethods,
}
pub type ModuleInitFunc =
Option<unsafe extern "C" fn(*mut crate::runtime::JsContext, *mut JsModuleDef) -> c_int>;
pub type CFunctionData = Option<
unsafe extern "C" fn(
*mut crate::runtime::JsContext,
JsValue,
c_int,
*const JsValue,
c_int,
*mut JsValue,
) -> JsValue,
>;
pub type RawFunction = *const c_void;
pub type InterruptHandler =
Option<unsafe extern "C" fn(*mut crate::runtime::JsRuntime, *mut c_void) -> c_int>;
pub type ModuleNormalizeFunc = Option<
unsafe extern "C" fn(
*mut crate::runtime::JsContext,
*const c_char,
*const c_char,
*mut c_void,
) -> *mut c_char,
>;
pub type ModuleLoaderFunc = Option<
unsafe extern "C" fn(*mut crate::runtime::JsContext, *const c_char, *mut c_void) -> JsValue,
>;
pub type ModuleLoaderFunc2 = Option<
unsafe extern "C" fn(
*mut crate::runtime::JsContext,
*const c_char,
*mut crate::runtime::JsContext,
*const c_char,
*mut c_void,
) -> JsValue,
>;
pub type ModuleCheckAttrs = Option<
unsafe extern "C" fn(
*mut crate::runtime::JsContext,
*mut crate::runtime::JsContext,
*mut c_void,
) -> c_int,
>;
pub type JobFunc =
Option<unsafe extern "C" fn(*mut crate::runtime::JsContext, c_int, *const JsValue) -> JsValue>;
pub type PromiseRejectionTracker = Option<
unsafe extern "C" fn(*mut crate::runtime::JsContext, JsValue, JsValue, c_int, *mut c_void),
>;
#[repr(C)]
pub struct JSMallocFunctions {
pub js_malloc: Option<unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void>,
pub js_free: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
pub js_realloc: Option<unsafe extern "C" fn(*mut c_void, *mut c_void, usize) -> *mut c_void>,
pub js_malloc_usable_size: Option<unsafe extern "C" fn(*const c_void) -> usize>,
}
#[repr(C)]
pub struct JSPropertyEnum {
pub is_enumerable: crate::cdefs::Bool,
pub atom: crate::atoms::Atom,
}
#[repr(C)]
pub struct JSPropertyDescriptor {
pub flags: c_int,
pub value: JsValue,
pub getter: JsValue,
pub setter: JsValue,
}
#[repr(C)]
pub struct JSPrintValueOptions {
pub show_hidden: u8,
pub raw_dump: u8,
pub max_depth: u32,
pub max_string_length: u32,
pub max_item_count: u32,
}
pub type PrintValueWrite = Option<unsafe extern "C" fn(*mut c_void, *const c_char, usize)>;
#[repr(C)]
#[derive(Default, Clone, Copy)]
pub struct JSMemoryUsage {
pub malloc_size: i64,
pub malloc_count: i64,
pub memory_used_size: i64,
pub malloc_limit: i64,
pub memory_used_count: i64,
pub atom_count: i64,
pub atom_size: i64,
pub str_count: i64,
pub str_size: i64,
pub obj_count: i64,
pub obj_size: i64,
pub prop_count: i64,
pub prop_size: i64,
pub shape_count: i64,
pub shape_size: i64,
pub js_func_count: i64,
pub js_func_size: i64,
pub js_func_code_size: i64,
pub js_func_pc2line_count: i64,
pub js_func_pc2line_size: i64,
pub c_func_count: i64,
pub array_count: i64,
pub fast_array_count: i64,
pub fast_array_elements: i64,
pub binary_object_count: i64,
pub binary_object_size: i64,
}
#[repr(C)]
pub struct JsModuleDef {
pub name: String,
pub declared: Vec<String>,
pub values: Vec<(String, engine::Value)>,
pub init: ModuleInitFunc,
pub private: Option<engine::Value>,
}
const _: () = {
use core::mem::{align_of, offset_of, size_of};
assert!(size_of::<JSCFunctionListEntry>() == 32);
assert!(offset_of!(JSCFunctionListEntry, name) == 0);
assert!(offset_of!(JSCFunctionListEntry, prop_flags) == 8);
assert!(offset_of!(JSCFunctionListEntry, def_type) == 9);
assert!(offset_of!(JSCFunctionListEntry, magic) == 10);
assert!(offset_of!(JSCFunctionListEntry, u) == 16);
assert!(offset_of!(FunctionEntry, length) == 0);
assert!(offset_of!(FunctionEntry, cproto) == 1);
assert!(offset_of!(FunctionEntry, cfunc) == 8);
assert!(align_of::<JSCFunctionListEntry>() == 8);
assert!(size_of::<JSClassDef>() == 40);
assert!(offset_of!(JSClassDef, class_name) == 0);
assert!(offset_of!(JSClassDef, finalizer) == 8);
assert!(offset_of!(JSClassDef, gc_mark) == 16);
assert!(offset_of!(JSClassDef, call) == 24);
assert!(offset_of!(JSClassDef, exotic) == 32);
assert!(size_of::<JSCFunctionType>() == 8);
assert!(size_of::<ClassId>() == 4);
};