diff --git a/src/node.cc b/src/node.cc index b63bbafcdb..1b45d5cbb5 100644 --- a/src/node.cc +++ b/src/node.cc @@ -231,6 +231,18 @@ Handle ExecuteString(v8::Handle source, return scope.Close(result); } +static Handle ByteLength(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 1 || !args[0]->IsString()) { + return ThrowException(Exception::Error(String::New("Bad argument."))); + } + + Local length = Integer::New(DecodeBytes(args[0], ParseEncoding(args[1], UTF8))); + + return scope.Close(length); +} + static Handle Chdir(const Arguments& args) { HandleScope scope; @@ -624,6 +636,7 @@ static Local Load(int argc, char *argv[]) { // define various internal methods NODE_SET_METHOD(process, "compile", Compile); + NODE_SET_METHOD(process, "_byteLength", ByteLength); NODE_SET_METHOD(process, "reallyExit", Exit); NODE_SET_METHOD(process, "chdir", Chdir); NODE_SET_METHOD(process, "cwd", Cwd); diff --git a/test/mjsunit/test-byte-length.js b/test/mjsunit/test-byte-length.js new file mode 100644 index 0000000000..5f1ddcabc7 --- /dev/null +++ b/test/mjsunit/test-byte-length.js @@ -0,0 +1,11 @@ +process.mixin(require("./common")); + +assertEquals(14, process._byteLength("Il était tué")); +assertEquals(14, process._byteLength("Il était tué", "utf8")); + +assertEquals(12, process._byteLength("Il était tué", "ascii")); + +assertEquals(12, process._byteLength("Il était tué", "binary")); + +assertThrows('process._byteLength()'); +assertThrows('process._byteLength(5)'); \ No newline at end of file