Execute distiller in isolated world
[chromium-blink-merge.git] / gin / shell / gin_main.cc
blob0b461eed2ccf6ede4d223312b2bd3d31581dc725
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/at_exit.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/i18n/icu_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "gin/array_buffer.h"
12 #include "gin/modules/console.h"
13 #include "gin/modules/module_runner_delegate.h"
14 #include "gin/public/isolate_holder.h"
15 #include "gin/try_catch.h"
16 #include "gin/v8_initializer.h"
18 namespace gin {
19 namespace {
21 std::string Load(const base::FilePath& path) {
22 std::string source;
23 if (!ReadFileToString(path, &source))
24 LOG(FATAL) << "Unable to read " << path.LossyDisplayName();
25 return source;
28 void Run(base::WeakPtr<Runner> runner, const base::FilePath& path) {
29 if (!runner)
30 return;
31 Runner::Scope scope(runner.get());
32 runner->Run(Load(path), path.AsUTF8Unsafe());
35 std::vector<base::FilePath> GetModuleSearchPaths() {
36 std::vector<base::FilePath> module_base(1);
37 CHECK(base::GetCurrentDirectory(&module_base[0]));
38 return module_base;
41 class GinShellRunnerDelegate : public ModuleRunnerDelegate {
42 public:
43 GinShellRunnerDelegate() : ModuleRunnerDelegate(GetModuleSearchPaths()) {
44 AddBuiltinModule(Console::kModuleName, Console::GetModule);
47 void UnhandledException(ShellRunner* runner, TryCatch& try_catch) override {
48 ModuleRunnerDelegate::UnhandledException(runner, try_catch);
49 LOG(ERROR) << try_catch.GetStackTrace();
52 private:
53 DISALLOW_COPY_AND_ASSIGN(GinShellRunnerDelegate);
56 } // namespace
57 } // namespace gin
59 int main(int argc, char** argv) {
60 base::AtExitManager at_exit;
61 base::CommandLine::Init(argc, argv);
62 base::i18n::InitializeICU();
63 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
64 gin::V8Initializer::LoadV8Snapshot();
65 gin::V8Initializer::LoadV8Natives();
66 #endif
68 base::MessageLoop message_loop;
70 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
71 gin::ArrayBufferAllocator::SharedInstance());
72 gin::IsolateHolder instance;
75 gin::GinShellRunnerDelegate delegate;
76 gin::ShellRunner runner(&delegate, instance.isolate());
79 gin::Runner::Scope scope(&runner);
80 runner.GetContextHolder()
81 ->isolate()
82 ->SetCaptureStackTraceForUncaughtExceptions(true);
85 base::CommandLine::StringVector args =
86 base::CommandLine::ForCurrentProcess()->GetArgs();
87 for (base::CommandLine::StringVector::const_iterator it = args.begin();
88 it != args.end(); ++it) {
89 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
90 gin::Run, runner.GetWeakPtr(), base::FilePath(*it)));
93 message_loop.RunUntilIdle();
94 return 0;