Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Temporary function to determine str byte length
Browse files Browse the repository at this point in the history
Will need a better place later on
  • Loading branch information
felixge authored and ry committed Nov 11, 2009
1 parent 1026ffe commit 7371fcb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/node.cc
Expand Up @@ -231,6 +231,18 @@ Handle<Value> ExecuteString(v8::Handle<v8::String> source,
return scope.Close(result);
}

static Handle<Value> ByteLength(const Arguments& args) {
HandleScope scope;

if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New("Bad argument.")));
}

Local<Integer> length = Integer::New(DecodeBytes(args[0], ParseEncoding(args[1], UTF8)));

return scope.Close(length);
}

static Handle<Value> Chdir(const Arguments& args) {
HandleScope scope;

Expand Down Expand Up @@ -624,6 +636,7 @@ static Local<Object> 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);
Expand Down
11 changes: 11 additions & 0 deletions 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)');

0 comments on commit 7371fcb

Please sign in to comment.