diff options
author | agirbal <antoine@10gen.com> | 2011-05-30 11:22:28 -0700 |
---|---|---|
committer | agirbal <antoine@10gen.com> | 2011-05-30 11:22:28 -0700 |
commit | e371c76ccac4f166b7f267114526a6c8b073f5de (patch) | |
tree | 1d3579fa9ee81e4593bd770423322e141dde5305 /scripting | |
parent | 1eb57466fc10c3538e1b1585fc709451909af359 (diff) | |
download | mongo-e371c76ccac4f166b7f267114526a6c8b073f5de.tar.gz |
SERVER-3168: methods implemented for BinData
Diffstat (limited to 'scripting')
-rw-r--r-- | scripting/v8_db.cpp | 22 | ||||
-rw-r--r-- | scripting/v8_db.h | 1 |
2 files changed, 20 insertions, 3 deletions
diff --git a/scripting/v8_db.cpp b/scripting/v8_db.cpp index 5994a8ff9a9..f846ace44e9 100644 --- a/scripting/v8_db.cpp +++ b/scripting/v8_db.cpp @@ -74,6 +74,7 @@ namespace mongo { v8::Local<v8::Template> proto = binData->PrototypeTemplate(); scope->injectV8Function("toString", binDataToString, proto); scope->injectV8Function("base64", binDataToBase64, proto); + scope->injectV8Function("hex", binDataToHex, proto); return binData; } @@ -712,11 +713,12 @@ namespace mongo { v8::Handle<v8::Object> it = args.This(); int len = it->Get( scope->V8STR_LEN )->Int32Value(); int type = it->Get( scope->V8STR_TYPE )->Int32Value(); - v8::String::Utf8Value data( it->Get( scope->getV8Str( "data" ) ) ); + Local<External> c = External::Cast( *(it->GetInternalField( 0 )) ); + char* data = (char*)(c->Value()); stringstream ss; ss << "BinData(" << type << ",\""; - base64::encode( ss, *data, len ); + base64::encode( ss, data, len ); ss << "\")"; string ret = ss.str(); return v8::String::New( ret.c_str() ); @@ -726,7 +728,6 @@ namespace mongo { v8::Handle<v8::Object> it = args.This(); int len = Handle<v8::Number>::Cast(it->Get(scope->V8STR_LEN))->Int32Value(); Local<External> c = External::Cast( *(it->GetInternalField( 0 )) ); - cout << "Internal count " << it->InternalFieldCount() << endl; char* data = (char*)(c->Value()); stringstream ss; base64::encode( ss, (const char *)data, len ); @@ -734,6 +735,21 @@ namespace mongo { return v8::String::New(ret.c_str()); } + v8::Handle<v8::Value> binDataToHex( V8Scope* scope, const v8::Arguments& args ) { + v8::Handle<v8::Object> it = args.This(); + int len = Handle<v8::Number>::Cast(it->Get(scope->V8STR_LEN))->Int32Value(); + Local<External> c = External::Cast( *(it->GetInternalField( 0 )) ); + char* data = (char*)(c->Value()); + stringstream ss; + ss << hex; + for( int i = 0; i < len; i++ ) { + unsigned v = (unsigned char) data[i]; + ss << v; + } + string ret = ss.str(); + return v8::String::New(ret.c_str()); + } + v8::Handle<v8::Value> numberLongInit( V8Scope* scope, const v8::Arguments& args ) { if (args.Length() != 0 && args.Length() != 1 && args.Length() != 3) { diff --git a/scripting/v8_db.h b/scripting/v8_db.h index b58f38a7741..8dc66c0e1c2 100644 --- a/scripting/v8_db.h +++ b/scripting/v8_db.h @@ -65,6 +65,7 @@ namespace mongo { v8::Handle<v8::Value> binDataInit( V8Scope* scope, const v8::Arguments& args ); v8::Handle<v8::Value> binDataToString( V8Scope* scope, const v8::Arguments& args ); v8::Handle<v8::Value> binDataToBase64( V8Scope* scope, const v8::Arguments& args ); + v8::Handle<v8::Value> binDataToHex( V8Scope* scope, const v8::Arguments& args ); v8::Handle<v8::Value> numberLongInit( V8Scope* scope, const v8::Arguments& args ); v8::Handle<v8::Value> numberLongToNumber(V8Scope* scope, const v8::Arguments& args); |