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"
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"
20 std::string
Load(const base::FilePath
& path
) {
22 if (!ReadFileToString(path
, &source
))
23 LOG(FATAL
) << "Unable to read " << path
.LossyDisplayName();
27 void Run(base::WeakPtr
<Runner
> runner
, const base::FilePath
& path
) {
30 Runner::Scope
scope(runner
.get());
31 runner
->Run(Load(path
), path
.AsUTF8Unsafe());
34 std::vector
<base::FilePath
> GetModuleSearchPaths() {
35 std::vector
<base::FilePath
> module_base(1);
36 CHECK(base::GetCurrentDirectory(&module_base
[0]));
40 class GinShellRunnerDelegate
: public ModuleRunnerDelegate
{
42 GinShellRunnerDelegate() : ModuleRunnerDelegate(GetModuleSearchPaths()) {
43 AddBuiltinModule(Console::kModuleName
, Console::GetModule
);
46 void UnhandledException(ShellRunner
* runner
, TryCatch
& try_catch
) override
{
47 ModuleRunnerDelegate::UnhandledException(runner
, try_catch
);
48 LOG(ERROR
) << try_catch
.GetStackTrace();
52 DISALLOW_COPY_AND_ASSIGN(GinShellRunnerDelegate
);
58 int main(int argc
, char** argv
) {
59 base::AtExitManager at_exit
;
60 base::CommandLine::Init(argc
, argv
);
61 base::i18n::InitializeICU();
62 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
63 gin::IsolateHolder::LoadV8Snapshot();
66 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode
,
67 gin::ArrayBufferAllocator::SharedInstance());
68 gin::IsolateHolder instance
;
70 base::MessageLoop message_loop
;
72 gin::GinShellRunnerDelegate delegate
;
73 gin::ShellRunner
runner(&delegate
, instance
.isolate());
76 gin::Runner::Scope
scope(&runner
);
77 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
80 base::CommandLine::StringVector args
=
81 base::CommandLine::ForCurrentProcess()->GetArgs();
82 for (base::CommandLine::StringVector::const_iterator it
= args
.begin();
83 it
!= args
.end(); ++it
) {
84 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(
85 gin::Run
, runner
.GetWeakPtr(), base::FilePath(*it
)));
88 message_loop
.RunUntilIdle();