Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is_typed_array #415

Merged
merged 18 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
# https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
strategy:
matrix:
msrv: [1.65.0] # 1.65.0 introduced GAT stabilization
msrv: [1.81.0] # 'home' crate requires 1.81.0
name: ubuntu / ${{ matrix.msrv }}
steps:
- uses: actions/checkout@v4
Expand All @@ -112,7 +112,7 @@ jobs:
with:
toolchain: ${{ matrix.msrv }}
- name: cargo +${{ matrix.msrv }} check
run: cargo check
run: cargo +${{ matrix.msrv }} check --features full-async,bindgen

coverage:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target
.ccls-cache
Cargo.lock
richarddd marked this conversation as resolved.
Show resolved Hide resolved
.gdb_history
Cargo.lock
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "rquickjs"
version = "0.8.1"
authors = ["Mees Delzenne <[email protected]>", "K. <[email protected]>"]
edition = "2021"
rust-version = "1.65"
rust-version = "1.81"
license = "MIT"
readme = "README.md"
description = "High level bindings to the QuickJS JavaScript engine"
Expand Down
2 changes: 1 addition & 1 deletion core/src/runtime/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl RawRuntime {
}

pub fn is_job_pending(&self) -> bool {
0 != unsafe { qjs::JS_IsJobPending(self.rt.as_ptr()) }
(unsafe { qjs::JS_IsJobPending(self.rt.as_ptr()) } as i32) != 0
}

pub fn execute_pending_job(&mut self) -> StdResult<bool, *mut qjs::JSContext> {
Expand Down
12 changes: 6 additions & 6 deletions core/src/runtime/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::{

use crate::JsLifetime;

unsafe fn to_static<'js, T: JsLifetime<'js>>(this: T) -> T::Changed<'static>
unsafe fn to_static<'js, T>(this: T) -> T::Changed<'static>
where
T: Sized,
T: JsLifetime<'js> + Sized,
{
assert_eq!(
std::mem::size_of::<T>(),
Expand Down Expand Up @@ -40,9 +40,9 @@ where
)
}

unsafe fn from_static_box<'js, T: JsLifetime<'js>>(this: Box<T::Changed<'static>>) -> Box<T>
unsafe fn from_static_box<'js, T>(this: Box<T::Changed<'static>>) -> Box<T>
where
T: Sized,
T: JsLifetime<'js> + Sized,
{
assert_eq!(
std::mem::size_of::<T>(),
Expand All @@ -58,9 +58,9 @@ where
Box::from_raw(Box::into_raw(this) as *mut T)
}

unsafe fn from_static_ref<'a, 'js, T: JsLifetime<'js>>(this: &'a T::Changed<'static>) -> &'a T
unsafe fn from_static_ref<'a, 'js, T>(this: &'a T::Changed<'static>) -> &'a T
where
T: Sized,
T: JsLifetime<'js> + Sized,
{
std::mem::transmute(this)
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ impl<'js> Value<'js> {
/// Check if the value is a function
#[inline]
pub fn is_function(&self) -> bool {
0 != unsafe { qjs::JS_IsFunction(self.ctx.as_ptr(), self.value) }
(unsafe { qjs::JS_IsFunction(self.ctx.as_ptr(), self.value) } as i32) != 0
}

/// Check if the value is a constructor function
#[inline]
pub fn is_constructor(&self) -> bool {
0 != unsafe { qjs::JS_IsConstructor(self.ctx.as_ptr(), self.value) }
(unsafe { qjs::JS_IsConstructor(self.ctx.as_ptr(), self.value) } as i32) != 0
}

/// Check if the value is a promise.
Expand All @@ -376,7 +376,7 @@ impl<'js> Value<'js> {
/// Check if the value is an error
#[inline]
pub fn is_error(&self) -> bool {
0 != unsafe { qjs::JS_IsError(self.ctx.as_ptr(), self.value) }
(unsafe { qjs::JS_IsError(self.ctx.as_ptr(), self.value) } as i32) != 0
}

/// Reference as value
Expand Down
2 changes: 1 addition & 1 deletion core/src/value/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'js> ArrayBuffer<'js> {
size as _,
Some(drop_raw::<T>),
capacity as _,
0,
false,
);
ctx.handle_exception(val).inspect_err(|_| {
// don't forget to free data when error occurred
Expand Down
5 changes: 2 additions & 3 deletions core/src/value/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ impl<'js> Function<'js> {

/// Returns whether this function is an constructor.
pub fn is_constructor(&self) -> bool {
let res = unsafe { qjs::JS_IsConstructor(self.ctx().as_ptr(), self.0.as_js_value()) };
res != 0
((unsafe { qjs::JS_IsConstructor(self.ctx().as_ptr(), self.0.as_js_value()) }) as i32) != 0
}

/// Set whether this function is a constructor or not.
Expand All @@ -171,7 +170,7 @@ impl<'js> Function<'js> {
qjs::JS_SetConstructorBit(
self.ctx().as_ptr(),
self.0.as_js_value(),
is_constructor as i32,
is_constructor.into(),
)
};
}
Expand Down
27 changes: 26 additions & 1 deletion core/src/value/typed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ impl<'js, T> IntoJs<'js> for TypedArray<'js, T> {
impl<'js> Object<'js> {
pub fn is_typed_array<T: TypedArrayItem>(&self) -> bool {
let array_type = unsafe { qjs::JS_GetTypedArrayType(self.value) };
T::ARRAY_TYPE == array_type.try_into().unwrap()
if array_type < 0 {
return false;
}
if let Ok(array_type) = TryInto::<qjs::JSTypedArrayEnum>::try_into(array_type) {
return array_type == T::ARRAY_TYPE;
}
return false;
}

/// Interpret as [`TypedArray`]
Expand Down Expand Up @@ -436,4 +442,23 @@ mod test {
assert_eq!(val.as_bytes().unwrap(), &res)
});
}

#[test]
fn is_typed_array() {
test_with(|ctx| {
let val: Value = ctx
.eval(
r#"
new Uint32Array([0xCAFEDEAD, 0xFEEDBEAD])
"#,
)
.unwrap();
let obj = val.into_object().unwrap();
assert!(obj.is_typed_array::<u32>());
assert!(!obj.is_typed_array::<i32>());

let obj = Object::new(ctx).unwrap();
assert!(!obj.is_typed_array::<u8>());
});
}
}
Loading
Loading