Skip to content

Commit

Permalink
Set root path to reflect the current javascript file when using load().
Browse files Browse the repository at this point in the history
This helps us use consistent relative paths in library code.
  • Loading branch information
cscott committed Dec 8, 2011
1 parent 530e135 commit 7602942
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 14 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions v8-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand Down

0 comments on commit 7602942

Please sign in to comment.