1 #include "env-inl.h"
2 #include "node_external_reference.h"
3 #include "util-inl.h"
4 #include "v8.h"
5
6 #include <cstdint>
7
8 namespace node {
9 namespace {
10
11 using v8::Context;
12 using v8::Function;
13 using v8::FunctionCallbackInfo;
14 using v8::Local;
15 using v8::Object;
16 using v8::Value;
17
SetupTimers(const FunctionCallbackInfo<Value> & args)18 void SetupTimers(const FunctionCallbackInfo<Value>& args) {
19 CHECK(args[0]->IsFunction());
20 CHECK(args[1]->IsFunction());
21 auto env = Environment::GetCurrent(args);
22
23 env->set_immediate_callback_function(args[0].As<Function>());
24 env->set_timers_callback_function(args[1].As<Function>());
25 }
26
GetLibuvNow(const FunctionCallbackInfo<Value> & args)27 void GetLibuvNow(const FunctionCallbackInfo<Value>& args) {
28 Environment* env = Environment::GetCurrent(args);
29 args.GetReturnValue().Set(env->GetNow());
30 }
31
ScheduleTimer(const FunctionCallbackInfo<Value> & args)32 void ScheduleTimer(const FunctionCallbackInfo<Value>& args) {
33 auto env = Environment::GetCurrent(args);
34 env->ScheduleTimer(args[0]->IntegerValue(env->context()).FromJust());
35 }
36
ToggleTimerRef(const FunctionCallbackInfo<Value> & args)37 void ToggleTimerRef(const FunctionCallbackInfo<Value>& args) {
38 Environment::GetCurrent(args)->ToggleTimerRef(args[0]->IsTrue());
39 }
40
ToggleImmediateRef(const FunctionCallbackInfo<Value> & args)41 void ToggleImmediateRef(const FunctionCallbackInfo<Value>& args) {
42 Environment::GetCurrent(args)->ToggleImmediateRef(args[0]->IsTrue());
43 }
44
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)45 void Initialize(Local<Object> target,
46 Local<Value> unused,
47 Local<Context> context,
48 void* priv) {
49 Environment* env = Environment::GetCurrent(context);
50
51 SetMethod(context, target, "getLibuvNow", GetLibuvNow);
52 SetMethod(context, target, "setupTimers", SetupTimers);
53 SetMethod(context, target, "scheduleTimer", ScheduleTimer);
54 SetMethod(context, target, "toggleTimerRef", ToggleTimerRef);
55 SetMethod(context, target, "toggleImmediateRef", ToggleImmediateRef);
56
57 target
58 ->Set(context,
59 FIXED_ONE_BYTE_STRING(env->isolate(), "immediateInfo"),
60 env->immediate_info()->fields().GetJSArray())
61 .Check();
62
63 target
64 ->Set(context,
65 FIXED_ONE_BYTE_STRING(env->isolate(), "timeoutInfo"),
66 env->timeout_info().GetJSArray())
67 .Check();
68 }
69 } // anonymous namespace
RegisterTimerExternalReferences(ExternalReferenceRegistry * registry)70 void RegisterTimerExternalReferences(ExternalReferenceRegistry* registry) {
71 registry->Register(GetLibuvNow);
72 registry->Register(SetupTimers);
73 registry->Register(ScheduleTimer);
74 registry->Register(ToggleTimerRef);
75 registry->Register(ToggleImmediateRef);
76 }
77
78 } // namespace node
79
80 NODE_BINDING_CONTEXT_AWARE_INTERNAL(timers, node::Initialize)
81 NODE_BINDING_EXTERNAL_REFERENCE(timers, node::RegisterTimerExternalReferences)
82