From 4f7fe371b28251382c9b710baebd914337d9b23c Mon Sep 17 00:00:00 2001 From: "ager@chromium.org" Date: Wed, 16 Mar 2011 19:55:31 +0000 Subject: [PATCH] Fix Array::New(length) in the API to return an array with the provided length. The internal factory method initializes the elements but does not set the length property of the array. Add array api test case for length. R=antonm@chromium.org BUG=v8:1256 TEST=cctest/test-api/Array Review URL: http://codereview.chromium.org/6674034 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7210 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 5 +++++ src/api.cc | 4 +++- src/factory.cc | 4 ++-- src/factory.h | 2 +- test/cctest/test-api.cc | 4 ++++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/v8.h b/include/v8.h index ad316ce..7875cfa 100644 --- a/include/v8.h +++ b/include/v8.h @@ -1706,7 +1706,12 @@ class Array : public Object { */ V8EXPORT Local CloneElementAt(uint32_t index); + /** + * Creates a JavaScript array with the given length. If the length + * is negative the returned array will have length 0. + */ V8EXPORT static Local New(int length = 0); + static inline Array* Cast(Value* obj); private: V8EXPORT Array(); diff --git a/src/api.cc b/src/api.cc index 9e77d31..8a8640d 100644 --- a/src/api.cc +++ b/src/api.cc @@ -3960,7 +3960,9 @@ Local v8::Array::New(int length) { EnsureInitialized("v8::Array::New()"); LOG_API("Array::New"); ENTER_V8; - i::Handle obj = i::Factory::NewJSArray(length); + int real_length = length > 0 ? length : 0; + i::Handle obj = i::Factory::NewJSArray(real_length); + obj->set_length(*i::Factory::NewNumberFromInt(real_length)); return Utils::ToLocal(obj); } diff --git a/src/factory.cc b/src/factory.cc index 4d2c6b4..e848b57 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -717,10 +717,10 @@ Handle Factory::NewJSObjectFromMap(Handle map) { } -Handle Factory::NewJSArray(int length, +Handle Factory::NewJSArray(int capacity, PretenureFlag pretenure) { Handle obj = NewJSObject(Top::array_function(), pretenure); - CALL_HEAP_FUNCTION(Handle::cast(obj)->Initialize(length), JSArray); + CALL_HEAP_FUNCTION(Handle::cast(obj)->Initialize(capacity), JSArray); } diff --git a/src/factory.h b/src/factory.h index 23e7001..e03e98b 100644 --- a/src/factory.h +++ b/src/factory.h @@ -221,7 +221,7 @@ class Factory : public AllStatic { static Handle NewJSObjectFromMap(Handle map); // JS arrays are pretenured when allocated by the parser. - static Handle NewJSArray(int init_length, + static Handle NewJSArray(int capacity, PretenureFlag pretenure = NOT_TENURED); static Handle NewJSArrayWithElements( diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 2a4ac22..7552930 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -2078,6 +2078,10 @@ THREADED_TEST(Array) { CHECK_EQ(1, arr->Get(0)->Int32Value()); CHECK_EQ(2, arr->Get(1)->Int32Value()); CHECK_EQ(3, arr->Get(2)->Int32Value()); + array = v8::Array::New(27); + CHECK_EQ(27, array->Length()); + array = v8::Array::New(-27); + CHECK_EQ(0, array->Length()); } -- 2.7.4