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

Side by Side Diff: src/extensions/run-extension.cc

Issue 6520017: mimic the jsc shell function run() for latest sunspider (Closed)
Patch Set: Add run() as an extension Created 9 years, 8 months 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 unified diff | Download patch
« no previous file with comments | « src/extensions/run-extension.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "run-extension.h"
29
30 namespace v8 {
31 namespace internal {
32
33 const char* const RunExtension::kSource = "native function run();";
34
35 class StopWatch {
36 public:
37 StopWatch() : start_(0), stop_(0) {};
38 void start() { start_ = OS::TimeCurrentMillis(); }
39 void stop() { stop_ = OS::TimeCurrentMillis(); }
40 uint32_t ElapsedMillis() { return static_cast<int32_t>(stop_ - start_); }
41 private:
42 double start_;
43 double stop_;
44 };
45
46
47 v8::Handle<v8::FunctionTemplate> RunExtension::GetNativeFunction(
48 v8::Handle<v8::String> str) {
49 return v8::FunctionTemplate::New(RunExtension::Run);
50 }
51
52
53 bool RunExtension::ExecuteString(v8::Handle<v8::String> source,
54 v8::Handle<v8::Value> name) {
55 v8::HandleScope handle_scope;
56 v8::TryCatch try_catch;
57 v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
58 if (script.IsEmpty()) {
59 return false;
60 } else {
61 v8::Handle<Value> result = script->Run();
62 if (result.IsEmpty()) {
63 ASSERT(try_catch.HasCaught());
64 return false;
65 } else {
66 ASSERT(!try_catch.HasCaught());
67 return true;
68 }
69 }
70 }
71
72
73 v8::Handle<v8::String> RunExtension::ReadFile(const char* name) {
74 FILE* file = fopen(name, "rb");
75 if (file == NULL) return v8::Handle<v8::String>();
76
77 fseek(file, 0, SEEK_END);
78 int size = ftell(file);
79 rewind(file);
80
81 char* chars = new char[size + 1];
82 chars[size] = '\0';
83 for (int i = 0; i < size;) {
84 int read = fread(&chars[i], 1, size - i, file);
85 i += read;
86 }
87 fclose(file);
88 v8::Handle<v8::String> result = v8::String::New(chars, size);
89 delete[] chars;
90 return result;
91 }
92
93
94 static inline v8::Persistent<v8::Context> CreateShellContext() {
95 // Create a template for the global object.
96 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
97 return v8::Context::New(NULL, global);
98 }
99
100
101 v8::Handle<v8::Value> RunExtension::Run(const v8::Arguments& args) {
102 StopWatch watch;
103 watch.start();
104 v8::Persistent<v8::Context> context = CreateShellContext();
105 v8::Context::Scope context_scope(context);
106 for (int i = 0; i < args.Length(); i++) {
107 v8::HandleScope handle_scope;
108 v8::String::Utf8Value file(args[0]); // we only eval the first argument
109 if (*file == NULL) {
110 return ThrowException(v8::String::New("Error loading file"));
111 }
112 v8::Handle<v8::String> source = ReadFile(*file);
113 if (source.IsEmpty()) {
114 return ThrowException(v8::String::New("Error loading file"));
115 }
116 if (!ExecuteString(source, v8::String::New(*file))) {
117 return ThrowException(v8::String::New("Error executing file"));
118 }
119
120 }
121 context.Dispose();
122 watch.stop();
123 return v8::Integer::New(watch.ElapsedMillis());
124 }
125
126
127 void RunExtension::Register() {
128 static RunExtension run_extension;
129 static v8::DeclareExtension run_extension_declaration(&run_extension);
130 }
131
132 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/extensions/run-extension.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698