[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 1 | // 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 Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame^] | 7 | #include "base/strings/strcat.h" |
| 8 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 9 | namespace gin { |
| 10 | |
[email protected] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 11 | namespace internal { |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 12 | |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 13 | CallbackHolderBase::CallbackHolderBase(v8::Isolate* isolate) |
| 14 | : v8_ref_(isolate, v8::External::New(isolate, this)) { |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 15 | v8_ref_.SetWeak(this, &CallbackHolderBase::FirstWeakCallback, |
| 16 | v8::WeakCallbackType::kParameter); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | CallbackHolderBase::~CallbackHolderBase() { |
| 20 | DCHECK(v8_ref_.IsEmpty()); |
| 21 | } |
| 22 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 23 | v8::Local<v8::External> CallbackHolderBase::GetHandle(v8::Isolate* isolate) { |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 24 | return v8::Local<v8::External>::New(isolate, v8_ref_); |
| 25 | } |
| 26 | |
| 27 | // static |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 28 | void CallbackHolderBase::FirstWeakCallback( |
| 29 | const v8::WeakCallbackInfo<CallbackHolderBase>& data) { |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 30 | data.GetParameter()->v8_ref_.Reset(); |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 31 | data.SetSecondPassCallback(SecondWeakCallback); |
| 32 | } |
| 33 | |
| 34 | // static |
| 35 | void CallbackHolderBase::SecondWeakCallback( |
| 36 | const v8::WeakCallbackInfo<CallbackHolderBase>& data) { |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 37 | delete data.GetParameter(); |
| 38 | } |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 39 | |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame^] | 40 | void 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] | cb6c2bb | 2013-12-17 21:47:04 | [diff] [blame] | 64 | } // namespace internal |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 65 | |
| 66 | } // namespace gin |