pub fn mod_evaluate(
    &mut self,
    id: ModuleId,
  ) -> mpsc::Receiver<Result<(), AnyError>> {
    let state_rc = Self::state(self.v8_isolate());
    let module_map_rc = Self::module_map(self.v8_isolate());
    let scope = &mut self.handle_scope();

    let module = module_map_rc
      .borrow()
      .get_handle(id)
      .map(|handle| v8::Local::new(scope, handle))
      .expect("ModuleInfo not found");
    let mut status = module.get_status();
    assert_eq!(status, v8::ModuleStatus::Instantiated);

    let (sender, receiver) = mpsc::channel(1);

    // IMPORTANT: Top-level-await is enabled, which means that return value
    // of module evaluation is a promise.
    //
    // Because that promise is created internally by V8, when error occurs during
    // module evaluation the promise is rejected, and since the promise has no rejection
    // handler it will result in call to `bindings::promise_reject_callback` adding
    // the promise to pending promise rejection table - meaning JsRuntime will return
    // error on next poll().
    //
    // This situation is not desirable as we want to manually return error at the
    // end of this function to handle it further. It means we need to manually
    // remove this promise from pending promise rejection table.
    //
    // For more details see:
    // https://github.com/denoland/deno/issues/4908
    // https://v8.dev/features/top-level-await#module-execution-order
    let maybe_value = module.evaluate(scope);

    // Update status after evaluating.
    status = module.get_status();

    if let Some(value) = maybe_value {
      assert!(
        status == v8::ModuleStatus::Evaluated
          || status == v8::ModuleStatus::Errored
      );
      let promise = v8::Local::<v8::Promise>::try_from(value)
        .expect("Expected to get promise as module evaluation result");
      let promise_global = v8::Global::new(scope, promise);
      let mut state = state_rc.borrow_mut();
      state.pending_promise_exceptions.remove(&promise_global);
      let promise_global = v8::Global::new(scope, promise);
      assert!(
        state.pending_mod_evaluate.is_none(),
        "There is already pending top level module evaluation"
      );

      state.pending_mod_evaluate = Some(ModEvaluate {
        promise: promise_global,
        sender,
      });
      scope.perform_microtask_checkpoint();
    } else {
      assert!(status == v8::ModuleStatus::Errored);
    }

    receiver
  }
// TODO(bartlomieju): make it return `ModuleEvaluationFuture`?
/// Evaluates an already instantiated ES module.
///
/// Returns a receiver handle that resolves when module promise resolves.
/// Implementors must manually call `run_event_loop()` to drive module
/// evaluation future.
///
/// `AnyError` can be downcast to a type that exposes additional information
/// about the V8 exception. By default this type is `JsError`, however it may
/// be a different type if `RuntimeOptions::js_error_create_fn` has been set.
///
/// This function panics if module has not been instantiated.
make it return `ModuleEvaluationFuture`.
Evaluates an already instantiated ES module.

Returns a receiver handle that resolves when module promise resolves.
Implementors must manually call `run_event_loop()` to drive module
evaluation future.

`AnyError` can be downcast to a type that exposes additional information
about the V8 exception. By default this type is `JsError`, however it may
be a different type if `RuntimeOptions::js_error_create_fn` has been set.

