1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/builtins/builtins-utils-inl.h"
#include "src/logging/counters.h"
#include "src/objects/js-weak-refs-inl.h"
namespace v8 {
namespace internal {
BUILTIN(FinalizationRegistryConstructor) {
HandleScope scope(isolate);
Handle<JSFunction> target = args.target();
if (args.new_target()->IsUndefined(isolate)) { // [[Call]]
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
handle(target->shared().Name(), isolate)));
}
// [[Construct]]
Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
Handle<Object> cleanup = args.atOrUndefined(isolate, 1);
if (!cleanup->IsCallable()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kWeakRefsCleanupMustBeCallable));
}
Handle<JSObject> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, result,
JSObject::New(target, new_target, Handle<AllocationSite>::null()));
Handle<JSFinalizationRegistry> finalization_registry =
Handle<JSFinalizationRegistry>::cast(result);
finalization_registry->set_native_context(*isolate->native_context());
finalization_registry->set_cleanup(*cleanup);
finalization_registry->set_flags(
JSFinalizationRegistry::ScheduledForCleanupField::encode(false));
DCHECK(finalization_registry->active_cells().IsUndefined(isolate));
DCHECK(finalization_registry->cleared_cells().IsUndefined(isolate));
DCHECK(finalization_registry->key_map().IsUndefined(isolate));
return *finalization_registry;
}
BUILTIN(FinalizationRegistryRegister) {
HandleScope scope(isolate);
const char* method_name = "FinalizationRegistry.prototype.register";
// 1. Let finalizationGroup be the this value.
//
// 2. If Type(finalizationGroup) is not Object, throw a TypeError
// exception.
//
// 4. If finalizationGroup does not have a [[Cells]] internal slot,
// throw a TypeError exception.
CHECK_RECEIVER(JSFinalizationRegistry, finalization_registry, method_name);
Handle<Object> target = args.atOrUndefined(isolate, 1);
// 3. If Type(target) is not Object, throw a TypeError exception.
if (!target->IsJSReceiver()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(MessageTemplate::kWeakRefsRegisterTargetMustBeObject));
}
Handle<Object> holdings = args.atOrUndefined(isolate, 2);
if (target->SameValue(*holdings)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(
MessageTemplate::kWeakRefsRegisterTargetAndHoldingsMustNotBeSame));
}
Handle<Object> unregister_token = args.atOrUndefined(isolate, 3);
// 5. If Type(unregisterToken) is not Object,
// a. If unregisterToken is not undefined, throw a TypeError exception.
if (!unregister_token->IsJSReceiver() && !unregister_token->IsUndefined()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(MessageTemplate::kWeakRefsUnregisterTokenMustBeObject,
unregister_token));
}
// TODO(marja): Realms.
JSFinalizationRegistry::Register(finalization_registry,
Handle<JSReceiver>::cast(target), holdings,
unregister_token, isolate);
return ReadOnlyRoots(isolate).undefined_value();
}
BUILTIN(FinalizationRegistryUnregister) {
HandleScope scope(isolate);
const char* method_name = "FinalizationRegistry.prototype.unregister";
// 1. Let finalizationGroup be the this value.
//
// 2. If Type(finalizationGroup) is not Object, throw a TypeError
// exception.
//
// 3. If finalizationGroup does not have a [[Cells]] internal slot,
// throw a TypeError exception.
CHECK_RECEIVER(JSFinalizationRegistry, finalization_registry, method_name);
Handle<Object> unregister_token = args.atOrUndefined(isolate, 1);
// 4. If Type(unregisterToken) is not Object, throw a TypeError exception.
if (!unregister_token->IsJSReceiver()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(MessageTemplate::kWeakRefsUnregisterTokenMustBeObject,
unregister_token));
}
bool success = JSFinalizationRegistry::Unregister(
finalization_registry, Handle<JSReceiver>::cast(unregister_token),
isolate);
return *isolate->factory()->ToBoolean(success);
}
BUILTIN(FinalizationRegistryCleanupSome) {
HandleScope scope(isolate);
const char* method_name = "FinalizationRegistry.prototype.cleanupSome";
// 1. Let finalizationGroup be the this value.
//
// 2. If Type(finalizationGroup) is not Object, throw a TypeError
// exception.
//
// 3. If finalizationGroup does not have a [[Cells]] internal slot,
// throw a TypeError exception.
CHECK_RECEIVER(JSFinalizationRegistry, finalization_registry, method_name);
Handle<Object> callback(finalization_registry->cleanup(), isolate);
Handle<Object> callback_obj = args.atOrUndefined(isolate, 1);
// 4. If callback is not undefined and IsCallable(callback) is
// false, throw a TypeError exception.
if (!callback_obj->IsUndefined(isolate)) {
if (!callback_obj->IsCallable()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(MessageTemplate::kWeakRefsCleanupMustBeCallable));
}
callback = callback_obj;
}
// Don't do set_scheduled_for_cleanup(false); we still have the task
// scheduled.
if (JSFinalizationRegistry::Cleanup(isolate, finalization_registry, callback)
.IsNothing()) {
DCHECK(isolate->has_pending_exception());
return ReadOnlyRoots(isolate).exception();
}
return ReadOnlyRoots(isolate).undefined_value();
}
BUILTIN(FinalizationRegistryCleanupIteratorNext) {
HandleScope scope(isolate);
CHECK_RECEIVER(JSFinalizationRegistryCleanupIterator, iterator, "next");
Handle<JSFinalizationRegistry> finalization_registry(
iterator->finalization_registry(), isolate);
if (!finalization_registry->NeedsCleanup()) {
return *isolate->factory()->NewJSIteratorResult(
handle(ReadOnlyRoots(isolate).undefined_value(), isolate), true);
}
Handle<Object> holdings =
handle(JSFinalizationRegistry::PopClearedCellHoldings(
finalization_registry, isolate),
isolate);
return *isolate->factory()->NewJSIteratorResult(holdings, false);
}
BUILTIN(WeakRefConstructor) {
HandleScope scope(isolate);
Handle<JSFunction> target = args.target();
if (args.new_target()->IsUndefined(isolate)) { // [[Call]]
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
handle(target->shared().Name(), isolate)));
}
// [[Construct]]
Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
Handle<Object> target_object = args.atOrUndefined(isolate, 1);
if (!target_object->IsJSReceiver()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(
MessageTemplate::kWeakRefsWeakRefConstructorTargetMustBeObject));
}
Handle<JSReceiver> target_receiver =
handle(JSReceiver::cast(*target_object), isolate);
isolate->heap()->KeepDuringJob(target_receiver);
// TODO(marja): Realms.
Handle<JSObject> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, result,
JSObject::New(target, new_target, Handle<AllocationSite>::null()));
Handle<JSWeakRef> weak_ref = Handle<JSWeakRef>::cast(result);
weak_ref->set_target(*target_receiver);
return *weak_ref;
}
BUILTIN(WeakRefDeref) {
HandleScope scope(isolate);
CHECK_RECEIVER(JSWeakRef, weak_ref, "WeakRef.prototype.deref");
if (weak_ref->target().IsJSReceiver()) {
Handle<JSReceiver> target =
handle(JSReceiver::cast(weak_ref->target()), isolate);
// KeepDuringJob might allocate and cause a GC, but it won't clear
// weak_ref since we hold a Handle to its target.
isolate->heap()->KeepDuringJob(target);
} else {
DCHECK(weak_ref->target().IsUndefined(isolate));
}
return weak_ref->target();
}
} // namespace internal
} // namespace v8
|