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

Unified 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: Implementing privateScriptController.import Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Source/core/xml/DocumentXMLTreeViewer.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/bindings/core/v8/PrivateScriptRunner.cpp
diff --git a/Source/bindings/core/v8/PrivateScriptRunner.cpp b/Source/bindings/core/v8/PrivateScriptRunner.cpp
index 9dcc4cc107717fb46fe5e48163afc4dbd84e41a1..adacb80f111d4ed67f311162c69352d58bf2220a 100644
--- a/Source/bindings/core/v8/PrivateScriptRunner.cpp
+++ b/Source/bindings/core/v8/PrivateScriptRunner.cpp
@@ -41,11 +41,24 @@ static void dumpV8Message(v8::Handle<v8::Message> message)
fprintf(stderr, "%s (line %d): %s\n", fileName.utf8().data(), lineNumber, toCoreString(errorMessage).utf8().data());
}
+static void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
+
static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, String scriptClassName, const char* source, size_t size)
{
v8::TryCatch block;
String sourceString(source, size);
String fileName = scriptClassName + ".js";
+
+ v8::Handle<v8::Object> global = isolate->GetCurrentContext()->Global();
+ v8::Handle<v8::Value> privateScriptController = global->Get(v8String(isolate, "privateScriptController"));
+ RELEASE_ASSERT(privateScriptController->IsUndefined() || privateScriptController->IsObject());
+ if (privateScriptController->IsObject()) {
+ v8::Handle<v8::Object> privateScriptControllerObject = privateScriptController->ToObject(isolate);
+ v8::Handle<v8::Value> importFunctionValue = privateScriptControllerObject->Get(v8String(isolate, "import"));
+ if (importFunctionValue->IsUndefined())
+ privateScriptControllerObject->Set(v8String(isolate, "import"), v8::FunctionTemplate::New(isolate, importFunction)->GetFunction());
+ }
+
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(v8String(isolate, sourceString), fileName, TextPosition::minimumPosition(), 0, 0, isolate, NotSharableCrossOrigin);
if (block.HasCaught()) {
fprintf(stderr, "Private script error: Compile failed. (Class name = %s)\n", scriptClassName.utf8().data());
@@ -62,6 +75,19 @@ static v8::Handle<v8::Value> compileAndRunPrivateScript(v8::Isolate* isolate, St
return result;
}
+void importFunction(const v8::FunctionCallbackInfo<v8::Value>& args)
+{
+ v8::Isolate* isolate = args.GetIsolate();
+ RELEASE_ASSERT(isolate && (args.Length() == 1));
+ String resourceFileName = toCoreString(args[0]->ToString());
+ String resourceData = loadResourceAsASCIIString(resourceFileName.utf8().data());
+ RELEASE_ASSERT(resourceData.length());
+ if (resourceFileName.endsWith(".js"))
+ compileAndRunPrivateScript(isolate, resourceFileName.replace(".js", ""), resourceData.utf8().data(), resourceData.length());
+ else if (resourceFileName.endsWith(".css"))
+ args.GetReturnValue().Set(v8String(isolate, resourceData));
+}
+
// FIXME: If we have X.js, XPartial-1.js and XPartial-2.js, currently all of the JS files
// are compiled when any of the JS files is requested. Ideally we should avoid compiling
// unrelated JS files. For example, if a method in XPartial-1.js is requested, we just
« no previous file with comments | « no previous file | Source/core/xml/DocumentXMLTreeViewer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698