@@ -12,16 +12,13 @@ use std::collections::{HashMap, VecDeque};
1212use std:: future:: Future ;
1313use std:: pin:: Pin ;
1414use std:: sync:: atomic:: AtomicUsize ;
15- use std:: time:: Duration ;
16- use wasi:: clocks:: monotonic_clock:: subscribe_duration;
17- use wasi_async_runtime:: { Reactor , block_on} ;
15+ use wstd:: runtime:: block_on;
1816
1917pub const RESOURCE_TABLE_NAME : & str = "__wasm_rquickjs_resources" ;
2018pub const RESOURCE_ID_KEY : & str = "__wasm_rquickjs_resource_id" ;
2119pub const DISPOSE_SYMBOL : & str = "__wasm_rquickjs_symbol_dispose" ;
2220
2321pub struct JsState {
24- pub reactor : RefCell < Option < Reactor > > ,
2522 pub rt : AsyncRuntime ,
2623 pub ctx : AsyncContext ,
2724 pub last_resource_id : AtomicUsize ,
@@ -30,7 +27,7 @@ pub struct JsState {
3027 pub abort_handles : RefCell < HashMap < usize , AbortHandle > > ,
3128 pub last_abort_id : AtomicUsize ,
3229 next_tick_queue :
33- RefCell < VecDeque < Box < dyn FnOnce ( ) -> Pin < Box < dyn Future < Output = ( ) > + ' static > > > > > ,
30+ RefCell < VecDeque < Box < dyn FnOnce ( ) -> Pin < Box < dyn Future < Output = ( ) > + ' static > > > > > ,
3431}
3532
3633impl Default for JsState {
@@ -41,7 +38,7 @@ impl Default for JsState {
4138
4239impl JsState {
4340 pub fn new ( ) -> Self {
44- block_on ( |_reactor| async {
41+ block_on ( async {
4542 let rt = AsyncRuntime :: new ( ) . expect ( "Failed to create AsyncRuntime" ) ;
4643 let ctx = AsyncContext :: full ( & rt)
4744 . await
@@ -126,7 +123,6 @@ impl JsState {
126123
127124 let last_resource_id = AtomicUsize :: new ( 1 ) ;
128125 Self {
129- reactor : RefCell :: new ( None ) ,
130126 rt,
131127 ctx,
132128 last_resource_id,
@@ -141,31 +137,35 @@ impl JsState {
141137
142138 pub fn add_next_tick_callback (
143139 & self ,
144- callback : Box < dyn FnOnce ( ) -> Pin < Box < dyn Future < Output = ( ) > + ' static > > > ,
140+ callback : Box < dyn FnOnce ( ) -> Pin < Box < dyn Future < Output = ( ) > + ' static > > > ,
145141 ) {
146142 self . next_tick_queue . borrow_mut ( ) . push_back ( callback) ;
147143 }
148144
149145 fn pop_next_tick_callback (
150146 & self ,
151- ) -> Option < Box < dyn FnOnce ( ) -> Pin < Box < dyn Future < Output = ( ) > + ' static > > > > {
147+ ) -> Option < Box < dyn FnOnce ( ) -> Pin < Box < dyn Future < Output = ( ) > + ' static > > > > {
152148 let result = self . next_tick_queue . borrow_mut ( ) . pop_front ( ) ;
153149 result
154150 }
155151
156152 pub async fn idle ( & self ) {
157- let mut n = 0 ;
153+ let mut n;
158154
159155 loop {
160156 n = 0 ;
161157
162158 while let Some ( f) = self . pop_next_tick_callback ( ) {
159+ println ! ( "** CALLING NEXT_TICK CALLBACK" ) ;
163160 f ( ) . await ;
164161 n += 1 ;
165162 }
166163
164+ println ! ( "** CALLING RT.IDLE" ) ;
167165 self . rt . idle ( ) . await ;
168166
167+ println ! ( "** IDLE DONE {n}" ) ;
168+
169169 if n == 0 {
170170 break ;
171171 }
@@ -188,10 +188,10 @@ pub fn get_js_state() -> &'static JsState {
188188pub fn async_exported_function < F : Future > ( future : F ) -> F :: Output {
189189 let js_state = get_js_state ( ) ;
190190
191- block_on ( |reactor| async move {
191+ block_on ( async move {
192192 use futures:: StreamExt ;
193193
194- js_state . reactor . replace ( Some ( reactor ) ) ;
194+ println ! ( "async_exported_function start" ) ;
195195 if let Some ( mut resource_drop_queue_rx) = js_state. resource_drop_queue_rx . take ( ) {
196196 let resource_dropper = async move {
197197 while let Some ( resource_id) = resource_drop_queue_rx. next ( ) . await {
@@ -214,6 +214,8 @@ pub fn async_exported_function<F: Future>(future: F) -> F::Output {
214214 . resource_drop_queue_rx
215215 . replace ( Some ( resource_drop_queue_rx) ) ;
216216
217+ println ! ( "async_exported_function end" ) ;
218+
217219 result
218220 } else {
219221 // This case will never happen because block_on does not allow reentry
@@ -251,7 +253,7 @@ where
251253 . map ( |e| crate :: wrappers:: JsResult ( Err ( e) ) )
252254 } ,
253255 )
254- . await
256+ . await
255257}
256258
257259async fn call_js_export_internal < A , R , FR , TME > (
@@ -303,6 +305,8 @@ where
303305 if value. is_promise( ) {
304306 let promise: Promise = value. into_promise( ) . unwrap( ) ;
305307 let promise_future = promise. into_future:: <R > ( ) ;
308+ println!( "promise future await start" ) ;
309+
306310 match promise_future. await {
307311 Ok ( result) => {
308312 map_result( result)
@@ -332,7 +336,9 @@ where
332336 }
333337 }
334338 } ) . await ;
339+ println ! ( "async_with finished, before idle" ) ;
335340 js_state. idle ( ) . await ;
341+ println ! ( "after idle" ) ;
336342 result
337343}
338344
@@ -416,7 +422,7 @@ where
416422 |a| a,
417423 |_, _| None ,
418424 )
419- . await
425+ . await
420426}
421427
422428pub async fn call_js_resource_method_returning_result < A , R , E > (
@@ -444,7 +450,7 @@ where
444450 . map ( |e| crate :: wrappers:: JsResult ( Err ( e) ) )
445451 } ,
446452 )
447- . await
453+ . await
448454}
449455
450456async fn call_js_resource_method_internal < A , R , FR , TME > (
@@ -552,14 +558,6 @@ pub fn enqueue_drop_js_resource(resource_id: usize) {
552558 . expect ( "Failed to enqueue resource drop" ) ;
553559}
554560
555- pub async fn sleep ( duration : Duration ) {
556- let js_state = get_js_state ( ) ;
557- let reactor = js_state. reactor . borrow ( ) . clone ( ) . unwrap ( ) ;
558-
559- let pollable = subscribe_duration ( duration. as_nanos ( ) as u64 ) ;
560- reactor. wait_for ( pollable) . await ;
561- }
562-
563561async fn drop_js_resource ( resource_id : usize ) {
564562 let js_state = get_js_state ( ) ;
565563
@@ -570,7 +568,7 @@ async fn drop_js_resource(resource_id: usize) {
570568 panic!( "Failed to delete resource {resource_id}: {e:?}" ) ;
571569 }
572570 } )
573- . await ;
571+ . await ;
574572 js_state. idle ( ) . await ;
575573}
576574
@@ -700,9 +698,9 @@ pub fn format_js_exception(exc: &Value) -> String {
700698 . unwrap_or_else ( || {
701699 let formatted_exc = pretty_stringify_or_debug_print ( & exc) ;
702700 if formatted_exc. contains ( "\n " ) {
703- format ! ( "JavaScript exception:\n {formatted_exc}" , )
701+ format ! ( "JavaScript exception:\n {formatted_exc}" , )
704702 } else {
705- format ! ( "JavaScript exception: {formatted_exc}" , )
703+ format ! ( "JavaScript exception: {formatted_exc}" , )
706704 }
707705 } )
708706}
0 commit comments