summaryrefslogtreecommitdiff
path: root/ext/ffi/lib.rs
blob: 6c7f3375f9c9bdf7a59767bf296f9496b311174b (plain)
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
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

use core::ptr::NonNull;
use deno_core::anyhow::anyhow;
use deno_core::error::generic_error;
use deno_core::error::range_error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::Future;
use deno_core::include_js_files;
use deno_core::op;

use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::serde_v8;
use deno_core::v8;
use deno_core::Extension;
use deno_core::OpState;
use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::ZeroCopyBuf;
use dlopen::raw::Library;
use libffi::middle::Arg;
use libffi::middle::Cif;
use libffi::raw::*;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::path::Path;
use std::path::PathBuf;
use std::ptr;
use std::rc::Rc;

thread_local! {
  static IS_ISOLATE_THREAD: RefCell<bool> = RefCell::new(false);
}

pub struct Unstable(pub bool);

fn check_unstable(state: &OpState, api_name: &str) {
  let unstable = state.borrow::<Unstable>();

  if !unstable.0 {
    eprintln!(
      "Unstable API '{}'. The --unstable flag must be provided.",
      api_name
    );
    std::process::exit(70);
  }
}

pub fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
  let state = state.borrow();
  check_unstable(&state, api_name)
}

pub trait FfiPermissions {
  fn check(&mut self, path: Option<&Path>) -> Result<(), AnyError>;
}

#[derive(Clone)]
struct Symbol {
  cif: libffi::middle::Cif,
  ptr: libffi::middle::CodePtr,
  parameter_types: Vec<NativeType>,
  result_type: NativeType,
}

#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl Send for Symbol {}
unsafe impl Sync for Symbol {}

#[derive(Clone)]
struct PtrSymbol {
  cif: libffi::middle::Cif,
  ptr: libffi::middle::CodePtr,
}

impl PtrSymbol {
  fn new(fn_ptr: u64, def: &ForeignFunction) -> Self {
    let ptr = libffi::middle::CodePtr::from_ptr(fn_ptr as _);
    let cif = libffi::middle::Cif::new(
      def
        .parameters
        .clone()
        .into_iter()
        .map(libffi::middle::Type::from),
      def.result.into(),
    );

    Self { cif, ptr }
  }
}

#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl Send for PtrSymbol {}
unsafe impl Sync for PtrSymbol {}

struct DynamicLibraryResource {
  lib: Library,
  symbols: HashMap<String, Symbol>,
}

impl Resource for DynamicLibraryResource {
  fn name(&self) -> Cow<str> {
    "dynamicLibrary".into()
  }

  fn close(self: Rc<Self>) {
    drop(self)
  }
}

impl DynamicLibraryResource {
  fn register(
    &mut self,
    name: String,
    foreign_fn: ForeignFunction,
  ) -> Result<(), AnyError> {
    IS_ISOLATE_THREAD.with(|s| s.replace(true));
    let symbol = match &foreign_fn.name {
      Some(symbol) => symbol,
      None => &name,
    };
    // By default, Err returned by this function does not tell
    // which symbol wasn't exported. So we'll modify the error
    // message to include the name of symbol.
    let fn_ptr = match unsafe { self.lib.symbol::<*const c_void>(symbol) } {
      Ok(value) => Ok(value),
      Err(err) => Err(generic_error(format!(
        "Failed to register symbol {}: {}",
        symbol, err
      ))),
    }?;
    let ptr = libffi::middle::CodePtr::from_ptr(fn_ptr as _);
    let cif = libffi::middle::Cif::new(
      foreign_fn
        .parameters
        .clone()
        .into_iter()
        .map(libffi::middle::Type::from),
      foreign_fn.result.into(),
    );

    self.symbols.insert(
      name,
      Symbol {
        cif,
        ptr,
        parameter_types: foreign_fn.parameters,
        result_type: foreign_fn.result,
      },
    );

    Ok(())
  }

  fn get_static(&self, symbol: String) -> Result<*const c_void, AnyError> {
    // By default, Err returned by this function does not tell
    // which symbol wasn't exported. So we'll modify the error
    // message to include the name of symbol.
    match unsafe { self.lib.symbol::<*const c_void>(&symbol) } {
      Ok(value) => Ok(Ok(value)),
      Err(err) => Err(generic_error(format!(
        "Failed to register symbol {}: {}",
        symbol, err
      ))),
    }?
  }
}

pub fn init<P: FfiPermissions + 'static>(unstable: bool) -> Extension {
  Extension::builder()
    .js(include_js_files!(
      prefix "deno:ext/ffi",
      "00_ffi.js",
    ))
    .ops(vec![
      op_ffi_load::decl::<P>(),
      op_ffi_get_static::decl(),
      op_ffi_call::decl(),
      op_ffi_call_nonblocking::decl(),
      op_ffi_call_ptr::decl::<P>(),
      op_ffi_call_ptr_nonblocking::decl::<P>(),
      op_ffi_ptr_of::decl::<P>(),
      op_ffi_buf_copy_into::decl::<P>(),
      op_ffi_cstr_read::decl::<P>(),
      op_ffi_read_u8::decl::<P>(),
      op_ffi_read_i8::decl::<P>(),
      op_ffi_read_u16::decl::<P>(),
      op_ffi_read_i16::decl::<P>(),
      op_ffi_read_u32::decl::<P>(),
      op_ffi_read_i32::decl::<P>(),
      op_ffi_read_u64::decl::<P>(),
      op_ffi_read_f32::decl::<P>(),
      op_ffi_read_f64::decl::<P>(),
      op_ffi_unsafe_callback_create::decl::<P>(),
    ])
    .state(move |state| {
      // Stolen from deno_webgpu, is there a better option?
      state.put(Unstable(unstable));
      Ok(())
    })
    .build()
}

