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

Delta Between Two Patch Sets: src/JsValue.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Left Patch Set: Created April 12, 2013, 2:55 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/JsEngine.cpp ('k') | test/FileSystemJsObject.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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)
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
5 v8::Local<v8::String> toV8String(const std::string& str) 14 v8::Local<v8::String> toV8String(const std::string& str)
6 { 15 {
7 return v8::String::New(str.c_str(), str.length()); 16 return v8::String::New(str.c_str(), str.length());
8 } 17 }
9 } 18 }
10 19
11 AdblockPlus::JsValue::JsValue(v8::Isolate* isolate, 20 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEngine& jsEngine,
12 v8::Handle<v8::Context> context, v8::Handle<v8::Value> value) 21 v8::Handle<v8::Value> value)
13 : isolate(isolate), 22 : jsEngine(jsEngine),
14 context(v8::Persistent<v8::Context>::New(isolate, context)), 23 value(v8::Persistent<v8::Value>::New(jsEngine.isolate, value))
15 value(v8::Persistent<v8::Value>::New(isolate, value))
16 { 24 {
17 } 25 }
18 26
19 AdblockPlus::JsValue::~JsValue() 27 AdblockPlus::JsValue::~JsValue()
20 { 28 {
21 context.Dispose(isolate); 29 value.Dispose(jsEngine.isolate);
22 value.Dispose(isolate);
23 } 30 }
24 31
25 bool AdblockPlus::JsValue::IsUndefined() const 32 bool AdblockPlus::JsValue::IsUndefined() const
26 { 33 {
27 return value->IsUndefined(); 34 return value->IsUndefined();
28 } 35 }
29 36
30 bool AdblockPlus::JsValue::IsNull() const 37 bool AdblockPlus::JsValue::IsNull() const
31 { 38 {
32 return value->IsNull(); 39 return value->IsNull();
(...skipping 12 matching lines...) Expand all
45 bool AdblockPlus::JsValue::IsBool() const 52 bool AdblockPlus::JsValue::IsBool() const
46 { 53 {
47 return value->IsBoolean() || value->IsBooleanObject(); 54 return value->IsBoolean() || value->IsBooleanObject();
48 } 55 }
49 56
50 bool AdblockPlus::JsValue::IsObject() const 57 bool AdblockPlus::JsValue::IsObject() const
51 { 58 {
52 return value->IsObject(); 59 return value->IsObject();
53 } 60 }
54 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
55 std::string AdblockPlus::JsValue::AsString() const 72 std::string AdblockPlus::JsValue::AsString() const
56 { 73 {
57 const v8::Locker locker(isolate); 74 const JsEngine::Context context(jsEngine);
58 const v8::HandleScope handleScope; 75 return fromV8String(value);
59 const v8::Context::Scope contextScope(context);
60
61 v8::String::Utf8Value stringValue(value);
62 if (stringValue.length())
63 return std::string(*stringValue, stringValue.length());
64 else
65 return std::string();
66 } 76 }
67 77
68 int64_t AdblockPlus::JsValue::AsInt() const 78 int64_t AdblockPlus::JsValue::AsInt() const
69 { 79 {
70 const v8::Locker locker(isolate); 80 const JsEngine::Context context(jsEngine);
71 const v8::HandleScope handleScope;
72 const v8::Context::Scope contextScope(context);
73
74 return value->IntegerValue(); 81 return value->IntegerValue();
75 } 82 }
76 83
77 bool AdblockPlus::JsValue::AsBool() const 84 bool AdblockPlus::JsValue::AsBool() const
78 { 85 {
79 const v8::Locker locker(isolate); 86 const JsEngine::Context context(jsEngine);
80 const v8::HandleScope handleScope; 87 return value->BooleanValue();
81 const v8::Context::Scope contextScope(context); 88 }
82 89
83 return value->BooleanValue(); 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;
84 } 105 }
85 106
86 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const 107 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const
87 { 108 {
88 if (!IsObject()) 109 if (!IsObject())
89 throw new std::runtime_error("Attempting to get property of a non-object"); 110 throw new std::runtime_error("Attempting to get property of a non-object");
90 111
91 const v8::Locker locker(isolate); 112 const JsEngine::Context context(jsEngine);
92 const v8::HandleScope handleScope;
93 const v8::Context::Scope contextScope(context);
94
95 v8::Local<v8::String> property = toV8String(name); 113 v8::Local<v8::String> property = toV8String(name);
96 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); 114 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value);
97 return JsValuePtr(new JsValue(isolate, context, obj->Get(property))); 115 return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
98 } 116 }
99 117
100 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val) 118 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val)
101 { 119 {
102 if (!IsObject()) 120 if (!IsObject())
103 throw new std::runtime_error("Attempting to set property on a non-object"); 121 throw new std::runtime_error("Attempting to set property on a non-object");
104 122
105 v8::Local<v8::String> property = toV8String(name); 123 v8::Local<v8::String> property = toV8String(name);
106 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); 124 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value);
107 obj->Set(property, val); 125 obj->Set(property, val);
108 } 126 }
109 127
110 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) 128 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val)
111 { 129 {
112 const v8::Locker locker(isolate); 130 const JsEngine::Context context(jsEngine);
113 const v8::HandleScope handleScope;
114 const v8::Context::Scope contextScope(context);
115
116 SetProperty(name, toV8String(val)); 131 SetProperty(name, toV8String(val));
117 } 132 }
118 133
119 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) 134 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
120 { 135 {
121 const v8::Locker locker(isolate); 136 const JsEngine::Context context(jsEngine);
122 const v8::HandleScope handleScope;
123 const v8::Context::Scope contextScope(context);
124
125 SetProperty(name, v8::Integer::New(val)); 137 SetProperty(name, v8::Integer::New(val));
126 } 138 }
127 139
128 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) 140 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
129 { 141 {
130 const v8::Locker locker(isolate); 142 const JsEngine::Context context(jsEngine);
131 const v8::HandleScope handleScope;
132 const v8::Context::Scope contextScope(context);
133
134 SetProperty(name, v8::Boolean::New(val)); 143 SetProperty(name, v8::Boolean::New(val));
135 } 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 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld