2011-09-19 18:36:47 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "execution.h"
|
|
|
|
#include "factory.h"
|
|
|
|
#include "macro-assembler.h"
|
|
|
|
#include "global-handles.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
|
|
|
static v8::Persistent<v8::Context> env;
|
|
|
|
|
|
|
|
static void InitializeVM() {
|
|
|
|
if (env.IsEmpty()) env = v8::Context::New();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
env->Enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckMap(Map* map, int type, int instance_size) {
|
|
|
|
CHECK(map->IsHeapObject());
|
|
|
|
#ifdef DEBUG
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->Contains(map));
|
2008-08-22 13:33:59 +00:00
|
|
|
#endif
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK_EQ(HEAP->meta_map(), map->map());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(type, map->instance_type());
|
|
|
|
CHECK_EQ(instance_size, map->instance_size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HeapMaps) {
|
|
|
|
InitializeVM();
|
2011-03-18 20:35:07 +00:00
|
|
|
CheckMap(HEAP->meta_map(), MAP_TYPE, Map::kSize);
|
|
|
|
CheckMap(HEAP->heap_number_map(), HEAP_NUMBER_TYPE, HeapNumber::kSize);
|
|
|
|
CheckMap(HEAP->fixed_array_map(), FIXED_ARRAY_TYPE, kVariableSizeSentinel);
|
|
|
|
CheckMap(HEAP->string_map(), STRING_TYPE, kVariableSizeSentinel);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckOddball(Object* obj, const char* string) {
|
|
|
|
CHECK(obj->IsOddball());
|
|
|
|
bool exc;
|
|
|
|
Object* print_string = *Execution::ToString(Handle<Object>(obj), &exc);
|
|
|
|
CHECK(String::cast(print_string)->IsEqualTo(CStrVector(string)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckSmi(int value, const char* string) {
|
|
|
|
bool exc;
|
|
|
|
Object* print_string =
|
|
|
|
*Execution::ToString(Handle<Object>(Smi::FromInt(value)), &exc);
|
|
|
|
CHECK(String::cast(print_string)->IsEqualTo(CStrVector(string)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckNumber(double value, const char* string) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* obj = HEAP->NumberFromDouble(value)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(obj->IsNumber());
|
|
|
|
bool exc;
|
|
|
|
Object* print_string = *Execution::ToString(Handle<Object>(obj), &exc);
|
|
|
|
CHECK(String::cast(print_string)->IsEqualTo(CStrVector(string)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckFindCodeObject() {
|
|
|
|
// Test FindCodeObject
|
|
|
|
#define __ assm.
|
|
|
|
|
2011-03-31 16:17:37 +00:00
|
|
|
Assembler assm(Isolate::Current(), NULL, 0);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
__ nop(); // supported on all architectures
|
|
|
|
|
|
|
|
CodeDesc desc;
|
|
|
|
assm.GetCode(&desc);
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* code = HEAP->CreateCode(
|
2010-10-25 15:22:03 +00:00
|
|
|
desc,
|
|
|
|
Code::ComputeFlags(Code::STUB),
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object>(HEAP->undefined_value()))->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(code->IsCode());
|
|
|
|
|
|
|
|
HeapObject* obj = HeapObject::cast(code);
|
|
|
|
Address obj_addr = obj->address();
|
|
|
|
|
|
|
|
for (int i = 0; i < obj->Size(); i += kPointerSize) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* found = HEAP->FindCodeObject(obj_addr + i);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(code, found);
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* copy = HEAP->CreateCode(
|
2010-10-25 15:22:03 +00:00
|
|
|
desc,
|
|
|
|
Code::ComputeFlags(Code::STUB),
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object>(HEAP->undefined_value()))->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(copy->IsCode());
|
|
|
|
HeapObject* obj_copy = HeapObject::cast(copy);
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* not_right = HEAP->FindCodeObject(obj_copy->address() +
|
2008-08-22 13:33:59 +00:00
|
|
|
obj_copy->Size() / 2);
|
|
|
|
CHECK(not_right != code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HeapObjects) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* value = HEAP->NumberFromDouble(1.000123)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsHeapNumber());
|
|
|
|
CHECK(value->IsNumber());
|
|
|
|
CHECK_EQ(1.000123, value->Number());
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
value = HEAP->NumberFromDouble(1.0)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsSmi());
|
|
|
|
CHECK(value->IsNumber());
|
|
|
|
CHECK_EQ(1.0, value->Number());
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
value = HEAP->NumberFromInt32(1024)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsSmi());
|
|
|
|
CHECK(value->IsNumber());
|
|
|
|
CHECK_EQ(1024.0, value->Number());
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
value = HEAP->NumberFromInt32(Smi::kMinValue)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsSmi());
|
|
|
|
CHECK(value->IsNumber());
|
|
|
|
CHECK_EQ(Smi::kMinValue, Smi::cast(value)->value());
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
value = HEAP->NumberFromInt32(Smi::kMaxValue)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsSmi());
|
|
|
|
CHECK(value->IsNumber());
|
|
|
|
CHECK_EQ(Smi::kMaxValue, Smi::cast(value)->value());
|
|
|
|
|
2009-10-12 11:19:20 +00:00
|
|
|
#ifndef V8_TARGET_ARCH_X64
|
2009-10-08 12:36:12 +00:00
|
|
|
// TODO(lrn): We need a NumberFromIntptr function in order to test this.
|
2011-03-18 20:35:07 +00:00
|
|
|
value = HEAP->NumberFromInt32(Smi::kMinValue - 1)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsHeapNumber());
|
|
|
|
CHECK(value->IsNumber());
|
|
|
|
CHECK_EQ(static_cast<double>(Smi::kMinValue - 1), value->Number());
|
2009-10-08 12:36:12 +00:00
|
|
|
#endif
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
MaybeObject* maybe_value =
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->NumberFromUint32(static_cast<uint32_t>(Smi::kMaxValue) + 1);
|
2010-10-25 15:22:03 +00:00
|
|
|
value = maybe_value->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(value->IsHeapNumber());
|
|
|
|
CHECK(value->IsNumber());
|
2009-10-08 12:36:12 +00:00
|
|
|
CHECK_EQ(static_cast<double>(static_cast<uint32_t>(Smi::kMaxValue) + 1),
|
|
|
|
value->Number());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// nan oddball checks
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->nan_value()->IsNumber());
|
|
|
|
CHECK(isnan(HEAP->nan_value()->Number()));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> s = FACTORY->NewStringFromAscii(CStrVector("fisk hest "));
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(s->IsString());
|
|
|
|
CHECK_EQ(10, s->length());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
String* object_symbol = String::cast(HEAP->Object_symbol());
|
|
|
|
CHECK(
|
|
|
|
Isolate::Current()->context()->global()->HasLocalProperty(object_symbol));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check ToString for oddballs
|
2011-03-18 20:35:07 +00:00
|
|
|
CheckOddball(HEAP->true_value(), "true");
|
|
|
|
CheckOddball(HEAP->false_value(), "false");
|
|
|
|
CheckOddball(HEAP->null_value(), "null");
|
|
|
|
CheckOddball(HEAP->undefined_value(), "undefined");
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check ToString for Smis
|
|
|
|
CheckSmi(0, "0");
|
|
|
|
CheckSmi(42, "42");
|
|
|
|
CheckSmi(-42, "-42");
|
|
|
|
|
|
|
|
// Check ToString for Numbers
|
|
|
|
CheckNumber(1.1, "1.1");
|
|
|
|
|
|
|
|
CheckFindCodeObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Tagging) {
|
|
|
|
InitializeVM();
|
2009-05-22 14:01:06 +00:00
|
|
|
int request = 24;
|
2010-05-27 12:30:45 +00:00
|
|
|
CHECK_EQ(request, static_cast<int>(OBJECT_POINTER_ALIGN(request)));
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(Smi::FromInt(42)->IsSmi());
|
2010-10-18 12:58:56 +00:00
|
|
|
CHECK(Failure::RetryAfterGC(NEW_SPACE)->IsFailure());
|
2009-05-22 14:01:06 +00:00
|
|
|
CHECK_EQ(NEW_SPACE,
|
2010-10-18 12:58:56 +00:00
|
|
|
Failure::RetryAfterGC(NEW_SPACE)->allocation_space());
|
2008-09-05 12:34:09 +00:00
|
|
|
CHECK_EQ(OLD_POINTER_SPACE,
|
2010-10-18 12:58:56 +00:00
|
|
|
Failure::RetryAfterGC(OLD_POINTER_SPACE)->allocation_space());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(Failure::Exception()->IsFailure());
|
|
|
|
CHECK(Smi::FromInt(Smi::kMinValue)->IsSmi());
|
|
|
|
CHECK(Smi::FromInt(Smi::kMaxValue)->IsSmi());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GarbageCollection) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2010-03-05 09:51:10 +00:00
|
|
|
// Check GC.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> name = FACTORY->LookupAsciiSymbol("theFunction");
|
|
|
|
Handle<String> prop_name = FACTORY->LookupAsciiSymbol("theSlot");
|
|
|
|
Handle<String> prop_namex = FACTORY->LookupAsciiSymbol("theSlotx");
|
|
|
|
Handle<String> obj_name = FACTORY->LookupAsciiSymbol("theObject");
|
2010-03-05 09:51:10 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
v8::HandleScope inner_scope;
|
|
|
|
// Allocate a function and keep it in global object's property.
|
|
|
|
Handle<JSFunction> function =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewFunction(name, FACTORY->undefined_value());
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Map> initial_map =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
2010-03-05 09:51:10 +00:00
|
|
|
function->set_initial_map(*initial_map);
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate::Current()->context()->global()->SetProperty(
|
2011-03-02 04:53:43 +00:00
|
|
|
*name, *function, NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
// Allocate an object. Unrooted after leaving the scope.
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSObject> obj = FACTORY->NewJSObject(function);
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked();
|
|
|
|
obj->SetProperty(
|
|
|
|
*prop_namex, Smi::FromInt(24), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
|
|
|
|
CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name));
|
|
|
|
CHECK_EQ(Smi::FromInt(24), obj->GetProperty(*prop_namex));
|
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
// Function should be alive.
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(Isolate::Current()->context()->global()->HasLocalProperty(*name));
|
2010-03-05 09:51:10 +00:00
|
|
|
// Check function is retained.
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* func_value = Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(*name)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(func_value->IsJSFunction());
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSFunction> function(JSFunction::cast(func_value));
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope inner_scope;
|
|
|
|
// Allocate another object, make it reachable from global.
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSObject> obj = FACTORY->NewJSObject(function);
|
|
|
|
Isolate::Current()->context()->global()->SetProperty(
|
2011-03-02 04:53:43 +00:00
|
|
|
*obj_name, *obj, NONE, kNonStrictMode)->ToObjectChecked();
|
|
|
|
obj->SetProperty(
|
|
|
|
*prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
// After gc, it should survive.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(Isolate::Current()->context()->global()->HasLocalProperty(*obj_name));
|
|
|
|
CHECK(Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(*obj_name)->ToObjectChecked()->IsJSObject());
|
|
|
|
Object* obj = Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(*obj_name)->ToObjectChecked();
|
2010-10-25 15:22:03 +00:00
|
|
|
JSObject* js_obj = JSObject::cast(obj);
|
|
|
|
CHECK_EQ(Smi::FromInt(23), js_obj->GetProperty(*prop_name));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void VerifyStringAllocation(const char* string) {
|
2010-03-05 09:51:10 +00:00
|
|
|
v8::HandleScope scope;
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> s = FACTORY->NewStringFromUtf8(CStrVector(string));
|
2009-11-11 09:50:06 +00:00
|
|
|
CHECK_EQ(StrLength(string), s->length());
|
2009-03-17 09:33:06 +00:00
|
|
|
for (int index = 0; index < s->length(); index++) {
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(static_cast<uint16_t>(string[index]), s->Get(index));
|
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(String) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
VerifyStringAllocation("a");
|
|
|
|
VerifyStringAllocation("ab");
|
|
|
|
VerifyStringAllocation("abc");
|
|
|
|
VerifyStringAllocation("abcd");
|
|
|
|
VerifyStringAllocation("fiskerdrengen er paa havet");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(LocalHandles) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
const char* name = "Kasper the spunky";
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> string = FACTORY->NewStringFromAscii(CStrVector(name));
|
2009-11-11 09:50:06 +00:00
|
|
|
CHECK_EQ(StrLength(name), string->length());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GlobalHandles) {
|
|
|
|
InitializeVM();
|
2011-08-04 15:18:18 +00:00
|
|
|
GlobalHandles* global_handles = Isolate::Current()->global_handles();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Object> h1;
|
|
|
|
Handle<Object> h2;
|
|
|
|
Handle<Object> h3;
|
|
|
|
Handle<Object> h4;
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk"));
|
|
|
|
Handle<Object> u = FACTORY->NewNumber(1.12344);
|
2010-03-05 09:51:10 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
h1 = global_handles->Create(*i);
|
|
|
|
h2 = global_handles->Create(*u);
|
|
|
|
h3 = global_handles->Create(*i);
|
|
|
|
h4 = global_handles->Create(*u);
|
2010-03-05 09:51:10 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// after gc, it should survive
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK((*h1)->IsString());
|
|
|
|
CHECK((*h2)->IsHeapNumber());
|
|
|
|
CHECK((*h3)->IsString());
|
|
|
|
CHECK((*h4)->IsHeapNumber());
|
|
|
|
|
|
|
|
CHECK_EQ(*h3, *h1);
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Destroy(h1.location());
|
|
|
|
global_handles->Destroy(h3.location());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK_EQ(*h4, *h2);
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Destroy(h2.location());
|
|
|
|
global_handles->Destroy(h4.location());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool WeakPointerCleared = false;
|
|
|
|
|
2008-10-23 08:25:23 +00:00
|
|
|
static void TestWeakGlobalHandleCallback(v8::Persistent<v8::Value> handle,
|
2008-08-22 13:33:59 +00:00
|
|
|
void* id) {
|
2009-05-19 08:59:23 +00:00
|
|
|
if (1234 == reinterpret_cast<intptr_t>(id)) WeakPointerCleared = true;
|
2010-07-19 13:26:25 +00:00
|
|
|
handle.Dispose();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(WeakGlobalHandlesScavenge) {
|
|
|
|
InitializeVM();
|
2011-08-04 15:18:18 +00:00
|
|
|
GlobalHandles* global_handles = Isolate::Current()->global_handles();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
WeakPointerCleared = false;
|
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Object> h1;
|
|
|
|
Handle<Object> h2;
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk"));
|
|
|
|
Handle<Object> u = FACTORY->NewNumber(1.12344);
|
2010-03-05 09:51:10 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
h1 = global_handles->Create(*i);
|
|
|
|
h2 = global_handles->Create(*u);
|
2010-03-05 09:51:10 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->MakeWeak(h2.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&TestWeakGlobalHandleCallback);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Scavenge treats weak pointers as normal roots.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK((*h1)->IsString());
|
|
|
|
CHECK((*h2)->IsHeapNumber());
|
|
|
|
|
|
|
|
CHECK(!WeakPointerCleared);
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(!global_handles->IsNearDeath(h2.location()));
|
|
|
|
CHECK(!global_handles->IsNearDeath(h1.location()));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Destroy(h1.location());
|
|
|
|
global_handles->Destroy(h2.location());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(WeakGlobalHandlesMark) {
|
|
|
|
InitializeVM();
|
2011-08-04 15:18:18 +00:00
|
|
|
GlobalHandles* global_handles = Isolate::Current()->global_handles();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
WeakPointerCleared = false;
|
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Object> h1;
|
|
|
|
Handle<Object> h2;
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk"));
|
|
|
|
Handle<Object> u = FACTORY->NewNumber(1.12344);
|
2010-03-05 09:51:10 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
h1 = global_handles->Create(*i);
|
|
|
|
h2 = global_handles->Create(*u);
|
2010-03-05 09:51:10 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
// Make sure the object is promoted.
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->MakeWeak(h2.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&TestWeakGlobalHandleCallback);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(!GlobalHandles::IsNearDeath(h1.location()));
|
|
|
|
CHECK(!GlobalHandles::IsNearDeath(h2.location()));
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK((*h1)->IsString());
|
|
|
|
|
|
|
|
CHECK(WeakPointerCleared);
|
|
|
|
CHECK(!GlobalHandles::IsNearDeath(h1.location()));
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Destroy(h1.location());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DeleteWeakGlobalHandle) {
|
|
|
|
InitializeVM();
|
2011-08-04 15:18:18 +00:00
|
|
|
GlobalHandles* global_handles = Isolate::Current()->global_handles();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
WeakPointerCleared = false;
|
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Object> h;
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk"));
|
|
|
|
h = global_handles->Create(*i);
|
2010-03-05 09:51:10 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->MakeWeak(h.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&TestWeakGlobalHandleCallback);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Scanvenge does not recognize weak reference.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK(!WeakPointerCleared);
|
|
|
|
|
|
|
|
// Mark-compact treats weak reference properly.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK(WeakPointerCleared);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* not_so_random_string_table[] = {
|
|
|
|
"abstract",
|
|
|
|
"boolean",
|
|
|
|
"break",
|
|
|
|
"byte",
|
|
|
|
"case",
|
|
|
|
"catch",
|
|
|
|
"char",
|
|
|
|
"class",
|
|
|
|
"const",
|
|
|
|
"continue",
|
|
|
|
"debugger",
|
|
|
|
"default",
|
|
|
|
"delete",
|
|
|
|
"do",
|
|
|
|
"double",
|
|
|
|
"else",
|
|
|
|
"enum",
|
|
|
|
"export",
|
|
|
|
"extends",
|
|
|
|
"false",
|
|
|
|
"final",
|
|
|
|
"finally",
|
|
|
|
"float",
|
|
|
|
"for",
|
|
|
|
"function",
|
|
|
|
"goto",
|
|
|
|
"if",
|
|
|
|
"implements",
|
|
|
|
"import",
|
|
|
|
"in",
|
|
|
|
"instanceof",
|
|
|
|
"int",
|
|
|
|
"interface",
|
|
|
|
"long",
|
|
|
|
"native",
|
|
|
|
"new",
|
|
|
|
"null",
|
|
|
|
"package",
|
|
|
|
"private",
|
|
|
|
"protected",
|
|
|
|
"public",
|
|
|
|
"return",
|
|
|
|
"short",
|
|
|
|
"static",
|
|
|
|
"super",
|
|
|
|
"switch",
|
|
|
|
"synchronized",
|
|
|
|
"this",
|
|
|
|
"throw",
|
|
|
|
"throws",
|
|
|
|
"transient",
|
|
|
|
"true",
|
|
|
|
"try",
|
|
|
|
"typeof",
|
|
|
|
"var",
|
|
|
|
"void",
|
|
|
|
"volatile",
|
|
|
|
"while",
|
|
|
|
"with",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckSymbols(const char** strings) {
|
|
|
|
for (const char* string = *strings; *strings != 0; string = *strings++) {
|
2010-10-25 15:22:03 +00:00
|
|
|
Object* a;
|
2011-03-18 20:35:07 +00:00
|
|
|
MaybeObject* maybe_a = HEAP->LookupAsciiSymbol(string);
|
2009-08-18 12:43:36 +00:00
|
|
|
// LookupAsciiSymbol may return a failure if a GC is needed.
|
2010-10-25 15:22:03 +00:00
|
|
|
if (!maybe_a->ToObject(&a)) continue;
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(a->IsSymbol());
|
2010-10-25 15:22:03 +00:00
|
|
|
Object* b;
|
2011-03-18 20:35:07 +00:00
|
|
|
MaybeObject* maybe_b = HEAP->LookupAsciiSymbol(string);
|
2010-10-25 15:29:06 +00:00
|
|
|
if (!maybe_b->ToObject(&b)) continue;
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(b, a);
|
|
|
|
CHECK(String::cast(b)->IsEqualTo(CStrVector(string)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SymbolTable) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
CheckSymbols(not_so_random_string_table);
|
|
|
|
CheckSymbols(not_so_random_string_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FunctionAllocation) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> name = FACTORY->LookupAsciiSymbol("theFunction");
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSFunction> function =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewFunction(name, FACTORY->undefined_value());
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Map> initial_map =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
2010-03-05 09:51:10 +00:00
|
|
|
function->set_initial_map(*initial_map);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> prop_name = FACTORY->LookupAsciiSymbol("theSlot");
|
|
|
|
Handle<JSObject> obj = FACTORY->NewJSObject(function);
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name));
|
2008-08-22 13:33:59 +00:00
|
|
|
// Check that we can add properties to function objects.
|
2011-03-02 04:53:43 +00:00
|
|
|
function->SetProperty(
|
|
|
|
*prop_name, Smi::FromInt(24), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(Smi::FromInt(24), function->GetProperty(*prop_name));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ObjectProperties) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2011-03-18 20:35:07 +00:00
|
|
|
String* object_symbol = String::cast(HEAP->Object_symbol());
|
|
|
|
Object* raw_object = Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(object_symbol)->ToObjectChecked();
|
2010-10-25 15:22:03 +00:00
|
|
|
JSFunction* object_function = JSFunction::cast(raw_object);
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSFunction> constructor(object_function);
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSObject> obj = FACTORY->NewJSObject(constructor);
|
|
|
|
Handle<String> first = FACTORY->LookupAsciiSymbol("first");
|
|
|
|
Handle<String> second = FACTORY->LookupAsciiSymbol("second");
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// check for empty
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(!obj->HasLocalProperty(*first));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// add first
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->HasLocalProperty(*first));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// delete first
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION));
|
|
|
|
CHECK(!obj->HasLocalProperty(*first));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// add first and then second
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
|
|
|
obj->SetProperty(
|
|
|
|
*second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->HasLocalProperty(*first));
|
|
|
|
CHECK(obj->HasLocalProperty(*second));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// delete first and then second
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION));
|
|
|
|
CHECK(obj->HasLocalProperty(*second));
|
|
|
|
CHECK(obj->DeleteProperty(*second, JSObject::NORMAL_DELETION));
|
|
|
|
CHECK(!obj->HasLocalProperty(*first));
|
|
|
|
CHECK(!obj->HasLocalProperty(*second));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// add first and then second
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
|
|
|
obj->SetProperty(
|
|
|
|
*second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->HasLocalProperty(*first));
|
|
|
|
CHECK(obj->HasLocalProperty(*second));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// delete second and then first
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->DeleteProperty(*second, JSObject::NORMAL_DELETION));
|
|
|
|
CHECK(obj->HasLocalProperty(*first));
|
|
|
|
CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION));
|
|
|
|
CHECK(!obj->HasLocalProperty(*first));
|
|
|
|
CHECK(!obj->HasLocalProperty(*second));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// check string and symbol match
|
|
|
|
static const char* string1 = "fisk";
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> s1 = FACTORY->NewStringFromAscii(CStrVector(string1));
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*s1, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> s1_symbol = FACTORY->LookupAsciiSymbol(string1);
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->HasLocalProperty(*s1_symbol));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// check symbol and string match
|
|
|
|
static const char* string2 = "fugl";
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> s2_symbol = FACTORY->LookupAsciiSymbol(string2);
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*s2_symbol, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> s2 = FACTORY->NewStringFromAscii(CStrVector(string2));
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(obj->HasLocalProperty(*s2));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(JSObjectMaps) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> name = FACTORY->LookupAsciiSymbol("theFunction");
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSFunction> function =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewFunction(name, FACTORY->undefined_value());
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<Map> initial_map =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
2010-03-05 09:51:10 +00:00
|
|
|
function->set_initial_map(*initial_map);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> prop_name = FACTORY->LookupAsciiSymbol("theSlot");
|
|
|
|
Handle<JSObject> obj = FACTORY->NewJSObject(function);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Set a propery
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Check the map has changed
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK(*initial_map != obj->map());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(JSArray) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> name = FACTORY->LookupAsciiSymbol("Array");
|
|
|
|
Object* raw_object = Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(*name)->ToObjectChecked();
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSFunction> function = Handle<JSFunction>(
|
2010-10-25 15:22:03 +00:00
|
|
|
JSFunction::cast(raw_object));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Allocate the object.
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSObject> object = FACTORY->NewJSObject(function);
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSArray> array = Handle<JSArray>::cast(object);
|
2010-08-31 08:05:42 +00:00
|
|
|
// We just initialized the VM, no heap allocation failure yet.
|
2010-10-25 15:22:03 +00:00
|
|
|
Object* ok = array->Initialize(0)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Set array length to 0.
|
2010-10-25 15:22:03 +00:00
|
|
|
ok = array->SetElementsLength(Smi::FromInt(0))->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(Smi::FromInt(0), array->length());
|
|
|
|
CHECK(array->HasFastElements()); // Must be in fast mode.
|
|
|
|
|
|
|
|
// array[length] = name.
|
2011-06-16 14:12:58 +00:00
|
|
|
ok = array->SetElement(0, *name, kNonStrictMode, true)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(Smi::FromInt(1), array->length());
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(array->GetElement(0), *name);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
// Set array length with larger than smi value.
|
|
|
|
Handle<Object> length =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewNumberFromUint(static_cast<uint32_t>(Smi::kMaxValue) + 1);
|
2010-10-25 15:22:03 +00:00
|
|
|
ok = array->SetElementsLength(*length)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
uint32_t int_length = 0;
|
2010-05-27 12:30:45 +00:00
|
|
|
CHECK(length->ToArrayIndex(&int_length));
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(*length, array->length());
|
2009-07-28 08:43:51 +00:00
|
|
|
CHECK(array->HasDictionaryElements()); // Must be in slow mode.
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// array[length] = name.
|
2011-06-16 14:12:58 +00:00
|
|
|
ok = array->SetElement(
|
|
|
|
int_length, *name, kNonStrictMode, true)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
uint32_t new_int_length = 0;
|
2010-05-27 12:30:45 +00:00
|
|
|
CHECK(array->length()->ToArrayIndex(&new_int_length));
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(static_cast<double>(int_length), new_int_length - 1);
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(array->GetElement(int_length), *name);
|
|
|
|
CHECK_EQ(array->GetElement(0), *name);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(JSObjectCopy) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
2011-03-18 20:35:07 +00:00
|
|
|
String* object_symbol = String::cast(HEAP->Object_symbol());
|
|
|
|
Object* raw_object = Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(object_symbol)->ToObjectChecked();
|
2010-10-25 15:22:03 +00:00
|
|
|
JSFunction* object_function = JSFunction::cast(raw_object);
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSFunction> constructor(object_function);
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSObject> obj = FACTORY->NewJSObject(constructor);
|
|
|
|
Handle<String> first = FACTORY->LookupAsciiSymbol("first");
|
|
|
|
Handle<String> second = FACTORY->LookupAsciiSymbol("second");
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(
|
|
|
|
*first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
|
|
|
obj->SetProperty(
|
|
|
|
*second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-06-16 14:12:58 +00:00
|
|
|
Object* ok =
|
|
|
|
obj->SetElement(0, *first, kNonStrictMode, true)->ToObjectChecked();
|
2010-08-31 08:05:42 +00:00
|
|
|
|
2011-06-16 14:12:58 +00:00
|
|
|
ok = obj->SetElement(1, *second, kNonStrictMode, true)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Make the clone.
|
2010-03-05 09:51:10 +00:00
|
|
|
Handle<JSObject> clone = Copy(obj);
|
|
|
|
CHECK(!clone.is_identical_to(obj));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK_EQ(obj->GetElement(0), clone->GetElement(0));
|
|
|
|
CHECK_EQ(obj->GetElement(1), clone->GetElement(1));
|
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(obj->GetProperty(*first), clone->GetProperty(*first));
|
|
|
|
CHECK_EQ(obj->GetProperty(*second), clone->GetProperty(*second));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Flip the values.
|
2011-03-02 04:53:43 +00:00
|
|
|
clone->SetProperty(
|
|
|
|
*first, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked();
|
|
|
|
clone->SetProperty(
|
|
|
|
*second, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-06-16 14:12:58 +00:00
|
|
|
ok = clone->SetElement(0, *second, kNonStrictMode, true)->ToObjectChecked();
|
|
|
|
ok = clone->SetElement(1, *first, kNonStrictMode, true)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK_EQ(obj->GetElement(1), clone->GetElement(0));
|
|
|
|
CHECK_EQ(obj->GetElement(0), clone->GetElement(1));
|
|
|
|
|
2010-03-05 09:51:10 +00:00
|
|
|
CHECK_EQ(obj->GetProperty(*second), clone->GetProperty(*first));
|
|
|
|
CHECK_EQ(obj->GetProperty(*first), clone->GetProperty(*second));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(StringAllocation) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned char chars[] = { 0xe5, 0xa4, 0xa7 };
|
|
|
|
for (int length = 0; length < 100; length++) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
char* non_ascii = NewArray<char>(3 * length + 1);
|
|
|
|
char* ascii = NewArray<char>(length + 1);
|
|
|
|
non_ascii[3 * length] = 0;
|
|
|
|
ascii[length] = 0;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
ascii[i] = 'a';
|
|
|
|
non_ascii[3 * i] = chars[0];
|
|
|
|
non_ascii[3 * i + 1] = chars[1];
|
|
|
|
non_ascii[3 * i + 2] = chars[2];
|
|
|
|
}
|
|
|
|
Handle<String> non_ascii_sym =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->LookupSymbol(Vector<const char>(non_ascii, 3 * length));
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(length, non_ascii_sym->length());
|
|
|
|
Handle<String> ascii_sym =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->LookupSymbol(Vector<const char>(ascii, length));
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(length, ascii_sym->length());
|
|
|
|
Handle<String> non_ascii_str =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewStringFromUtf8(Vector<const char>(non_ascii, 3 * length));
|
2008-08-22 13:33:59 +00:00
|
|
|
non_ascii_str->Hash();
|
|
|
|
CHECK_EQ(length, non_ascii_str->length());
|
|
|
|
Handle<String> ascii_str =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewStringFromUtf8(Vector<const char>(ascii, length));
|
2008-08-22 13:33:59 +00:00
|
|
|
ascii_str->Hash();
|
|
|
|
CHECK_EQ(length, ascii_str->length());
|
|
|
|
DeleteArray(non_ascii);
|
|
|
|
DeleteArray(ascii);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ObjectsFoundInHeap(Handle<Object> objs[], int size) {
|
|
|
|
// Count the number of objects found in the heap.
|
|
|
|
int found_count = 0;
|
|
|
|
HeapIterator iterator;
|
2010-01-25 22:53:18 +00:00
|
|
|
for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
|
2008-08-22 13:33:59 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (*objs[i] == obj) {
|
|
|
|
found_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Iteration) {
|
|
|
|
InitializeVM();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
// Array of objects to scan haep for.
|
|
|
|
const int objs_count = 6;
|
|
|
|
Handle<Object> objs[objs_count];
|
|
|
|
int next_objs_index = 0;
|
|
|
|
|
2008-09-05 12:34:09 +00:00
|
|
|
// Allocate a JS array to OLD_POINTER_SPACE and NEW_SPACE
|
2011-03-18 20:35:07 +00:00
|
|
|
objs[next_objs_index++] = FACTORY->NewJSArray(10);
|
|
|
|
objs[next_objs_index++] = FACTORY->NewJSArray(10, TENURED);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2008-09-05 12:34:09 +00:00
|
|
|
// Allocate a small string to OLD_DATA_SPACE and NEW_SPACE
|
2008-08-22 13:33:59 +00:00
|
|
|
objs[next_objs_index++] =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewStringFromAscii(CStrVector("abcdefghij"));
|
2008-08-22 13:33:59 +00:00
|
|
|
objs[next_objs_index++] =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewStringFromAscii(CStrVector("abcdefghij"), TENURED);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Allocate a large string (for large object space).
|
2011-03-18 20:35:07 +00:00
|
|
|
int large_size = HEAP->MaxObjectSizeInPagedSpace() + 1;
|
2008-08-22 13:33:59 +00:00
|
|
|
char* str = new char[large_size];
|
|
|
|
for (int i = 0; i < large_size - 1; ++i) str[i] = 'a';
|
|
|
|
str[large_size - 1] = '\0';
|
|
|
|
objs[next_objs_index++] =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewStringFromAscii(CStrVector(str), TENURED);
|
2008-08-22 13:33:59 +00:00
|
|
|
delete[] str;
|
|
|
|
|
|
|
|
// Add a Map object to look for.
|
|
|
|
objs[next_objs_index++] = Handle<Map>(HeapObject::cast(*objs[0])->map());
|
|
|
|
|
|
|
|
CHECK_EQ(objs_count, next_objs_index);
|
|
|
|
CHECK_EQ(objs_count, ObjectsFoundInHeap(objs, objs_count));
|
|
|
|
}
|
2010-03-25 13:10:50 +00:00
|
|
|
|
|
|
|
|
2010-03-25 14:07:17 +00:00
|
|
|
TEST(EmptyHandleEscapeFrom) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
Handle<JSObject> runaway;
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::HandleScope nested;
|
|
|
|
Handle<JSObject> empty;
|
|
|
|
runaway = empty.EscapeFrom(&nested);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(runaway.is_null());
|
|
|
|
}
|
2010-03-25 15:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int LenFromSize(int size) {
|
|
|
|
return (size - FixedArray::kHeaderSize) / kPointerSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Regression39128) {
|
|
|
|
// Test case for crbug.com/39128.
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
// Increase the chance of 'bump-the-pointer' allocation in old space.
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-03-25 15:32:58 +00:00
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
// The plan: create JSObject which references objects in new space.
|
|
|
|
// Then clone this object (forcing it to go into old space) and check
|
2010-05-27 12:30:45 +00:00
|
|
|
// that region dirty marks are updated correctly.
|
2010-03-25 15:32:58 +00:00
|
|
|
|
|
|
|
// Step 1: prepare a map for the object. We add 1 inobject property to it.
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSFunction> object_ctor(
|
|
|
|
Isolate::Current()->global_context()->object_function());
|
2010-03-25 15:32:58 +00:00
|
|
|
CHECK(object_ctor->has_initial_map());
|
|
|
|
Handle<Map> object_map(object_ctor->initial_map());
|
|
|
|
// Create a map with single inobject property.
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Map> my_map = FACTORY->CopyMap(object_map, 1);
|
2010-03-25 15:32:58 +00:00
|
|
|
int n_properties = my_map->inobject_properties();
|
|
|
|
CHECK_GT(n_properties, 0);
|
|
|
|
|
|
|
|
int object_size = my_map->instance_size();
|
|
|
|
|
|
|
|
// Step 2: allocate a lot of objects so to almost fill new space: we need
|
|
|
|
// just enough room to allocate JSObject and thus fill the newspace.
|
|
|
|
|
|
|
|
int allocation_amount = Min(FixedArray::kMaxSize,
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->MaxObjectSizeInNewSpace());
|
2010-03-25 15:32:58 +00:00
|
|
|
int allocation_len = LenFromSize(allocation_amount);
|
2011-03-18 20:35:07 +00:00
|
|
|
NewSpace* new_space = HEAP->new_space();
|
2010-03-25 15:32:58 +00:00
|
|
|
Address* top_addr = new_space->allocation_top_address();
|
|
|
|
Address* limit_addr = new_space->allocation_limit_address();
|
|
|
|
while ((*limit_addr - *top_addr) > allocation_amount) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(!HEAP->always_allocate());
|
|
|
|
Object* array = HEAP->AllocateFixedArray(allocation_len)->ToObjectChecked();
|
|
|
|
CHECK(!array->IsFailure());
|
2010-03-25 15:32:58 +00:00
|
|
|
CHECK(new_space->Contains(array));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: now allocate fixed array and JSObject to fill the whole new space.
|
2010-04-13 11:59:37 +00:00
|
|
|
int to_fill = static_cast<int>(*limit_addr - *top_addr - object_size);
|
2010-03-25 15:32:58 +00:00
|
|
|
int fixed_array_len = LenFromSize(to_fill);
|
|
|
|
CHECK(fixed_array_len < FixedArray::kMaxLength);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(!HEAP->always_allocate());
|
|
|
|
Object* array = HEAP->AllocateFixedArray(fixed_array_len)->ToObjectChecked();
|
|
|
|
CHECK(!array->IsFailure());
|
2010-03-25 15:32:58 +00:00
|
|
|
CHECK(new_space->Contains(array));
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* object = HEAP->AllocateJSObjectFromMap(*my_map)->ToObjectChecked();
|
2010-03-25 15:32:58 +00:00
|
|
|
CHECK(new_space->Contains(object));
|
|
|
|
JSObject* jsobject = JSObject::cast(object);
|
2010-05-27 12:30:45 +00:00
|
|
|
CHECK_EQ(0, FixedArray::cast(jsobject->elements())->length());
|
2010-03-25 15:32:58 +00:00
|
|
|
CHECK_EQ(0, jsobject->properties()->length());
|
|
|
|
// Create a reference to object in new space in jsobject.
|
|
|
|
jsobject->FastPropertyAtPut(-1, array);
|
|
|
|
|
2010-04-13 11:59:37 +00:00
|
|
|
CHECK_EQ(0, static_cast<int>(*limit_addr - *top_addr));
|
2010-03-25 15:32:58 +00:00
|
|
|
|
|
|
|
// Step 4: clone jsobject, but force always allocate first to create a clone
|
|
|
|
// in old pointer space.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address old_pointer_space_top = HEAP->old_pointer_space()->top();
|
2010-03-25 15:32:58 +00:00
|
|
|
AlwaysAllocateScope aa_scope;
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* clone_obj = HEAP->CopyJSObject(jsobject)->ToObjectChecked();
|
2010-03-25 15:32:58 +00:00
|
|
|
JSObject* clone = JSObject::cast(clone_obj);
|
|
|
|
if (clone->address() != old_pointer_space_top) {
|
|
|
|
// Alas, got allocated from free list, we cannot do checks.
|
|
|
|
return;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->old_pointer_space()->Contains(clone->address()));
|
2010-03-25 15:32:58 +00:00
|
|
|
}
|
2010-06-07 15:39:10 +00:00
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
|
2010-06-07 15:39:10 +00:00
|
|
|
TEST(TestCodeFlushing) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2010-06-11 08:52:36 +00:00
|
|
|
// If we do not flush code this test is invalid.
|
|
|
|
if (!FLAG_flush_code) return;
|
2010-06-07 15:39:10 +00:00
|
|
|
InitializeVM();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
const char* source = "function foo() {"
|
|
|
|
" var x = 42;"
|
|
|
|
" var y = 42;"
|
|
|
|
" var z = x + y;"
|
|
|
|
"};"
|
|
|
|
"foo()";
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> foo_name = FACTORY->LookupAsciiSymbol("foo");
|
2010-06-07 15:39:10 +00:00
|
|
|
|
|
|
|
// This compile will add the code to the compilation cache.
|
2010-12-07 11:31:57 +00:00
|
|
|
{ v8::HandleScope scope;
|
|
|
|
CompileRun(source);
|
|
|
|
}
|
2010-06-07 15:39:10 +00:00
|
|
|
|
|
|
|
// Check function is compiled.
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* func_value = Isolate::Current()->context()->global()->
|
|
|
|
GetProperty(*foo_name)->ToObjectChecked();
|
2010-06-07 15:39:10 +00:00
|
|
|
CHECK(func_value->IsJSFunction());
|
|
|
|
Handle<JSFunction> function(JSFunction::cast(func_value));
|
|
|
|
CHECK(function->shared()->is_compiled());
|
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-06-07 15:39:10 +00:00
|
|
|
|
|
|
|
CHECK(function->shared()->is_compiled());
|
2010-08-17 11:44:01 +00:00
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-06-07 15:39:10 +00:00
|
|
|
|
|
|
|
// foo should no longer be in the compilation cache
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(!function->shared()->is_compiled() || function->IsOptimized());
|
|
|
|
CHECK(!function->is_compiled() || function->IsOptimized());
|
2010-06-07 15:39:10 +00:00
|
|
|
// Call foo to get it recompiled.
|
|
|
|
CompileRun("foo()");
|
|
|
|
CHECK(function->shared()->is_compiled());
|
2010-08-17 11:44:01 +00:00
|
|
|
CHECK(function->is_compiled());
|
2010-06-07 15:39:10 +00:00
|
|
|
}
|
2010-10-18 14:59:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Count the number of global contexts in the weak list of global contexts.
|
|
|
|
static int CountGlobalContexts() {
|
|
|
|
int count = 0;
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* object = HEAP->global_contexts_list();
|
2010-10-18 14:59:03 +00:00
|
|
|
while (!object->IsUndefined()) {
|
|
|
|
count++;
|
|
|
|
object = Context::cast(object)->get(Context::NEXT_CONTEXT_LINK);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Count the number of user functions in the weak list of optimized
|
|
|
|
// functions attached to a global context.
|
|
|
|
static int CountOptimizedUserFunctions(v8::Handle<v8::Context> context) {
|
|
|
|
int count = 0;
|
|
|
|
Handle<Context> icontext = v8::Utils::OpenHandle(*context);
|
|
|
|
Object* object = icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST);
|
|
|
|
while (object->IsJSFunction() && !JSFunction::cast(object)->IsBuiltin()) {
|
|
|
|
count++;
|
|
|
|
object = JSFunction::cast(object)->next_function_link();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
TEST(TestInternalWeakLists) {
|
2011-03-18 20:35:07 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
static const int kNumTestContexts = 10;
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Persistent<v8::Context> ctx[kNumTestContexts];
|
|
|
|
|
|
|
|
CHECK_EQ(0, CountGlobalContexts());
|
|
|
|
|
|
|
|
// Create a number of global contests which gets linked together.
|
|
|
|
for (int i = 0; i < kNumTestContexts; i++) {
|
|
|
|
ctx[i] = v8::Context::New();
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2011-04-01 14:38:30 +00:00
|
|
|
bool opt = (FLAG_always_opt && i::V8::UseCrankshaft());
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
CHECK_EQ(i + 1, CountGlobalContexts());
|
|
|
|
|
|
|
|
ctx[i]->Enter();
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Create a handle scope so no function objects get stuch in the outer
|
|
|
|
// handle scope
|
|
|
|
v8::HandleScope scope;
|
|
|
|
const char* source = "function f1() { };"
|
|
|
|
"function f2() { };"
|
|
|
|
"function f3() { };"
|
|
|
|
"function f4() { };"
|
|
|
|
"function f5() { };";
|
|
|
|
CompileRun(source);
|
|
|
|
CHECK_EQ(0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
CompileRun("f1()");
|
|
|
|
CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
CompileRun("f2()");
|
|
|
|
CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
CompileRun("f3()");
|
|
|
|
CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
CompileRun("f4()");
|
|
|
|
CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
CompileRun("f5()");
|
|
|
|
CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
|
|
|
|
// Remove function f1, and
|
|
|
|
CompileRun("f1=null");
|
|
|
|
|
|
|
|
// Scavenge treats these references as strong.
|
|
|
|
for (int j = 0; j < 10; j++) {
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark compact handles the weak references.
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
|
|
|
|
// Get rid of f3 and f5 in the same way.
|
|
|
|
CompileRun("f3=null");
|
|
|
|
for (int j = 0; j < 10; j++) {
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
}
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
CompileRun("f5=null");
|
|
|
|
for (int j = 0; j < 10; j++) {
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
}
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i]));
|
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
ctx[i]->Exit();
|
|
|
|
}
|
|
|
|
|
2010-10-18 17:01:30 +00:00
|
|
|
// Force compilation cache cleanup.
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-10-18 17:01:30 +00:00
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
// Dispose the global contexts one by one.
|
|
|
|
for (int i = 0; i < kNumTestContexts; i++) {
|
|
|
|
ctx[i].Dispose();
|
|
|
|
ctx[i].Clear();
|
|
|
|
|
|
|
|
// Scavenge treats these references as strong.
|
|
|
|
for (int j = 0; j < 10; j++) {
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2010-10-18 14:59:03 +00:00
|
|
|
CHECK_EQ(kNumTestContexts - i, CountGlobalContexts());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark compact handles the weak references.
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-10-18 14:59:03 +00:00
|
|
|
CHECK_EQ(kNumTestContexts - i - 1, CountGlobalContexts());
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(0, CountGlobalContexts());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Count the number of global contexts in the weak list of global contexts
|
|
|
|
// causing a GC after the specified number of elements.
|
|
|
|
static int CountGlobalContextsWithGC(int n) {
|
|
|
|
int count = 0;
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> object(HEAP->global_contexts_list());
|
2010-10-18 14:59:03 +00:00
|
|
|
while (!object->IsUndefined()) {
|
|
|
|
count++;
|
2011-09-19 18:36:47 +00:00
|
|
|
if (count == n) HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-10-18 14:59:03 +00:00
|
|
|
object =
|
|
|
|
Handle<Object>(Context::cast(*object)->get(Context::NEXT_CONTEXT_LINK));
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Count the number of user functions in the weak list of optimized
|
|
|
|
// functions attached to a global context causing a GC after the
|
|
|
|
// specified number of elements.
|
|
|
|
static int CountOptimizedUserFunctionsWithGC(v8::Handle<v8::Context> context,
|
|
|
|
int n) {
|
|
|
|
int count = 0;
|
|
|
|
Handle<Context> icontext = v8::Utils::OpenHandle(*context);
|
|
|
|
Handle<Object> object(icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST));
|
|
|
|
while (object->IsJSFunction() &&
|
|
|
|
!Handle<JSFunction>::cast(object)->IsBuiltin()) {
|
|
|
|
count++;
|
2011-09-19 18:36:47 +00:00
|
|
|
if (count == n) HEAP->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-12-07 11:31:57 +00:00
|
|
|
object = Handle<Object>(
|
|
|
|
Object::cast(JSFunction::cast(*object)->next_function_link()));
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
TEST(TestInternalWeakListsTraverseWithGC) {
|
2011-03-18 20:35:07 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2010-10-18 14:59:03 +00:00
|
|
|
static const int kNumTestContexts = 10;
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Persistent<v8::Context> ctx[kNumTestContexts];
|
|
|
|
|
|
|
|
CHECK_EQ(0, CountGlobalContexts());
|
|
|
|
|
|
|
|
// Create an number of contexts and check the length of the weak list both
|
|
|
|
// with and without GCs while iterating the list.
|
|
|
|
for (int i = 0; i < kNumTestContexts; i++) {
|
|
|
|
ctx[i] = v8::Context::New();
|
|
|
|
CHECK_EQ(i + 1, CountGlobalContexts());
|
|
|
|
CHECK_EQ(i + 1, CountGlobalContextsWithGC(i / 2 + 1));
|
2010-12-07 11:01:02 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2011-04-01 14:38:30 +00:00
|
|
|
bool opt = (FLAG_always_opt && i::V8::UseCrankshaft());
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Compile a number of functions the length of the weak list of optimized
|
|
|
|
// functions both with and without GCs while iterating the list.
|
|
|
|
ctx[0]->Enter();
|
|
|
|
const char* source = "function f1() { };"
|
|
|
|
"function f2() { };"
|
|
|
|
"function f3() { };"
|
|
|
|
"function f4() { };"
|
|
|
|
"function f5() { };";
|
|
|
|
CompileRun(source);
|
|
|
|
CHECK_EQ(0, CountOptimizedUserFunctions(ctx[0]));
|
|
|
|
CompileRun("f1()");
|
|
|
|
CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[0]));
|
|
|
|
CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1));
|
|
|
|
CompileRun("f2()");
|
|
|
|
CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[0]));
|
|
|
|
CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1));
|
|
|
|
CompileRun("f3()");
|
|
|
|
CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[0]));
|
|
|
|
CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1));
|
|
|
|
CompileRun("f4()");
|
|
|
|
CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[0]));
|
|
|
|
CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 2));
|
|
|
|
CompileRun("f5()");
|
|
|
|
CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[0]));
|
|
|
|
CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 4));
|
|
|
|
|
|
|
|
ctx[0]->Exit();
|
2010-10-18 14:59:03 +00:00
|
|
|
}
|
2010-11-15 10:38:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(TestSizeOfObjectsVsHeapIteratorPrecision) {
|
|
|
|
InitializeVM();
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->EnsureHeapIsIterable();
|
2011-03-18 20:35:07 +00:00
|
|
|
intptr_t size_of_objects_1 = HEAP->SizeOfObjects();
|
2011-09-19 18:36:47 +00:00
|
|
|
HeapIterator iterator;
|
2010-11-15 10:38:24 +00:00
|
|
|
intptr_t size_of_objects_2 = 0;
|
|
|
|
for (HeapObject* obj = iterator.next();
|
|
|
|
obj != NULL;
|
|
|
|
obj = iterator.next()) {
|
|
|
|
size_of_objects_2 += obj->Size();
|
|
|
|
}
|
2011-09-19 18:36:47 +00:00
|
|
|
// Delta must be within 5% of the larger result.
|
|
|
|
// TODO(gc): Tighten this up by distinguishing between byte
|
|
|
|
// arrays that are real and those that merely mark free space
|
|
|
|
// on the heap.
|
2010-11-15 10:38:24 +00:00
|
|
|
if (size_of_objects_1 > size_of_objects_2) {
|
|
|
|
intptr_t delta = size_of_objects_1 - size_of_objects_2;
|
|
|
|
PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, "
|
|
|
|
"Iterator: %" V8_PTR_PREFIX "d, "
|
|
|
|
"delta: %" V8_PTR_PREFIX "d\n",
|
|
|
|
size_of_objects_1, size_of_objects_2, delta);
|
2011-09-19 18:36:47 +00:00
|
|
|
CHECK_GT(size_of_objects_1 / 20, delta);
|
2010-11-15 10:38:24 +00:00
|
|
|
} else {
|
|
|
|
intptr_t delta = size_of_objects_2 - size_of_objects_1;
|
|
|
|
PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, "
|
|
|
|
"Iterator: %" V8_PTR_PREFIX "d, "
|
|
|
|
"delta: %" V8_PTR_PREFIX "d\n",
|
|
|
|
size_of_objects_1, size_of_objects_2, delta);
|
2011-09-19 18:36:47 +00:00
|
|
|
CHECK_GT(size_of_objects_2 / 20, delta);
|
2010-11-15 10:38:24 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-21 10:49:40 +00:00
|
|
|
|
|
|
|
|
2011-09-20 13:37:40 +00:00
|
|
|
TEST(GrowAndShrinkNewSpace) {
|
|
|
|
InitializeVM();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
NewSpace* new_space = HEAP->new_space();
|
|
|
|
|
|
|
|
// Explicitly growing should double the space capacity.
|
|
|
|
int old_capacity, new_capacity;
|
|
|
|
old_capacity = new_space->Capacity();
|
|
|
|
new_space->Grow();
|
|
|
|
new_capacity = new_space->Capacity();
|
|
|
|
ASSERT_EQ(2 * old_capacity, new_capacity);
|
|
|
|
|
2011-09-20 15:35:36 +00:00
|
|
|
// Fill up new space to the point that it is almost full.
|
|
|
|
while (new_space->SizeAsInt() + FixedArray::SizeFor(1000) < new_capacity) {
|
2011-09-20 13:37:40 +00:00
|
|
|
ASSERT(HEAP->InNewSpace(*FACTORY->NewFixedArray(1000, NOT_TENURED)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly shrinking should not affect space capacity.
|
|
|
|
old_capacity = new_space->Capacity();
|
|
|
|
new_space->Shrink();
|
|
|
|
new_capacity = new_space->Capacity();
|
|
|
|
ASSERT_EQ(old_capacity, new_capacity);
|
|
|
|
|
|
|
|
// Perform scavenge to empty the new space.
|
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
|
|
|
ASSERT_LE(new_space->SizeAsInt(), old_capacity);
|
|
|
|
|
|
|
|
// Explicitly shrinking should halve the space capacity.
|
|
|
|
old_capacity = new_space->Capacity();
|
|
|
|
new_space->Shrink();
|
|
|
|
new_capacity = new_space->Capacity();
|
|
|
|
ASSERT_EQ(old_capacity, 2 * new_capacity);
|
|
|
|
|
|
|
|
// Consecutive shrinking should not affect space capacity.
|
|
|
|
old_capacity = new_space->Capacity();
|
|
|
|
new_space->Shrink();
|
|
|
|
new_space->Shrink();
|
|
|
|
new_space->Shrink();
|
|
|
|
new_capacity = new_space->Capacity();
|
|
|
|
ASSERT_EQ(old_capacity, new_capacity);
|
|
|
|
}
|