OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright (C) 2011 Google Inc. All rights reserved. | 3 * Copyright (C) 2011 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 |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | 17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N |
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #include "V8ArrayBufferViewCustom.h" | 27 #include "V8ArrayBufferViewCustom.h" |
28 | 28 |
29 #include "V8ArrayBufferViewCustomScript.h" | 29 #include "V8ArrayBufferViewCustomScript.h" |
| 30 |
30 #include <v8.h> | 31 #include <v8.h> |
31 | 32 |
32 | 33 |
33 namespace WebCore { | 34 namespace WebCore { |
34 | 35 |
35 // The random suffix helps to avoid name collision. | 36 // The random suffix helps to avoid name collision. |
36 const char fastSetFlagName[] = "TypedArray::FastSet::8NkZVq"; | 37 const char hiddenCopyMethodName[] = "TypedArray::HiddenCopy::8NkZVq"; |
37 | 38 |
38 bool fastSetInstalled(v8::Handle<v8::Object> array) | 39 v8::Handle<v8::Value> getHiddenCopyMethod(v8::Handle<v8::Object> prototype) |
39 { | 40 { |
40 // Use a hidden flag in the global object an indicator of whether the fast | 41 v8::Handle<v8::String> key = v8::String::New(hiddenCopyMethodName); |
41 // 'set' is installed or not. | 42 return prototype->GetHiddenValue(key); |
42 v8::Handle<v8::Object> global = array->CreationContext()->Global(); | |
43 v8::Handle<v8::String> key = v8::String::New(fastSetFlagName); | |
44 v8::Handle<v8::Value> fastSetFlag = global->GetHiddenValue(key); | |
45 return !fastSetFlag.IsEmpty(); | |
46 } | 43 } |
47 | 44 |
48 void installFastSet(v8::Handle<v8::Object> array) | 45 v8::Handle<v8::Value> installHiddenCopyMethod(v8::Handle<v8::Object> prototype)
{ |
49 { | |
50 v8::TryCatch tryCatch; | 46 v8::TryCatch tryCatch; |
51 tryCatch.SetVerbose(true); | 47 tryCatch.SetVerbose(true); |
52 v8::Handle<v8::Object> global = array->CreationContext()->Global(); | |
53 v8::Handle<v8::String> key = v8::String::New(fastSetFlagName); | |
54 global->SetHiddenValue(key, v8::Boolean::New(true)); | |
55 | |
56 String source(reinterpret_cast<const char*>(V8ArrayBufferViewCustomScript_js
), | 48 String source(reinterpret_cast<const char*>(V8ArrayBufferViewCustomScript_js
), |
57 sizeof(V8ArrayBufferViewCustomScript_js)); | 49 sizeof(V8ArrayBufferViewCustomScript_js)); |
58 v8::Handle<v8::Script> script = v8::Script::Compile(v8String(source)); | 50 v8::Handle<v8::Script> script = v8::Script::Compile(v8String(source)); |
59 script->Run(); | 51 v8::Handle<v8::Value> value = script->Run(); |
| 52 v8::Handle<v8::String> key = v8::String::New(hiddenCopyMethodName); |
| 53 prototype->SetHiddenValue(key, value); |
| 54 return value; |
60 } | 55 } |
61 | 56 |
62 | 57 bool copyElements(v8::Handle<v8::Object> destArray, v8::Handle<v8::Object> srcAr
ray, uint32_t length, uint32_t offset) |
63 void copyElements(v8::Handle<v8::Object> destArray, v8::Handle<v8::Object> srcAr
ray, uint32_t offset) | |
64 { | 58 { |
65 v8::Handle<v8::String> key = v8::String::New("set"); | 59 v8::Handle<v8::Value> prototype_value = destArray->GetPrototype(); |
66 v8::Handle<v8::Function> set = destArray->Get(key).As<v8::Function>(); | 60 if (prototype_value.IsEmpty() || !prototype_value->IsObject()) |
67 v8::Handle<v8::Value> arguments[2]; | 61 return false; |
68 int numberOfArguments = 1; | 62 v8::Handle<v8::Object> prototype = prototype_value.As<v8::Object>(); |
| 63 v8::Handle<v8::Value> value = getHiddenCopyMethod(prototype); |
| 64 if (value.IsEmpty()) |
| 65 value = installHiddenCopyMethod(prototype); |
| 66 if (value.IsEmpty() || !value->IsFunction()) |
| 67 return false; |
| 68 v8::Handle<v8::Function> copy_method = value.As<v8::Function>(); |
| 69 v8::Handle<v8::Value> arguments[3]; |
69 arguments[0] = srcArray; | 70 arguments[0] = srcArray; |
70 if (offset) { | 71 arguments[1] = v8::Uint32::New(length); |
71 arguments[1] = v8::Uint32::New(offset); | 72 arguments[2] = v8::Uint32::New(offset); |
72 numberOfArguments = 2; | 73 copy_method->Call(destArray, 3, arguments); |
73 } | 74 return true; |
74 set->Call(destArray, numberOfArguments, arguments); | |
75 } | 75 } |
76 | 76 |
77 } | 77 } |
OLD | NEW |