#![doc = "Raw FFI bindings to JavaScriptCore"]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
#[cfg(jsc_source)]
mod jsc_init {
use std::sync::Once;
unsafe extern "C" {
fn rong_jsc_initialize();
}
static INIT: Once = Once::new();
pub fn ensure_initialized() {
INIT.call_once(|| unsafe { rong_jsc_initialize() });
}
}
#[cfg(jsc_source)]
pub use jsc_init::ensure_initialized;
#[cfg(jsc_source)]
pub mod bytecode_bridge {
use std::ffi::{c_char, c_int};
#[repr(C)]
pub struct RongJSCBytecodeResult {
pub data: *mut u8,
pub size: usize,
pub error: *const c_char,
}
#[repr(C)]
pub struct RongJSCRunBytecodeResult {
pub value: crate::JSValueRef,
pub is_exception: c_int,
pub error: *const c_char,
}
unsafe extern "C" {
pub fn rong_jsc_bytecode_supported() -> c_int;
pub fn rong_jsc_compile_to_bytecode(
ctx: *mut crate::OpaqueJSContext,
source: *const c_char,
source_len: usize,
source_url: *const c_char,
) -> RongJSCBytecodeResult;
pub fn rong_jsc_free_bytecode(data: *mut u8);
pub fn rong_jsc_free_error(error: *const c_char);
pub fn rong_jsc_run_bytecode(
ctx: *mut crate::OpaqueJSContext,
bytes: *const u8,
size: usize,
) -> RongJSCRunBytecodeResult;
}
}
#[cfg(test)]
mod test {
use super::*;
use std::ffi::CString;
use std::ptr;
#[test]
fn test_jscore_raw_binding() {
unsafe {
let global_context = JSGlobalContextCreate(ptr::null_mut());
let js_code = CString::new("Math.sqrt(16)").expect("CString::new failed");
let js_string = JSStringCreateWithUTF8CString(js_code.as_ptr());
let mut exception: JSValueRef = ptr::null_mut();
let result = JSEvaluateScript(
global_context,
js_string,
ptr::null_mut(), ptr::null_mut(), 1, &mut exception,
);
if !exception.is_null() {
let exception_string =
JSValueToStringCopy(global_context, exception, ptr::null_mut());
let exception_cstring = JSStringGetCharactersPtr(exception_string);
println!("JavaScript exception occurred: {:?}", exception_cstring);
JSStringRelease(exception_string);
} else {
let result_number = JSValueToNumber(global_context, result, ptr::null_mut());
assert_eq!(result_number, 4.0);
}
JSStringRelease(js_string);
JSGlobalContextRelease(global_context);
}
}
}