Skip to content

Commit

Permalink
Revert "Remove AMD-style module support from gin/."
Browse files Browse the repository at this point in the history
This reverts commit c1ab49a.

Reason for revert: Likely the cause of
RunnerTest.Run failure in gin_unittests: Check failed: (isolate->snapshot_blob()) == nullptr.

Original change's description:
> Remove AMD-style module support from gin/.
> 
> Bug: 718047
> Change-Id: If480fff65a2e5993ed5143eede8354f890de3a3b
> Reviewed-on: https://chromium-review.googlesource.com/812172
> Commit-Queue: Yuzhu Shen <yzshen@chromium.org>
> Reviewed-by: Jochen Eisinger <jochen@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#522850}

TBR=yzshen@chromium.org,jochen@chromium.org

Change-Id: I4a6efb21dd7972994a06ddab1316fa43e5d3e060
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 718047, 793480
Reviewed-on: https://chromium-review.googlesource.com/817373
Reviewed-by: Yuzhu Shen <yzshen@chromium.org>
Commit-Queue: Yuzhu Shen <yzshen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522921}
  • Loading branch information
yzshen authored and Commit Bot committed Dec 8, 2017
1 parent 33286c6 commit a8f25f1
Show file tree
Hide file tree
Showing 33 changed files with 2,064 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "content/shell/test_runner/web_test_runner.h"
#include "content/shell/test_runner/web_view_test_proxy.h"
#include "content/test/mock_webclipboard_impl.h"
#include "gin/modules/module_registry.h"
#include "media/base/audio_latency.h"
#include "media/base/mime_util.h"
#include "media/media_features.h"
Expand Down
26 changes: 26 additions & 0 deletions gin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ component("gin") {
"isolate_holder.cc",
"modules/console.cc",
"modules/console.h",
"modules/file_module_provider.cc",
"modules/file_module_provider.h",
"modules/module_registry.cc",
"modules/module_registry.h",
"modules/module_registry_observer.h",
"modules/module_runner_delegate.cc",
"modules/module_runner_delegate.h",
"modules/timer.cc",
"modules/timer.h",
"object_template_builder.cc",
"object_template_builder.h",
"per_context_data.cc",
Expand Down Expand Up @@ -122,9 +131,20 @@ executable("gin_shell") {
source_set("gin_test") {
testonly = true
sources = [
"test/file.cc",
"test/file.h",
"test/file_runner.cc",
"test/file_runner.h",
"test/gc.cc",
"test/gc.h",
"test/gtest.cc",
"test/gtest.h",
"test/v8_test.cc",
"test/v8_test.h",
]
data = [
"test/expect.js",
]

public_deps = [
":gin",
Expand All @@ -149,10 +169,13 @@ test("gin_unittests") {
"converter_unittest.cc",
"data_object_builder_unittest.cc",
"interceptor_unittest.cc",
"modules/module_registry_unittest.cc",
"modules/timer_unittest.cc",
"per_context_data_unittest.cc",
"shell/gin_shell_unittest.cc",
"shell_runner_unittest.cc",
"test/run_all_unittests.cc",
"test/run_js_tests.cc",
"v8_isolate_memory_dump_provider_unittest.cc",
"v8_platform_unittest.cc",
"wrappable_unittest.cc",
Expand All @@ -168,7 +191,10 @@ test("gin_unittests") {
configs += [ "//v8:external_startup_data" ]

data = [
"modules/module_registry_unittests.js",
"shell/hello_world.js",
"test/file_unittests.js",
"test/gtest_unittests.js",
"../OWNERS",
]

Expand Down
32 changes: 21 additions & 11 deletions gin/modules/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,41 @@
#include "base/strings/string_util.h"
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
#include "gin/public/wrapper_info.h"

using v8::ObjectTemplate;

namespace gin {

namespace {

void Log(const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
void Log(Arguments* args) {
std::vector<std::string> messages;
if (!args.GetRemaining(&messages)) {
args.ThrowError();
if (!args->GetRemaining(&messages)) {
args->ThrowError();
return;
}
printf("%s\n", base::JoinString(messages, " ").c_str());
}

WrapperInfo g_wrapper_info = { kEmbedderNativeGin };

} // namespace

// static
void Console::Register(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> templ) {
v8::Local<v8::FunctionTemplate> log_templ =
v8::FunctionTemplate::New(isolate, Log);
log_templ->RemovePrototype();
const char Console::kModuleName[] = "console";

templ->Set(StringToSymbol(isolate, "log"), log_templ);
v8::Local<v8::Value> Console::GetModule(v8::Isolate* isolate) {
PerIsolateData* data = PerIsolateData::From(isolate);
v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info);
if (templ.IsEmpty()) {
templ = ObjectTemplateBuilder(isolate)
.SetMethod("log", Log)
.Build();
data->SetObjectTemplate(&g_wrapper_info, templ);
}
return templ->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
}

} // namespace gin
4 changes: 2 additions & 2 deletions gin/modules/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace gin {
// we'd like to evolve the API to match window.console in browsers.
class GIN_EXPORT Console {
public:
static void Register(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> templ);
static const char kModuleName[];
static v8::Local<v8::Value> GetModule(v8::Isolate* isolate);
};

} // namespace gin
Expand Down
74 changes: 74 additions & 0 deletions gin/modules/file_module_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gin/modules/file_module_provider.h"

#include <stddef.h>

#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_split.h"
#include "base/threading/thread_task_runner_handle.h"
#include "gin/converter.h"

namespace gin {

namespace {

void AttempToLoadModule(const base::WeakPtr<Runner>& runner,
const std::vector<base::FilePath>& search_paths,
const std::string& id) {
if (!runner)
return;

std::vector<std::string> components = base::SplitString(
id, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);

base::FilePath path;
for (size_t i = 0; i < components.size(); ++i) {
// TODO(abarth): Technically the path components can be UTF-8. We don't
// handle that case correctly yet.
path = path.AppendASCII(components[i]);
}
path = path.AddExtension(FILE_PATH_LITERAL("js"));

for (size_t i = 0; i < search_paths.size(); ++i) {
std::string source;
if (!ReadFileToString(search_paths[i].Append(path), &source))
continue;

Runner::Scope scope(runner.get());
runner->Run(source, id);
return;
}
LOG(ERROR) << "Failed to load module from disk: " << id;
}

} // namespace

FileModuleProvider::FileModuleProvider(
const std::vector<base::FilePath>& search_paths)
: search_paths_(search_paths) {
}

FileModuleProvider::~FileModuleProvider() = default;

void FileModuleProvider::AttempToLoadModules(
Runner* runner, const std::set<std::string>& ids) {
std::set<std::string> modules = ids;
for (std::set<std::string>::const_iterator it = modules.begin();
it != modules.end(); ++it) {
const std::string& id = *it;
if (attempted_ids_.count(id))
continue;
attempted_ids_.insert(id);
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(AttempToLoadModule, runner->GetWeakPtr(),
search_paths_, id));
}
}

} // namespace gin
45 changes: 45 additions & 0 deletions gin/modules/file_module_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GIN_MODULES_FILE_MODULE_PROVIDER_H_
#define GIN_MODULES_FILE_MODULE_PROVIDER_H_

#include <set>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "gin/gin_export.h"
#include "gin/runner.h"

namespace gin {

// FileModuleProvider knows how to load AMD modules off disk. It searches for
// modules in the directories indiciated by |search_paths|. Although we still
// read from the file system on the main thread, we'll eventually want to move
// the reads to a background thread.
class GIN_EXPORT FileModuleProvider {
public:
explicit FileModuleProvider(
const std::vector<base::FilePath>& search_paths);
~FileModuleProvider();

// Searches for modules with |ids| in the file system. If found, the modules
// will be executed asynchronously by |runner|.
void AttempToLoadModules(Runner* runner, const std::set<std::string>& ids);

private:
std::vector<base::FilePath> search_paths_;

// We'll only search for a given module once. We remember the set of modules
// we've already looked for in |attempted_ids_|.
std::set<std::string> attempted_ids_;

DISALLOW_COPY_AND_ASSIGN(FileModuleProvider);
};

} // namespace gin

#endif // GIN_MODULES_FILE_MODULE_PROVIDER_H_
Loading

0 comments on commit a8f25f1

Please sign in to comment.