blob: c0a7bc2171d4fd51d2d794b47038484716a493a4 [file] [log] [blame]
[email protected]e87f3122013-11-12 00:41:271// 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#ifndef GIN_ARGUMENTS_H_
6#define GIN_ARGUMENTS_H_
7
[email protected]e87f3122013-11-12 00:41:278#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:349#include "gin/gin_export.h"
[email protected]e87f3122013-11-12 00:41:2710
11namespace gin {
12
[email protected]60531d52013-11-27 02:10:1513// Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
14// with Converter to make it easier to marshall arguments and return values
15// between V8 and C++.
[email protected]48c21632013-12-12 21:32:3416class GIN_EXPORT Arguments {
[email protected]e87f3122013-11-12 00:41:2717 public:
[email protected]7618ebbb2013-11-27 03:38:2618 Arguments();
[email protected]e87f3122013-11-12 00:41:2719 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
20 ~Arguments();
21
22 template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2523 bool GetHolder(T* out) {
[email protected]7618ebbb2013-11-27 03:38:2624 return ConvertFromV8(isolate_, info_->Holder(), out);
[email protected]e87f3122013-11-12 00:41:2725 }
26
27 template<typename T>
[email protected]314cde12013-11-23 20:26:5128 bool GetData(T* out) {
[email protected]7618ebbb2013-11-27 03:38:2629 return ConvertFromV8(isolate_, info_->Data(), out);
[email protected]314cde12013-11-23 20:26:5130 }
31
32 template<typename T>
[email protected]e87f3122013-11-12 00:41:2733 bool GetNext(T* out) {
[email protected]7618ebbb2013-11-27 03:38:2634 if (next_ >= info_->Length()) {
[email protected]e87f3122013-11-12 00:41:2735 insufficient_arguments_ = true;
36 return false;
37 }
deepak.sfaaa1b62015-04-30 07:30:4838 v8::Local<v8::Value> val = (*info_)[next_++];
[email protected]7618ebbb2013-11-27 03:38:2639 return ConvertFromV8(isolate_, val, out);
[email protected]e87f3122013-11-12 00:41:2740 }
41
42 template<typename T>
[email protected]858eeea0a2013-11-19 05:17:1243 bool GetRemaining(std::vector<T>* out) {
[email protected]7618ebbb2013-11-27 03:38:2644 if (next_ >= info_->Length()) {
[email protected]858eeea0a2013-11-19 05:17:1245 insufficient_arguments_ = true;
46 return false;
47 }
[email protected]7618ebbb2013-11-27 03:38:2648 int remaining = info_->Length() - next_;
[email protected]858eeea0a2013-11-19 05:17:1249 out->resize(remaining);
50 for (int i = 0; i < remaining; ++i) {
deepak.sfaaa1b62015-04-30 07:30:4851 v8::Local<v8::Value> val = (*info_)[next_++];
[email protected]7618ebbb2013-11-27 03:38:2652 if (!ConvertFromV8(isolate_, val, &out->at(i)))
[email protected]858eeea0a2013-11-19 05:17:1253 return false;
54 }
55 return true;
56 }
57
[email protected]97f9a7952014-03-14 11:50:3358 bool Skip() {
59 if (next_ >= info_->Length())
60 return false;
61 next_++;
62 return true;
63 }
64
65 int Length() const {
66 return info_->Length();
67 }
68
[email protected]858eeea0a2013-11-19 05:17:1269 template<typename T>
[email protected]e87f3122013-11-12 00:41:2770 void Return(T val) {
bashidbd2ef9bb2015-06-02 01:39:3271 v8::Local<v8::Value> v8_value;
72 if (!TryConvertToV8(isolate_, val, &v8_value))
73 return;
74 info_->GetReturnValue().Set(v8_value);
[email protected]e87f3122013-11-12 00:41:2775 }
76
rdevlin.cronind982fdf2017-03-23 22:17:4377 // Returns the creation context of the Holder.
78 v8::Local<v8::Context> GetHolderCreationContext();
79
jochen87d2fee2015-07-13 08:21:3480 // Always check the return value whether the handle is empty before
81 // dereferencing the handle.
deepak.sfaaa1b62015-04-30 07:30:4882 v8::Local<v8::Value> PeekNext() const;
[email protected]97f21ca2013-11-17 17:46:0783
rdevlin.cronincd6754502017-04-19 16:14:1484 // Returns all arguments. Since this doesn't require any conversion, it
85 // cannot fail. This does not rely on or modify the current position in the
86 // array used by Get/PeekNext().
87 std::vector<v8::Local<v8::Value>> GetAll() const;
88
[email protected]481d2492013-12-13 23:55:2589 void ThrowError() const;
90 void ThrowTypeError(const std::string& message) const;
[email protected]e87f3122013-11-12 00:41:2791
92 v8::Isolate* isolate() const { return isolate_; }
93
[email protected]744a4942014-07-18 16:46:1494 // Allows the function handler to distinguish between normal invocation
95 // and object construction.
96 bool IsConstructCall() const;
97
[email protected]e87f3122013-11-12 00:41:2798 private:
99 v8::Isolate* isolate_;
[email protected]7618ebbb2013-11-27 03:38:26100 const v8::FunctionCallbackInfo<v8::Value>* info_;
[email protected]e87f3122013-11-12 00:41:27101 int next_;
102 bool insufficient_arguments_;
[email protected]e87f3122013-11-12 00:41:27103};
104
[email protected]e87f3122013-11-12 00:41:27105} // namespace gin
106
107#endif // GIN_ARGUMENTS_H_