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>
|
|
|
|
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2016-08-10 16:46:16 +00:00
|
|
|
namespace {
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
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-11-22 12:43:17 +00:00
|
|
|
context_.Reset();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
void Check(const char* source, int get, int set, int has,
|
2008-08-22 13:33:59 +00:00
|
|
|
Expectations expectations,
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<Value> value = Local<Value>());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
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:
|
2015-12-07 15:27:40 +00:00
|
|
|
virtual v8::Local<Value> Get(Local<Name> key);
|
|
|
|
virtual v8::Local<Value> Set(Local<Name> key, Local<Value> value);
|
|
|
|
virtual v8::Local<Integer> Query(Local<Name> 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.
|
2015-12-07 15:27:40 +00:00
|
|
|
virtual void PostInitializeContext(Local<Context> context) {}
|
2012-06-21 11:31:30 +00:00
|
|
|
|
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.
|
2014-11-27 10:21:32 +00:00
|
|
|
static void HandleGet(Local<Name> key,
|
2013-06-20 12:28:27 +00:00
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info);
|
2014-11-27 10:21:32 +00:00
|
|
|
static void HandleSet(Local<Name> key, Local<Value> value,
|
2013-06-20 12:28:27 +00:00
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info);
|
2014-11-27 10:21:32 +00:00
|
|
|
static void HandleQuery(Local<Name> key,
|
2013-06-20 12:28:27 +00:00
|
|
|
const v8::PropertyCallbackInfo<v8::Integer>& info);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Isolate* isolate() const { return CcTest::isolate(); }
|
|
|
|
|
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);
|
2013-12-18 10:31:42 +00:00
|
|
|
Local<FunctionTemplate> function = FunctionTemplate::New(isolate);
|
2013-11-12 11:44:58 +00:00
|
|
|
Local<Value> data = External::New(CcTest::isolate(), this);
|
2014-11-27 10:21:32 +00:00
|
|
|
GetHolder(function)->SetHandler(v8::NamedPropertyHandlerConfiguration(
|
2018-09-13 14:55:18 +00:00
|
|
|
&HandleGet, &HandleSet, &HandleQuery, nullptr, nullptr, data));
|
|
|
|
Local<Context> context = Context::New(
|
|
|
|
isolate, nullptr, function->InstanceTemplate(), Local<Value>());
|
2013-05-28 11:54:52 +00:00
|
|
|
context_.Reset(isolate, context);
|
|
|
|
context->Enter();
|
2008-08-22 13:33:59 +00:00
|
|
|
is_initialized_ = true;
|
2015-07-01 12:19:55 +00:00
|
|
|
// Reset counts. Bootstrapping might have called into the interceptor.
|
|
|
|
get_count_ = 0;
|
|
|
|
set_count_ = 0;
|
|
|
|
query_count_ = 0;
|
2013-05-28 11:54:52 +00:00
|
|
|
PostInitializeContext(context);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
void DeclarationContext::Check(const char* source, int get, int set, int query,
|
2008-08-22 13:33:59 +00:00
|
|
|
Expectations expectations,
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<Value> value) {
|
2008-08-22 13:33:59 +00:00
|
|
|
InitializeIfNeeded();
|
|
|
|
// A retry after a GC may pollute the counts, so perform gc now
|
|
|
|
// to avoid that.
|
2016-09-07 10:02:58 +00:00
|
|
|
CcTest::CollectGarbage(v8::internal::NEW_SPACE);
|
2013-09-19 09:17:13 +00:00
|
|
|
HandleScope scope(CcTest::isolate());
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch catcher(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
catcher.SetVerbose(true);
|
2015-12-07 15:27:40 +00:00
|
|
|
Local<Context> context = CcTest::isolate()->GetCurrentContext();
|
|
|
|
MaybeLocal<Script> script = Script::Compile(
|
2020-03-09 10:41:45 +00:00
|
|
|
context, String::NewFromUtf8(CcTest::isolate(), source).ToLocalChecked());
|
2012-08-23 16:38:15 +00:00
|
|
|
if (expectations == EXPECT_ERROR) {
|
|
|
|
CHECK(script.IsEmpty());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!script.IsEmpty());
|
2015-12-07 15:27:40 +00:00
|
|
|
MaybeLocal<Value> result = script.ToLocalChecked()->Run(context);
|
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()) {
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK(value->Equals(context, result.ToLocalChecked()).FromJust());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK(expectations == EXPECT_EXCEPTION);
|
|
|
|
CHECK(catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK(value->Equals(context, catcher.Exception()).FromJust());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-19 09:46:15 +00:00
|
|
|
// Clean slate for the next test.
|
2016-09-07 10:02:58 +00:00
|
|
|
CcTest::CollectAllAvailableGarbage();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DeclarationContext::HandleGet(
|
2014-11-27 10:21:32 +00:00
|
|
|
Local<Name> key, const v8::PropertyCallbackInfo<v8::Value>& info) {
|
2013-06-20 12:28:27 +00:00
|
|
|
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(
|
2014-11-27 10:21:32 +00:00
|
|
|
Local<Name> key, Local<Value> value,
|
2013-06-20 12:28:27 +00:00
|
|
|
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(
|
2014-11-27 10:21:32 +00:00
|
|
|
Local<Name> key, const v8::PropertyCallbackInfo<v8::Integer>& info) {
|
2013-06-20 12:28:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<Value> DeclarationContext::Get(Local<Name> key) {
|
|
|
|
return v8::Local<Value>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<Value> DeclarationContext::Set(Local<Name> key, Local<Value> value) {
|
|
|
|
return v8::Local<Value>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
v8::Local<Integer> DeclarationContext::Query(Local<Name> key) {
|
|
|
|
return v8::Local<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 16:46:16 +00:00
|
|
|
} // namespace
|
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());
|
2013-09-26 08:47:59 +00:00
|
|
|
v8::V8::Initialize();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
2014-07-14 14:01:04 +00:00
|
|
|
0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
2014-07-14 14:01:04 +00:00
|
|
|
1, // initialization
|
|
|
|
0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ DeclarationContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
[runtime] Intercept function declarations.
We used to intercept function definitions, but not declarations.
GenericNamedPropertySetterCallback now also intercepts function declarations.
For definitions, we call DeclareGlobal and then InitializeVarGlobal. For
declarations, we never call InitializeVarGlobal, thus we must check for
interceptors in DeclareGlobal.
If the semantics of a redeclaration are wrong, e.g., redeclaring a read-only
property, an exception is thrown independent of whether an interceptor is
installed. Usually, i.e., not during a declaration, we only throw if
the call is not successfully intercepted.
BUG=v8:5375
Review-Url: https://codereview.chromium.org/2334733002
Cr-Commit-Position: refs/heads/master@{#39450}
2016-09-15 15:47:48 +00:00
|
|
|
1, 1, EXPECT_RESULT);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AbsentPropertyContext: public DeclarationContext {
|
|
|
|
protected:
|
2018-09-14 15:34:02 +00:00
|
|
|
v8::Local<Integer> Query(Local<Name> key) override {
|
2015-12-07 15:27:40 +00:00
|
|
|
return v8::Local<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Absent) {
|
2013-09-26 08:21:48 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-09-26 08:47:59 +00:00
|
|
|
v8::V8::Initialize();
|
2013-09-26 08:21:48 +00:00
|
|
|
HandleScope scope(isolate);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
1, // access
|
2014-07-14 14:01:04 +00:00
|
|
|
0, 0, EXPECT_RESULT, Undefined(isolate));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
2014-07-14 14:01:04 +00:00
|
|
|
1, // initialization
|
|
|
|
0, EXPECT_RESULT, Number::New(isolate, 0));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
[runtime] Intercept function declarations.
We used to intercept function definitions, but not declarations.
GenericNamedPropertySetterCallback now also intercepts function declarations.
For definitions, we call DeclareGlobal and then InitializeVarGlobal. For
declarations, we never call InitializeVarGlobal, thus we must check for
interceptors in DeclareGlobal.
If the semantics of a redeclaration are wrong, e.g., redeclaring a read-only
property, an exception is thrown independent of whether an interceptor is
installed. Usually, i.e., not during a declaration, we only throw if
the call is not successfully intercepted.
BUG=v8:5375
Review-Url: https://codereview.chromium.org/2334733002
Cr-Commit-Position: refs/heads/master@{#39450}
2016-09-15 15:47:48 +00:00
|
|
|
1, 1, EXPECT_RESULT);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ AbsentPropertyContext context;
|
|
|
|
context.Check("if (false) { var x = 0 }; x",
|
|
|
|
1, // access
|
2014-07-14 14:01:04 +00:00
|
|
|
0, 0, EXPECT_RESULT, Undefined(isolate));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AppearingPropertyContext: public DeclarationContext {
|
|
|
|
public:
|
|
|
|
enum State {
|
|
|
|
DECLARE,
|
|
|
|
INITIALIZE_IF_ASSIGN,
|
|
|
|
UNKNOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
AppearingPropertyContext() : state_(DECLARE) { }
|
|
|
|
|
|
|
|
protected:
|
2018-09-14 15:34:02 +00:00
|
|
|
v8::Local<Integer> Query(Local<Name> key) override {
|
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;
|
2015-12-07 15:27:40 +00:00
|
|
|
return Local<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;
|
2014-01-03 14:31:17 +00:00
|
|
|
return Integer::New(isolate(), 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.
|
2015-12-07 15:27:40 +00:00
|
|
|
return v8::Local<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
State state_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Appearing) {
|
2013-09-26 08:47:59 +00:00
|
|
|
v8::V8::Initialize();
|
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
|
2014-07-14 14:01:04 +00:00
|
|
|
0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("var x = 0; x",
|
|
|
|
1, // access
|
2014-07-14 14:01:04 +00:00
|
|
|
1, // initialization
|
|
|
|
0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ AppearingPropertyContext context;
|
|
|
|
context.Check("function x() { }; x",
|
|
|
|
1, // access
|
[runtime] Intercept function declarations.
We used to intercept function definitions, but not declarations.
GenericNamedPropertySetterCallback now also intercepts function declarations.
For definitions, we call DeclareGlobal and then InitializeVarGlobal. For
declarations, we never call InitializeVarGlobal, thus we must check for
interceptors in DeclareGlobal.
If the semantics of a redeclaration are wrong, e.g., redeclaring a read-only
property, an exception is thrown independent of whether an interceptor is
installed. Usually, i.e., not during a declaration, we only throw if
the call is not successfully intercepted.
BUG=v8:5375
Review-Url: https://codereview.chromium.org/2334733002
Cr-Commit-Position: refs/heads/master@{#39450}
2016-09-15 15:47:48 +00:00
|
|
|
1, 1, EXPECT_RESULT);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExistsInPrototypeContext: public DeclarationContext {
|
2014-01-03 14:31:17 +00:00
|
|
|
public:
|
|
|
|
ExistsInPrototypeContext() { InitializeIfNeeded(); }
|
2008-08-22 13:33:59 +00:00
|
|
|
protected:
|
2018-09-14 15:34:02 +00:00
|
|
|
v8::Local<Integer> Query(Local<Name> key) override {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Let it seem that the property exists in the prototype object.
|
2014-01-03 14:31:17 +00:00
|
|
|
return Integer::New(isolate(), v8::None);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the prototype as the holder for the interceptors.
|
2018-09-14 15:34:02 +00:00
|
|
|
Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) override {
|
2008-08-22 13:33:59 +00:00
|
|
|
return function->PrototypeTemplate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExistsInPrototype) {
|
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;
|
2014-07-18 13:47:25 +00:00
|
|
|
context.Check("this.x = 87; this.x", 0, 0, 1, EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 87));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ ExistsInPrototypeContext context;
|
|
|
|
context.Check("var x; x",
|
|
|
|
0,
|
2012-06-21 11:31:30 +00:00
|
|
|
0,
|
|
|
|
0,
|
2013-09-26 08:21:48 +00:00
|
|
|
EXPECT_RESULT, Undefined(CcTest::isolate()));
|
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,
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AbsentInPrototypeContext: public DeclarationContext {
|
|
|
|
protected:
|
2018-09-14 15:34:02 +00:00
|
|
|
v8::Local<Integer> Query(Local<Name> key) override {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Let it seem that the property is absent in the prototype object.
|
2015-12-07 15:27:40 +00:00
|
|
|
return Local<Integer>();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the prototype as the holder for the interceptors.
|
2018-09-14 15:34:02 +00:00
|
|
|
Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) override {
|
2008-08-22 13:33:59 +00:00
|
|
|
return function->PrototypeTemplate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(AbsentInPrototype) {
|
2013-09-26 08:47:59 +00:00
|
|
|
v8::V8::Initialize();
|
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,
|
2013-09-26 08:21:48 +00:00
|
|
|
EXPECT_RESULT, Undefined(CcTest::isolate()));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-21 11:31:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-12-07 15:27:40 +00:00
|
|
|
void Check(const char* source, Expectations expectations,
|
|
|
|
v8::Local<Value> value = Local<Value>()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(context_->GetIsolate());
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch catcher(context_->GetIsolate());
|
2012-08-23 16:38:15 +00:00
|
|
|
catcher.SetVerbose(true);
|
2015-12-07 15:27:40 +00:00
|
|
|
MaybeLocal<Script> script = Script::Compile(
|
2020-03-09 10:41:45 +00:00
|
|
|
context_,
|
|
|
|
String::NewFromUtf8(context_->GetIsolate(), source).ToLocalChecked());
|
2012-08-23 16:38:15 +00:00
|
|
|
if (expectations == EXPECT_ERROR) {
|
|
|
|
CHECK(script.IsEmpty());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!script.IsEmpty());
|
2015-12-07 15:27:40 +00:00
|
|
|
MaybeLocal<Value> result = script.ToLocalChecked()->Run(context_);
|
2012-08-23 16:38:15 +00:00
|
|
|
if (expectations == EXPECT_RESULT) {
|
|
|
|
CHECK(!catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK(value->Equals(context_, result.ToLocalChecked()).FromJust());
|
2012-08-23 16:38:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK(expectations == EXPECT_EXCEPTION);
|
|
|
|
CHECK(catcher.HasCaught());
|
|
|
|
if (!value.IsEmpty()) {
|
2015-12-07 15:27:40 +00:00
|
|
|
CHECK(value->Equals(context_, catcher.Exception()).FromJust());
|
2012-08-23 16:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
2012-08-23 16:38:15 +00:00
|
|
|
|
|
|
|
{ SimpleContext context;
|
|
|
|
context.Check("var x = 1; x",
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(isolate, 1));
|
2012-08-23 16:38:15 +00:00
|
|
|
context.Check("var x = 2; x",
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(isolate, 2));
|
2012-08-23 16:38:15 +00:00
|
|
|
context.Check("x = 5; x",
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(isolate, 5));
|
2012-08-23 16:38:15 +00:00
|
|
|
context.Check("var x = 6; x",
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(isolate, 6));
|
2012-08-23 16:38:15 +00:00
|
|
|
context.Check("this.x",
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(isolate, 6));
|
2012-08-23 16:38:15 +00:00
|
|
|
context.Check("function x() { return 7 }; x()",
|
2014-01-03 14:31:17 +00:00
|
|
|
EXPECT_RESULT, Number::New(isolate, 7));
|
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
|
|
|
|
2014-11-07 16:29:13 +00:00
|
|
|
TEST(CrossScriptReferences_Simple) {
|
|
|
|
i::FLAG_use_strict = true;
|
|
|
|
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check("let x = 1; x", EXPECT_RESULT, Number::New(isolate, 1));
|
|
|
|
context.Check("let x = 5; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CrossScriptReferences_Simple2) {
|
|
|
|
i::FLAG_use_strict = true;
|
|
|
|
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
for (int k = 0; k < 100; k++) {
|
|
|
|
SimpleContext context;
|
|
|
|
bool cond = (k % 2) == 0;
|
|
|
|
if (cond) {
|
|
|
|
context.Check("let x = 1; x", EXPECT_RESULT, Number::New(isolate, 1));
|
|
|
|
context.Check("let z = 4; z", EXPECT_RESULT, Number::New(isolate, 4));
|
|
|
|
} else {
|
|
|
|
context.Check("let z = 1; z", EXPECT_RESULT, Number::New(isolate, 1));
|
|
|
|
context.Check("let x = 4; x", EXPECT_RESULT, Number::New(isolate, 4));
|
|
|
|
}
|
|
|
|
context.Check("let y = 2; x", EXPECT_RESULT,
|
|
|
|
Number::New(isolate, cond ? 1 : 4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
2012-08-28 11:25:08 +00:00
|
|
|
|
2014-11-10 10:46:37 +00:00
|
|
|
// Check that simple cross-script global scope access works.
|
2017-10-13 16:33:03 +00:00
|
|
|
const char* decs[] = {"'use strict'; var x = 1; x",
|
|
|
|
"x",
|
|
|
|
"'use strict'; function x() { return 1 }; x()",
|
|
|
|
"x()",
|
|
|
|
"'use strict'; let x = 1; x",
|
|
|
|
"x",
|
|
|
|
"'use strict'; const x = 1; x",
|
|
|
|
"x",
|
|
|
|
nullptr};
|
|
|
|
|
|
|
|
for (int i = 0; decs[i] != nullptr; i += 2) {
|
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
|
|
|
SimpleContext context;
|
2014-01-03 14:31:17 +00:00
|
|
|
context.Check(decs[i], EXPECT_RESULT, Number::New(isolate, 1));
|
|
|
|
context.Check(decs[i+1], EXPECT_RESULT, Number::New(isolate, 1));
|
2012-08-23 16:38:15 +00:00
|
|
|
}
|
2014-11-10 10:46:37 +00:00
|
|
|
|
|
|
|
// Check that cross-script global scope access works with late declarations.
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check("function d0() { return x0 }", // dynamic lookup
|
|
|
|
EXPECT_RESULT, Undefined(isolate));
|
|
|
|
context.Check("this.x0 = -1;"
|
|
|
|
"d0()",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, -1));
|
|
|
|
context.Check("'use strict';"
|
|
|
|
"function f0() { let y = 10; return x0 + y }"
|
|
|
|
"function g0() { let y = 10; return eval('x0 + y') }"
|
|
|
|
"function h0() { let y = 10; return (1,eval)('x0') + y }"
|
|
|
|
"x0 + f0() + g0() + h0()",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 26));
|
|
|
|
|
|
|
|
context.Check("'use strict';"
|
|
|
|
"let x1 = 1;"
|
|
|
|
"function f1() { let y = 10; return x1 + y }"
|
|
|
|
"function g1() { let y = 10; return eval('x1 + y') }"
|
|
|
|
"function h1() { let y = 10; return (1,eval)('x1') + y }"
|
|
|
|
"function i1() { "
|
|
|
|
" let y = 10; return (typeof x2 === 'undefined' ? 0 : 2) + y"
|
|
|
|
"}"
|
|
|
|
"function j1() { let y = 10; return eval('x2 + y') }"
|
|
|
|
"function k1() { let y = 10; return (1,eval)('x2') + y }"
|
|
|
|
"function cl() { "
|
|
|
|
" let y = 10; "
|
|
|
|
" return { "
|
|
|
|
" f: function(){ return x1 + y },"
|
|
|
|
" g: function(){ return eval('x1 + y') },"
|
|
|
|
" h: function(){ return (1,eval)('x1') + y },"
|
|
|
|
" i: function(){"
|
|
|
|
" return (typeof x2 == 'undefined' ? 0 : 2) + y"
|
|
|
|
" },"
|
|
|
|
" j: function(){ return eval('x2 + y') },"
|
|
|
|
" k: function(){ return (1,eval)('x2') + y },"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"let o = cl();"
|
|
|
|
"x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
context.Check("x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
context.Check("o.f() + o.g() + o.h();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 33));
|
|
|
|
context.Check("i1() + o.i();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 20));
|
|
|
|
|
|
|
|
context.Check("'use strict';"
|
|
|
|
"let x2 = 2;"
|
|
|
|
"function f2() { let y = 20; return x2 + y }"
|
|
|
|
"function g2() { let y = 20; return eval('x2 + y') }"
|
|
|
|
"function h2() { let y = 20; return (1,eval)('x2') + y }"
|
|
|
|
"function i2() { let y = 20; return x1 + y }"
|
|
|
|
"function j2() { let y = 20; return eval('x1 + y') }"
|
|
|
|
"function k2() { let y = 20; return (1,eval)('x1') + y }"
|
|
|
|
"x2 + eval('x2') + (1,eval)('x2') + f2() + g2() + h2();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 72));
|
|
|
|
context.Check("x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
context.Check("i1() + j1() + k1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
context.Check("i2() + j2() + k2();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 63));
|
|
|
|
context.Check("o.f() + o.g() + o.h();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 33));
|
|
|
|
context.Check("o.i() + o.j() + o.k();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
context.Check("i1() + o.i();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 24));
|
|
|
|
|
|
|
|
context.Check("'use strict';"
|
|
|
|
"let x0 = 100;"
|
|
|
|
"x0 + eval('x0') + (1,eval)('x0') + "
|
|
|
|
" d0() + f0() + g0() + h0();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 730));
|
|
|
|
context.Check("x0 + eval('x0') + (1,eval)('x0') + "
|
|
|
|
" d0() + f0() + g0() + h0();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 730));
|
|
|
|
context.Check("delete this.x0;"
|
|
|
|
"x0 + eval('x0') + (1,eval)('x0') + "
|
|
|
|
" d0() + f0() + g0() + h0();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 730));
|
|
|
|
context.Check("this.x1 = 666;"
|
|
|
|
"x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
context.Check("delete this.x1;"
|
|
|
|
"x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 36));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that caching does respect scopes.
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
const char* script1 = "(function(){ return y1 })()";
|
|
|
|
const char* script2 = "(function(){ return y2 })()";
|
|
|
|
|
|
|
|
context.Check(script1, EXPECT_EXCEPTION);
|
|
|
|
context.Check("this.y1 = 1; this.y2 = 2; 0;",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 0));
|
|
|
|
context.Check(script1,
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 1));
|
|
|
|
context.Check("'use strict'; let y1 = 3; 0;",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 0));
|
2014-11-12 11:34:09 +00:00
|
|
|
context.Check(script1,
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 3));
|
2014-11-10 10:46:37 +00:00
|
|
|
context.Check("y1 = 4;",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 4));
|
2014-11-12 11:34:09 +00:00
|
|
|
context.Check(script1,
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 4));
|
2014-11-10 10:46:37 +00:00
|
|
|
|
|
|
|
context.Check(script2,
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 2));
|
|
|
|
context.Check("'use strict'; let y2 = 5; 0;",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 0));
|
2014-11-12 11:34:09 +00:00
|
|
|
context.Check(script1,
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 4));
|
|
|
|
context.Check(script2,
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 5));
|
2014-11-10 10:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-23 11:58:45 +00:00
|
|
|
TEST(CrossScriptReferencesHarmonyRegress) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"function i1() { "
|
|
|
|
" let y = 10; return (typeof x2 === 'undefined' ? 0 : 2) + y"
|
|
|
|
"}"
|
|
|
|
"i1();"
|
|
|
|
"i1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 10));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x2 = 2; i1();",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 12));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-10 10:46:37 +00:00
|
|
|
TEST(GlobalLexicalOSR) {
|
|
|
|
i::FLAG_use_strict = true;
|
|
|
|
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
SimpleContext context;
|
|
|
|
|
|
|
|
context.Check("'use strict';"
|
|
|
|
"let x = 1; x;",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 1));
|
|
|
|
context.Check("'use strict';"
|
|
|
|
"let y = 2*x;"
|
|
|
|
"++x;"
|
|
|
|
"let z = 0;"
|
|
|
|
"const limit = 100000;"
|
|
|
|
"for (var i = 0; i < limit; ++i) {"
|
|
|
|
" z += x + y;"
|
|
|
|
"}"
|
|
|
|
"z;",
|
|
|
|
EXPECT_RESULT, Number::New(isolate, 400000));
|
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;
|
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
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
const char* firsts[] = {"var x = 1; x", "function x() { return 1 }; x()",
|
|
|
|
"let x = 1; x", "const x = 1; x", nullptr};
|
|
|
|
const char* seconds[] = {"var x = 2; x", "function x() { return 2 }; x()",
|
|
|
|
"let x = 2; x", "const x = 2; x", nullptr};
|
2012-08-23 16:38:15 +00:00
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
for (int i = 0; firsts[i] != nullptr; ++i) {
|
|
|
|
for (int j = 0; seconds[j] != nullptr; ++j) {
|
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
|
|
|
SimpleContext context;
|
2014-01-03 14:31:17 +00:00
|
|
|
context.Check(firsts[i], EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 1));
|
2014-11-07 16:29:13 +00:00
|
|
|
bool success_case = i < 2 && j < 2;
|
|
|
|
Local<Value> success_result;
|
|
|
|
if (success_case) success_result = Number::New(CcTest::isolate(), 2);
|
|
|
|
|
|
|
|
context.Check(seconds[j], success_case ? EXPECT_RESULT : EXPECT_EXCEPTION,
|
|
|
|
success_result);
|
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
|
|
|
}
|
|
|
|
}
|
2014-10-29 10:59:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(CrossScriptDynamicLookup) {
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
2020-03-09 10:41:45 +00:00
|
|
|
Local<String> undefined_string = String::NewFromUtf8Literal(
|
|
|
|
CcTest::isolate(), "undefined", v8::NewStringType::kInternalized);
|
|
|
|
Local<String> number_string = String::NewFromUtf8Literal(
|
|
|
|
CcTest::isolate(), "number", v8::NewStringType::kInternalized);
|
2014-10-29 10:59:41 +00:00
|
|
|
|
|
|
|
context.Check(
|
|
|
|
"function f(o) { with(o) { return x; } }"
|
|
|
|
"function g(o) { with(o) { x = 15; } }"
|
|
|
|
"function h(o) { with(o) { return typeof x; } }",
|
|
|
|
EXPECT_RESULT, Undefined(CcTest::isolate()));
|
|
|
|
context.Check("h({})", EXPECT_RESULT, undefined_string);
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 1;"
|
|
|
|
"f({})",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
2014-11-07 16:29:13 +00:00
|
|
|
"g({});0",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
|
|
|
|
context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
|
|
|
context.Check("h({})", EXPECT_RESULT, number_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CrossScriptGlobal) {
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
|
|
|
|
context.Check(
|
|
|
|
"var global = this;"
|
|
|
|
"global.x = 255;"
|
2014-10-29 10:59:41 +00:00
|
|
|
"x",
|
2014-11-07 16:29:13 +00:00
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 255));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 1;"
|
|
|
|
"global.x",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 255));
|
|
|
|
context.Check("global.x = 15; x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 1));
|
|
|
|
context.Check("x = 221; global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 15));
|
|
|
|
context.Check(
|
|
|
|
"z = 15;"
|
|
|
|
"function f() { return z; };"
|
|
|
|
"for (var k = 0; k < 3; k++) { f(); }"
|
|
|
|
"f()",
|
2014-10-29 10:59:41 +00:00
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
2014-11-07 16:29:13 +00:00
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let z = 5; f()",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
|
|
|
|
context.Check(
|
|
|
|
"function f() { konst = 10; return konst; };"
|
|
|
|
"f()",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 10));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"const konst = 255;"
|
|
|
|
"f()",
|
|
|
|
EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CrossScriptStaticLookupUndeclared) {
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
2020-03-09 10:41:45 +00:00
|
|
|
Local<String> undefined_string = String::NewFromUtf8Literal(
|
|
|
|
CcTest::isolate(), "undefined", v8::NewStringType::kInternalized);
|
|
|
|
Local<String> number_string = String::NewFromUtf8Literal(
|
|
|
|
CcTest::isolate(), "number", v8::NewStringType::kInternalized);
|
2014-11-07 16:29:13 +00:00
|
|
|
|
|
|
|
context.Check(
|
|
|
|
"function f(o) { return x; }"
|
2014-11-11 11:16:30 +00:00
|
|
|
"function g(v) { x = v; }"
|
2014-11-07 16:29:13 +00:00
|
|
|
"function h(o) { return typeof x; }",
|
|
|
|
EXPECT_RESULT, Undefined(CcTest::isolate()));
|
|
|
|
context.Check("h({})", EXPECT_RESULT, undefined_string);
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 1;"
|
|
|
|
"f({})",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
2014-11-11 11:16:30 +00:00
|
|
|
"g(15);x",
|
2014-11-07 16:29:13 +00:00
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
|
|
|
context.Check("h({})", EXPECT_RESULT, number_string);
|
2014-10-29 10:59:41 +00:00
|
|
|
context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
|
|
|
context.Check("h({})", EXPECT_RESULT, number_string);
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 11:16:30 +00:00
|
|
|
|
|
|
|
|
2014-11-11 15:15:42 +00:00
|
|
|
TEST(CrossScriptLoadICs) {
|
2014-11-11 11:16:30 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(
|
|
|
|
"x = 15;"
|
2019-04-30 11:04:41 +00:00
|
|
|
"function f() { return x; };"
|
|
|
|
"function g() { return x; };"
|
|
|
|
"%PrepareFunctionForOptimization(f);"
|
|
|
|
"%PrepareFunctionForOptimization(g);"
|
2014-11-11 11:16:30 +00:00
|
|
|
"f()",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 5;"
|
|
|
|
"f()",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("g()", EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
|
|
|
|
}
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("f()", EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
|
|
|
|
}
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(g); g()", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 5));
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f); f()", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 5));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(
|
|
|
|
"x = 15;"
|
|
|
|
"function f() { return x; }"
|
2019-04-30 11:04:41 +00:00
|
|
|
"%PrepareFunctionForOptimization(f);"
|
2014-11-11 11:16:30 +00:00
|
|
|
"f()",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("f()", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
|
|
|
|
}
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f); f()", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 15));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 5;"
|
2019-04-30 11:04:41 +00:00
|
|
|
"%PrepareFunctionForOptimization(f);"
|
2014-11-11 11:16:30 +00:00
|
|
|
"f()",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("f()", EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
|
|
|
|
}
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f); f()", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 5));
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 15:15:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(CrossScriptStoreICs) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(
|
|
|
|
"var global = this;"
|
|
|
|
"x = 15;"
|
2019-04-30 11:04:41 +00:00
|
|
|
"function f(v) { x = v; };"
|
|
|
|
"function g(v) { x = v; };"
|
|
|
|
"%PrepareFunctionForOptimization(f);"
|
|
|
|
"%PrepareFunctionForOptimization(g);"
|
2014-11-11 15:15:42 +00:00
|
|
|
"f(10); x",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 10));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 5;"
|
|
|
|
"f(7); x",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 7));
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 10));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("g(31); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 31));
|
|
|
|
}
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 10));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("f(32); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 32));
|
|
|
|
}
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 10));
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(g); g(18); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 18));
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 10));
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f); f(33); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 33));
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 10));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check(
|
|
|
|
"var global = this;"
|
|
|
|
"x = 15;"
|
2019-04-30 11:04:41 +00:00
|
|
|
"function f(v) { x = v; };"
|
|
|
|
"%PrepareFunctionForOptimization(f);"
|
2014-11-11 15:15:42 +00:00
|
|
|
"f(10); x",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 10));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("f(18); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 18));
|
|
|
|
}
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f); f(20); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 20));
|
|
|
|
context.Check(
|
|
|
|
"'use strict';"
|
|
|
|
"let x = 5;"
|
|
|
|
"f(8); x",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 8));
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 20));
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
context.Check("f(13); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 13));
|
|
|
|
}
|
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 20));
|
2019-04-30 11:04:41 +00:00
|
|
|
context.Check(
|
|
|
|
"%PrepareFunctionForOptimization(f);"
|
|
|
|
"%OptimizeFunctionOnNextCall(f); f(41); x",
|
|
|
|
EXPECT_RESULT, Number::New(CcTest::isolate(), 41));
|
2014-11-11 15:15:42 +00:00
|
|
|
context.Check("global.x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 20));
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 18:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(CrossScriptAssignmentToConst) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
|
|
|
|
context.Check("function f() { x = 27; }", EXPECT_RESULT,
|
|
|
|
Undefined(CcTest::isolate()));
|
|
|
|
context.Check("'use strict';const x = 1; x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 1));
|
2019-04-30 11:04:41 +00:00
|
|
|
context.Check("%PrepareFunctionForOptimization(f);f();", EXPECT_EXCEPTION);
|
2014-11-21 18:09:25 +00:00
|
|
|
context.Check("x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
context.Check("f();", EXPECT_EXCEPTION);
|
|
|
|
context.Check("x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f);f();", EXPECT_EXCEPTION);
|
|
|
|
context.Check("x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 16:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(Regress425510) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
|
|
|
|
context.Check("'use strict'; o; const o = 10", EXPECT_EXCEPTION);
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
context.Check("o.prototype", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 13:28:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(Regress3941) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check("function f() { x = 1; }", EXPECT_RESULT,
|
|
|
|
Undefined(CcTest::isolate()));
|
|
|
|
context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// Train ICs.
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check("function f() { x = 1; }", EXPECT_RESULT,
|
|
|
|
Undefined(CcTest::isolate()));
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
context.Check("f(); x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
}
|
|
|
|
context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// Optimize.
|
|
|
|
SimpleContext context;
|
2019-04-30 11:04:41 +00:00
|
|
|
context.Check(
|
|
|
|
"function f() { x = 1; };"
|
|
|
|
"%PrepareFunctionForOptimization(f);",
|
|
|
|
EXPECT_RESULT, Undefined(CcTest::isolate()));
|
2015-03-13 13:28:05 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
context.Check("f(); x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
|
|
|
|
}
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f); f(); x", EXPECT_RESULT,
|
|
|
|
Number::New(CcTest::isolate(), 1));
|
|
|
|
|
|
|
|
context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Regress3941_Reads) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
|
|
|
|
HandleScope handle_scope(CcTest::isolate());
|
|
|
|
|
|
|
|
{
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check("function f() { return x; }", EXPECT_RESULT,
|
|
|
|
Undefined(CcTest::isolate()));
|
|
|
|
context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// Train ICs.
|
|
|
|
SimpleContext context;
|
|
|
|
context.Check("function f() { return x; }", EXPECT_RESULT,
|
|
|
|
Undefined(CcTest::isolate()));
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
context.Check("f()", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// Optimize.
|
|
|
|
SimpleContext context;
|
2019-04-30 11:04:41 +00:00
|
|
|
context.Check(
|
|
|
|
"function f() { return x; };"
|
|
|
|
"%PrepareFunctionForOptimization(f);",
|
|
|
|
EXPECT_RESULT, Undefined(CcTest::isolate()));
|
2015-03-13 13:28:05 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
context.Check("f()", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
context.Check("%OptimizeFunctionOnNextCall(f);", EXPECT_RESULT,
|
|
|
|
Undefined(CcTest::isolate()));
|
|
|
|
|
|
|
|
context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
|
|
|
} // namespace v8
|