Store Object.observe state per-isolate rather than per-context

This requires adding a new JSObject to the strong root list and populating it from
object-observe.js. The main other change is that we now directly use ObjectHashTable
from JS rather than using WeakMap, since using the latter would end up leaking whichever
Context initialized that observation state.

Added a test via the API showing that different contexts all end up working on the same state.

Review URL: https://codereview.chromium.org/11274014
Patch from Adam Klein <adamk@chromium.org>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12873 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent b22aacc6
......@@ -4144,7 +4144,7 @@ class Internals {
static const int kNullValueRootIndex = 7;
static const int kTrueValueRootIndex = 8;
static const int kFalseValueRootIndex = 9;
static const int kEmptySymbolRootIndex = 118;
static const int kEmptySymbolRootIndex = 119;
static const int kJSObjectType = 0xaa;
static const int kFirstNonstringType = 0x80;
......
......@@ -2852,6 +2852,15 @@ bool Heap::CreateInitialObjects() {
}
set_natives_source_cache(FixedArray::cast(obj));
// Allocate object to hold object observation state.
{ MaybeObject* maybe_obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
if (!maybe_obj->ToObject(&obj)) return false;
}
{ MaybeObject* maybe_obj = AllocateJSObjectFromMap(Map::cast(obj));
if (!maybe_obj->ToObject(&obj)) return false;
}
set_observation_state(JSObject::cast(obj));
// Handling of script id generation is in FACTORY->NewScript.
set_last_script_id(undefined_value());
......
......@@ -155,7 +155,8 @@ namespace internal {
V(Smi, construct_stub_deopt_pc_offset, ConstructStubDeoptPCOffset) \
V(Smi, getter_stub_deopt_pc_offset, GetterStubDeoptPCOffset) \
V(Smi, setter_stub_deopt_pc_offset, SetterStubDeoptPCOffset) \
V(Map, external_map, ExternalMap)
V(Map, external_map, ExternalMap) \
V(JSObject, observation_state, ObservationState)
#define ROOT_LIST(V) \
STRONG_ROOT_LIST(V) \
......
......@@ -30,21 +30,31 @@
var InternalObjectIsFrozen = $Object.isFrozen;
var InternalObjectFreeze = $Object.freeze;
var InternalWeakMapProto = {
__proto__: null,
set: $WeakMap.prototype.set,
get: $WeakMap.prototype.get,
has: $WeakMap.prototype.has
var observationState = %GetObservationState();
if (IS_UNDEFINED(observationState.observerInfoMap)) {
observationState.observerInfoMap = %CreateObjectHashTable();
observationState.objectInfoMap = %CreateObjectHashTable();
}
function createInternalWeakMap() {
var map = new $WeakMap;
map.__proto__ = InternalWeakMapProto;
return map;
function InternalObjectHashTable(table) {
this.table = table;
}
var observerInfoMap = createInternalWeakMap();
var objectInfoMap = createInternalWeakMap();
InternalObjectHashTable.prototype = {
get: function(key) {
return %ObjectHashTableGet(this.table, key);
},
set: function(key, value) {
return %ObjectHashTableSet(this.table, key, value);
},
has: function(key) {
return %ObjectHashTableHas(this.table, key);
}
};
var observerInfoMap = new InternalObjectHashTable(
observationState.observerInfoMap);
var objectInfoMap = new InternalObjectHashTable(observationState.objectInfoMap);
function ObjectObserve(object, callback) {
if (!IS_SPEC_OBJECT(object))
......
......@@ -726,6 +726,11 @@ bool Object::IsMapCache() {
}
bool Object::IsObjectHashTable() {
return IsHashTable();
}
bool Object::IsPrimitive() {
return IsOddball() || IsNumber() || IsString();
}
......
......@@ -873,6 +873,7 @@ class MaybeObject BASE_EMBEDDED {
V(UndetectableObject) \
V(AccessCheckNeeded) \
V(JSGlobalPropertyCell) \
V(ObjectHashTable) \
class JSReceiver;
......
......@@ -13245,6 +13245,49 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SetIsObserved) {
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_GetObservationState) {
ASSERT(args.length() == 0);
return isolate->heap()->observation_state();
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectHashTable) {
ASSERT(args.length() == 0);
return ObjectHashTable::Allocate(0);
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_ObjectHashTableGet) {
NoHandleAllocation ha;
ASSERT(args.length() == 2);
CONVERT_ARG_CHECKED(ObjectHashTable, table, 0);
Object* key = args[1];
Object* lookup = table->Lookup(key);
return lookup->IsTheHole() ? isolate->heap()->undefined_value() : lookup;
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_ObjectHashTableSet) {
HandleScope scope(isolate);
ASSERT(args.length() == 3);
CONVERT_ARG_HANDLE_CHECKED(ObjectHashTable, table, 0);
Handle<Object> key = args.at<Object>(1);
Handle<Object> value = args.at<Object>(2);
PutIntoObjectHashTable(table, key, value);
return isolate->heap()->undefined_value();
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_ObjectHashTableHas) {
NoHandleAllocation ha;
ASSERT(args.length() == 2);
CONVERT_ARG_CHECKED(ObjectHashTable, table, 0);
Object* key = args[1];
Object* lookup = table->Lookup(key);
return isolate->heap()->ToBoolean(!lookup->IsTheHole());
}
// ----------------------------------------------------------------------------
// Implementation of Runtime
......
......@@ -320,6 +320,11 @@ namespace internal {
/* Harmony observe */ \
F(IsObserved, 1, 1) \
F(SetIsObserved, 2, 1) \
F(GetObservationState, 0, 1) \
F(CreateObjectHashTable, 0, 1) \
F(ObjectHashTableGet, 2, 1) \
F(ObjectHashTableSet, 3, 1) \
F(ObjectHashTableHas, 2, 1) \
\
/* Statements */ \
F(NewClosure, 3, 1) \
......
......@@ -79,6 +79,7 @@
'test-lockers.cc',
'test-log.cc',
'test-mark-compact.cc',
'test-object-observe.cc',
'test-parsing.cc',
'test-platform-tls.cc',
'test-profile-generator.cc',
......
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "v8.h"
#include "cctest.h"
using namespace v8;
namespace {
// Need to create a new isolate when FLAG_harmony_observation is on.
class HarmonyIsolate {
public:
HarmonyIsolate() {
i::FLAG_harmony_observation = true;
isolate_ = Isolate::New();
isolate_->Enter();
}
~HarmonyIsolate() {
isolate_->Exit();
isolate_->Dispose();
}
private:
Isolate* isolate_;
};
}
TEST(PerIsolateState) {
HarmonyIsolate isolate;
HandleScope scope;
LocalContext context1;
CompileRun(
"var count = 0;"
"var calls = 0;"
"var observer = function(records) { count = records.length; calls++ };"
"var obj = {};"
"Object.observe(obj, observer);"
"Object.notify(obj, {type: 'a'});");
Handle<Value> observer = CompileRun("observer");
Handle<Value> obj = CompileRun("obj");
{
LocalContext context2;
context2->Global()->Set(String::New("obj"), obj);
CompileRun("Object.notify(obj, {type: 'b'});");
}
{
LocalContext context3;
context3->Global()->Set(String::New("obj"), obj);
CompileRun("Object.notify(obj, {type: 'c'});");
}
{
LocalContext context4;
context4->Global()->Set(String::New("observer"), observer);
CompileRun("Object.deliverChangeRecords(observer)");
}
CHECK_EQ(1, CompileRun("calls")->Int32Value());
CHECK_EQ(3, CompileRun("count")->Int32Value());
}
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