11#include " node_errors.h"
22#include " node_watchdog.h"
33#include " util.h"
4+ #include " base_object-inl.h"
45
56namespace node {
67namespace util {
@@ -11,6 +12,7 @@ using v8::Boolean;
1112using v8::Context;
1213using v8::Function;
1314using v8::FunctionCallbackInfo;
15+ using v8::FunctionTemplate;
1416using v8::IndexFilter;
1517using v8::Integer;
1618using v8::Isolate;
@@ -181,6 +183,37 @@ void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
181183 isolate->EnqueueMicrotask (args[0 ].As <Function>());
182184}
183185
186+ class WeakReference : public BaseObject {
187+ public:
188+ WeakReference (Environment* env, Local<Object> object, Local<Object> target)
189+ : BaseObject(env, object) {
190+ MakeWeak ();
191+ target_.Reset (env->isolate (), target);
192+ target_.SetWeak ();
193+ }
194+
195+ static void New (const FunctionCallbackInfo<Value>& args) {
196+ Environment* env = Environment::GetCurrent (args);
197+ CHECK (args.IsConstructCall ());
198+ CHECK (args[0 ]->IsObject ());
199+ new WeakReference (env, args.This (), args[0 ].As <Object>());
200+ }
201+
202+ static void Get (const FunctionCallbackInfo<Value>& args) {
203+ WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder ());
204+ Isolate* isolate = args.GetIsolate ();
205+ if (!weak_ref->target_ .IsEmpty ())
206+ args.GetReturnValue ().Set (weak_ref->target_ .Get (isolate));
207+ }
208+
209+ SET_MEMORY_INFO_NAME (WeakReference)
210+ SET_SELF_SIZE (WeakReference)
211+ SET_NO_MEMORY_INFO ()
212+
213+ private:
214+ Persistent<Object> target_;
215+ };
216+
184217void Initialize (Local<Object> target,
185218 Local<Value> unused,
186219 Local<Context> context,
@@ -241,6 +274,16 @@ void Initialize(Local<Object> target,
241274 should_abort_on_uncaught_toggle,
242275 env->should_abort_on_uncaught_toggle ().GetJSArray ())
243276 .FromJust ());
277+
278+ Local<String> weak_ref_string =
279+ FIXED_ONE_BYTE_STRING (env->isolate (), " WeakReference" );
280+ Local<FunctionTemplate> weak_ref =
281+ env->NewFunctionTemplate (WeakReference::New);
282+ weak_ref->InstanceTemplate ()->SetInternalFieldCount (1 );
283+ weak_ref->SetClassName (weak_ref_string);
284+ env->SetProtoMethod (weak_ref, " get" , WeakReference::Get);
285+ target->Set (context, weak_ref_string,
286+ weak_ref->GetFunction (context).ToLocalChecked ()).FromJust ();
244287}
245288
246289} // namespace util
0 commit comments