@@ -12,7 +12,6 @@ use deno_core::{
1212 ModuleSpecifier ,
1313} ;
1414use errors:: ErrorMetadata ;
15- use futures:: future:: Either ;
1615use serde_json:: Value as JsonValue ;
1716use value:: {
1817 ConvexObject ,
@@ -22,6 +21,7 @@ use value::{
2221use super :: {
2322 client:: {
2423 AsyncSyscallCompletion ,
24+ EvaluateResult ,
2525 PendingAsyncSyscall ,
2626 } ,
2727 context_state:: ContextState ,
@@ -302,10 +302,10 @@ impl<'enter, 'scope: 'enter> EnteredContext<'enter, 'scope> {
302302 url : & ModuleSpecifier ,
303303 name : & str ,
304304 args : ConvexObject ,
305- ) -> anyhow:: Result < Either < ConvexValue , ( v8:: Global < v8:: Promise > , Vec < PendingAsyncSyscall > ) > >
306- {
305+ ) -> anyhow:: Result < ( v8:: Global < v8:: Promise > , EvaluateResult ) > {
307306 let module_global = {
308- let context_state = self . context_state ( ) ?;
307+ let context_state = self . context_state_mut ( ) ?;
308+ context_state. environment . start_execution ( ) ?;
309309 context_state
310310 . module_map
311311 . modules
@@ -392,32 +392,15 @@ impl<'enter, 'scope: 'enter> EnteredContext<'enter, 'scope> {
392392 . ok_or_else ( || anyhow ! ( "Failed to call invoke function" ) ) ?
393393 . try_into ( ) ?;
394394
395- match promise. state ( ) {
396- v8:: PromiseState :: Pending => {
397- let promise = v8:: Global :: new ( self . scope , promise) ;
398- let pending = mem:: take ( & mut self . context_state_mut ( ) ?. pending_async_syscalls ) ;
399- Ok ( Either :: Right ( ( promise, pending) ) )
400- } ,
401- v8:: PromiseState :: Fulfilled => {
402- let result: v8:: Local < v8:: String > = promise. result ( self . scope ) . try_into ( ) ?;
403- let result = helpers:: to_rust_string ( self . scope , & result) ?;
404- // TODO: `deserialize_udf_result`
405- let result_json: JsonValue = serde_json:: from_str ( & result) ?;
406- let result = ConvexValue :: try_from ( result_json) ?;
407- Ok ( Either :: Left ( result) )
408- } ,
409- v8:: PromiseState :: Rejected => {
410- todo ! ( )
411- } ,
412- }
395+ let evaluate_result = self . check_promise_result ( & promise) ?;
396+ Ok ( ( v8:: Global :: new ( self . scope , promise) , evaluate_result) )
413397 }
414398
415399 pub fn poll_function (
416400 & mut self ,
417401 completions : Vec < AsyncSyscallCompletion > ,
418402 promise : & v8:: Global < v8:: Promise > ,
419- ) -> anyhow:: Result < Either < ConvexValue , ( v8:: Global < v8:: Promise > , Vec < PendingAsyncSyscall > ) > >
420- {
403+ ) -> anyhow:: Result < EvaluateResult > {
421404 let completed = {
422405 let context_state = self . context_state_mut ( ) ?;
423406 let mut completed = vec ! [ ] ;
@@ -451,19 +434,27 @@ impl<'enter, 'scope: 'enter> EnteredContext<'enter, 'scope> {
451434 self . execute_user_code ( |s| s. perform_microtask_checkpoint ( ) ) ?;
452435
453436 let promise = v8:: Local :: new ( self . scope , promise) ;
437+ self . check_promise_result ( & promise)
438+ }
439+
440+ fn check_promise_result (
441+ & mut self ,
442+ promise : & v8:: Local < v8:: Promise > ,
443+ ) -> anyhow:: Result < EvaluateResult > {
454444 match promise. state ( ) {
455445 v8:: PromiseState :: Pending => {
456- let promise = v8 :: Global :: new ( self . scope , promise ) ;
457- let pending = mem:: take ( & mut self . context_state_mut ( ) ?. pending_async_syscalls ) ;
458- Ok ( Either :: Right ( ( promise , pending ) ) )
446+ let async_syscalls =
447+ mem:: take ( & mut self . context_state_mut ( ) ?. pending_async_syscalls ) ;
448+ Ok ( EvaluateResult :: Pending { async_syscalls } )
459449 } ,
460450 v8:: PromiseState :: Fulfilled => {
461451 let result: v8:: Local < v8:: String > = promise. result ( self . scope ) . try_into ( ) ?;
462452 let result = helpers:: to_rust_string ( self . scope , & result) ?;
463453 // TODO: `deserialize_udf_result`
464454 let result_json: JsonValue = serde_json:: from_str ( & result) ?;
465455 let result = ConvexValue :: try_from ( result_json) ?;
466- Ok ( Either :: Left ( result) )
456+ let outcome = self . context_state_mut ( ) ?. environment . finish_execution ( ) ?;
457+ Ok ( EvaluateResult :: Ready { result, outcome } )
467458 } ,
468459 v8:: PromiseState :: Rejected => {
469460 todo ! ( )
0 commit comments