17 #include <glog/logging.h> 33 ReturnVal<JsValue> ToJsObject(
bool value) {
35 return v8::BooleanObject::New(
GetIsolate(), value);
36 #elif defined(USING_JSC) 37 LocalVar<JsValue> value_js =
ToJsValue(value);
38 JSValueRef arg = value_js;
43 ReturnVal<JsValue> ToJsObject(
double value) {
45 return v8::NumberObject::New(
GetIsolate(), value);
46 #elif defined(USING_JSC) 47 LocalVar<JsValue> value_js =
ToJsValue(value);
48 JSValueRef arg = value_js;
53 ReturnVal<JsValue> ToJsObject(
const std::string& value) {
56 #elif defined(USING_JSC) 57 LocalVar<JsValue> value_js =
ToJsValue(value);
58 JSValueRef arg = value_js;
64 ExceptionOr<void> StoreValue(Handle<JsValue> input, proto::Value* output,
65 std::vector<ReturnVal<JsValue>>* memory);
66 ReturnVal<JsValue> InternalFromStored(
const proto::Value& item);
68 ExceptionOr<void> StoreObject(
proto::ValueType kind, Handle<JsObject>
object,
69 proto::Object* output,
70 std::vector<Handle<JsValue>>* memory) {
74 output->clear_array_length();
76 output->clear_entries();
81 LocalVar<JsValue> except;
82 LocalVar<JsValue> value =
GetMemberRaw(
object, property, &except);
86 child->set_key(property);
92 ExceptionOr<void> StoreValue(Handle<JsValue> input, proto::Value* output,
93 std::vector<ReturnVal<JsValue>>* memory) {
101 "Duplicate copies of the same object are not supported.");
103 memory->push_back(input);
107 output->set_kind(kind);
123 output->set_value_string(str.data(), str.size());
139 CHECK(buffer.TryConvert(input));
140 output->set_value_bytes(buffer.data(), buffer.size());
147 LocalVar<JsObject>
object = UnsafeJsCast<JsObject>(input);
153 proto::Object* output_object = output->mutable_value_object();
154 return StoreObject(kind,
object, output_object, memory);
165 ReturnVal<JsValue> FromStoredObject(
const proto::Object&
object) {
166 LocalVar<JsObject> ret;
167 if (
object.has_array_length())
173 LocalVar<JsValue> value = InternalFromStored(entry.value());
180 ReturnVal<JsValue> InternalFromStored(
const proto::Value& item) {
181 DCHECK(item.IsInitialized());
182 switch (item.kind()) {
188 DCHECK(item.has_value_bool());
191 DCHECK(item.has_value_number());
194 DCHECK(item.has_value_string());
198 DCHECK(item.has_value_bool());
199 return ToJsObject(item.value_bool());
201 DCHECK(item.has_value_number());
202 return ToJsObject(item.value_number());
204 DCHECK(item.has_value_string());
205 return ToJsObject(item.value_string());
218 DCHECK(item.has_value_bytes());
219 const std::string& str = item.value_bytes();
220 ByteBuffer temp(reinterpret_cast<const uint8_t*>(&str[0]), str.size());
221 return temp.ToJsValue(item.kind());
225 return FromStoredObject(item.value_object());
227 LOG(FATAL) <<
"Invalid stored value " << item.kind();
234 std::vector<ReturnVal<JsValue>> seen;
235 return StoreValue(input.
ToJsValue(), result, &seen);
240 CHECK(ret.
TryConvert(InternalFromStored(value)));
Any LoadFromProto(const proto::Value &value)
ExceptionOr< void > StoreInProto(Any input, proto::Value *result)
ReturnVal< JsObject > CreateObject()
ReturnVal< JsValue > JsUndefined()
bool contains(const std::vector< T > &vec, U &&elem)
bool IsObject(Handle< JsValue > value)
static JsError Rethrow(Handle< JsValue > error)
bool IsNullOrUndefined(Handle< JsValue > value)
std::vector< std::string > GetMemberNames(Handle< JsObject > object)
ReturnVal< JsValue > JsNull()
bool BooleanFromValue(Handle< JsValue > value)
ReturnVal< JsObject > CreateArray(size_t length)
ReturnVal< JsValue > ToJsValue(T &&source)
ReturnVal< JsValue > RawToJsValue(Handle< T > source)
JSValueRef CreateNativeObject(const std::string &name, JSValueRef *args, size_t argc)
#define RETURN_IF_ERROR(code)
proto::ValueType GetValueType(Handle< JsValue > value)
double NumberFromValue(Handle< JsValue > value)
void SetMemberRaw(Handle< JsObject > object, const std::string &name, Handle< JsValue > value)
std::string ConvertToString(Handle< JsValue > value)
static JsError DOMException(ExceptionCode code)
ReturnVal< JsValue > ToJsValue() const override
size_t ArrayLength(Handle< JsObject > value)
ReturnVal< JsString > JsStringFromUtf8(const std::string &str)
v8::Isolate * GetIsolate()
ReturnVal< JsValue > GetMemberRaw(Handle< JsObject > object, const std::string &name, LocalVar< JsValue > *exception=nullptr)
bool TryConvert(Handle< JsValue > value) override
bool IsBuiltInObject(Handle< JsObject > object)