[oden-js] Fix cross-platform conversions

Lots of places were assuming that bool and c int were interchangeable,
but of course on windows a c int is 32 bits and that confused
everybody. Tighten up the definitions in static-functions, which
causes us to fix boolean conversion. Also add tests for boolean
conversion, and also add better debug formatting of JS values.
This commit is contained in:
John Doty 2023-08-19 07:38:15 -07:00
parent 0cb5944d0a
commit 6200ed31b6
5 changed files with 82 additions and 31 deletions

View file

@ -182,7 +182,8 @@ impl ContextRef {
where
T: Into<bool>,
{
self.check_exception(unsafe { sys::JS_NewBool(self.ctx, value.into()) })
let b = if value.into() { 1 } else { 0 };
self.check_exception(unsafe { sys::JS_NewBool(self.ctx, b) })
}
/// Construct a new value that wraps a strongly-typed closure.

View file

@ -77,7 +77,9 @@ impl TryIntoValue for f64 {
impl TryIntoValue for bool {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
ctx.new_bool(self)
let result = ctx.new_bool(self);
// eprintln!("{self} -> {result:?}");
result
}
}

View file

@ -6,3 +6,26 @@ pub use from::*;
mod function;
pub use function::*;
#[cfg(test)]
mod tests {
use super::*;
use crate::{Context, ContextRef, Runtime};
fn check_round_trip<T>(ctx: &ContextRef, v: T)
where
T: TryIntoValue + TryFromValue + PartialEq + Clone + std::fmt::Debug,
{
let val = v.clone().try_into_value(ctx).expect("Could not make value");
let new = T::try_from_value(&val, ctx).expect("Could not unwrap value");
assert_eq!(v, new);
}
#[test]
fn round_trip() {
let ctx = Context::new(Runtime::new());
check_round_trip(&ctx, true);
check_round_trip(&ctx, false);
check_round_trip(&ctx, 12);
}
}