// 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 "v8.h" #include "heap.h" #include "cctest.h" using namespace v8; 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::Handle 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::Handle Get(Local key); virtual v8::Handle Set(Local key, Local value); virtual v8::Handle 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(Handle 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); 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)->SetNamedPropertyHandler(&HandleGet, &HandleSet, &HandleQuery, 0, 0, data); Local context = Context::New(isolate, 0, function->InstanceTemplate(), Local()); context_.Reset(isolate, context); context->Enter(); is_initialized_ = true; PostInitializeContext(context); } void DeclarationContext::Check(const char* source, int get, int set, int query, Expectations expectations, v8::Handle value) { InitializeIfNeeded(); // A retry after a GC may pollute the counts, so perform gc now // to avoid that. CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE); HandleScope scope(CcTest::isolate()); TryCatch catcher; catcher.SetVerbose(true); Local