1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/function_template.h"
7 #include "base/strings/strcat.h"
13 CallbackHolderBase::CallbackHolderBase(v8::Isolate* isolate)
14 : v8_ref_(isolate, v8::External::New(isolate, this)) {
15 v8_ref_.SetWeak(this, &CallbackHolderBase::FirstWeakCallback,
16 v8::WeakCallbackType::kParameter);
19 CallbackHolderBase::~CallbackHolderBase() {
20 DCHECK(v8_ref_.IsEmpty());
23 v8::Local<v8::External> CallbackHolderBase::GetHandle(v8::Isolate* isolate) {
24 return v8::Local<v8::External>::New(isolate, v8_ref_);
28 void CallbackHolderBase::FirstWeakCallback(
29 const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
30 data.GetParameter()->v8_ref_.Reset();
31 data.SetSecondPassCallback(SecondWeakCallback);
35 void CallbackHolderBase::SecondWeakCallback(
36 const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
37 delete data.GetParameter();
40 void ThrowConversionError(Arguments* args,
41 const InvokerOptions& invoker_options,
43 if (index == 0 && invoker_options.holder_is_first_argument) {
44 // Failed to get the appropriate `this` object. This can happen if a
45 // method is invoked using Function.prototype.[call|apply] and passed an
46 // invalid (or null) `this` argument.
48 invoker_options.holder_type
49 ? base::StrCat({"Illegal invocation: Function must be "
50 "called on an object of type ",
51 invoker_options.holder_type})
52 : "Illegal invocation";
53 args->ThrowTypeError(error);
55 // Otherwise, this failed parsing on a different argument.
56 // Arguments::ThrowError() will try to include appropriate information.
57 // Ideally we would include the expected c++ type in the error message
58 // here, too (which we can access via typeid(ArgType).name()), however we
59 // compile with no-rtti, which disables typeid.
64 } // namespace internal