// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ #define SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ #include #include "gin/converter.h" namespace gin { template <> struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, v8::Local val) { return val; } static bool FromV8(v8::Isolate* isolate, v8::Local val, v8::Local* out) { if (!val->IsArray()) return false; *out = v8::Local::Cast(val); return true; } }; template struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, const std::set& val) { v8::Local result( v8::Array::New(isolate, static_cast(val.size()))); auto context = isolate->GetCurrentContext(); typename std::set::const_iterator it; int i; for (i = 0, it = val.begin(); it != val.end(); ++it, ++i) result->Set(context, i, Converter::ToV8(isolate, *it)).Check(); return result; } static bool FromV8(v8::Isolate* isolate, v8::Local val, std::set* out) { if (!val->IsArray()) return false; auto context = isolate->GetCurrentContext(); std::set result; v8::Local array(v8::Local::Cast(val)); uint32_t length = array->Length(); for (uint32_t i = 0; i < length; ++i) { T item; if (!Converter::FromV8(isolate, array->Get(context, i).ToLocalChecked(), &item)) return false; result.insert(item); } out->swap(result); return true; } }; } // namespace gin #endif // SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_