1
+ #include " timers.h"
1
2
#include " env-inl.h"
2
3
#include " node_external_reference.h"
3
4
#include " util-inl.h"
6
7
#include < cstdint>
7
8
8
9
namespace node {
9
- namespace {
10
+ namespace timers {
10
11
11
12
using v8::Context;
12
13
using v8::Function;
13
14
using v8::FunctionCallbackInfo;
14
15
using v8::Local;
16
+ using v8::Number;
15
17
using v8::Object;
16
18
using v8::Value;
17
19
18
- void SetupTimers (const FunctionCallbackInfo<Value>& args) {
20
+ void BindingData:: SetupTimers (const FunctionCallbackInfo<Value>& args) {
19
21
CHECK (args[0 ]->IsFunction ());
20
22
CHECK (args[1 ]->IsFunction ());
21
23
auto env = Environment::GetCurrent (args);
@@ -24,36 +26,128 @@ void SetupTimers(const FunctionCallbackInfo<Value>& args) {
24
26
env->set_timers_callback_function (args[1 ].As <Function>());
25
27
}
26
28
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 ));
30
32
}
31
33
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
+ data->env ()->ScheduleTimer (duration);
54
+ }
55
+
56
+ void BindingData::SlowToggleTimerRef (
57
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
58
+ ToggleTimerRefImpl (Environment::GetBindingData<BindingData>(args),
59
+ 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),
73
+ args[0 ]->IsTrue ());
74
+ }
75
+
76
+ void BindingData::FastToggleImmediateRef (Local<Object> receiver, 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 ;
35
92
}
36
93
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;
39
99
}
40
100
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);
43
111
}
44
112
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_ (
114
+ v8::CFunction::Make (FastGetLibuvNow));
115
+ v8::CFunction BindingData::fast_schedule_timers_ (
116
+ v8::CFunction::Make (FastScheduleTimers));
117
+ v8::CFunction BindingData::fast_toggle_timer_ref_ (
118
+ v8::CFunction::Make (FastToggleTimerRef));
119
+ v8::CFunction BindingData::fast_toggle_immediate_ref_ (
120
+ v8::CFunction::Make (FastToggleImmediateRef));
121
+
122
+ void BindingData::Initialize (Local<Object> target,
123
+ Local<Value> unused,
124
+ Local<Context> context,
125
+ void * priv) {
49
126
Environment* env = Environment::GetCurrent (context);
127
+ BindingData* const binding_data =
128
+ env->AddBindingData <BindingData>(context, target);
129
+ if (binding_data == nullptr ) return ;
50
130
51
- SetMethod (context, target, " getLibuvNow" , GetLibuvNow);
52
131
SetMethod (context, target, " setupTimers" , SetupTimers);
53
- SetMethod (context, target, " scheduleTimer" , ScheduleTimer);
54
- SetMethod (context, target, " toggleTimerRef" , ToggleTimerRef);
55
- SetMethod (context, target, " toggleImmediateRef" , ToggleImmediateRef);
132
+ SetFastMethod (
133
+ context, target, " getLibuvNow" , SlowGetLibuvNow, &fast_get_libuv_now_);
134
+ SetFastMethod (context,
135
+ target,
136
+ " scheduleTimer" ,
137
+ SlowScheduleTimers,
138
+ &fast_schedule_timers_);
139
+ SetFastMethod (context,
140
+ target,
141
+ " toggleTimerRef" ,
142
+ SlowToggleTimerRef,
143
+ &fast_toggle_timer_ref_);
144
+ SetFastMethod (context,
145
+ target,
146
+ " toggleImmediateRef" ,
147
+ SlowToggleImmediateRef,
148
+ &fast_toggle_immediate_ref_);
56
149
150
+ // TODO(joyeecheung): move these into BindingData.
57
151
target
58
152
->Set (context,
59
153
FIXED_ONE_BYTE_STRING (env->isolate (), " immediateInfo" ),
@@ -66,16 +160,33 @@ void Initialize(Local<Object> target,
66
160
env->timeout_info ().GetJSArray ())
67
161
.Check ();
68
162
}
69
- } // anonymous namespace
70
- void RegisterTimerExternalReferences (ExternalReferenceRegistry* registry) {
71
- registry-> Register (GetLibuvNow);
163
+
164
+ void BindingData:: RegisterTimerExternalReferences (
165
+ ExternalReferenceRegistry* registry) {
72
166
registry->Register (SetupTimers);
73
- registry->Register (ScheduleTimer);
74
- registry->Register (ToggleTimerRef);
75
- registry->Register (ToggleImmediateRef);
167
+
168
+ registry->Register (SlowGetLibuvNow);
169
+ registry->Register (FastGetLibuvNow);
170
+ registry->Register (fast_get_libuv_now_.GetTypeInfo ());
171
+
172
+ registry->Register (SlowScheduleTimers);
173
+ registry->Register (FastScheduleTimers);
174
+ registry->Register (fast_schedule_timers_.GetTypeInfo ());
175
+
176
+ registry->Register (SlowToggleTimerRef);
177
+ registry->Register (FastToggleTimerRef);
178
+ registry->Register (fast_toggle_timer_ref_.GetTypeInfo ());
179
+
180
+ registry->Register (SlowToggleImmediateRef);
181
+ registry->Register (FastToggleImmediateRef);
182
+ registry->Register (fast_toggle_immediate_ref_.GetTypeInfo ());
76
183
}
77
184
185
+ } // namespace timers
186
+
78
187
} // namespace node
79
188
80
- NODE_BINDING_CONTEXT_AWARE_INTERNAL (timers, node::Initialize)
81
- NODE_BINDING_EXTERNAL_REFERENCE(timers, node::RegisterTimerExternalReferences)
189
+ NODE_BINDING_CONTEXT_AWARE_INTERNAL (timers,
190
+ node::timers::BindingData::Initialize)
191
+ NODE_BINDING_EXTERNAL_REFERENCE(
192
+ timers, node::timers::BindingData::RegisterTimerExternalReferences)
0 commit comments