1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/builtins/builtins-utils-inl.h"
6 #include "src/logging/counters.h"
7 #include "src/objects/js-weak-refs-inl.h"
8 
9 namespace v8 {
10 namespace internal {
11 
12 // https://tc39.es/ecma262/#sec-finalization-registry.prototype.unregister
BUILTIN(FinalizationRegistryUnregister)13 BUILTIN(FinalizationRegistryUnregister) {
14   HandleScope scope(isolate);
15   const char* method_name = "FinalizationRegistry.prototype.unregister";
16 
17   // 1. Let finalizationGroup be the this value.
18   //
19   // 2. Perform ? RequireInternalSlot(finalizationRegistry, [[Cells]]).
20   CHECK_RECEIVER(JSFinalizationRegistry, finalization_registry, method_name);
21 
22   Handle<Object> unregister_token = args.atOrUndefined(isolate, 1);
23 
24   // 3. If CanBeHeldWeakly(unregisterToken) is false, throw a TypeError
25   // exception.
26   if (!unregister_token->CanBeHeldWeakly()) {
27     THROW_NEW_ERROR_RETURN_FAILURE(
28         isolate, NewTypeError(MessageTemplate::kInvalidWeakRefsUnregisterToken,
29                               unregister_token));
30   }
31 
32   bool success = JSFinalizationRegistry::Unregister(
33       finalization_registry, Handle<HeapObject>::cast(unregister_token),
34       isolate);
35 
36   return *isolate->factory()->ToBoolean(success);
37 }
38 
39 }  // namespace internal
40 }  // namespace v8
41