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

Side by Side Diff: Source/bindings/core/v8/PrivateScriptRunner.cpp

Issue 454773002: Blink-in-JS: Support private scripts in partial interfaces (PrivateScriptRunner part) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 4 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 | « Source/bindings/core/idl.gypi ('k') | Source/bindings/core/v8/PrivateScriptRunner.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #define LOG_ERROR_ALWAYS(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNC TION, __VA_ARGS__) 21 #define LOG_ERROR_ALWAYS(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNC TION, __VA_ARGS__)
22 22
23 static v8::Handle<v8::Value> compilePrivateScript(v8::Isolate* isolate, String c lassName) 23 static v8::Handle<v8::Value> compileAndRunInternalScript(v8::Isolate* isolate, S tring className, const unsigned char* source, size_t size)
24 { 24 {
25 size_t index;
26 #ifndef NDEBUG
27 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting); i ndex++) {
28 if (className == kPrivateScriptSourcesForTesting[index].name)
29 break;
30 }
31 if (index != WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting)) {
32 String source(reinterpret_cast<const char*>(kPrivateScriptSourcesForTest ing[index].source), kPrivateScriptSourcesForTesting[index].size);
33 return V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, sou rce), isolate);
34 }
35 #endif
36
37 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
38 // by make_private_script.py.
39 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) {
40 if (className == kPrivateScriptSources[index].name)
41 break;
42 }
43 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) {
44 LOG_ERROR_ALWAYS("Private script error: Target source code was not found . (Class name = %s)\n", className.utf8().data());
45 RELEASE_ASSERT_NOT_REACHED();
46 }
47
48 v8::TryCatch block; 25 v8::TryCatch block;
49 String source(reinterpret_cast<const char*>(kPrivateScriptSources[index].sou rce), kPrivateScriptSources[index].size); 26 String sourceString(reinterpret_cast<const char*>(source), size);
50 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(v 8String(isolate, source), isolate); 27 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(v 8String(isolate, sourceString), isolate);
51 if (block.HasCaught()) { 28 if (block.HasCaught()) {
52 LOG_ERROR_ALWAYS("Private script error: Compile failed. (Class name = %s )\n", className.utf8().data()); 29 LOG_ERROR_ALWAYS("Private script error: Compile failed. (Class name = %s )\n", className.utf8().data());
53 if (!block.Message().IsEmpty()) 30 if (!block.Message().IsEmpty())
54 LOG_ERROR_ALWAYS("%s\n", toCoreString(block.Message()->Get()).utf8() .data()); 31 LOG_ERROR_ALWAYS("%s\n", toCoreString(block.Message()->Get()).utf8() .data());
55 RELEASE_ASSERT_NOT_REACHED(); 32 RELEASE_ASSERT_NOT_REACHED();
56 } 33 }
57 return result; 34 return result;
58 } 35 }
59 36
37 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the JS files
38 // are compiled when any of the JS files is requested. Ideally we should avoid c ompiling
39 // unrelated JS files. For example, if a method in XPartial-1.js is requested, w e just
40 // need to compile X.js and XPartial-1.js, and don't need to compile XPartial-2. js.
41 static void compilePrivateScript(v8::Isolate* isolate, String className)
42 {
43 int compiledScriptCount = 0;
44 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, which is auto-generated
45 // by make_private_script_source.py.
46 #ifndef NDEBUG
47 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTest ing); index++) {
48 if (className == kPrivateScriptSourcesForTesting[index].dependencyClassN ame) {
49 compileAndRunInternalScript(isolate, className, kPrivateScriptSource sForTesting[index].source, kPrivateScriptSourcesForTesting[index].size);
50 compiledScriptCount++;
51 }
52 }
53 #endif
54
55 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
56 // by make_private_script_source.py.
57 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); inde x++) {
58 if (className == kPrivateScriptSources[index].dependencyClassName) {
59 compileAndRunInternalScript(isolate, className, kPrivateScriptSource s[index].source, kPrivateScriptSources[index].size);
60 compiledScriptCount++;
61 }
62 }
63
64 if (!compiledScriptCount) {
65 LOG_ERROR_ALWAYS("Private script error: Target source code was not found . (Class name = %s)\n", className.utf8().data());
66 RELEASE_ASSERT_NOT_REACHED();
67 }
68 }
69
70 static v8::Handle<v8::Value> compilePrivateScriptRunner(v8::Isolate* isolate)
71 {
72 const String className = "PrivateScriptRunner";
73 size_t index;
74 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
75 // by make_private_script_source.py.
76 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) {
77 if (className == kPrivateScriptSources[index].className)
78 break;
79 }
80 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) {
81 LOG_ERROR_ALWAYS("Private script error: Target source code was not found . (Class name = %s)\n", className.utf8().data());
82 RELEASE_ASSERT_NOT_REACHED();
83 }
84 return compileAndRunInternalScript(isolate, className, kPrivateScriptSources [index].source, kPrivateScriptSources[index].size);
85 }
86
60 static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptStat e, String className) 87 static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptStat e, String className)
61 { 88 {
62 ASSERT(scriptState->perContextData()); 89 ASSERT(scriptState->perContextData());
63 ASSERT(scriptState->executionContext()); 90 ASSERT(scriptState->executionContext());
64 v8::Isolate* isolate = scriptState->isolate(); 91 v8::Isolate* isolate = scriptState->isolate();
65 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile dPrivateScript(className); 92 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile dPrivateScript(className);
66 if (compiledClass.IsEmpty()) { 93 if (compiledClass.IsEmpty()) {
67 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()-> compiledPrivateScript("PrivateScriptRunner"); 94 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()-> compiledPrivateScript("PrivateScriptRunner");
68 if (installedClasses.IsEmpty()) { 95 if (installedClasses.IsEmpty()) {
69 installedClasses = compilePrivateScript(isolate, "PrivateScriptRunne r"); 96 installedClasses = compilePrivateScriptRunner(isolate);
70 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri ptRunner", installedClasses); 97 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri ptRunner", installedClasses);
71 } 98 }
72 RELEASE_ASSERT(!installedClasses.IsEmpty()); 99 RELEASE_ASSERT(!installedClasses.IsEmpty());
73 RELEASE_ASSERT(installedClasses->IsObject()); 100 RELEASE_ASSERT(installedClasses->IsObject());
74 101
75 compilePrivateScript(isolate, className); 102 compilePrivateScript(isolate, className);
76 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St ring(isolate, className)); 103 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St ring(isolate, className));
77 RELEASE_ASSERT(!compiledClass.IsEmpty()); 104 RELEASE_ASSERT(!compiledClass.IsEmpty());
78 RELEASE_ASSERT(compiledClass->IsObject()); 105 RELEASE_ASSERT(compiledClass->IsObject());
79 scriptState->perContextData()->setCompiledPrivateScript(className, compi ledClass); 106 scriptState->perContextData()->setCompiledPrivateScript(className, compi ledClass);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 v8::Handle<v8::Value> message = exceptionObject->Get(v8String(isolate, " message")); 256 v8::Handle<v8::Value> message = exceptionObject->Get(v8String(isolate, " message"));
230 RELEASE_ASSERT(!message.IsEmpty() && message->IsString()); 257 RELEASE_ASSERT(!message.IsEmpty() && message->IsString());
231 exceptionState.throwDOMException(V8ReferenceError, toCoreString(v8::Hand le<v8::String>::Cast(message))); 258 exceptionState.throwDOMException(V8ReferenceError, toCoreString(v8::Hand le<v8::String>::Cast(message)));
232 exceptionState.throwIfNeeded(); 259 exceptionState.throwIfNeeded();
233 return true; 260 return true;
234 } 261 }
235 return false; 262 return false;
236 } 263 }
237 264
238 } // namespace blink 265 } // namespace blink
OLDNEW
« no previous file with comments | « Source/bindings/core/idl.gypi ('k') | Source/bindings/core/v8/PrivateScriptRunner.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698