/// Defines the accepted types that can be used as
/// parameters and return values in FFI.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
enum NativeType {
  Void,
  U8,
  I8,
  U16,
  I16,
  U32,
  I32,
  U64,
  I64,
  USize,
  ISize,
  F32,
  F64,
  Pointer,
  Function,
}

impl From<NativeType> for libffi::middle::Type {
  fn from(native_type: NativeType) -> Self {
    match native_type {
      NativeType::Void => libffi::middle::Type::void(),
      NativeType::U8 => libffi::middle::Type::u8(),
      NativeType::I8 => libffi::middle::Type::i8(),
      NativeType::U16 => libffi::middle::Type::u16(),
      NativeType::I16 => libffi::middle::Type::i16(),
      NativeType::U32 => libffi::middle::Type::u32(),
      NativeType::I32 => libffi::middle::Type::i32(),
      NativeType::U64 => libffi::middle::Type::u64(),
      NativeType::I64 => libffi::middle::Type::i64(),
      NativeType::USize => libffi::middle::Type::usize(),
      NativeType::ISize => libffi::middle::Type::isize(),
      NativeType::F32 => libffi::middle::Type::f32(),
      NativeType::F64 => libffi::middle::Type::f64(),
      NativeType::Pointer => libffi::middle::Type::pointer(),
      NativeType::Function => libffi::middle::Type::pointer(),
    }
  }
}

/// Intermediate format for easy translation from NativeType + V8 value
/// to libffi argument types.
#[repr(C)]
union NativeValue {
  void_value: (),
  u8_value: u8,
  i8_value: i8,
  u16_value: u16,
  i16_value: i16,
  u32_value: u32,
  i32_value: i32,
  u64_value: u64,
  i64_value: i64,
  usize_value: usize,
  isize_value: isize,
  f32_value: f32,
  f64_value: f64,
  pointer: *const u8,
}

impl NativeValue {
  unsafe fn as_arg(&self, native_type: NativeType) -> Arg {
    match native_type {
      NativeType::Void => unreachable!(),
      NativeType::U8 => Arg::new(&self.u8_value),
      NativeType::I8 => Arg::new(&self.i8_value),
      NativeType::U16 => Arg::new(&self.u16_value),
      NativeType::I16 => Arg::new(&self.i16_value),
      NativeType::U32 => Arg::new(&self.u32_value),
      NativeType::I32 => Arg::new(&self.i32_value),
      NativeType::U64 => Arg::new(&self.u64_value),
      NativeType::I64 => Arg::new(&self.i64_value),
      NativeType::USize => Arg::new(&self.usize_value),
      NativeType::ISize => Arg::new(&self.isize_value),
      NativeType::F32 => Arg::new(&self.f32_value),
      NativeType::F64 => Arg::new(&self.f64_value),
      NativeType::Pointer | NativeType::Function => Arg::new(&self.pointer),
    }
  }

  // SAFETY: native_type must correspond to the type of value represented by the union field
  unsafe fn to_value(&self, native_type: NativeType) -> Value {
    match native_type {
      NativeType::Void => Value::Null,
      NativeType::U8 => Value::from(self.u8_value),
      NativeType::I8 => Value::from(self.i8_value),
      NativeType::U16 => Value::from(self.u16_value),
      NativeType::I16 => Value::from(self.i16_value),
      NativeType::U32 => Value::from(self.u32_value),
      NativeType::I32 => Value::from(self.i32_value),
      NativeType::U64 => {
        json!(U32x2::from(self.u64_value))
      }
      NativeType::I64 => {
        json!(U32x2::from(self.i64_value as u64))
      }
      NativeType::USize => {
        json!(U32x2::from(self.usize_value as u64))
      }
      NativeType::ISize => {
        json!(U32x2::from(self.isize_value as u64))
      }
      NativeType::F32 => Value::from(self.f32_value),
      NativeType::F64 => Value::from(self.f64_value),
      NativeType::Pointer | NativeType::Function => {
        json!(U32x2::from(self.pointer as u64))
      }
    }
  }

  // SAFETY: native_type must correspond to the type of value represented by the union field
  unsafe fn to_v8<'scope>(
    &self,
    scope: &mut v8::HandleScope<'scope>,
    native_type: NativeType,
  ) -> serde_v8::Value<'scope> {
    match native_type {
      NativeType::Void => {
        let local_value: v8::Local<v8::Value> = v8::undefined(scope).into();
        local_value.into()
      }
      NativeType::U8 => {
        let local_value: v8::Local<v8::Value> =
          v8::Integer::new_from_unsigned(scope, self.u8_value as u32).into();
        local_value.into()
      }
      NativeType::I8 => {
        let local_value: v8::Local<v8::Value> =
          v8::Integer::new(scope, self.i8_value as i32).into();
        local_value.into()
      }
      NativeType::U16 => {
        let local_value: v8::Local<v8::Value> =
          v8::Integer::new_from_unsigned(scope, self.u16_value as u32).into();
        local_value.into()
      }
      NativeType::I16 => {
        let local_value: v8::Local<v8::Value> =
          v8::Integer::new(scope, self.i16_value as i32).into();
        local_value.into()
      }
      NativeType::U32 => {
        let local_value: v8::Local<v8::Value> =
          v8::Integer::new_from_unsigned(scope, self.u32_value).into();
        local_value.into()
      }
      NativeType::I32 => {
        let local_value: v8::Local<v8::Value> =
          v8::Integer::new(scope, self.i32_value).into();
        local_value.into()
      }
      NativeType::U64 => {
        let local_value: v8::Local<v8::Value> =
          v8::BigInt::new_from_u64(scope, self.u64_value).into();
        local_value.into()
      }
      NativeType::I64 => {
        let local_value: v8::Local<v8::Value> =
          v8::BigInt::new_from_i64(scope, self.i64_value).into();
        local_value.into()
      }
      NativeType::USize => {
        let local_value: v8::Local<v8::Value> =
          v8::BigInt::new_from_u64(scope, self.usize_value as u64).into();
        local_value.into()
      }
      NativeType::ISize => {
        let local_value: v8::Local<v8::Value> =
          v8::BigInt::new_from_i64(scope, self.isize_value as i64).into();
        local_value.into()
      }
      NativeType::F32 => {
        let local_value: v8::Local<v8::Value> =
          v8::Number::new(scope, self.f32_value as f64).into();
        local_value.into()
      }
      NativeType::F64 => {
        let local_value: v8::Local<v8::Value> =
          v8::Number::new(scope, self.f64_value).into();
        local_value.into()
      }
      NativeType::Pointer | NativeType::Function => {
        let local_value: v8::Local<v8::Value> =
          v8::BigInt::new_from_u64(scope, self.pointer as u64).into();
        local_value.into()
      }
    }
  }
}

