rustyscript 0.12.3

Effortless JS Integration for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
use std::{
    collections::{HashMap, HashSet},
    path::{Path, PathBuf},
    pin::Pin,
    rc::Rc,
    sync::Arc,
    task::Poll,
    time::Duration,
};

use deno_core::{
    futures::FutureExt, serde_json, serde_v8::from_v8, v8, JsRuntime, JsRuntimeForSnapshot,
    PollEventLoopOptions,
};
use deno_features::FeatureChecker;
use serde::de::DeserializeOwned;
use tokio_util::sync::CancellationToken;

use crate::{
    ext,
    module_loader::{LoaderOptions, RustyLoader},
    traits::{ToDefinedValue, ToModuleSpecifier, ToV8String},
    transpiler::transpile,
    utilities, Error, ExtensionOptions, Module, ModuleHandle,
};

/// Wrapper trait to make the `InnerRuntime` generic over the runtime types
pub trait RuntimeTrait {
    fn try_new(options: deno_core::RuntimeOptions) -> Result<Self, Error>
    where
        Self: Sized;
    fn rt_mut(&mut self) -> &mut JsRuntime;
}
impl RuntimeTrait for JsRuntime {
    fn try_new(options: deno_core::RuntimeOptions) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let rt = Self::try_new(options)?;
        Ok(rt)
    }
    fn rt_mut(&mut self) -> &mut JsRuntime {
        self
    }
}
impl RuntimeTrait for JsRuntimeForSnapshot {
    fn try_new(options: deno_core::RuntimeOptions) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let rt = Self::try_new(options)?;
        Ok(rt)
    }
    fn rt_mut(&mut self) -> &mut JsRuntime {
        self
    }
}

/// Represents a function that can be registered with the runtime
pub trait RsFunction:
    Fn(&[serde_json::Value]) -> Result<serde_json::Value, Error> + 'static
{
}
impl<F> RsFunction for F where
    F: Fn(&[serde_json::Value]) -> Result<serde_json::Value, Error> + 'static
{
}

/// Represents an async function that can be registered with the runtime
pub trait RsAsyncFunction:
    Fn(
        Vec<serde_json::Value>,
    ) -> Pin<Box<dyn std::future::Future<Output = Result<serde_json::Value, Error>>>>
    + 'static
{
}
impl<F> RsAsyncFunction for F where
    F: Fn(
            Vec<serde_json::Value>,
        ) -> Pin<Box<dyn std::future::Future<Output = Result<serde_json::Value, Error>>>>
        + 'static
{
}

/// Decodes a set of arguments into a vector of v8 values
/// This is used to pass arguments to a javascript function
/// And is faster and more flexible than using `json_args!`
fn decode_args<'a>(
    args: &impl serde::ser::Serialize,
    scope: &mut v8::HandleScope<'a>,
) -> Result<Vec<v8::Local<'a, v8::Value>>, Error> {
    let args = deno_core::serde_v8::to_v8(scope, args)?;
    match v8::Local::<v8::Array>::try_from(args) {
        Ok(args) => {
            let len = args.length();
            let mut result = Vec::with_capacity(len as usize);
            for i in 0..len {
                let index = v8::Integer::new(
                    scope,
                    i.try_into().map_err(|_| {
                        Error::Runtime(format!(
                            "Could not decode {len} arguments - use `big_json_args`"
                        ))
                    })?,
                );
                let arg = args
                    .get(scope, index.into())
                    .ok_or_else(|| Error::Runtime(format!("Invalid argument at index {i}")))?;
                result.push(arg);
            }
            Ok(result)
        }
        Err(_) if args.is_undefined() || args.is_null() => Ok(vec![]),
        Err(_) => Ok(vec![args]),
    }
}

/// Represents the set of options accepted by the runtime constructor
pub struct RuntimeOptions {
    /// A set of `deno_core` extensions to add to the runtime
    pub extensions: Vec<deno_core::Extension>,

    /// Additional options for the built-in extensions
    pub extension_options: ext::ExtensionOptions,

    /// Function to use as entrypoint if the module does not provide one
    pub default_entrypoint: Option<String>,

    /// Amount of time to run for before killing the thread
    pub timeout: Duration,

    /// Optional maximum heap size for the runtime
    ///
    /// If the heap size is exceeded, the runtime will return a `HeapExhausted` error.
    ///
    /// **WARNING** this is not a minimum heap size; the underlying V8 isolate will still crash if this number is too small for startup
    /// (~5mb with default features)
    pub max_heap_size: Option<usize>,

    /// Optional cache provider for the module loader
    #[allow(deprecated)]
    pub module_cache: Option<Box<dyn crate::module_loader::ModuleCacheProvider>>,

    /// Optional import provider for the module loader
    pub import_provider: Option<Box<dyn crate::module_loader::ImportProvider>>,

    /// Optional snapshot to load into the runtime
    ///
    /// This will reduce load times, but requires the same extensions to be loaded as when the snapshot was created  
    ///
    /// WARNING: Snapshots MUST be used on the same system they were created on
    pub startup_snapshot: Option<&'static [u8]>,

