1+ #include " timers.h"
12#include " env-inl.h"
23#include " node_external_reference.h"
34#include " util-inl.h"
67#include < cstdint>
78
89namespace node {
9- namespace {
10+ namespace timers {
1011
1112using v8::Context;
1213using v8::Function;
1314using v8::FunctionCallbackInfo;
1415using v8::Local;
16+ using v8::Number;
1517using v8::Object;
1618using v8::Value;
1719
18- void SetupTimers (const FunctionCallbackInfo<Value>& args) {
20+ void BindingData:: SetupTimers (const FunctionCallbackInfo<Value>& args) {
1921 CHECK (args[0 ]->IsFunction ());
2022 CHECK (args[1 ]->IsFunction ());
2123 auto env = Environment::GetCurrent (args);
@@ -24,36 +26,124 @@ void SetupTimers(const FunctionCallbackInfo<Value>& args) {
2426 env->set_timers_callback_function (args[1 ].As <Function>());
2527}
2628
27- void GetLibuvNow (const FunctionCallbackInfo<Value>& args) {
28- Environment* env = Environment::GetCurrent (args);
29- args.GetReturnValue ().Set (env-> GetNow ( ));
29+ void BindingData::SlowGetLibuvNow (const FunctionCallbackInfo<Value>& args) {
30+ double now = GetLibuvNowImpl ( Environment::GetBindingData<BindingData> (args) );
31+ args.GetReturnValue ().Set (Number::New (args. GetIsolate (), now ));
3032}
3133
32- void ScheduleTimer (const FunctionCallbackInfo<Value>& args) {
33- auto env = Environment::GetCurrent (args);
34- env->ScheduleTimer (args[0 ]->IntegerValue (env->context ()).FromJust ());
34+ double BindingData::FastGetLibuvNow (Local<Object> receiver) {
35+ return GetLibuvNowImpl (FromJSObject<BindingData>(receiver));
36+ }
37+
38+ double BindingData::GetLibuvNowImpl (BindingData* data) {
39+ return static_cast <double >(data->env ()->GetNowUint64 ());
40+ }
41+
42+ void BindingData::SlowScheduleTimers (const FunctionCallbackInfo<Value>& args) {
43+ int64_t duration =
44+ args[0 ]->IntegerValue (args.GetIsolate ()->GetCurrentContext ()).FromJust ();
45+ ScheduleTimersImpl (Environment::GetBindingData<BindingData>(args), duration);
46+ }
47+
48+ void BindingData::FastScheduleTimers (Local<Object> receiver, int64_t duration) {
49+ ScheduleTimersImpl (FromJSObject<BindingData>(receiver), duration);
50+ }
51+
52+ void BindingData::ScheduleTimersImpl (BindingData* data, int64_t duration) {
53+
54+ data->env ()->ScheduleTimer (duration);
55+ }
56+
57+ void BindingData::SlowToggleTimerRef (
58+ const v8::FunctionCallbackInfo<v8::Value>& args) {
59+ ToggleTimerRefImpl (Environment::GetBindingData<BindingData>(args), args[0 ]->IsTrue ());
60+ }
61+
62+ void BindingData::FastToggleTimerRef (Local<Object> receiver, bool ref) {
63+ ToggleTimerRefImpl (FromJSObject<BindingData>(receiver), ref);
64+ }
65+
66+ void BindingData::ToggleTimerRefImpl (BindingData* data, bool ref) {
67+ data->env ()->ToggleTimerRef (ref);
68+ }
69+
70+ void BindingData::SlowToggleImmediateRef (
71+ const v8::FunctionCallbackInfo<v8::Value>& args) {
72+ ToggleImmediateRefImpl (Environment::GetBindingData<BindingData>(args), args[0 ]->IsTrue ());
73+ }
74+
75+ void BindingData::FastToggleImmediateRef (Local<Object> receiver,
76+ bool ref) {
77+ ToggleImmediateRefImpl (FromJSObject<BindingData>(receiver), ref);
78+ }
79+
80+ void BindingData::ToggleImmediateRefImpl (BindingData* data, bool ref) {
81+ data->env ()->ToggleImmediateRef (ref);
82+ }
83+
84+ BindingData::BindingData (Environment* env, Local<Object> object)
85+ : SnapshotableObject(env, object, type_int) {}
86+
87+ bool BindingData::PrepareForSerialization (Local<Context> context,
88+ v8::SnapshotCreator* creator) {
89+ // Return true because we need to maintain the reference to the binding from
90+ // JS land.
91+ return true ;
3592}
3693
37- void ToggleTimerRef (const FunctionCallbackInfo<Value>& args) {
38- Environment::GetCurrent (args)->ToggleTimerRef (args[0 ]->IsTrue ());
94+ InternalFieldInfoBase* BindingData::Serialize (int index) {
95+ DCHECK_EQ (index, BaseObject::kEmbedderType );
96+ InternalFieldInfo* info =
97+ InternalFieldInfoBase::New<InternalFieldInfo>(type ());
98+ return info;
3999}
40100
41- void ToggleImmediateRef (const FunctionCallbackInfo<Value>& args) {
42- Environment::GetCurrent (args)->ToggleImmediateRef (args[0 ]->IsTrue ());
101+ void BindingData::Deserialize (Local<Context> context,
102+ Local<Object> holder,
103+ int index,
104+ InternalFieldInfoBase* info) {
105+ DCHECK_EQ (index, BaseObject::kEmbedderType );
106+ v8::HandleScope scope (context->GetIsolate ());
107+ Environment* env = Environment::GetCurrent (context);
108+ // Recreate the buffer in the constructor.
109+ BindingData* binding = env->AddBindingData <BindingData>(context, holder);
110+ CHECK_NOT_NULL (binding);
43111}
44112
45- void Initialize (Local<Object> target,
46- Local<Value> unused,
47- Local<Context> context,
48- void * priv) {
113+ v8::CFunction BindingData::fast_get_libuv_now_ (v8::CFunction::Make(FastGetLibuvNow));
114+ v8::CFunction BindingData::fast_schedule_timers_ (v8::CFunction::Make(FastScheduleTimers));
115+ v8::CFunction BindingData::fast_toggle_timer_ref_ (v8::CFunction::Make(FastToggleTimerRef));
116+ v8::CFunction BindingData::fast_toggle_immediate_ref_ (v8::CFunction::Make(FastToggleImmediateRef));
117+
118+ void BindingData::Initialize (Local<Object> target,
119+ Local<Value> unused,
120+ Local<Context> context,
121+ void * priv) {
49122 Environment* env = Environment::GetCurrent (context);
123+ BindingData* const binding_data =
124+ env->AddBindingData <BindingData>(context, target);
125+ if (binding_data == nullptr ) return ;
50126
51- SetMethod (context, target, " getLibuvNow" , GetLibuvNow);
52127 SetMethod (context, target, " setupTimers" , SetupTimers);
53- SetMethod (context, target, " scheduleTimer" , ScheduleTimer);
54- SetMethod (context, target, " toggleTimerRef" , ToggleTimerRef);
55- SetMethod (context, target, " toggleImmediateRef" , ToggleImmediateRef);
128+ SetFastMethod (
129+ context, target, " getLibuvNow" , SlowGetLibuvNow, &fast_get_libuv_now_);
130+ SetFastMethod (context,
131+ target,
132+ " scheduleTimer" ,
133+ SlowScheduleTimers,
134+ &fast_schedule_timers_);
135+ SetFastMethod (context,
136+ target,
137+ " toggleTimerRef" ,
138+ SlowToggleTimerRef,
139+ &fast_toggle_timer_ref_);
140+ SetFastMethod (context,
141+ target,
142+ " toggleImmediateRef" ,
143+ SlowToggleImmediateRef,
144+ &fast_toggle_immediate_ref_);
56145
146+ // TODO(joyeecheung): move these into BindingData.
57147 target
58148 ->Set (context,
59149 FIXED_ONE_BYTE_STRING (env->isolate (), " immediateInfo" ),
@@ -66,16 +156,32 @@ void Initialize(Local<Object> target,
66156 env->timeout_info ().GetJSArray ())
67157 .Check ();
68158}
69- } // anonymous namespace
70- void RegisterTimerExternalReferences (ExternalReferenceRegistry* registry) {
71- registry->Register (GetLibuvNow);
159+
160+ void BindingData::RegisterTimerExternalReferences (ExternalReferenceRegistry* registry) {
72161 registry->Register (SetupTimers);
73- registry->Register (ScheduleTimer);
74- registry->Register (ToggleTimerRef);
75- registry->Register (ToggleImmediateRef);
162+
163+ registry->Register (SlowGetLibuvNow);
164+ registry->Register (FastGetLibuvNow);
165+ registry->Register (fast_get_libuv_now_.GetTypeInfo ());
166+
167+ registry->Register (SlowScheduleTimers);
168+ registry->Register (FastScheduleTimers);
169+ registry->Register (fast_schedule_timers_.GetTypeInfo ());
170+
171+ registry->Register (SlowToggleTimerRef);
172+ registry->Register (FastToggleTimerRef);
173+ registry->Register (fast_toggle_timer_ref_.GetTypeInfo ());
174+
175+ registry->Register (SlowToggleImmediateRef);
176+ registry->Register (FastToggleImmediateRef);
177+ registry->Register (fast_toggle_immediate_ref_.GetTypeInfo ());
76178}
77179
180+ } // namespace timers
181+
78182} // namespace node
79183
80- NODE_BINDING_CONTEXT_AWARE_INTERNAL (timers, node::Initialize)
81- NODE_BINDING_EXTERNAL_REFERENCE(timers, node::RegisterTimerExternalReferences)
184+ NODE_BINDING_CONTEXT_AWARE_INTERNAL (timers,
185+ node::timers::BindingData::Initialize)
186+ NODE_BINDING_EXTERNAL_REFERENCE(
187+ timers, node::timers::BindingData::RegisterTimerExternalReferences)
0 commit comments