forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8EntityHolder.cs
More file actions
56 lines (47 loc) · 1.72 KB
/
Copy pathV8EntityHolder.cs
File metadata and controls
56 lines (47 loc) · 1.72 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
namespace Microsoft.ClearScript.V8.SplitProxy
{
internal readonly struct V8EntityHolder
{
private readonly string name;
private readonly bool registered;
private readonly V8Entity.Handle handle;
public V8Entity.Handle Handle => (handle != V8Entity.Handle.Empty) ? handle : throw new InvalidOperationException("The " + name + " proxy has been destroyed");
public V8EntityHolder(string name, Func<V8Entity.Handle> acquireHandle)
{
this.name = name;
V8Proxy.OnEntityHolderCreated();
registered = true;
handle = acquireHandle();
}
private V8EntityHolder(string name)
{
this.name = name;
registered = false;
handle = V8Entity.Handle.Empty;
}
public void ReleaseEntity()
{
var tempHandle = handle;
if (tempHandle != V8Entity.Handle.Empty)
{
V8SplitProxyNative.InvokeNoThrow(static (instance, tempHandle) => instance.V8Entity_Release(tempHandle), tempHandle);
}
}
public static void Destroy(ref V8EntityHolder holder)
{
var tempHandle = holder.handle;
if (tempHandle != V8Entity.Handle.Empty)
{
V8SplitProxyNative.InvokeNoThrow(static (instance, tempHandle) => instance.V8Entity_DestroyHandle(tempHandle), tempHandle);
}
if (holder.registered)
{
V8Proxy.OnEntityHolderDestroyed();
}
holder = new V8EntityHolder(holder.name);
}
}
}