This function panics if module has not been instantiated.
make it return ` ModuleEvaluationFuture ` . Evaluates an already instantiated ES module . Returns a receiver handle that resolves when module promise resolves . Implementors must manually call ` run_event_loop () ` to drive module evaluation future . ` AnyError ` can be downcast to a type that exposes additional information about the V8 exception . By default this type is ` JsError ` however it may be a different type if ` RuntimeOptions :: js_error_create_fn ` has been set . This function panics if module has not been instantiated .
pub fn mod_evaluate(
    &mut self,
    id: ModuleId,
  ) -> mpsc::Receiver<Result<(), AnyError>> {
    let state_rc = Self::state(self.v8_isolate());
    let module_map_rc = Self::module_map(self.v8_isolate());
    let scope = &mut self.handle_scope();
    let module = module_map_rc
      .borrow()
      .get_handle(id)
      .map(|handle| v8::Local::new(scope, handle))
      .expect("ModuleInfo not found");
    let mut status = module.get_status();
    assert_eq!(status, v8::ModuleStatus::Instantiated);
    let (sender, receiver) = mpsc::channel(1);
    // IMPORTANT: Top-level-await is enabled, which means that return value
    // of module evaluation is a promise.
    //
    // Because that promise is created internally by V8, when error occurs during
    // module evaluation the promise is rejected, and since the promise has no rejection
    // handler it will result in call to `bindings::promise_reject_callback` adding
    // the promise to pending promise rejection table - meaning JsRuntime will return
    // error on next poll().
    //
    // This situation is not desirable as we want to manually return error at the
    // end of this function to handle it further. It means we need to manually
    // remove this promise from pending promise rejection table.
    //
    // For more details see:
    // https://github.com/denoland/deno/issues/4908
    // https://v8.dev/features/top-level-await#module-execution-order
    let maybe_value = module.evaluate(scope);
    // Update status after evaluating.
    status = module.get_status();
    if let Some(value) = maybe_value {
      assert!(
        status == v8::ModuleStatus::Evaluated
          || status == v8::ModuleStatus::Errored
      );
      let promise = v8::Local::<v8::Promise>::try_from(value)
        .expect("Expected to get promise as module evaluation result");
      let promise_global = v8::Global::new(scope, promise);
      let mut state = state_rc.borrow_mut();
      state.pending_promise_exceptions.remove(&promise_global);
      let promise_global = v8::Global::new(scope, promise);
      assert!(
        state.pending_mod_evaluate.is_none(),
        "There is already pending top level module evaluation"
      );
      state.pending_mod_evaluate = Some(ModEvaluate {
        promise: promise_global,
        sender,
      });
      scope.perform_microtask_checkpoint();
    } else {
      assert!(status == v8::ModuleStatus::Errored);
    }
    receiver
  }
pub fn mod_evaluate ( & mut self , id : ModuleId , ) -> mpsc :: Receiver < Result < ( ) , AnyError > > { let state_rc = Self :: state ( self . v8_isolate ( ) ) ; let module_map_rc = Self :: module_map ( self . v8_isolate ( ) ) ; let scope = & mut self . handle_scope ( ) ; let module = module_map_rc . borrow ( ) . get_handle ( id ) . map ( | handle | v8 :: Local :: new ( scope , handle ) ) . expect ( " " ) ; let mut status = module . get_status ( ) ; assert_eq ! ( status v8 ModuleStatus Instantiated ) ; let ( sender , receiver ) = mpsc :: channel ( 1 ) ; // IMPORTANT: Top-level-await is enabled, which means that return value // of module evaluation is a promise. // // Because that promise is created internally by V8, when error occurs during // module evaluation the promise is rejected, and since the promise has no rejection // handler it will result in call to `bindings::promise_reject_callback` adding // the promise to pending promise rejection table - meaning JsRuntime will return // error on next poll(). // // This situation is not desirable as we want to manually return error at the // end of this function to handle it further. It means we need to manually // remove this promise from pending promise rejection table. // // For more details see: // https://github.com/denoland/deno/issues/4908 // https://v8.dev/features/top-level-await#module-execution-order let maybe_value = module . evaluate ( scope ) ; // Update status after evaluating. status = module . get_status ( ) ; if let Some ( value ) = maybe_value { assert ! ( status v8 ModuleStatus Evaluated status v8 ModuleStatus Errored ) ; let promise = v8 :: Local :: < v8 :: Promise > :: try_from ( value ) . expect ( " " ) ; let promise_global = v8 :: Global :: new ( scope , promise ) ; let mut state = state_rc . borrow_mut ( ) ; state . pending_promise_exceptions . remove ( & promise_global ) ; let promise_global = v8 :: Global :: new ( scope , promise ) ; assert ! ( state pending_mod_evaluate is_none ( ) " " ) ; state . pending_mod_evaluate = Some ( ModEvaluate { promise : promise_global , sender , } ) ; scope . perform_microtask_checkpoint ( ) ; } else { assert ! ( status v8 ModuleStatus Errored ) ; } receiver }
TODO(bartlomieju): make it return `ModuleEvaluationFuture`?
TODO ( bartlomieju ) : make it return ` ModuleEvaluationFuture ` ?
&mut self
id
ModuleId
&mut self
id
ModuleId