Skip to content

Commit 7602942

Browse files
committed
Set root path to reflect the current javascript file when using load().
This helps us use consistent relative paths in library code.
1 parent 530e135 commit 7602942

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

utils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ namespace V8GLUtils {
3232
delete[] tmp_js_path;
3333
}
3434

35+
char *pushRootPath(char *new_path) {
36+
char *old_path = root_path;
37+
char *pch = strrchr(new_path, V8GLUtils::separator);
38+
int last_index = pch ? (pch - new_path + 1) : 2;
39+
root_path = new char[last_index + 1];
40+
strncpy(root_path, pch ? new_path : "./", last_index);
41+
root_path[last_index] = '\0';
42+
return old_path;
43+
}
44+
void popRootPath(char *old_path) {
45+
delete[] root_path;
46+
root_path = old_path;
47+
}
48+
3549
char* getRootPath(void) {
3650
return V8GLUtils::root_path;
3751
}

utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace V8GLUtils {
66

77
char* getRootPath(void);
88
char* getRealPath(char* file_path);
9+
10+
char *pushRootPath(char *new_path);
11+
void popRootPath(char *old_path);
912
};
1013

1114
#endif

v8-gl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,15 @@ Handle<Value> load(const Arguments& args) {
146146
//get argument
147147
String::Utf8Value value0(args[i]);
148148
char* arg0 = *value0;
149-
string str(V8GLUtils::getRealPath(arg0));
150-
if(!exec(str)) {
149+
char* filepath = V8GLUtils::getRealPath(arg0);
150+
151+
char *old_path = V8GLUtils::pushRootPath(filepath);
152+
bool success = exec(string(filepath));
153+
V8GLUtils::popRootPath(old_path);
154+
155+
if(!success) {
151156
fprintf(stderr, "Error reading '%s'.\n", arg0);
152-
return v8::Undefined();
157+
return ThrowException(String::New("Failed to load script"));
153158
}
154159
}
155160

0 commit comments

Comments
 (0)