unsafe impl Send for NativeValue {}

#[derive(Serialize, Debug, Clone, Copy)]
struct U32x2(u32, u32);

impl From<u64> for U32x2 {
  fn from(value: u64) -> Self {
    Self((value >> 32) as u32, value as u32)
  }
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForeignFunction {
  name: Option<String>,
  parameters: Vec<NativeType>,
  result: NativeType,
}

// ForeignStatic's name and type fields are read and used by
// serde_v8 to determine which variant a ForeignSymbol is.
// They are not used beyond that and are thus marked with underscores.
#[derive(Deserialize, Debug)]
struct ForeignStatic {
  #[serde(rename(deserialize = "name"))]
  _name: Option<String>,
  #[serde(rename(deserialize = "type"))]
  _type: String,
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum ForeignSymbol {
  ForeignFunction(ForeignFunction),
  ForeignStatic(ForeignStatic),
}

#[derive(Deserialize, Debug)]
struct FfiLoadArgs {
  path: String,
  symbols: HashMap<String, ForeignSymbol>,
}

// `path` is only used on Windows.
#[allow(unused_variables)]
pub(crate) fn format_error(e: dlopen::Error, path: String) -> String {
  match e {
    #[cfg(target_os = "windows")]
    // This calls FormatMessageW with library path
    // as replacement for the insert sequences.
    // Unlike libstd which passes the FORMAT_MESSAGE_IGNORE_INSERTS
    // flag without any arguments.
    //
    // https://github.com/denoland/deno/issues/11632
    dlopen::Error::OpeningLibraryError(e) => {
      use std::ffi::OsStr;
      use std::os::windows::ffi::OsStrExt;
      use winapi::shared::minwindef::DWORD;
      use winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER;
      use winapi::um::errhandlingapi::GetLastError;
      use winapi::um::winbase::FormatMessageW;
      use winapi::um::winbase::FORMAT_MESSAGE_ARGUMENT_ARRAY;
      use winapi::um::winbase::FORMAT_MESSAGE_FROM_SYSTEM;
      use winapi::um::winnt::LANG_SYSTEM_DEFAULT;
      use winapi::um::winnt::MAKELANGID;
      use winapi::um::winnt::SUBLANG_SYS_DEFAULT;

      let err_num = match e.raw_os_error() {
        Some(err_num) => err_num,
        // This should never hit unless dlopen changes its error type.
        None => return e.to_string(),
      };

      // Language ID (0x0800)
      let lang_id =
        MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT) as DWORD;

      let mut buf = vec![0; 500];

      let path = OsStr::new(&path)
        .encode_wide()
        .chain(Some(0).into_iter())
        .collect::<Vec<_>>();

      let arguments = [path.as_ptr()];

      loop {
        unsafe {
          let length = FormatMessageW(
            FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
            std::ptr::null_mut(),
            err_num as DWORD,
            lang_id as DWORD,
            buf.as_mut_ptr(),
            buf.len() as DWORD,
            arguments.as_ptr() as _,
          );

          if length == 0 {
            let err_num = GetLastError();
            if err_num == ERROR_INSUFFICIENT_BUFFER {
              buf.resize(buf.len() * 2, 0);
              continue;
            }

            // Something went wrong, just return the original error.
            return e.to_string();
          }

          let msg = String::from_utf16_lossy(&buf[..length as usize]);
          return msg;
        }
      }
    }
    _ => e.to_string(),
  }
}

#[op]
fn op_ffi_load<FP>(
  state: &mut deno_core::OpState,
  args: FfiLoadArgs,
) -> Result<ResourceId, AnyError>
where
  FP: FfiPermissions + 'static,
{
  let path = args.path;

  check_unstable(state, "Deno.dlopen");
  let permissions = state.borrow_mut::<FP>();
  permissions.check(Some(&PathBuf::from(&path)))?;

  let lib = Library::open(&path).map_err(|e| {
    dlopen::Error::OpeningLibraryError(std::io::Error::new(
      std::io::ErrorKind::Other,
      format_error(e, path),
    ))
  })?;

  let mut resource = DynamicLibraryResource {
    lib,
    symbols: HashMap::new(),
  };

  for (symbol, foreign_symbol) in args.symbols {
    match foreign_symbol {
      ForeignSymbol::ForeignStatic(_) => {
        // No-op: Statics will be handled separately and are not part of the Rust-side resource.
      }
      ForeignSymbol::ForeignFunction(foreign_fn) => {
        resource.register(symbol, foreign_fn)?;
      }
    }
  }

  Ok(state.resource_table.add(resource))
}

fn ffi_parse_args<'scope>(
  scope: &mut v8::HandleScope<'scope>,
  args: serde_v8::Value<'scope>,
  parameter_types: &[NativeType],
) -> Result<Vec<NativeValue>, AnyError>
where
  'scope: 'scope,
{
  if parameter_types.is_empty() {
    return Ok(vec![]);
  }

  let args = v8::Local::<v8::Array>::try_from(args.v8_value)
    .map_err(|_| type_error("Invalid FFI parameters, expected Array"))?;
  let mut ffi_args: Vec<NativeValue> =
    Vec::with_capacity(parameter_types.len());

  for (index, native_type) in parameter_types.iter().enumerate() {
    let value = args.get_index(scope, index as u32).unwrap();
    match native_type {
      NativeType::Void => {
        unreachable!();
      }
      NativeType::U8 => {
        let value = value
          .uint32_value(scope)
          .ok_or_else(|| type_error("Invalid FFI u8 type, expected number"))?
          as u8;
        ffi_args.push(NativeValue { u8_value: value });
      }
      NativeType::I8 => {
        let value = value
          .int32_value(scope)
          .ok_or_else(|| type_error("Invalid FFI i8 type, expected number"))?
          as i8;
        ffi_args.push(NativeValue { i8_value: value });
      }
      NativeType::U16 => {
        let value = value
          .uint32_value(scope)
          .ok_or_else(|| type_error("Invalid FFI u16 type, expected number"))?
          as u16;
        ffi_args.push(NativeValue { u16_value: value });
      }
      NativeType::I16 => {
        let value = value
          .int32_value(scope)
          .ok_or_else(|| type_error("Invalid FFI i16 type, expected number"))?
          as i16;
        ffi_args.push(NativeValue { i16_value: value });
      }
      NativeType::U32 => {
        let value = value
          .uint32_value(scope)
          .ok_or_else(|| type_error("Invalid FFI u32 type, expected number"))?
          as u32;
        ffi_args.push(NativeValue { u32_value: value });
      }
      NativeType::I32 => {
        let value = value
          .int32_value(scope)
          .ok_or_else(|| type_error("Invalid FFI i32 type, expected number"))?
          as i32;
        ffi_args.push(NativeValue { i32_value: value });
      }
      NativeType::U64 => {
        let value: u64 =
          if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
            value.u64_value().0
          } else {
            value.integer_value(scope).ok_or_else(|| {
              type_error("Invalid FFI u64 type, expected number")
            })? as u64
          };
        ffi_args.push(NativeValue { u64_value: value });
      }
      NativeType::I64 => {
        let value: i64 =
          if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
            value.i64_value().0
          } else {
            value.integer_value(scope).ok_or_else(|| {
              type_error("Invalid FFI i64 type, expected number")
            })? as i64
          };
        ffi_args.push(NativeValue { i64_value: value });
      }
      NativeType::USize => {
        let value: usize =
          if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
            value.u64_value().0 as usize
          } else {
            value.integer_value(scope).ok_or_else(|| {
              type_error("Invalid FFI usize type, expected number")
            })? as usize
          };
        ffi_args.push(NativeValue { usize_value: value });
      }
      NativeType::ISize => {
        let value: isize =
          if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
            value.i64_value().0 as isize
          } else {
            value.integer_value(scope).ok_or_else(|| {
              type_error("Invalid FFI isize type, expected number")
            })? as isize
          };
        ffi_args.push(NativeValue { isize_value: value });
      }
      NativeType::F32 => {
        let value = value
          .number_value(scope)
          .ok_or_else(|| type_error("Invalid FFI f32 type, expected number"))?
          as f32;
        ffi_args.push(NativeValue { f32_value: value });
      }
      NativeType::F64 => {
        let value = value
          .number_value(scope)
          .ok_or_else(|| type_error("Invalid FFI f64 type, expected number"))?
          as f64;
        ffi_args.push(NativeValue { f64_value: value });
      }
      NativeType::Pointer => {
        if value.is_null() {
          let value: *const u8 = ptr::null();
          ffi_args.push(NativeValue { pointer: value })
        } else if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
          let value = value.u64_value().0 as *const u8;
          ffi_args.push(NativeValue { pointer: value });
        } else if let Ok(value) =
          v8::Local::<v8::ArrayBufferView>::try_from(value)
        {
          let byte_offset = value.byte_offset();
          let backing_store = value
            .buffer(scope)
            .ok_or_else(|| {
              type_error(
                "Invalid FFI ArrayBufferView, expected data in the buffer",
              )
            })?
            .get_backing_store();
          let pointer = &backing_store[byte_offset] as *const _ as *const u8;
          ffi_args.push(NativeValue { pointer });
        } else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value)
        {
          let backing_store = value.get_backing_store();
          let pointer = &backing_store as *const _ as *const u8;
          ffi_args.push(NativeValue { pointer });
        } else {
          return Err(type_error("Invalid FFI pointer type, expected null, BigInt, ArrayBuffer, or ArrayBufferView"));
        }
      }
      NativeType::Function => {
        if value.is_null() {
          let value: *const u8 = ptr::null();
          ffi_args.push(NativeValue { pointer: value })
        } else if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
          let value = value.u64_value().0 as *const u8;
          ffi_args.push(NativeValue { pointer: value });
        } else {
          return Err(type_error(
            "Invalid FFI function type, expected null, or BigInt",
          ));
        }
      }
    }
  }

  Ok(ffi_args)
}

