electron/native_mate/arguments.h

103 lines
2.4 KiB
C
Raw Normal View History

2014-04-15 03:04:36 +00:00
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#ifndef NATIVE_MATE_ARGUMENTS_H_
#define NATIVE_MATE_ARGUMENTS_H_
#include "base/basictypes.h"
#include "native_mate/compat.h"
#include "native_mate/converter.h"
namespace mate {
// Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
// with Converter to make it easier to marshall arguments and return values
// between V8 and C++.
class Arguments {
public:
Arguments();
explicit Arguments(const MATE_METHOD_ARGS_TYPE& info);
~Arguments();
v8::Local<v8::Object> GetHolder() const {
return info_->Holder();
}
2014-04-15 03:04:36 +00:00
template<typename T>
bool GetHolder(T* out) {
return ConvertFromV8(isolate_, info_->Holder(), out);
}
template<typename T>
bool GetData(T* out) {
return ConvertFromV8(isolate_, info_->Data(), out);
}
template<typename T>
bool GetNext(T* out) {
if (next_ >= info_->Length()) {
insufficient_arguments_ = true;
return false;
}
v8::Local<v8::Value> val = (*info_)[next_];
bool success = ConvertFromV8(isolate_, val, out);
if (success)
next_++;
return success;
2014-04-15 03:04:36 +00:00
}
template<typename T>
bool GetRemaining(std::vector<T>* out) {
if (next_ >= info_->Length()) {
insufficient_arguments_ = true;
return false;
}
int remaining = info_->Length() - next_;
out->resize(remaining);
for (int i = 0; i < remaining; ++i) {
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> val = (*info_)[next_++];
2014-04-15 03:04:36 +00:00
if (!ConvertFromV8(isolate_, val, &out->at(i)))
return false;
}
return true;
}
2015-05-22 11:11:02 +00:00
v8::Local<v8::Object> GetThis() {
return info_->This();
}
bool IsConstructCall() const {
return info_->IsConstructCall();
}
2014-04-22 07:57:11 +00:00
int Length() const {
return info_->Length();
}
2014-04-15 03:04:36 +00:00
#if NODE_VERSION_AT_LEAST(0, 11, 0)
template<typename T>
void Return(T val) {
info_->GetReturnValue().Set(ConvertToV8(isolate_, val));
}
#endif
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> PeekNext() const;
2014-04-15 03:04:36 +00:00
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> ThrowError() const;
v8::Local<v8::Value> ThrowError(const std::string& message) const;
v8::Local<v8::Value> ThrowTypeError(const std::string& message) const;
2014-04-15 03:04:36 +00:00
v8::Isolate* isolate() const { return isolate_; }
private:
v8::Isolate* isolate_;
const MATE_METHOD_ARGS_TYPE* info_;
int next_;
bool insufficient_arguments_;
};
} // namespace mate
#endif // NATIVE_MATE_ARGUMENTS_H_