diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-02-11 10:04:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-11 10:04:59 +0100 |
commit | 79b3bc05d6de520f1df73face1744ae3d8be0bb8 (patch) | |
tree | f4c449efa67b88c767df52dd3ecec2246dded2e5 /core/es_isolate.rs | |
parent | 81905a867ea3f942619229e330840d132c57a5da (diff) |
workers: basic event loop (#3828)
* establish basic event loop for workers
* make "self.close()" inside worker
* remove "runWorkerMessageLoop() - instead manually call global function
in Rust when message arrives. This is done in preparation for structured clone
* refactor "WorkerChannel" and use distinct structs for internal
and external channels; "WorkerChannelsInternal" and "WorkerHandle"
* move "State.worker_channels_internal" to "Worker.internal_channels"
* add "WorkerEvent" enum for child->host communication;
currently "Message(Buf)" and "Error(ErrBox)" variants are supported
* add tests for nested workers
* add tests for worker throwing error on startup
Diffstat (limited to 'core/es_isolate.rs')
-rw-r--r-- | core/es_isolate.rs | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/core/es_isolate.rs b/core/es_isolate.rs index 295fe00ed..aeb6e318a 100644 --- a/core/es_isolate.rs +++ b/core/es_isolate.rs @@ -235,6 +235,51 @@ impl EsIsolate { } } + /// TODO(bartlomieju): copy-pasta to avoid problem with global handle attached + /// to ErrBox + pub fn mod_evaluate_dyn_import( + &mut self, + id: ModuleId, + ) -> Result<(), ErrBox> { + let isolate = self.core_isolate.v8_isolate.as_ref().unwrap(); + let mut locker = v8::Locker::new(isolate); + let mut hs = v8::HandleScope::new(locker.enter()); + let scope = hs.enter(); + assert!(!self.core_isolate.global_context.is_empty()); + let context = self.core_isolate.global_context.get(scope).unwrap(); + let mut cs = v8::ContextScope::new(scope, context); + let scope = cs.enter(); + + let info = self.modules.get_info(id).expect("ModuleInfo not found"); + let mut module = info.handle.get(scope).expect("Empty module handle"); + let mut status = module.get_status(); + + if status == v8::ModuleStatus::Instantiated { + let ok = module.evaluate(scope, context).is_some(); + // Update status after evaluating. + status = module.get_status(); + if ok { + assert!( + status == v8::ModuleStatus::Evaluated + || status == v8::ModuleStatus::Errored + ); + } else { + assert!(status == v8::ModuleStatus::Errored); + } + } + + match status { + v8::ModuleStatus::Evaluated => Ok(()), + v8::ModuleStatus::Errored => { + let i = &mut self.core_isolate; + let exception = module.get_exception(); + i.exception_to_err_result(scope, exception) + .map_err(|err| i.attach_handle_to_error(scope, err, exception)) + } + other => panic!("Unexpected module status {:?}", other), + } + } + /// Evaluates an already instantiated ES module. /// /// ErrBox can be downcast to a type that exposes additional information about @@ -274,7 +319,6 @@ impl EsIsolate { let i = &mut self.core_isolate; let exception = module.get_exception(); i.exception_to_err_result(scope, exception) - .map_err(|err| i.attach_handle_to_error(scope, err, exception)) } other => panic!("Unexpected module status {:?}", other), } @@ -425,7 +469,7 @@ impl EsIsolate { // Load is done. let module_id = load.root_module_id.unwrap(); self.mod_instantiate(module_id)?; - match self.mod_evaluate(module_id) { + match self.mod_evaluate_dyn_import(module_id) { Ok(()) => self.dyn_import_done(dyn_import_id, module_id)?, Err(err) => self.dyn_import_error(dyn_import_id, err)?, }; |