fn ffi_call(
  call_args: Vec<NativeValue>,
  cif: &libffi::middle::Cif,
  fun_ptr: libffi::middle::CodePtr,
  parameter_types: &[NativeType],
  result_type: NativeType,
) -> Result<NativeValue, AnyError> {
  let call_args: Vec<Arg> = call_args
    .iter()
    .enumerate()
    .map(|(index, ffi_arg)| unsafe {
      ffi_arg.as_arg(*parameter_types.get(index).unwrap())
    })
    .collect();

  Ok(match result_type {
    NativeType::Void => NativeValue {
      void_value: unsafe { cif.call::<()>(fun_ptr, &call_args) },
    },
    NativeType::U8 => NativeValue {
      u8_value: unsafe { cif.call::<u8>(fun_ptr, &call_args) },
    },
    NativeType::I8 => NativeValue {
      i8_value: unsafe { cif.call::<i8>(fun_ptr, &call_args) },
    },
    NativeType::U16 => NativeValue {
      u16_value: unsafe { cif.call::<u16>(fun_ptr, &call_args) },
    },
    NativeType::I16 => NativeValue {
      i16_value: unsafe { cif.call::<i16>(fun_ptr, &call_args) },
    },
    NativeType::U32 => NativeValue {
      u32_value: unsafe { cif.call::<u32>(fun_ptr, &call_args) },
    },
    NativeType::I32 => NativeValue {
      i32_value: unsafe { cif.call::<i32>(fun_ptr, &call_args) },
    },
    NativeType::U64 => NativeValue {
      u64_value: unsafe { cif.call::<u64>(fun_ptr, &call_args) },
    },
    NativeType::I64 => NativeValue {
      i64_value: unsafe { cif.call::<i64>(fun_ptr, &call_args) },
    },
    NativeType::USize => NativeValue {
      usize_value: unsafe { cif.call::<usize>(fun_ptr, &call_args) },
    },
    NativeType::ISize => NativeValue {
      isize_value: unsafe { cif.call::<isize>(fun_ptr, &call_args) },
    },
    NativeType::F32 => NativeValue {
      f32_value: unsafe { cif.call::<f32>(fun_ptr, &call_args) },
    },
    NativeType::F64 => NativeValue {
      f64_value: unsafe { cif.call::<f64>(fun_ptr, &call_args) },
    },
    NativeType::Pointer | NativeType::Function => NativeValue {
      pointer: unsafe { cif.call::<*const u8>(fun_ptr, &call_args) },
    },
  })
}

