OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "bindings/core/v8/PrivateScriptRunner.h" | 6 #include "bindings/core/v8/PrivateScriptRunner.h" |
7 | 7 |
8 #include "bindings/core/v8/DOMWrapperWorld.h" | 8 #include "bindings/core/v8/DOMWrapperWorld.h" |
9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
11 #include "bindings/core/v8/V8PerContextData.h" | 11 #include "bindings/core/v8/V8PerContextData.h" |
12 #include "bindings/core/v8/V8ScriptRunner.h" | 12 #include "bindings/core/v8/V8ScriptRunner.h" |
13 #include "core/PrivateScriptSources.h" | 13 #include "core/PrivateScriptSources.h" |
14 #ifndef NDEBUG | 14 #ifndef NDEBUG |
15 #include "core/PrivateScriptSourcesForTesting.h" | 15 #include "core/PrivateScriptSourcesForTesting.h" |
16 #endif | 16 #endif |
17 #include "core/dom/ExceptionCode.h" | 17 #include "core/dom/ExceptionCode.h" |
18 | 18 |
19 namespace blink { | 19 namespace blink { |
20 | 20 |
21 static v8::Handle<v8::Value> compilePrivateScript(v8::Isolate* isolate, String c lassName) | 21 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the JS files |
22 // are compiled when any of the JS files is requested. Ideally we should avoid c ompiling | |
23 // unrelated JS files. For example, if a method in XPartial-1.js is requested, w e just | |
24 // need to compile X.js and XPartial-1.js, and don't need to compile XPartial-2. js. | |
25 static void compilePrivateScript(v8::Isolate* isolate, String className) | |
22 { | 26 { |
23 size_t index; | 27 int compiledScriptCount = 0; |
28 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated | |
29 // by make_private_script.py. | |
24 #ifndef NDEBUG | 30 #ifndef NDEBUG |
25 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting); i ndex++) { | 31 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTest ing); index++) { |
26 if (className == kPrivateScriptSourcesForTesting[index].name) | 32 if (className == kPrivateScriptSourcesForTesting[index].dependencyClassN ame) { |
27 break; | 33 v8::TryCatch block; |
Jens Widell
2014/08/09 14:52:25
We could unduplicate some code here; this block is
haraken
2014/08/11 01:18:30
Removed the duplication.
| |
28 } | 34 String source(reinterpret_cast<const char*>(kPrivateScriptSourcesFor Testing[index].source), kPrivateScriptSourcesForTesting[index].size); |
29 if (index != WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting)) { | 35 V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, source ), isolate); |
30 String source(reinterpret_cast<const char*>(kPrivateScriptSourcesForTest ing[index].source), kPrivateScriptSourcesForTesting[index].size); | 36 if (block.HasCaught()) { |
31 return V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, sou rce), isolate); | 37 WTF_LOG_ERROR("Private script error: Compile failed. (Class name = %s)\n", className.utf8().data()); |
38 if (!block.Message().IsEmpty()) | |
39 WTF_LOG_ERROR("%s\n", toCoreString(block.Message()->Get()).u tf8().data()); | |
40 RELEASE_ASSERT_NOT_REACHED(); | |
41 } | |
42 compiledScriptCount++; | |
43 } | |
32 } | 44 } |
33 #endif | 45 #endif |
34 | 46 |
47 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); inde x++) { | |
48 if (className == kPrivateScriptSources[index].dependencyClassName) { | |
49 v8::TryCatch block; | |
50 String source(reinterpret_cast<const char*>(kPrivateScriptSources[in dex].source), kPrivateScriptSources[index].size); | |
51 V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, source ), isolate); | |
52 if (block.HasCaught()) { | |
53 WTF_LOG_ERROR("Private script error: Compile failed. (Class name = %s)\n", className.utf8().data()); | |
54 if (!block.Message().IsEmpty()) | |
55 WTF_LOG_ERROR("%s\n", toCoreString(block.Message()->Get()).u tf8().data()); | |
56 RELEASE_ASSERT_NOT_REACHED(); | |
57 } | |
58 compiledScriptCount++; | |
59 } | |
60 } | |
61 | |
62 if (!compiledScriptCount) { | |
63 WTF_LOG_ERROR("Private script error: Target source code was not found. ( Class name = %s)\n", className.utf8().data()); | |
64 RELEASE_ASSERT_NOT_REACHED(); | |
65 } | |
66 } | |
67 | |
68 static v8::Handle<v8::Value> compilePrivateScriptRunner(v8::Isolate* isolate) | |
69 { | |
70 String className = "PrivateScriptRunner"; | |
71 size_t index; | |
35 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated | 72 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated |
36 // by make_private_script.py. | 73 // by make_private_script.py. |
37 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { | 74 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { |
38 if (className == kPrivateScriptSources[index].name) | 75 if (className == kPrivateScriptSources[index].className) |
39 break; | 76 break; |
40 } | 77 } |
41 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) { | 78 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) { |
42 WTF_LOG_ERROR("Private script error: Target source code was not found. ( Class name = %s)\n", className.utf8().data()); | 79 WTF_LOG_ERROR("Private script error: Target source code was not found. ( Class name = %s)\n", className.utf8().data()); |
43 RELEASE_ASSERT_NOT_REACHED(); | 80 RELEASE_ASSERT_NOT_REACHED(); |
44 } | 81 } |
45 | 82 |
46 v8::TryCatch block; | 83 v8::TryCatch block; |
47 String source(reinterpret_cast<const char*>(kPrivateScriptSources[index].sou rce), kPrivateScriptSources[index].size); | 84 String source(reinterpret_cast<const char*>(kPrivateScriptSources[index].sou rce), kPrivateScriptSources[index].size); |
48 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(v 8String(isolate, source), isolate); | 85 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(v 8String(isolate, source), isolate); |
49 if (block.HasCaught()) { | 86 if (block.HasCaught()) { |
50 WTF_LOG_ERROR("Private script error: Compile failed. (Class name = %s)\n ", className.utf8().data()); | 87 WTF_LOG_ERROR("Private script error: Compile failed. (Class name = %s)\n ", className.utf8().data()); |
51 if (!block.Message().IsEmpty()) | 88 if (!block.Message().IsEmpty()) |
52 WTF_LOG_ERROR("%s\n", toCoreString(block.Message()->Get()).utf8().da ta()); | 89 WTF_LOG_ERROR("%s\n", toCoreString(block.Message()->Get()).utf8().da ta()); |
53 RELEASE_ASSERT_NOT_REACHED(); | 90 RELEASE_ASSERT_NOT_REACHED(); |
54 } | 91 } |
55 return result; | 92 return result; |
56 } | 93 } |
57 | 94 |
58 static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptStat e, String className) | 95 static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptStat e, String className) |
59 { | 96 { |
60 ASSERT(scriptState->perContextData()); | 97 ASSERT(scriptState->perContextData()); |
61 ASSERT(scriptState->executionContext()); | 98 ASSERT(scriptState->executionContext()); |
62 v8::Isolate* isolate = scriptState->isolate(); | 99 v8::Isolate* isolate = scriptState->isolate(); |
63 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile dPrivateScript(className); | 100 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile dPrivateScript(className); |
64 if (compiledClass.IsEmpty()) { | 101 if (compiledClass.IsEmpty()) { |
65 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()-> compiledPrivateScript("PrivateScriptRunner"); | 102 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()-> compiledPrivateScript("PrivateScriptRunner"); |
66 if (installedClasses.IsEmpty()) { | 103 if (installedClasses.IsEmpty()) { |
67 installedClasses = compilePrivateScript(isolate, "PrivateScriptRunne r"); | 104 installedClasses = compilePrivateScriptRunner(isolate); |
68 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri ptRunner", installedClasses); | 105 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri ptRunner", installedClasses); |
69 } | 106 } |
70 RELEASE_ASSERT(!installedClasses.IsEmpty()); | 107 RELEASE_ASSERT(!installedClasses.IsEmpty()); |
71 RELEASE_ASSERT(installedClasses->IsObject()); | 108 RELEASE_ASSERT(installedClasses->IsObject()); |
72 | 109 |
73 compilePrivateScript(isolate, className); | 110 compilePrivateScript(isolate, className); |
74 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St ring(isolate, className)); | 111 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St ring(isolate, className)); |
75 RELEASE_ASSERT(!compiledClass.IsEmpty()); | 112 RELEASE_ASSERT(!compiledClass.IsEmpty()); |
76 RELEASE_ASSERT(compiledClass->IsObject()); | 113 RELEASE_ASSERT(compiledClass->IsObject()); |
77 scriptState->perContextData()->setCompiledPrivateScript(className, compi ledClass); | 114 scriptState->perContextData()->setCompiledPrivateScript(className, compi ledClass); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 v8::Handle<v8::Value> message = exceptionObject->Get(v8String(isolate, " message")); | 264 v8::Handle<v8::Value> message = exceptionObject->Get(v8String(isolate, " message")); |
228 RELEASE_ASSERT(!message.IsEmpty() && message->IsString()); | 265 RELEASE_ASSERT(!message.IsEmpty() && message->IsString()); |
229 exceptionState.throwDOMException(V8ReferenceError, toCoreString(v8::Hand le<v8::String>::Cast(message))); | 266 exceptionState.throwDOMException(V8ReferenceError, toCoreString(v8::Hand le<v8::String>::Cast(message))); |
230 exceptionState.throwIfNeeded(); | 267 exceptionState.throwIfNeeded(); |
231 return true; | 268 return true; |
232 } | 269 } |
233 return false; | 270 return false; |
234 } | 271 } |
235 | 272 |
236 } // namespace blink | 273 } // namespace blink |
OLD | NEW |