OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // TODO(dcarney): remove this | |
29 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | |
30 #define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT | |
31 #define V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW | |
32 | |
33 #include <v8.h> | 28 #include <v8.h> |
34 | 29 |
35 #include <string> | 30 #include <string> |
36 #include <map> | 31 #include <map> |
37 | 32 |
38 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 33 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
39 #error Using compressed startup data is not supported for this sample | 34 #error Using compressed startup data is not supported for this sample |
40 #endif | 35 #endif |
41 | 36 |
42 using namespace std; | 37 using namespace std; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 if (!InstallMaps(opts, output)) | 174 if (!InstallMaps(opts, output)) |
180 return false; | 175 return false; |
181 | 176 |
182 // Compile and run the script | 177 // Compile and run the script |
183 if (!ExecuteScript(script_)) | 178 if (!ExecuteScript(script_)) |
184 return false; | 179 return false; |
185 | 180 |
186 // The script compiled and ran correctly. Now we fetch out the | 181 // The script compiled and ran correctly. Now we fetch out the |
187 // Process function from the global object. | 182 // Process function from the global object. |
188 Handle<String> process_name = String::New("Process"); | 183 Handle<String> process_name = String::New("Process"); |
189 Handle<Value> process_val = context_->Global()->Get(process_name); | 184 Handle<Value> process_val = context->Global()->Get(process_name); |
190 | 185 |
191 // If there is no Process function, or if it is not a function, | 186 // If there is no Process function, or if it is not a function, |
192 // bail out | 187 // bail out |
193 if (!process_val->IsFunction()) return false; | 188 if (!process_val->IsFunction()) return false; |
194 | 189 |
195 // It is a function; cast it to a Function | 190 // It is a function; cast it to a Function |
196 Handle<Function> process_fun = Handle<Function>::Cast(process_val); | 191 Handle<Function> process_fun = Handle<Function>::Cast(process_val); |
197 | 192 |
198 // Store the function in a Persistent handle, since we also want | 193 // Store the function in a Persistent handle, since we also want |
199 // that to remain after this call returns | 194 // that to remain after this call returns |
200 process_ = Persistent<Function>::New(GetIsolate(), process_fun); | 195 process_.Reset(GetIsolate(), process_fun); |
201 | 196 |
202 // All done; all went well | 197 // All done; all went well |
203 return true; | 198 return true; |
204 } | 199 } |
205 | 200 |
206 | 201 |
207 bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) { | 202 bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) { |
208 HandleScope handle_scope(GetIsolate()); | 203 HandleScope handle_scope(GetIsolate()); |
209 | 204 |
210 // We're just about to compile the script; set up an error handler to | 205 // We're just about to compile the script; set up an error handler to |
(...skipping 22 matching lines...) Expand all Loading... |
233 } | 228 } |
234 | 229 |
235 | 230 |
236 bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts, | 231 bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts, |
237 map<string, string>* output) { | 232 map<string, string>* output) { |
238 HandleScope handle_scope(GetIsolate()); | 233 HandleScope handle_scope(GetIsolate()); |
239 | 234 |
240 // Wrap the map object in a JavaScript wrapper | 235 // Wrap the map object in a JavaScript wrapper |
241 Handle<Object> opts_obj = WrapMap(opts); | 236 Handle<Object> opts_obj = WrapMap(opts); |
242 | 237 |
| 238 v8::Local<v8::Context> context = |
| 239 v8::Local<v8::Context>::New(GetIsolate(), context_); |
| 240 |
243 // Set the options object as a property on the global object. | 241 // Set the options object as a property on the global object. |
244 context_->Global()->Set(String::New("options"), opts_obj); | 242 context->Global()->Set(String::New("options"), opts_obj); |
245 | 243 |
246 Handle<Object> output_obj = WrapMap(output); | 244 Handle<Object> output_obj = WrapMap(output); |
247 context_->Global()->Set(String::New("output"), output_obj); | 245 context->Global()->Set(String::New("output"), output_obj); |
248 | 246 |
249 return true; | 247 return true; |
250 } | 248 } |
251 | 249 |
252 | 250 |
253 bool JsHttpRequestProcessor::Process(HttpRequest* request) { | 251 bool JsHttpRequestProcessor::Process(HttpRequest* request) { |
254 // Create a handle scope to keep the temporary object references. | 252 // Create a handle scope to keep the temporary object references. |
255 HandleScope handle_scope(GetIsolate()); | 253 HandleScope handle_scope(GetIsolate()); |
256 | 254 |
257 v8::Local<v8::Context> context = | 255 v8::Local<v8::Context> context = |
258 v8::Local<v8::Context>::New(GetIsolate(), context_); | 256 v8::Local<v8::Context>::New(GetIsolate(), context_); |
259 | 257 |
260 // Enter this processor's context so all the remaining operations | 258 // Enter this processor's context so all the remaining operations |
261 // take place there | 259 // take place there |
262 Context::Scope context_scope(context); | 260 Context::Scope context_scope(context); |
263 | 261 |
264 // Wrap the C++ request object in a JavaScript wrapper | 262 // Wrap the C++ request object in a JavaScript wrapper |
265 Handle<Object> request_obj = WrapRequest(request); | 263 Handle<Object> request_obj = WrapRequest(request); |
266 | 264 |
267 // Set up an exception handler before calling the Process function | 265 // Set up an exception handler before calling the Process function |
268 TryCatch try_catch; | 266 TryCatch try_catch; |
269 | 267 |
270 // Invoke the process function, giving the global object as 'this' | 268 // Invoke the process function, giving the global object as 'this' |
271 // and one argument, the request. | 269 // and one argument, the request. |
272 const int argc = 1; | 270 const int argc = 1; |
273 Handle<Value> argv[argc] = { request_obj }; | 271 Handle<Value> argv[argc] = { request_obj }; |
274 Handle<Value> result = process_->Call(context_->Global(), argc, argv); | 272 v8::Local<v8::Function> process = |
| 273 v8::Local<v8::Function>::New(GetIsolate(), process_); |
| 274 Handle<Value> result = process->Call(context->Global(), argc, argv); |
275 if (result.IsEmpty()) { | 275 if (result.IsEmpty()) { |
276 String::Utf8Value error(try_catch.Exception()); | 276 String::Utf8Value error(try_catch.Exception()); |
277 Log(*error); | 277 Log(*error); |
278 return false; | 278 return false; |
279 } else { | 279 } else { |
280 return true; | 280 return true; |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 | 284 |
(...skipping 18 matching lines...) Expand all Loading... |
303 // Utility function that wraps a C++ http request object in a | 303 // Utility function that wraps a C++ http request object in a |
304 // JavaScript object. | 304 // JavaScript object. |
305 Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) { | 305 Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) { |
306 // Handle scope for temporary handles. | 306 // Handle scope for temporary handles. |
307 HandleScope handle_scope(GetIsolate()); | 307 HandleScope handle_scope(GetIsolate()); |
308 | 308 |
309 // Fetch the template for creating JavaScript map wrappers. | 309 // Fetch the template for creating JavaScript map wrappers. |
310 // It only has to be created once, which we do on demand. | 310 // It only has to be created once, which we do on demand. |
311 if (map_template_.IsEmpty()) { | 311 if (map_template_.IsEmpty()) { |
312 Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate()); | 312 Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate()); |
313 map_template_ = Persistent<ObjectTemplate>::New(GetIsolate(), raw_template); | 313 map_template_.Reset(GetIsolate(), raw_template); |
314 } | 314 } |
315 Handle<ObjectTemplate> templ = | 315 Handle<ObjectTemplate> templ = |
316 Local<ObjectTemplate>::New(GetIsolate(), map_template_); | 316 Local<ObjectTemplate>::New(GetIsolate(), map_template_); |
317 | 317 |
318 // Create an empty map wrapper. | 318 // Create an empty map wrapper. |
319 Handle<Object> result = templ->NewInstance(); | 319 Handle<Object> result = templ->NewInstance(); |
320 | 320 |
321 // Wrap the raw C++ pointer in an External so it can be referenced | 321 // Wrap the raw C++ pointer in an External so it can be referenced |
322 // from within JavaScript. | 322 // from within JavaScript. |
323 Handle<External> map_ptr = External::New(obj); | 323 Handle<External> map_ptr = External::New(obj); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 * JavaScript object. | 410 * JavaScript object. |
411 */ | 411 */ |
412 Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { | 412 Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { |
413 // Handle scope for temporary handles. | 413 // Handle scope for temporary handles. |
414 HandleScope handle_scope(GetIsolate()); | 414 HandleScope handle_scope(GetIsolate()); |
415 | 415 |
416 // Fetch the template for creating JavaScript http request wrappers. | 416 // Fetch the template for creating JavaScript http request wrappers. |
417 // It only has to be created once, which we do on demand. | 417 // It only has to be created once, which we do on demand. |
418 if (request_template_.IsEmpty()) { | 418 if (request_template_.IsEmpty()) { |
419 Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate()); | 419 Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate()); |
420 request_template_ = | 420 request_template_.Reset(GetIsolate(), raw_template); |
421 Persistent<ObjectTemplate>::New(GetIsolate(), raw_template); | |
422 } | 421 } |
423 Handle<ObjectTemplate> templ = | 422 Handle<ObjectTemplate> templ = |
424 Local<ObjectTemplate>::New(GetIsolate(), request_template_); | 423 Local<ObjectTemplate>::New(GetIsolate(), request_template_); |
425 | 424 |
426 // Create an empty http request wrapper. | 425 // Create an empty http request wrapper. |
427 Handle<Object> result = templ->NewInstance(); | 426 Handle<Object> result = templ->NewInstance(); |
428 | 427 |
429 // Wrap the raw C++ pointer in an External so it can be referenced | 428 // Wrap the raw C++ pointer in an External so it can be referenced |
430 // from within JavaScript. | 429 // from within JavaScript. |
431 Handle<External> request_ptr = External::New(request); | 430 Handle<External> request_ptr = External::New(request); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 JsHttpRequestProcessor processor(isolate, source); | 632 JsHttpRequestProcessor processor(isolate, source); |
634 map<string, string> output; | 633 map<string, string> output; |
635 if (!processor.Initialize(&options, &output)) { | 634 if (!processor.Initialize(&options, &output)) { |
636 fprintf(stderr, "Error initializing processor.\n"); | 635 fprintf(stderr, "Error initializing processor.\n"); |
637 return 1; | 636 return 1; |
638 } | 637 } |
639 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) | 638 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) |
640 return 1; | 639 return 1; |
641 PrintMap(&output); | 640 PrintMap(&output); |
642 } | 641 } |
OLD | NEW |