| 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 "bindings/core/v8/PrivateScriptRunner.h" | 5 #include "bindings/core/v8/PrivateScriptRunner.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/DOMWrapperWorld.h" | 7 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
| 10 #include "bindings/core/v8/V8PerContextData.h" | 10 #include "bindings/core/v8/V8PerContextData.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 result, block)) { | 103 result, block)) { |
| 104 fprintf(stderr, | 104 fprintf(stderr, |
| 105 "Private script error: installClass() failed. (Class name = %s)\n", | 105 "Private script error: installClass() failed. (Class name = %s)\n", |
| 106 scriptClassName.utf8().data()); | 106 scriptClassName.utf8().data()); |
| 107 dumpV8Message(context, block.Message()); | 107 dumpV8Message(context, block.Message()); |
| 108 RELEASE_NOTREACHED(); | 108 RELEASE_NOTREACHED(); |
| 109 } | 109 } |
| 110 return result; | 110 return result; |
| 111 } | 111 } |
| 112 | 112 |
| 113 // Private scripts can use privateScriptController.import(bundledResource, compi
leAndRunScript) to import dependent resources. | 113 // Private scripts can use privateScriptController.import(bundledResource, |
| 114 // compileAndRunScript) to import dependent resources. |
| 114 // |bundledResource| is a string resource name. | 115 // |bundledResource| is a string resource name. |
| 115 // |compileAndRunScript| optional boolean representing if the javascript should
be executed. Default: true. | 116 // |compileAndRunScript| optional boolean representing if the javascript should |
| 117 // be executed. Default: true. |
| 116 void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args) { | 118 void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 117 v8::Isolate* isolate = args.GetIsolate(); | 119 v8::Isolate* isolate = args.GetIsolate(); |
| 118 RELEASE_ASSERT(isolate && (args.Length() >= 1)); | 120 RELEASE_ASSERT(isolate && (args.Length() >= 1)); |
| 119 String resourceFileName = toCoreString( | 121 String resourceFileName = toCoreString( |
| 120 args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked()); | 122 args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked()); |
| 121 String resourceData = | 123 String resourceData = |
| 122 loadResourceAsASCIIString(resourceFileName.utf8().data()); | 124 loadResourceAsASCIIString(resourceFileName.utf8().data()); |
| 123 RELEASE_ASSERT(resourceData.length()); | 125 RELEASE_ASSERT(resourceData.length()); |
| 124 bool compileAndRunScript = true; | 126 bool compileAndRunScript = true; |
| 125 if (args.Length() == 2) { | 127 if (args.Length() == 2) { |
| 126 RELEASE_ASSERT(args[1]->IsBoolean()); | 128 RELEASE_ASSERT(args[1]->IsBoolean()); |
| 127 compileAndRunScript = args[1].As<v8::Boolean>()->Value(); | 129 compileAndRunScript = args[1].As<v8::Boolean>()->Value(); |
| 128 } | 130 } |
| 129 | 131 |
| 130 if (resourceFileName.endsWith(".js") && compileAndRunScript) | 132 if (resourceFileName.endsWith(".js") && compileAndRunScript) |
| 131 compileAndRunPrivateScript( | 133 compileAndRunPrivateScript( |
| 132 ScriptState::current(isolate), resourceFileName.replace(".js", ""), | 134 ScriptState::current(isolate), resourceFileName.replace(".js", ""), |
| 133 resourceData.utf8().data(), resourceData.length()); | 135 resourceData.utf8().data(), resourceData.length()); |
| 134 args.GetReturnValue().Set(v8String(isolate, resourceData)); | 136 args.GetReturnValue().Set(v8String(isolate, resourceData)); |
| 135 } | 137 } |
| 136 | 138 |
| 137 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the
JS files | 139 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the |
| 138 // are compiled when any of the JS files is requested. Ideally we should avoid c
ompiling | 140 // JS files are compiled when any of the JS files is requested. Ideally we |
| 139 // unrelated JS files. For example, if a method in XPartial-1.js is requested, w
e just | 141 // should avoid compiling unrelated JS files. For example, if a method in |
| 140 // need to compile X.js and XPartial-1.js, and don't need to compile XPartial-2.
js. | 142 // XPartial-1.js is requested, we just need to compile X.js and XPartial-1.js, |
| 143 // and don't need to compile XPartial-2.js. |
| 141 static void installPrivateScript(v8::Isolate* isolate, String className) { | 144 static void installPrivateScript(v8::Isolate* isolate, String className) { |
| 142 ScriptState* scriptState = ScriptState::current(isolate); | 145 ScriptState* scriptState = ScriptState::current(isolate); |
| 143 int compiledScriptCount = 0; | 146 int compiledScriptCount = 0; |
| 144 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, whi
ch is auto-generated | 147 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, |
| 145 // by make_private_script_source.py. | 148 // which is auto-generated by make_private_script_source.py. |
| 146 #ifndef NDEBUG | 149 #ifndef NDEBUG |
| 147 for (size_t index = 0; | 150 for (size_t index = 0; |
| 148 index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting); index++) { | 151 index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting); index++) { |
| 149 if (className == kPrivateScriptSourcesForTesting[index].className) { | 152 if (className == kPrivateScriptSourcesForTesting[index].className) { |
| 150 compileAndRunPrivateScript( | 153 compileAndRunPrivateScript( |
| 151 scriptState, kPrivateScriptSourcesForTesting[index].scriptClassName, | 154 scriptState, kPrivateScriptSourcesForTesting[index].scriptClassName, |
| 152 kPrivateScriptSourcesForTesting[index].source, | 155 kPrivateScriptSourcesForTesting[index].source, |
| 153 kPrivateScriptSourcesForTesting[index].size); | 156 kPrivateScriptSourcesForTesting[index].size); |
| 154 compiledScriptCount++; | 157 compiledScriptCount++; |
| 155 } | 158 } |
| 156 } | 159 } |
| 157 #endif | 160 #endif |
| 158 | 161 |
| 159 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is au
to-generated | 162 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is |
| 160 // by make_private_script_source.py. | 163 // auto-generated by make_private_script_source.py. |
| 161 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); | 164 for (size_t index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); |
| 162 index++) { | 165 index++) { |
| 163 if (className == kPrivateScriptSources[index].className) { | 166 if (className == kPrivateScriptSources[index].className) { |
| 164 String resourceData = | 167 String resourceData = |
| 165 loadResourceAsASCIIString(kPrivateScriptSources[index].resourceFile); | 168 loadResourceAsASCIIString(kPrivateScriptSources[index].resourceFile); |
| 166 compileAndRunPrivateScript( | 169 compileAndRunPrivateScript( |
| 167 scriptState, kPrivateScriptSources[index].scriptClassName, | 170 scriptState, kPrivateScriptSources[index].scriptClassName, |
| 168 resourceData.utf8().data(), resourceData.length()); | 171 resourceData.utf8().data(), resourceData.length()); |
| 169 compiledScriptCount++; | 172 compiledScriptCount++; |
| 170 } | 173 } |
| 171 } | 174 } |
| 172 | 175 |
| 173 if (!compiledScriptCount) { | 176 if (!compiledScriptCount) { |
| 174 fprintf(stderr, | 177 fprintf(stderr, |
| 175 "Private script error: Target source code was not found. (Class " | 178 "Private script error: Target source code was not found. (Class " |
| 176 "name = %s)\n", | 179 "name = %s)\n", |
| 177 className.utf8().data()); | 180 className.utf8().data()); |
| 178 RELEASE_NOTREACHED(); | 181 RELEASE_NOTREACHED(); |
| 179 } | 182 } |
| 180 } | 183 } |
| 181 | 184 |
| 182 static v8::Local<v8::Value> installPrivateScriptRunner(v8::Isolate* isolate) { | 185 static v8::Local<v8::Value> installPrivateScriptRunner(v8::Isolate* isolate) { |
| 183 const String className = "PrivateScriptRunner"; | 186 const String className = "PrivateScriptRunner"; |
| 184 size_t index; | 187 size_t index; |
| 185 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is au
to-generated | 188 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is |
| 186 // by make_private_script_source.py. | 189 // auto-generated by make_private_script_source.py. |
| 187 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { | 190 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { |
| 188 if (className == kPrivateScriptSources[index].className) | 191 if (className == kPrivateScriptSources[index].className) |
| 189 break; | 192 break; |
| 190 } | 193 } |
| 191 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) { | 194 if (index == WTF_ARRAY_LENGTH(kPrivateScriptSources)) { |
| 192 fprintf(stderr, | 195 fprintf(stderr, |
| 193 "Private script error: Target source code was not found. (Class " | 196 "Private script error: Target source code was not found. (Class " |
| 194 "name = %s)\n", | 197 "name = %s)\n", |
| 195 className.utf8().data()); | 198 className.utf8().data()); |
| 196 RELEASE_NOTREACHED(); | 199 RELEASE_NOTREACHED(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 v8::Local<v8::Function>::Cast(initializeFunction), holder, 0, 0, | 261 v8::Local<v8::Function>::Cast(initializeFunction), holder, 0, 0, |
| 259 isolate) | 262 isolate) |
| 260 .ToLocal(&result)) { | 263 .ToLocal(&result)) { |
| 261 fprintf(stderr, | 264 fprintf(stderr, |
| 262 "Private script error: Object constructor threw an exception.\n"); | 265 "Private script error: Object constructor threw an exception.\n"); |
| 263 dumpV8Message(context, block.Message()); | 266 dumpV8Message(context, block.Message()); |
| 264 RELEASE_NOTREACHED(); | 267 RELEASE_NOTREACHED(); |
| 265 } | 268 } |
| 266 } | 269 } |
| 267 | 270 |
| 268 // Inject the prototype object of the private script into the prototype chain
of the holder object. | 271 // Inject the prototype object of the private script into the prototype chain |
| 269 // This is necessary to let the holder object use properties defined on the pr
ototype object | 272 // of the holder object. This is necessary to let the holder object use |
| 270 // of the private script. (e.g., if the prototype object has |foo|, the holder
object should be able | 273 // properties defined on the prototype object of the private script. (e.g., if |
| 271 // to use it with |this.foo|.) | 274 // the prototype object has |foo|, the holder object should be able to use it |
| 275 // with |this.foo|.) |
| 272 if (classObject->GetPrototype() != holderObject->GetPrototype()) { | 276 if (classObject->GetPrototype() != holderObject->GetPrototype()) { |
| 273 if (!v8CallBoolean( | 277 if (!v8CallBoolean( |
| 274 classObject->SetPrototype(context, holderObject->GetPrototype()))) { | 278 classObject->SetPrototype(context, holderObject->GetPrototype()))) { |
| 275 fprintf(stderr, "Private script error: SetPrototype failed.\n"); | 279 fprintf(stderr, "Private script error: SetPrototype failed.\n"); |
| 276 dumpV8Message(context, block.Message()); | 280 dumpV8Message(context, block.Message()); |
| 277 RELEASE_NOTREACHED(); | 281 RELEASE_NOTREACHED(); |
| 278 } | 282 } |
| 279 } | 283 } |
| 280 if (!v8CallBoolean(holderObject->SetPrototype(context, classObject))) { | 284 if (!v8CallBoolean(holderObject->SetPrototype(context, classObject))) { |
| 281 fprintf(stderr, "Private script error: SetPrototype failed.\n"); | 285 fprintf(stderr, "Private script error: SetPrototype failed.\n"); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 rethrowExceptionInPrivateScript( | 499 rethrowExceptionInPrivateScript( |
| 496 scriptState->isolate(), block, scriptStateInUserScript, | 500 scriptState->isolate(), block, scriptStateInUserScript, |
| 497 ExceptionState::ExecutionContext, methodName, className); | 501 ExceptionState::ExecutionContext, methodName, className); |
| 498 block.ReThrow(); | 502 block.ReThrow(); |
| 499 return v8::Local<v8::Value>(); | 503 return v8::Local<v8::Value>(); |
| 500 } | 504 } |
| 501 return result; | 505 return result; |
| 502 } | 506 } |
| 503 | 507 |
| 504 } // namespace blink | 508 } // namespace blink |
| OLD | NEW |