struct UnsafeCallbackResource {
  // Closure is never directly touched, but it keeps the C callback alive
  // until `close()` method is called.
  #[allow(dead_code)]
  closure: libffi::middle::Closure<'static>,
  info: *const CallbackInfo,
}

impl Resource for UnsafeCallbackResource {
  fn name(&self) -> Cow<str> {
    "unsafecallback".into()
  }

  fn close(self: Rc<Self>) {
    // SAFETY: This drops the closure and the callback info associated with it.
    // Any retained function pointers to the closure become dangling pointers.
    // It is up to the user to know that it is safe to call the `close()` on the
    // UnsafeCallback instance.
    unsafe {
      let info = Box::from_raw(self.info as *mut CallbackInfo);
      let isolate = info.isolate.as_mut().unwrap();
      v8::Global::from_raw(isolate, info.callback);
      v8::Global::from_raw(isolate, info.context);
    }
  }
}

struct CallbackInfo {
  pub callback: NonNull<v8::Function>,
  pub context: NonNull<v8::Context>,
  pub isolate: *mut v8::Isolate,
}

unsafe extern "C" fn deno_ffi_callback(
  cif: &libffi::low::ffi_cif,
  result: &mut c_void,
  args: *const *const c_void,
  info: &CallbackInfo,
) {
  let isolate = &mut *info.isolate;
  let callback = v8::Global::from_raw(isolate, info.callback);
  let context = std::mem::transmute::<
    NonNull<v8::Context>,
    v8::Local<v8::Context>,
  >(info.context);
  IS_ISOLATE_THREAD.with(|is_event_loop_thread| {
    if !(*is_event_loop_thread.borrow()) {
      // Call from another thread, not yet supported.
      eprintln!(
        "Calling Deno FFI's callbacks from other threads is not supported"
      );
      std::process::exit(1);
    }
  });
  // Call from main thread. If this callback is being triggered due to a
  // function call coming from Deno itself, then this callback will build
  // ontop of that stack.
  // If this callback is being triggered outside of Deno (for example from a
  // signal handler) then this will either create an empty new stack if
  // Deno currently has nothing running and is waiting for promises to resolve,
  // or will (very incorrectly) build ontop of whatever stack exists.
  // The callback will even be called through from a `while (true)` liveloop, but
  // it somehow cannot change the values that the loop sees, even if they both
  // refer the same `let bool_value`.
  let mut cb_scope = v8::CallbackScope::new(context);
  let mut scope = v8::HandleScope::new(&mut cb_scope);
  let func = callback.open(&mut scope);
  let result = result as *mut c_void;
  let repr: &[*mut ffi_type] =
    std::slice::from_raw_parts(cif.arg_types, cif.nargs as usize);
  let vals: &[*const c_void] =
    std::slice::from_raw_parts(args, cif.nargs as usize);

  let mut params: Vec<v8::Local<v8::Value>> = vec![];
  for (repr, val) in repr.iter().zip(vals) {
    let value: v8::Local<v8::Value> = match (*(*repr)).type_ as _ {
      FFI_TYPE_FLOAT => {
        let value = *((*val) as *const f32);
        v8::Number::new(&mut scope, value as f64).into()
      }
      FFI_TYPE_DOUBLE => {
        let value = *((*val) as *const f64);
        v8::Number::new(&mut scope, value).into()
      }
      FFI_TYPE_SINT8 => {
        let value = *((*val) as *const i8);
        v8::Integer::new(&mut scope, value as i32).into()
      }
      FFI_TYPE_UINT8 => {
        let value = *((*val) as *const u8);
        v8::Integer::new_from_unsigned(&mut scope, value as u32).into()
      }
      FFI_TYPE_SINT16 => {
        let value = *((*val) as *const i16);
        v8::Integer::new(&mut scope, value as i32).into()
      }
      FFI_TYPE_UINT16 => {
        let value = *((*val) as *const u16);
        v8::Integer::new_from_unsigned(&mut scope, value as u32).into()
      }
      FFI_TYPE_INT | FFI_TYPE_SINT32 => {
        let value = *((*val) as *const i32);
        v8::Integer::new(&mut scope, value).into()
      }
      FFI_TYPE_UINT32 => {
        let value = *((*val) as *const u32);
        v8::Integer::new_from_unsigned(&mut scope, value).into()
      }
      FFI_TYPE_SINT64 => {
        let result = *((*val) as *const i64);
        v8::BigInt::new_from_i64(&mut scope, result).into()
      }
      FFI_TYPE_POINTER | FFI_TYPE_STRUCT | FFI_TYPE_UINT64 => {
        let result = *((*val) as *const u64);
        v8::BigInt::new_from_u64(&mut scope, result).into()
      }
      FFI_TYPE_VOID => v8::undefined(&mut scope).into(),
      _ => {
        unreachable!()
      }
    };
    params.push(value);
  }

  let recv = v8::undefined(&mut scope);
  let call_result = func.call(&mut scope, recv.into(), &params);
  std::mem::forget(callback);

  if call_result.is_none() {
    // JS function threw an exception. Set the return value to zero and return.
    // The exception continue propagating up the call chain when the event loop
    // resumes.
    match (*cif.rtype).type_ as _ {
      FFI_TYPE_INT | FFI_TYPE_SINT32 | FFI_TYPE_UINT32 => {
        // zero is equal for signed and unsigned alike
        *(result as *mut u32) = 0;
      }
      FFI_TYPE_FLOAT => {
        *(result as *mut f32) = 0.0;
      }
      FFI_TYPE_DOUBLE => {
        *(result as *mut f64) = 0.0;
      }
      FFI_TYPE_SINT8 | FFI_TYPE_UINT8 => {
        // zero is equal for signed and unsigned alike
        *(result as *mut u8) = 0;
      }
      FFI_TYPE_SINT16 | FFI_TYPE_UINT16 => {
        // zero is equal for signed and unsigned alike
        *(result as *mut u16) = 0;
      }
      FFI_TYPE_POINTER | FFI_TYPE_STRUCT | FFI_TYPE_UINT64
      | FFI_TYPE_SINT64 => {
        *(result as *mut u64) = 0;
      }
      FFI_TYPE_VOID => {
        // nop
      }
      _ => {
        unreachable!();
      }
    };

    return;
  }
  let value = call_result.unwrap();

  match (*cif.rtype).type_ as _ {
    FFI_TYPE_INT | FFI_TYPE_SINT32 => {
      *(result as *mut i32) = value
        .int32_value(&mut scope)
        .expect("Unable to deserialize result parameter.")
        as i32;
    }
    FFI_TYPE_FLOAT => {
      *(result as *mut f32) = value
        .number_value(&mut scope)
        .expect("Unable to deserialize result parameter.")
        as f32;
    }
    FFI_TYPE_DOUBLE => {
      *(result as *mut f64) = value
        .number_value(&mut scope)
        .expect("Unable to deserialize result parameter.");
    }
    FFI_TYPE_POINTER | FFI_TYPE_STRUCT => {
      if let Ok(value) = v8::Local::<v8::ArrayBufferView>::try_from(value) {
        let byte_offset = value.byte_offset();
        let backing_store = value
          .buffer(&mut scope)
          .expect("Unable to deserialize result parameter.")
          .get_backing_store();
        let pointer = &backing_store[byte_offset] as *const _ as *const u8;
        *(result as *mut *const u8) = pointer;
      } else if let Ok(value) = v8::Local::<v8::ArrayBuffer>::try_from(value) {
        let backing_store = value.get_backing_store();
        let pointer = &backing_store as *const _ as *const u8;
        *(result as *mut *const u8) = pointer;
      } else if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
        *(result as *mut u64) = value.u64_value().0;
      } else if value.is_null() {
        *(result as *mut *const c_void) = ptr::null();
      } else {
        // Fallthrough: Probably someone returned a number but this could
        // also be eg. a string. This is essentially UB.
        *(result as *mut u64) = value
          .integer_value(&mut scope)
          .expect("Unable to deserialize result parameter.")
          as u64;
      }
    }
    FFI_TYPE_SINT8 => {
      *(result as *mut i8) = value
        .int32_value(&mut scope)
        .expect("Unable to deserialize result parameter.")
        as i8;
    }
    FFI_TYPE_UINT8 => {
      *(result as *mut u8) = value
        .uint32_value(&mut scope)
        .expect("Unable to deserialize result parameter.")
        as u8;
    }
    FFI_TYPE_SINT16 => {
      *(result as *mut i16) = value
        .int32_value(&mut scope)
        .expect("Unable to deserialize result parameter.")
        as i16;
    }
    FFI_TYPE_UINT16 => {
      *(result as *mut u16) = value
        .uint32_value(&mut scope)
        .expect("Unable to deserialize result parameter.")
        as u16;
    }
    FFI_TYPE_UINT32 => {
      *(result as *mut u32) = value
        .uint32_value(&mut scope)
        .expect("Unable to deserialize result parameter.");
    }
    FFI_TYPE_SINT64 => {
      if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
        *(result as *mut i64) = value.i64_value().0;
      } else {
        *(result as *mut i64) = value
          .integer_value(&mut scope)
          .expect("Unable to deserialize result parameter.")
          as i64;
      }
    }
    FFI_TYPE_UINT64 => {
      if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
        *(result as *mut u64) = value.u64_value().0;
      } else {
        *(result as *mut u64) = value
          .integer_value(&mut scope)
          .expect("Unable to deserialize result parameter.")
          as u64;
      }
    }
    FFI_TYPE_VOID => {
      // nop
    }
    _ => {
      unreachable!();
    }
  };
}

