[oden] Grab my incomplete QuickJS wrapper
This commit is contained in:
parent
aa70df41a3
commit
898b1fe129
114 changed files with 244181 additions and 0 deletions
58
oden-js-sys/src/lib.rs
Normal file
58
oden-js-sys/src/lib.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
include!("static-functions.rs");
|
||||
|
||||
use std::fmt;
|
||||
|
||||
impl fmt::Debug for JSValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "JS[{}", self.tag)?;
|
||||
if self.tag < 0 {
|
||||
write!(f, " {:?}]", unsafe { self.u.ptr })
|
||||
} else {
|
||||
write!(f, " {}]", unsafe { self.u.int32 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::ffi::CStr;
|
||||
|
||||
// Small sanity test that starts the runtime and evaluates code.
|
||||
#[test]
|
||||
fn test_eval() {
|
||||
unsafe {
|
||||
let rt = JS_NewRuntime();
|
||||
let ctx = JS_NewContext(rt);
|
||||
|
||||
let code_str = "1 + 1\0";
|
||||
let code = CStr::from_bytes_with_nul(code_str.as_bytes()).unwrap();
|
||||
let script = CStr::from_bytes_with_nul("script\0".as_bytes()).unwrap();
|
||||
|
||||
let value = JS_Eval(
|
||||
ctx,
|
||||
code.as_ptr(),
|
||||
(code_str.len() - 1).try_into().unwrap(),
|
||||
script.as_ptr(),
|
||||
JS_EVAL_TYPE_GLOBAL as i32,
|
||||
);
|
||||
assert_eq!(value.tag, 0);
|
||||
assert_eq!(value.u.int32, 2);
|
||||
|
||||
JS_DupValue(ctx, value);
|
||||
JS_FreeValue(ctx, value);
|
||||
|
||||
let ival = JS_NewInt32(ctx, 12);
|
||||
assert_eq!(ival.tag, 0);
|
||||
let fval = JS_NewFloat64(ctx, f64::MAX);
|
||||
assert_eq!(fval.tag, 7);
|
||||
let bval = JS_NewBool(ctx, true);
|
||||
assert_eq!(bval.tag, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
219
oden-js-sys/src/static-functions.rs
Normal file
219
oden-js-sys/src/static-functions.rs
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
// This code originally from the quick-js crate.
|
||||
// https://github.com/theduke/quickjs-rs/blob/master/libquickjs-sys/src/static-functions.rs
|
||||
// Copyright (c) 2019 Christoph Herzog <christoph.herzog@theduke.at>
|
||||
|
||||
extern "C" {
|
||||
fn JS_ValueGetTag_real(v: JSValue) -> i32;
|
||||
fn JS_DupValue_real(ctx: *mut JSContext, v: JSValue);
|
||||
fn JS_DupValueRT_real(rt: *mut JSRuntime, v: JSValue);
|
||||
fn JS_FreeValue_real(ctx: *mut JSContext, v: JSValue);
|
||||
fn JS_FreeValueRT_real(rt: *mut JSRuntime, v: JSValue);
|
||||
fn JS_NewBool_real(ctx: *mut JSContext, v: bool) -> JSValue;
|
||||
fn JS_NewInt32_real(ctx: *mut JSContext, v: i32) -> JSValue;
|
||||
fn JS_NewUint32_real(ctx: *mut JSContext, v: u32) -> JSValue;
|
||||
fn JS_NewFloat64_real(ctx: *mut JSContext, v: f64) -> JSValue;
|
||||
fn JS_VALUE_IS_NAN_real(v: JSValue) -> bool;
|
||||
fn JS_VALUE_GET_FLOAT64_real(v: JSValue) -> f64;
|
||||
fn JS_VALUE_GET_NORM_TAG_real(v: JSValue) -> ::std::os::raw::c_int;
|
||||
fn JS_IsNumber_real(v: JSValue) -> bool;
|
||||
fn JS_IsBigInt_real(ctx: *mut JSContext, v: JSValue) -> bool;
|
||||
fn JS_IsBigFloat_real(v: JSValue) -> bool;
|
||||
fn JS_IsBigDecimal_real(v: JSValue) -> bool;
|
||||
fn JS_IsBool_real(v: JSValue) -> bool;
|
||||
fn JS_IsNull_real(v: JSValue) -> bool;
|
||||
fn JS_IsUndefined_real(v: JSValue) -> bool;
|
||||
fn JS_IsException_real(v: JSValue) -> bool;
|
||||
fn JS_IsUninitialized_real(v: JSValue) -> bool;
|
||||
fn JS_IsString_real(v: JSValue) -> bool;
|
||||
fn JS_IsSymbol_real(v: JSValue) -> bool;
|
||||
fn JS_IsObject_real(v: JSValue) -> bool;
|
||||
fn JS_ToUint32_real(ctx: *mut JSContext, pres: *mut u32, val: JSValue) -> i32;
|
||||
fn JS_GetProperty_real(ctx: *mut JSContext, this_obj: JSValue, prop: JSAtom) -> JSValue;
|
||||
fn JS_SetProperty_real(
|
||||
ctx: *mut JSContext,
|
||||
this_obj: JSValue,
|
||||
prop: JSAtom,
|
||||
val: JSValue,
|
||||
) -> ::std::os::raw::c_int;
|
||||
fn JS_NewCFunction_real(
|
||||
ctx: *mut JSContext,
|
||||
func: *mut JSCFunction,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
length: ::std::os::raw::c_int,
|
||||
) -> JSValue;
|
||||
fn JS_NewCFunctionMagic_real(
|
||||
ctx: *mut JSContext,
|
||||
func: *mut JSCFunctionMagic,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
length: ::std::os::raw::c_int,
|
||||
cproto: JSCFunctionEnum,
|
||||
magic: ::std::os::raw::c_int,
|
||||
) -> JSValue;
|
||||
fn JS_MakeException_real() -> JSValue;
|
||||
|
||||
}
|
||||
|
||||
pub unsafe fn JS_ValueGetTag(v: JSValue) -> i32 {
|
||||
JS_ValueGetTag_real(v)
|
||||
}
|
||||
|
||||
/// Increment the refcount of this value
|
||||
pub unsafe fn JS_DupValue(ctx: *mut JSContext, v: JSValue) {
|
||||
JS_DupValue_real(ctx, v);
|
||||
}
|
||||
|
||||
/// Increment the refcount of this value
|
||||
pub unsafe fn JS_DupValueRT(rt: *mut JSRuntime, v: JSValue) {
|
||||
JS_DupValueRT_real(rt, v);
|
||||
}
|
||||
|
||||
/// Decrement the refcount of this value
|
||||
pub unsafe fn JS_FreeValue(ctx: *mut JSContext, v: JSValue) {
|
||||
JS_FreeValue_real(ctx, v);
|
||||
}
|
||||
|
||||
/// Decrement the refcount of this value
|
||||
pub unsafe fn JS_FreeValueRT(rt: *mut JSRuntime, v: JSValue) {
|
||||
JS_FreeValueRT_real(rt, v);
|
||||
}
|
||||
|
||||
/// create a new boolean value
|
||||
pub unsafe fn JS_NewBool(ctx: *mut JSContext, v: bool) -> JSValue {
|
||||
JS_NewBool_real(ctx, v)
|
||||
}
|
||||
|
||||
/// create a new int32 value
|
||||
pub unsafe fn JS_NewInt32(ctx: *mut JSContext, v: i32) -> JSValue {
|
||||
JS_NewInt32_real(ctx, v)
|
||||
}
|
||||
|
||||
/// create a new int32 value
|
||||
pub unsafe fn JS_NewUint32(ctx: *mut JSContext, v: u32) -> JSValue {
|
||||
JS_NewUint32_real(ctx, v)
|
||||
}
|
||||
|
||||
/// create a new f64 value, please note that if the passed f64 fits in a i32 this will return a value with flag 0 (i32)
|
||||
pub unsafe fn JS_NewFloat64(ctx: *mut JSContext, v: f64) -> JSValue {
|
||||
JS_NewFloat64_real(ctx, v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a NaN value
|
||||
pub unsafe fn JS_VALUE_IS_NAN(v: JSValue) -> bool {
|
||||
JS_VALUE_IS_NAN_real(v)
|
||||
}
|
||||
|
||||
/// get a f64 value from a JSValue
|
||||
pub unsafe fn JS_VALUE_GET_FLOAT64(v: JSValue) -> f64 {
|
||||
JS_VALUE_GET_FLOAT64_real(v)
|
||||
}
|
||||
|
||||
/// same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing
|
||||
pub unsafe fn JS_VALUE_GET_NORM_TAG(v: JSValue) -> ::std::os::raw::c_int {
|
||||
JS_VALUE_GET_NORM_TAG_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a Number
|
||||
pub unsafe fn JS_IsNumber(v: JSValue) -> bool {
|
||||
JS_IsNumber_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a BigInt
|
||||
pub unsafe fn JS_IsBigInt(ctx: *mut JSContext, v: JSValue) -> bool {
|
||||
JS_IsBigInt_real(ctx, v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a BigFloat
|
||||
pub unsafe fn JS_IsBigFloat(v: JSValue) -> bool {
|
||||
JS_IsBigFloat_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a BigDecimal
|
||||
pub unsafe fn JS_IsBigDecimal(v: JSValue) -> bool {
|
||||
JS_IsBigDecimal_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a Boolean
|
||||
pub unsafe fn JS_IsBool(v: JSValue) -> bool {
|
||||
JS_IsBool_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is null
|
||||
pub unsafe fn JS_IsNull(v: JSValue) -> bool {
|
||||
JS_IsNull_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is Undefined
|
||||
pub unsafe fn JS_IsUndefined(v: JSValue) -> bool {
|
||||
JS_IsUndefined_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is an Exception
|
||||
pub unsafe fn JS_IsException(v: JSValue) -> bool {
|
||||
JS_IsException_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is initialized
|
||||
pub unsafe fn JS_IsUninitialized(v: JSValue) -> bool {
|
||||
JS_IsUninitialized_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a String
|
||||
pub unsafe fn JS_IsString(v: JSValue) -> bool {
|
||||
JS_IsString_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is a Symbol
|
||||
pub unsafe fn JS_IsSymbol(v: JSValue) -> bool {
|
||||
JS_IsSymbol_real(v)
|
||||
}
|
||||
|
||||
/// check if a JSValue is an Object
|
||||
pub unsafe fn JS_IsObject(v: JSValue) -> bool {
|
||||
JS_IsObject_real(v)
|
||||
}
|
||||
|
||||
/// get a u32 value from a JSValue
|
||||
pub unsafe fn JS_ToUint32(ctx: *mut JSContext, pres: *mut u32, val: JSValue) -> i32 {
|
||||
JS_ToUint32_real(ctx, pres, val)
|
||||
}
|
||||
|
||||
/// get a property of an object identified by a JSAtom
|
||||
pub unsafe fn JS_GetProperty(ctx: *mut JSContext, this_obj: JSValue, prop: JSAtom) -> JSValue {
|
||||
JS_GetProperty_real(ctx, this_obj, prop)
|
||||
}
|
||||
|
||||
/// set a property of an object identified by a JSAtom
|
||||
pub unsafe fn JS_SetProperty(
|
||||
ctx: *mut JSContext,
|
||||
this_obj: JSValue,
|
||||
prop: JSAtom,
|
||||
val: JSValue,
|
||||
) -> ::std::os::raw::c_int {
|
||||
JS_SetProperty_real(ctx, this_obj, prop, val)
|
||||
}
|
||||
|
||||
/// create a new Function based on a JSCFunction
|
||||
pub unsafe fn JS_NewCFunction(
|
||||
ctx: *mut JSContext,
|
||||
func: *mut JSCFunction,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
length: ::std::os::raw::c_int,
|
||||
) -> JSValue {
|
||||
JS_NewCFunction_real(ctx, func, name, length)
|
||||
}
|
||||
|
||||
/// create a new Function based on a JSCFunction
|
||||
pub unsafe fn JS_NewCFunctionMagic(
|
||||
ctx: *mut JSContext,
|
||||
func: *mut JSCFunctionMagic,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
length: ::std::os::raw::c_int,
|
||||
cproto: JSCFunctionEnum,
|
||||
magic: ::std::os::raw::c_int,
|
||||
) -> JSValue {
|
||||
JS_NewCFunctionMagic_real(ctx, func, name, length, cproto, magic)
|
||||
}
|
||||
|
||||
pub unsafe fn JS_MakeException() -> JSValue {
|
||||
JS_MakeException_real()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue