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

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

Issue 757963002: [Blink-in-JS] 'Import' function to load sub-modules and other resources for private scripts (Part-3) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years 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
« no previous file with comments | « no previous file | Source/core/xml/A.js » ('j') | Source/core/xml/DocumentXMLTreeViewer.js » ('J')
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"
(...skipping 22 matching lines...) Expand all
33 message->GetLineNumber(); 33 message->GetLineNumber();
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 static void includeFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
43 44
44 static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, St ring scriptClassName, const char* source, size_t size) 45 static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, St ring scriptClassName, const char* source, size_t size)
45 { 46 {
46 v8::TryCatch block; 47 v8::TryCatch block;
47 String sourceString(source, size); 48 String sourceString(source, size);
48 String fileName = scriptClassName + ".js"; 49 String fileName = scriptClassName + ".js";
50
51 v8::Handle<v8::Context> context = isolate->GetCurrentContext();
52 v8::Handle<v8::Object> global = context->Global();
53 v8::Handle<v8::Value> includeFunctionObject = global->Get(v8String(isolate, "include"));
54 if (isUndefinedOrNull(includeFunctionObject))
55 global->Set(v8String(isolate, "include"), v8::FunctionTemplate::New(isol ate, includeFunction)->GetFunction());
56
49 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isola te, sourceString), fileName, TextPosition::minimumPosition(), 0, 0, isolate, Not SharableCrossOrigin); 57 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isola te, sourceString), fileName, TextPosition::minimumPosition(), 0, 0, isolate, Not SharableCrossOrigin);
50 if (block.HasCaught()) { 58 if (block.HasCaught()) {
51 fprintf(stderr, "Private script error: Compile failed. (Class name = %s) \n", scriptClassName.utf8().data()); 59 fprintf(stderr, "Private script error: Compile failed. (Class name = %s) \n", scriptClassName.utf8().data());
52 dumpV8Message(block.Message()); 60 dumpV8Message(block.Message());
53 RELEASE_ASSERT_NOT_REACHED(); 61 RELEASE_ASSERT_NOT_REACHED();
54 } 62 }
55 63
56 v8::Handle<v8::Value> result = V8ScriptRunner::runCompiledInternalScript(iso late, script); 64 v8::Handle<v8::Value> result = V8ScriptRunner::runCompiledInternalScript(iso late, script);
57 if (block.HasCaught()) { 65 if (block.HasCaught()) {
58 fprintf(stderr, "Private script error: installClass() failed. (Class nam e = %s)\n", scriptClassName.utf8().data()); 66 fprintf(stderr, "Private script error: installClass() failed. (Class nam e = %s)\n", scriptClassName.utf8().data());
59 dumpV8Message(block.Message()); 67 dumpV8Message(block.Message());
60 RELEASE_ASSERT_NOT_REACHED(); 68 RELEASE_ASSERT_NOT_REACHED();
61 } 69 }
62 return result; 70 return result;
63 } 71 }
64 72
73 void includeFunction(const v8::FunctionCallbackInfo<v8::Value>& args)
74 {
75 RELEASE_ASSERT(args.Length() == 1);
76 String scriptFileName = toCoreString(args[0]->ToString());
77 String resourceData = loadResourceAsASCIIString(scriptFileName.utf8().data() );
78 compileAndRunPrivateScript(args.GetIsolate(), scriptFileName, resourceData.u tf8().data(), resourceData.length());
vivekg 2014/11/25 07:12:32 Calling the compileAndRunPrivateScript enables the
79 }
80
65 // FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the JS files 81 // 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 82 // 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 83 // 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. 84 // 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) 85 static void installPrivateScript(v8::Isolate* isolate, String className)
70 { 86 {
71 int compiledScriptCount = 0; 87 int compiledScriptCount = 0;
72 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, which is auto-generated 88 // |kPrivateScriptSourcesForTesting| is defined in V8PrivateScriptSources.h, which is auto-generated
73 // by make_private_script_source.py. 89 // by make_private_script_source.py.
74 #ifndef NDEBUG 90 #ifndef NDEBUG
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 v8::Handle<v8::Value> result = V8ScriptRunner::callFunction(v8::Handle<v8::F unction>::Cast(method), scriptState->executionContext(), holder, argc, argv, scr iptState->isolate()); 310 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()) { 311 if (block.HasCaught()) {
296 rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptSta teInUserScript, ExceptionState::ExecutionContext, methodName, className); 312 rethrowExceptionInPrivateScript(scriptState->isolate(), block, scriptSta teInUserScript, ExceptionState::ExecutionContext, methodName, className);
297 block.ReThrow(); 313 block.ReThrow();
298 return v8::Handle<v8::Value>(); 314 return v8::Handle<v8::Value>();
299 } 315 }
300 return result; 316 return result;
301 } 317 }
302 318
303 } // namespace blink 319 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | Source/core/xml/A.js » ('j') | Source/core/xml/DocumentXMLTreeViewer.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698