From 6beb7a69a1ec3630de41b9010b7282c7a2a75693 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 11 Aug 2015 00:04:01 +0200 Subject: [PATCH] Provide global function loadScript to load external file. --- libjsengine/JSEngine.h | 2 +- libjsengine/JSV8Engine.cpp | 46 +++++++++++++++++++++++++++++++++----- libjsengine/JSV8Engine.h | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/libjsengine/JSEngine.h b/libjsengine/JSEngine.h index adebfdd13..961026b60 100644 --- a/libjsengine/JSEngine.h +++ b/libjsengine/JSEngine.h @@ -59,7 +59,7 @@ class JSEngine { public: // should be used to evalute javascript expression - virtual T eval(char const* _cstr) const = 0; + virtual T eval(char const* _cstr, char const* _origin = "(shell)") const = 0; }; } diff --git a/libjsengine/JSV8Engine.cpp b/libjsengine/JSV8Engine.cpp index 1a75a4620..471ea71e0 100644 --- a/libjsengine/JSV8Engine.cpp +++ b/libjsengine/JSV8Engine.cpp @@ -21,6 +21,8 @@ */ #include +#include +#include #include "JSV8Engine.h" #include "JSV8Printer.h" #include "libjsengine/JSEngineResources.hpp" @@ -103,6 +105,35 @@ Handle ConsoleLog(Arguments const& _args) return Undefined(); } +Handle LoadScript(Arguments const& _args) +{ + Local wrap = Local::Cast(_args.Data()); + auto engine = reinterpret_cast(wrap->Value()); + + if (_args.Length() < 1) + return v8::ThrowException(v8::String::New("Missing file name.")); + if (_args[0].IsEmpty() || _args[0]->IsUndefined()) + return v8::ThrowException(v8::String::New("Invalid file name.")); + v8::String::Utf8Value fileName(_args[0]); + if (fileName.length() == 0) + return v8::ThrowException(v8::String::New("Invalid file name.")); + + std::ifstream is(*fileName, std::ifstream::binary); + if (!is) + return v8::ThrowException(v8::String::New("Error opening file.")); + + string contents; + is.seekg(0, is.end); + streamoff length = is.tellg(); + if (length > 0) + { + is.seekg(0, is.beg); + contents.resize(length); + is.read(const_cast(contents.data()), length); + } + + return engine->eval(contents.data(), *fileName).value(); +} class JSV8Scope { @@ -158,11 +189,14 @@ JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope()) auto consoleTemplate = ObjectTemplate::New(); - Local function = FunctionTemplate::New(ConsoleLog, External::New(this)); - consoleTemplate->Set(String::New("debug"), function); - consoleTemplate->Set(String::New("log"), function); - consoleTemplate->Set(String::New("error"), function); + Local consoleLog = FunctionTemplate::New(ConsoleLog, External::New(this)); + consoleTemplate->Set(String::New("debug"), consoleLog); + consoleTemplate->Set(String::New("log"), consoleLog); + consoleTemplate->Set(String::New("error"), consoleLog); context()->Global()->Set(String::New("console"), consoleTemplate->NewInstance()); + + Local loadScript = FunctionTemplate::New(LoadScript, External::New(this)); + context()->Global()->Set(String::New("loadScript"), loadScript->GetFunction()); } JSV8Engine::~JSV8Engine() @@ -170,11 +204,11 @@ JSV8Engine::~JSV8Engine() delete m_scope; } -JSV8Value JSV8Engine::eval(char const* _cstr) const +JSV8Value JSV8Engine::eval(char const* _cstr, char const* _origin) const { TryCatch tryCatch; Local source = String::New(_cstr); - Local name(String::New("(shell)")); + Local name(String::New(_origin)); ScriptOrigin origin(name); Handle