| 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
|
|
|