From cfc26899b65040bb30c9b8c3ead7aca8c8669d20 Mon Sep 17 00:00:00 2001 From: Cong Liu Date: Wed, 19 Nov 2014 12:28:04 +0800 Subject: [PATCH] Fixed build failure and test case on OSX --- src/node_buffer.cc | 10 +++++-- src/string_bytes.cc | 28 ++++++++++-------- test/common.js | 5 ++++ test/simple/test-v8-externalize-string.js | 36 +++++++++++++++++++++++ 4 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 test/simple/test-v8-externalize-string.js diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 76790feeebe..1d39767c493 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -124,11 +124,17 @@ Local New(Isolate* isolate, Handle string, enum encoding enc) { if (string->IsExternalAscii()) { const String::ExternalAsciiStringResource* ext; ext = string->GetExternalAsciiStringResource(); - tmp_string = String::NewFromOneByte(isolate, reinterpret_cast(ext->data()), String::NewStringType::kNormalString, ext->length()); + tmp_string = String::NewFromOneByte(isolate, + reinterpret_cast(ext->data()), + String::kNormalString, + ext->length()); } else if (string->IsExternal()) { const String::ExternalStringResource* ext; ext = string->GetExternalStringResource(); - tmp_string = String::NewFromTwoByte(isolate, ext->data(), String::NewStringType::kNormalString, ext->length()); + tmp_string = String::NewFromTwoByte(isolate, + ext->data(), + String::kNormalString, + ext->length()); } else { tmp_string = string; } diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 4a917785be3..fb4cdc77851 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -304,12 +304,14 @@ size_t StringBytes::Write(Isolate* isolate, CHECK(val->IsString() == true); Local str = val.As(); bool is_extern_ascii = is_extern && str->IsExternalAscii(); - if (is_extern) { - if (is_extern_ascii) { - str = String::NewFromOneByte(isolate, reinterpret_cast(data), String::NewStringType::kNormalString, len); - } else { - str = String::NewFromTwoByte(isolate, reinterpret_cast(data), String::NewStringType::kNormalString, len); - } + if (is_extern_ascii) { + str = String::NewFromOneByte(isolate, + reinterpret_cast(data), + String::kNormalString, len); + } else if (is_extern) { + str = String::NewFromTwoByte(isolate, + reinterpret_cast(data), + String::kNormalString, len); } len = len < buflen ? len : buflen; @@ -445,12 +447,14 @@ size_t StringBytes::Size(Isolate* isolate, Local str = val->ToString(); bool is_extern_ascii = is_extern && str->IsExternalAscii(); - if (is_extern) { - if (is_extern_ascii) { - str = String::NewFromOneByte(isolate, reinterpret_cast(data), String::NewStringType::kNormalString, data_size); - } else { - str = String::NewFromTwoByte(isolate, reinterpret_cast(data), String::NewStringType::kNormalString, data_size); - } + if (is_extern_ascii) { + str = String::NewFromOneByte(isolate, + reinterpret_cast(data), + String::kNormalString, data_size); + } else if (is_extern) { + str = String::NewFromTwoByte(isolate, + reinterpret_cast(data), + String::kNormalString, data_size); } switch (encoding) { diff --git a/test/common.js b/test/common.js index 40f70728327..d85edefebde 100644 --- a/test/common.js +++ b/test/common.js @@ -105,6 +105,11 @@ if (global.gc) { knownGlobals.push(gc); } +if (global.externalizeString) { + knownGlobals.push(externalizeString); + knownGlobals.push(isAsciiString); +} + if (global.DTRACE_HTTP_SERVER_RESPONSE) { knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); diff --git a/test/simple/test-v8-externalize-string.js b/test/simple/test-v8-externalize-string.js new file mode 100644 index 00000000000..af8a1395cca --- /dev/null +++ b/test/simple/test-v8-externalize-string.js @@ -0,0 +1,36 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// Flags: --expose_externalize_string + +var common = require('../common'); +var assert = require('assert'); + +assert(typeof externalizeString === 'function', + 'Run this test with --expose_externalize_string.'); + +var str = 'δΈ­'; +var buf = new Buffer(str); +assert.equal(buf.toString('hex'), 'e4b8ad'); + +externalizeString(str, true); // force two bytes string +var extbuf = new Buffer(str); // convert string to utf8 buffer +assert.equal(extbuf.toString('hex'), 'e4b8ad');