    /// Optional configuration parameters for building the underlying v8 isolate
    ///
    /// This can be used to alter the behavior of the runtime.
    ///
    /// See the `rusty_v8` documentation for more information
    pub isolate_params: Option<v8::CreateParams>,

    /// Optional shared array buffer store to use for the runtime.
    ///
    /// Allows data-sharing between runtimes across threads
    pub shared_array_buffer_store: Option<deno_core::SharedArrayBufferStore>,

    /// A whitelist of custom schema prefixes that are allowed to be loaded from javascript
    ///
    /// By default only `http`/`https` (`url_import` crate feature), and `file` (`fs_import` crate feature) are allowed
    pub schema_whlist: HashSet<String>,
}

impl Default for RuntimeOptions {
    fn default() -> Self {
        Self {
            extensions: Vec::default(),
            default_entrypoint: None,
            timeout: Duration::MAX,
            max_heap_size: None,
            module_cache: None,
            import_provider: None,
            startup_snapshot: None,
            isolate_params: None,
            shared_array_buffer_store: None,
            schema_whlist: HashSet::default(),

            extension_options: ExtensionOptions::default(),
        }
    }
}

/// Deno `JsRuntime` wrapper providing helper functions needed
/// by the public-facing Runtime API
///
/// This struct is not intended to be used directly by the end user
/// It provides a set of async functions that can be used to interact with the
/// underlying deno runtime instance
pub struct InnerRuntime<RT: RuntimeTrait> {
    pub module_loader: Rc<RustyLoader>,
    pub deno_runtime: RT,

    pub cwd: PathBuf,
    pub default_entrypoint: Option<String>,
}
impl<RT: RuntimeTrait> InnerRuntime<RT> {
    pub fn new(
        options: RuntimeOptions,
        heap_exhausted_token: CancellationToken,
    ) -> Result<Self, Error> {
        let cwd = std::env::current_dir()?;
        let module_loader = Rc::new(RustyLoader::new(LoaderOptions {
            cache_provider: options.module_cache,
            import_provider: options.import_provider,
            schema_whlist: options.schema_whlist,
            cwd: cwd.clone(),

            #[cfg(feature = "node_experimental")]
            node_resolver: options.extension_options.node_resolver.clone(),

            ..Default::default()
        }));

        // If a snapshot is provided, do not reload ESM for extensions
        let is_snapshot = options.startup_snapshot.is_some();
        let extensions = ext::all_extensions(
            options.extensions,
            options.extension_options,
            options.shared_array_buffer_store.clone(),
            is_snapshot,
        );

        // If a heap size is provided, set the isolate params (preserving any user-provided params otherwise)
        let isolate_params = match options.isolate_params {
            Some(params) => {
                if let Some(max_heap_size) = options.max_heap_size {
                    Some(params.heap_limits(0, max_heap_size))
                } else {
                    Some(params)
                }
            }
            None => {
                if let Some(max_heap_size) = options.max_heap_size {
                    let params = v8::Isolate::create_params().heap_limits(0, max_heap_size);
                    Some(params)
                } else {
                    None
                }
            }
        };

        let mut deno_runtime = RT::try_new(deno_core::RuntimeOptions {
            module_loader: Some(module_loader.clone()),

            extension_transpiler: Some(module_loader.as_extension_transpiler()),
            create_params: isolate_params,
            shared_array_buffer_store: options.shared_array_buffer_store.clone(),

            startup_snapshot: options.startup_snapshot,
            extensions,

            ..Default::default()
        })?;

        let mut feature_checker = FeatureChecker::default();
        feature_checker.set_exit_cb(Box::new(|_, _| {}));
        deno_runtime
            .rt_mut()
            .op_state()
            .borrow_mut()
            .put(Arc::new(feature_checker));

        // Add a callback to terminate the runtime if the max_heap_size limit is approached
        if options.max_heap_size.is_some() {
            let isolate_handle = deno_runtime.rt_mut().v8_isolate().thread_safe_handle();

            deno_runtime
                .rt_mut()
                .add_near_heap_limit_callback(move |current_value, _| {
                    isolate_handle.terminate_execution();

                    // Signal the outer runtime to cancel block_on future (avoid hanging) and return friendly error
                    heap_exhausted_token.cancel();

                    // Spike the heap limit while terminating to avoid segfaulting
                    // Callback may fire multiple times if memory usage increases quicker then termination finalizes
                    5 * current_value
                });
        }

        let default_entrypoint = options.default_entrypoint;
        Ok(Self {
            module_loader,
            deno_runtime,
            cwd,
            default_entrypoint,
        })
    }

    /// Destroy the `RustyScript` runtime, returning the deno RT instance
    #[allow(dead_code)]
    pub fn into_inner(self) -> RT {
        self.deno_runtime
    }

    /// Access the underlying deno runtime instance directly
    pub fn deno_runtime(&mut self) -> &mut JsRuntime {
        self.deno_runtime.rt_mut()
    }