#[derive(Deserialize)]
struct RegisterCallbackArgs {
  parameters: Vec<NativeType>,
  result: NativeType,
}

#[op(v8)]
fn op_ffi_unsafe_callback_create<FP, 'scope>(
  state: &mut deno_core::OpState,
  scope: &mut v8::HandleScope<'scope>,
  args: RegisterCallbackArgs,
  cb: serde_v8::Value<'scope>,
) -> Result<serde_v8::Value<'scope>, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafeCallback");
  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  let v8_value = cb.v8_value;
  let cb = v8::Local::<v8::Function>::try_from(v8_value)?;

  let isolate: *mut v8::Isolate = &mut *scope as &mut v8::Isolate;
  let callback = v8::Global::new(scope, cb).into_raw();
  let current_context = scope.get_current_context();
  let context = v8::Global::new(scope, current_context).into_raw();

  let info = Box::leak(Box::new(CallbackInfo {
    callback,
    context,
    isolate,
  }));
  let cif = Cif::new(
    args.parameters.into_iter().map(libffi::middle::Type::from),
    libffi::middle::Type::from(args.result),
  );

  let closure = libffi::middle::Closure::new(cif, deno_ffi_callback, info);
  let ptr = *closure.code_ptr() as usize as u64;
  let resource = UnsafeCallbackResource { closure, info };
  let rid = state.resource_table.add(resource);

  let rid_local = v8::Integer::new_from_unsigned(scope, rid);
  let ptr_local = v8::BigInt::new_from_u64(scope, ptr);
  let array = v8::Array::new(scope, 2);
  array.set_index(scope, 0, rid_local.into());
  array.set_index(scope, 1, ptr_local.into());
  let array_value: v8::Local<v8::Value> = array.into();

  Ok(array_value.into())
}

