Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsValue.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Patch Set: Addressed review comments Created April 17, 2013, 7:56 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/JsEngine.cpp ('k') | test/FileSystemJsObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <AdblockPlus.h>
2
3 namespace
4 {
5 std::string fromV8String(v8::Handle<v8::Value> value)
6 {
7 v8::String::Utf8Value stringValue(value);
8 if (stringValue.length())
9 return std::string(*stringValue, stringValue.length());
10 else
11 return std::string();
12 }
13
14 v8::Local<v8::String> toV8String(const std::string& str)
15 {
16 return v8::String::New(str.c_str(), str.length());
17 }
18 }
19
20 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEngine& jsEngine,
21 v8::Handle<v8::Value> value)
22 : jsEngine(jsEngine),
23 value(v8::Persistent<v8::Value>::New(jsEngine.isolate, value))
24 {
25 }
26
27 AdblockPlus::JsValue::~JsValue()
28 {
29 value.Dispose(jsEngine.isolate);
30 }
31
32 bool AdblockPlus::JsValue::IsUndefined() const
33 {
34 return value->IsUndefined();
35 }
36
37 bool AdblockPlus::JsValue::IsNull() const
38 {
39 return value->IsNull();
40 }
41
42 bool AdblockPlus::JsValue::IsString() const
43 {
44 return value->IsString() || value->IsStringObject();
45 }
46
47 bool AdblockPlus::JsValue::IsNumber() const
48 {
49 return value->IsNumber() || value->IsNumberObject();
50 }
51
52 bool AdblockPlus::JsValue::IsBool() const
53 {
54 return value->IsBoolean() || value->IsBooleanObject();
55 }
56
57 bool AdblockPlus::JsValue::IsObject() const
58 {
59 return value->IsObject();
60 }
61
62 bool AdblockPlus::JsValue::IsArray() const
63 {
64 return value->IsArray();
65 }
66
67 bool AdblockPlus::JsValue::IsFunction() const
68 {
69 return value->IsFunction();
70 }
71
72 std::string AdblockPlus::JsValue::AsString() const
73 {
74 const JsEngine::Context context(jsEngine);
75 return fromV8String(value);
76 }
77
78 int64_t AdblockPlus::JsValue::AsInt() const
79 {
80 const JsEngine::Context context(jsEngine);
81 return value->IntegerValue();
82 }
83
84 bool AdblockPlus::JsValue::AsBool() const
85 {
86 const JsEngine::Context context(jsEngine);
87 return value->BooleanValue();
88 }
89
90 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const
91 {
92 if (!IsArray())
93 throw std::runtime_error("Cannot convert a non-array to list");
94
95 const JsEngine::Context context(jsEngine);
96 JsValueList result;
97 v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast(value);
98 uint32_t length = array->Length();
99 for (uint32_t i = 0; i < length; i++)
100 {
101 v8::Local<v8::Value> item = array->Get(i);
102 result.push_back(JsValuePtr(new JsValue(jsEngine, item)));
103 }
104 return result;
105 }
106
107 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const
108 {
109 if (!IsObject())
110 throw new std::runtime_error("Attempting to get property of a non-object");
111
112 const JsEngine::Context context(jsEngine);
113 v8::Local<v8::String> property = toV8String(name);
114 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value);
115 return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
116 }
117
118 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val)
119 {
120 if (!IsObject())
121 throw new std::runtime_error("Attempting to set property on a non-object");
122
123 v8::Local<v8::String> property = toV8String(name);
124 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value);
125 obj->Set(property, val);
126 }
127
128 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val)
129 {
130 const JsEngine::Context context(jsEngine);
131 SetProperty(name, toV8String(val));
132 }
133
134 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
135 {
136 const JsEngine::Context context(jsEngine);
137 SetProperty(name, v8::Integer::New(val));
138 }
139
140 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
141 {
142 const JsEngine::Context context(jsEngine);
143 SetProperty(name, v8::Boolean::New(val));
144 }
145
146 std::string AdblockPlus::JsValue::GetClassName() const
147 {
148 if (!IsObject())
149 throw new std::runtime_error("Cannot get constructor of a non-object");
150
151 const JsEngine::Context context(jsEngine);
152 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value);
153 return fromV8String(obj->GetConstructorName());
154 }
155
156 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(
157 const JsValueList& params,
158 AdblockPlus::JsValuePtr thisPtr) const
159 {
160 if (!IsFunction())
161 throw new std::runtime_error("Attempting to call a non-function");
162
163 const JsEngine::Context context(jsEngine);
164
165 if (!thisPtr)
166 thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine.context->Global()));
167 if (!thisPtr->IsObject())
168 throw new std::runtime_error("`this` pointer has to be an object");
169 v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast(thisPtr- >value);
170
171 size_t argc = params.size();
172 v8::Handle<v8::Value>* argv = new v8::Handle<v8::Value>[argc];
173 for (size_t i = 0; i < argc; ++i)
174 argv[i] = params[i]->value;
175
176 const v8::TryCatch tryCatch;
177 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast(value);
178 v8::Local<v8::Value> result = func->Call(thisObj, argc, argv);
179 delete argv;
180
181 if (tryCatch.HasCaught())
182 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
183
184 return JsValuePtr(new JsValue(jsEngine, result));
185 }
OLDNEW
« no previous file with comments | « src/JsEngine.cpp ('k') | test/FileSystemJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld