use core::ffi::{c_int, c_void};
use engine::Value;
use crate::atoms::Atom;
use crate::builtins_typed;
use crate::cdefs::Bool;
use crate::conversions::arg;
use crate::jsvalue::{self, JsValue};
use crate::runtime::{self, JsContext};
use crate::value_helpers::jsvalue_of;
pub type FreeArrayBufferDataFunc = Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>;
pub mod typed_array_kind {
pub const UINT8C: i32 = 0;
pub const INT8: i32 = 1;
pub const UINT8: i32 = 2;
pub const INT16: i32 = 3;
pub const UINT16: i32 = 4;
pub const INT32: i32 = 5;
pub const UINT32: i32 = 6;
pub const BIG_INT64: i32 = 7;
pub const BIG_UINT64: i32 = 8;
pub const FLOAT16: i32 = 9;
pub const FLOAT32: i32 = 10;
pub const FLOAT64: i32 = 11;
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewArrayBuffer(
ctx: *mut JsContext,
buf: *mut u8,
len: usize,
free_func: FreeArrayBufferDataFunc,
opaque: *mut c_void,
is_shared: Bool,
) -> JsValue {
let Some(_context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
if is_shared != 0 {
let _ = is_shared;
}
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let bytes: Vec<u8> = if buf.is_null() {
vec![0; len]
} else {
unsafe { core::slice::from_raw_parts(buf, len) }.to_vec()
};
let release: Option<Box<dyn FnOnce()>> = free_func.map(|free_func| {
let buf = buf.cast::<c_void>();
Box::new(move || {
unsafe { free_func(opaque, buf) };
}) as Box<dyn FnOnce()>
});
context.with_current_interp(|interp| {
let buffer = match release {
Some(release) => builtins_typed::new_array_buffer_with_release(interp, bytes, release),
None => builtins_typed::new_array_buffer(interp, bytes),
};
jsvalue_of(Value::Object(buffer))
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_DetachArrayBuffer(ctx: *mut JsContext, obj: JsValue) {
let Some(_context) = (unsafe { ctx.as_ref() }) else {
return;
};
let Some(Value::Object(object)) = (unsafe { arg(obj) }) else {
return;
};
let Some(storage) = object.array_buffer_storage() else {
return;
};
let mut storage = storage.borrow_mut();
storage.bytes = Vec::new();
storage.detached = true;
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewTypedArray(
ctx: *mut JsContext,
argc: c_int,
argv: *const JsValue,
array_type: c_int,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let element = match builtins_typed::element_from_kind(array_type) {
Some(element) => element,
None => {
return runtime::throw_error(context, "TypeError", "unknown typed array kind");
}
};
let args = unsafe { crate::calls::decode_args(argc, argv) };
let result = context
.with_current_interp(|interp| builtins_typed::construct_typed_array(interp, element, args));
runtime::result_value(context, result)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewUint8Array(
ctx: *mut JsContext,
buf: *mut u8,
len: usize,
free_func: FreeArrayBufferDataFunc,
opaque: *mut c_void,
is_shared: Bool,
) -> JsValue {
let buffer = unsafe { JS_NewArrayBuffer(ctx, buf, len, free_func, opaque, is_shared) };
if buffer.is_exception() {
return buffer;
}
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
context.with_current_interp(|interp| match unsafe { arg(buffer) } {
Some(Value::Object(object)) => {
let view = builtins_typed::new_view(interp, &object, builtins_typed::UINT8, 0, len);
jsvalue_of(Value::Object(view))
}
_ => jsvalue::EXCEPTION,
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewUint8ArrayCopy(
ctx: *mut JsContext,
buf: *const u8,
len: usize,
) -> JsValue {
let buffer = unsafe { crate::bjson::JS_NewArrayBufferCopy(ctx, buf, len) };
if buffer.is_exception() {
return buffer;
}
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
context.with_current_interp(|interp| match unsafe { arg(buffer) } {
Some(Value::Object(object)) => {
let view = builtins_typed::new_view(interp, &object, builtins_typed::UINT8, 0, len);
jsvalue_of(Value::Object(view))
}
_ => jsvalue::EXCEPTION,
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewUint8ArrayCopy2(
ctx: *mut JsContext,
buf: *const u8,
len: usize,
is_immutable: Bool,
) -> JsValue {
if is_immutable != 0 {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
return runtime::throw_error(
context,
"TypeError",
"immutable ArrayBuffers are not supported",
);
}
unsafe { JS_NewUint8ArrayCopy(ctx, buf, len) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetTypedArrayBuffer(
ctx: *mut JsContext,
obj: JsValue,
pbyte_offset: *mut usize,
pbyte_length: *mut usize,
pbytes_per_element: *mut usize,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let Some(Value::Object(object)) = (unsafe { arg(obj) }) else {
return runtime::throw_error(context, "TypeError", "not a typed array");
};
let Some((buffer, element, offset, length)) = object.typed_array_parts() else {
return runtime::throw_error(context, "TypeError", "not a typed array");
};
let size = element.size();
if !pbyte_offset.is_null() {
unsafe { *pbyte_offset = offset * size };
}
if !pbyte_length.is_null() {
unsafe { *pbyte_length = length * size };
}
if !pbytes_per_element.is_null() {
unsafe { *pbytes_per_element = size };
}
jsvalue_of(Value::Object(buffer))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetTypedArrayType(obj: JsValue) -> c_int {
let Some(Value::Object(object)) = (unsafe { arg(obj) }) else {
return -1;
};
match object.typed_array_element_type() {
Some(element) => builtins_typed::kind_from_element(element),
None => -1,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_AtomicsPollWaiters(_ctx: *mut JsContext) -> i64 {
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_AtomicsSetAsyncWaker(_cb: *const c_void, _opaque: *mut c_void) {}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SetSharedArrayBufferFunctions(
_rt: *mut runtime::JsRuntime,
_sf: *const c_void,
) {
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_JSONStringify(
ctx: *mut JsContext,
obj: JsValue,
replacer: JsValue,
space0: JsValue,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let value = unsafe { arg(obj) }.unwrap_or(Value::Undefined);
let replacer = unsafe { arg(replacer) }.unwrap_or(Value::Undefined);
let space = unsafe { arg(space0) }.unwrap_or(Value::Undefined);
let result = context.with_current_interp(|interp| {
let global = Value::Object(interp.realm.global.clone());
let json = interp.read_field(&global, "JSON")?;
let stringify = interp.read_field(&json, "stringify")?;
interp.call_value(stringify, json, vec![value, replacer, space])
});
runtime::result_value(context, result)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ParseJSON(
ctx: *mut JsContext,
buf: *const core::ffi::c_char,
buf_len: usize,
filename: *const core::ffi::c_char,
) -> JsValue {
unsafe { JS_ParseJSON2(ctx, buf, buf_len, filename, 0) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ParseJSON2(
ctx: *mut JsContext,
buf: *const core::ffi::c_char,
buf_len: usize,
_filename: *const core::ffi::c_char,
flags: c_int,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
if buf.is_null() {
return runtime::throw_error(context, "SyntaxError", "null JSON");
}
let bytes = unsafe { core::slice::from_raw_parts(buf.cast::<u8>(), buf_len) };
let text = String::from_utf8_lossy(bytes).into_owned();
const JS_PARSE_JSON_EXT: c_int = 1 << 0;
let extended = flags & JS_PARSE_JSON_EXT != 0;
let result = context
.with_current_interp(|interp| engine::builtins::json::parse_text(interp, &text, extended));
runtime::result_value(context, result)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ParseJSON3(
ctx: *mut JsContext,
buf: *const core::ffi::c_char,
buf_len: usize,
filename: *const core::ffi::c_char,
flags: c_int,
_pr: *mut c_void,
) -> JsValue {
unsafe { JS_ParseJSON2(ctx, buf, buf_len, filename, flags) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SameValue(_ctx: *mut JsContext, op1: JsValue, op2: JsValue) -> Bool {
match (unsafe { arg(op1) }, unsafe { arg(op2) }) {
(Some(left), Some(right)) => c_int::from(left.same_value(&right)),
_ => 0,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SameValueZero(
_ctx: *mut JsContext,
op1: JsValue,
op2: JsValue,
) -> Bool {
match (unsafe { arg(op1) }, unsafe { arg(op2) }) {
(Some(left), Some(right)) => c_int::from(left.same_value_zero(&right)),
_ => 0,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_StrictEq(_ctx: *mut JsContext, op1: JsValue, op2: JsValue) -> Bool {
match (unsafe { arg(op1) }, unsafe { arg(op2) }) {
(Some(left), Some(right)) => c_int::from(left.strict_equals(&right)),
_ => 0,
}
}
const _: Option<Atom> = None;