forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8FastProxyHelpers.cs
More file actions
71 lines (56 loc) · 2.21 KB
/
Copy pathV8FastProxyHelpers.cs
File metadata and controls
71 lines (56 loc) · 2.21 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Runtime.CompilerServices;
using Microsoft.ClearScript.Util;
using Microsoft.ClearScript.V8.SplitProxy;
namespace Microsoft.ClearScript.V8.FastProxy
{
internal static class NullableHelpers
{
private static readonly ConditionalWeakTable<Type, Holder<IImpl>> table = new();
public static bool TryGet<TNullable>(Type underlyingType, in V8Value.FastArg arg, object obj, out TNullable value)
{
return GetImpl(underlyingType).TryGet(arg, obj, out value);
}
public static void Set<TNullable>(Type underlyingType, in V8FastResult result, ref TNullable value)
{
GetImpl(underlyingType).Set(result, ref value);
}
private static IImpl GetImpl(Type underlyingType)
{
lock (table)
{
var holder = table.GetOrCreateValue(underlyingType);
return holder.Value ?? (holder.Value = CreateImpl(underlyingType));
}
}
private static IImpl CreateImpl(Type underlyingType)
{
return (IImpl)typeof(Impl<>).MakeGenericType(underlyingType).CreateInstance();
}
#region Nested type: IImpl
private interface IImpl
{
bool TryGet<TNullable>(in V8Value.FastArg arg, object obj, out TNullable value);
void Set<TNullable>(in V8FastResult result, ref TNullable value);
}
#endregion
#region Nested type: Impl<TUnderlying>
private sealed class Impl<TUnderlying> : IImpl where TUnderlying : struct
{
#region IImpl implementation
bool IImpl.TryGet<TNullable>(in V8Value.FastArg arg, object obj, out TNullable value)
{
value = default;
return V8FastArgImpl.TryGet(arg, obj, out Unsafe.As<TNullable, TUnderlying?>(ref value));
}
void IImpl.Set<TNullable>(in V8FastResult result, ref TNullable value)
{
result.SetNullable(Unsafe.As<TNullable, TUnderlying?>(ref value));
}
#endregion
}
#endregion
}
}