@@ -10,6 +10,7 @@ use lol_html::{
10
10
ElementContentHandlers as NativeElementContentHandlers ,
11
11
} ;
12
12
use std:: mem;
13
+ use std:: rc:: Rc ;
13
14
use thiserror:: Error ;
14
15
use wasm_bindgen:: JsCast ;
15
16
@@ -29,13 +30,12 @@ extern "C" {
29
30
}
30
31
31
32
macro_rules! make_handler {
32
- ( $handler: ident, $JsArgType: ident, $stack_ptr: ident) => {
33
+ ( $handler: ident, $JsArgType: ident, $this : ident , $ stack_ptr: ident) => {
33
34
move |arg: & mut _| {
34
35
let ( js_arg, anchor) = $JsArgType:: from_native( arg) ;
35
- let this = JsValue :: NULL ;
36
36
let js_arg = JsValue :: from( js_arg) ;
37
37
38
- let res = match $handler. call1( & this, & js_arg) {
38
+ let res = match $handler. call1( & $ this, & js_arg) {
39
39
Ok ( res) => {
40
40
if let Some ( promise) = res. dyn_ref:: <JsPromise >( ) {
41
41
await_promise( $stack_ptr, promise) ;
@@ -72,18 +72,22 @@ extern "C" {
72
72
73
73
impl IntoNativeHandlers < NativeElementContentHandlers < ' static > > for ElementContentHandlers {
74
74
fn into_native ( self , stack_ptr : * mut u8 ) -> NativeElementContentHandlers < ' static > {
75
+ let handlers: Rc < JsValue > = Rc :: new ( ( & self ) . into ( ) ) ;
75
76
let mut native = NativeElementContentHandlers :: default ( ) ;
76
77
77
78
if let Some ( handler) = self . element ( ) {
78
- native = native. element ( make_handler ! ( handler, Element , stack_ptr) ) ;
79
+ let this = handlers. clone ( ) ;
80
+ native = native. element ( make_handler ! ( handler, Element , this, stack_ptr) ) ;
79
81
}
80
82
81
83
if let Some ( handler) = self . comments ( ) {
82
- native = native. comments ( make_handler ! ( handler, Comment , stack_ptr) ) ;
84
+ let this = handlers. clone ( ) ;
85
+ native = native. comments ( make_handler ! ( handler, Comment , this, stack_ptr) ) ;
83
86
}
84
87
85
88
if let Some ( handler) = self . text ( ) {
86
- native = native. text ( make_handler ! ( handler, TextChunk , stack_ptr) ) ;
89
+ let this = handlers. clone ( ) ;
90
+ native = native. text ( make_handler ! ( handler, TextChunk , this, stack_ptr) ) ;
87
91
}
88
92
89
93
native
@@ -109,22 +113,27 @@ extern "C" {
109
113
110
114
impl IntoNativeHandlers < NativeDocumentContentHandlers < ' static > > for DocumentContentHandlers {
111
115
fn into_native ( self , stack_ptr : * mut u8 ) -> NativeDocumentContentHandlers < ' static > {
116
+ let handlers: Rc < JsValue > = Rc :: new ( ( & self ) . into ( ) ) ;
112
117
let mut native = NativeDocumentContentHandlers :: default ( ) ;
113
118
114
119
if let Some ( handler) = self . doctype ( ) {
115
- native = native. doctype ( make_handler ! ( handler, Doctype , stack_ptr) ) ;
120
+ let this = handlers. clone ( ) ;
121
+ native = native. doctype ( make_handler ! ( handler, Doctype , this, stack_ptr) ) ;
116
122
}
117
123
118
124
if let Some ( handler) = self . comments ( ) {
119
- native = native. comments ( make_handler ! ( handler, Comment , stack_ptr) ) ;
125
+ let this = handlers. clone ( ) ;
126
+ native = native. comments ( make_handler ! ( handler, Comment , this, stack_ptr) ) ;
120
127
}
121
128
122
129
if let Some ( handler) = self . text ( ) {
123
- native = native. text ( make_handler ! ( handler, TextChunk , stack_ptr) ) ;
130
+ let this = handlers. clone ( ) ;
131
+ native = native. text ( make_handler ! ( handler, TextChunk , this, stack_ptr) ) ;
124
132
}
125
133
126
134
if let Some ( handler) = self . end ( ) {
127
- native = native. end ( make_handler ! ( handler, DocumentEnd , stack_ptr) ) ;
135
+ let this = handlers. clone ( ) ;
136
+ native = native. end ( make_handler ! ( handler, DocumentEnd , this, stack_ptr) ) ;
128
137
}
129
138
130
139
native
0 commit comments