Skip to content
Browse files

Add RunFileInScope API, catch script compilation errors from RunFile,…

… doc fix.
  • Loading branch information...
1 parent 1a154f2 commit 1652e15784d8783ca607b43ac3e5d5a0931f6364 jpike committed Jul 26, 2012
Showing with 19 additions and 6 deletions.
  1. +3 −1 README.markdown
  2. +10 −4 src/vu8/Context.cpp
  3. +6 −1 src/vu8/Context.hpp
View
4 README.markdown
@@ -2,9 +2,11 @@
vu8 is a project that allows one to give JavaScript access to C++ classes and methods. The binding library is a header only library that uses template-metaprogramming extensively to make binding as easy as possible for the library user.
+This library makes writing node.js modules that take advantage of C++ methods very easy but is also ideal for use in C++ systems where node.js is not required.
+
## vu8 also provides
-* libvu8.a - a static library to add "vu8.load" to the v8 JavaScript context. "vu8.load" is a system for loading plugins from shared libraries.
+* libvu8.so - a dynamic library to add "vu8.load" to the v8 JavaScript context. "vu8.load" is a system for loading plugins from shared libraries.
* v8 cmake - cmake modules to make it easy to build modules for use with "vu8.load". It is recommended that modules use the vu8 meta-programming library to bind C++ to JavaScript but this is not mandatory.
* vu8bin - A binary for running JavaScript files in a context which has vu8 module loading functions provided.
View
14 src/vu8/Context.cpp
@@ -87,9 +87,7 @@ v8::Handle<v8::Value> RunFile(const v8::Arguments& args) {
Context& context =
*reinterpret_cast<Context *>(v8::External::Unwrap(ctxtValue));
- context.RunFile(fileName.c_str());
-
- return v8::Undefined();
+ return context.RunFile(fileName.c_str()) ? v8::True() : v8::False();
}
namespace {
@@ -123,12 +121,16 @@ void Context::Release() {
context_ = v8::Persistent<v8::Context>();
}
-void Context::RunFile(char const *filename) {
+bool Context::RunFile(char const *filename) {
v8::HandleScope scope;
UnlockAtExit unlockCtxt(context_);
if (IsEmpty()) initContext(*this, context_);
else context_->Enter();
+ return RunFileInScope(filename);
+}
+
+bool Context::RunFileInScope(char const *filename) {
std::ifstream stream(filename);
if (! stream) {
std::string error = "could not locate file ";
@@ -148,7 +150,11 @@ void Context::RunFile(char const *filename) {
v8::Handle<v8::Script> script = v8::Script::Compile(
v8::String::New(scriptStream.str().c_str()), origin.get());
+
+ if (script.IsEmpty()) return false;
+
script->Run();
+ return true;
}
Context::Context(std::string const& libPath) : libPath_(libPath) {}
View
7 src/vu8/Context.hpp
@@ -23,7 +23,12 @@ struct Context {
typedef v8::Handle<v8::Value> (*ModuleLoadCallback)();
bool IsEmpty() const { return context_.IsEmpty(); }
- void RunFile(char const *filename);
+
+ // Returns false on failure, use v8::TryCatch around it to find out why.
+ bool RunFile(char const *filename);
+
+ // Same as RunFile but for use inside a pre-existing handle scope.
+ bool RunFileInScope(char const *filename);
void Set(char const *name, Module& module) {
context_->Global()->Set(v8::String::New(name), module.NewInstance());

0 comments on commit 1652e15

Please sign in to comment.
Something went wrong with that request. Please try again.