#[op(v8)]
fn op_ffi_call_ptr<FP, 'scope>(
  scope: &mut v8::HandleScope<'scope>,
  state: Rc<RefCell<deno_core::OpState>>,
  pointer: u64,
  def: ForeignFunction,
  parameters: serde_v8::Value<'scope>,
) -> Result<serde_v8::Value<'scope>, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable2(&state, "Deno.UnsafeFnPointer#call");
  {
    let mut state = state.borrow_mut();
    let permissions = state.borrow_mut::<FP>();
    permissions.check(None)?;
  };

  let symbol = PtrSymbol::new(pointer, &def);
  let call_args = ffi_parse_args(scope, parameters, &def.parameters)?;

  let result = ffi_call(
    call_args,
    &symbol.cif,
    symbol.ptr,
    &def.parameters,
    def.result,
  )?;
  // SAFETY: Same return type declared to libffi; trust user to have it right beyond that.
  let result = unsafe { result.to_v8(scope, def.result) };
  Ok(result)
}

#[op(v8)]
fn op_ffi_call_ptr_nonblocking<'scope, FP>(
  scope: &mut v8::HandleScope<'scope>,
  state: Rc<RefCell<deno_core::OpState>>,
  pointer: u64,
  def: ForeignFunction,
  parameters: serde_v8::Value<'scope>,
) -> Result<impl Future<Output = Result<Value, AnyError>>, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable2(&state, "Deno.UnsafeFnPointer#call");
  {
    let mut state = state.borrow_mut();
    let permissions = state.borrow_mut::<FP>();
    permissions.check(None)?;
  };

  let symbol = PtrSymbol::new(pointer, &def);
  let call_args = ffi_parse_args(scope, parameters, &def.parameters)?;

  let join_handle = tokio::task::spawn_blocking(move || {
    let PtrSymbol { cif, ptr } = symbol.clone();
    ffi_call(call_args, &cif, ptr, &def.parameters, def.result)
  });

  Ok(async move {
    let result = join_handle
      .await
      .map_err(|err| anyhow!("Nonblocking FFI call failed: {}", err))??;
    // SAFETY: Same return type declared to libffi; trust user to have it right beyond that.
    Ok(unsafe { result.to_value(def.result) })
  })
}

#[op(v8)]
fn op_ffi_get_static<'scope>(
  scope: &mut v8::HandleScope<'scope>,
  state: &mut deno_core::OpState,
  rid: ResourceId,
  name: String,
  static_type: NativeType,
) -> Result<serde_v8::Value<'scope>, AnyError> {
  let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;

  let data_ptr = resource.get_static(name)? as *const u8;

  Ok(match static_type {
    NativeType::Void => {
      return Err(type_error("Invalid FFI static type 'void'"));
    }
    NativeType::U8 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const u8) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::I8 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const i8) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::U16 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const u16) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::I16 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const i16) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::U32 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const u32) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::I32 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const i32) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::U64 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const u64) };
      let big_int = v8::BigInt::new_from_u64(scope, result);
      serde_v8::from_v8(scope, big_int.into())?
    }
    NativeType::I64 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const i64) };
      let big_int = v8::BigInt::new_from_i64(scope, result);
      serde_v8::from_v8(scope, big_int.into())?
    }
    NativeType::USize => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const usize) };
      let big_int = v8::BigInt::new_from_u64(scope, result as u64);
      serde_v8::from_v8(scope, big_int.into())?
    }
    NativeType::ISize => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const isize) };
      let big_int = v8::BigInt::new_from_i64(scope, result as i64);
      serde_v8::from_v8(scope, big_int.into())?
    }
    NativeType::F32 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const f32) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::F64 => {
      let result = unsafe { ptr::read_unaligned(data_ptr as *const f64) };
      let number = v8::Number::new(scope, result as f64);
      serde_v8::from_v8(scope, number.into())?
    }
    NativeType::Pointer | NativeType::Function => {
      let result = data_ptr as *const u8 as u64;
      let big_int = v8::BigInt::new_from_u64(scope, result);
      serde_v8::from_v8(scope, big_int.into())?
    }
  })
}

