blob: cfce6ed09a795f8c885e8211bbeb51abfa6882ed [file] [log] [blame]
[email protected]314cde12013-11-23 20:26:511// Copyright 2013 The Chromium 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 "gin/function_template.h"
6
Devlin Cronine9db9842018-04-09 17:51:057#include "base/strings/strcat.h"
8
[email protected]314cde12013-11-23 20:26:519namespace gin {
10
[email protected]cb6c2bb2013-12-17 21:47:0411namespace internal {
[email protected]314cde12013-11-23 20:26:5112
[email protected]bf0142902014-02-11 15:06:1213CallbackHolderBase::CallbackHolderBase(v8::Isolate* isolate)
14 : v8_ref_(isolate, v8::External::New(isolate, this)) {
dcarney99ade9082015-04-22 09:55:4215 v8_ref_.SetWeak(this, &CallbackHolderBase::FirstWeakCallback,
16 v8::WeakCallbackType::kParameter);
[email protected]bf0142902014-02-11 15:06:1217}
18
19CallbackHolderBase::~CallbackHolderBase() {
20 DCHECK(v8_ref_.IsEmpty());
21}
22
deepak.sfaaa1b62015-04-30 07:30:4823v8::Local<v8::External> CallbackHolderBase::GetHandle(v8::Isolate* isolate) {
[email protected]bf0142902014-02-11 15:06:1224 return v8::Local<v8::External>::New(isolate, v8_ref_);
25}
26
27// static
dcarney99ade9082015-04-22 09:55:4228void CallbackHolderBase::FirstWeakCallback(
29 const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
[email protected]bf0142902014-02-11 15:06:1230 data.GetParameter()->v8_ref_.Reset();
dcarney99ade9082015-04-22 09:55:4231 data.SetSecondPassCallback(SecondWeakCallback);
32}
33
34// static
35void CallbackHolderBase::SecondWeakCallback(
36 const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
[email protected]bf0142902014-02-11 15:06:1237 delete data.GetParameter();
38}
[email protected]81f8b91b2013-11-26 21:02:5139
Devlin Cronine9db9842018-04-09 17:51:0540void ThrowConversionError(Arguments* args,
41 const InvokerOptions& invoker_options,
42 size_t index) {
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.
47 std::string error =
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);
54 } else {
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.
60 args->ThrowError();
61 }
62}
63
[email protected]cb6c2bb2013-12-17 21:47:0464} // namespace internal
[email protected]314cde12013-11-23 20:26:5165
66} // namespace gin