function_template.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "gin/function_template.h"
  5. #include "base/strings/strcat.h"
  6. namespace gin {
  7. namespace internal {
  8. CallbackHolderBase::CallbackHolderBase(v8::Isolate* isolate)
  9. : v8_ref_(isolate, v8::External::New(isolate, this)) {
  10. v8_ref_.SetWeak(this, &CallbackHolderBase::FirstWeakCallback,
  11. v8::WeakCallbackType::kParameter);
  12. }
  13. CallbackHolderBase::~CallbackHolderBase() {
  14. DCHECK(v8_ref_.IsEmpty());
  15. }
  16. v8::Local<v8::External> CallbackHolderBase::GetHandle(v8::Isolate* isolate) {
  17. return v8::Local<v8::External>::New(isolate, v8_ref_);
  18. }
  19. // static
  20. void CallbackHolderBase::FirstWeakCallback(
  21. const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
  22. data.GetParameter()->v8_ref_.Reset();
  23. data.SetSecondPassCallback(SecondWeakCallback);
  24. }
  25. // static
  26. void CallbackHolderBase::SecondWeakCallback(
  27. const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
  28. delete data.GetParameter();
  29. }
  30. void ThrowConversionError(Arguments* args,
  31. const InvokerOptions& invoker_options,
  32. size_t index) {
  33. if (index == 0 && invoker_options.holder_is_first_argument) {
  34. // Failed to get the appropriate `this` object. This can happen if a
  35. // method is invoked using Function.prototype.[call|apply] and passed an
  36. // invalid (or null) `this` argument.
  37. std::string error =
  38. invoker_options.holder_type
  39. ? base::StrCat({"Illegal invocation: Function must be "
  40. "called on an object of type ",
  41. invoker_options.holder_type})
  42. : "Illegal invocation";
  43. args->ThrowTypeError(error);
  44. } else {
  45. // Otherwise, this failed parsing on a different argument.
  46. // Arguments::ThrowError() will try to include appropriate information.
  47. // Ideally we would include the expected c++ type in the error message
  48. // here, too (which we can access via typeid(ArgType).name()), however we
  49. // compile with no-rtti, which disables typeid.
  50. args->ThrowError();
  51. }
  52. }
  53. } // namespace internal
  54. } // namespace gin