Skip to content

Commit

Permalink
Convert the rest of the functions in core.cc to use CreateFunctionTem…
Browse files Browse the repository at this point in the history
…plate.

BUG=

Review URL: https://codereview.chromium.org/89723002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237492 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
[email protected] committed Nov 27, 2013
1 parent 2ff0a25 commit 7618ebb
Show file tree
Hide file tree
Showing 20 changed files with 246 additions and 155 deletions.
19 changes: 16 additions & 3 deletions gin/arguments.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@

namespace gin {

Arguments::Arguments()
: isolate_(NULL),
info_(NULL),
next_(0),
insufficient_arguments_(false) {
}

Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
: isolate_(info.GetIsolate()),
info_(info),
info_(&info),
next_(0),
insufficient_arguments_(false) {
}
Expand All @@ -20,9 +27,9 @@ Arguments::~Arguments() {
}

v8::Handle<v8::Value> Arguments::PeekNext() {
if (next_ >= info_.Length())
if (next_ >= info_->Length())
return v8::Handle<v8::Value>();
return info_[next_];
return (*info_)[next_];
}

void Arguments::ThrowError() {
Expand All @@ -38,4 +45,10 @@ void Arguments::ThrowTypeError(const std::string& message) {
StringToV8(isolate_, message)));
}

