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