OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * 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 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 } | 330 } |
331 }; | 331 }; |
332 | 332 |
333 PassRefPtr<JsObject> JsObject::create(v8::Local<v8::Object> v8Handle) | 333 PassRefPtr<JsObject> JsObject::create(v8::Local<v8::Object> v8Handle) |
334 { | 334 { |
335 return adoptRef(new JsObject(v8Handle)); | 335 return adoptRef(new JsObject(v8Handle)); |
336 } | 336 } |
337 | 337 |
338 v8::Local<v8::Value> JsInterop::fromDart(DartDOMData* domData, Dart_Handle handl
e, Dart_Handle& exception) | 338 v8::Local<v8::Value> JsInterop::fromDart(DartDOMData* domData, Dart_Handle handl
e, Dart_Handle& exception) |
339 { | 339 { |
| 340 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); |
340 v8::Handle<v8::Value> value = V8Converter::toV8IfPrimitive(domData, handle,
exception); | 341 v8::Handle<v8::Value> value = V8Converter::toV8IfPrimitive(domData, handle,
exception); |
341 if (!value.IsEmpty() || exception) | 342 if (!value.IsEmpty() || exception) |
342 return value; | 343 return value; |
343 | 344 |
344 Dart_Handle jso = invokeTopLevelJsInteropMethod(domData, "unwrap_jso", 1, &h
andle); | 345 Dart_Handle jso = invokeTopLevelJsInteropMethod(domData, "unwrap_jso", 1, &h
andle); |
345 ASSERT(!Dart_IsError(jso)); | 346 ASSERT(!Dart_IsError(jso)); |
346 if (DartDOMWrapper::subtypeOf(jso, JsObject::dartClassId)) { | 347 if (DartDOMWrapper::subtypeOf(jso, JsObject::dartClassId)) { |
347 JsObject* object = DartDOMWrapper::unwrapDartWrapper<JsObject>(domData,
jso, exception); | 348 JsObject* object = DartDOMWrapper::unwrapDartWrapper<JsObject>(domData,
jso, exception); |
348 if (exception) | 349 if (exception) |
349 return v8::Local<v8::Value>(); | 350 return v8::Local<v8::Value>(); |
350 return object->localV8Object(); | 351 return object->localV8Object(); |
351 } | 352 } |
352 // TODO(terry): START of uncommented block by Jacob, I've re-enabled for cla
mped arrays... | 353 // TODO(terry): START of uncommented block by Jacob, I've re-enabled for cla
mped arrays... |
353 value = V8Converter::toV8IfBrowserNative(domData, handle, exception); | 354 value = V8Converter::toV8IfBrowserNative(domData, handle, exception); |
354 if (!value.IsEmpty() || exception) | 355 if (!value.IsEmpty() || exception) |
355 return value; | 356 return value; |
356 | 357 |
357 if (DartUtilities::isFunction(domData, handle)) { | 358 if (DartUtilities::isFunction(domData, handle)) { |
358 v8::Local<v8::Object> functionProxy = dartFunctionTemplate()->InstanceTe
mplate()->NewInstance(); | 359 v8::Local<v8::Object> functionProxy = dartFunctionTemplate()->InstanceTe
mplate()->NewInstance(); |
359 DartHandleProxy::writePointerToProxy(functionProxy, handle); | 360 DartHandleProxy::writePointerToProxy(functionProxy, handle); |
360 // The raw functionProxy doesn't behave enough like a true JS function | 361 // The raw functionProxy doesn't behave enough like a true JS function |
361 // so we wrap it in a true JS function. | 362 // so we wrap it in a true JS function. |
362 return domData->jsInteropData()->wrapDartFunction()->Call(functionProxy,
0, 0); | 363 v8::Local<v8::Function> object = domData->jsInteropData()->wrapDartFunct
ion()->Call(functionProxy, 0, 0).As<v8::Function>(); |
| 364 |
| 365 v8::Local<v8::String> existingDartWrapperKey = domData->jsInteropData()-
>existingDartWrapperHiddenField(v8Isolate); |
| 366 v8::Local<v8::Object> containerForDartHandle = dartObjectTemplate()->Ins
tanceTemplate()->NewInstance(); |
| 367 DartHandleProxy::writePointerToProxy(containerForDartHandle, handle); |
| 368 object->SetHiddenValue(existingDartWrapperKey, containerForDartHandle); |
| 369 return object; |
363 } | 370 } |
364 | 371 |
365 v8::Local<v8::Object> proxy; | 372 v8::Local<v8::Object> proxy; |
366 ASSERT(Dart_IsInstance(handle)); | 373 ASSERT(Dart_IsInstance(handle)); |
367 // Simulate the behavior of the Dart dev compiler where new List() is | 374 // Simulate the behavior of the Dart dev compiler where new List() is |
368 // equivalent to a JavaScript array. We accomplish this by creating a | 375 // equivalent to a JavaScript array. We accomplish this by creating a |
369 // JavaScript object that fakes that it is a JavaScript array but is | 376 // JavaScript object that fakes that it is a JavaScript array but is |
370 // actually backed by a Dart list. This is not a breaking change as | 377 // actually backed by a Dart list. This is not a breaking change as |
371 // existing Dart-JS interop passed arrays as opaque Dart handles. | 378 // existing Dart-JS interop passed arrays as opaque Dart handles. |
372 // The jsify method can still be called if you wish to create a copy | 379 // The jsify method can still be called if you wish to create a copy |
373 // of a json like Dart data structure. | 380 // of a json like Dart data structure. |
374 if (Dart_IsList(handle)) { | 381 if (Dart_IsList(handle)) { |
375 proxy = dartListTemplate()->InstanceTemplate()->NewInstance(); | 382 proxy = dartListTemplate()->InstanceTemplate()->NewInstance(); |
376 } else { | 383 } else { |
377 proxy = dartObjectTemplate()->InstanceTemplate()->NewInstance(); | 384 proxy = dartObjectTemplate()->InstanceTemplate()->NewInstance(); |
378 } | 385 } |
379 DartHandleProxy::writePointerToProxy(proxy, handle); | 386 DartHandleProxy::writePointerToProxy(proxy, handle); |
380 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); | |
381 proxy->SetHiddenValue(v8::String::NewFromUtf8(v8Isolate, "dartProxy"), v8::B
oolean::New(v8Isolate, true)); | 387 proxy->SetHiddenValue(v8::String::NewFromUtf8(v8Isolate, "dartProxy"), v8::B
oolean::New(v8Isolate, true)); |
382 | 388 |
383 return proxy; | 389 return proxy; |
384 } | 390 } |
385 | 391 |
386 // Given a Blink Element instance, make it a V8Object that can then be returned | 392 // Given a Blink Element instance, make it a V8Object that can then be returned |
387 // as a Dart JsObject that holds the Javascript object (V8Object). | 393 // as a Dart JsObject that holds the Javascript object (V8Object). |
388 void JsInterop::returnBlinkElement(Element* value, Dart_NativeArguments args, Da
rt_Handle& exception) | 394 void JsInterop::returnBlinkElement(Element* value, Dart_NativeArguments args, Da
rt_Handle& exception) |
389 { | 395 { |
390 JsInteropScopes scopes(args); | 396 JsInteropScopes scopes(args); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 return JsObject::toDart(object, sometimesUseHtml); | 465 return JsObject::toDart(object, sometimesUseHtml); |
460 } | 466 } |
461 | 467 |
462 Dart_Handle JsObject::toDart(v8::Local<v8::Object> object, bool sometimesUseHtml
) | 468 Dart_Handle JsObject::toDart(v8::Local<v8::Object> object, bool sometimesUseHtml
) |
463 { | 469 { |
464 Dart_Handle wrapper; | 470 Dart_Handle wrapper; |
465 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); | 471 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); |
466 DartDOMData* domData = DartDOMData::current(); | 472 DartDOMData* domData = DartDOMData::current(); |
467 DartJsInteropData* interopData = domData->jsInteropData(); | 473 DartJsInteropData* interopData = domData->jsInteropData(); |
468 v8::Local<v8::String> existingDartWrapperKey = interopData->existingDartWrap
perHiddenField(v8Isolate); | 474 v8::Local<v8::String> existingDartWrapperKey = interopData->existingDartWrap
perHiddenField(v8Isolate); |
469 | |
470 // TODO(alanknight): This will fail for multiple isolates referencing the sa
me JS object. | |
471 // We probably need to use a different property name for different isolates. | |
472 v8::Local<v8::Value> hiddenValue = object->GetHiddenValue(existingDartWrappe
rKey); | 475 v8::Local<v8::Value> hiddenValue = object->GetHiddenValue(existingDartWrappe
rKey); |
473 | 476 |
474 if (*hiddenValue && hiddenValue->IsObject()) { | 477 if (*hiddenValue && hiddenValue->IsObject()) { |
475 DartPersistentValue* scriptValue = DartHandleProxy::readPointerFromProxy
(hiddenValue.As<v8::Object>()); | 478 DartPersistentValue* scriptValue = DartHandleProxy::readPointerFromProxy
(hiddenValue.As<v8::Object>()); |
476 ASSERT(scriptValue->isIsolateAlive()); | 479 ASSERT(scriptValue->isIsolateAlive()); |
477 // If the isolate does not match we fall back to using the existing JS | 480 // If the isolate does not match we fall back to using the existing JS |
478 // wrapper for the Dart object so that simple cases that would work in | 481 // wrapper for the Dart object so that simple cases that would work in |
479 // Dart2Js work. We could alternately throw an exception here. | 482 // Dart2Js work. We could alternately throw an exception here. |
480 if (scriptValue->isolate() == Dart_CurrentIsolate()) { | 483 if (scriptValue->isolate() == Dart_CurrentIsolate()) { |
481 Dart_Handle wrapper = scriptValue->value(); | 484 Dart_Handle wrapper = scriptValue->value(); |
(...skipping 1338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1820 { | 1823 { |
1821 JsInteropInternal::callMethodCallback(args); | 1824 JsInteropInternal::callMethodCallback(args); |
1822 } | 1825 } |
1823 | 1826 |
1824 void JsInterop::jsInteropContextCallback(Dart_NativeArguments args) | 1827 void JsInterop::jsInteropContextCallback(Dart_NativeArguments args) |
1825 { | 1828 { |
1826 return JsInteropInternal::contextCallback(args); | 1829 return JsInteropInternal::contextCallback(args); |
1827 } | 1830 } |
1828 | 1831 |
1829 } | 1832 } |
OLD | NEW |