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.
5 #ifndef GIN_ARGUMENTS_H_
6 #define GIN_ARGUMENTS_H_
8 #include "gin/converter.h"
9 #include "gin/gin_export.h"
13 // 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++.
16 class GIN_EXPORT Arguments {
19 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
23 bool GetHolder(T* out) {
24 return ConvertFromV8(isolate_, info_->Holder(), out);
28 bool GetData(T* out) {
29 return ConvertFromV8(isolate_, info_->Data(), out);
33 bool GetNext(T* out) {
34 if (next_ >= info_->Length()) {
35 insufficient_arguments_ = true;
38 v8::Local<v8::Value> val = (*info_)[next_++];
39 return ConvertFromV8(isolate_, val, out);
43 bool GetRemaining(std::vector<T>* out) {
44 if (next_ >= info_->Length()) {
45 insufficient_arguments_ = true;
48 int remaining = info_->Length() - next_;
49 out->resize(remaining);
50 for (int i = 0; i < remaining; ++i) {
51 v8::Local<v8::Value> val = (*info_)[next_++];
52 if (!ConvertFromV8(isolate_, val, &out->at(i)))
59 if (next_ >= info_->Length())
66 return info_->Length();
71 v8::Local<v8::Value> v8_value;
72 if (!TryConvertToV8(isolate_, val, &v8_value))
74 info_->GetReturnValue().Set(v8_value);
77 // Returns the creation context of the Holder.
78 v8::Local<v8::Context> GetHolderCreationContext();
80 // Always check the return value whether the handle is empty before
81 // dereferencing the handle.
82 v8::Local<v8::Value> PeekNext() const;
84 // 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;
89 void ThrowError() const;
90 void ThrowTypeError(const std::string& message) const;
92 v8::Isolate* isolate() const { return isolate_; }
94 // Allows the function handler to distinguish between normal invocation
95 // and object construction.
96 bool IsConstructCall() const;
99 v8::Isolate* isolate_;
100 const v8::FunctionCallbackInfo<v8::Value>* info_;
102 bool insufficient_arguments_;
107 #endif // GIN_ARGUMENTS_H_