use core::ffi::{c_char, c_void};
use core::ptr::{null_mut, NonNull};
use crate::capi;
use crate::glue;
use crate::trampoline;
pub const REDJS_OK: i32 = 0;
pub const REDJS_EXCEPTION: i32 = 1;
pub const REDJS_BAD_ARG: i32 = 2;
pub const REDJS_EXHAUSTED: i32 = 3;
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct RjsLayout {
pub abi_version: u32,
pub cf_argc_offset: u32,
pub cf_this_offset: u32,
pub cf_args_offset: u32,
pub cf_callee_offset: u32,
pub reserved0: u32,
pub tag_number: u64,
pub double_encode_offset: u64,
pub not_cell_mask: u64,
pub value_undefined: u64,
pub value_null: u64,
pub value_true: u64,
pub value_false: u64,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct RjsHeapStats {
pub heap_size: u64,
pub heap_capacity: u64,
pub extra_memory: u64,
pub object_count: u64,
pub protected_object_count: u64,
pub global_object_count: u64,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct RjsStr {
pub ptr: *const c_void,
pub len: u32,
pub is8bit: u32,
}
impl Default for RjsStr {
fn default() -> Self {
Self { ptr: core::ptr::null(), len: 0, is8bit: 1 }
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct RjsTaInfo {
pub data: *mut c_void,
pub length: u32,
pub byte_length: u32,
pub byte_offset: u32,
pub kind: u32,
pub detached: u32,
pub shared: u32,
}
impl Default for RjsTaInfo {
fn default() -> Self {
Self { data: core::ptr::null_mut(), length: 0, byte_length: 0, byte_offset: 0, kind: 0, detached: 0, shared: 0 }
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct RjsAbInfo {
pub data: *mut c_void,
pub byte_length: u64,
pub detached: u32,
pub shared: u32,
}
impl Default for RjsAbInfo {
fn default() -> Self {
Self { data: core::ptr::null_mut(), byte_length: 0, detached: 0, shared: 0 }
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct RjsPumpStats {
pub now: f64,
pub timers_fired: u32,
pub raf_fired: u32,
pub raf_pending: u32,
pub timers_active: u32,
pub next_timer_deadline: f64,
}
#[no_mangle]
pub unsafe extern "C" fn redjs_init(options: *const c_char) -> i32 {
core::hint::black_box(crate::icu::bun_icu_maybe_decompress as *const ());
glue::init(options)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_set_options(options: *const c_char) -> i32 {
glue::rjs_set_options(options)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_context_create() -> *mut c_void {
glue::init(core::ptr::null());
#[cfg(feature = "native-aot")]
let aot_global = crate::aot::create_context_if_active();
#[cfg(not(feature = "native-aot"))]
let aot_global: *mut c_void = null_mut();
let g = if aot_global.is_null() { capi::JSGlobalContextCreate(null_mut()) } else { aot_global };
if !g.is_null() {
crate::context::register(g);
crate::errors::install_rejection(g);
}
g
}
#[no_mangle]
pub unsafe extern "C" fn redjs_context_release(ctx: *mut c_void) {
#[cfg(feature = "native-aot")]
crate::aot::forget_context(ctx);
crate::context::forget(ctx);
capi::JSGlobalContextRelease(ctx);
}
#[no_mangle]
pub unsafe extern "C" fn redjs_context_id(ctx: *mut c_void) -> u64 {
crate::context::id_of(ctx).map(|id| id.0).unwrap_or(0)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_global(ctx: *mut c_void) -> u64 {
glue::rjs_global(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_layout(out: *mut RjsLayout) {
glue::rjs_layout(out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_lock(ctx: *mut c_void) {
glue::rjs_lock(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_unlock(ctx: *mut c_void) {
glue::rjs_unlock(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_drain_microtasks(ctx: *mut c_void) {
crate::web::checkpoint(ctx);
}
#[no_mangle]
pub unsafe extern "C" fn redjs_gc(ctx: *mut c_void, mode: i32) {
glue::rjs_gc(ctx, mode)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_heap_stats(ctx: *mut c_void, out: *mut RjsHeapStats) {
glue::rjs_heap_stats(ctx, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_report_extra_memory(ctx: *mut c_void, bytes: u64) {
glue::rjs_report_extra_memory(ctx, bytes)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_bytecode_generate(
ctx: *mut c_void, src: *const u8, len: usize, url: *const u8, url_len: usize,
depth: u32, handle: *mut *mut c_void, bytes: *mut *const u8,
byte_length: *mut usize, out: *mut u64,
) -> i32 {
glue::rjs_bytecode_generate(ctx, src, len, url, url_len, depth, handle, bytes, byte_length, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_bytecode_free(handle: *mut c_void) {
glue::rjs_bytecode_free(handle)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_eval8_cached(
ctx: *mut c_void, src: *const u8, len: usize, url: *const u8, url_len: usize,
bytes: *const u8, byte_length: usize, used: *mut u32, out: *mut u64,
) -> i32 {
glue::rjs_eval8_cached(ctx, src, len, url, url_len, bytes, byte_length, used, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_aot_build_info(destination: *mut u8, capacity: usize) -> usize {
#[cfg(feature = "native-aot")]
{
let info = format!(
"{{\"frontend\":\"{}\",\"engine\":\"{}\",\"tableId\":\"{}\",\"compileOptions\":\"{}\"}}",
crate::aot::FRONTEND_REVISION, crate::aot::ENGINE_VERSION,
crate::aot::BUILD_TABLE_ID, crate::aot::COMPILE_OPTIONS,
);
if capacity >= info.len() && !destination.is_null() {
core::ptr::copy_nonoverlapping(info.as_ptr(), destination, info.len());
}
info.len()
}
#[cfg(not(feature = "native-aot"))]
{ let _ = (destination, capacity); 0 }
}
#[no_mangle]
pub unsafe extern "C" fn redjs_aot_load_handle(file: isize, identity: *const u8, profile: u32) -> u32 {
#[cfg(feature = "native-aot")]
{ glue::rjs_aot_load_handle_with_profile(file, identity, profile) }
#[cfg(not(feature = "native-aot"))]
{ let _ = (file, identity, profile); 0 }
}
#[no_mangle]
pub unsafe extern "C" fn redjs_aot_run(ctx: *mut c_void, module: u32, out: *mut u64) -> i32 {
if ctx.is_null() || out.is_null() { return REDJS_BAD_ARG; }
#[cfg(feature = "native-aot")]
{ glue::rjs_aot_run(ctx, module, out) }
#[cfg(not(feature = "native-aot"))]
{ let _ = module; REDJS_BAD_ARG }
}
#[no_mangle]
pub unsafe extern "C" fn redjs_aot_is_active() -> i32 {
#[cfg(feature = "native-aot")]
{ glue::rjs_aot_is_active() }
#[cfg(not(feature = "native-aot"))]
{ 0 }
}
#[no_mangle]
pub unsafe extern "C" fn redjs_eval16(
ctx: *mut c_void,
src: *const u16,
len: usize,
url: *const u16,
url_len: usize,
out: *mut u64,
) -> i32 {
glue::rjs_eval16(ctx, src, len, url, url_len, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_eval8(
ctx: *mut c_void,
src: *const u8,
len: usize,
url: *const u8,
url_len: usize,
out: *mut u64,
) -> i32 {
glue::rjs_eval8(ctx, src, len, url, url_len, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_kind(ctx: *mut c_void, v: u64) -> u32 {
glue::rjs_kind(ctx, v)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_to_number(ctx: *mut c_void, v: u64, out: *mut f64) -> i32 {
glue::rjs_to_number(ctx, v, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_to_string(ctx: *mut c_void, v: u64, out: *mut u64) -> i32 {
glue::rjs_to_string(ctx, v, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_to_boolean(ctx: *mut c_void, v: u64) -> i32 {
glue::rjs_to_boolean(ctx, v)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_strict_equals(ctx: *mut c_void, a: u64, b: u64) -> i32 {
glue::rjs_strict_equals(ctx, a, b)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_protect(ctx: *mut c_void, v: u64) {
capi::JSValueProtect(ctx, v as usize as capi::JSValueRef);
}
#[no_mangle]
pub unsafe extern "C" fn redjs_unprotect(ctx: *mut c_void, v: u64) {
capi::JSValueUnprotect(ctx, v as usize as capi::JSValueRef);
}
#[no_mangle]
pub unsafe extern "C" fn redjs_string16(ctx: *mut c_void, p: *const u16, len: usize, out: *mut u64) -> i32 {
glue::rjs_string16(ctx, p, len, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_string8(ctx: *mut c_void, p: *const u8, len: usize, out: *mut u64) -> i32 {
glue::rjs_string8(ctx, p, len, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_string_info(ctx: *mut c_void, v: u64, out: *mut RjsStr) -> i32 {
glue::rjs_string_info(ctx, v, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_atom16(ctx: *mut c_void, p: *const u16, len: usize) -> *mut c_void {
glue::rjs_atom16(ctx, p, len)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_atom8(ctx: *mut c_void, p: *const u8, len: usize) -> *mut c_void {
glue::rjs_atom8(ctx, p, len)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_atom_private(ctx: *mut c_void, p: *const u8, len: usize) -> *mut c_void {
glue::rjs_atom_private(ctx, p, len)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_atom_free(ctx: *mut c_void, atom: *mut c_void) {
glue::rjs_atom_free(ctx, atom)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_get(ctx: *mut c_void, base: u64, atom: *mut c_void, out: *mut u64) -> i32 {
glue::rjs_get(ctx, base, atom, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_set(ctx: *mut c_void, obj: u64, atom: *mut c_void, v: u64) -> i32 {
glue::rjs_set(ctx, obj, atom, v)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_define(ctx: *mut c_void, obj: u64, atom: *mut c_void, v: u64, attrs: u32) -> i32 {
glue::rjs_define(ctx, obj, atom, v, attrs)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_get_index(ctx: *mut c_void, base: u64, index: u32, out: *mut u64) -> i32 {
glue::rjs_get_index(ctx, base, index, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_set_index(ctx: *mut c_void, obj: u64, index: u32, v: u64) -> i32 {
glue::rjs_set_index(ctx, obj, index, v)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_has(ctx: *mut c_void, obj: u64, atom: *mut c_void) -> i32 {
glue::rjs_has(ctx, obj, atom)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_delete(ctx: *mut c_void, obj: u64, atom: *mut c_void) -> i32 {
glue::rjs_delete(ctx, obj, atom)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_object_create(ctx: *mut c_void, proto: u64, out: *mut u64) -> i32 {
glue::rjs_object_create(ctx, proto, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_array_create(ctx: *mut c_void, len: u32, out: *mut u64) -> i32 {
glue::rjs_array_create(ctx, len, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_array_length(ctx: *mut c_void, v: u64, out: *mut u32) -> i32 {
glue::rjs_array_length(ctx, v, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_function_create(
ctx: *mut c_void,
name: *const u16,
name_len: usize,
arity: u32,
ms_fn: *const c_void,
out: *mut u64,
) -> i32 {
let Some(entry) = trampoline::acquire(trampoline::KIND_BARE, ms_fn, null_mut()) else {
return REDJS_EXHAUSTED;
};
glue::rjs_function_create(ctx, name, name_len, arity, entry, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_function_create_with_data(
ctx: *mut c_void,
name: *const u16,
name_len: usize,
arity: u32,
ms_fn: *const c_void,
data: *mut c_void,
out: *mut u64,
) -> i32 {
let Some(entry) = trampoline::acquire(trampoline::KIND_DATA, ms_fn, data) else {
return REDJS_EXHAUSTED;
};
glue::rjs_function_create(ctx, name, name_len, arity, entry, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_function_create_sysv(
ctx: *mut c_void,
name: *const u16,
name_len: usize,
arity: u32,
sysv_fn: *const c_void,
out: *mut u64,
) -> i32 {
glue::rjs_function_create(ctx, name, name_len, arity, sysv_fn, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_host_slots(used: *mut u32, total: *mut u32) {
if !used.is_null() {
*used = trampoline::slots_used();
}
if !total.is_null() {
*total = trampoline::slots_total();
}
}
#[no_mangle]
pub unsafe extern "C" fn redjs_call(
ctx: *mut c_void,
f: u64,
this_v: u64,
args: *const u64,
argc: u32,
out: *mut u64,
) -> i32 {
glue::rjs_call(ctx, f, this_v, args, argc, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_construct(
ctx: *mut c_void,
ctor: u64,
args: *const u64,
argc: u32,
out: *mut u64,
) -> i32 {
glue::rjs_construct(ctx, ctor, args, argc, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_throw(ctx: *mut c_void, err: u64) {
glue::rjs_throw(ctx, err)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_error_create(
ctx: *mut c_void,
kind: u32,
msg: *const u16,
len: usize,
out: *mut u64,
) -> i32 {
glue::rjs_error_create(ctx, kind, msg, len, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_error_info(ctx: *mut c_void, err: u64, msg_out: *mut u64, stack_out: *mut u64) -> i32 {
glue::rjs_error_info(ctx, err, msg_out, stack_out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_exception_pending(ctx: *mut c_void, out: *mut u64) -> i32 {
glue::rjs_exception_pending(ctx, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_arraybuffer_external(
ctx: *mut c_void,
bytes: *mut c_void,
byte_len: usize,
dealloc: *const c_void, dealloc_ctx: *mut c_void,
out: *mut u64,
) -> i32 {
let mut exc: capi::JSValueRef = core::ptr::null();
let dealloc: capi::JSTypedArrayBytesDeallocator = core::mem::transmute(dealloc);
let obj = capi::JSObjectMakeArrayBufferWithBytesNoCopy(ctx, bytes, byte_len, dealloc, dealloc_ctx, &mut exc);
finish_capi_make(obj, exc, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_arraybuffer_info(ctx: *mut c_void, v: u64, out: *mut RjsAbInfo) -> i32 {
glue::rjs_arraybuffer_info(ctx, v, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_arraybuffer_detach(ctx: *mut c_void, v: u64) -> i32 {
glue::rjs_arraybuffer_detach(ctx, v)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_typedarray_create(ctx: *mut c_void, kind: u32, length: usize, out: *mut u64) -> i32 {
let capi_kind = capi::ta_kind_to_capi(kind);
if capi_kind == capi::K_JS_TYPED_ARRAY_TYPE_NONE {
return REDJS_BAD_ARG;
}
let mut exc: capi::JSValueRef = core::ptr::null();
let obj = capi::JSObjectMakeTypedArray(ctx, capi_kind, length, &mut exc);
finish_capi_make(obj, exc, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_typedarray_external(
ctx: *mut c_void,
kind: u32,
bytes: *mut c_void,
byte_len: usize,
dealloc: *const c_void, dealloc_ctx: *mut c_void,
out: *mut u64,
) -> i32 {
let capi_kind = capi::ta_kind_to_capi(kind);
if capi_kind == capi::K_JS_TYPED_ARRAY_TYPE_NONE {
return REDJS_BAD_ARG;
}
let mut exc: capi::JSValueRef = core::ptr::null();
let dealloc: capi::JSTypedArrayBytesDeallocator = core::mem::transmute(dealloc);
let obj = capi::JSObjectMakeTypedArrayWithBytesNoCopy(ctx, capi_kind, bytes, byte_len, dealloc, dealloc_ctx, &mut exc);
finish_capi_make(obj, exc, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_typedarray_wrap(
ctx: *mut c_void,
kind: u32,
ab: u64,
byte_offset: usize,
length: usize,
out: *mut u64,
) -> i32 {
let capi_kind = capi::ta_kind_to_capi(kind);
if capi_kind == capi::K_JS_TYPED_ARRAY_TYPE_NONE {
return REDJS_BAD_ARG;
}
let mut exc: capi::JSValueRef = core::ptr::null();
let obj = capi::JSObjectMakeTypedArrayWithArrayBufferAndOffset(
ctx,
capi_kind,
ab as usize as capi::JSObjectRef,
byte_offset,
length,
&mut exc,
);
finish_capi_make(obj, exc, out)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_typedarray_info(ctx: *mut c_void, v: u64, out: *mut RjsTaInfo) -> i32 {
glue::rjs_typedarray_info(ctx, v, out)
}
unsafe fn finish_capi_make(obj: capi::JSObjectRef, exc: capi::JSValueRef, out: *mut u64) -> i32 {
if obj.is_null() {
if !exc.is_null() && !out.is_null() {
*out = exc as usize as u64;
return REDJS_EXCEPTION;
}
return REDJS_BAD_ARG;
}
if !out.is_null() {
*out = obj as usize as u64;
}
REDJS_OK
}
#[no_mangle]
pub unsafe extern "C" fn redjs_promise_create(
ctx: *mut c_void,
resolve_out: *mut u64,
reject_out: *mut u64,
out: *mut u64,
) -> i32 {
let mut resolve: capi::JSObjectRef = null_mut();
let mut reject: capi::JSObjectRef = null_mut();
let mut exc: capi::JSValueRef = core::ptr::null();
let promise = capi::JSObjectMakeDeferredPromise(ctx, &mut resolve, &mut reject, &mut exc);
if promise.is_null() {
if !exc.is_null() && !out.is_null() {
*out = exc as usize as u64;
return REDJS_EXCEPTION;
}
return REDJS_BAD_ARG;
}
*resolve_out = resolve as usize as u64;
*reject_out = reject as usize as u64;
*out = promise as usize as u64;
REDJS_OK
}
#[no_mangle]
pub unsafe extern "C" fn redjs_promise_info(ctx: *mut c_void, v: u64, status_out: *mut u32, result_out: *mut u64) -> i32 {
glue::rjs_promise_info(ctx, v, status_out, result_out)
}
static HOST_FINALIZER: core::sync::atomic::AtomicPtr<c_void> =
core::sync::atomic::AtomicPtr::new(null_mut());
unsafe extern "C" fn host_finalize(obj: capi::JSObjectRef) {
let f = HOST_FINALIZER.load(core::sync::atomic::Ordering::Acquire);
if let Some(f) = NonNull::new(f) {
let data = capi::JSObjectGetPrivate(obj);
if !data.is_null() {
let cb: unsafe extern "C" fn(*mut c_void) = core::mem::transmute(f.as_ptr());
cb(data);
}
}
}
#[no_mangle]
pub unsafe extern "C" fn redjs_set_host_finalizer(f: *const c_void) {
HOST_FINALIZER.store(f as *mut c_void, core::sync::atomic::Ordering::Release);
}
#[no_mangle]
pub unsafe extern "C" fn redjs_hostclass_create(name: *const c_char) -> *mut c_void {
let mut def = capi::JSClassDefinition::empty();
def.class_name = name;
def.finalize = Some(host_finalize);
capi::JSClassCreate(&def)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_hostobject_create(
ctx: *mut c_void,
cls: *mut c_void,
data: *mut c_void,
proto: u64,
out: *mut u64,
) -> i32 {
let obj = capi::JSObjectMake(ctx, cls, data);
if obj.is_null() {
return REDJS_BAD_ARG;
}
if proto != 0 {
capi::JSObjectSetPrototype(ctx, obj, proto as usize as capi::JSValueRef);
}
if !out.is_null() {
*out = obj as usize as u64;
}
REDJS_OK
}
#[no_mangle]
pub unsafe extern "C" fn redjs_hostobject_data(v: u64) -> *mut c_void {
if v == 0 || (v & (0xfffe_0000_0000_0000u64 | 0x2)) != 0 {
return core::ptr::null_mut();
}
capi::JSObjectGetPrivate(v as usize as capi::JSObjectRef)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_hostobject_set_data(v: u64, data: *mut c_void) -> i32 {
if v == 0 || (v & (0xfffe_0000_0000_0000u64 | 0x2)) != 0 {
return 0;
}
capi::JSObjectSetPrivate(v as usize as capi::JSObjectRef, data) as i32
}
#[no_mangle]
pub unsafe extern "C" fn redjs_web_install(ctx: *mut c_void) -> i32 {
crate::web::install(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_pump(ctx: *mut c_void, flags: u32, stats: *mut RjsPumpStats) {
crate::web::pump(ctx, flags, stats)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_now() -> f64 {
crate::web::now_ms()
}
#[no_mangle]
pub unsafe extern "C" fn redjs_set_uncaught_handler(handler: *const c_void) {
crate::web::set_uncaught(handler)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_webshim_install(
ctx: *mut c_void,
scripts_root: *const u16,
scripts_len: usize,
assets_root: *const u16,
assets_len: usize,
) -> i32 {
let scripts = if scripts_len == 0 {
String::new()
} else {
String::from_utf16_lossy(core::slice::from_raw_parts(scripts_root, scripts_len))
};
let assets = if assets_len == 0 {
String::new()
} else {
String::from_utf16_lossy(core::slice::from_raw_parts(assets_root, assets_len))
};
crate::webapi::install(ctx, &scripts, &assets)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_navigator_set_user_agent(ua: *const u16, len: usize) {
if ua.is_null() {
crate::webapi::navigator::set_user_agent(None);
return;
}
let s = String::from_utf16_lossy(core::slice::from_raw_parts(ua, len));
crate::webapi::navigator::set_user_agent(Some(&s));
}
#[no_mangle]
pub unsafe extern "C" fn redjs_navigator_set_languages(list: *const u16, len: usize) {
if list.is_null() {
crate::webapi::navigator::set_languages(None);
return;
}
let s = String::from_utf16_lossy(core::slice::from_raw_parts(list, len));
crate::webapi::navigator::set_languages(Some(s.split(',').map(str::to_owned).collect()));
}
#[no_mangle]
pub extern "C" fn redjs_navigator_set_on_line(state: i32) {
crate::webapi::navigator::set_on_line(match state {
s if s < 0 => None,
0 => Some(false),
_ => Some(true),
});
}
#[no_mangle]
pub extern "C" fn redjs_idb_set_budget_us(us: u32) {
crate::web::set_pump_budget_us(us);
}
#[no_mangle]
pub extern "C" fn redjs_idb_shutdown(timeout_ms: u32) -> i32 {
#[cfg(feature = "indexeddb")]
{
crate::webapi::indexeddb::shutdown(std::time::Duration::from_millis(timeout_ms as u64)) as i32
}
#[cfg(not(feature = "indexeddb"))]
{
let _ = timeout_ms;
1
}
}
#[no_mangle]
pub unsafe extern "C" fn redjs_idb_configure(folder_utf8: *const u8, len: usize, key32: *const u8, quota_bytes: u64) -> i32 {
#[cfg(feature = "indexeddb-sqlite")]
{
if folder_utf8.is_null() || len == 0 {
return 2;
}
let Ok(folder) = std::str::from_utf8(std::slice::from_raw_parts(folder_utf8, len)) else { return 2 };
let key = if key32.is_null() {
None
} else {
let mut k = [0u8; 32];
k.copy_from_slice(std::slice::from_raw_parts(key32, 32));
Some(k)
};
match crate::webapi::indexeddb::configure(std::path::PathBuf::from(folder), key, quota_bytes) {
Ok(()) => 0,
Err(_) => 1,
}
}
#[cfg(not(feature = "indexeddb-sqlite"))]
{
let _ = (folder_utf8, len, key32, quota_bytes);
3
}
}
#[no_mangle]
pub unsafe extern "C" fn redjs_content_provider_set(ctx: *mut c_void, read_fn: *const c_void) {
let read = if read_fn.is_null() {
None
} else {
Some(core::mem::transmute::<*const c_void, crate::webapi::fsutil::ContentReadFn>(read_fn))
};
crate::context::ensure(ctx);
crate::webapi::fsutil::set_content_provider(ctx, read);
}
#[no_mangle]
pub unsafe extern "C" fn redjs_dom_resize(ctx: *mut c_void, width: i32, height: i32, dpr: f64) {
crate::webapi::dom::resize(ctx, width, height, dpr)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_dom_set_viewport(
_ctx: *mut c_void,
width: i32,
height: i32,
outer_width: i32,
outer_height: i32,
dpr: f64,
) {
crate::webapi::dom::set_viewport((width, height), (outer_width, outer_height), dpr)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_dom_set_visibility(ctx: *mut c_void, hidden: i32) {
crate::webapi::dom::set_visibility(ctx, hidden != 0)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_dom_content_loaded(ctx: *mut c_void) {
crate::webapi::dom::dom_content_loaded(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_dom_register_element(ctx: *mut c_void, id: *const u16, id_len: usize, value: u64) {
let id = String::from_utf16_lossy(core::slice::from_raw_parts(id, id_len));
crate::webapi::dom::register_element(ctx, &id, value)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_image_pixels(
ctx: *mut c_void,
v: u64,
out_ptr: *mut *const u8,
out_byte_len: *mut usize,
out_width: *mut u32,
out_height: *mut u32,
) -> i32 {
let st = crate::webapi::util::state(ctx);
let bmp = if let Some(b) = crate::webapi::util::host_data::<crate::webapi::image::BitmapData>(ctx, st.bitmap_class, v) {
Some(&*b)
} else {
let slot = crate::webapi::util::get_a(ctx, v, st.s_image);
crate::webapi::util::host_data::<crate::webapi::image::BitmapData>(ctx, st.bitmap_class, slot).map(|b| &*b)
};
match bmp {
Some(b) => {
*out_ptr = b.rgba.as_ptr();
*out_byte_len = b.rgba.len();
*out_width = b.width;
*out_height = b.height;
REDJS_OK
}
None => REDJS_BAD_ARG,
}
}
#[no_mangle]
pub unsafe extern "C" fn redjs_define_accessor(
ctx: *mut c_void,
obj: u64,
atom: *mut c_void,
getter: u64,
setter: u64,
attrs: u32,
) -> i32 {
glue::rjs_define_accessor(ctx, obj, atom, getter, setter, attrs)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_release_memory(ctx: *mut c_void) {
glue::rjs_release_memory(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_release_memory_and_code(ctx: *mut c_void) {
glue::rjs_release_memory_and_code(ctx)
}
#[no_mangle]
pub unsafe extern "C" fn redjs_dialog_handler_set(ctx: *mut c_void, handler: *const c_void) {
let f = if handler.is_null() {
None
} else {
Some(core::mem::transmute::<*const c_void, crate::webapi::dialogs::DialogFn>(handler))
};
crate::context::ensure(ctx); crate::webapi::dialogs::set_handler(ctx, f);
}