    /// Set the current working directory for the runtime
    /// This is used to resolve relative paths in the module loader
    pub fn set_current_dir(&mut self, path: impl AsRef<Path>) -> Result<&Path, Error> {
        let path = path.as_ref();
        let path = utilities::resolve_path(path, Some(&self.cwd))?
            .to_file_path()
            .map_err(|()| Error::Runtime("Invalid path".to_string()))?;

        self.cwd = path;
        self.module_loader.set_current_dir(self.cwd.clone());
        Ok(&self.cwd)
    }

    pub fn current_dir(&self) -> &Path {
        &self.cwd
    }

    /// Remove and return a value from the state
    pub fn take<T>(&mut self) -> Option<T>
    where
        T: 'static,
    {
        let state = self.deno_runtime().op_state();
        if let Ok(mut state) = state.try_borrow_mut() {
            if state.has::<T>() {
                return Some(state.take());
            }
        }

        None
    }

    /// Add a value to the state
    /// Only one value of each type is stored
    pub fn put<T>(&mut self, value: T) -> Result<(), Error>
    where
        T: 'static,
    {
        let state = self.deno_runtime().op_state();
        let mut state = state.try_borrow_mut()?;
        state.put(value);

        Ok(())
    }

    /// Register an async rust function
    /// The function must return a Future that resolves to a `serde_json::Value`
    /// and accept a vec of `serde_json::Value` as arguments
    pub fn register_async_function<F>(&mut self, name: &str, callback: F) -> Result<(), Error>
    where
        F: RsAsyncFunction,
    {
        let state = self.deno_runtime().op_state();
        let mut state = state.try_borrow_mut()?;

        if !state.has::<HashMap<String, Box<dyn RsAsyncFunction>>>() {
            state.put(HashMap::<String, Box<dyn RsAsyncFunction>>::new());
        }

        // Insert the callback into the state
        state
            .borrow_mut::<HashMap<String, Box<dyn RsAsyncFunction>>>()
            .insert(name.to_string(), Box::new(callback));

        Ok(())
    }

    /// Register a rust function
    /// The function must return a `serde_json::Value`
    /// and accept a slice of `serde_json::Value` as arguments
    pub fn register_function<F>(&mut self, name: &str, callback: F) -> Result<(), Error>
    where
        F: RsFunction,
    {
        let state = self.deno_runtime().op_state();
        let mut state = state.try_borrow_mut()?;

        if !state.has::<HashMap<String, Box<dyn RsFunction>>>() {
            state.put(HashMap::<String, Box<dyn RsFunction>>::new());
        }

        // Insert the callback into the state
        state
            .borrow_mut::<HashMap<String, Box<dyn RsFunction>>>()
            .insert(name.to_string(), Box::new(callback));

        Ok(())
    }

    /// Runs the JS event loop to completion
    pub async fn await_event_loop(
        &mut self,
        options: PollEventLoopOptions,
        timeout: Option<Duration>,
    ) -> Result<(), Error> {
        if let Some(timeout) = timeout {
            Ok(tokio::select! {
                r = self.deno_runtime().run_event_loop(options) => r,
                () = tokio::time::sleep(timeout) => Ok(()),
            }?)
        } else {
            Ok(self.deno_runtime().run_event_loop(options).await?)
        }
    }

    /// Advances the JS event loop by one tick
    /// Return true if the event loop is pending
    pub async fn advance_event_loop(
        &mut self,
        options: PollEventLoopOptions,
    ) -> Result<bool, Error> {
        let result = std::future::poll_fn(|cx| {
            Poll::Ready(match self.deno_runtime().poll_event_loop(cx, options) {
                Poll::Ready(t) => t.map(|()| false),
                Poll::Pending => Ok(true),
            })
        })
        .await?;

        Ok(result)
    }

    /// Evaluate a piece of non-ECMAScript-module JavaScript code
    /// The expression is evaluated in the global context, so changes persist
    ///
    /// Async because some expressions may require a tokio runtime
    ///
    /// # Arguments
    /// * `expr` - A string representing the JavaScript expression to evaluate
    ///
    /// # Returns
    /// A `Result` containing the deserialized result of the expression (`T`)
    /// or an error (`Error`) if the expression cannot be evaluated or if the
    /// result cannot be deserialized.
    #[allow(clippy::unused_async, reason = "Prevent panic on sleep calls")]
    pub async fn eval(&mut self, expr: impl ToString) -> Result<v8::Global<v8::Value>, Error> {
        let result = self.deno_runtime().execute_script("", expr.to_string())?;
        Ok(result)
    }

    /// Attempt to get a value out of the global context (globalThis.name)
    ///
    /// # Arguments
    /// * `name` - Name of the object to extract
    ///
    /// # Returns
    /// A `Result` containing the non-null value extracted or an error (`Error`)
    pub fn get_global_value(&mut self, name: &str) -> Result<v8::Global<v8::Value>, Error> {
        let context = self.deno_runtime().main_context();
        let mut scope = self.deno_runtime().handle_scope();
        let global = context.open(&mut scope).global(&mut scope);

        let key = name.to_v8_string(&mut scope)?;
        let value = global.get(&mut scope, key.into());

        match value.if_defined() {
            Some(v) => Ok(v8::Global::<v8::Value>::new(&mut scope, v)),
            _ => Err(Error::ValueNotFound(name.to_string())),
        }
    }

