1use core::ffi::{c_int, c_void};
31
32use engine::Value;
33
34use crate::atoms::Atom;
35use crate::builtins_typed;
36use crate::cdefs::Bool;
37use crate::conversions::arg;
38use crate::jsvalue::{self, JsValue};
39use crate::runtime::{self, JsContext};
40use crate::value_helpers::jsvalue_of;
41
42pub type FreeArrayBufferDataFunc = Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>;
44
45pub mod typed_array_kind {
50 pub const UINT8C: i32 = 0;
52 pub const INT8: i32 = 1;
54 pub const UINT8: i32 = 2;
56 pub const INT16: i32 = 3;
58 pub const UINT16: i32 = 4;
60 pub const INT32: i32 = 5;
62 pub const UINT32: i32 = 6;
64 pub const BIG_INT64: i32 = 7;
66 pub const BIG_UINT64: i32 = 8;
68 pub const FLOAT16: i32 = 9;
70 pub const FLOAT32: i32 = 10;
72 pub const FLOAT64: i32 = 11;
74}
75
76#[unsafe(no_mangle)]
88pub unsafe extern "C" fn JS_NewArrayBuffer(
89 ctx: *mut JsContext,
90 buf: *mut u8,
91 len: usize,
92 free_func: FreeArrayBufferDataFunc,
93 opaque: *mut c_void,
94 is_shared: Bool,
95) -> JsValue {
96 let Some(_context) = (unsafe { ctx.as_ref() }) else {
97 return jsvalue::EXCEPTION;
98 };
99 if is_shared != 0 {
100 let _ = is_shared;
104 }
105 let Some(context) = (unsafe { ctx.as_ref() }) else {
106 return jsvalue::EXCEPTION;
107 };
108 let bytes: Vec<u8> = if buf.is_null() {
109 vec![0; len]
110 } else {
111 unsafe { core::slice::from_raw_parts(buf, len) }.to_vec()
113 };
114 let release: Option<Box<dyn FnOnce()>> = free_func.map(|free_func| {
119 let buf = buf.cast::<c_void>();
120 Box::new(move || {
121 unsafe { free_func(opaque, buf) };
124 }) as Box<dyn FnOnce()>
125 });
126 context.with_current_interp(|interp| {
127 let buffer = match release {
128 Some(release) => builtins_typed::new_array_buffer_with_release(interp, bytes, release),
129 None => builtins_typed::new_array_buffer(interp, bytes),
130 };
131 jsvalue_of(Value::Object(buffer))
132 })
133}
134
135#[unsafe(no_mangle)]
141pub unsafe extern "C" fn JS_DetachArrayBuffer(ctx: *mut JsContext, obj: JsValue) {
142 let Some(_context) = (unsafe { ctx.as_ref() }) else {
143 return;
144 };
145 let Some(Value::Object(object)) = (unsafe { arg(obj) }) else {
146 return;
147 };
148 let Some(storage) = object.array_buffer_storage() else {
149 return;
150 };
151 let mut storage = storage.borrow_mut();
152 storage.bytes = Vec::new();
156 storage.detached = true;
157}
158
159#[unsafe(no_mangle)]
168pub unsafe extern "C" fn JS_NewTypedArray(
169 ctx: *mut JsContext,
170 argc: c_int,
171 argv: *const JsValue,
172 array_type: c_int,
173) -> JsValue {
174 let Some(context) = (unsafe { ctx.as_ref() }) else {
175 return jsvalue::EXCEPTION;
176 };
177 let element = match builtins_typed::element_from_kind(array_type) {
178 Some(element) => element,
179 None => {
180 return runtime::throw_error(context, "TypeError", "unknown typed array kind");
181 }
182 };
183 let args = unsafe { crate::calls::decode_args(argc, argv) };
185 let result = context
186 .with_current_interp(|interp| builtins_typed::construct_typed_array(interp, element, args));
187 runtime::result_value(context, result)
188}
189
190#[unsafe(no_mangle)]
196pub unsafe extern "C" fn JS_NewUint8Array(
197 ctx: *mut JsContext,
198 buf: *mut u8,
199 len: usize,
200 free_func: FreeArrayBufferDataFunc,
201 opaque: *mut c_void,
202 is_shared: Bool,
203) -> JsValue {
204 let buffer = unsafe { JS_NewArrayBuffer(ctx, buf, len, free_func, opaque, is_shared) };
206 if buffer.is_exception() {
207 return buffer;
208 }
209 let Some(context) = (unsafe { ctx.as_ref() }) else {
211 return jsvalue::EXCEPTION;
212 };
213 context.with_current_interp(|interp| match unsafe { arg(buffer) } {
214 Some(Value::Object(object)) => {
215 let view = builtins_typed::new_view(interp, &object, builtins_typed::UINT8, 0, len);
216 jsvalue_of(Value::Object(view))
217 }
218 _ => jsvalue::EXCEPTION,
219 })
220}
221
222#[unsafe(no_mangle)]
228pub unsafe extern "C" fn JS_NewUint8ArrayCopy(
229 ctx: *mut JsContext,
230 buf: *const u8,
231 len: usize,
232) -> JsValue {
233 let buffer = unsafe { crate::bjson::JS_NewArrayBufferCopy(ctx, buf, len) };
235 if buffer.is_exception() {
236 return buffer;
237 }
238 let Some(context) = (unsafe { ctx.as_ref() }) else {
239 return jsvalue::EXCEPTION;
240 };
241 context.with_current_interp(|interp| match unsafe { arg(buffer) } {
242 Some(Value::Object(object)) => {
243 let view = builtins_typed::new_view(interp, &object, builtins_typed::UINT8, 0, len);
244 jsvalue_of(Value::Object(view))
245 }
246 _ => jsvalue::EXCEPTION,
247 })
248}
249
250#[unsafe(no_mangle)]
259pub unsafe extern "C" fn JS_NewUint8ArrayCopy2(
260 ctx: *mut JsContext,
261 buf: *const u8,
262 len: usize,
263 is_immutable: Bool,
264) -> JsValue {
265 if is_immutable != 0 {
266 let Some(context) = (unsafe { ctx.as_ref() }) else {
267 return jsvalue::EXCEPTION;
268 };
269 return runtime::throw_error(
270 context,
271 "TypeError",
272 "immutable ArrayBuffers are not supported",
273 );
274 }
275 unsafe { JS_NewUint8ArrayCopy(ctx, buf, len) }
277}
278
279#[unsafe(no_mangle)]
285pub unsafe extern "C" fn JS_GetTypedArrayBuffer(
286 ctx: *mut JsContext,
287 obj: JsValue,
288 pbyte_offset: *mut usize,
289 pbyte_length: *mut usize,
290 pbytes_per_element: *mut usize,
291) -> JsValue {
292 let Some(context) = (unsafe { ctx.as_ref() }) else {
293 return jsvalue::EXCEPTION;
294 };
295 let Some(Value::Object(object)) = (unsafe { arg(obj) }) else {
296 return runtime::throw_error(context, "TypeError", "not a typed array");
297 };
298 let Some((buffer, element, offset, length)) = object.typed_array_parts() else {
299 return runtime::throw_error(context, "TypeError", "not a typed array");
300 };
301 let size = element.size();
302 if !pbyte_offset.is_null() {
303 unsafe { *pbyte_offset = offset * size };
305 }
306 if !pbyte_length.is_null() {
307 unsafe { *pbyte_length = length * size };
309 }
310 if !pbytes_per_element.is_null() {
311 unsafe { *pbytes_per_element = size };
313 }
314 jsvalue_of(Value::Object(buffer))
315}
316
317#[unsafe(no_mangle)]
323pub unsafe extern "C" fn JS_GetTypedArrayType(obj: JsValue) -> c_int {
324 let Some(Value::Object(object)) = (unsafe { arg(obj) }) else {
325 return -1;
326 };
327 match object.typed_array_element_type() {
328 Some(element) => builtins_typed::kind_from_element(element),
329 None => -1,
330 }
331}
332
333#[unsafe(no_mangle)]
342pub unsafe extern "C" fn JS_AtomicsPollWaiters(_ctx: *mut JsContext) -> i64 {
343 0
344}
345
346#[unsafe(no_mangle)]
352pub unsafe extern "C" fn JS_AtomicsSetAsyncWaker(_cb: *const c_void, _opaque: *mut c_void) {}
353
354#[unsafe(no_mangle)]
360pub unsafe extern "C" fn JS_SetSharedArrayBufferFunctions(
361 _rt: *mut runtime::JsRuntime,
362 _sf: *const c_void,
363) {
364}
365
366#[unsafe(no_mangle)]
376pub unsafe extern "C" fn JS_JSONStringify(
377 ctx: *mut JsContext,
378 obj: JsValue,
379 replacer: JsValue,
380 space0: JsValue,
381) -> JsValue {
382 let Some(context) = (unsafe { ctx.as_ref() }) else {
383 return jsvalue::EXCEPTION;
384 };
385 let value = unsafe { arg(obj) }.unwrap_or(Value::Undefined);
386 let replacer = unsafe { arg(replacer) }.unwrap_or(Value::Undefined);
387 let space = unsafe { arg(space0) }.unwrap_or(Value::Undefined);
388 let result = context.with_current_interp(|interp| {
389 let global = Value::Object(interp.realm.global.clone());
390 let json = interp.read_field(&global, "JSON")?;
391 let stringify = interp.read_field(&json, "stringify")?;
392 interp.call_value(stringify, json, vec![value, replacer, space])
393 });
394 runtime::result_value(context, result)
395}
396
397#[unsafe(no_mangle)]
403pub unsafe extern "C" fn JS_ParseJSON(
404 ctx: *mut JsContext,
405 buf: *const core::ffi::c_char,
406 buf_len: usize,
407 filename: *const core::ffi::c_char,
408) -> JsValue {
409 unsafe { JS_ParseJSON2(ctx, buf, buf_len, filename, 0) }
411}
412
413#[unsafe(no_mangle)]
422pub unsafe extern "C" fn JS_ParseJSON2(
423 ctx: *mut JsContext,
424 buf: *const core::ffi::c_char,
425 buf_len: usize,
426 _filename: *const core::ffi::c_char,
427 flags: c_int,
428) -> JsValue {
429 let Some(context) = (unsafe { ctx.as_ref() }) else {
430 return jsvalue::EXCEPTION;
431 };
432 if buf.is_null() {
433 return runtime::throw_error(context, "SyntaxError", "null JSON");
434 }
435 let bytes = unsafe { core::slice::from_raw_parts(buf.cast::<u8>(), buf_len) };
437 let text = String::from_utf8_lossy(bytes).into_owned();
438 const JS_PARSE_JSON_EXT: c_int = 1 << 0;
439 let extended = flags & JS_PARSE_JSON_EXT != 0;
440 let result = context
441 .with_current_interp(|interp| engine::builtins::json::parse_text(interp, &text, extended));
442 runtime::result_value(context, result)
443}
444
445#[unsafe(no_mangle)]
455pub unsafe extern "C" fn JS_ParseJSON3(
456 ctx: *mut JsContext,
457 buf: *const core::ffi::c_char,
458 buf_len: usize,
459 filename: *const core::ffi::c_char,
460 flags: c_int,
461 _pr: *mut c_void,
462) -> JsValue {
463 unsafe { JS_ParseJSON2(ctx, buf, buf_len, filename, flags) }
465}
466
467#[unsafe(no_mangle)]
473pub unsafe extern "C" fn JS_SameValue(_ctx: *mut JsContext, op1: JsValue, op2: JsValue) -> Bool {
474 match (unsafe { arg(op1) }, unsafe { arg(op2) }) {
475 (Some(left), Some(right)) => c_int::from(left.same_value(&right)),
476 _ => 0,
477 }
478}
479
480#[unsafe(no_mangle)]
486pub unsafe extern "C" fn JS_SameValueZero(
487 _ctx: *mut JsContext,
488 op1: JsValue,
489 op2: JsValue,
490) -> Bool {
491 match (unsafe { arg(op1) }, unsafe { arg(op2) }) {
492 (Some(left), Some(right)) => c_int::from(left.same_value_zero(&right)),
493 _ => 0,
494 }
495}
496
497#[unsafe(no_mangle)]
503pub unsafe extern "C" fn JS_StrictEq(_ctx: *mut JsContext, op1: JsValue, op2: JsValue) -> Bool {
504 match (unsafe { arg(op1) }, unsafe { arg(op2) }) {
505 (Some(left), Some(right)) => c_int::from(left.strict_equals(&right)),
506 _ => 0,
507 }
508}
509
510const _: Option<Atom> = None;