From 47fcf785ac1468744da55469bc78fbfd8628d9fb Mon Sep 17 00:00:00 2001 From: Brandon Beacher Date: Tue, 3 Nov 2009 13:13:38 -0500 Subject: [PATCH] Added process.chdir() --- src/node.cc | 19 +++++++++++++++++++ test/mjsunit/test-chdir.js | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/mjsunit/test-chdir.js 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);