    /// Attempt to get a value out of a module context
    ///     ///
    /// # Arguments
    /// * `module` - A handle to a loaded module
    /// * `name` - Name of the object to extract
    ///
    /// # Returns
    /// A `Result` containing the non-null value extracted or an error (`Error`)
    pub fn get_module_export_value(
        &mut self,
        module_context: &ModuleHandle,
        name: &str,
    ) -> Result<v8::Global<v8::Value>, Error> {
        let module_namespace = self
            .deno_runtime()
            .get_module_namespace(module_context.id())?;
        let mut scope = self.deno_runtime().handle_scope();
        let module_namespace = module_namespace.open(&mut scope);
        assert!(module_namespace.is_module_namespace_object());

        let key = name.to_v8_string(&mut scope)?;
        let value = module_namespace.get(&mut scope, key.into());

        match value.if_defined() {
            Some(v) => Ok(v8::Global::<v8::Value>::new(&mut scope, v)),
            _ => Err(Error::ValueNotFound(name.to_string())),
        }
    }

    pub async fn resolve_with_event_loop(
        &mut self,
        value: v8::Global<v8::Value>,
    ) -> Result<v8::Global<v8::Value>, Error> {
        let future = self.deno_runtime().resolve(value);
        let result = self
            .deno_runtime()
            .with_event_loop_future(future, PollEventLoopOptions::default())
            .await?;
        Ok(result)
    }

    pub fn decode_value<T>(&mut self, value: v8::Global<v8::Value>) -> Result<T, Error>
    where
        T: DeserializeOwned,
    {
        let mut scope = self.deno_runtime().handle_scope();
        let result = v8::Local::<v8::Value>::new(&mut scope, value);
        Ok(from_v8(&mut scope, result)?)
    }

    pub fn get_value_ref(
        &mut self,
        module_context: Option<&ModuleHandle>,
        name: &str,
    ) -> Result<v8::Global<v8::Value>, Error> {
        // Try to get the value from the module context first
        let result = module_context
            .and_then(|module_context| self.get_module_export_value(module_context, name).ok());

        // If it's not found, try the global context
        match result {
            Some(result) => Ok(result),
            None => self
                .get_global_value(name)
                .map_err(|_| Error::ValueNotFound(name.to_string())),
        }
    }

    /// Retrieves a javascript function by its name from the Deno runtime's global context.
    ///
    /// # Arguments
    /// * `module_context` - A module handle to use for context, to find exports
    /// * `name` - A string representing the name of the javascript function to retrieve.
    ///
    /// # Returns
    /// A `Result` containing a `v8::Global<v8::Function>` if
    /// the function is found, or an error (`Error`) if the function cannot be found or
    /// if it is not a valid javascript function.
    pub fn get_function_by_name(
        &mut self,
        module_context: Option<&ModuleHandle>,
        name: &str,
    ) -> Result<v8::Global<v8::Function>, Error> {
        // Get the value
        let value = self.get_value_ref(module_context, name)?;

        // Convert it into a function
        let mut scope = self.deno_runtime().handle_scope();
        let local_value = v8::Local::<v8::Value>::new(&mut scope, value);
        let f: v8::Local<v8::Function> = local_value
            .try_into()
            .or::<Error>(Err(Error::ValueNotCallable(name.to_string())))?;

        // Return it as a global
        Ok(v8::Global::<v8::Function>::new(&mut scope, f))
    }

    pub fn call_function_by_ref(
        &mut self,
        module_context: Option<&ModuleHandle>,
        function: &v8::Global<v8::Function>,
        args: &impl serde::ser::Serialize,
    ) -> Result<v8::Global<v8::Value>, Error> {
        // Namespace, if provided
        let module_namespace = if let Some(module_context) = module_context {
            Some(
                self.deno_runtime()
                    .get_module_namespace(module_context.id())?,
            )
        } else {
            None
        };

        let mut scope = self.deno_runtime().handle_scope();
        let mut scope = v8::TryCatch::new(&mut scope);

        // Get the namespace
        // Module-level if supplied, none otherwise
        let namespace: v8::Local<v8::Value> = if let Some(namespace) = module_namespace {
            v8::Local::<v8::Object>::new(&mut scope, namespace).into()
        } else {
            // Create a new object to use as the namespace if none is provided
            //let obj: v8::Local<v8::Value> = v8::Object::new(&mut scope).into();
            let obj: v8::Local<v8::Value> = v8::undefined(&mut scope).into();
            obj
        };

        let function_instance = function.open(&mut scope);

        // Prep arguments
        let args = decode_args(args, &mut scope)?;

        // Call the function
        let result = function_instance.call(&mut scope, namespace, &args);
        match result {
            Some(value) => {
                let value = v8::Global::new(&mut scope, value);
                Ok(value)
            }
            None if scope.has_caught() => {
                let e = scope
                    .message()
                    .ok_or_else(|| Error::Runtime("Unknown error".to_string()))?;

                let filename = e.get_script_resource_name(&mut scope);
                let linenumber = e.get_line_number(&mut scope).unwrap_or_default();
                let filename = if let Some(v) = filename {
                    let filename = v.to_rust_string_lossy(&mut scope);
                    format!("{filename}:{linenumber}: ")
                } else if let Some(module_context) = module_context {
                    let filename = module_context.module().filename().to_string_lossy();
                    format!("{filename}:{linenumber}: ")
                } else {
                    String::new()
                };

                let msg = e.get(&mut scope).to_rust_string_lossy(&mut scope);

                let s = format!("{filename}{msg}");
                Err(Error::Runtime(s))
            }
            None => Err(Error::Runtime(
                "Unknown error during function execution".to_string(),
            )),
        }
    }

