diff options
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index bcf999b3b..353c4a442 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -318,6 +318,7 @@ pub struct WebWorker { pub worker_type: WebWorkerType, pub main_module: ModuleSpecifier, poll_for_messages_fn: Option<v8::Global<v8::Value>>, + bootstrap_fn_global: Option<v8::Global<v8::Function>>, } pub struct WebWorkerOptions { @@ -496,6 +497,25 @@ impl WebWorker { (internal_handle, external_handle) }; + let bootstrap_fn_global = { + let context = js_runtime.global_context(); + let scope = &mut js_runtime.handle_scope(); + let context_local = v8::Local::new(scope, context); + let global_obj = context_local.global(scope); + let bootstrap_str = v8::String::new(scope, "bootstrap").unwrap(); + let bootstrap_ns: v8::Local<v8::Object> = global_obj + .get(scope, bootstrap_str.into()) + .unwrap() + .try_into() + .unwrap(); + let main_runtime_str = v8::String::new(scope, "workerRuntime").unwrap(); + let bootstrap_fn = + bootstrap_ns.get(scope, main_runtime_str.into()).unwrap(); + let bootstrap_fn = + v8::Local::<v8::Function>::try_from(bootstrap_fn).unwrap(); + v8::Global::new(scope, bootstrap_fn) + }; + ( Self { id: worker_id, @@ -505,6 +525,7 @@ impl WebWorker { worker_type: options.worker_type, main_module, poll_for_messages_fn: None, + bootstrap_fn_global: Some(bootstrap_fn_global), }, external_handle, ) @@ -513,15 +534,24 @@ impl WebWorker { pub fn bootstrap(&mut self, options: &BootstrapOptions) { // Instead of using name for log we use `worker-${id}` because // WebWorkers can have empty string as name. - let script = format!( - "bootstrap.workerRuntime({}, \"{}\", \"{}\")", - options.as_json(), - self.name, - self.id - ); - self - .execute_script(&located_script_name!(), &script) - .expect("Failed to execute worker bootstrap script"); + { + let scope = &mut self.js_runtime.handle_scope(); + let options_v8 = + deno_core::serde_v8::to_v8(scope, options.as_json()).unwrap(); + let bootstrap_fn = self.bootstrap_fn_global.take().unwrap(); + let bootstrap_fn = v8::Local::new(scope, bootstrap_fn); + let undefined = v8::undefined(scope); + let name_str: v8::Local<v8::Value> = + v8::String::new(scope, &self.name).unwrap().into(); + let id_str: v8::Local<v8::Value> = + v8::String::new(scope, &format!("{}", self.id)) + .unwrap() + .into(); + bootstrap_fn + .call(scope, undefined.into(), &[options_v8, name_str, id_str]) + .unwrap(); + } + // TODO(bartlomieju): this could be done using V8 API, without calling `execute_script`. // Save a reference to function that will start polling for messages // from a worker host; it will be called after the user code is loaded. let script = r#" |