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

Side by Side Diff: src/JsValue.cpp

Issue 10184021: Some refactoring of global JavaScript objects (Closed)
Patch Set: Created April 15, 2013, 2:54 p.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') | src/WebRequestJsObject.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 2
3 namespace 3 namespace
4 { 4 {
5 std::string fromV8String(v8::Handle<v8::Value> value) 5 std::string fromV8String(v8::Handle<v8::Value> value)
6 { 6 {
7 v8::String::Utf8Value stringValue(value); 7 v8::String::Utf8Value stringValue(value);
8 if (stringValue.length()) 8 if (stringValue.length())
9 return std::string(*stringValue, stringValue.length()); 9 return std::string(*stringValue, stringValue.length());
10 else 10 else
(...skipping 13 matching lines...) Expand all
24 { 24 {
25 } 25 }
26 26
27 AdblockPlus::JsValue::~JsValue() 27 AdblockPlus::JsValue::~JsValue()
28 { 28 {
29 value.Dispose(jsEngine.isolate); 29 value.Dispose(jsEngine.isolate);
30 } 30 }
31 31
32 bool AdblockPlus::JsValue::IsUndefined() const 32 bool AdblockPlus::JsValue::IsUndefined() const
33 { 33 {
34 const JsEngine::Context context(jsEngine);
34 return value->IsUndefined(); 35 return value->IsUndefined();
35 } 36 }
36 37
37 bool AdblockPlus::JsValue::IsNull() const 38 bool AdblockPlus::JsValue::IsNull() const
38 { 39 {
40 const JsEngine::Context context(jsEngine);
39 return value->IsNull(); 41 return value->IsNull();
40 } 42 }
41 43
42 bool AdblockPlus::JsValue::IsString() const 44 bool AdblockPlus::JsValue::IsString() const
43 { 45 {
46 const JsEngine::Context context(jsEngine);
44 return value->IsString() || value->IsStringObject(); 47 return value->IsString() || value->IsStringObject();
45 } 48 }
46 49
47 bool AdblockPlus::JsValue::IsNumber() const 50 bool AdblockPlus::JsValue::IsNumber() const
48 { 51 {
52 const JsEngine::Context context(jsEngine);
49 return value->IsNumber() || value->IsNumberObject(); 53 return value->IsNumber() || value->IsNumberObject();
50 } 54 }
51 55
52 bool AdblockPlus::JsValue::IsBool() const 56 bool AdblockPlus::JsValue::IsBool() const
53 { 57 {
58 const JsEngine::Context context(jsEngine);
54 return value->IsBoolean() || value->IsBooleanObject(); 59 return value->IsBoolean() || value->IsBooleanObject();
55 } 60 }
56 61
57 bool AdblockPlus::JsValue::IsObject() const 62 bool AdblockPlus::JsValue::IsObject() const
58 { 63 {
64 const JsEngine::Context context(jsEngine);
59 return value->IsObject(); 65 return value->IsObject();
60 } 66 }
61 67
62 bool AdblockPlus::JsValue::IsArray() const 68 bool AdblockPlus::JsValue::IsArray() const
63 { 69 {
70 const JsEngine::Context context(jsEngine);
64 return value->IsArray(); 71 return value->IsArray();
65 } 72 }
66 73
67 bool AdblockPlus::JsValue::IsFunction() const 74 bool AdblockPlus::JsValue::IsFunction() const
68 { 75 {
76 const JsEngine::Context context(jsEngine);
69 return value->IsFunction(); 77 return value->IsFunction();
70 } 78 }
71 79
72 std::string AdblockPlus::JsValue::AsString() const 80 std::string AdblockPlus::JsValue::AsString() const
73 { 81 {
74 const JsEngine::Context context(jsEngine); 82 const JsEngine::Context context(jsEngine);
75 return fromV8String(value); 83 return fromV8String(value);
76 } 84 }
77 85
78 int64_t AdblockPlus::JsValue::AsInt() const 86 int64_t AdblockPlus::JsValue::AsInt() const
(...skipping 18 matching lines...) Expand all
97 v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast(value); 105 v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast(value);
98 uint32_t length = array->Length(); 106 uint32_t length = array->Length();
99 for (uint32_t i = 0; i < length; i++) 107 for (uint32_t i = 0; i < length; i++)
100 { 108 {
101 v8::Local<v8::Value> item = array->Get(i); 109 v8::Local<v8::Value> item = array->Get(i);
102 result.push_back(JsValuePtr(new JsValue(jsEngine, item))); 110 result.push_back(JsValuePtr(new JsValue(jsEngine, item)));
103 } 111 }
104 return result; 112 return result;
105 } 113 }
106 114
115 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
116 {
117 if (!IsObject())
118 throw new std::runtime_error("Attempting to get propert list for a non-objec t");
119
120 const JsEngine::Context context(jsEngine);
121 const v8::Persistent<v8::Object> object = v8::Persistent<v8::Object>::Cast(val ue);
122 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper tyNames()))->AsList();
123 std::vector<std::string> result;
124 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++ it)
125 result.push_back((*it)->AsString());
126 return result;
127 }
128
129
107 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const 130 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const
108 { 131 {
109 if (!IsObject()) 132 if (!IsObject())
110 throw new std::runtime_error("Attempting to get property of a non-object"); 133 throw new std::runtime_error("Attempting to get property of a non-object");
111 134
112 const JsEngine::Context context(jsEngine); 135 const JsEngine::Context context(jsEngine);
113 v8::Local<v8::String> property = toV8String(name); 136 v8::Local<v8::String> property = toV8String(name);
114 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); 137 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value);
115 return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); 138 return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
116 } 139 }
(...skipping 13 matching lines...) Expand all
130 const JsEngine::Context context(jsEngine); 153 const JsEngine::Context context(jsEngine);
131 SetProperty(name, toV8String(val)); 154 SetProperty(name, toV8String(val));
132 } 155 }
133 156
134 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) 157 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
135 { 158 {
136 const JsEngine::Context context(jsEngine); 159 const JsEngine::Context context(jsEngine);
137 SetProperty(name, v8::Integer::New(val)); 160 SetProperty(name, v8::Integer::New(val));
138 } 161 }
139 162
163 void AdblockPlus::JsValue::SetProperty(const std::string& name, JsValuePtr val)
164 {
165 const JsEngine::Context context(jsEngine);
166 SetProperty(name, val->value);
167 }
168
140 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) 169 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
141 { 170 {
142 const JsEngine::Context context(jsEngine); 171 const JsEngine::Context context(jsEngine);
143 SetProperty(name, v8::Boolean::New(val)); 172 SetProperty(name, v8::Boolean::New(val));
144 } 173 }
145 174
146 std::string AdblockPlus::JsValue::GetClassName() const 175 std::string AdblockPlus::JsValue::GetClassName() const
147 { 176 {
148 if (!IsObject()) 177 if (!IsObject())
149 throw new std::runtime_error("Cannot get constructor of a non-object"); 178 throw new std::runtime_error("Cannot get constructor of a non-object");
(...skipping 26 matching lines...) Expand all
176 const v8::TryCatch tryCatch; 205 const v8::TryCatch tryCatch;
177 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast(value); 206 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast(value);
178 v8::Local<v8::Value> result = func->Call(thisObj, argc, argv); 207 v8::Local<v8::Value> result = func->Call(thisObj, argc, argv);
179 delete argv; 208 delete argv;
180 209
181 if (tryCatch.HasCaught()) 210 if (tryCatch.HasCaught())
182 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 211 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
183 212
184 return JsValuePtr(new JsValue(jsEngine, result)); 213 return JsValuePtr(new JsValue(jsEngine, result));
185 } 214 }
OLDNEW
« no previous file with comments | « src/JsEngine.cpp ('k') | src/WebRequestJsObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld