-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathPromiseProxy.cpp
More file actions
46 lines (36 loc) · 1.5 KB
/
Copy pathPromiseProxy.cpp
File metadata and controls
46 lines (36 loc) · 1.5 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
#include "PromiseProxy.h"
#include <CoreFoundation/CoreFoundation.h>
#include "BuiltinLoader.h"
#include "Helpers.h"
#include "Runtime.h"
using namespace v8;
namespace tns {
// Reports whether the calling thread runs the isolate's runtime loop. That loop
// is where timers fire and is always being pumped, so a promise resolution is
// marshaled back to its creating thread only when that thread is the runtime
// loop; a promise created elsewhere settles on whichever thread resolves it.
static void IsRuntimeRunloopCallback(const FunctionCallbackInfo<Value>& args) {
Runtime* runtime = Runtime::GetRuntime(args.GetIsolate());
bool isRuntimeLoop =
runtime != nullptr && CFRunLoopGetCurrent() == runtime->RuntimeLoop();
args.GetReturnValue().Set(isRuntimeLoop);
}
void PromiseProxy::Init(v8::Local<v8::Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<v8::Function> isRuntimeRunloop;
bool success = v8::Function::New(context, IsRuntimeRunloopCallback)
.ToLocal(&isRuntimeRunloop);
tns::Assert(success, isolate);
Local<Object> binding = Object::New(isolate);
success = binding
->Set(context, tns::ToV8String(isolate, "isRuntimeRunloop"),
isRuntimeRunloop)
.FromMaybe(false);
tns::Assert(success, isolate);
Local<Value> result;
success =
BuiltinLoader::RunBuiltin(context, BuiltinId::kPromiseProxy, binding)
.ToLocal(&result);
tns::Assert(success, isolate);
}
} // namespace tns