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

Side by Side Diff: sky/engine/bindings-dart/core/dart/V8Converter.cpp

Issue 918273002: Remove bindings-dart (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "bindings/core/dart/V8Converter.h"
33
34 #include "bindings/core/dart/DartBlob.h"
35 #include "bindings/core/dart/DartDOMStringList.h"
36 #include "bindings/core/dart/DartEvent.h"
37 #include "bindings/core/dart/DartImageData.h"
38 #include "bindings/core/dart/DartNode.h"
39 #include "bindings/core/dart/DartWindow.h"
40 #include "bindings/core/v8/V8Blob.h"
41 #include "bindings/core/v8/V8DOMStringList.h"
42 #include "bindings/core/v8/V8Event.h"
43 #include "bindings/core/v8/V8ImageData.h"
44 #include "bindings/core/v8/V8Node.h"
45 #include "bindings/core/v8/V8Window.h"
46 #include "bindings/core/dart/DartUtilities.h"
47 #include "bindings/modules/dart/DartIDBCursor.h"
48 #include "bindings/modules/dart/DartIDBCursorWithValue.h"
49 #include "bindings/modules/dart/DartIDBDatabase.h"
50 #include "bindings/modules/dart/DartIDBFactory.h"
51 #include "bindings/modules/dart/DartIDBKeyRange.h"
52 #include "bindings/modules/v8/V8IDBCursor.h"
53 #include "bindings/modules/v8/V8IDBCursorWithValue.h"
54 #include "bindings/modules/v8/V8IDBDatabase.h"
55 #include "bindings/modules/v8/V8IDBFactory.h"
56 #include "bindings/modules/v8/V8IDBKeyRange.h"
57 #include "bindings/core/v8/custom/V8ArrayBufferViewCustom.h"
58 #include "bindings/core/v8/custom/V8DataViewCustom.h"
59 #include "bindings/core/v8/custom/V8Float32ArrayCustom.h"
60 #include "bindings/core/v8/custom/V8Float64ArrayCustom.h"
61 #include "bindings/core/v8/custom/V8Int16ArrayCustom.h"
62 #include "bindings/core/v8/custom/V8Int32ArrayCustom.h"
63 #include "bindings/core/v8/custom/V8Int8ArrayCustom.h"
64 #include "bindings/core/v8/custom/V8TypedArrayCustom.h"
65 #include "bindings/core/v8/custom/V8Uint16ArrayCustom.h"
66 #include "bindings/core/v8/custom/V8Uint32ArrayCustom.h"
67 #include "bindings/core/v8/custom/V8Uint8ArrayCustom.h"
68 #include "bindings/core/v8/custom/V8Uint8ClampedArrayCustom.h"
69
70 #include "wtf/Vector.h"
71
72 #include <utility>
73
74 namespace blink {
75
76 class DartToV8Map {
77 public:
78 DartToV8Map()
79 : m_list()
80 {
81 }
82
83 v8::Handle<v8::Value> get(Dart_Handle key)
84 {
85 for (size_t i = 0; i < m_list.size(); ++i) {
86 if (Dart_IdentityEquals(key, m_list[i].first))
87 return m_list[i].second;
88 }
89 return v8::Handle<v8::Value>();
90 }
91
92 v8::Handle<v8::Value> put(Dart_Handle key, v8::Handle<v8::Value> value)
93 {
94 ASSERT(!value.IsEmpty());
95 ASSERT(get(key).IsEmpty());
96 m_list.append(std::make_pair(key, value));
97 return value;
98 }
99
100 private:
101 Vector< std::pair< Dart_Handle, v8::Handle<v8::Value> > > m_list;
102 };
103
104 class V8ToDartMap {
105 public:
106 V8ToDartMap()
107 : m_list()
108 {
109 }
110
111 Dart_Handle get(v8::Handle<v8::Value> key)
112 {
113 for (size_t i = 0; i < m_list.size(); ++i) {
114 if (m_list[i].first == key)
115 return m_list[i].second;
116 }
117 return 0;
118 }
119
120 Dart_Handle put(v8::Handle<v8::Value> key, Dart_Handle value)
121 {
122 ASSERT(!get(key));
123 m_list.append(std::make_pair(key, value));
124 return value;
125 }
126
127 private:
128 Vector< std::pair<v8::Handle<v8::Value> , Dart_Handle> > m_list;
129 };
130
131 v8::Handle<v8::Value> V8Converter::toV8IfPrimitive(DartDOMData* domData, Dart_Ha ndle value, Dart_Handle& exception)
132 {
133 if (Dart_IsNull(value))
134 return v8::Null(v8::Isolate::GetCurrent());
135 if (Dart_IsString(value))
136 return stringToV8(value);
137 if (Dart_IsBoolean(value))
138 return booleanToV8(value);
139 if (Dart_IsNumber(value))
140 return numberToV8(value, exception);
141 if (DartUtilities::isDateTime(domData, value))
142 return dateToV8(value, exception);
143
144 return v8::Handle<v8::Value>();
145 }
146
147 v8::Handle<v8::Value> V8Converter::toV8IfBrowserNative(DartDOMData* domData, Dar t_Handle value, Dart_Handle& exception)
148 {
149 if (Dart_IsByteBuffer(value)) {
150 Dart_Handle data = Dart_GetDataFromByteBuffer(value);
151 return arrayBufferToV8(data, exception);
152 }
153 if (Dart_IsTypedData(value))
154 return arrayBufferToV8(value, exception);
155 if (DartDOMWrapper::subtypeOf(value, DartBlob::dartClassId))
156 return blobToV8(value, exception);
157 if (DartDOMWrapper::subtypeOf(value, DartImageData::dartClassId))
158 return imageDataToV8(value, exception);
159 if (DartDOMWrapper::subtypeOf(value, DartIDBKeyRange::dartClassId))
160 return idbKeyRangeToV8(value, exception);
161 if (DartDOMWrapper::subtypeOf(value, DartDOMStringList::dartClassId))
162 return domStringListToV8(value, exception);
163 if (DartDOMWrapper::subtypeOf(value, DartNode::dartClassId))
164 return nodeToV8(value, exception);
165 if (DartDOMWrapper::subtypeOf(value, DartEvent::dartClassId))
166 return eventToV8(value, exception);
167 if (DartDOMWrapper::subtypeOf(value, DartWindow::dartClassId))
168 return windowToV8(value, exception);
169
170 return v8::Handle<v8::Value>();
171 }
172
173 v8::Handle<v8::Value> V8Converter::toV8(Dart_Handle value, DartToV8Map& map, Dar t_Handle& exception)
174 {
175 v8::Handle<v8::Value> result = map.get(value);
176 if (!result.IsEmpty())
177 return result;
178
179 DartDOMData* domData = DartDOMData::current();
180
181 result = toV8IfPrimitive(domData, value, exception);
182 if (!result.IsEmpty())
183 return result;
184
185 // ArrayBuffer{,View} checks in toV8IfBrowserNative must go before IsList
186 // check as they implement list.
187 result = toV8IfBrowserNative(domData, value, exception);
188 if (!result.IsEmpty()) {
189 map.put(value, result);
190 return result;
191 }
192
193 if (Dart_IsList(value))
194 return listToV8(value, map, exception);
195
196 bool isMap = DartUtilities::dartToBool(DartUtilities::invokeUtilsMethod("isM ap", 1, &value), exception);
197 ASSERT(!exception);
198 if (isMap)
199 return mapToV8(value, map, exception);
200
201 exception = Dart_NewStringFromCString("unsupported object type for conversio n");
202 return v8::Handle<v8::Value>();
203 }
204
205 v8::Handle<v8::Value> V8Converter::toV8(Dart_Handle value, Dart_Handle& exceptio n)
206 {
207 DartToV8Map map;
208 return toV8(value, map, exception);
209 }
210
211 Dart_Handle V8Converter::toDartIfPrimitive(v8::Handle<v8::Value> value)
212 {
213 if (value->IsUndefined())
214 return Dart_Null();
215 if (value->IsNull())
216 return Dart_Null();
217 if (value->IsString())
218 return stringToDart(value);
219 if (value->IsNumber())
220 return DartUtilities::numberToDart(value->NumberValue());
221 if (value->IsBoolean())
222 return Dart_NewBoolean(value->BooleanValue());
223 if (value->IsDate())
224 return dateToDart(value);
225 return 0;
226 }
227
228 /**
229 * Convert a subset of types that are backed by browser native objects.
230 * These are types that can be passed transparently between Dart and JS
231 * with the dart:js interop library.
232 */
233 Dart_Handle V8Converter::toDartIfBrowserNative(v8::Handle<v8::Object> object, v8 ::Isolate* isolate, Dart_Handle& exception)
234 {
235 if (V8ArrayBuffer::hasInstance(object, isolate))
236 return arrayBufferToDart(object, exception);
237 if (V8ArrayBufferView::hasInstance(object, isolate))
238 return arrayBufferViewToDart(object, exception);
239 if (V8Blob::hasInstance(object, isolate))
240 return blobToDart(object, exception);
241 if (V8DOMStringList::hasInstance(object, isolate))
242 return domStringListToDart(object, exception);
243 if (V8ImageData::hasInstance(object, isolate))
244 return imageDataToDart(object, exception);
245 if (V8IDBKeyRange::hasInstance(object, isolate))
246 return idbKeyRangeToDart(object, exception);
247 if (V8IDBDatabase::hasInstance(object, isolate))
248 return idbDatabaseToDart(object, exception);
249 if (V8IDBFactory::hasInstance(object, isolate))
250 return idbFactoryToDart(object, exception);
251 if (V8IDBCursorWithValue::hasInstance(object, isolate))
252 return idbCursorWithValueToDart(object, exception);
253 if (V8IDBCursor::hasInstance(object, isolate))
254 return idbCursorToDart(object, exception);
255 if (V8Node::hasInstance(object, isolate)) {
256 // FIXME(jacobr): there has to be a faster way to perform this check.
257 if (!object->CreationContext()->Global()->StrictEquals(DartUtilities::cu rrentV8Context()->Global())) {
258 // The window is from a different context so do not convert.
259 return 0;
260 }
261 return nodeToDart(object);
262 }
263 if (V8Event::hasInstance(object, isolate)) {
264 // FIXME(jacobr): there has to be a faster way to perform this check.
265 if (!object->CreationContext()->Global()->StrictEquals(DartUtilities::cu rrentV8Context()->Global())) {
266 // The window is from a different context so do not convert.
267 return 0;
268 }
269 return eventToDart(object);
270 }
271
272 v8::Handle<v8::Object> window = object->FindInstanceInPrototypeChain(V8Windo w::domTemplate(isolate));
273 if (!window.IsEmpty()) {
274 // FIXME(jacobr): there has to be a faster way to perform this check.
275 if (!object->CreationContext()->Global()->StrictEquals(DartUtilities::cu rrentV8Context()->Global())) {
276 // The window is from a different context so do not convert.
277 return 0;
278 }
279 return windowToDart(window);
280 }
281 return 0;
282 }
283
284 Dart_Handle V8Converter::toDart(v8::Handle<v8::Value> value, V8ToDartMap& map, D art_Handle& exception)
285 {
286 Dart_Handle result = map.get(value);
287 if (result)
288 return result;
289
290 result = toDartIfPrimitive(value);
291 if (result)
292 return result;
293
294 if (value->IsArray())
295 return listToDart(value.As<v8::Array>(), map, exception);
296 if (value->IsObject()) {
297 v8::Handle<v8::Object> object = value.As<v8::Object>();
298 v8::Isolate* isolate = object->CreationContext()->GetIsolate();
299
300 result = toDartIfBrowserNative(object, isolate, exception);
301 if (result) {
302 map.put(object, result);
303 return result;
304 }
305 return mapToDart(object, map, exception);
306 }
307
308 // FIXME: support other types.
309 exception = Dart_NewStringFromCString("unsupported object type for conversio n");
310 return 0;
311 }
312
313 Dart_Handle V8Converter::toDart(v8::Handle<v8::Value> value, Dart_Handle& except ion)
314 {
315 V8ToDartMap map;
316 return toDart(value, map, exception);
317 }
318
319 v8::Handle<v8::String> V8Converter::stringToV8(Dart_Handle value)
320 {
321 ASSERT(Dart_IsString(value));
322 uint8_t* data = 0;
323 intptr_t length = -1;
324 Dart_Handle ALLOW_UNUSED result = Dart_StringToUTF8(value, &data, &length);
325 ASSERT(!Dart_IsError(result));
326 return v8::String::NewFromUtf8(
327 v8::Isolate::GetCurrent(), reinterpret_cast<const char*>(data),
328 v8::String::kNormalString, length);
329 }
330
331 Dart_Handle V8Converter::stringToDart(v8::Handle<v8::Value> value)
332 {
333 ASSERT(value->IsString());
334 v8::String::Utf8Value stringValue(value);
335 const char* data = *stringValue;
336 return Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(data), string Value.length());
337 }
338
339 v8::Handle<v8::Value> V8Converter::nodeToV8(Dart_Handle value, Dart_Handle& exce ption)
340 {
341 Node* node = DartNode::toNative(value, exception);
342 ASSERT(!exception);
343 if (exception)
344 return v8::Handle<v8::Value>();
345 ASSERT(node);
346
347 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
348 return blink::toV8(node, state->context()->Global(), state->isolate());
349 }
350
351 Dart_Handle V8Converter::nodeToDart(v8::Handle<v8::Value> value)
352 {
353 ASSERT(value->IsObject());
354
355 v8::Handle<v8::Object> object = value.As<v8::Object>();
356 Node* node = V8Node::toNative(object);
357 ASSERT(node);
358 return DartNode::toDart(node);
359 }
360
361 v8::Handle<v8::Value> V8Converter::eventToV8(Dart_Handle value, Dart_Handle& exc eption)
362 {
363 Event* event = DartEvent::toNative(value, exception);
364 ASSERT(!exception);
365 if (exception)
366 return v8::Handle<v8::Value>();
367 ASSERT(event);
368 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
369 return blink::toV8(event, state->context()->Global(), state->isolate());
370 }
371
372 Dart_Handle V8Converter::eventToDart(v8::Handle<v8::Value> value)
373 {
374 ASSERT(value->IsObject());
375
376 v8::Handle<v8::Object> object = value.As<v8::Object>();
377 Event* event = V8Event::toNative(object);
378 ASSERT(event);
379 return DartEvent::toDart(event);
380 }
381
382 v8::Handle<v8::Value> V8Converter::windowToV8(Dart_Handle value, Dart_Handle& ex ception)
383 {
384 LocalDOMWindow* window = DartWindow::toNative(value, exception);
385 ASSERT(!exception);
386 if (exception)
387 return v8::Handle<v8::Value>();
388 ASSERT(window);
389 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
390 return blink::toV8(window, state->context()->Global(), state->isolate());
391 }
392
393 Dart_Handle V8Converter::windowToDart(v8::Handle<v8::Value> value)
394 {
395 ASSERT(value->IsObject());
396
397 v8::Handle<v8::Object> object = value.As<v8::Object>();
398 LocalDOMWindow* window = V8Window::toNative(object);
399 ASSERT(window);
400 return DartWindow::toDart(window);
401 }
402
403 v8::Handle<v8::Value> V8Converter::booleanToV8(Dart_Handle value)
404 {
405 ASSERT(Dart_IsBoolean(value));
406 bool booleanValue;
407 Dart_Handle ALLOW_UNUSED result = Dart_BooleanValue(value, &booleanValue);
408 ASSERT(!Dart_IsError(result));
409 return v8::Boolean::New(v8::Isolate::GetCurrent(), booleanValue);
410 }
411
412 v8::Handle<v8::Value> V8Converter::numberToV8(Dart_Handle value)
413 {
414 Dart_Handle exception = 0;
415 v8::Handle<v8::Value> result = numberToV8(value, exception);
416 ASSERT(!exception);
417 return result;
418 }
419
420 v8::Handle<v8::Value> V8Converter::numberToV8(Dart_Handle value, Dart_Handle& ex ception)
421 {
422 ASSERT(Dart_IsNumber(value));
423
424 double asDouble = DartUtilities::dartToDouble(value, exception);
425 if (exception)
426 return v8::Undefined(v8::Isolate::GetCurrent());
427 return v8::Number::New(v8::Isolate::GetCurrent(), asDouble);
428 }
429
430 v8::Handle<v8::Value> V8Converter::listToV8(Dart_Handle value)
431 {
432 DartToV8Map map;
433 Dart_Handle exception = 0;
434 v8::Handle<v8::Value> result = listToV8(value, map, exception);
435 ASSERT(!exception);
436 return result;
437 }
438
439 v8::Handle<v8::Value> V8Converter::listToV8(Dart_Handle value, DartToV8Map& map, Dart_Handle& exception)
440 {
441 ASSERT(Dart_IsList(value));
442
443 intptr_t length = 0;
444 Dart_Handle result = Dart_ListLength(value, &length);
445 if (!DartUtilities::checkResult(result, exception))
446 return v8::Handle<v8::Value>();
447
448 v8::Local<v8::Array> array = v8::Array::New(v8::Isolate::GetCurrent(), lengt h);
449 map.put(value, array);
450
451 for (intptr_t i = 0; i < length; ++i) {
452 result = Dart_ListGetAt(value, i);
453 if (!DartUtilities::checkResult(result, exception))
454 return v8::Handle<v8::Value>();
455 v8::Handle<v8::Value> v8value = toV8(result, map, exception);
456 if (exception)
457 return v8::Handle<v8::Value>();
458 array->Set(i, v8value);
459 }
460
461 return array;
462 }
463
464 v8::Handle<v8::Value> V8Converter::dateToV8(Dart_Handle value)
465 {
466 Dart_Handle exception = 0;
467 v8::Handle<v8::Value> result = dateToV8(value, exception);
468 ASSERT(!exception);
469 return result;
470 }
471
472 v8::Handle<v8::Value> V8Converter::dateToV8(Dart_Handle value, Dart_Handle& exce ption)
473 {
474 ASSERT(DartUtilities::isDateTime(DartDOMData::current(), value));
475
476 Dart_Handle asDouble = DartUtilities::invokeUtilsMethod("dateTimeToDouble", 1, &value);
477
478 if (!DartUtilities::checkResult(asDouble, exception))
479 return v8::Handle<v8::Value>();
480
481 double doubleValue = DartUtilities::dartToDouble(asDouble, exception);
482
483 return v8::Date::New(v8::Isolate::GetCurrent(), doubleValue);
484 }
485
486 Dart_Handle V8Converter::dateToDart(v8::Handle<v8::Value> value)
487 {
488 ASSERT(value->IsDate());
489 return DartUtilities::dateToDart(value->NumberValue());
490 }
491
492 Dart_Handle V8Converter::blobToDart(v8::Handle<v8::Object> object, Dart_Handle& exception)
493 {
494 return DartBlob::toDart(V8Blob::toNative(object));
495 }
496
497 v8::Handle<v8::Value> V8Converter::blobToV8(Dart_Handle handle, Dart_Handle& exc eption)
498 {
499 Blob* blob = DartBlob::toNative(handle, exception);
500 if (exception)
501 return v8::Handle<v8::Value>();
502
503 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
504 return blink::toV8(blob, state->context()->Global(), state->isolate());
505 }
506
507 Dart_Handle V8Converter::imageDataToDart(v8::Handle<v8::Object> object, Dart_Han dle& exception)
508 {
509 return DartImageData::toDart(V8ImageData::toNative(object));
510 }
511
512 v8::Handle<v8::Value> V8Converter::imageDataToV8(Dart_Handle handle, Dart_Handle & exception)
513 {
514 ImageData* imageData = DartImageData::toNative(handle, exception);
515 if (exception)
516 return v8::Handle<v8::Value>();
517
518 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
519 return blink::toV8(imageData, state->context()->Global(), state->isolate());
520 }
521
522 Dart_Handle V8Converter::idbKeyRangeToDart(v8::Handle<v8::Object> object, Dart_H andle& exception)
523 {
524 return DartIDBKeyRange::toDart(V8IDBKeyRange::toNative(object));
525 }
526
527 Dart_Handle V8Converter::domStringListToDart(v8::Handle<v8::Object> object, Dart _Handle& exception)
528 {
529 return DartDOMStringList::toDart(V8DOMStringList::toNative(object));
530 }
531
532 Dart_Handle V8Converter::idbDatabaseToDart(v8::Handle<v8::Object> object, Dart_H andle& exception)
533 {
534 return DartIDBDatabase::toDart(V8IDBDatabase::toNative(object));
535 }
536
537 Dart_Handle V8Converter::idbFactoryToDart(v8::Handle<v8::Object> object, Dart_Ha ndle& exception)
538 {
539 return DartIDBFactory::toDart(V8IDBFactory::toNative(object));
540 }
541
542 Dart_Handle V8Converter::idbCursorToDart(v8::Handle<v8::Object> object, Dart_Han dle& exception)
543 {
544 return DartIDBCursor::toDart(V8IDBCursor::toNative(object));
545 }
546
547 Dart_Handle V8Converter::idbCursorWithValueToDart(v8::Handle<v8::Object> object, Dart_Handle& exception)
548 {
549 return DartIDBCursorWithValue::toDart(V8IDBCursorWithValue::toNative(object) );
550 }
551
552 v8::Handle<v8::Value> V8Converter::idbKeyRangeToV8(Dart_Handle handle, Dart_Hand le& exception)
553 {
554 IDBKeyRange* keyRange = DartIDBKeyRange::toNative(handle, exception);
555 if (exception)
556 return v8::Handle<v8::Value>();
557
558 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
559 return blink::toV8(keyRange, state->context()->Global(), state->isolate());
560 }
561
562 v8::Handle<v8::Value> V8Converter::domStringListToV8(Dart_Handle handle, Dart_Ha ndle& exception)
563 {
564 DOMStringList* list = DartDOMStringList::toNative(handle, exception);
565 if (exception)
566 return v8::Handle<v8::Value>();
567
568 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
569 return blink::toV8(list, state->context()->Global(), state->isolate());
570 }
571
572 Dart_Handle V8Converter::listToDart(v8::Handle<v8::Array> value, V8ToDartMap& ma p, Dart_Handle& exception)
573 {
574 ASSERT(value->IsArray());
575
576 uint32_t length = value->Length();
577
578 Dart_Handle list = Dart_NewList(length);
579 ASSERT(!Dart_IsError(list));
580 map.put(value, list);
581
582 for (uint32_t i = 0; i < length; ++i) {
583 Dart_Handle result = toDart(value->Get(i), map, exception);
584 if (exception)
585 return 0;
586 if (!DartUtilities::checkResult(result, exception))
587 return 0;
588 result = Dart_ListSetAt(list, i, result);
589 if (!DartUtilities::checkResult(result, exception))
590 return 0;
591 }
592
593 return list;
594 }
595
596 v8::Handle<v8::Value> V8Converter::mapToV8(Dart_Handle value, DartToV8Map& map, Dart_Handle& exception)
597 {
598 Dart_Handle asList = DartUtilities::invokeUtilsMethod("convertMapToList", 1, &value);
599 if (!DartUtilities::checkResult(asList, exception))
600 return v8::Handle<v8::Value>();
601 ASSERT(Dart_IsList(asList));
602
603 // Now we have a list [key, value, key, value, ....], create a v8 object and set necesary
604 // properties on it.
605 v8::Handle<v8::Object> object = v8::Object::New(v8::Isolate::GetCurrent());
606 map.put(value, object);
607
608 // We converted to internal Dart list, methods shouldn't throw exceptions no w.
609 intptr_t length = 0;
610 Dart_Handle ALLOW_UNUSED result = Dart_ListLength(asList, &length);
611 ASSERT(!Dart_IsError(result));
612 ASSERT(!(length % 2));
613 for (intptr_t i = 0; i < length; i += 2) {
614 v8::Handle<v8::Value> key = toV8(Dart_ListGetAt(asList, i), map, excepti on);
615 if (exception)
616 return v8::Handle<v8::Value>();
617 v8::Handle<v8::Value> value = toV8(Dart_ListGetAt(asList, i + 1), map, e xception);
618 if (exception)
619 return v8::Handle<v8::Value>();
620
621 object->Set(key, value);
622 }
623
624 return object;
625 }
626
627 Dart_Handle V8Converter::mapToDart(v8::Handle<v8::Object> object, V8ToDartMap& m ap, Dart_Handle& exception)
628 {
629 v8::Handle<v8::Array> ownPropertyNames = object->GetOwnPropertyNames();
630 uint32_t ownPropertiesCount = ownPropertyNames->Length();
631
632 Dart_Handle result = DartUtilities::invokeUtilsMethod("createMap", 0, 0);
633 ASSERT(!Dart_IsError(result));
634 map.put(object, result);
635
636 Dart_Handle asList = Dart_NewList(ownPropertiesCount * 2);
637 ASSERT(!Dart_IsError(asList));
638
639 for (uint32_t i = 0; i < ownPropertiesCount; i++) {
640 v8::Handle<v8::Value> v8key = ownPropertyNames->Get(i);
641
642 Dart_Handle key = toDart(v8key, map, exception);
643 if (exception)
644 return 0;
645 Dart_ListSetAt(asList, 2 * i, key);
646
647 Dart_Handle value = toDart(object->Get(v8key), map, exception);
648 if (exception)
649 return 0;
650 Dart_ListSetAt(asList, 2 * i + 1, value);
651 }
652
653 Dart_Handle args[2] = { result, asList };
654 DartUtilities::invokeUtilsMethod("populateMap", 2, args);
655 return result;
656 }
657
658 v8::Handle<v8::Value> V8Converter::arrayBufferToV8(Dart_Handle value, Dart_Handl e& exception)
659 {
660 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
661 return blink::toV8(DartUtilities::dartToExternalizedArrayBuffer(value, excep tion).get(), state->context()->Global(), state->isolate());
662 }
663
664 Dart_Handle V8Converter::arrayBufferToDart(v8::Handle<v8::Object> object, Dart_H andle& exception)
665 {
666 return DartUtilities::arrayBufferToDart(V8ArrayBuffer::toNative(object));
667 }
668
669 v8::Handle<v8::Value> V8Converter::arrayBufferViewToV8(Dart_Handle value, Dart_H andle& exception)
670 {
671 RefPtr<ArrayBufferView> view = DartUtilities::dartToExternalizedArrayBufferV iew(value, exception);
672 V8ScriptState* state = DartUtilities::v8ScriptStateForCurrentIsolate();
673
674 switch (view->type()) {
675 case ArrayBufferView::TypeInt8:
676 return blink::toV8(static_cast<Int8Array*>(view.get()), state->context() ->Global(), state->isolate());
677 case ArrayBufferView::TypeUint8:
678 return blink::toV8(static_cast<Uint8Array*>(view.get()), state->context( )->Global(), state->isolate());
679 case ArrayBufferView::TypeUint8Clamped:
680 return blink::toV8(static_cast<Uint8ClampedArray*>(view.get()), state->c ontext()->Global(), state->isolate());
681 case ArrayBufferView::TypeInt16:
682 return blink::toV8(static_cast<Int16Array*>(view.get()), state->context( )->Global(), state->isolate());
683 case ArrayBufferView::TypeUint16:
684 return blink::toV8(static_cast<Uint16Array*>(view.get()), state->context ()->Global(), state->isolate());
685 case ArrayBufferView::TypeInt32:
686 return blink::toV8(static_cast<Int32Array*>(view.get()), state->context( )->Global(), state->isolate());
687 case ArrayBufferView::TypeUint32:
688 return blink::toV8(static_cast<Uint32Array*>(view.get()), state->context ()->Global(), state->isolate());
689 case ArrayBufferView::TypeFloat32:
690 return blink::toV8(static_cast<Float32Array*>(view.get()), state->contex t()->Global(), state->isolate());
691 case ArrayBufferView::TypeFloat64:
692 return blink::toV8(static_cast<Float64Array*>(view.get()), state->contex t()->Global(), state->isolate());
693 case ArrayBufferView::TypeDataView:
694 return blink::toV8(static_cast<DataView*>(view.get()), state->context()- >Global(), state->isolate());
695 default:
696 ASSERT_NOT_REACHED();
697 return v8::Handle<v8::Value>();
698 }
699 }
700
701 Dart_Handle V8Converter::arrayBufferViewToDart(v8::Handle<v8::Object> object, Da rt_Handle& exception)
702 {
703 RefPtr<ArrayBufferView> view = V8ArrayBufferView::toNative(object);
704 return DartUtilities::arrayBufferViewToDart(view.get());
705 }
706
707 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/bindings-dart/core/dart/V8Converter.h ('k') | sky/engine/bindings-dart/core/dart/custom/DartBlobCustom.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698