// Copyright 2007-2008 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 #include "src/v8.h" #include "src/heap/heap-inl.h" #include "src/heap/heap.h" #include "test/cctest/cctest.h" using namespace v8; namespace { enum Expectations { EXPECT_RESULT, EXPECT_EXCEPTION, EXPECT_ERROR }; // A DeclarationContext holds a reference to a v8::Context and keeps // track of various declaration related counters to make it easier to // track if global declarations in the presence of interceptors behave // the right way. class DeclarationContext { public: DeclarationContext(); virtual ~DeclarationContext() { if (is_initialized_) { Isolate* isolate = CcTest::isolate(); HandleScope scope(isolate); Local context = Local::New(isolate, context_); context->Exit(); context_.Reset(); } } void Check(const char* source, int get, int set, int has, Expectations expectations, v8::Local value = Local()); int get_count() const { return get_count_; } int set_count() const { return set_count_; } int query_count() const { return query_count_; } protected: virtual v8::Local Get(Local key); virtual v8::Local Set(Local key, Local value); virtual v8::Local Query(Local key); void InitializeIfNeeded(); // Perform optional initialization steps on the context after it has // been created. Defaults to none but may be overwritten. virtual void PostInitializeContext(Local context) {} // Get the holder for the interceptor. Default to the instance template // but may be overwritten. virtual Local GetHolder(Local function) { return function->InstanceTemplate(); } // The handlers are called as static functions that forward // to the instance specific virtual methods. static void HandleGet(Local key, const v8::PropertyCallbackInfo& info); static void HandleSet(Local key, Local value, const v8::PropertyCallbackInfo& info); static void HandleQuery(Local key, const v8::PropertyCallbackInfo& info); v8::Isolate* isolate() const { return CcTest::isolate(); } private: bool is_initialized_; Persistent context_; int get_count_; int set_count_; int query_count_; static DeclarationContext* GetInstance(Local data); }; DeclarationContext::DeclarationContext() : is_initialized_(false), get_count_(0), set_count_(0), query_count_(0) { // Do nothing. } void DeclarationContext::InitializeIfNeeded() { if (is_initialized_) return; Isolate* isolate = CcTest::isolate(); HandleScope scope(isolate); Local function = FunctionTemplate::New(isolate); Local data = External::New(CcTest::isolate(), this); GetHolder(function)->SetHandler(v8::NamedPropertyHandlerConfiguration( &HandleGet, &HandleSet, &HandleQuery, 0, 0, data)); Local context = Context::New(isolate, 0, function->InstanceTemplate(), Local()); context_.Reset(isolate, context); context->Enter(); is_initialized_ = true; // Reset counts. Bootstrapping might have called into the interceptor. get_count_ = 0; set_count_ = 0; query_count_ = 0; PostInitializeContext(context); } void DeclarationContext::Check(const char* source, int get, int set, int query, Expectations expectations, v8::Local value) { InitializeIfNeeded(); // A retry after a GC may pollute the counts, so perform gc now // to avoid that. CcTest::CollectGarbage(v8::internal::NEW_SPACE); HandleScope scope(CcTest::isolate()); TryCatch catcher(CcTest::isolate()); catcher.SetVerbose(true); Local context = CcTest::isolate()->GetCurrentContext(); MaybeLocal