Permalink
Browse files

Set root path to reflect the current javascript file when using load().

This helps us use consistent relative paths in library code.
  • Loading branch information...
1 parent 530e135 commit 7602942b225df3bb66a79426a995b04b717d8008 @cscott cscott committed Dec 8, 2011
Showing with 25 additions and 3 deletions.
  1. +14 −0 utils.cpp
  2. +3 −0 utils.h
  3. +8 −3 v8-gl.cpp
View
@@ -32,6 +32,20 @@ namespace V8GLUtils {
delete[] tmp_js_path;
}
+ char *pushRootPath(char *new_path) {
+ char *old_path = root_path;
+ char *pch = strrchr(new_path, V8GLUtils::separator);
+ int last_index = pch ? (pch - new_path + 1) : 2;
+ root_path = new char[last_index + 1];
+ strncpy(root_path, pch ? new_path : "./", last_index);
+ root_path[last_index] = '\0';
+ return old_path;
+ }
+ void popRootPath(char *old_path) {
+ delete[] root_path;
+ root_path = old_path;
+ }
+
char* getRootPath(void) {
return V8GLUtils::root_path;
}
View
@@ -6,6 +6,9 @@ namespace V8GLUtils {
char* getRootPath(void);
char* getRealPath(char* file_path);
+
+ char *pushRootPath(char *new_path);
+ void popRootPath(char *old_path);
};
#endif
View
@@ -146,10 +146,15 @@ Handle<Value> load(const Arguments& args) {
//get argument
String::Utf8Value value0(args[i]);
char* arg0 = *value0;
- string str(V8GLUtils::getRealPath(arg0));
- if(!exec(str)) {
+ char* filepath = V8GLUtils::getRealPath(arg0);
+
+ char *old_path = V8GLUtils::pushRootPath(filepath);
+ bool success = exec(string(filepath));
+ V8GLUtils::popRootPath(old_path);
+
+ if(!success) {
fprintf(stderr, "Error reading '%s'.\n", arg0);
- return v8::Undefined();
+ return ThrowException(String::New("Failed to load script"));
}
}

0 comments on commit 7602942

Please sign in to comment.