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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/extensions/run-extension.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/extensions/run-extension.cc
diff --git a/src/extensions/run-extension.cc b/src/extensions/run-extension.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dbfce75c8e0b1f3f003b2e8bd8854102e7f53608
--- /dev/null
+++ b/src/extensions/run-extension.cc
@@ -0,0 +1,132 @@
+// Copyright 2010 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "run-extension.h"
+
+namespace v8 {
+namespace internal {
+
+const char* const RunExtension::kSource = "native function run();";
+
+class StopWatch {
+public:
+ StopWatch() : start_(0), stop_(0) {};
+ void start() { start_ = OS::TimeCurrentMillis(); }
+ void stop() { stop_ = OS::TimeCurrentMillis(); }
+ uint32_t ElapsedMillis() { return static_cast<int32_t>(stop_ - start_); }
+private:
+ double start_;
+ double stop_;
+};
+
+
+v8::Handle<v8::FunctionTemplate> RunExtension::GetNativeFunction(
+ v8::Handle<v8::String> str) {
+ return v8::FunctionTemplate::New(RunExtension::Run);
+}
+
+
+bool RunExtension::ExecuteString(v8::Handle<v8::String> source,
+ v8::Handle<v8::Value> name) {
+ v8::HandleScope handle_scope;
+ v8::TryCatch try_catch;
+ v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
+ if (script.IsEmpty()) {
+ return false;
+ } else {
+ v8::Handle<Value> result = script->Run();
+ if (result.IsEmpty()) {
+ ASSERT(try_catch.HasCaught());
+ return false;
+ } else {
+ ASSERT(!try_catch.HasCaught());
+ return true;
+ }
+ }
+}
+
+
+v8::Handle<v8::String> RunExtension::ReadFile(const char* name) {
+ FILE* file = fopen(name, "rb");
+ if (file == NULL) return v8::Handle<v8::String>();
+
+ fseek(file, 0, SEEK_END);
+ int size = ftell(file);
+ rewind(file);
+
+ char* chars = new char[size + 1];
+ chars[size] = '\0';
+ for (int i = 0; i < size;) {
+ int read = fread(&chars[i], 1, size - i, file);
+ i += read;
+ }
+ fclose(file);
+ v8::Handle<v8::String> result = v8::String::New(chars, size);
+ delete[] chars;
+ return result;
+}
+
+
+static inline v8::Persistent<v8::Context> CreateShellContext() {
+ // Create a template for the global object.
+ v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
+ return v8::Context::New(NULL, global);
+}
+
+
+v8::Handle<v8::Value> RunExtension::Run(const v8::Arguments& args) {
+ StopWatch watch;
+ watch.start();
+ v8::Persistent<v8::Context> context = CreateShellContext();
+ v8::Context::Scope context_scope(context);
+ for (int i = 0; i < args.Length(); i++) {
+ v8::HandleScope handle_scope;
+ v8::String::Utf8Value file(args[0]); // we only eval the first argument
+ if (*file == NULL) {
+ return ThrowException(v8::String::New("Error loading file"));
+ }
+ v8::Handle<v8::String> source = ReadFile(*file);
+ if (source.IsEmpty()) {
+ return ThrowException(v8::String::New("Error loading file"));
+ }
+ if (!ExecuteString(source, v8::String::New(*file))) {
+ return ThrowException(v8::String::New("Error executing file"));
+ }
+
+ }
+ context.Dispose();
+ watch.stop();
+ return v8::Integer::New(watch.ElapsedMillis());
+}
+
+
+void RunExtension::Register() {
+ static RunExtension run_extension;
+ static v8::DeclareExtension run_extension_declaration(&run_extension);
+}
+
+} } // namespace v8::internal
« 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