Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: Source/bindings/v8/NPV8Object.cpp

Issue 306853002: Replace Context::Scope with ScriptState::Scope (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/bindings/v8/ScriptController.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007, 2008, 2009 Google, Inc. All rights reserved. 3 * Copyright (C) 2007, 2008, 2009 Google, Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 free(v8NpObject); 70 free(v8NpObject);
71 } 71 }
72 72
73 static NPClass V8NPObjectClass = { 73 static NPClass V8NPObjectClass = {
74 NP_CLASS_STRUCT_VERSION, 74 NP_CLASS_STRUCT_VERSION,
75 allocV8NPObject, 75 allocV8NPObject,
76 freeV8NPObject, 76 freeV8NPObject,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
78 }; 78 };
79 79
80 static v8::Local<v8::Context> mainWorldContext(NPP npp, NPObject* npObject) 80 static ScriptState* mainWorldScriptState(v8::Isolate* isolate, NPP npp, NPObject * npObject)
81 { 81 {
82 ASSERT(npObject->_class == &V8NPObjectClass); 82 ASSERT(npObject->_class == &V8NPObjectClass);
83 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject); 83 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
84 DOMWindow* window = object->rootObject; 84 DOMWindow* window = object->rootObject;
85 if (!window || !window->isCurrentlyDisplayedInFrame()) 85 if (!window || !window->isCurrentlyDisplayedInFrame())
86 return v8::Local<v8::Context>(); 86 return 0;
87 return toV8Context(object->rootObject->frame(), DOMWrapperWorld::mainWorld() ); 87 v8::HandleScope handleScope(isolate);
88 v8::Handle<v8::Context> context = toV8Context(object->rootObject->frame(), D OMWrapperWorld::mainWorld());
89 return ScriptState::from(context);
88 } 90 }
89 91
90 static PassOwnPtr<v8::Handle<v8::Value>[]> createValueListFromVariantArgs(const NPVariant* arguments, uint32_t argumentCount, NPObject* owner, v8::Isolate* isol ate) 92 static PassOwnPtr<v8::Handle<v8::Value>[]> createValueListFromVariantArgs(const NPVariant* arguments, uint32_t argumentCount, NPObject* owner, v8::Isolate* isol ate)
91 { 93 {
92 OwnPtr<v8::Handle<v8::Value>[]> argv = adoptArrayPtr(new v8::Handle<v8::Valu e>[argumentCount]); 94 OwnPtr<v8::Handle<v8::Value>[]> argv = adoptArrayPtr(new v8::Handle<v8::Valu e>[argumentCount]);
93 for (uint32_t index = 0; index < argumentCount; index++) { 95 for (uint32_t index = 0; index < argumentCount; index++) {
94 const NPVariant* arg = &arguments[index]; 96 const NPVariant* arg = &arguments[index];
95 argv[index] = convertNPVariantToV8Object(arg, owner, isolate); 97 argv[index] = convertNPVariantToV8Object(arg, owner, isolate);
96 } 98 }
97 return argv.release(); 99 return argv.release();
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 return false; 225 return false;
224 226
225 if (!strcmp(identifier->value.string, "eval")) { 227 if (!strcmp(identifier->value.string, "eval")) {
226 if (argumentCount != 1) 228 if (argumentCount != 1)
227 return false; 229 return false;
228 if (arguments[0].type != NPVariantType_String) 230 if (arguments[0].type != NPVariantType_String)
229 return false; 231 return false;
230 return _NPN_Evaluate(npp, npObject, const_cast<NPString*>(&arguments[0]. value.stringValue), result); 232 return _NPN_Evaluate(npp, npObject, const_cast<NPString*>(&arguments[0]. value.stringValue), result);
231 } 233 }
232 234
233 v8::HandleScope handleScope(isolate);
234 // FIXME: should use the plugin's owner frame as the security context. 235 // FIXME: should use the plugin's owner frame as the security context.
235 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 236 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
236 if (context.IsEmpty()) 237 if (!scriptState)
237 return false; 238 return false;
238 239
239 v8::Context::Scope scope(context); 240 ScriptState::Scope scope(scriptState);
240 ExceptionCatcher exceptionCatcher; 241 ExceptionCatcher exceptionCatcher;
241 242
242 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NpOb ject->v8Object); 243 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NpOb ject->v8Object);
243 v8::Handle<v8::Value> functionObject = v8Object->Get(v8AtomicString(isolate, identifier->value.string)); 244 v8::Handle<v8::Value> functionObject = v8Object->Get(v8AtomicString(isolate, identifier->value.string));
244 if (functionObject.IsEmpty() || functionObject->IsNull()) { 245 if (functionObject.IsEmpty() || functionObject->IsNull()) {
245 NULL_TO_NPVARIANT(*result); 246 NULL_TO_NPVARIANT(*result);
246 return false; 247 return false;
247 } 248 }
248 if (functionObject->IsUndefined()) { 249 if (functionObject->IsUndefined()) {
249 VOID_TO_NPVARIANT(*result); 250 VOID_TO_NPVARIANT(*result);
(...skipping 29 matching lines...) Expand all
279 if (!v8NpObject) { 280 if (!v8NpObject) {
280 if (npObject->_class->invokeDefault) 281 if (npObject->_class->invokeDefault)
281 return npObject->_class->invokeDefault(npObject, arguments, argument Count, result); 282 return npObject->_class->invokeDefault(npObject, arguments, argument Count, result);
282 283
283 VOID_TO_NPVARIANT(*result); 284 VOID_TO_NPVARIANT(*result);
284 return true; 285 return true;
285 } 286 }
286 287
287 VOID_TO_NPVARIANT(*result); 288 VOID_TO_NPVARIANT(*result);
288 289
289 v8::HandleScope handleScope(isolate); 290 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
290 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 291 if (!scriptState)
291 if (context.IsEmpty())
292 return false; 292 return false;
293 293
294 v8::Context::Scope scope(context); 294 ScriptState::Scope scope(scriptState);
295 ExceptionCatcher exceptionCatcher; 295 ExceptionCatcher exceptionCatcher;
296 296
297 // Lookup the function object and call it. 297 // Lookup the function object and call it.
298 v8::Local<v8::Object> functionObject = v8::Local<v8::Object>::New(isolate, v 8NpObject->v8Object); 298 v8::Local<v8::Object> functionObject = v8::Local<v8::Object>::New(isolate, v 8NpObject->v8Object);
299 if (!functionObject->IsFunction()) 299 if (!functionObject->IsFunction())
300 return false; 300 return false;
301 301
302 v8::Local<v8::Value> resultObject; 302 v8::Local<v8::Value> resultObject;
303 v8::Handle<v8::Function> function = v8::Local<v8::Function>::Cast(functionOb ject); 303 v8::Handle<v8::Function> function = v8::Local<v8::Function>::Cast(functionOb ject);
304 if (!function->IsNull()) { 304 if (!function->IsNull()) {
(...skipping 23 matching lines...) Expand all
328 { 328 {
329 VOID_TO_NPVARIANT(*result); 329 VOID_TO_NPVARIANT(*result);
330 if (!npObject) 330 if (!npObject)
331 return false; 331 return false;
332 332
333 V8NPObject* v8NpObject = npObjectToV8NPObject(npObject); 333 V8NPObject* v8NpObject = npObjectToV8NPObject(npObject);
334 if (!v8NpObject) 334 if (!v8NpObject)
335 return false; 335 return false;
336 336
337 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 337 v8::Isolate* isolate = v8::Isolate::GetCurrent();
338 v8::HandleScope handleScope(isolate); 338 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
339 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 339 if (!scriptState)
340 if (context.IsEmpty())
341 return false; 340 return false;
342 341
343 v8::Context::Scope scope(context); 342 ScriptState::Scope scope(scriptState);
344 ExceptionCatcher exceptionCatcher; 343 ExceptionCatcher exceptionCatcher;
345 344
346 // FIXME: Is this branch still needed after switching to using UserGestureIn dicator? 345 // FIXME: Is this branch still needed after switching to using UserGestureIn dicator?
347 String filename; 346 String filename;
348 if (!popupsAllowed) 347 if (!popupsAllowed)
349 filename = "npscript"; 348 filename = "npscript";
350 349
351 LocalFrame* frame = v8NpObject->rootObject->frame(); 350 LocalFrame* frame = v8NpObject->rootObject->frame();
352 ASSERT(frame); 351 ASSERT(frame);
353 352
354 String script = String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Len gth); 353 String script = String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Len gth);
355 354
356 UserGestureIndicator gestureIndicator(popupsAllowed ? DefinitelyProcessingNe wUserGesture : PossiblyProcessingUserGesture); 355 UserGestureIndicator gestureIndicator(popupsAllowed ? DefinitelyProcessingNe wUserGesture : PossiblyProcessingUserGesture);
357 v8::Local<v8::Value> v8result = frame->script().executeScriptAndReturnValue( context, ScriptSourceCode(script, KURL(ParsedURLString, filename))); 356 v8::Local<v8::Value> v8result = frame->script().executeScriptAndReturnValue( scriptState->context(), ScriptSourceCode(script, KURL(ParsedURLString, filename) ));
358 357
359 if (v8result.IsEmpty()) 358 if (v8result.IsEmpty())
360 return false; 359 return false;
361 360
362 if (_NPN_IsAlive(npObject)) 361 if (_NPN_IsAlive(npObject))
363 convertV8ObjectToNPVariant(v8result, npObject, result, isolate); 362 convertV8ObjectToNPVariant(v8result, npObject, result, isolate);
364 return true; 363 return true;
365 } 364 }
366 365
367 bool _NPN_GetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, NP Variant* result) 366 bool _NPN_GetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, NP Variant* result)
368 { 367 {
369 if (!npObject) 368 if (!npObject)
370 return false; 369 return false;
371 370
372 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { 371 if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
373 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 372 v8::Isolate* isolate = v8::Isolate::GetCurrent();
374 v8::HandleScope handleScope(isolate); 373 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
375 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 374 if (!scriptState)
376 if (context.IsEmpty())
377 return false; 375 return false;
378 376
379 v8::Context::Scope scope(context); 377 ScriptState::Scope scope(scriptState);
380 ExceptionCatcher exceptionCatcher; 378 ExceptionCatcher exceptionCatcher;
381 379
382 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object); 380 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object);
383 v8::Local<v8::Value> v8result = obj->Get(npIdentifierToV8Identifier(prop ertyName, isolate)); 381 v8::Local<v8::Value> v8result = obj->Get(npIdentifierToV8Identifier(prop ertyName, isolate));
384 382
385 if (v8result.IsEmpty()) 383 if (v8result.IsEmpty())
386 return false; 384 return false;
387 385
388 convertV8ObjectToNPVariant(v8result, npObject, result, isolate); 386 convertV8ObjectToNPVariant(v8result, npObject, result, isolate);
389 return true; 387 return true;
390 } 388 }
391 389
392 if (npObject->_class->hasProperty && npObject->_class->getProperty) { 390 if (npObject->_class->hasProperty && npObject->_class->getProperty) {
393 if (npObject->_class->hasProperty(npObject, propertyName)) 391 if (npObject->_class->hasProperty(npObject, propertyName))
394 return npObject->_class->getProperty(npObject, propertyName, result) ; 392 return npObject->_class->getProperty(npObject, propertyName, result) ;
395 } 393 }
396 394
397 VOID_TO_NPVARIANT(*result); 395 VOID_TO_NPVARIANT(*result);
398 return false; 396 return false;
399 } 397 }
400 398
401 bool _NPN_SetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, co nst NPVariant* value) 399 bool _NPN_SetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, co nst NPVariant* value)
402 { 400 {
403 if (!npObject) 401 if (!npObject)
404 return false; 402 return false;
405 403
406 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { 404 if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
407 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 405 v8::Isolate* isolate = v8::Isolate::GetCurrent();
408 v8::HandleScope handleScope(isolate); 406 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
409 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 407 if (!scriptState)
410 if (context.IsEmpty())
411 return false; 408 return false;
412 409
413 v8::Context::Scope scope(context); 410 ScriptState::Scope scope(scriptState);
414 ExceptionCatcher exceptionCatcher; 411 ExceptionCatcher exceptionCatcher;
415 412
416 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object); 413 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object);
417 obj->Set(npIdentifierToV8Identifier(propertyName, context->GetIsolate()) , convertNPVariantToV8Object(value, object->rootObject->frame()->script().window ScriptNPObject(), context->GetIsolate())); 414 obj->Set(npIdentifierToV8Identifier(propertyName, isolate), convertNPVar iantToV8Object(value, object->rootObject->frame()->script().windowScriptNPObject (), isolate));
418 return true; 415 return true;
419 } 416 }
420 417
421 if (npObject->_class->setProperty) 418 if (npObject->_class->setProperty)
422 return npObject->_class->setProperty(npObject, propertyName, value); 419 return npObject->_class->setProperty(npObject, propertyName, value);
423 420
424 return false; 421 return false;
425 } 422 }
426 423
427 bool _NPN_RemoveProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName) 424 bool _NPN_RemoveProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName)
428 { 425 {
429 if (!npObject) 426 if (!npObject)
430 return false; 427 return false;
431 428
432 V8NPObject* object = npObjectToV8NPObject(npObject); 429 V8NPObject* object = npObjectToV8NPObject(npObject);
433 if (!object) 430 if (!object)
434 return false; 431 return false;
435 432
436 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 433 v8::Isolate* isolate = v8::Isolate::GetCurrent();
437 v8::HandleScope handleScope(isolate); 434 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
438 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 435 if (!scriptState)
439 if (context.IsEmpty())
440 return false; 436 return false;
441 v8::Context::Scope scope(context); 437 ScriptState::Scope scope(scriptState);
442 ExceptionCatcher exceptionCatcher; 438 ExceptionCatcher exceptionCatcher;
443 439
444 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8O bject); 440 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8O bject);
445 // FIXME: Verify that setting to undefined is right. 441 // FIXME: Verify that setting to undefined is right.
446 obj->Set(npIdentifierToV8Identifier(propertyName, isolate), v8::Undefined(is olate)); 442 obj->Set(npIdentifierToV8Identifier(propertyName, isolate), v8::Undefined(is olate));
447 return true; 443 return true;
448 } 444 }
449 445
450 bool _NPN_HasProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName) 446 bool _NPN_HasProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName)
451 { 447 {
452 if (!npObject) 448 if (!npObject)
453 return false; 449 return false;
454 450
455 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { 451 if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
456 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 452 v8::Isolate* isolate = v8::Isolate::GetCurrent();
457 v8::HandleScope handleScope(isolate); 453 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
458 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 454 if (!scriptState)
459 if (context.IsEmpty())
460 return false; 455 return false;
461 v8::Context::Scope scope(context); 456 ScriptState::Scope scope(scriptState);
462 ExceptionCatcher exceptionCatcher; 457 ExceptionCatcher exceptionCatcher;
463 458
464 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object); 459 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object);
465 return obj->Has(npIdentifierToV8Identifier(propertyName, isolate)); 460 return obj->Has(npIdentifierToV8Identifier(propertyName, isolate));
466 } 461 }
467 462
468 if (npObject->_class->hasProperty) 463 if (npObject->_class->hasProperty)
469 return npObject->_class->hasProperty(npObject, propertyName); 464 return npObject->_class->hasProperty(npObject, propertyName);
470 return false; 465 return false;
471 } 466 }
472 467
473 bool _NPN_HasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName) 468 bool _NPN_HasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName)
474 { 469 {
475 if (!npObject) 470 if (!npObject)
476 return false; 471 return false;
477 472
478 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { 473 if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
479 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 474 v8::Isolate* isolate = v8::Isolate::GetCurrent();
480 v8::HandleScope handleScope(isolate); 475 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
481 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 476 if (!scriptState)
482 if (context.IsEmpty())
483 return false; 477 return false;
484 v8::Context::Scope scope(context); 478 ScriptState::Scope scope(scriptState);
485 ExceptionCatcher exceptionCatcher; 479 ExceptionCatcher exceptionCatcher;
486 480
487 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object); 481 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object);
488 v8::Handle<v8::Value> prop = obj->Get(npIdentifierToV8Identifier(methodN ame, isolate)); 482 v8::Handle<v8::Value> prop = obj->Get(npIdentifierToV8Identifier(methodN ame, isolate));
489 return prop->IsFunction(); 483 return prop->IsFunction();
490 } 484 }
491 485
492 if (npObject->_class->hasMethod) 486 if (npObject->_class->hasMethod)
493 return npObject->_class->hasMethod(npObject, methodName); 487 return npObject->_class->hasMethod(npObject, methodName);
494 return false; 488 return false;
495 } 489 }
496 490
497 void _NPN_SetException(NPObject* npObject, const NPUTF8 *message) 491 void _NPN_SetException(NPObject* npObject, const NPUTF8 *message)
498 { 492 {
499 if (!npObject || !npObjectToV8NPObject(npObject)) { 493 if (!npObject || !npObjectToV8NPObject(npObject)) {
500 // We won't be able to find a proper scope for this exception, so just t hrow it. 494 // We won't be able to find a proper scope for this exception, so just t hrow it.
501 // This is consistent with JSC, which throws a global exception all the time. 495 // This is consistent with JSC, which throws a global exception all the time.
502 throwError(v8GeneralError, message, v8::Isolate::GetCurrent()); 496 throwError(v8GeneralError, message, v8::Isolate::GetCurrent());
503 return; 497 return;
504 } 498 }
505 499
506 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 500 v8::Isolate* isolate = v8::Isolate::GetCurrent();
507 v8::HandleScope handleScope(isolate); 501 ScriptState* scriptState = mainWorldScriptState(isolate, 0, npObject);
508 v8::Handle<v8::Context> context = mainWorldContext(0, npObject); 502 if (!scriptState)
509 if (context.IsEmpty())
510 return; 503 return;
511 504
512 v8::Context::Scope scope(context); 505 ScriptState::Scope scope(scriptState);
513 ExceptionCatcher exceptionCatcher; 506 ExceptionCatcher exceptionCatcher;
514 507
515 throwError(v8GeneralError, message, context->GetIsolate()); 508 throwError(v8GeneralError, message, isolate);
516 } 509 }
517 510
518 bool _NPN_Enumerate(NPP npp, NPObject* npObject, NPIdentifier** identifier, uint 32_t* count) 511 bool _NPN_Enumerate(NPP npp, NPObject* npObject, NPIdentifier** identifier, uint 32_t* count)
519 { 512 {
520 if (!npObject) 513 if (!npObject)
521 return false; 514 return false;
522 515
523 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { 516 if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
524 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 517 v8::Isolate* isolate = v8::Isolate::GetCurrent();
525 v8::HandleScope handleScope(isolate); 518 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
526 v8::Local<v8::Context> context = mainWorldContext(npp, npObject); 519 if (!scriptState)
527 if (context.IsEmpty())
528 return false; 520 return false;
529 v8::Context::Scope scope(context); 521 ScriptState::Scope scope(scriptState);
530 ExceptionCatcher exceptionCatcher; 522 ExceptionCatcher exceptionCatcher;
531 523
532 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object); 524 v8::Handle<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object- >v8Object);
533 525
534 // FIXME: http://b/issue?id=1210340: Use a v8::Object::Keys() method whe n it exists, instead of evaluating javascript. 526 // FIXME: http://b/issue?id=1210340: Use a v8::Object::Keys() method whe n it exists, instead of evaluating javascript.
535 527
536 // FIXME: Figure out how to cache this helper function. Run a helper fu nction that collects the properties 528 // FIXME: Figure out how to cache this helper function. Run a helper fu nction that collects the properties
537 // on the object into an array. 529 // on the object into an array.
538 const char enumeratorCode[] = 530 const char enumeratorCode[] =
539 "(function (obj) {" 531 "(function (obj) {"
540 " var props = [];" 532 " var props = [];"
541 " for (var prop in obj) {" 533 " for (var prop in obj) {"
542 " props[props.length] = prop;" 534 " props[props.length] = prop;"
543 " }" 535 " }"
544 " return props;" 536 " return props;"
545 "});"; 537 "});";
546 v8::Handle<v8::String> source = v8AtomicString(isolate, enumeratorCode); 538 v8::Handle<v8::String> source = v8AtomicString(isolate, enumeratorCode);
547 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScri pt(source, context->GetIsolate()); 539 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScri pt(source, isolate);
548 ASSERT(!result.IsEmpty()); 540 ASSERT(!result.IsEmpty());
549 ASSERT(result->IsFunction()); 541 ASSERT(result->IsFunction());
550 v8::Handle<v8::Function> enumerator = v8::Handle<v8::Function>::Cast(res ult); 542 v8::Handle<v8::Function> enumerator = v8::Handle<v8::Function>::Cast(res ult);
551 v8::Handle<v8::Value> argv[] = { obj }; 543 v8::Handle<v8::Value> argv[] = { obj };
552 v8::Local<v8::Value> propsObj = V8ScriptRunner::callInternalFunction(enu merator, v8::Handle<v8::Object>::Cast(result), WTF_ARRAY_LENGTH(argv), argv, con text->GetIsolate()); 544 v8::Local<v8::Value> propsObj = V8ScriptRunner::callInternalFunction(enu merator, v8::Handle<v8::Object>::Cast(result), WTF_ARRAY_LENGTH(argv), argv, iso late);
553 if (propsObj.IsEmpty()) 545 if (propsObj.IsEmpty())
554 return false; 546 return false;
555 547
556 // Convert the results into an array of NPIdentifiers. 548 // Convert the results into an array of NPIdentifiers.
557 v8::Handle<v8::Array> props = v8::Handle<v8::Array>::Cast(propsObj); 549 v8::Handle<v8::Array> props = v8::Handle<v8::Array>::Cast(propsObj);
558 *count = props->Length(); 550 *count = props->Length();
559 *identifier = static_cast<NPIdentifier*>(calloc(*count, sizeof(NPIdentif ier))); 551 *identifier = static_cast<NPIdentifier*>(calloc(*count, sizeof(NPIdentif ier)));
560 for (uint32_t i = 0; i < *count; ++i) { 552 for (uint32_t i = 0; i < *count; ++i) {
561 v8::Local<v8::Value> name = props->Get(v8::Integer::New(context->Get Isolate(), i)); 553 v8::Local<v8::Value> name = props->Get(v8::Integer::New(isolate, i)) ;
562 (*identifier)[i] = getStringIdentifier(v8::Local<v8::String>::Cast(n ame)); 554 (*identifier)[i] = getStringIdentifier(v8::Local<v8::String>::Cast(n ame));
563 } 555 }
564 return true; 556 return true;
565 } 557 }
566 558
567 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class-> enumerate) 559 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class-> enumerate)
568 return npObject->_class->enumerate(npObject, identifier, count); 560 return npObject->_class->enumerate(npObject, identifier, count);
569 561
570 return false; 562 return false;
571 } 563 }
572 564
573 bool _NPN_Construct(NPP npp, NPObject* npObject, const NPVariant* arguments, uin t32_t argumentCount, NPVariant* result) 565 bool _NPN_Construct(NPP npp, NPObject* npObject, const NPVariant* arguments, uin t32_t argumentCount, NPVariant* result)
574 { 566 {
575 if (!npObject) 567 if (!npObject)
576 return false; 568 return false;
577 569
578 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 570 v8::Isolate* isolate = v8::Isolate::GetCurrent();
579 571
580 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { 572 if (V8NPObject* object = npObjectToV8NPObject(npObject)) {
581 v8::HandleScope handleScope(isolate); 573 ScriptState* scriptState = mainWorldScriptState(isolate, npp, npObject);
582 v8::Handle<v8::Context> context = mainWorldContext(npp, npObject); 574 if (!scriptState)
583 if (context.IsEmpty())
584 return false; 575 return false;
585 v8::Context::Scope scope(context); 576 ScriptState::Scope scope(scriptState);
586 ExceptionCatcher exceptionCatcher; 577 ExceptionCatcher exceptionCatcher;
587 578
588 // Lookup the constructor function. 579 // Lookup the constructor function.
589 v8::Handle<v8::Object> ctorObj = v8::Local<v8::Object>::New(isolate, obj ect->v8Object); 580 v8::Handle<v8::Object> ctorObj = v8::Local<v8::Object>::New(isolate, obj ect->v8Object);
590 if (!ctorObj->IsFunction()) 581 if (!ctorObj->IsFunction())
591 return false; 582 return false;
592 583
593 // Call the constructor. 584 // Call the constructor.
594 v8::Local<v8::Value> resultObject; 585 v8::Local<v8::Value> resultObject;
595 v8::Handle<v8::Function> ctor = v8::Handle<v8::Function>::Cast(ctorObj); 586 v8::Handle<v8::Function> ctor = v8::Handle<v8::Function>::Cast(ctorObj);
596 if (!ctor->IsNull()) { 587 if (!ctor->IsNull()) {
597 LocalFrame* frame = object->rootObject->frame(); 588 LocalFrame* frame = object->rootObject->frame();
598 ASSERT(frame); 589 ASSERT(frame);
599 OwnPtr<v8::Handle<v8::Value>[]> argv = createValueListFromVariantArg s(arguments, argumentCount, npObject, isolate); 590 OwnPtr<v8::Handle<v8::Value>[]> argv = createValueListFromVariantArg s(arguments, argumentCount, npObject, isolate);
600 resultObject = V8ObjectConstructor::newInstanceInDocument(isolate, c tor, argumentCount, argv.get(), frame ? frame->document() : 0); 591 resultObject = V8ObjectConstructor::newInstanceInDocument(isolate, c tor, argumentCount, argv.get(), frame ? frame->document() : 0);
601 } 592 }
602 593
603 if (resultObject.IsEmpty()) 594 if (resultObject.IsEmpty())
604 return false; 595 return false;
605 596
606 convertV8ObjectToNPVariant(resultObject, npObject, result, isolate); 597 convertV8ObjectToNPVariant(resultObject, npObject, result, isolate);
607 return true; 598 return true;
608 } 599 }
609 600
610 if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npObject->_class) && npObject->_class-> construct) 601 if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npObject->_class) && npObject->_class-> construct)
611 return npObject->_class->construct(npObject, arguments, argumentCount, r esult); 602 return npObject->_class->construct(npObject, arguments, argumentCount, r esult);
612 603
613 return false; 604 return false;
614 } 605 }
OLDNEW
« no previous file with comments | « no previous file | Source/bindings/v8/ScriptController.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698