Commit 4f7fe371 authored by ager@chromium.org's avatar ager@chromium.org

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
parent a20ee851
......@@ -1706,7 +1706,12 @@ class Array : public Object {
*/
V8EXPORT Local<Object> 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<Array> New(int length = 0);
static inline Array* Cast(Value* obj);
private:
V8EXPORT Array();
......
......@@ -3960,7 +3960,9 @@ Local<v8::Array> v8::Array::New(int length) {
EnsureInitialized("v8::Array::New()");
LOG_API("Array::New");
ENTER_V8;
i::Handle<i::JSArray> obj = i::Factory::NewJSArray(length);
int real_length = length > 0 ? length : 0;
i::Handle<i::JSArray> obj = i::Factory::NewJSArray(real_length);
obj->set_length(*i::Factory::NewNumberFromInt(real_length));
return Utils::ToLocal(obj);
}
......
......@@ -717,10 +717,10 @@ Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
}
Handle<JSArray> Factory::NewJSArray(int length,
Handle<JSArray> Factory::NewJSArray(int capacity,
PretenureFlag pretenure) {
Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(capacity), JSArray);
}
......
......@@ -221,7 +221,7 @@ class Factory : public AllStatic {
static Handle<JSObject> NewJSObjectFromMap(Handle<Map> map);
// JS arrays are pretenured when allocated by the parser.
static Handle<JSArray> NewJSArray(int init_length,
static Handle<JSArray> NewJSArray(int capacity,
PretenureFlag pretenure = NOT_TENURED);
static Handle<JSArray> NewJSArrayWithElements(
......
......@@ -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());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment