1- #include "node_util.h"
21#include "base_object-inl.h"
32#include "node_errors.h"
43#include "node_external_reference.h"
@@ -17,8 +16,6 @@ using v8::CFunction;
1716using v8::Context;
1817using v8::External;
1918using v8::FunctionCallbackInfo;
20- using v8::FunctionTemplate;
21- using v8::HandleScope;
2219using v8::IndexFilter;
2320using v8::Integer;
2421using v8::Isolate;
@@ -201,109 +198,6 @@ void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
201198 args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
202199}
203200
204- WeakReference::WeakReference(Realm* realm,
205- Local<Object> object,
206- Local<Object> target)
207- : WeakReference(realm, object, target, 0) {}
208-
209- WeakReference::WeakReference(Realm* realm,
210- Local<Object> object,
211- Local<Object> target,
212- uint64_t reference_count)
213- : SnapshotableObject(realm, object, type_int),
214- reference_count_(reference_count) {
215- MakeWeak();
216- if (!target.IsEmpty()) {
217- target_.Reset(realm->isolate(), target);
218- if (reference_count_ == 0) {
219- target_.SetWeak();
220- }
221- }
222- }
223-
224- bool WeakReference::PrepareForSerialization(Local<Context> context,
225- v8::SnapshotCreator* creator) {
226- if (target_.IsEmpty()) {
227- target_index_ = 0;
228- return true;
229- }
230-
231- // Users can still hold strong references to target in addition to the
232- // reference that we manage here, and they could expect that the referenced
233- // object remains the same as long as that external strong reference
234- // is alive. Since we have no way to know if there is any other reference
235- // keeping the target alive, the best we can do to maintain consistency is to
236- // simply save a reference to the target in the snapshot (effectively making
237- // it strong) during serialization, and restore it during deserialization.
238- // If there's no known counted reference from our side, we'll make the
239- // reference here weak upon deserialization so that it can be GC'ed if users
240- // do not hold additional references to it.
241- Local<Object> target = target_.Get(context->GetIsolate());
242- target_index_ = creator->AddData(context, target);
243- DCHECK_NE(target_index_, 0);
244- target_.Reset();
245- return true;
246- }
247-
248- InternalFieldInfoBase* WeakReference::Serialize(int index) {
249- DCHECK_IS_SNAPSHOT_SLOT(index);
250- InternalFieldInfo* info =
251- InternalFieldInfoBase::New<InternalFieldInfo>(type());
252- info->target = target_index_;
253- info->reference_count = reference_count_;
254- return info;
255- }
256-
257- void WeakReference::Deserialize(Local<Context> context,
258- Local<Object> holder,
259- int index,
260- InternalFieldInfoBase* info) {
261- DCHECK_IS_SNAPSHOT_SLOT(index);
262- HandleScope scope(context->GetIsolate());
263-
264- InternalFieldInfo* weak_info = reinterpret_cast<InternalFieldInfo*>(info);
265- Local<Object> target;
266- if (weak_info->target != 0) {
267- target = context->GetDataFromSnapshotOnce<Object>(weak_info->target)
268- .ToLocalChecked();
269- }
270- new WeakReference(
271- Realm::GetCurrent(context), holder, target, weak_info->reference_count);
272- }
273-
274- void WeakReference::New(const FunctionCallbackInfo<Value>& args) {
275- Realm* realm = Realm::GetCurrent(args);
276- CHECK(args.IsConstructCall());
277- CHECK(args[0]->IsObject());
278- new WeakReference(realm, args.This(), args[0].As<Object>());
279- }
280-
281- void WeakReference::Get(const FunctionCallbackInfo<Value>& args) {
282- WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
283- Isolate* isolate = args.GetIsolate();
284- if (!weak_ref->target_.IsEmpty())
285- args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
286- }
287-
288- void WeakReference::IncRef(const FunctionCallbackInfo<Value>& args) {
289- WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
290- weak_ref->reference_count_++;
291- if (weak_ref->target_.IsEmpty()) return;
292- if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
293- args.GetReturnValue().Set(
294- v8::Number::New(args.GetIsolate(), weak_ref->reference_count_));
295- }
296-
297- void WeakReference::DecRef(const FunctionCallbackInfo<Value>& args) {
298- WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
299- CHECK_GE(weak_ref->reference_count_, 1);
300- weak_ref->reference_count_--;
301- if (weak_ref->target_.IsEmpty()) return;
302- if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
303- args.GetReturnValue().Set(
304- v8::Number::New(args.GetIsolate(), weak_ref->reference_count_));
305- }
306-
307201static uint32_t GetUVHandleTypeCode(const uv_handle_type type) {
308202 // TODO(anonrig): We can use an enum here and then create the array in the
309203 // binding, which will remove the hard-coding in C++ and JS land.
@@ -391,10 +285,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
391285 registry->Register(GetExternalValue);
392286 registry->Register(Sleep);
393287 registry->Register(ArrayBufferViewHasBuffer);
394- registry->Register(WeakReference::New);
395- registry->Register(WeakReference::Get);
396- registry->Register(WeakReference::IncRef);
397- registry->Register(WeakReference::DecRef);
398288 registry->Register(GuessHandleType);
399289 registry->Register(FastGuessHandleType);
400290 registry->Register(fast_guess_handle_type_.GetTypeInfo());
@@ -494,15 +384,6 @@ void Initialize(Local<Object> target,
494384 env->should_abort_on_uncaught_toggle().GetJSArray())
495385 .FromJust());
496386
497- Local<FunctionTemplate> weak_ref =
498- NewFunctionTemplate(isolate, WeakReference::New);
499- weak_ref->InstanceTemplate()->SetInternalFieldCount(
500- WeakReference::kInternalFieldCount);
501- SetProtoMethod(isolate, weak_ref, "get", WeakReference::Get);
502- SetProtoMethod(isolate, weak_ref, "incRef", WeakReference::IncRef);
503- SetProtoMethod(isolate, weak_ref, "decRef", WeakReference::DecRef);
504- SetConstructorFunction(context, target, "WeakReference", weak_ref);
505-
506387 SetFastMethodNoSideEffect(context,
507388 target,
508389 "guessHandleType",
0 commit comments