2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2007-2008 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// 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 <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "heap.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
|
|
|
|
enum Expectations {
|
|
|
|
EXPECT_RESULT,
|
2012-08-23 16:38:15 +00:00
|
|
|
EXPECT_EXCEPTION,
|
|
|
|
EXPECT_ERROR
|
2008-08-22 13:33:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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_) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::isolate();
|
2013-05-28 11:54:52 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
Local<Context> context = Local<Context>::New(isolate, context_);
|
|
|
|
context->Exit();
|
2013-09-03 07:34:34 +00:00
|
|
|
context_.Dispose();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check(const char* source,
|
|
|
|
int get, int set, int has,
|
|
|
|
Expectations expectations,
|
|
|
|
v8::Handle<Value> value = Local<Value>());
|
|
|
|
|
|
|
|
int get_count() const { return get_count_; }
|
|
|
|
int set_count() const { return set_count_; }
|
2010-06-04 11:49:44 +00:00
|
|
|
int query_count() const { return query_count_; }
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual v8::Handle<Value> Get(Local<String> key);
|
|
|
|
virtual v8::Handle<Value> Set(Local<String> key, Local<Value> value);
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
void InitializeIfNeeded();
|
|
|
|
|
2012-06-21 11:31:30 +00:00
|
|
|
// Perform optional initialization steps on the context after it has
|
|
|
|
// been created. Defaults to none but may be overwritten.
|
|
|
|
virtual void PostInitializeContext(Handle<Context> context) {}
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
// Get the holder for the interceptor. Default to the instance template
|
|
|
|
// but may be overwritten.
|
|
|
|
virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
|
|
|
|
return function->InstanceTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The handlers are called as static functions that forward
|
|
|
|
// to the instance specific virtual methods.
|
2013-06-20 12:28:27 +00:00
|
|
|
static void HandleGet(Local<String> key,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info);
|
|
|
|
static void HandleSet(Local<String> key,
|
|
|
|
Local<Value> value,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info);
|
|
|
|
static void HandleQuery(Local<String> key,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Integer>& info);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_initialized_;
|
|
|
|
Persistent<Context> context_;
|
|
|
|
|
|
|
|
int get_count_;
|
|
|
|
int set_count_;
|
2010-06-04 11:49:44 +00:00
|
|
|
int query_count_;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
static DeclarationContext* GetInstance(Local<Value> data);
|
2008-08-22 13:33:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DeclarationContext::DeclarationContext()
|
2010-06-04 11:49:44 +00:00
|
|
|
: is_initialized_(false), get_count_(0), set_count_(0), query_count_(0) {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DeclarationContext::InitializeIfNeeded() {
|
|
|
|
if (is_initialized_) return;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
HandleScope scope(isolate);
|
2008-08-22 13:33:59 +00:00
|
|
|
Local<FunctionTemplate> function = FunctionTemplate::New();
|
2009-08-18 12:43:36 +00:00
|
|
|
Local<Value> data = External::New(this);
|
2008-08-22 13:33:59 +00:00
|
|
|
GetHolder(function)->SetNamedPropertyHandler(&HandleGet,
|
|
|
|
&HandleSet,
|
2010-06-04 11:49:44 +00:00
|
|
|
&HandleQuery,
|
2008-08-22 13:33:59 +00:00
|
|
|
0, 0,
|
|
|
|
data);
|
2013-05-28 11:54:52 +00:00
|
|
|
Local<Context> context = Context::New(isolate,
|
|
|
|
0,
|
|
|
|
function->InstanceTemplate(),
|
|
|
|
Local<Value>());
|
|
|
|
context_.Reset(isolate, context);
|
|
|
|
context->Enter();
|
2008-08-22 13:33:59 +00:00
|
|
|
is_initialized_ = true;
|
2013-05-28 11:54:52 +00:00
|
|
|
PostInitializeContext(context);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DeclarationContext::Check(const char* source,
|
2010-06-04 11:49:44 +00:00
|
|
|
int get, int set, int query,
|
2008-08-22 13:33:59 +00:00
|
|
|
Expectations expectations,
|
|
|
|
v8::Handle<Value> value) {
|
|
|
|
InitializeIfNeeded();
|
|
|
|
// A retry after a GC may pollute the counts, so perform gc now
|
|
|
|
// to avoid that.
|
2013-09-19 09:46:15 +00:00
|
|
|
CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE);
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
TryCatch catcher;
|
|
|
|
catcher.SetVerbose(true);
|
2012-08-23 16:38:15 +00:00
|
|
|
Local<Script> script = Script::Compile(String::New(source));
|
|
|
|
if (expectations == EXPECT_ERROR) {
|
|
|
|
CHECK(script.IsEmpty());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!script.IsEmpty());
|
|
|
|
Local<Value> result = script->Run();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(get, get_count());
|
|
|
|
CHECK_EQ(set, set_count());
|
2010-06-04 11:49:44 +00:00
|
|
|
CHECK_EQ(query, query_count());
|
2008-08-22 13:33:59 +00:00
|
|
|
if (expectations == EXPECT_RESULT) {
|
|
|
|
CHECK(!catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
CHECK_EQ(value, result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK(expectations == EXPECT_EXCEPTION);
|
|
|
|
CHECK(catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
CHECK_EQ(value, catcher.Exception());
|
|
|
|
}
|
|
|
|
}
|
2013-09-19 09:46:15 +00:00
|
|
|
// Clean slate for the next test.
|
|
|
|
CcTest::heap()->CollectAllAvailableGarbage();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DeclarationContext::HandleGet(
|
|
|
|
Local<String> key,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
|
|
|
DeclarationContext* context = GetInstance(info.Data());
|
2008-08-22 13:33:59 +00:00
|
|
|
context->get_count_++;
|
2013-06-20 12:28:27 +00:00
|
|
|
info.GetReturnValue().Set(context->Get(key));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DeclarationContext::HandleSet(
|
|
|
|
Local<String> key,
|
|
|
|
Local<Value> value,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
|
|
|
DeclarationContext* context = GetInstance(info.Data());
|
2008-08-22 13:33:59 +00:00
|
|
|
context->set_count_++;
|
2013-06-20 12:28:27 +00:00
|
|
|
info.GetReturnValue().Set(context->Set(key, value));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DeclarationContext::HandleQuery(
|
|
|
|
Local<String> key,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Integer>& info) {
|
|
|
|
DeclarationContext* context = GetInstance(info.Data());
|
2010-06-04 11:49:44 +00:00
|
|
|
context->query_count_++;
|
2013-06-20 12:28:27 +00:00
|
|
|
info.GetReturnValue().Set(context->Query(key));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
DeclarationContext* DeclarationContext::GetInstance(Local<Value> data) {
|
|
|
|
void* value = Local<External>::Cast(data)->Value();
|
2012-11-13 12:27:03 +00:00
|
|
|
return static_cast<DeclarationContext*>(value);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<Value> DeclarationContext::Get(Local<String> key) {
|
|
|
|
return v8::Handle<Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<Value> DeclarationContext::Set(Local<String> key,
|
|
|
|
Local<Value> value) {
|
|
|
|
return v8::Handle<Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-04 11:49:44 +00:00
|
|
|
v8::Handle<Integer> DeclarationContext::Query(Local<String> key) {
|
|
|
|
return v8::Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test global declaration of a property the interceptor doesn't know
|
|
|
|
// about and doesn't handle.
|
|
|
|
TEST(Unknown) {
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
|
|
|
1, // declaration
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
2011-02-17 21:04:53 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
0,
|
|
|
|
EXPECT_RESULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("const x; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // declaration
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("const x = 0; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // declaration
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Undefined()); // SB 0 - BUG 1213579
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PresentPropertyContext: public DeclarationContext {
|
|
|
|
protected:
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
|
|
|
return Integer::New(v8::None);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Present) {
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ PresentPropertyContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
|
|
|
0,
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_EXCEPTION); // x is not defined!
|
|
|
|
}
|
|
|
|
|
|
|
|
{ PresentPropertyContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
|
|
|
1, // initialization
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ PresentPropertyContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
2011-02-17 21:04:53 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
0,
|
|
|
|
EXPECT_RESULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ PresentPropertyContext context;
|
|
|
|
context.Check("const x; x",
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // access
|
|
|
|
1, // initialization
|
2011-09-14 08:08:16 +00:00
|
|
|
1, // (re-)declaration
|
2011-09-15 12:00:30 +00:00
|
|
|
EXPECT_RESULT, Undefined());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ PresentPropertyContext context;
|
|
|
|
context.Check("const x = 0; x",
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // access
|
|
|
|
1, // initialization
|
2011-09-14 08:08:16 +00:00
|
|
|
1, // (re-)declaration
|
2011-09-15 12:00:30 +00:00
|
|
|
EXPECT_RESULT, Number::New(0));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AbsentPropertyContext: public DeclarationContext {
|
|
|
|
protected:
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
|
|
|
return v8::Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Absent) {
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
2010-06-04 11:49:44 +00:00
|
|
|
1, // declaration
|
2008-08-22 13:33:59 +00:00
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
2011-02-17 21:04:53 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
0,
|
|
|
|
EXPECT_RESULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("const x; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // declaration
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("const x = 0; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // declaration
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Undefined()); // SB 0 - BUG 1213579
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("if (false) { var x = 0 }; x",
|
|
|
|
1, // access
|
|
|
|
1, // declaration
|
|
|
|
1, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AppearingPropertyContext: public DeclarationContext {
|
|
|
|
public:
|
|
|
|
enum State {
|
|
|
|
DECLARE,
|
|
|
|
INITIALIZE_IF_ASSIGN,
|
|
|
|
UNKNOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
AppearingPropertyContext() : state_(DECLARE) { }
|
|
|
|
|
|
|
|
protected:
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
2008-08-22 13:33:59 +00:00
|
|
|
switch (state_) {
|
|
|
|
case DECLARE:
|
|
|
|
// Force declaration by returning that the
|
|
|
|
// property is absent.
|
|
|
|
state_ = INITIALIZE_IF_ASSIGN;
|
2010-06-04 11:49:44 +00:00
|
|
|
return Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
case INITIALIZE_IF_ASSIGN:
|
|
|
|
// Return that the property is present so we only get the
|
|
|
|
// setter called when initializing with a value.
|
|
|
|
state_ = UNKNOWN;
|
2010-06-04 11:49:44 +00:00
|
|
|
return Integer::New(v8::None);
|
2008-08-22 13:33:59 +00:00
|
|
|
default:
|
2009-04-24 14:29:42 +00:00
|
|
|
CHECK(state_ == UNKNOWN);
|
2008-08-22 13:33:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Do the lookup in the object.
|
2010-06-04 11:49:44 +00:00
|
|
|
return v8::Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
State state_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Appearing) {
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
|
|
|
1, // declaration
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
|
|
|
2, // declaration + initialization
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
2011-02-17 21:04:53 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
0,
|
|
|
|
EXPECT_RESULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("const x; x",
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // access
|
2008-08-22 13:33:59 +00:00
|
|
|
2, // declaration + initialization
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // declaration
|
|
|
|
EXPECT_RESULT, Undefined());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("const x = 0; x",
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // access
|
2008-08-22 13:33:59 +00:00
|
|
|
2, // declaration + initialization
|
2011-09-15 12:00:30 +00:00
|
|
|
1, // declaration
|
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
// Result is undefined because declaration succeeded but
|
|
|
|
// initialization to 0 failed (due to context behavior).
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReappearingPropertyContext: public DeclarationContext {
|
|
|
|
public:
|
|
|
|
enum State {
|
|
|
|
DECLARE,
|
|
|
|
DONT_DECLARE,
|
|
|
|
INITIALIZE,
|
|
|
|
UNKNOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
ReappearingPropertyContext() : state_(DECLARE) { }
|
|
|
|
|
|
|
|
protected:
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
2008-08-22 13:33:59 +00:00
|
|
|
switch (state_) {
|
|
|
|
case DECLARE:
|
|
|
|
// Force the first declaration by returning that
|
|
|
|
// the property is absent.
|
|
|
|
state_ = DONT_DECLARE;
|
2010-06-04 11:49:44 +00:00
|
|
|
return Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
case DONT_DECLARE:
|
|
|
|
// Ignore the second declaration by returning
|
|
|
|
// that the property is already there.
|
|
|
|
state_ = INITIALIZE;
|
2010-06-04 11:49:44 +00:00
|
|
|
return Integer::New(v8::None);
|
2008-08-22 13:33:59 +00:00
|
|
|
case INITIALIZE:
|
|
|
|
// Force an initialization by returning that
|
|
|
|
// the property is absent. This will make sure
|
|
|
|
// that the setter is called and it will not
|
|
|
|
// lead to redeclaration conflicts (yet).
|
|
|
|
state_ = UNKNOWN;
|
2010-06-04 11:49:44 +00:00
|
|
|
return Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
default:
|
2009-04-24 14:29:42 +00:00
|
|
|
CHECK(state_ == UNKNOWN);
|
2008-08-22 13:33:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Do the lookup in the object.
|
2010-06-04 11:49:44 +00:00
|
|
|
return Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
State state_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Reappearing) {
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ ReappearingPropertyContext context;
|
|
|
|
context.Check("const x; var x = 0",
|
|
|
|
0,
|
2011-09-15 12:00:30 +00:00
|
|
|
3, // const declaration+initialization, var initialization
|
|
|
|
3, // 2 x declaration + var initialization
|
|
|
|
EXPECT_RESULT, Undefined());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExistsInPrototypeContext: public DeclarationContext {
|
|
|
|
protected:
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Let it seem that the property exists in the prototype object.
|
2010-06-04 11:49:44 +00:00
|
|
|
return Integer::New(v8::None);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the prototype as the holder for the interceptors.
|
|
|
|
virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
|
|
|
|
return function->PrototypeTemplate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExistsInPrototype) {
|
2012-04-20 13:35:09 +00:00
|
|
|
i::FLAG_es52_globals = true;
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Sanity check to make sure that the holder of the interceptor
|
|
|
|
// really is the prototype object.
|
|
|
|
{ ExistsInPrototypeContext context;
|
|
|
|
context.Check("this.x = 87; this.x",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
EXPECT_RESULT, Number::New(87));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInPrototypeContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
0,
|
2012-06-21 11:31:30 +00:00
|
|
|
0,
|
|
|
|
0,
|
2012-04-16 13:20:50 +00:00
|
|
|
EXPECT_RESULT, Undefined());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInPrototypeContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
0,
|
|
|
|
0,
|
2012-06-21 11:31:30 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInPrototypeContext context;
|
|
|
|
context.Check("const x; x",
|
|
|
|
0,
|
|
|
|
0,
|
2012-06-21 11:31:30 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInPrototypeContext context;
|
|
|
|
context.Check("const x = 0; x",
|
|
|
|
0,
|
|
|
|
0,
|
2012-06-21 11:31:30 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AbsentInPrototypeContext: public DeclarationContext {
|
|
|
|
protected:
|
2010-06-04 11:49:44 +00:00
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Let it seem that the property is absent in the prototype object.
|
2010-06-04 11:49:44 +00:00
|
|
|
return Handle<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the prototype as the holder for the interceptors.
|
|
|
|
virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
|
|
|
|
return function->PrototypeTemplate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(AbsentInPrototype) {
|
2012-04-20 13:35:09 +00:00
|
|
|
i::FLAG_es52_globals = true;
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ AbsentInPrototypeContext context;
|
|
|
|
context.Check("if (false) { var x = 0; }; x",
|
|
|
|
0,
|
|
|
|
0,
|
2012-06-21 11:31:30 +00:00
|
|
|
0,
|
2008-08-22 13:33:59 +00:00
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
}
|
2012-06-21 11:31:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExistsInHiddenPrototypeContext: public DeclarationContext {
|
|
|
|
public:
|
|
|
|
ExistsInHiddenPrototypeContext() {
|
|
|
|
hidden_proto_ = FunctionTemplate::New();
|
|
|
|
hidden_proto_->SetHiddenPrototype(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual v8::Handle<Integer> Query(Local<String> key) {
|
|
|
|
// Let it seem that the property exists in the hidden prototype object.
|
|
|
|
return Integer::New(v8::None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install the hidden prototype after the global object has been created.
|
|
|
|
virtual void PostInitializeContext(Handle<Context> context) {
|
|
|
|
Local<Object> global_object = context->Global();
|
|
|
|
Local<Object> hidden_proto = hidden_proto_->GetFunction()->NewInstance();
|
|
|
|
context->DetachGlobal();
|
|
|
|
context->Global()->SetPrototype(hidden_proto);
|
|
|
|
context->ReattachGlobal(global_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the hidden prototype as the holder for the interceptors.
|
|
|
|
virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
|
|
|
|
return hidden_proto_->InstanceTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Local<FunctionTemplate> hidden_proto_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExistsInHiddenPrototype) {
|
|
|
|
i::FLAG_es52_globals = true;
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2012-06-21 11:31:30 +00:00
|
|
|
|
|
|
|
{ ExistsInHiddenPrototypeContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
|
|
|
0,
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_EXCEPTION); // x is not defined!
|
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInHiddenPrototypeContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
|
|
|
1, // initialization
|
|
|
|
2, // declaration + initialization
|
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInHiddenPrototypeContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
EXPECT_RESULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mstarzinger): The semantics of global const is vague.
|
|
|
|
{ ExistsInHiddenPrototypeContext context;
|
|
|
|
context.Check("const x; x",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1, // (re-)declaration
|
|
|
|
EXPECT_RESULT, Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mstarzinger): The semantics of global const is vague.
|
|
|
|
{ ExistsInHiddenPrototypeContext context;
|
|
|
|
context.Check("const x = 0; x",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1, // (re-)declaration
|
|
|
|
EXPECT_RESULT, Number::New(0));
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 16:38:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleContext {
|
|
|
|
public:
|
2013-05-08 07:45:16 +00:00
|
|
|
SimpleContext()
|
2013-09-19 09:17:13 +00:00
|
|
|
: handle_scope_(CcTest::isolate()),
|
|
|
|
context_(Context::New(CcTest::isolate())) {
|
2012-08-23 16:38:15 +00:00
|
|
|
context_->Enter();
|
|
|
|
}
|
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
~SimpleContext() {
|
2012-08-23 16:38:15 +00:00
|
|
|
context_->Exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check(const char* source,
|
|
|
|
Expectations expectations,
|
|
|
|
v8::Handle<Value> value = Local<Value>()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(context_->GetIsolate());
|
2012-08-23 16:38:15 +00:00
|
|
|
TryCatch catcher;
|
|
|
|
catcher.SetVerbose(true);
|
|
|
|
Local<Script> script = Script::Compile(String::New(source));
|
|
|
|
if (expectations == EXPECT_ERROR) {
|
|
|
|
CHECK(script.IsEmpty());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!script.IsEmpty());
|
|
|
|
Local<Value> result = script->Run();
|
|
|
|
if (expectations == EXPECT_RESULT) {
|
|
|
|
CHECK(!catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
CHECK_EQ(value, result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK(expectations == EXPECT_EXCEPTION);
|
|
|
|
CHECK(catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
CHECK_EQ(value, catcher.Exception());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-08 07:45:16 +00:00
|
|
|
HandleScope handle_scope_;
|
|
|
|
Local<Context> context_;
|
2012-08-23 16:38:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
TEST(CrossScriptReferences) {
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2012-08-23 16:38:15 +00:00
|
|
|
|
|
|
|
{ SimpleContext context;
|
|
|
|
context.Check("var x = 1; x",
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("var x = 2; x",
|
|
|
|
EXPECT_RESULT, Number::New(2));
|
|
|
|
context.Check("const x = 3; x",
|
|
|
|
EXPECT_RESULT, Number::New(3));
|
|
|
|
context.Check("const x = 4; x",
|
|
|
|
EXPECT_RESULT, Number::New(4));
|
|
|
|
context.Check("x = 5; x",
|
|
|
|
EXPECT_RESULT, Number::New(5));
|
|
|
|
context.Check("var x = 6; x",
|
|
|
|
EXPECT_RESULT, Number::New(6));
|
|
|
|
context.Check("this.x",
|
|
|
|
EXPECT_RESULT, Number::New(6));
|
|
|
|
context.Check("function x() { return 7 }; x()",
|
|
|
|
EXPECT_RESULT, Number::New(7));
|
|
|
|
}
|
|
|
|
|
|
|
|
{ SimpleContext context;
|
|
|
|
context.Check("const x = 1; x",
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("var x = 2; x", // assignment ignored
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("const x = 3; x",
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("x = 4; x", // assignment ignored
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("var x = 5; x", // assignment ignored
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("this.x",
|
|
|
|
EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check("function x() { return 7 }; x",
|
|
|
|
EXPECT_EXCEPTION);
|
|
|
|
}
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 16:38:15 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
TEST(CrossScriptReferencesHarmony) {
|
2012-08-23 16:38:15 +00:00
|
|
|
i::FLAG_use_strict = true;
|
|
|
|
i::FLAG_harmony_scoping = true;
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
i::FLAG_harmony_modules = true;
|
2012-08-23 16:38:15 +00:00
|
|
|
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2012-08-28 11:25:08 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
const char* decs[] = {
|
|
|
|
"var x = 1; x", "x", "this.x",
|
|
|
|
"function x() { return 1 }; x()", "x()", "this.x()",
|
|
|
|
"let x = 1; x", "x", "this.x",
|
|
|
|
"const x = 1; x", "x", "this.x",
|
|
|
|
"module x { export let a = 1 }; x.a", "x.a", "this.x.a",
|
|
|
|
NULL
|
|
|
|
};
|
2012-08-23 16:38:15 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
for (int i = 0; decs[i] != NULL; i += 3) {
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(decs[i], EXPECT_RESULT, Number::New(1));
|
|
|
|
context.Check(decs[i+1], EXPECT_RESULT, Number::New(1));
|
2012-08-28 11:25:08 +00:00
|
|
|
// TODO(rossberg): The current ES6 draft spec does not reflect lexical
|
|
|
|
// bindings on the global object. However, this will probably change, in
|
|
|
|
// which case we reactivate the following test.
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
if (i/3 < 2) context.Check(decs[i+2], EXPECT_RESULT, Number::New(1));
|
2012-08-23 16:38:15 +00:00
|
|
|
}
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
}
|
2012-08-23 16:38:15 +00:00
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
TEST(CrossScriptConflicts) {
|
|
|
|
i::FLAG_use_strict = true;
|
|
|
|
i::FLAG_harmony_scoping = true;
|
|
|
|
i::FLAG_harmony_modules = true;
|
2012-08-23 16:38:15 +00:00
|
|
|
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2012-08-23 16:38:15 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
const char* firsts[] = {
|
|
|
|
"var x = 1; x",
|
|
|
|
"function x() { return 1 }; x()",
|
|
|
|
"let x = 1; x",
|
|
|
|
"const x = 1; x",
|
|
|
|
"module x { export let a = 1 }; x.a",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
const char* seconds[] = {
|
|
|
|
"var x = 2; x",
|
|
|
|
"function x() { return 2 }; x()",
|
|
|
|
"let x = 2; x",
|
|
|
|
"const x = 2; x",
|
|
|
|
"module x { export let a = 2 }; x.a",
|
|
|
|
NULL
|
|
|
|
};
|
2012-08-23 16:38:15 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
for (int i = 0; firsts[i] != NULL; ++i) {
|
|
|
|
for (int j = 0; seconds[j] != NULL; ++j) {
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(firsts[i], EXPECT_RESULT, Number::New(1));
|
|
|
|
// TODO(rossberg): All tests should actually be errors in Harmony,
|
|
|
|
// but we currently do not detect the cases where the first declaration
|
|
|
|
// is not lexical.
|
|
|
|
context.Check(seconds[j],
|
|
|
|
i < 2 ? EXPECT_RESULT : EXPECT_ERROR, Number::New(2));
|
|
|
|
}
|
2012-08-23 16:38:15 +00:00
|
|
|
}
|
|
|
|
}
|