forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8FastArgImpl.cs
More file actions
808 lines (654 loc) · 27.9 KB
/
Copy pathV8FastArgImpl.cs
File metadata and controls
808 lines (654 loc) · 27.9 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Microsoft.ClearScript.Util;
using Microsoft.ClearScript.V8.SplitProxy;
namespace Microsoft.ClearScript.V8.FastProxy
{
internal static class V8FastArgImpl
{
public static object InitializeObject(V8ScriptEngine engine, in V8Value.FastArg arg)
{
if (arg.IsUndefined())
{
var importValue = engine.UndefinedImportValue;
return (importValue is not Undefined) ? importValue : Nonexistent.Value;
}
if (arg.IsNull())
{
return engine.NullImportValue ?? Nonexistent.Value;
}
if (arg.TryGetV8Object(out var v8Object))
{
return engine.MarshalToHost(v8Object, false);
}
if (arg.TryGetHostObject(out var hostObject))
{
return engine.MarshalToHost(hostObject, false);
}
return Nonexistent.Value;
}
public static bool IsFalsy(in V8Value.FastArg arg, object obj)
{
if (IsNull(arg, obj) || IsUndefined(arg, obj))
{
return true;
}
if (TryGet(arg, obj, out bool boolValue))
{
return !boolValue;
}
if (TryGet(arg, obj, out double doubleValue))
{
return (doubleValue == 0) || double.IsNaN(doubleValue);
}
if (TryGet(arg, obj, out BigInteger bigIntegerValue))
{
return bigIntegerValue.IsZero;
}
if (TryGet(arg, obj, out ReadOnlySpan<char> charSpanValue))
{
return charSpanValue.IsEmpty;
}
return false;
}
public static bool IsTruthy(in V8Value.FastArg arg, object obj) => !IsFalsy(arg, obj);
public static bool IsUndefined(in V8Value.FastArg arg, object obj) => (arg.IsUndefined() && (obj is Nonexistent)) || (obj is Undefined);
public static bool IsNull(in V8Value.FastArg arg, object obj) => (arg.IsNull() && (obj is Nonexistent)) || (obj is null);
public static bool TryGet(in V8Value.FastArg arg, object obj, out bool value) => arg.TryGetBoolean(out value) || TryGetFromObject(obj, out value, out _);
public static bool TryGet(in V8Value.FastArg arg, object obj, out char value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out sbyte value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out byte value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out short value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out ushort value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out int value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out uint value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out long value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out ulong value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out float value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out double value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out decimal value) => TryGetNumber(arg, obj, out value);
public static bool TryGet(in V8Value.FastArg arg, object obj, out string value) => arg.TryGetString(out value) || TryGetFromObject(obj, out value, out _);
public static bool TryGet(in V8Value.FastArg arg, object obj, out ReadOnlySpan<char> value)
{
if (arg.TryGetCharSpan(out value))
{
return true;
}
if (TryGetFromObject(obj, out string stringValue, out _))
{
value = stringValue.AsSpan();
return true;
}
return false;
}
public static bool TryGet(in V8Value.FastArg arg, object obj, out DateTime value) => arg.TryGetDateTime(out value) || TryGetFromObject(obj, out value, out _);
public static bool TryGet(in V8Value.FastArg arg, object obj, out BigInteger value) => TryGetNumber(arg, obj, out value);
public static bool TryGet<T>(in V8Value.FastArg arg, object obj, out T? value) where T : struct
{
if (IsNull(arg, obj) || IsUndefined(arg, obj))
{
value = null;
return true;
}
if (TryGet(arg, obj, out T structValue))
{
value = structValue;
return true;
}
value = null;
return false;
}
public static bool TryGet<T>(in V8Value.FastArg arg, object obj, out T value)
{
value = default;
var type = typeof(T);
if (type == typeof(bool))
{
return TryGet(arg, obj, out Unsafe.As<T, bool>(ref value));
}
if (type == typeof(char))
{
return TryGet(arg, obj, out Unsafe.As<T, char>(ref value));
}
if (type == typeof(sbyte))
{
return TryGet(arg, obj, out Unsafe.As<T, sbyte>(ref value));
}
if (type == typeof(byte))
{
return TryGet(arg, obj, out Unsafe.As<T, byte>(ref value));
}
if (type == typeof(short))
{
return TryGet(arg, obj, out Unsafe.As<T, short>(ref value));
}
if (type == typeof(ushort))
{
return TryGet(arg, obj, out Unsafe.As<T, ushort>(ref value));
}
if (type == typeof(int))
{
return TryGet(arg, obj, out Unsafe.As<T, int>(ref value));
}
if (type == typeof(uint))
{
return TryGet(arg, obj, out Unsafe.As<T, uint>(ref value));
}
if (type == typeof(long))
{
return TryGet(arg, obj, out Unsafe.As<T, long>(ref value));
}
if (type == typeof(ulong))
{
return TryGet(arg, obj, out Unsafe.As<T, ulong>(ref value));
}
if (type == typeof(float))
{
return TryGet(arg, obj, out Unsafe.As<T, float>(ref value));
}
if (type == typeof(double))
{
return TryGet(arg, obj, out Unsafe.As<T, double>(ref value));
}
if (type == typeof(decimal))
{
return TryGet(arg, obj, out Unsafe.As<T, decimal>(ref value));
}
if (type == typeof(string))
{
return TryGet(arg, obj, out Unsafe.As<T, string>(ref value));
}
if (type == typeof(DateTime))
{
return TryGet(arg, obj, out Unsafe.As<T, DateTime>(ref value));
}
if (type == typeof(BigInteger))
{
return TryGet(arg, obj, out Unsafe.As<T, BigInteger>(ref value));
}
if (Nullable.GetUnderlyingType(type) is {} underlyingType)
{
return NullableHelpers.TryGet(underlyingType, arg, obj, out value);
}
if (TryGetFromObject(obj, out value, out var isObject))
{
return true;
}
if (isObject)
{
return false;
}
{
if (arg.IsUndefined())
{
if (Undefined.Value is T checkedValue)
{
value = checkedValue;
return true;
}
return false;
}
}
{
if (arg.TryGetBoolean(out var boolValue))
{
if (boolValue is T checkedValue)
{
value = checkedValue;
return true;
}
return false;
}
}
{
if (arg.TryGetNumber(out var doubleValue))
{
if (doubleValue is T checkedValue)
{
value = checkedValue;
return true;
}
return false;
}
}
{
if (arg.TryGetString(out var stringValue))
{
if (stringValue is T checkedValue)
{
value = checkedValue;
return true;
}
return false;
}
}
{
if (arg.TryGetDateTime(out var dateTimeValue))
{
if (dateTimeValue is T checkedValue)
{
value = checkedValue;
return true;
}
return false;
}
}
{
if (arg.TryGetBigInteger(out var bigIntegerValue))
{
if (bigIntegerValue is T checkedValue)
{
value = checkedValue;
return true;
}
return false;
}
}
return false;
}
public static bool GetBoolean(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out bool result), result, kind, name);
public static char GetChar(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out char result), result, kind, name);
public static sbyte GetSByte(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out sbyte result), result, kind, name);
public static byte GetByte(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out byte result), result, kind, name);
public static short GetInt16(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out short result), result, kind, name);
public static ushort GetUInt16(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out ushort result), result, kind, name);
public static int GetInt32(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out int result), result, kind, name);
public static uint GetUInt32(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out uint result), result, kind, name);
public static long GetInt64(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out long result), result, kind, name);
public static ulong GetUInt64(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out ulong result), result, kind, name);
public static float GetSingle(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out float result), result, kind, name);
public static double GetDouble(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out double result), result, kind, name);
public static decimal GetDecimal(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out decimal result), result, kind, name);
public static string GetString(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => IsNull(arg, obj) ? null : ReturnOrThrow(TryGet(arg, obj, out string result), result, kind, name);
public static ReadOnlySpan<char> GetCharSpan(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out ReadOnlySpan<char> result), result, kind, name);
public static DateTime GetDateTime(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out DateTime result), result, kind, name);
public static BigInteger GetBigInteger(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => ReturnOrThrow(TryGet(arg, obj, out BigInteger result), result, kind, name);
public static T? GetNullable<T>(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) where T : struct => ReturnOrThrow(TryGet(arg, obj, out T? result), result, kind, name);
public static T Get<T>(in V8Value.FastArg arg, object obj, V8FastArgKind kind, string name) => (IsNull(arg, obj) && (default(T) is null)) ? default : ReturnOrThrow(TryGet(arg, obj, out T result), result, kind, name);
private static bool TryGetFromObject<T>(object obj, out T value, out bool isObject)
{
isObject = obj is not Nonexistent;
if (isObject)
{
if (obj is T checkedValue)
{
value = checkedValue;
return true;
}
}
value = default;
return false;
}
private static bool TryGetNumber<T>(in V8Value.FastArg arg, object obj, out T value) where T : struct
{
if (arg.TryGetNumber(out var doubleValue))
{
return TryGetNumberFromDouble(doubleValue, out value);
}
if (arg.TryGetBigInteger(out var bigIntegerValue))
{
return TryGetNumberFromBigInteger(bigIntegerValue, out value);
}
if (obj is not Nonexistent)
{
return TryGetNumberFromObject(obj, out value);
}
value = default;
return false;
}
private static bool TryGetNumberFromObject<T>(object obj, out T value) where T : struct
{
if (obj is char charValue)
{
return TryGetNumberFromInt64(charValue, out value);
}
if (obj is sbyte sbyteValue)
{
return TryGetNumberFromInt64(sbyteValue, out value);
}
if (obj is byte byteValue)
{
return TryGetNumberFromInt64(byteValue, out value);
}
if (obj is short shortValue)
{
return TryGetNumberFromInt64(shortValue, out value);
}
if (obj is ushort ushortValue)
{
return TryGetNumberFromInt64(ushortValue, out value);
}
if (obj is int intValue)
{
return TryGetNumberFromInt64(intValue, out value);
}
if (obj is uint uintValue)
{
return TryGetNumberFromInt64(uintValue, out value);
}
if (obj is long longValue)
{
return TryGetNumberFromInt64(longValue, out value);
}
if (obj is ulong ulongValue)
{
return TryGetNumberFromUInt64(ulongValue, out value);
}
if (obj is float floatValue)
{
return TryGetNumberFromDouble(floatValue, out value);
}
if (obj is double doubleValue)
{
return TryGetNumberFromDouble(doubleValue, out value);
}
if (obj is decimal decimalValue)
{
return TryGetNumberFromDecimal(decimalValue, out value);
}
if (obj is BigInteger bigIntegerValue)
{
return TryGetNumberFromBigInteger(bigIntegerValue, out value);
}
value = default;
return false;
}
private static bool TryGetNumberFromInt64<T>(long longValue, out T value) where T : struct
{
if (TryGetIntegerFromInt64(longValue, out value))
{
return true;
}
var type = typeof(T);
if (type == typeof(float))
{
if ((longValue >= -MiscHelpers.MaxInt32InSingle) && (longValue <= MiscHelpers.MaxInt32InSingle))
{
Unsafe.As<T, float>(ref value) = longValue;
return true;
}
}
else if (type == typeof(double))
{
if ((longValue >= -MiscHelpers.MaxInt64InDouble) && (longValue <= MiscHelpers.MaxInt64InDouble))
{
Unsafe.As<T, double>(ref value) = longValue;
return true;
}
}
else if (type == typeof(decimal))
{
Unsafe.As<T, decimal>(ref value) = longValue;
return true;
}
return false;
}
private static bool TryGetNumberFromUInt64<T>(ulong ulongValue, out T value) where T : struct
{
if (TryGetIntegerFromUInt64(ulongValue, out value))
{
return true;
}
var type = typeof(T);
if (type == typeof(float))
{
if (ulongValue <= MiscHelpers.MaxInt32InSingle)
{
Unsafe.As<T, float>(ref value) = ulongValue;
return true;
}
}
else if (type == typeof(double))
{
if (ulongValue <= MiscHelpers.MaxInt64InDouble)
{
Unsafe.As<T, double>(ref value) = ulongValue;
return true;
}
}
else if (type == typeof(decimal))
{
Unsafe.As<T, decimal>(ref value) = ulongValue;
return true;
}
return false;
}
private static bool TryGetNumberFromDouble<T>(double doubleValue, out T value) where T : struct
{
if (Math.Abs(doubleValue) <= MiscHelpers.MaxInt64InDoubleAsDouble)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
var truncatedValue = Math.Truncate(doubleValue);
if ((truncatedValue == doubleValue) && TryGetIntegerFromInt64((long)truncatedValue, out value))
{
return true;
}
// ReSharper restore CompareOfFloatsByEqualityOperator
}
return TryGetFloatingPointFromDouble(doubleValue, out value);
}
private static bool TryGetNumberFromDecimal<T>(decimal decimalValue, out T value) where T : struct
{
if (Math.Abs(decimalValue) <= MiscHelpers.MaxBigIntegerInDecimalAsDecimal)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
var truncatedValue = Math.Truncate(decimalValue);
if ((truncatedValue == decimalValue) && TryGetIntegerFromBigInteger((BigInteger)truncatedValue, out value))
{
return true;
}
// ReSharper restore CompareOfFloatsByEqualityOperator
}
return TryGetFloatingPointFromDecimal(decimalValue, out value);
}
private static bool TryGetNumberFromBigInteger<T>(BigInteger bigIntegerValue, out T value) where T : struct
{
if (TryGetIntegerFromBigInteger(bigIntegerValue, out value))
{
return true;
}
var type = typeof(T);
if (type == typeof(float))
{
if (BigInteger.Abs(bigIntegerValue) <= MiscHelpers.MaxInt32InSingle)
{
Unsafe.As<T, float>(ref value) = (float)bigIntegerValue;
return true;
}
}
else if (type == typeof(double))
{
if (BigInteger.Abs(bigIntegerValue) <= MiscHelpers.MaxInt64InDouble)
{
Unsafe.As<T, double>(ref value) = (double)bigIntegerValue;
return true;
}
}
else if (type == typeof(decimal))
{
if (BigInteger.Abs(bigIntegerValue) <= MiscHelpers.MaxBigIntegerInDecimal)
{
Unsafe.As<T, decimal>(ref value) = (decimal)bigIntegerValue;
return true;
}
}
return false;
}
private static bool TryGetIntegerFromInt64<T>(long longValue, out T value) where T : struct
{
value = default;
var type = typeof(T);
if (type == typeof(char))
{
if ((longValue >= char.MinValue) && (longValue <= char.MaxValue))
{
Unsafe.As<T, char>(ref value) = (char)longValue;
return true;
}
}
else if (type == typeof(sbyte))
{
if ((longValue >= sbyte.MinValue) && (longValue <= sbyte.MaxValue))
{
Unsafe.As<T, sbyte>(ref value) = (sbyte)longValue;
return true;
}
}
else if (type == typeof(byte))
{
if ((longValue >= 0) && (longValue <= byte.MaxValue))
{
Unsafe.As<T, byte>(ref value) = (byte)longValue;
return true;
}
}
else if (type == typeof(short))
{
if ((longValue >= short.MinValue) && (longValue <= short.MaxValue))
{
Unsafe.As<T, short>(ref value) = (short)longValue;
return true;
}
}
else if (type == typeof(ushort))
{
if ((longValue >= 0) && (longValue <= ushort.MaxValue))
{
Unsafe.As<T, ushort>(ref value) = (ushort)longValue;
return true;
}
}
else if (type == typeof(int))
{
if ((longValue >= int.MinValue) && (longValue <= int.MaxValue))
{
Unsafe.As<T, int>(ref value) = (int)longValue;
return true;
}
}
else if (type == typeof(uint))
{
if ((longValue >= 0) && (longValue <= uint.MaxValue))
{
Unsafe.As<T, uint>(ref value) = (uint)longValue;
return true;
}
}
else if (type == typeof(long))
{
Unsafe.As<T, long>(ref value) = longValue;
return true;
}
else if (type == typeof(ulong))
{
if (longValue >= 0)
{
Unsafe.As<T, ulong>(ref value) = (ulong)longValue;
return true;
}
}
else if (type == typeof(BigInteger))
{
Unsafe.As<T, BigInteger>(ref value) = longValue;
return true;
}
return false;
}
private static bool TryGetIntegerFromUInt64<T>(ulong ulongValue, out T value) where T : struct
{
if (ulongValue <= long.MaxValue)
{
return TryGetIntegerFromInt64((long)ulongValue, out value);
}
value = default;
if (typeof(T) == typeof(ulong))
{
Unsafe.As<T, ulong>(ref value) = ulongValue;
return true;
}
if (typeof(T) == typeof(BigInteger))
{
Unsafe.As<T, BigInteger>(ref value) = ulongValue;
return true;
}
return false;
}
private static bool TryGetIntegerFromBigInteger<T>(BigInteger bigIntegerValue, out T value) where T : struct
{
if ((bigIntegerValue >= long.MinValue) && (bigIntegerValue <= long.MaxValue))
{
return TryGetIntegerFromInt64((long)bigIntegerValue, out value);
}
if ((bigIntegerValue >= 0) && (bigIntegerValue <= ulong.MaxValue))
{
return TryGetIntegerFromUInt64((ulong)bigIntegerValue, out value);
}
value = default;
if (typeof(T) == typeof(BigInteger))
{
Unsafe.As<T, BigInteger>(ref value) = bigIntegerValue;
return true;
}
return false;
}
private static bool TryGetFloatingPointFromDouble<T>(double doubleValue, out T value) where T : struct
{
value = default;
var type = typeof(T);
if (type == typeof(float))
{
if ((doubleValue >= float.MinValue) && (doubleValue <= float.MaxValue))
{
Unsafe.As<T, float>(ref value) = (float)doubleValue;
return true;
}
return false;
}
if (type == typeof(double))
{
Unsafe.As<T, double>(ref value) = doubleValue;
return true;
}
if (type == typeof(decimal))
{
return MiscHelpers.Try(out Unsafe.As<T, decimal>(ref value), static doubleValue => (decimal)doubleValue, doubleValue);
}
return false;
}
private static bool TryGetFloatingPointFromDecimal<T>(decimal decimalValue, out T value) where T : struct
{
value = default;
var type = typeof(T);
if (type == typeof(float))
{
Unsafe.As<T, float>(ref value) = (float)decimalValue;
return true;
}
if (type == typeof(double))
{
Unsafe.As<T, double>(ref value) = (double)decimalValue;
return true;
}
if (type == typeof(decimal))
{
Unsafe.As<T, decimal>(ref value) = decimalValue;
return true;
}
return false;
}
private static T ReturnOrThrow<T>(bool succeeded, T result, V8FastArgKind kind, string name) => succeeded ? result : throw CreateArgumentException(kind, name);
private static ReadOnlySpan<char> ReturnOrThrow(bool succeeded, in ReadOnlySpan<char> result, V8FastArgKind kind, string name) => succeeded ? result : throw CreateArgumentException(kind, name);
private static ArgumentException CreateArgumentException(V8FastArgKind kind, string name)
{
return kind switch
{
V8FastArgKind.PropertyValue => new ArgumentException($"Invalid value specified for property '{name.ToNonNull("[unknown]")}'"),
V8FastArgKind.MethodArg => new ArgumentException($"Invalid argument specified for method parameter '{name.ToNonNull("[unnamed]")}'"),
_ => new ArgumentException($"Invalid argument specified for function parameter '{name.ToNonNull("[unnamed]")}'")
};
}
}
}