[Telemetry]: Fix v8_object_stats metric to avoid having a '.' in trace names
[chromium-blink-merge.git] / gin / runner.cc
blobe8e4089d5fbc98df270179aa6e515c52add34a91
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 "gin/runner.h"
7 #include "gin/converter.h"
8 #include "gin/per_context_data.h"
9 #include "gin/try_catch.h"
11 using v8::Context;
12 using v8::HandleScope;
13 using v8::Isolate;
14 using v8::Object;
15 using v8::ObjectTemplate;
16 using v8::Script;
18 namespace gin {
20 RunnerDelegate::RunnerDelegate() {
23 RunnerDelegate::~RunnerDelegate() {
26 v8::Handle<ObjectTemplate> RunnerDelegate::GetGlobalTemplate(Runner* runner) {
27 return v8::Handle<ObjectTemplate>();
30 void RunnerDelegate::DidCreateContext(Runner* runner) {
33 void RunnerDelegate::WillRunScript(Runner* runner) {
36 void RunnerDelegate::DidRunScript(Runner* runner) {
39 void RunnerDelegate::UnhandledException(Runner* runner, TryCatch& try_catch) {
40 CHECK(false) << try_catch.GetStackTrace();
43 Runner::Runner(RunnerDelegate* delegate, Isolate* isolate)
44 : ContextHolder(isolate),
45 delegate_(delegate),
46 weak_factory_(this) {
47 v8::Isolate::Scope isolate_scope(isolate);
48 HandleScope handle_scope(isolate);
49 v8::Handle<v8::Context> context =
50 Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this));
52 SetContext(context);
53 PerContextData::From(context)->set_runner(this);
55 v8::Context::Scope scope(context);
56 delegate_->DidCreateContext(this);
59 Runner::~Runner() {
62 void Runner::Run(const std::string& source, const std::string& resource_name) {
63 TryCatch try_catch;
64 v8::Handle<Script> script = Script::New(StringToV8(isolate(), source),
65 StringToV8(isolate(), resource_name));
66 if (try_catch.HasCaught()) {
67 delegate_->UnhandledException(this, try_catch);
68 return;
71 Run(script);
74 void Runner::Run(v8::Handle<Script> script) {
75 TryCatch try_catch;
76 delegate_->WillRunScript(this);
78 script->Run();
80 delegate_->DidRunScript(this);
81 if (try_catch.HasCaught()) {
82 delegate_->UnhandledException(this, try_catch);
86 v8::Handle<v8::Value> Runner::Call(v8::Handle<v8::Function> function,
87 v8::Handle<v8::Value> receiver,
88 int argc,
89 v8::Handle<v8::Value> argv[]) {
90 TryCatch try_catch;
91 delegate_->WillRunScript(this);
93 v8::Handle<v8::Value> result = function->Call(receiver, argc, argv);
95 delegate_->DidRunScript(this);
96 if (try_catch.HasCaught()) {
97 delegate_->UnhandledException(this, try_catch);
100 return result;
103 Runner::Scope::Scope(Runner* runner)
104 : isolate_scope_(runner->isolate()),
105 handle_scope_(runner->isolate()),
106 scope_(runner->context()) {
109 Runner::Scope::~Scope() {
112 } // namespace gin