#[op(v8)]
fn op_ffi_call<'scope>(
  scope: &mut v8::HandleScope<'scope>,
  state: Rc<RefCell<deno_core::OpState>>,
  rid: ResourceId,
  symbol: String,
  parameters: serde_v8::Value<'scope>,
) -> Result<serde_v8::Value<'scope>, AnyError> {
  let symbol = {
    let state = &mut state.borrow();
    let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;

    resource
      .symbols
      .get(&symbol)
      .ok_or_else(|| type_error("Invalid FFI symbol name"))?
      .clone()
  };

  let call_args = ffi_parse_args(scope, parameters, &symbol.parameter_types)?;

  let result = ffi_call(
    call_args,
    &symbol.cif,
    symbol.ptr,
    &symbol.parameter_types,
    symbol.result_type,
  )?;
  // SAFETY: Same return type declared to libffi; trust user to have it right beyond that.
  let result = unsafe { result.to_v8(scope, symbol.result_type) };
  Ok(result)
}

/// A non-blocking FFI call.
#[op(v8)]
fn op_ffi_call_nonblocking<'scope>(
  scope: &mut v8::HandleScope<'scope>,
  state: Rc<RefCell<deno_core::OpState>>,
  rid: ResourceId,
  symbol: String,
  parameters: serde_v8::Value<'scope>,
) -> Result<impl Future<Output = Result<Value, AnyError>> + 'static, AnyError> {
  let symbol = {
    let state = state.borrow();
    let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;
    let symbols = &resource.symbols;
    symbols
      .get(&symbol)
      .ok_or_else(|| type_error("Invalid FFI symbol name"))?
      .clone()
  };

  let call_args = ffi_parse_args(scope, parameters, &symbol.parameter_types)?;

  let result_type = symbol.result_type;
  let join_handle = tokio::task::spawn_blocking(move || {
    let Symbol {
      cif,
      ptr,
      parameter_types,
      ..
    } = symbol.clone();
    ffi_call(call_args, &cif, ptr, &parameter_types, result_type)
  });

  Ok(async move {
    let result = join_handle
      .await
      .map_err(|err| anyhow!("Nonblocking FFI call failed: {}", err))??;
    // SAFETY: Same return type declared to libffi; trust user to have it right beyond that.
    Ok(unsafe { result.to_value(result_type) })
  })
}

#[op(v8)]
fn op_ffi_ptr_of<FP, 'scope>(
  scope: &mut v8::HandleScope<'scope>,
  state: &mut deno_core::OpState,
  buf: ZeroCopyBuf,
) -> Result<serde_v8::Value<'scope>, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointer#of");
  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  let big_int: v8::Local<v8::Value> =
    v8::BigInt::new_from_u64(scope, buf.as_ptr() as u64).into();
  Ok(big_int.into())
}

#[op]
fn op_ffi_buf_copy_into<FP>(
  state: &mut deno_core::OpState,
  src: u64,
  mut dst: ZeroCopyBuf,
  len: usize,
) -> Result<(), AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#copyInto");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  if dst.len() < len {
    Err(range_error(
      "Destination length is smaller than source length",
    ))
  } else {
    let src = src as *const u8;
    unsafe { ptr::copy(src, dst.as_mut_ptr(), len) };
    Ok(())
  }
}

#[op]
fn op_ffi_cstr_read<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<String, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getCString");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  let ptr = ptr as *const c_char;
  Ok(unsafe { CStr::from_ptr(ptr) }.to_str()?.to_string())
}

#[op]
fn op_ffi_read_u8<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<u8, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getUint8");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const u8) })
}

#[op]
fn op_ffi_read_i8<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<i8, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getInt8");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const i8) })
}

#[op]
fn op_ffi_read_u16<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<u16, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getUint16");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const u16) })
}

#[op]
fn op_ffi_read_i16<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<i16, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getInt16");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const i16) })
}

#[op]
fn op_ffi_read_u32<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<u32, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getUint32");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const u32) })
}

#[op]
fn op_ffi_read_i32<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<i32, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getInt32");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const i32) })
}

#[op(v8)]
fn op_ffi_read_u64<FP, 'scope>(
  scope: &mut v8::HandleScope<'scope>,
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<serde_v8::Value<'scope>, AnyError>
where
  FP: FfiPermissions + 'static,
  'scope: 'scope,
{
  check_unstable(state, "Deno.UnsafePointerView#getBigUint64");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  let result = unsafe { ptr::read_unaligned(ptr as *const u64) };

  let big_int: v8::Local<v8::Value> =
    v8::BigInt::new_from_u64(scope, result).into();
  Ok(big_int.into())
}

#[op]
fn op_ffi_read_f32<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<f32, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getFloat32");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const f32) })
}

#[op]
fn op_ffi_read_f64<FP>(
  state: &mut deno_core::OpState,
  ptr: u64,
) -> Result<f64, AnyError>
where
  FP: FfiPermissions + 'static,
{
  check_unstable(state, "Deno.UnsafePointerView#getFloat64");

  let permissions = state.borrow_mut::<FP>();
  permissions.check(None)?;

  Ok(unsafe { ptr::read_unaligned(ptr as *const f64) })
}

#[cfg(test)]
mod tests {
  #[cfg(target_os = "windows")]
  #[test]
  fn test_format_error() {
    use super::format_error;

    // BAD_EXE_FORMAT
    let err = dlopen::Error::OpeningLibraryError(
      std::io::Error::from_raw_os_error(0x000000C1),
    );
    assert_eq!(
      format_error(err, "foo.dll".to_string()),
      "foo.dll is not a valid Win32 application.\r\n".to_string(),
    );
  }
}