template<>
bool Arguments::GetNext<Arguments>(Arguments* out) {
*out = *this;
return true;
}

} // namespace gin
28 changes: 15 additions & 13 deletions gin/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,50 @@ namespace gin {
// between V8 and C++.
class Arguments {
public:
Arguments();
explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
~Arguments();

template<typename T>
// TODO(aa): Rename GetHolder().
bool Holder(T* out) {
return ConvertFromV8(info_.Holder(), out);
return ConvertFromV8(isolate_, info_->Holder(), out);
}

template<typename T>
bool GetData(T* out) {
return ConvertFromV8(info_.Data(), out);
return ConvertFromV8(isolate_, info_->Data(), out);
}

template<typename T>
bool GetNext(T* out) {
if (next_ >= info_.Length()) {
if (next_ >= info_->Length()) {
insufficient_arguments_ = true;
return false;
}
v8::Handle<v8::Value> val = info_[next_++];
return ConvertFromV8(val, out);
v8::Handle<v8::Value> val = (*info_)[next_++];
return ConvertFromV8(isolate_, val, out);
}

template<typename T>
bool GetRemaining(std::vector<T>* out) {
if (next_ >= info_.Length()) {
if (next_ >= info_->Length()) {
insufficient_arguments_ = true;
return false;
}
int remaining = info_.Length() - next_;
int remaining = info_->Length() - next_;
out->resize(remaining);
for (int i = 0; i < remaining; ++i) {
v8::Handle<v8::Value> val = info_[next_++];
if (!ConvertFromV8(val, &out->at(i)))
v8::Handle<v8::Value> val = (*info_)[next_++];
if (!ConvertFromV8(isolate_, val, &out->at(i)))
return false;
}
return true;
}

template<typename T>
void Return(T val) {
info_.GetReturnValue().Set(ConvertToV8(isolate_, val));
info_->GetReturnValue().Set(ConvertToV8(isolate_, val));
}

v8::Handle<v8::Value> PeekNext();
Expand All @@ -69,13 +70,14 @@ class Arguments {

private:
v8::Isolate* isolate_;
const v8::FunctionCallbackInfo<v8::Value>& info_;
const v8::FunctionCallbackInfo<v8::Value>* info_;
int next_;
bool insufficient_arguments_;

DISALLOW_COPY_AND_ASSIGN(Arguments);
};

template<>
bool Arguments::GetNext<Arguments>(Arguments* out);

} // namespace gin

#endif // GIN_ARGUMENTS_H_
26 changes: 12 additions & 14 deletions gin/array_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,14 @@ void ArrayBuffer::Private::WeakCallback(

// ArrayBuffer ----------------------------------------------------------------

ArrayBuffer::ArrayBuffer(v8::Isolate* isolate)
: isolate_(isolate),
bytes_(0),
ArrayBuffer::ArrayBuffer()
: bytes_(0),
num_bytes_(0) {
}

ArrayBuffer::ArrayBuffer(v8::Isolate* isolate,
v8::Handle<v8::ArrayBuffer> array)
: isolate_(isolate) {
private_ = ArrayBuffer::Private::From(isolate_, array);
v8::Handle<v8::ArrayBuffer> array) {
private_ = ArrayBuffer::Private::From(isolate, array);
bytes_ = private_->buffer();
num_bytes_ = private_->length();
}
Expand All @@ -131,19 +129,19 @@ ArrayBuffer::~ArrayBuffer() {

// Converter<ArrayBuffer> -----------------------------------------------------

bool Converter<ArrayBuffer>::FromV8(v8::Handle<v8::Value> val,
bool Converter<ArrayBuffer>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
ArrayBuffer* out) {
if (!val->IsArrayBuffer())
return false;
*out = ArrayBuffer(out->isolate(), v8::Handle<v8::ArrayBuffer>::Cast(val));
*out = ArrayBuffer(isolate, v8::Handle<v8::ArrayBuffer>::Cast(val));
return true;
}

// ArrayBufferView ------------------------------------------------------------

ArrayBufferView::ArrayBufferView(v8::Isolate* isolate)
: array_buffer_(isolate),
offset_(0),
ArrayBufferView::ArrayBufferView()
: offset_(0),
num_bytes_(0) {
}

Expand All @@ -159,12 +157,12 @@ ArrayBufferView::~ArrayBufferView() {

// Converter<ArrayBufferView> -------------------------------------------------

bool Converter<ArrayBufferView>::FromV8(v8::Handle<v8::Value> val,
bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
ArrayBufferView* out) {
if (!val->IsArrayBufferView())
return false;
*out = ArrayBufferView(out->isolate(),
v8::Handle<v8::ArrayBufferView>::Cast(val));
*out = ArrayBufferView(isolate, v8::Handle<v8::ArrayBufferView>::Cast(val));
return true;
}

Expand Down
16 changes: 8 additions & 8 deletions gin/array_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {

class ArrayBuffer {
public:
ArrayBuffer();
explicit ArrayBuffer(v8::Isolate* isolate);
ArrayBuffer(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> buffer);
~ArrayBuffer();

void* bytes() const { return bytes_; }
size_t num_bytes() const { return num_bytes_; }

v8::Isolate* isolate() const { return isolate_; }

private:
class Private;

v8::Isolate* isolate_;
scoped_refptr<Private> private_;
void* bytes_;
size_t num_bytes_;

DISALLOW_COPY(ArrayBuffer);
};

template<>
struct Converter<ArrayBuffer> {
static bool FromV8(v8::Handle<v8::Value> val,
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
ArrayBuffer* out);
};

class ArrayBufferView {
public:
explicit ArrayBufferView(v8::Isolate* isolate);
ArrayBufferView();
ArrayBufferView(v8::Isolate* isolate, v8::Handle<v8::ArrayBufferView> view);
~ArrayBufferView();

Expand All @@ -59,17 +59,17 @@ class ArrayBufferView {
}
size_t num_bytes() const { return num_bytes_; }

v8::Isolate* isolate() const { return array_buffer_.isolate(); }

private:
ArrayBuffer array_buffer_;
size_t offset_;
size_t num_bytes_;

DISALLOW_COPY(ArrayBufferView);
};

template<>
struct Converter<ArrayBufferView> {
static bool FromV8(v8::Handle<v8::Value> val,
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
ArrayBufferView* out);
};

Expand Down
41 changes: 24 additions & 17 deletions gin/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Handle<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
return Boolean::New(val).As<Value>();
}

bool Converter<bool>::FromV8(Handle<Value> val, bool* out) {
bool Converter<bool>::FromV8(Isolate* isolate, Handle<Value> val, bool* out) {
*out = val->BooleanValue();
return true;
}
Expand All @@ -33,7 +33,8 @@ Handle<Value> Converter<int32_t>::ToV8(Isolate* isolate, int32_t val) {
return Integer::New(val, isolate).As<Value>();
}

bool Converter<int32_t>::FromV8(Handle<Value> val, int32_t* out) {
bool Converter<int32_t>::FromV8(Isolate* isolate, Handle<Value> val,
int32_t* out) {
if (!val->IsInt32())
return false;
*out = val->Int32Value();
Expand All @@ -44,7 +45,8 @@ Handle<Value> Converter<uint32_t>::ToV8(Isolate* isolate, uint32_t val) {
return Integer::NewFromUnsigned(val, isolate).As<Value>();
}

bool Converter<uint32_t>::FromV8(Handle<Value> val, uint32_t* out) {
bool Converter<uint32_t>::FromV8(Isolate* isolate, Handle<Value> val,
uint32_t* out) {
if (!val->IsUint32())
return false;
*out = val->Uint32Value();
Expand All @@ -55,7 +57,8 @@ Handle<Value> Converter<int64_t>::ToV8(Isolate* isolate, int64_t val) {
return Number::New(isolate, static_cast<double>(val)).As<Value>();
}

bool Converter<int64_t>::FromV8(Handle<Value> val, int64_t* out) {
bool Converter<int64_t>::FromV8(Isolate* isolate, Handle<Value> val,
int64_t* out) {
if (!val->IsNumber())
return false;
// Even though IntegerValue returns int64_t, JavaScript cannot represent
Expand All @@ -68,7 +71,8 @@ Handle<Value> Converter<uint64_t>::ToV8(Isolate* isolate, uint64_t val) {
return Number::New(isolate, static_cast<double>(val)).As<Value>();
}

bool Converter<uint64_t>::FromV8(Handle<Value> val, uint64_t* out) {
bool Converter<uint64_t>::FromV8(Isolate* isolate, Handle<Value> val,
uint64_t* out) {
if (!val->IsNumber())
return false;
*out = static_cast<uint64_t>(val->IntegerValue());
Expand All @@ -79,7 +83,8 @@ Handle<Value> Converter<double>::ToV8(Isolate* isolate, double val) {
return Number::New(isolate, val).As<Value>();
}

bool Converter<double>::FromV8(Handle<Value> val, double* out) {
bool Converter<double>::FromV8(Isolate* isolate, Handle<Value> val,
double* out) {
if (!val->IsNumber())
return false;
*out = val->NumberValue();
Expand All @@ -94,7 +99,7 @@ Handle<Value> Converter<std::string>::ToV8(Isolate* isolate,
static_cast<uint32_t>(val.length()));
}

bool Converter<std::string>::FromV8(Handle<Value> val,
bool Converter<std::string>::FromV8(Isolate* isolate, Handle<Value> val,
std::string* out) {
if (!val->IsString())
return false;
Expand All @@ -105,59 +110,61 @@ bool Converter<std::string>::FromV8(Handle<Value> val,
return true;
}

bool Converter<Handle<Function> >::FromV8(Handle<Value> val,
bool Converter<Handle<Function> >::FromV8(Isolate* isolate, Handle<Value> val,
Handle<Function>* out) {
if (!val->IsFunction())
return false;
*out = Handle<Function>::Cast(val);
return true;
}

Handle<Value> Converter<Handle<Object> >::ToV8(v8::Isolate* isolate,
Handle<Value> Converter<Handle<Object> >::ToV8(Isolate* isolate,
Handle<Object> val) {
return val.As<Value>();
}

bool Converter<Handle<Object> >::FromV8(Handle<Value> val,
bool Converter<Handle<Object> >::FromV8(Isolate* isolate, Handle<Value> val,
Handle<Object>* out) {
if (!val->IsObject())
return false;
*out = Handle<Object>::Cast(val);
return true;
}

Handle<Value> Converter<Handle<ArrayBuffer> >::ToV8(v8::Isolate* isolate,
Handle<Value> Converter<Handle<ArrayBuffer> >::ToV8(Isolate* isolate,
Handle<ArrayBuffer> val) {
return val.As<Value>();
}

bool Converter<Handle<ArrayBuffer> >::FromV8(Handle<Value> val,
bool Converter<Handle<ArrayBuffer> >::FromV8(Isolate* isolate,
Handle<Value> val,
Handle<ArrayBuffer>* out) {
if (!val->IsArrayBuffer())
return false;
*out = Handle<ArrayBuffer>::Cast(val);
return true;
}

Handle<Value> Converter<Handle<External> >::ToV8(v8::Isolate* isolate,
Handle<Value> Converter<Handle<External> >::ToV8(Isolate* isolate,
Handle<External> val) {
return val.As<Value>();
}

bool Converter<Handle<External> >::FromV8(Handle<Value> val,
bool Converter<Handle<External> >::FromV8(Isolate* isolate,
v8::Handle<Value> val,
Handle<External>* out) {
if (!val->IsExternal())
return false;
*out = Handle<External>::Cast(val);
return true;
}

Handle<Value> Converter<Handle<Value> >::ToV8(v8::Isolate* isolate,
Handle<Value> Converter<Handle<Value> >::ToV8(Isolate* isolate,
Handle<Value> val) {
return val;
}

bool Converter<Handle<Value> >::FromV8(Handle<Value> val,
bool Converter<Handle<Value> >::FromV8(Isolate* isolate, Handle<Value> val,
Handle<Value>* out) {
*out = val;
return true;
Expand All @@ -175,7 +182,7 @@ std::string V8ToString(v8::Handle<v8::Value> value) {
if (value.IsEmpty())
return std::string();
std::string result;
if (!ConvertFromV8(value, &result))
if (!ConvertFromV8(NULL, value, &result))
return std::string();
return result;
}
Expand Down
Loading

0 comments on commit 7618ebb

Please sign in to comment.