diff --git a/src/node.cc b/src/node.cc index 0cf2d2fd7d..ef55ef1c53 100644 --- a/src/node.cc +++ b/src/node.cc @@ -231,6 +231,24 @@ Handle ExecuteString(v8::Handle source, return scope.Close(result); } +static Handle Chdir(const Arguments& args) { + HandleScope scope; + + if (args.Length() != 1 || !args[0]->IsString()) { + return ThrowException(Exception::Error(String::New("Bad argument."))); + } + + String::Utf8Value path(args[0]->ToString()); + + int r = chdir(*path); + + if (r != 0) { + return ThrowException(Exception::Error(String::New(strerror(errno)))); + } + + return Undefined(); +} + static Handle Cwd(const Arguments& args) { HandleScope scope; @@ -607,6 +625,7 @@ static Local Load(int argc, char *argv[]) { // define various internal methods NODE_SET_METHOD(process, "compile", Compile); NODE_SET_METHOD(process, "reallyExit", Exit); + NODE_SET_METHOD(process, "chdir", Chdir); NODE_SET_METHOD(process, "cwd", Cwd); NODE_SET_METHOD(process, "dlopen", DLOpen); NODE_SET_METHOD(process, "kill", Kill); diff --git a/test/mjsunit/test-chdir.js b/test/mjsunit/test-chdir.js new file mode 100644 index 0000000000..11a32c638f --- /dev/null +++ b/test/mjsunit/test-chdir.js @@ -0,0 +1,9 @@ +process.mixin(require("./common")); + +var dirname = path.dirname(__filename); + +assertTrue(process.cwd() !== dirname); + +process.chdir(dirname); + +assertTrue(process.cwd() === dirname);