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