-
Notifications
You must be signed in to change notification settings - Fork 1
/
v8_c_promise.cc
80 lines (60 loc) · 2.16 KB
/
v8_c_promise.cc
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
#include "v8_c_private.h"
typedef v8::Persistent<v8::Promise::Resolver> Resolver;
extern "C"
{
ResolverPtr v8_Promise_NewResolver(ContextPtr pContext)
{
VALUE_SCOPE(pContext);
v8::MaybeLocal<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(context);
if (resolver.IsEmpty())
{
return NULL;
}
return new Resolver(isolate, resolver.ToLocalChecked());
}
Error v8_Resolver_Resolve(ContextPtr pContext, ResolverPtr pResolver, ValuePtr pValue)
{
VALUE_SCOPE(pContext);
v8::Local<v8::Value> value = static_cast<Value *>(pValue)->Get(isolate);
v8::Local<v8::Promise::Resolver> resolver = static_cast<Resolver *>(pResolver)->Get(isolate);
v8::Maybe<bool> result = resolver->Resolve(context, value);
if (result.IsNothing())
{
return v8_String_Create("Something went wrong: resolve returned nothing.");
}
else if (!result.FromJust())
{
return v8_String_Create("Something went wrong: resolve failed.");
}
return Error{NULL, 0};
}
Error v8_Resolver_Reject(ContextPtr pContext, ResolverPtr pResolver, ValuePtr pValue)
{
VALUE_SCOPE(pContext);
v8::Local<v8::Value> value = static_cast<Value *>(pValue)->Get(isolate);
v8::Local<v8::Promise::Resolver> resolver = static_cast<Resolver *>(pResolver)->Get(isolate);
v8::Maybe<bool> result = resolver->Reject(context, value);
if (result.IsNothing())
{
return v8_String_Create("Something went wrong: resolve returned nothing.");
}
else if (!result.FromJust())
{
return v8_String_Create("Something went wrong: resolve failed.");
}
return Error{NULL, 0};
}
CallResult v8_Resolver_GetPromise(ContextPtr pContext, ResolverPtr pResolver)
{
VALUE_SCOPE(pContext);
v8::Local<v8::Promise::Resolver> resolver = static_cast<Resolver *>(pResolver)->Get(isolate);
return v8_Value_ValueTuple(isolate, context, static_cast<v8::Local<v8::Value>>(resolver->GetPromise()));
}
void v8_Resolver_Release(ContextPtr pContext, ResolverPtr pResolver)
{
VALUE_SCOPE(pContext);
Resolver *resolver = static_cast<Resolver *>(pResolver);
resolver->Reset();
delete resolver;
}
}