@@ -6,7 +6,7 @@ use crate::utils::json;
6
6
use crate :: utils:: path:: find_up;
7
7
use crate :: utils:: units:: { bytes_to_display, mib_to_bytes, percentage_value} ;
8
8
9
- use anyhow:: { anyhow, bail, Error } ;
9
+ use anyhow:: { anyhow, bail, Context , Error } ;
10
10
use arc_swap:: ArcSwapOption ;
11
11
use base_mem_check:: { MemCheckState , WorkerHeapStatistics } ;
12
12
use base_rt:: DenoRuntimeDropToken ;
@@ -18,15 +18,16 @@ use deno_core::error::{AnyError, JsError};
18
18
use deno_core:: url:: Url ;
19
19
use deno_core:: v8:: { self , GCCallbackFlags , GCType , HeapStatistics , Isolate } ;
20
20
use deno_core:: {
21
- located_script_name , serde_json, JsRuntime , ModuleCodeString , ModuleId , ModuleLoader ,
22
- ModuleSpecifier , OpState , PollEventLoopOptions , ResolutionKind , RuntimeOptions ,
21
+ serde_json, JsRuntime , ModuleId , ModuleLoader , ModuleSpecifier , OpState , PollEventLoopOptions ,
22
+ ResolutionKind , RuntimeOptions ,
23
23
} ;
24
24
use deno_http:: DefaultHttpPropertyExtractor ;
25
25
use deno_tls:: deno_native_certs:: load_native_certs;
26
26
use deno_tls:: rustls:: RootCertStore ;
27
27
use deno_tls:: RootCertStoreProvider ;
28
28
use futures_util:: future:: poll_fn;
29
29
use futures_util:: task:: AtomicWaker ;
30
+ use futures_util:: FutureExt ;
30
31
use log:: error;
31
32
use once_cell:: sync:: { Lazy , OnceCell } ;
32
33
use sb_core:: http:: sb_core_http;
@@ -205,15 +206,45 @@ impl MemCheck {
205
206
}
206
207
207
208
pub trait GetRuntimeContext {
208
- fn get_runtime_context ( ) -> impl Serialize ;
209
- }
209
+ fn get_runtime_context (
210
+ conf : & WorkerRuntimeOpts ,
211
+ use_inspector : bool ,
212
+ version : Option < & str > ,
213
+ ) -> impl Serialize {
214
+ serde_json:: json!( {
215
+ "target" : env!( "TARGET" ) ,
216
+ "kind" : conf. to_worker_kind( ) . to_string( ) ,
217
+ "debug" : cfg!( debug_assertions) ,
218
+ "inspector" : use_inspector,
219
+ "version" : {
220
+ "runtime" : version. unwrap_or( "0.1.0" ) ,
221
+ "deno" : MAYBE_DENO_VERSION
222
+ . get( )
223
+ . map( |it| & * * it)
224
+ . unwrap_or( "UNKNOWN" ) ,
225
+ } ,
226
+ "flags" : {
227
+ "SHOULD_DISABLE_DEPRECATED_API_WARNING" : SHOULD_DISABLE_DEPRECATED_API_WARNING
228
+ . get( )
229
+ . copied( )
230
+ . unwrap_or_default( ) ,
231
+ "SHOULD_USE_VERBOSE_DEPRECATED_API_WARNING" : SHOULD_USE_VERBOSE_DEPRECATED_API_WARNING
232
+ . get( )
233
+ . copied( )
234
+ . unwrap_or_default( )
235
+ }
236
+ } )
237
+ }
210
238
211
- impl GetRuntimeContext for ( ) {
212
- fn get_runtime_context ( ) -> impl Serialize {
213
- serde_json:: json!( null)
239
+ fn get_extra_context ( ) -> impl Serialize {
240
+ serde_json:: json!( { } )
214
241
}
215
242
}
216
243
244
+ type DefaultRuntimeContext = ( ) ;
245
+
246
+ impl GetRuntimeContext for DefaultRuntimeContext { }
247
+
217
248
#[ derive( Debug , Clone ) ]
218
249
struct GlobalMainContext ( v8:: Global < v8:: Context > ) ;
219
250
@@ -240,7 +271,7 @@ pub enum WillTerminateReason {
240
271
WallClock ,
241
272
}
242
273
243
- pub struct DenoRuntime < RuntimeContext = ( ) > {
274
+ pub struct DenoRuntime < RuntimeContext = DefaultRuntimeContext > {
244
275
pub js_runtime : ManuallyDrop < JsRuntime > ,
245
276
pub drop_token : CancellationToken ,
246
277
pub env_vars : HashMap < String , String > , // TODO: does this need to be pub?
@@ -282,7 +313,7 @@ impl<RuntimeContext> Drop for DenoRuntime<RuntimeContext> {
282
313
}
283
314
}
284
315
285
- impl DenoRuntime < ( ) > {
316
+ impl DenoRuntime < DefaultRuntimeContext > {
286
317
pub async fn acquire ( ) -> OwnedSemaphorePermit {
287
318
RUNTIME_CREATION_SEM
288
319
. with ( |v| v. clone ( ) )
@@ -668,10 +699,10 @@ where
668
699
let global_obj = context_local. global ( scope) ;
669
700
let bootstrap_str =
670
701
v8:: String :: new_external_onebyte_static ( scope, b"bootstrap" ) . unwrap ( ) ;
671
- let bootstrap_ns: v8 :: Local < v8 :: Object > = global_obj
702
+ let bootstrap_ns = global_obj
672
703
. get ( scope, bootstrap_str. into ( ) )
673
704
. unwrap ( )
674
- . try_into ( )
705
+ . to_object ( scope )
675
706
. unwrap ( ) ;
676
707
677
708
let dispatch_load_event_fn_str =
@@ -717,56 +748,17 @@ where
717
748
op_state. put ( dispatch_fns) ;
718
749
op_state. put ( promise_metrics. clone ( ) ) ;
719
750
op_state. put ( GlobalMainContext ( main_context) ) ;
751
+ }
752
+
753
+ {
754
+ let op_state_rc = js_runtime. op_state ( ) ;
755
+ let mut op_state = op_state_rc. borrow_mut ( ) ;
720
756
721
757
// NOTE(Andreespirela): We do this because "NODE_DEBUG" is trying to be read during
722
758
// initialization, But we need the gotham state to be up-to-date.
723
759
op_state. put ( sb_env:: EnvVars :: default ( ) ) ;
724
760
}
725
761
726
- // Bootstrapping stage
727
- let script = format ! (
728
- "globalThis.bootstrapSBEdge({}, {})" ,
729
- serde_json:: json!( [
730
- // 0: target
731
- env!( "TARGET" ) ,
732
- // 1: isUserWorker
733
- conf. is_user_worker( ) ,
734
- // 2: isEventsWorker
735
- conf. is_events_worker( ) ,
736
- // 3: edgeRuntimeVersion
737
- option_env!( "GIT_V_TAG" ) . unwrap_or( "0.1.0" ) ,
738
- // 4: denoVersion
739
- MAYBE_DENO_VERSION
740
- . get( )
741
- . map( |it| & * * it)
742
- . unwrap_or( "UNKNOWN" ) ,
743
- // 5: shouldDisableDeprecatedApiWarning
744
- SHOULD_DISABLE_DEPRECATED_API_WARNING
745
- . get( )
746
- . copied( )
747
- . unwrap_or_default( ) ,
748
- // 6: shouldUseVerboseDeprecatedApiWarning
749
- SHOULD_USE_VERBOSE_DEPRECATED_API_WARNING
750
- . get( )
751
- . copied( )
752
- . unwrap_or_default( ) ,
753
- ] ) ,
754
- {
755
- let mut runtime_context = serde_json:: json!( RuntimeContext :: get_runtime_context( ) ) ;
756
-
757
- json:: merge_object(
758
- & mut runtime_context,
759
- & conf
760
- . as_user_worker( )
761
- . and_then( |it| it. context. clone( ) )
762
- . map( serde_json:: Value :: Object )
763
- . unwrap_or_else( || serde_json:: json!( { } ) ) ,
764
- ) ;
765
-
766
- runtime_context
767
- }
768
- ) ;
769
-
770
762
if let Some ( inspector) = maybe_inspector. clone ( ) {
771
763
inspector. server . register_inspector (
772
764
main_module_url. to_string ( ) ,
@@ -788,9 +780,60 @@ where
788
780
. put ( MemCheckWaker :: from ( mem_check. waker . clone ( ) ) ) ;
789
781
}
790
782
783
+ // Bootstrapping stage
784
+ let ( runtime_context, extra_context, bootstrap_fn) = {
785
+ let runtime_context = serde_json:: json!( RuntimeContext :: get_runtime_context(
786
+ & conf,
787
+ has_inspector,
788
+ option_env!( "GIT_V_TAG" ) ,
789
+ ) ) ;
790
+
791
+ let extra_context = {
792
+ let mut context = serde_json:: json!( RuntimeContext :: get_extra_context( ) ) ;
793
+
794
+ json:: merge_object (
795
+ & mut context,
796
+ & conf
797
+ . as_user_worker ( )
798
+ . and_then ( |it| it. context . clone ( ) )
799
+ . map ( serde_json:: Value :: Object )
800
+ . unwrap_or_else ( || serde_json:: json!( { } ) ) ,
801
+ ) ;
802
+
803
+ context
804
+ } ;
805
+
806
+ let context = js_runtime. main_context ( ) ;
807
+ let scope = & mut js_runtime. handle_scope ( ) ;
808
+ let context_local = v8:: Local :: new ( scope, context) ;
809
+ let global_obj = context_local. global ( scope) ;
810
+ let bootstrap_str =
811
+ v8:: String :: new_external_onebyte_static ( scope, b"bootstrapSBEdge" ) . unwrap ( ) ;
812
+ let bootstrap_fn = v8:: Local :: < v8:: Function > :: try_from (
813
+ global_obj. get ( scope, bootstrap_str. into ( ) ) . unwrap ( ) ,
814
+ )
815
+ . unwrap ( ) ;
816
+
817
+ let runtime_context_local = deno_core:: serde_v8:: to_v8 ( scope, runtime_context)
818
+ . context ( "failed to convert to v8 value" ) ?;
819
+ let runtime_context_global = v8:: Global :: new ( scope, runtime_context_local) ;
820
+ let extra_context_local = deno_core:: serde_v8:: to_v8 ( scope, extra_context)
821
+ . context ( "failed to convert to v8 value" ) ?;
822
+ let extra_context_global = v8:: Global :: new ( scope, extra_context_local) ;
823
+ let bootstrap_fn_global = v8:: Global :: new ( scope, bootstrap_fn) ;
824
+
825
+ (
826
+ runtime_context_global,
827
+ extra_context_global,
828
+ bootstrap_fn_global,
829
+ )
830
+ } ;
831
+
791
832
js_runtime
792
- . execute_script ( located_script_name ! ( ) , ModuleCodeString :: from ( script) )
793
- . expect ( "Failed to execute bootstrap script" ) ;
833
+ . call_with_args ( & bootstrap_fn, & [ runtime_context, extra_context] )
834
+ . now_or_never ( )
835
+ . transpose ( )
836
+ . context ( "failed to execute bootstrap script" ) ?;
794
837
795
838
{
796
839
// run inside a closure, so op_state_rc is released
@@ -1813,7 +1856,7 @@ mod test {
1813
1856
struct WithSyncFileAPI ;
1814
1857
1815
1858
impl GetRuntimeContext for WithSyncFileAPI {
1816
- fn get_runtime_context ( ) -> impl Serialize {
1859
+ fn get_extra_context ( ) -> impl Serialize {
1817
1860
serde_json:: json!( {
1818
1861
"useReadSyncFileAPI" : true ,
1819
1862
} )
@@ -2559,7 +2602,7 @@ mod test {
2559
2602
struct Ctx ;
2560
2603
2561
2604
impl GetRuntimeContext for Ctx {
2562
- fn get_runtime_context ( ) -> impl Serialize {
2605
+ fn get_extra_context ( ) -> impl Serialize {
2563
2606
serde_json:: json!( {
2564
2607
"useReadSyncFileAPI" : true ,
2565
2608
"shouldBootstrapMockFnThrowError" : true ,
0 commit comments