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

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

Issue 465283002: Blink-in-JS: Make error messages thrown in private scripts more verbose (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 | « no previous file | Source/bindings/core/v8/V8ScriptRunner.h » ('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 static void dumpV8Message(v8::Handle<v8::Message> message)
22 {
23 if (message.IsEmpty())
24 return;
22 25
23 static v8::Handle<v8::Value> compileAndRunInternalScript(v8::Isolate* isolate, S tring className, const unsigned char* source, size_t size) 26 v8::Handle<v8::Value> resourceName = message->GetScriptOrigin().ResourceName ();
27 String fileName = "Unknown JavaScript file";
28 if (!resourceName.IsEmpty() && resourceName->IsString())
29 fileName = toCoreString(v8::Handle<v8::String>::Cast(resourceName));
30 int lineNumber = message->GetLineNumber();
31 v8::Handle<v8::String> errorMessage = message->Get();
32 fprintf(stderr, "%s (line %d): %s\n", fileName.utf8().data(), lineNumber, to CoreString(errorMessage).utf8().data());
33 }
34
35 static void dumpJSError(String exceptionName, v8::Handle<v8::Message> message)
36 {
37 // FIXME: Set a ScriptOrigin of the private script and print a more informat ive message.
38 #ifndef NDEBUG
39 fprintf(stderr, "Private script throws an exception: %s\n", exceptionName.ut f8().data());
40 dumpV8Message(message);
41 #endif
42 }
43
44 static v8::Handle<v8::Value> compilePrivateScript(v8::Isolate* isolate, String c lassName, const unsigned char* source, size_t size)
Jens Widell 2014/08/13 12:08:38 I think "compileAndRun" was a bit more accurate. A
haraken 2014/08/13 12:37:57 Done.
24 { 45 {
25 v8::TryCatch block; 46 v8::TryCatch block;
26 String sourceString(reinterpret_cast<const char*>(source), size); 47 String sourceString(reinterpret_cast<const char*>(source), size);
27 v8::Handle<v8::Value> result = V8ScriptRunner::compileAndRunInternalScript(v 8String(isolate, sourceString), isolate); 48 String fileName = className + ".js";
Jens Widell 2014/08/13 12:08:38 This isn't necessarily correct for a partial inter
haraken 2014/08/13 12:37:57 Good point. I changed the code so that |className|
Jens Widell 2014/08/13 12:56:18 Sounds like a good plan.
49 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isola te, sourceString), fileName, TextPosition::minimumPosition(), 0, isolate, NotSha rableCrossOrigin, V8CacheOptionsOff);
28 if (block.HasCaught()) { 50 if (block.HasCaught()) {
29 LOG_ERROR_ALWAYS("Private script error: Compile failed. (Class name = %s )\n", className.utf8().data()); 51 fprintf(stderr, "Private script error: Compile failed. (Class name = %s) \n", className.utf8().data());
30 if (!block.Message().IsEmpty()) 52 dumpV8Message(block.Message());
31 LOG_ERROR_ALWAYS("%s\n", toCoreString(block.Message()->Get()).utf8() .data()); 53 RELEASE_ASSERT_NOT_REACHED();
54 }
55
56 v8::Handle<v8::Value> result = V8ScriptRunner::runCompiledInternalScript(scr ipt, isolate);
57 if (block.HasCaught()) {
58 fprintf(stderr, "Private script error: installClass() failed. (Class nam e = %s)\n", className.utf8().data());
59 dumpV8Message(block.Message());
32 RELEASE_ASSERT_NOT_REACHED(); 60 RELEASE_ASSERT_NOT_REACHED();
33 } 61 }
34 return result; 62 return result;
35 } 63 }
36 64
37 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the JS files 65 // 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 66 // 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 67 // 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. 68 // 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) 69 static void installPrivateScript(v8::Isolate* isolate, String className)
42 { 70 {
43 int compiledScriptCount = 0; 71 int compiledScriptCount = 0;
44 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, which is auto-generated 72 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, which is auto-generated
45 // by make_private_script_source.py. 73 // by make_private_script_source.py.
46 #ifndef NDEBUG 74 #ifndef NDEBUG
47 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTest ing); index++) { 75 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTest ing); index++) {
48 if (className == kPrivateScriptSourcesForTesting[index].dependencyClassN ame) { 76 if (className == kPrivateScriptSourcesForTesting[index].dependencyClassN ame) {
49 compileAndRunInternalScript(isolate, className, kPrivateScriptSource sForTesting[index].source, kPrivateScriptSourcesForTesting[index].size); 77 compilePrivateScript(isolate, className, kPrivateScriptSourcesForTes ting[index].source, kPrivateScriptSourcesForTesting[index].size);
50 compiledScriptCount++; 78 compiledScriptCount++;
51 } 79 }
52 } 80 }
53 #endif 81 #endif
54 82
55 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated 83 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
56 // by make_private_script_source.py. 84 // by make_private_script_source.py.
57 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); inde x++) { 85 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); inde x++) {
58 if (className == kPrivateScriptSources[index].dependencyClassName) { 86 if (className == kPrivateScriptSources[index].dependencyClassName) {
59 compileAndRunInternalScript(isolate, className, kPrivateScriptSource s[index].source, kPrivateScriptSources[index].size); 87 compilePrivateScript(isolate, className, kPrivateScriptSources[index ].source, kPrivateScriptSources[index].size);
60 compiledScriptCount++; 88 compiledScriptCount++;
61 } 89 }
62 } 90 }
63 91
64 if (!compiledScriptCount) { 92 if (!compiledScriptCount) {
65 LOG_ERROR_ALWAYS("Private script error: Target source code was not found . (Class name = %s)\n", className.utf8().data()); 93 fprintf(stderr, "Private script error: Target source code was not found. (Class name = %s)\n", className.utf8().data());
66 RELEASE_ASSERT_NOT_REACHED(); 94 RELEASE_ASSERT_NOT_REACHED();
67 } 95 }
68 } 96 }
69 97
70 static v8::Handle<v8::Value> compilePrivateScriptRunner(v8::Isolate* isolate) 98 static v8::Handle<v8::Value> installPrivateScriptRunner(v8::Isolate* isolate)
71 { 99 {
72 const String className = "PrivateScriptRunner"; 100 const String className = "PrivateScriptRunner";
73 size_t index; 101 size_t index;
74 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated 102 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is auto-generated
75 // by make_private_script_source.py. 103 // by make_private_script_source.py.
76 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { 104 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) {
77 if (className == kPrivateScriptSources[index].className) 105 if (className == kPrivateScriptSources[index].className)
78 break; 106 break;
79 } 107 }
80 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) { 108 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()); 109 fprintf(stderr, "Private script error: Target source code was not found. (Class name = %s)\n", className.utf8().data());
82 RELEASE_ASSERT_NOT_REACHED(); 110 RELEASE_ASSERT_NOT_REACHED();
83 } 111 }
84 return compileAndRunInternalScript(isolate, className, kPrivateScriptSources [index].source, kPrivateScriptSources[index].size); 112 return compilePrivateScript(isolate, className, kPrivateScriptSources[index] .source, kPrivateScriptSources[index].size);
85 } 113 }
86 114
87 static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptStat e, String className) 115 static v8::Handle<v8::Object> classObjectOfPrivateScript(ScriptState* scriptStat e, String className)
88 { 116 {
89 ASSERT(scriptState->perContextData()); 117 ASSERT(scriptState->perContextData());
90 ASSERT(scriptState->executionContext()); 118 ASSERT(scriptState->executionContext());
91 v8::Isolate* isolate = scriptState->isolate(); 119 v8::Isolate* isolate = scriptState->isolate();
92 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile dPrivateScript(className); 120 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile dPrivateScript(className);
93 if (compiledClass.IsEmpty()) { 121 if (compiledClass.IsEmpty()) {
94 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()-> compiledPrivateScript("PrivateScriptRunner"); 122 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()-> compiledPrivateScript("PrivateScriptRunner");
95 if (installedClasses.IsEmpty()) { 123 if (installedClasses.IsEmpty()) {
96 installedClasses = compilePrivateScriptRunner(isolate); 124 installedClasses = installPrivateScriptRunner(isolate);
97 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri ptRunner", installedClasses); 125 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri ptRunner", installedClasses);
98 } 126 }
99 RELEASE_ASSERT(!installedClasses.IsEmpty()); 127 RELEASE_ASSERT(!installedClasses.IsEmpty());
100 RELEASE_ASSERT(installedClasses->IsObject()); 128 RELEASE_ASSERT(installedClasses->IsObject());
101 129
102 compilePrivateScript(isolate, className); 130 installPrivateScript(isolate, className);
103 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St ring(isolate, className)); 131 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St ring(isolate, className));
104 RELEASE_ASSERT(!compiledClass.IsEmpty()); 132 RELEASE_ASSERT(!compiledClass.IsEmpty());
105 RELEASE_ASSERT(compiledClass->IsObject()); 133 RELEASE_ASSERT(compiledClass->IsObject());
106 scriptState->perContextData()->setCompiledPrivateScript(className, compi ledClass); 134 scriptState->perContextData()->setCompiledPrivateScript(className, compi ledClass);
107 } 135 }
108 return v8::Handle<v8::Object>::Cast(compiledClass); 136 return v8::Handle<v8::Object>::Cast(compiledClass);
109 } 137 }
110 138
111 static void initializeHolderIfNeeded(ScriptState* scriptState, v8::Handle<v8::Ob ject> classObject, v8::Handle<v8::Value> holder) 139 static void initializeHolderIfNeeded(ScriptState* scriptState, v8::Handle<v8::Ob ject> classObject, v8::Handle<v8::Value> holder)
112 { 140 {
113 RELEASE_ASSERT(!holder.IsEmpty()); 141 RELEASE_ASSERT(!holder.IsEmpty());
114 RELEASE_ASSERT(holder->IsObject()); 142 RELEASE_ASSERT(holder->IsObject());
115 v8::Handle<v8::Object> holderObject = v8::Handle<v8::Object>::Cast(holder); 143 v8::Handle<v8::Object> holderObject = v8::Handle<v8::Object>::Cast(holder);
116 v8::Isolate* isolate = scriptState->isolate(); 144 v8::Isolate* isolate = scriptState->isolate();
117 v8::Handle<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(isolate, holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate)); 145 v8::Handle<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(isolate, holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate));
118 if (isInitialized.IsEmpty()) { 146 if (isInitialized.IsEmpty()) {
119 v8::TryCatch block; 147 v8::TryCatch block;
120 v8::Handle<v8::Value> initializeFunction = classObject->Get(v8String(iso late, "initialize")); 148 v8::Handle<v8::Value> initializeFunction = classObject->Get(v8String(iso late, "initialize"));
121 if (!initializeFunction.IsEmpty() && initializeFunction->IsFunction()) { 149 if (!initializeFunction.IsEmpty() && initializeFunction->IsFunction()) {
122 v8::TryCatch block; 150 v8::TryCatch block;
123 V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(initiali zeFunction), scriptState->executionContext(), holder, 0, 0, isolate); 151 V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(initiali zeFunction), scriptState->executionContext(), holder, 0, 0, isolate);
124 if (block.HasCaught()) { 152 if (block.HasCaught()) {
125 LOG_ERROR_ALWAYS("Private script error: Object constructor threw an exception.\n"); 153 fprintf(stderr, "Private script error: Object constructor threw an exception.\n");
126 if (!block.Message().IsEmpty()) 154 dumpV8Message(block.Message());
127 LOG_ERROR_ALWAYS("%s\n", toCoreString(block.Message()->Get() ).utf8().data());
128 RELEASE_ASSERT_NOT_REACHED(); 155 RELEASE_ASSERT_NOT_REACHED();
129 } 156 }
130 } 157 }
131 158
132 // Inject the prototype object of the private script into the prototype chain of the holder object. 159 // Inject the prototype object of the private script into the prototype chain of the holder object.
133 // This is necessary to let the holder object use properties defined on the prototype object 160 // This is necessary to let the holder object use properties defined on the prototype object
134 // of the private script. (e.g., if the prototype object has |foo|, the holder object should be able 161 // of the private script. (e.g., if the prototype object has |foo|, the holder object should be able
135 // to use it with |this.foo|.) 162 // to use it with |this.foo|.)
136 if (classObject->GetPrototype() != holderObject->GetPrototype()) 163 if (classObject->GetPrototype() != holderObject->GetPrototype())
137 classObject->SetPrototype(holderObject->GetPrototype()); 164 classObject->SetPrototype(holderObject->GetPrototype());
(...skipping 18 matching lines...) Expand all
156 183
157 ScriptState::Scope scope(scriptState); 184 ScriptState::Scope scope(scriptState);
158 return classObjectOfPrivateScript(scriptState, className); 185 return classObjectOfPrivateScript(scriptState, className);
159 } 186 }
160 187
161 v8::Handle<v8::Value> PrivateScriptRunner::runDOMAttributeGetter(ScriptState* sc riptState, String className, String attributeName, v8::Handle<v8::Value> holder) 188 v8::Handle<v8::Value> PrivateScriptRunner::runDOMAttributeGetter(ScriptState* sc riptState, String className, String attributeName, v8::Handle<v8::Value> holder)
162 { 189 {
163 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className); 190 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className);
164 v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8S tring(scriptState->isolate(), attributeName)); 191 v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8S tring(scriptState->isolate(), attributeName));
165 if (descriptor.IsEmpty() || !descriptor->IsObject()) { 192 if (descriptor.IsEmpty() || !descriptor->IsObject()) {
166 LOG_ERROR_ALWAYS("Private script error: Target DOM attribute getter was not found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), a ttributeName.utf8().data()); 193 fprintf(stderr, "Private script error: Target DOM attribute getter was n ot found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), at tributeName.utf8().data());
167 RELEASE_ASSERT_NOT_REACHED(); 194 RELEASE_ASSERT_NOT_REACHED();
168 } 195 }
169 v8::Handle<v8::Value> getter = v8::Handle<v8::Object>::Cast(descriptor)->Get (v8String(scriptState->isolate(), "get")); 196 v8::Handle<v8::Value> getter = v8::Handle<v8::Object>::Cast(descriptor)->Get (v8String(scriptState->isolate(), "get"));
170 if (getter.IsEmpty() || !getter->IsFunction()) { 197 if (getter.IsEmpty() || !getter->IsFunction()) {
171 LOG_ERROR_ALWAYS("Private script error: Target DOM attribute getter was not found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), a ttributeName.utf8().data()); 198 fprintf(stderr, "Private script error: Target DOM attribute getter was n ot found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), at tributeName.utf8().data());
172 RELEASE_ASSERT_NOT_REACHED(); 199 RELEASE_ASSERT_NOT_REACHED();
173 } 200 }
174 initializeHolderIfNeeded(scriptState, classObject, holder); 201 initializeHolderIfNeeded(scriptState, classObject, holder);
175 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(getter), scriptState->executionContext(), holder, 0, 0, scriptState->isolate()); 202 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(getter), scriptState->executionContext(), holder, 0, 0, scriptState->isolate());
176 } 203 }
177 204
178 void PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, String className, String attributeName, v8::Handle<v8::Value> holder, v8::Handle<v8::V alue> v8Value) 205 void PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, String className, String attributeName, v8::Handle<v8::Value> holder, v8::Handle<v8::V alue> v8Value)
179 { 206 {
180 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className); 207 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className);
181 v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8S tring(scriptState->isolate(), attributeName)); 208 v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8S tring(scriptState->isolate(), attributeName));
182 if (descriptor.IsEmpty() || !descriptor->IsObject()) { 209 if (descriptor.IsEmpty() || !descriptor->IsObject()) {
183 LOG_ERROR_ALWAYS("Private script error: Target DOM attribute setter was not found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), a ttributeName.utf8().data()); 210 fprintf(stderr, "Private script error: Target DOM attribute setter was n ot found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), at tributeName.utf8().data());
184 RELEASE_ASSERT_NOT_REACHED(); 211 RELEASE_ASSERT_NOT_REACHED();
185 } 212 }
186 v8::Handle<v8::Value> setter = v8::Handle<v8::Object>::Cast(descriptor)->Get (v8String(scriptState->isolate(), "set")); 213 v8::Handle<v8::Value> setter = v8::Handle<v8::Object>::Cast(descriptor)->Get (v8String(scriptState->isolate(), "set"));
187 if (setter.IsEmpty() || !setter->IsFunction()) { 214 if (setter.IsEmpty() || !setter->IsFunction()) {
188 LOG_ERROR_ALWAYS("Private script error: Target DOM attribute setter was not found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), a ttributeName.utf8().data()); 215 fprintf(stderr, "Private script error: Target DOM attribute setter was n ot found. (Class name = %s, Attribute name = %s)\n", className.utf8().data(), at tributeName.utf8().data());
189 RELEASE_ASSERT_NOT_REACHED(); 216 RELEASE_ASSERT_NOT_REACHED();
190 } 217 }
191 initializeHolderIfNeeded(scriptState, classObject, holder); 218 initializeHolderIfNeeded(scriptState, classObject, holder);
192 v8::Handle<v8::Value> argv[] = { v8Value }; 219 v8::Handle<v8::Value> argv[] = { v8Value };
193 V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(setter), scriptS tate->executionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, scriptState->iso late()); 220 V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(setter), scriptS tate->executionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, scriptState->iso late());
194 } 221 }
195 222
196 v8::Handle<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState , String className, String methodName, v8::Handle<v8::Value> holder, int argc, v 8::Handle<v8::Value> argv[]) 223 v8::Handle<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState , String className, String methodName, v8::Handle<v8::Value> holder, int argc, v 8::Handle<v8::Value> argv[])
197 { 224 {
198 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className); 225 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState, className);
199 v8::Handle<v8::Value> method = classObject->Get(v8String(scriptState->isolat e(), methodName)); 226 v8::Handle<v8::Value> method = classObject->Get(v8String(scriptState->isolat e(), methodName));
200 if (method.IsEmpty() || !method->IsFunction()) { 227 if (method.IsEmpty() || !method->IsFunction()) {
201 LOG_ERROR_ALWAYS("Private script error: Target DOM method was not found. (Class name = %s, Method name = %s)\n", className.utf8().data(), methodName.utf 8().data()); 228 fprintf(stderr, "Private script error: Target DOM method was not found. (Class name = %s, Method name = %s)\n", className.utf8().data(), methodName.utf8 ().data());
202 RELEASE_ASSERT_NOT_REACHED(); 229 RELEASE_ASSERT_NOT_REACHED();
203 } 230 }
204 initializeHolderIfNeeded(scriptState, classObject, holder); 231 initializeHolderIfNeeded(scriptState, classObject, holder);
205 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(method), scriptState->executionContext(), holder, argc, argv, scriptState->isolate()); 232 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(method), scriptState->executionContext(), holder, argc, argv, scriptState->isolate());
206 } 233 }
207 234
208 static void dumpJSError(String exceptionName, String message)
209 {
210 // FIXME: Set a ScriptOrigin of the private script and print a more informat ive message.
211 #ifndef NDEBUG
212 fprintf(stderr, "Private script throws an exception: %s\n", exceptionName.ut f8().data());
213 if (!message.isEmpty())
214 fprintf(stderr, "%s\n", message.utf8().data());
215 #endif
216 }
217
218 bool PrivateScriptRunner::rethrowExceptionInPrivateScript(v8::Isolate* isolate, ExceptionState& exceptionState, v8::TryCatch& block) 235 bool PrivateScriptRunner::rethrowExceptionInPrivateScript(v8::Isolate* isolate, ExceptionState& exceptionState, v8::TryCatch& block)
219 { 236 {
220 v8::Handle<v8::Value> exception = block.Exception(); 237 v8::Handle<v8::Value> exception = block.Exception();
221 if (exception.IsEmpty() || !exception->IsObject()) 238 if (exception.IsEmpty() || !exception->IsObject())
222 return false; 239 return false;
223 240
224 v8::Handle<v8::Object> exceptionObject = v8::Handle<v8::Object>::Cast(except ion); 241 v8::Handle<v8::Object> exceptionObject = v8::Handle<v8::Object>::Cast(except ion);
225 v8::Handle<v8::Value> name = exceptionObject->Get(v8String(isolate, "name")) ; 242 v8::Handle<v8::Value> name = exceptionObject->Get(v8String(isolate, "name")) ;
226 if (name.IsEmpty() || !name->IsString()) 243 if (name.IsEmpty() || !name->IsString())
227 return false; 244 return false;
245
246 v8::Handle<v8::Message> tryCatchMessage = block.Message();
247 v8::Handle<v8::Value> message = exceptionObject->Get(v8String(isolate, "mess age"));
248 String messageString;
249 if (!message.IsEmpty() && message->IsString())
250 messageString = toCoreString(v8::Handle<v8::String>::Cast(message));
251
228 String exceptionName = toCoreString(v8::Handle<v8::String>::Cast(name)); 252 String exceptionName = toCoreString(v8::Handle<v8::String>::Cast(name));
229 if (exceptionName == "DOMExceptionInPrivateScript") { 253 if (exceptionName == "DOMExceptionInPrivateScript") {
230 v8::Handle<v8::Value> v8Message = exceptionObject->Get(v8String(isolate, "message"));
231 RELEASE_ASSERT(!v8Message.IsEmpty() && v8Message->IsString());
232 v8::Handle<v8::Value> code = exceptionObject->Get(v8String(isolate, "cod e")); 254 v8::Handle<v8::Value> code = exceptionObject->Get(v8String(isolate, "cod e"));
233 RELEASE_ASSERT(!code.IsEmpty() && code->IsInt32()); 255 RELEASE_ASSERT(!code.IsEmpty() && code->IsInt32());
234 exceptionState.throwDOMException(toInt32(code), toCoreString(v8::Handle< v8::String>::Cast(v8Message))); 256 exceptionState.throwDOMException(toInt32(code), messageString);
235 exceptionState.throwIfNeeded(); 257 exceptionState.throwIfNeeded();
236 return true; 258 return true;
237 } 259 }
238 if (exceptionName == "Error") { 260 if (exceptionName == "Error") {
239 v8::Handle<v8::Value> v8Message = exceptionObject->Get(v8String(isolate, "message")); 261 exceptionState.throwDOMException(V8GeneralError, messageString);
240 RELEASE_ASSERT(!v8Message.IsEmpty() && v8Message->IsString());
241 String message = toCoreString(v8::Handle<v8::String>::Cast(v8Message));
242 exceptionState.throwDOMException(V8GeneralError, message);
243 exceptionState.throwIfNeeded(); 262 exceptionState.throwIfNeeded();
244 dumpJSError(exceptionName, message); 263 dumpJSError(exceptionName, tryCatchMessage);
245 return true; 264 return true;
246 } 265 }
247 if (exceptionName == "TypeError") { 266 if (exceptionName == "TypeError") {
248 v8::Handle<v8::Value> v8Message = exceptionObject->Get(v8String(isolate, "message")); 267 exceptionState.throwDOMException(V8TypeError, messageString);
249 RELEASE_ASSERT(!v8Message.IsEmpty() && v8Message->IsString());
250 String message = toCoreString(v8::Handle<v8::String>::Cast(v8Message));
251 exceptionState.throwDOMException(V8TypeError, message);
252 exceptionState.throwIfNeeded(); 268 exceptionState.throwIfNeeded();
253 dumpJSError(exceptionName, message); 269 dumpJSError(exceptionName, tryCatchMessage);
254 return true; 270 return true;
255 } 271 }
256 if (exceptionName == "RangeError") { 272 if (exceptionName == "RangeError") {
257 v8::Handle<v8::Value> v8Message = exceptionObject->Get(v8String(isolate, "message")); 273 exceptionState.throwDOMException(V8RangeError, messageString);
258 RELEASE_ASSERT(!v8Message.IsEmpty() && v8Message->IsString());
259 String message = toCoreString(v8::Handle<v8::String>::Cast(v8Message));
260 exceptionState.throwDOMException(V8RangeError, message);
261 exceptionState.throwIfNeeded(); 274 exceptionState.throwIfNeeded();
262 dumpJSError(exceptionName, message); 275 dumpJSError(exceptionName, tryCatchMessage);
263 return true; 276 return true;
264 } 277 }
265 if (exceptionName == "SyntaxError") { 278 if (exceptionName == "SyntaxError") {
266 v8::Handle<v8::Value> v8Message = exceptionObject->Get(v8String(isolate, "message")); 279 exceptionState.throwDOMException(V8SyntaxError, messageString);
267 RELEASE_ASSERT(!v8Message.IsEmpty() && v8Message->IsString());
268 String message = toCoreString(v8::Handle<v8::String>::Cast(v8Message));
269 exceptionState.throwDOMException(V8SyntaxError, message);
270 exceptionState.throwIfNeeded(); 280 exceptionState.throwIfNeeded();
271 dumpJSError(exceptionName, message); 281 dumpJSError(exceptionName, tryCatchMessage);
272 return true; 282 return true;
273 } 283 }
274 if (exceptionName == "ReferenceError") { 284 if (exceptionName == "ReferenceError") {
275 v8::Handle<v8::Value> v8Message = exceptionObject->Get(v8String(isolate, "message")); 285 exceptionState.throwDOMException(V8ReferenceError, messageString);
276 RELEASE_ASSERT(!v8Message.IsEmpty() && v8Message->IsString());
277 String message = toCoreString(v8::Handle<v8::String>::Cast(v8Message));
278 exceptionState.throwDOMException(V8ReferenceError, message);
279 exceptionState.throwIfNeeded(); 286 exceptionState.throwIfNeeded();
280 dumpJSError(exceptionName, message); 287 dumpJSError(exceptionName, tryCatchMessage);
281 return true; 288 return true;
282 } 289 }
283 return false; 290 return false;
284 } 291 }
285 292
286 } // namespace blink 293 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | Source/bindings/core/v8/V8ScriptRunner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698