    /// A utility function that run provided future concurrently with the event loop.
    ///
    /// If the event loop resolves while polling the future, it will continue to be polled,
    /// Unless it returned an error
    ///
    /// Useful for interacting with local inspector session.
    pub async fn with_event_loop_future<'fut, T, E>(
        &mut self,
        mut fut: impl std::future::Future<Output = Result<T, E>> + Unpin + 'fut,
        poll_options: PollEventLoopOptions,
    ) -> Result<T, Error>
    where
        deno_core::error::AnyError: From<E>,
        Error: std::convert::From<E>,
    {
        // Manually implement tokio::select
        std::future::poll_fn(|cx| {
            let evt_status = self.deno_runtime().poll_event_loop(cx, poll_options);
            let fut_status = fut.poll_unpin(cx);

            match (evt_status, fut_status) {
                (Poll::Ready(Err(e)), _) => {
                    // Event loop failed
                    Poll::Ready(Err(e.into()))
                }

                (_, Poll::Pending) => {
                    // Continue polling
                    Poll::Pending
                }

                (_, Poll::Ready(t)) => {
                    for _ in 0..100 {
                        if let Poll::Ready(Err(e)) =
                            self.deno_runtime().poll_event_loop(cx, poll_options)
                        {
                            return Poll::Ready(Err(e.into()));
                        }
                    }

                    // Future resolved
                    Poll::Ready(t.map_err(Into::into))
                }
            }
        })
        .await
    }

    /// Get the entrypoint function for a module
    pub fn get_module_entrypoint(
        &mut self,
        module_context: &mut ModuleHandle,
    ) -> Result<Option<v8::Global<v8::Function>>, Error> {
        let default = self.default_entrypoint.clone();

        // Try to get an entrypoint from a call to `rustyscript.register_entrypoint` first
        let state = self.deno_runtime().op_state();
        let mut deep_state = state.try_borrow_mut()?;
        let entrypoint = deep_state.try_take::<v8::Global<v8::Function>>();
        if let Some(entrypoint) = entrypoint {
            return Ok(Some(entrypoint));
        }

        // Try to get an entrypoint from the default export next
        if let Ok(default_export) = self.get_module_export_value(module_context, "default") {
            let mut scope = self.deno_runtime().handle_scope();
            let default_export = v8::Local::new(&mut scope, default_export);
            if default_export.is_function() {
                if let Ok(f) = v8::Local::<v8::Function>::try_from(default_export) {
                    return Ok(Some(v8::Global::new(&mut scope, f)));
                }
            }
        }

        // Try to get an entrypoint from the default entrypoint
        if let Some(default) = default.as_deref() {
            if let Ok(f) = self.get_function_by_name(Some(module_context), default) {
                return Ok(Some(f));
            }
        }

        Ok(None)
    }

    /// Load one or more modules
    /// Returns a future that resolves to a handle to the main module, or the last
    /// side-module
    ///
    /// Will return a handle to the main module, or the last
    /// side-module
    pub async fn load_modules(
        &mut self,
        main_module: Option<&Module>,
        side_modules: Vec<&Module>,
    ) -> Result<ModuleHandle, Error> {
        if main_module.is_none() && side_modules.is_empty() {
            return Err(Error::Runtime(
                "Internal error: attempt to load no modules".to_string(),
            ));
        }

        let mut module_handle_stub = ModuleHandle::default();

        // Get additional modules first
        for side_module in side_modules {
            let module_specifier = side_module.filename().to_module_specifier(&self.cwd)?;
            let (code, sourcemap) = transpile(&module_specifier, side_module.contents())?;

            // Now CJS translation, for node
            #[cfg(feature = "node_experimental")]
            let code = self
                .module_loader
                .translate_cjs(&module_specifier, &code)
                .await?;

            let fast_code = deno_core::FastString::from(code.clone());

            let s_modid = self
                .deno_runtime()
                .load_side_es_module_from_code(&module_specifier, fast_code)
                .await?;

            // Update source map cache
            self.module_loader.insert_source_map(
                module_specifier.as_str(),
                code,
                sourcemap.map(|s| s.to_vec()),
            );

            let mod_load = self.deno_runtime().mod_evaluate(s_modid);
            self.with_event_loop_future(mod_load, PollEventLoopOptions::default())
                .await?;
            module_handle_stub = ModuleHandle::new(side_module, s_modid, None);
        }

        // Load main module
        if let Some(module) = main_module {
            let module_specifier = module.filename().to_module_specifier(&self.cwd)?;
            let (code, sourcemap) = transpile(&module_specifier, module.contents())?;

            // Now CJS translation, for node
            #[cfg(feature = "node_experimental")]
            let code = self
                .module_loader
                .translate_cjs(&module_specifier, &code)
                .await?;

            let fast_code = deno_core::FastString::from(code.clone());

            let module_id = self
                .deno_runtime()
                .load_main_es_module_from_code(&module_specifier, fast_code)
                .await?;

            // Update source map cache
            self.module_loader.insert_source_map(
                module_specifier.as_str(),
                code,
                sourcemap.map(|s| s.to_vec()),
            );

            // Finish execution
            let mod_load = self.deno_runtime().mod_evaluate(module_id);
            self.with_event_loop_future(mod_load, PollEventLoopOptions::default())
                .await?;
            module_handle_stub = ModuleHandle::new(module, module_id, None);
        }

        // Try to get the default entrypoint
        let entrypoint = self.get_module_entrypoint(&mut module_handle_stub)?;

        Ok(ModuleHandle::new(
            module_handle_stub.module(),
            module_handle_stub.id(),
            entrypoint,
        ))
    }
}

