use core::ffi::{c_char, c_int};
use engine::interp::JsResult;
use engine::object::Property;
use engine::{ClassId, Value};
use crate::cdefs::{self, CFunctionEnum, JSCFunctionListEntry, JSCFunctionType, JSClassDef, prop};
use crate::jsvalue::{self, JsValue};
use crate::runtime::{self, JsContext, JsRuntime};
use crate::value_helpers::jsvalue_of;
pub(crate) unsafe fn arg(value: JsValue) -> Option<Value> {
unsafe { runtime::argument(value) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewObject(ctx: *mut JsContext) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
context.with_current_interp(|interp| {
let proto = interp.realm.object_proto.clone();
jsvalue_of(Value::Object(interp.new_object(Some(proto))))
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetGlobalObject(ctx: *mut JsContext) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
context.with_current_interp(|interp| {
let global = interp.realm.global.clone();
jsvalue_of(Value::Object(global))
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewObjectProtoClass(
ctx: *mut JsContext,
proto: JsValue,
class_id: cdefs::ClassId,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let Some(proto) = (unsafe { arg(proto) }) else {
return runtime::throw_error(context, "TypeError", "prototype is not an object");
};
let Some(prototype) = proto.as_object().cloned() else {
return runtime::throw_error(context, "TypeError", "prototype is not an object");
};
context.with_current_interp(|interp| {
match interp.new_host_object(Some(prototype), ClassId::from_raw(class_id)) {
Some(object) => jsvalue_of(Value::Object(object)),
None => runtime::throw_error(
context,
"TypeError",
&format!("invalid class id {class_id}"),
),
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewObjectClass(ctx: *mut JsContext, class_id: c_int) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
context.with_current_interp(|interp| {
match interp.new_host_object(None, ClassId::from_raw(class_id as u32)) {
Some(object) => jsvalue_of(Value::Object(object)),
None => runtime::throw_error(
context,
"TypeError",
&format!("invalid class id {class_id}"),
),
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetPropertyStr(
ctx: *mut JsContext,
this_obj: JsValue,
name: *const c_char,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let Some(target) = (unsafe { arg(this_obj) }) else {
return runtime::throw_error(context, "TypeError", "not an object");
};
let name = unsafe { std::ffi::CStr::from_ptr(name) }.to_string_lossy();
let result = context.with_current_interp(|interp| interp.read_field(&target, &name));
runtime::result_value(context, result)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SetPropertyStr(
ctx: *mut JsContext,
this_obj: JsValue,
name: *const c_char,
value: JsValue,
) -> c_int {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return -1;
};
let Some(target) = (unsafe { arg(this_obj) }) else {
runtime::throw_error(context, "TypeError", "not an object");
return -1;
};
let Some(new_value) = (unsafe { arg(value) }) else {
runtime::throw_error(context, "TypeError", "not a value");
return -1;
};
let name = unsafe { std::ffi::CStr::from_ptr(name) }.to_string_lossy();
let result = context.with_current_interp(|interp| match target.as_object() {
Some(object) => interp.set_property(object, &name, new_value),
None => Err(engine::Signal::Throw(interp.make_error(
"TypeError",
&format!("cannot set property '{name}' of a primitive"),
))),
});
unsafe { runtime::release(value) };
match result {
Ok(()) => 0,
Err(signal) => {
let _ = runtime::result_value(context, Err(signal));
-1
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ToBool(ctx: *mut JsContext, val: JsValue) -> c_int {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return -1;
};
match unsafe { arg(val) } {
Some(value) => c_int::from(value.to_boolean()),
None => {
runtime::throw_error(context, "TypeError", "not a value");
-1
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ToIndex(ctx: *mut JsContext, plen: *mut u64, val: JsValue) -> c_int {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return -1;
};
let Some(value) = (unsafe { arg(val) }) else {
runtime::throw_error(context, "TypeError", "not a value");
return -1;
};
let number = match context.with_current_interp(|interp| interp.to_js_number(&value)) {
Ok(number) => number,
Err(signal) => {
let _ = runtime::result_value(context, Err(signal));
return -1;
}
};
let index = number.trunc();
if number.is_nan() || index != number || index < 0.0 || index > 9_007_199_254_740_991.0 {
runtime::throw_error(context, "RangeError", "invalid array index");
return -1;
}
if !plen.is_null() {
unsafe { *plen = index as u64 };
}
0
}
pub type RawFunction = *const core::ffi::c_void;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewCFunction2(
ctx: *mut JsContext,
func: RawFunction,
name: *const c_char,
length: c_int,
cproto: c_int,
magic: c_int,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let name = unsafe { std::ffi::CStr::from_ptr(name) }
.to_string_lossy()
.into_owned();
let Some(kind) = u8::try_from(cproto).ok().and_then(CFunctionEnum::from_raw) else {
return runtime::throw_error(
context,
"TypeError",
&format!("unsupported JSCFunctionEnum {cproto}"),
);
};
context.with_current_interp(|interp| {
let function = make_c_function(interp, func, kind, name, length, magic);
jsvalue_of(Value::Object(function))
})
}
pub(crate) fn make_c_function(
interp: &mut engine::Interp<'static>,
func: RawFunction,
kind: CFunctionEnum,
name: String,
length: c_int,
magic: c_int,
) -> engine::Gc<engine::JsObject> {
let arity = u32::try_from(length).unwrap_or(0);
let body: engine::object::HostFn = Box::new(move |interp, this, args| {
let context = current_context_or_null();
call_c_function(context, interp, func, kind, magic, this, args)
});
if kind.is_constructor() {
interp.new_host_ctor(name, arity, body)
} else {
interp.new_host_function(name, arity, body)
}
}
fn current_context_or_null() -> *mut JsContext {
runtime::current_context()
.map(|context| context as *const JsContext as *mut JsContext)
.unwrap_or(std::ptr::null_mut())
}
fn call_c_function<'i>(
ctx: *mut JsContext,
interp: &mut engine::Interp<'i>,
func: RawFunction,
kind: CFunctionEnum,
magic: c_int,
this: Value,
args: &[Value],
) -> JsResult<Value> {
let this = if kind.is_constructor() {
interp.current_new_target()
} else {
this
};
let this_value = crate::convert::encode(&this);
let arguments: Vec<JsValue> = args.iter().map(crate::convert::encode).collect();
let pointer = arguments.as_ptr();
let argc = c_int::try_from(arguments.len()).unwrap_or(c_int::MAX);
let context = current_context_or_null();
let _ = ctx;
let answer = unsafe {
match kind {
CFunctionEnum::Generic => {
let f: unsafe extern "C" fn(
*mut JsContext,
JsValue,
c_int,
*const JsValue,
) -> JsValue = core::mem::transmute(func);
f(context, this_value, argc, pointer)
}
CFunctionEnum::GenericMagic => {
let f: unsafe extern "C" fn(
*mut JsContext,
JsValue,
c_int,
*const JsValue,
c_int,
) -> JsValue = core::mem::transmute(func);
f(context, this_value, argc, pointer, magic)
}
CFunctionEnum::Constructor | CFunctionEnum::ConstructorOrFunc => {
let f: unsafe extern "C" fn(
*mut JsContext,
JsValue,
c_int,
*const JsValue,
) -> JsValue = core::mem::transmute(func);
f(context, this_value, argc, pointer)
}
CFunctionEnum::ConstructorMagic | CFunctionEnum::ConstructorOrFuncMagic => {
let f: unsafe extern "C" fn(
*mut JsContext,
JsValue,
c_int,
*const JsValue,
c_int,
) -> JsValue = core::mem::transmute(func);
f(context, this_value, argc, pointer, magic)
}
CFunctionEnum::ConstructorOrFuncFlags => {
let flags = if this_value.is_undefined() {
0
} else {
cdefs::JS_CALL_FLAG_CONSTRUCTOR
};
let f: unsafe extern "C" fn(
*mut JsContext,
JsValue,
JsValue,
c_int,
*const JsValue,
c_int,
) -> JsValue = core::mem::transmute(func);
f(context, this_value, this_value, argc, pointer, flags)
}
CFunctionEnum::Getter => {
let f: unsafe extern "C" fn(*mut JsContext, JsValue) -> JsValue =
core::mem::transmute(func);
f(context, this_value)
}
CFunctionEnum::GetterMagic => {
let f: unsafe extern "C" fn(*mut JsContext, JsValue, c_int) -> JsValue =
core::mem::transmute(func);
f(context, this_value, magic)
}
CFunctionEnum::Setter => {
let f: unsafe extern "C" fn(*mut JsContext, JsValue, JsValue) -> JsValue =
core::mem::transmute(func);
let value = args.first().cloned().unwrap_or(Value::Undefined);
let encoded = crate::convert::encode(&value);
let answer = f(context, this_value, encoded);
runtime::release(encoded);
answer
}
CFunctionEnum::SetterMagic => {
let f: unsafe extern "C" fn(*mut JsContext, JsValue, JsValue, c_int) -> JsValue =
core::mem::transmute(func);
let value = args.first().cloned().unwrap_or(Value::Undefined);
let encoded = crate::convert::encode(&value);
let answer = f(context, this_value, encoded, magic);
runtime::release(encoded);
answer
}
CFunctionEnum::FF | CFunctionEnum::FFF | CFunctionEnum::IteratorNext => {
return Err(engine::Signal::Throw(
interp.make_error("TypeError", "unsupported C function convention"),
));
}
}
};
for value in arguments {
unsafe { runtime::release(value) };
}
let answer_is_this = answer.has_ref_count() && answer.payload() == this_value.payload();
let result = finish_call(context, interp, answer);
if !answer_is_this {
unsafe { runtime::release(this_value) };
}
result
}
fn finish_call(
context: *mut JsContext,
interp: &mut engine::Interp<'_>,
answer: JsValue,
) -> JsResult<Value> {
if answer.is_exception() {
let pending = unsafe { context.as_ref() }.and_then(|context| context.take_pending());
return Err(match pending {
Some(value) => engine::Signal::Throw(value),
None => engine::Signal::Throw(interp.make_error(
"InternalError",
"C function returned JS_EXCEPTION without raising",
)),
});
}
let decoded = if answer.is_undefined() {
Some(Value::Undefined)
} else {
unsafe { runtime::argument(answer) }
};
unsafe { runtime::release(answer) };
Ok(decoded.unwrap_or(Value::Undefined))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SetConstructor(
ctx: *mut JsContext,
func_obj: JsValue,
proto: JsValue,
) -> c_int {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return -1;
};
let (Some(function), Some(prototype)) = (unsafe { arg(func_obj) }, unsafe { arg(proto) })
else {
runtime::throw_error(context, "TypeError", "not an object");
return -1;
};
let (Some(function), Some(prototype)) = (function.as_object(), prototype.as_object()) else {
runtime::throw_error(context, "TypeError", "not an object");
return -1;
};
function.define(
"prototype",
Property {
value: Some(Value::Object(prototype.clone())),
writable: true,
enumerable: false,
configurable: false,
..Property::default()
},
);
prototype.define(
"constructor",
Property::with_attributes(Value::Object(function.clone()), true, false),
);
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SetPropertyFunctionList(
ctx: *mut JsContext,
obj: JsValue,
tab: *const JSCFunctionListEntry,
len: c_int,
) -> c_int {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return -1;
};
let Some(object) = (unsafe { arg(obj) }).and_then(|value| value.as_object().cloned()) else {
runtime::throw_error(context, "TypeError", "not an object");
return -1;
};
if tab.is_null() || len <= 0 {
return 0;
}
context.with_current_interp(|interp| {
for index in 0..len as usize {
let entry = unsafe { &*tab.add(index) };
if let Err(signal) = install_entry(interp, &object, entry) {
let _ = runtime::result_value(context, Err(signal));
return -1;
}
}
0
})
}
fn install_entry(
interp: &mut engine::Interp<'static>,
object: &engine::Gc<engine::JsObject>,
entry: &JSCFunctionListEntry,
) -> JsResult<()> {
if entry.name.is_null() {
return Ok(());
}
let name = unsafe { std::ffi::CStr::from_ptr(entry.name) }
.to_string_lossy()
.into_owned();
let writable = entry.prop_flags & prop::WRITABLE != 0;
let enumerable = entry.prop_flags & prop::ENUMERABLE != 0;
let configurable = entry.prop_flags & prop::CONFIGURABLE != 0;
let data_property = |value: Value| Property {
value: Some(value),
writable,
enumerable,
configurable,
..Property::default()
};
let accessor_property = |getter: Option<Value>, setter: Option<Value>| Property {
getter,
setter,
writable: false,
enumerable,
configurable,
..Property::default()
};
match entry.def_type {
cdefs::JS_DEF_CFUNC => {
let func = unsafe { &entry.u.func };
let Some(kind) = CFunctionEnum::from_raw(func.cproto) else {
return Err(engine::Signal::Throw(interp.make_error(
"TypeError",
&format!("unsupported JSCFunctionEnum {}", func.cproto),
)));
};
let pointer = func.cfunc.as_ptr();
let value = make_c_function(
interp,
pointer,
kind,
name.clone(),
c_int::from(func.length),
entry.magic as c_int,
);
object.define(&name, data_property(Value::Object(value)));
}
cdefs::JS_DEF_CGETSET | cdefs::JS_DEF_CGETSET_MAGIC => {
let magic = entry.magic as c_int;
let (getter_kind, setter_kind) = if entry.def_type == cdefs::JS_DEF_CGETSET_MAGIC {
(CFunctionEnum::GetterMagic, CFunctionEnum::SetterMagic)
} else {
(CFunctionEnum::Getter, CFunctionEnum::Setter)
};
let pair = unsafe { &entry.u.getset };
let getter = wrap_accessor(interp, pair.get, getter_kind, magic, "get");
let setter = wrap_accessor(interp, pair.set, setter_kind, magic, "set");
object.define(&entry_name(entry), accessor_property(getter, setter));
}
cdefs::JS_DEF_PROP_STRING => {
let text = unsafe { entry.u.str_ };
let value = if text.is_null() {
Value::Undefined
} else {
let text = unsafe { std::ffi::CStr::from_ptr(text) };
interp.string(text.to_string_lossy().into_owned())
};
object.define(&entry_name(entry), data_property(value));
}
cdefs::JS_DEF_PROP_INT32 => {
let number = unsafe { entry.u.i32 };
object.define(&entry_name(entry), data_property(Value::Int(number)));
}
cdefs::JS_DEF_PROP_INT64 => {
let number = unsafe { entry.u.i64 };
object.define(
&entry_name(entry),
data_property(Value::Float(number as f64)),
);
}
cdefs::JS_DEF_PROP_DOUBLE => {
let number = unsafe { entry.u.f64 };
object.define(&entry_name(entry), data_property(Value::Float(number)));
}
cdefs::JS_DEF_PROP_UNDEFINED => {
object.define(&entry_name(entry), data_property(Value::Undefined));
}
cdefs::JS_DEF_PROP_BOOL => {
let flag = unsafe { entry.u.i32 } != 0;
object.define(&entry_name(entry), data_property(Value::Bool(flag)));
}
other => {
return Err(engine::Signal::Throw(interp.make_error(
"TypeError",
&format!("unsupported JS_DEF_* {other} in a function list"),
)));
}
}
Ok(())
}
fn entry_name(entry: &JSCFunctionListEntry) -> String {
if entry.name.is_null() {
return String::new();
}
unsafe { std::ffi::CStr::from_ptr(entry.name) }
.to_string_lossy()
.into_owned()
}
fn wrap_accessor(
interp: &mut engine::Interp<'static>,
function: JSCFunctionType,
kind: CFunctionEnum,
magic: c_int,
which: &str,
) -> Option<Value> {
if function.is_null() {
return None;
}
let pointer = function.as_ptr();
let body: engine::object::HostFn = Box::new(move |interp, this, args| {
let context = current_context_or_null();
call_c_function(context, interp, pointer, kind, magic, this, args)
});
Some(Value::Object(interp.new_host_function(which, 0, body)))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewClassID(pclass_id: *mut cdefs::ClassId) -> cdefs::ClassId {
let Some(slot) = (unsafe { pclass_id.as_mut() }) else {
return 0;
};
if *slot != 0 {
return *slot;
}
*slot = runtime::allocate_class_id();
*slot
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_NewClass(
rt: *mut JsRuntime,
class_id: cdefs::ClassId,
class_def: *const JSClassDef,
) -> c_int {
let (Some(runtime), Some(definition)) = (unsafe { rt.as_ref() }, unsafe { class_def.as_ref() })
else {
return -1;
};
let declaration = runtime::ClassDecl {
finalizer: definition.finalizer,
gc_mark: definition.gc_mark,
exotic: definition.exotic,
name: definition.class_name,
};
-c_int::from(!runtime.define_class(class_id, declaration))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_IsRegisteredClass(
rt: *mut JsRuntime,
class_id: cdefs::ClassId,
) -> c_int {
match unsafe { rt.as_ref() } {
Some(runtime) => c_int::from(runtime.has_class(class_id)),
None => 0,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetClassID(value: JsValue) -> cdefs::ClassId {
let Some(value) = (unsafe { arg(value) }) else {
return 0;
};
value
.as_object()
.and_then(|object| object.host_class_id())
.map_or(0, |id| id.get())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SetClassProto(
ctx: *mut JsContext,
class_id: cdefs::ClassId,
obj: JsValue,
) {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return;
};
let Some(prototype) = (unsafe { arg(obj) }).and_then(|value| value.as_object().cloned()) else {
return;
};
unsafe { runtime::release(obj) };
context.with_current_interp(|interp| {
interp.set_host_class_proto(ClassId::from_raw(class_id), prototype);
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetClassProto(
ctx: *mut JsContext,
class_id: cdefs::ClassId,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
context.with_interp(
|interp| match interp.host_class_proto(ClassId::from_raw(class_id)) {
Some(proto) => jsvalue_of(Value::Object(proto)),
None => jsvalue::UNDEFINED,
},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_SetOpaque(obj: JsValue, opaque: *mut core::ffi::c_void) {
let Some(value) = (unsafe { arg(obj) }) else {
return;
};
let Some(object) = value.as_object() else {
return;
};
object.set_host_opaque(engine::Opaque::new(opaque.cast()));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetOpaque(
obj: JsValue,
class_id: cdefs::ClassId,
) -> *mut core::ffi::c_void {
if runtime::is_finalizer_sentinel(obj) {
return runtime::finalizer_opaque(class_id)
.map(|opaque| opaque.cast())
.unwrap_or(std::ptr::null_mut());
}
let Some(value) = (unsafe { arg(obj) }) else {
return std::ptr::null_mut();
};
let Some(object) = value.as_object() else {
return std::ptr::null_mut();
};
if object.host_class_id().is_none_or(|id| id.get() != class_id) {
return std::ptr::null_mut();
}
object
.host_opaque()
.map_or(std::ptr::null_mut(), |opaque| opaque.as_ptr().cast())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetOpaque2(
ctx: *mut JsContext,
obj: JsValue,
class_id: cdefs::ClassId,
) -> *mut core::ffi::c_void {
let opaque = unsafe { JS_GetOpaque(obj, class_id) };
if !opaque.is_null() {
return opaque;
}
if let Some(context) = unsafe { ctx.as_ref() } {
let runtime = unsafe { context.runtime().as_ref() };
let name = runtime
.and_then(|runtime| runtime.class(class_id))
.map(|decl| class_name(decl.name))
.unwrap_or_else(|| format!("class {class_id}"));
runtime::throw_error(context, "TypeError", &format!("not a {name}"));
}
std::ptr::null_mut()
}
fn class_name(name: *const c_char) -> String {
if name.is_null() {
return "<anonymous>".to_string();
}
unsafe { std::ffi::CStr::from_ptr(name) }
.to_string_lossy()
.into_owned()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_GetRuntime(ctx: *mut JsContext) -> *mut JsRuntime {
match unsafe { ctx.as_ref() } {
Some(context) => context.runtime(),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ThrowRangeError(
ctx: *mut JsContext,
message: *const c_char,
) -> JsValue {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return jsvalue::EXCEPTION;
};
let message = if message.is_null() {
String::new()
} else {
unsafe { std::ffi::CStr::from_ptr(message) }
.to_string_lossy()
.into_owned()
};
runtime::throw_error(context, "RangeError", &message)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ToFloat64(ctx: *mut JsContext, pres: *mut f64, val: JsValue) -> c_int {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return -1;
};
let Some(value) = (unsafe { arg(val) }) else {
runtime::throw_error(context, "TypeError", "not a value");
return -1;
};
match context.with_current_interp(|interp| interp.to_js_number(&value)) {
Ok(number) => {
if !pres.is_null() {
unsafe { *pres = number };
}
0
}
Err(signal) => {
let _ = runtime::result_value(context, Err(signal));
-1
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_ToCStringLen2(
ctx: *mut JsContext,
plen: *mut usize,
val: JsValue,
_cesu8: c_int,
) -> *mut c_char {
let Some(context) = (unsafe { ctx.as_ref() }) else {
return std::ptr::null_mut();
};
let Some(value) = (unsafe { arg(val) }) else {
runtime::throw_error(context, "TypeError", "not a value");
return std::ptr::null_mut();
};
let text = match context.with_current_interp(|interp| interp.to_js_string(&value)) {
Ok(text) => text,
Err(signal) => {
let _ = runtime::result_value(context, Err(signal));
return std::ptr::null_mut();
}
};
let bytes = text.as_bytes();
let buffer = unsafe { crate::alloc::alloc_raw_zeroed(bytes.len() + 1) };
if buffer.is_null() {
runtime::throw_error(context, "InternalError", "out of memory");
return std::ptr::null_mut();
}
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), buffer, bytes.len());
}
if !plen.is_null() {
unsafe { *plen = bytes.len() };
}
buffer.cast()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn JS_FreeCString(_ctx: *mut JsContext, ptr: *const c_char) {
unsafe { crate::alloc::free_raw(ptr.cast_mut().cast()) }
}