Skip to content

Commit

Permalink
Safer wasm references
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 7, 2024
1 parent b6305b9 commit 33ab3b8
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion js-api/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lol_html::html_content::Comment as NativeComment;
#[wasm_bindgen]
pub struct Comment(NativeRefWrap<NativeComment<'static>>);

impl_from_native!(NativeComment --> Comment);
impl_from_native!(NativeComment => Comment);
impl_mutations!(Comment);

#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion js-api/src/doctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lol_html::html_content::Doctype as NativeDoctype;
#[wasm_bindgen]
pub struct Doctype(NativeRefWrap<NativeDoctype<'static>>);

impl_from_native!(NativeDoctype --> Doctype);
impl_from_native!(NativeDoctype => Doctype);

#[wasm_bindgen]
impl Doctype {
Expand Down
2 changes: 1 addition & 1 deletion js-api/src/document_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lol_html::html_content::DocumentEnd as NativeDocumentEnd;
#[wasm_bindgen]
pub struct DocumentEnd(NativeRefWrap<NativeDocumentEnd<'static>>);

impl_from_native!(NativeDocumentEnd --> DocumentEnd);
impl_from_native!(NativeDocumentEnd => DocumentEnd);

#[wasm_bindgen]
impl DocumentEnd {
Expand Down
2 changes: 1 addition & 1 deletion js-api/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl From<&NativeAttribute<'_>> for Attribute {
#[wasm_bindgen]
pub struct Element(NativeRefWrap<NativeElement<'static, 'static>>);

impl_from_native!(NativeElement --> Element);
impl_from_native!(NativeElement => Element);
impl_mutations!(Element);

#[wasm_bindgen]
Expand Down
14 changes: 4 additions & 10 deletions js-api/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use lol_html::{
DocumentContentHandlers as NativeDocumentContentHandlers,
ElementContentHandlers as NativeElementContentHandlers,
};
use std::mem;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -26,16 +25,11 @@ macro_rules! make_handler {
h
}
type_hint(Box::new(move |arg: &mut _| {
let (js_arg, anchor) = unsafe { $JsArgType::from_native(arg) };
$JsArgType::with_native(arg, |js_value| {
$handler.call1(&JsValue::NULL, &js_value)
}).map_err(|e| HandlerJsErrorWrap(e))?;

let res = match $handler.call1(&JsValue::NULL, &JsValue::from(js_arg)) {
Ok(_) => Ok(()),
Err(e) => Err(HandlerJsErrorWrap(e).into()),
};

mem::drop(anchor);

res
Ok(())
}))
}};
}
Expand Down
8 changes: 4 additions & 4 deletions js-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ macro_rules! impl_mutations {
}

macro_rules! impl_from_native {
($Ty:ident --> $JsTy:path) => {
($Ty:ty => $JsTy:path) => {
impl $JsTy {
pub(crate) unsafe fn from_native<'r>(inner: &'r mut $Ty) -> (Self, Anchor<'r>) {
let (ref_wrap, anchor) = unsafe { NativeRefWrap::wrap(inner) };
pub(crate) fn with_native<'r, R>(inner: &'r mut $Ty, callback: impl FnOnce(&JsValue) -> R) -> R {
let (ref_wrap, _anchor) = unsafe { NativeRefWrap::wrap(inner) };

($JsTy(ref_wrap), anchor)
(callback)(&JsValue::from($JsTy(ref_wrap)))
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion js-api/src/text_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lol_html::html_content::TextChunk as NativeTextChunk;
#[wasm_bindgen]
pub struct TextChunk(NativeRefWrap<NativeTextChunk<'static>>);

impl_from_native!(NativeTextChunk --> TextChunk);
impl_from_native!(NativeTextChunk => TextChunk);
impl_mutations!(TextChunk);

#[wasm_bindgen]
Expand Down

0 comments on commit 33ab3b8

Please sign in to comment.