#[cfg(test)]
mod test_inner_runtime {
    use serde::Deserialize;

    use crate::{async_callback, big_json_args, js_value::Function, json_args, sync_callback};

    #[cfg(any(feature = "web", feature = "web_stub"))]
    use crate::js_value::Promise;

    use super::*;

    /// Used for blocking functions
    fn run_async_task<T, F, U>(f: F) -> T
    where
        U: std::future::Future<Output = Result<T, Error>>,
        F: FnOnce() -> U,
    {
        let timeout = Duration::from_secs(2);
        let tokio = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .thread_keep_alive(timeout)
            .build()
            .unwrap();
        tokio
            .block_on(async move {
                tokio::time::timeout(timeout, f())
                    .await
                    .expect("Test failed")
            })
            .expect("Timed out")
    }

    macro_rules! assert_v8 {
        ($l:expr, $r:expr, $t:ty, $rt:expr) => {
            assert_eq!($rt.decode_value::<$t>($l).expect("Wrong type"), $r,)
        };
    }

    #[test]
    fn test_decode_args() {
        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");
        let mut scope = runtime.deno_runtime.handle_scope();

        // empty
        let args = decode_args(&json_args!(), &mut scope).expect("Could not decode args");
        assert_eq!(args.len(), 0);

        // single
        let args = decode_args(&json_args!(2), &mut scope).expect("Could not decode args");
        assert_eq!(args.len(), 1);

        // single raw
        let args = decode_args(&2, &mut scope).expect("Could not decode args");
        assert_eq!(args.len(), 1);

        // multiple heterogeneous
        let args = decode_args(&json_args!(2, "test"), &mut scope).expect("Could not decode args");
        assert_eq!(args.len(), 2);

        // multiple homogeneous
        let args = decode_args(&json_args!(2, 3), &mut scope).expect("Could not decode args");
        assert_eq!(args.len(), 2);

        // 16 args
        let args = decode_args(
            &(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
            &mut scope,
        )
        .expect("Could not decode args");
        assert_eq!(args.len(), 16);

        // 32 args
        let args = decode_args(
            &big_json_args!(
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                10, 11, 12, 13, 14, 15
            ),
            &mut scope,
        )
        .expect("Could not decode args");
        assert_eq!(args.len(), 32);
    }

    #[test]
    fn test_put_take() {
        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        runtime.put(2usize).expect("Could not put value");
        let v = runtime.take::<usize>().expect("Could not take value");
        assert_eq!(v, 2);
    }

    #[test]
    fn test_register_async_function() {
        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");
        runtime
            .register_async_function(
                "test",
                async_callback!(|a: i64, b: i64| async move { Ok::<i64, Error>(a + b) }),
            )
            .expect("Could not register function");

        let module = Module::new(
            "test.js",
            "
            globalThis.v = await rustyscript.async_functions.test(2, 3);
            ",
        );

        let rt = &mut runtime;
        let module = run_async_task(|| async move { rt.load_modules(Some(&module), vec![]).await });

        let result = runtime
            .get_value_ref(Some(&module), "v")
            .expect("Could not find global");
        assert_v8!(result, 5, usize, runtime);
    }

    #[test]
    fn test_register_function() {
        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");
        runtime
            .register_function(
                "test",
                sync_callback!(|a: i64, b: i64| { Ok::<i64, Error>(a + b) }),
            )
            .expect("Could not register function");

        run_async_task(|| async move {
            let v = runtime
                .eval("rustyscript.functions.test(2, 3)")
                .await
                .expect("failed to eval");
            assert_v8!(v, 5, usize, runtime);
            Ok(())
        });
    }

    #[cfg(any(feature = "web", feature = "web_stub"))]
    #[test]
    fn test_eval() {
        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        run_async_task(|| async move {
            let v = runtime.eval("2 + 2").await.expect("failed to eval");
            assert_v8!(v, 4, usize, runtime);
            let result = runtime
                .eval(
                    "
                let sleep = (ms) => new Promise((r) => setTimeout(r, ms));
                sleep(500).then(() => 2);
            ",
                )
                .await
                .expect("failed to eval");

            let result: Promise<u32> = runtime
                .decode_value(result)
                .expect("Could not decode promise");

            let result: u32 = result.resolve(runtime.deno_runtime()).await?;
            assert_eq!(result, 2);
            Ok(())
        });
    }

    #[cfg(feature = "web_stub")]
    #[test]
    fn test_base64() {
        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        run_async_task(|| async move {
            let result = runtime.eval("btoa('foo')").await.expect("failed to eval");
            assert_v8!(result, "Zm9v", String, runtime);

            let result = runtime
                .eval("atob(btoa('foo'))")
                .await
                .expect("failed to eval");
            assert_v8!(result, "foo", String, runtime);

            Ok(())
        });
    }

    #[test]
    fn test_get_value_ref() {
        let module = Module::new(
            "test.js",
            "
            globalThis.a = 2;
            export const b = 'test';
            export const fnc = null;
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module = run_async_task(|| async move { rt.load_modules(Some(&module), vec![]).await });

        let v = runtime
            .get_value_ref(None, "a")
            .expect("Could not find global");
        assert_v8!(v, 2, usize, runtime);

        let v = runtime
            .get_value_ref(Some(&module), "a")
            .expect("Could not find global");
        assert_v8!(v, 2, usize, runtime);

        let v = runtime
            .get_value_ref(Some(&module), "b")
            .expect("Could not find export");
        assert_v8!(v, "test", String, runtime);

        runtime
            .get_value_ref(Some(&module), "c")
            .expect_err("Could not detect null");

        runtime
            .get_value_ref(Some(&module), "d")
            .expect_err("Could not detect undeclared");
    }

    #[test]
    fn test_get_function_by_name() {
        let module = Module::new(
            "test.js",
            "
            globalThis.fna = () => {};
            export function fnb() {}
            export const fnc = 2;
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module = run_async_task(|| async move { rt.load_modules(Some(&module), vec![]).await });

        runtime
            .get_function_by_name(Some(&module), "fna")
            .expect("Did not find global");
        runtime
            .get_function_by_name(Some(&module), "fnb")
            .expect("Did not find export");
        runtime
            .get_function_by_name(Some(&module), "fnc")
            .expect_err("Did not detect non-function");
        runtime
            .get_function_by_name(Some(&module), "fnd")
            .expect_err("Did not detect undefined");
    }

    #[test]
    fn test_call_function_by_ref() {
        let module = Module::new(
            "test.js",
            "
            globalThis.fna = (i) => i;
            export function fnb() {
                return 'test';
            }
            export const fnc = 2;
            export const fne = () => {};

            export const will_err = () => {
                throw new Error('msg');
            }
        ",
        );

        run_async_task(|| async move {
            let mut runtime =
                InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                    .expect("Could not load runtime");
            let handle = runtime.load_modules(Some(&module), vec![]).await?;

            let f = runtime.get_function_by_name(None, "fna").unwrap();
            let result = runtime
                .call_function_by_ref(Some(&handle), &f, json_args!(2))
                .expect("Could not call global");
            assert_v8!(result, 2, usize, runtime);

            let f = runtime.get_function_by_name(Some(&handle), "fnb").unwrap();
            let result = runtime
                .call_function_by_ref(Some(&handle), &f, json_args!())
                .expect("Could not call export");
            assert_v8!(result, "test", String, runtime);

            let f = runtime.get_function_by_name(Some(&handle), "fne").unwrap();
            runtime
                .call_function_by_ref(Some(&handle), &f, json_args!())
                .expect("Did not allow undefined return");

            let f = runtime
                .get_function_by_name(Some(&handle), "will_err")
                .unwrap();
            runtime
                .call_function_by_ref(Some(&handle), &f, json_args!())
                .expect_err("Did not catch error");

            Ok(())
        });
    }

    #[test]
    fn test_ts_loader() {
        let module = Module::new(
            "test.ts",
            "
            export function test(left:number, right:number): number {
                return left + right;
            }
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module = run_async_task(|| async move { rt.load_modules(Some(&module), vec![]).await });

        let f = runtime.get_function_by_name(Some(&module), "test").unwrap();
        let rt = &mut runtime;
        let result = run_async_task(|| async move {
            rt.call_function_by_ref(Some(&module), &f, json_args!(2, 3))
        });
        assert_v8!(result, 5, usize, runtime);
    }

    #[cfg(any(feature = "web", feature = "web_stub"))]
    #[test]
    fn test_toplevel_await() {
        let module = Module::new(
            "test.js",
            "
            const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
            await sleep(100);
            export function test() {
                return 2;
            }
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module = run_async_task(|| async move {
            let h = rt.load_modules(Some(&module), vec![]).await;
            rt.await_event_loop(PollEventLoopOptions::default(), None)
                .await?;
            h
        });

        let f = runtime.get_function_by_name(Some(&module), "test").unwrap();
        let rt = &mut runtime;
        let result =
            run_async_task(
                || async move { rt.call_function_by_ref(Some(&module), &f, json_args!()) },
            );
        assert_v8!(result, 2, usize, runtime);
    }

    #[cfg(any(feature = "web", feature = "web_stub"))]
    #[test]
    fn test_promise() {
        let module = Module::new(
            "test.js",
            "
            export const test = () => {
                return new Promise((resolve) => {
                    setTimeout(() => {
                        resolve(2);
                    }, 50);
                });
            }
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        run_async_task(|| async move {
            let module = rt.load_modules(Some(&module), vec![]).await?;

            let f = rt.get_function_by_name(Some(&module), "test").unwrap();
            let result = rt.call_function_by_ref(Some(&module), &f, json_args!())?;

            let result = rt.resolve_with_event_loop(result).await?;
            assert_v8!(result, 2, usize, rt);

            Ok(())
        });
    }

    #[cfg(any(feature = "web", feature = "web_stub"))]
    #[test]
    fn test_async_fn() {
        let module = Module::new(
            "test.js",
            "
            const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
            export async function test() {
                await sleep(100);
                return 2;
            }
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        run_async_task(|| async move {
            let module = rt.load_modules(Some(&module), vec![]).await?;

            let f = rt.get_function_by_name(Some(&module), "test")?;
            let result = rt.call_function_by_ref(Some(&module), &f, json_args!())?;
            let result: Promise<usize> = rt.decode_value(result).expect("Could not deserialize");
            let result: usize = result.resolve(rt.deno_runtime()).await?;
            assert_eq!(2, result);

            Ok(())
        });
    }

    #[test]
    fn test_deep_error() {
        let module = Module::new(
            "test.js",
            "
            await new Promise(r => setTimeout(r)); throw 'huh';
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        run_async_task(|| async move {
            let result = rt.load_modules(Some(&module), vec![]).await;
            assert!(result.is_err());
            Ok(())
        });
    }

    #[test]
    fn test_serialize_deep_fn() {
        let module = Module::new(
            "test.js",
            "
            let a = 2;
            export const test = {
                'name': 'test',
                'func': (x) => x + a
            }
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module = run_async_task(|| async move { rt.load_modules(Some(&module), vec![]).await });

        #[derive(Deserialize)]
        #[allow(clippy::items_after_statements)]
        struct TestStruct {
            #[allow(dead_code)]
            name: String,
            func: Function,
        }

        let structure = runtime.get_value_ref(Some(&module), "test").unwrap();
        let structure: TestStruct = runtime
            .decode_value(structure)
            .expect("Could not deserialize");

        let function = structure
            .func
            .as_global(&mut runtime.deno_runtime().handle_scope());

        run_async_task(|| async move {
            let value = runtime
                .call_function_by_ref(Some(&module), &function, json_args!(2))
                .expect("could not call function");
            assert_v8!(value, 4, usize, runtime);

            let value = runtime
                .call_function_by_ref(Some(&module), &function, json_args!(3))
                .expect("could not call function twice");
            assert_v8!(value, 5, usize, runtime);

            Ok(())
        });
    }

    #[test]
    fn test_async_load_errors() {
        let module = Module::new(
            "test.js",
            "
            throw new Error('msg');
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module_ = module.clone();
        let result =
            run_async_task(
                || async move { Ok(rt.load_modules(Some(&module_), vec![]).await.is_err()) },
            );
        assert!(result);

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let result =
            run_async_task(
                || async move { Ok(rt.load_modules(None, vec![&module]).await.is_err()) },
            );
        assert!(result);
    }

    #[test]
    fn test_serialize_fn() {
        let module = Module::new(
            "test.js",
            "
            export const test = (x) => 2*x;
        ",
        );

        let mut runtime =
            InnerRuntime::<JsRuntime>::new(RuntimeOptions::default(), CancellationToken::new())
                .expect("Could not load runtime");

        let rt = &mut runtime;
        let module = run_async_task(|| async move { rt.load_modules(Some(&module), vec![]).await });

        let function = runtime
            .get_function_by_name(Some(&module), "test")
            .expect("Could not get function");

        run_async_task(|| async move {
            let value = runtime
                .call_function_by_ref(Some(&module), &function, json_args!(2))
                .expect("could not call function");
            assert_v8!(value, 4, usize, runtime);

            let value = runtime
                .call_function_by_ref(None, &function, json_args!(2))
                .expect("could not call function");
            assert_v8!(value, 4, usize, runtime);

            Ok(())
        });
    }
}