Browse Source

Merge pull request #2786 from chriseth/loadScript

Provide global function loadScript to load external file.
cl-refactor
Gav Wood 10 years ago
parent
commit
f2e15a4acf
  1. 2
      libjsengine/JSEngine.h
  2. 48
      libjsengine/JSV8Engine.cpp
  3. 2
      libjsengine/JSV8Engine.h

2
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;
};
}

48
libjsengine/JSV8Engine.cpp

@ -21,6 +21,8 @@
*/
#include <memory>
#include <iostream>
#include <fstream>
#include "JSV8Engine.h"
#include "JSV8Printer.h"
#include "libjsengine/JSEngineResources.hpp"
@ -93,7 +95,7 @@ void reportException(TryCatch* _tryCatch)
}
}
Handle<Value> ConsoleLog(Arguments const& _args)
Handle<Value> consoleLog(Arguments const& _args)
{
Local<External> wrap = Local<External>::Cast(_args.Data());
auto engine = reinterpret_cast<JSV8Engine const*>(wrap->Value());
@ -103,6 +105,35 @@ Handle<Value> ConsoleLog(Arguments const& _args)
return Undefined();
}
Handle<Value> loadScript(Arguments const& _args)
{
Local<External> wrap = Local<External>::Cast(_args.Data());
auto engine = reinterpret_cast<JSV8Engine const*>(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<char*>(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<FunctionTemplate> 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<FunctionTemplate> consoleLogFunction = FunctionTemplate::New(consoleLog, External::New(this));
consoleTemplate->Set(String::New("debug"), consoleLogFunction);
consoleTemplate->Set(String::New("log"), consoleLogFunction);
consoleTemplate->Set(String::New("error"), consoleLogFunction);
context()->Global()->Set(String::New("console"), consoleTemplate->NewInstance());
Local<FunctionTemplate> loadScriptFunction = FunctionTemplate::New(loadScript, External::New(this));
context()->Global()->Set(String::New("loadScript"), loadScriptFunction->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<String> source = String::New(_cstr);
Local<String> name(String::New("(shell)"));
Local<String> name(String::New(_origin));
ScriptOrigin origin(name);
Handle<Script> script = Script::Compile(source, &origin);

2
libjsengine/JSV8Engine.h

@ -54,7 +54,7 @@ class JSV8Engine: public JSEngine<JSV8Value>
public:
JSV8Engine();
virtual ~JSV8Engine();
JSV8Value eval(char const* _cstr) const;
JSV8Value eval(char const* _cstr, char const* _origin = "(shell)") const;
v8::Handle<v8::Context> const& context() const;
private:

Loading…
Cancel
Save