From 1e5e0194bd40733a964118803587794460fade34 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 23 Sep 2013 22:48:55 +0800 Subject: [PATCH] Add convient function for converting args from V8 Arguments. --- common/v8_conversions.h | 117 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/common/v8_conversions.h b/common/v8_conversions.h index 35a140c21b4a..b483d665915d 100644 --- a/common/v8_conversions.h +++ b/common/v8_conversions.h @@ -12,7 +12,7 @@ #include "browser/api/atom_api_window.h" #include "v8/include/v8.h" -// Trick to overload functions by return value's type. +// Convert V8 value to arbitrary supported types. struct FromV8Value { explicit FromV8Value(v8::Handle value) : value_(value) {} @@ -58,6 +58,7 @@ struct FromV8Value { v8::Handle value_; }; +// Convert arbitrary supported native type to V8 value. inline v8::Handle ToV8Value(const base::FilePath& path) { std::string path_string(path.AsUTF8Unsafe()); return v8::String::New(path_string.data(), path_string.size()); @@ -71,7 +72,6 @@ inline v8::Handle ToV8Value(int code) { return v8::Integer::New(code); } -inline v8::Handle ToV8Value(const std::vector& paths) { v8::Handle result = v8::Array::New(paths.size()); for (size_t i = 0; i < paths.size(); ++i) @@ -79,4 +79,117 @@ v8::Handle ToV8Value(const std::vector& paths) { return result; } +// Check if a V8 Value is of specified type. +template inline +bool V8ValueCanBeConvertedTo(v8::Handle value) { + return false; +} + +template<> inline +bool V8ValueCanBeConvertedTo(v8::Handle value) { + return value->IsNumber(); +} + +template<> inline +bool V8ValueCanBeConvertedTo(v8::Handle value) { + return value->IsString(); +} + +template<> inline +bool V8ValueCanBeConvertedTo(v8::Handle value) { + return V8ValueCanBeConvertedTo(value); +} + +template<> inline +bool V8ValueCanBeConvertedTo>( + v8::Handle value) { + return value->IsArray(); +} + +template<> +bool V8ValueCanBeConvertedTo(v8::Handle value) { + using atom::api::Window; + if (value->IsObject()) { + Window* window = Window::Unwrap(value->ToObject()); + if (window && window->window()) + return true; + } + return false; +} + +template<> inline +bool V8ValueCanBeConvertedTo>( + v8::Handle value) { + return value->IsFunction(); +} + +// Check and convert V8's Arguments to native types. +template +bool FromV8Arguments(const v8::Arguments& args, T1* value) { + if (!V8ValueCanBeConvertedTo(args[index])) + return false; + *value = static_cast(FromV8Value(args[index])); + return true; +} + +template +bool FromV8Arguments(const v8::Arguments& args, T1* a1, T2* a2) { + return FromV8Arguments(args, a1) && FromV8Arguments(args, a2); +} + +template +bool FromV8Arguments(const v8::Arguments& args, T1* a1, T2* a2, T3* a3) { + return FromV8Arguments(args, a1, a2) && + FromV8Arguments(args, a3); +} + +template +bool FromV8Arguments(const v8::Arguments& args, + T1* a1, + T2* a2, + T3* a3, + T4* a4) { + return FromV8Arguments(args, a1, a2, a3) && + FromV8Arguments(args, a4); +} + +template +bool FromV8Arguments(const v8::Arguments& args, + T1* a1, + T2* a2, + T3* a3, + T4* a4, + T5* a5) { + return FromV8Arguments(args, a1, a2, a3, a4) && + FromV8Arguments(args, a5); +} + +template +bool FromV8Arguments(const v8::Arguments& args, + T1* a1, + T2* a2, + T3* a3, + T4* a4, + T5* a5, + T6* a6) { + return FromV8Arguments(args, a1, a2, a3, a4, a5) && + FromV8Arguments(args, a6); +} + +template +bool FromV8Arguments(const v8::Arguments& args, + T1* a1, + T2* a2, + T3* a3, + T4* a4, + T5* a5, + T6* a6, + T7* a7) { + return + FromV8Arguments(args, a1, a2, a3, a4, a5, a6) && + FromV8Arguments(args, a7); +} + #endif // COMMON_V8_CONVERSIONS_H_