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" |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 v8::Handle<v8::Value> resourceName = message->GetScriptOrigin().ResourceName
(); | 35 v8::Handle<v8::Value> resourceName = message->GetScriptOrigin().ResourceName
(); |
36 String fileName = "Unknown JavaScript file"; | 36 String fileName = "Unknown JavaScript file"; |
37 if (!resourceName.IsEmpty() && resourceName->IsString()) | 37 if (!resourceName.IsEmpty() && resourceName->IsString()) |
38 fileName = toCoreString(v8::Handle<v8::String>::Cast(resourceName)); | 38 fileName = toCoreString(v8::Handle<v8::String>::Cast(resourceName)); |
39 int lineNumber = message->GetLineNumber(); | 39 int lineNumber = message->GetLineNumber(); |
40 v8::Handle<v8::String> errorMessage = message->Get(); | 40 v8::Handle<v8::String> errorMessage = message->Get(); |
41 fprintf(stderr, "%s (line %d): %s\n", fileName.utf8().data(), lineNumber, to
CoreString(errorMessage).utf8().data()); | 41 fprintf(stderr, "%s (line %d): %s\n", fileName.utf8().data(), lineNumber, to
CoreString(errorMessage).utf8().data()); |
42 } | 42 } |
43 | 43 |
| 44 static void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 45 |
44 static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, St
ring scriptClassName, const char* source, size_t size) | 46 static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, St
ring scriptClassName, const char* source, size_t size) |
45 { | 47 { |
46 v8::TryCatch block; | 48 v8::TryCatch block; |
47 String sourceString(source, size); | 49 String sourceString(source, size); |
48 String fileName = scriptClassName + ".js"; | 50 String fileName = scriptClassName + ".js"; |
| 51 |
| 52 v8::Handle<v8::Object> global = isolate->GetCurrentContext()->Global(); |
| 53 v8::Handle<v8::Value> privateScriptController = global->Get(v8String(isolate
, "privateScriptController")); |
| 54 RELEASE_ASSERT(privateScriptController->IsUndefined() || privateScriptContro
ller->IsObject()); |
| 55 if (privateScriptController->IsObject()) { |
| 56 v8::Handle<v8::Object> privateScriptControllerObject = privateScriptCont
roller->ToObject(isolate); |
| 57 v8::Handle<v8::Value> importFunctionValue = privateScriptControllerObjec
t->Get(v8String(isolate, "import")); |
| 58 if (importFunctionValue->IsUndefined()) |
| 59 privateScriptControllerObject->Set(v8String(isolate, "import"), v8::
FunctionTemplate::New(isolate, importFunction)->GetFunction()); |
| 60 } |
| 61 |
49 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isola
te, sourceString), fileName, TextPosition::minimumPosition(), 0, 0, isolate, Not
SharableCrossOrigin); | 62 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isola
te, sourceString), fileName, TextPosition::minimumPosition(), 0, 0, isolate, Not
SharableCrossOrigin); |
50 if (block.HasCaught()) { | 63 if (block.HasCaught()) { |
51 fprintf(stderr, "Private script error: Compile failed. (Class name = %s)
\n", scriptClassName.utf8().data()); | 64 fprintf(stderr, "Private script error: Compile failed. (Class name = %s)
\n", scriptClassName.utf8().data()); |
52 dumpV8Message(block.Message()); | 65 dumpV8Message(block.Message()); |
53 RELEASE_ASSERT_NOT_REACHED(); | 66 RELEASE_ASSERT_NOT_REACHED(); |
54 } | 67 } |
55 | 68 |
56 v8::Handle<v8::Value> result = V8ScriptRunner::runCompiledInternalScript(iso
late, script); | 69 v8::Handle<v8::Value> result = V8ScriptRunner::runCompiledInternalScript(iso
late, script); |
57 if (block.HasCaught()) { | 70 if (block.HasCaught()) { |
58 fprintf(stderr, "Private script error: installClass() failed. (Class nam
e = %s)\n", scriptClassName.utf8().data()); | 71 fprintf(stderr, "Private script error: installClass() failed. (Class nam
e = %s)\n", scriptClassName.utf8().data()); |
59 dumpV8Message(block.Message()); | 72 dumpV8Message(block.Message()); |
60 RELEASE_ASSERT_NOT_REACHED(); | 73 RELEASE_ASSERT_NOT_REACHED(); |
61 } | 74 } |
62 return result; | 75 return result; |
63 } | 76 } |
64 | 77 |
| 78 void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args) |
| 79 { |
| 80 v8::Isolate* isolate = args.GetIsolate(); |
| 81 RELEASE_ASSERT(isolate && (args.Length() == 1)); |
| 82 String resourceFileName = toCoreString(args[0]->ToString()); |
| 83 String resourceData = loadResourceAsASCIIString(resourceFileName.utf8().data
()); |
| 84 RELEASE_ASSERT(resourceData.length()); |
| 85 if (resourceFileName.endsWith(".js")) |
| 86 compileAndRunPrivateScript(isolate, resourceFileName.replace(".js", ""),
resourceData.utf8().data(), resourceData.length()); |
| 87 else if (resourceFileName.endsWith(".css")) |
| 88 args.GetReturnValue().Set(v8String(isolate, resourceData)); |
| 89 } |
| 90 |
65 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the
JS files | 91 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the
JS files |
66 // are compiled when any of the JS files is requested. Ideally we should avoid c
ompiling | 92 // are compiled when any of the JS files is requested. Ideally we should avoid c
ompiling |
67 // unrelated JS files. For example, if a method in XPartial-1.js is requested, w
e just | 93 // unrelated JS files. For example, if a method in XPartial-1.js is requested, w
e just |
68 // need to compile X.js and XPartial-1.js, and don't need to compile XPartial-2.
js. | 94 // need to compile X.js and XPartial-1.js, and don't need to compile XPartial-2.
js. |
69 static void installPrivateScript(v8::Isolate* isolate, String className) | 95 static void installPrivateScript(v8::Isolate* isolate, String className) |
70 { | 96 { |
71 int compiledScriptCount = 0; | 97 int compiledScriptCount = 0; |
72 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h,
which is auto-generated | 98 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h,
which is auto-generated |
73 // by make_private_script_source.py. | 99 // by make_private_script_source.py. |
74 #ifndef NDEBUG | 100 #ifndef NDEBUG |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 v8::Handle<v8::Value> result = V8ScriptRunner::callFunction(v8::Handle<v8::F
unction>::Cast(method), scriptState->executionContext(), holder, argc, argv, scr
iptState->isolate()); | 320 v8::Handle<v8::Value> result = V8ScriptRunner::callFunction(v8::Handle<v8::F
unction>::Cast(method), scriptState->executionContext(), holder, argc, argv, scr
iptState->isolate()); |
295 if (block.HasCaught()) { | 321 if (block.HasCaught()) { |
296 rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptSta
teInUserScript, ExceptionState::ExecutionContext, methodName, className); | 322 rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptSta
teInUserScript, ExceptionState::ExecutionContext, methodName, className); |
297 block.ReThrow(); | 323 block.ReThrow(); |
298 return v8::Handle<v8::Value>(); | 324 return v8::Handle<v8::Value>(); |
299 } | 325 } |
300 return result; | 326 return result; |
301 } | 327 } |
302 | 328 |
303 } // namespace blink | 329 } // namespace blink |
OLD | NEW |