2012-01-24 16:36:55 +00:00
|
|
|
// Copyright 2012 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>
|
2011-09-06 07:41:45 +00:00
|
|
|
#include <wchar.h>
|
2019-11-28 19:26:16 +00:00
|
|
|
#include <memory>
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2019-11-28 19:26:16 +00:00
|
|
|
#include "include/v8-profiler.h"
|
|
|
|
#include "include/v8.h"
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/compilation-cache.h"
|
|
|
|
#include "src/codegen/compiler.h"
|
2019-05-20 09:15:06 +00:00
|
|
|
#include "src/diagnostics/disasm.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/heap/spaces.h"
|
2016-05-19 12:27:18 +00:00
|
|
|
#include "src/interpreter/interpreter.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/objects/allocation-site-inl.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2019-11-28 19:26:16 +00:00
|
|
|
#include "src/objects/shared-function-info.h"
|
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 {
|
|
|
|
namespace internal {
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-04-09 12:21:47 +00:00
|
|
|
static Handle<Object> GetGlobalProperty(const char* name) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2016-03-08 17:29:05 +00:00
|
|
|
return JSReceiver::GetProperty(isolate, isolate->global_object(), name)
|
|
|
|
.ToHandleChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-25 00:19:47 +00:00
|
|
|
static void SetGlobalProperty(const char* name, Object value) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> object(value, isolate);
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> internalized_name =
|
|
|
|
isolate->factory()->InternalizeUtf8String(name);
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2014-06-27 13:48:37 +00:00
|
|
|
Runtime::SetObjectProperty(isolate, global, internalized_name, object,
|
2019-01-23 17:13:08 +00:00
|
|
|
StoreOrigin::kMaybeKeyed, Just(kDontThrow))
|
2017-10-16 10:55:06 +00:00
|
|
|
.Check();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Handle<JSFunction> Compile(const char* source) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<String> source_code = isolate->factory()->NewStringFromUtf8(
|
|
|
|
CStrVector(source)).ToHandleChecked();
|
2017-10-04 22:48:12 +00:00
|
|
|
Handle<SharedFunctionInfo> shared =
|
|
|
|
Compiler::GetSharedFunctionInfoForScript(
|
2018-07-17 15:08:58 +00:00
|
|
|
isolate, source_code, Compiler::ScriptDetails(),
|
|
|
|
v8::ScriptOriginOptions(), nullptr, nullptr,
|
|
|
|
v8::ScriptCompiler::kNoCompileOptions,
|
2018-02-21 14:35:23 +00:00
|
|
|
ScriptCompiler::kNoCacheNoReason, NOT_NATIVES_CODE)
|
2017-10-04 22:48:12 +00:00
|
|
|
.ToHandleChecked();
|
2013-06-04 10:30:05 +00:00
|
|
|
return isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
2016-03-08 12:07:27 +00:00
|
|
|
shared, isolate->native_context());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
static double Inc(Isolate* isolate, int x) {
|
2008-08-22 13:33:59 +00:00
|
|
|
const char* source = "result = %d + 1;";
|
2008-09-11 14:34:48 +00:00
|
|
|
EmbeddedVector<char, 512> buffer;
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(buffer, source, x);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2019-04-29 11:06:49 +00:00
|
|
|
Handle<JSFunction> fun = Compile(buffer.begin());
|
2008-08-22 13:33:59 +00:00
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, nullptr).Check();
|
2014-04-09 12:21:47 +00:00
|
|
|
return GetGlobalProperty("result")->Number();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Inc) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-09-05 08:48:34 +00:00
|
|
|
CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
static double Add(Isolate* isolate, int x, int y) {
|
2008-08-22 13:33:59 +00:00
|
|
|
Handle<JSFunction> fun = Compile("result = x + y;");
|
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
|
|
|
SetGlobalProperty("x", Smi::FromInt(x));
|
|
|
|
SetGlobalProperty("y", Smi::FromInt(y));
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, nullptr).Check();
|
2014-04-09 12:21:47 +00:00
|
|
|
return GetGlobalProperty("result")->Number();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Add) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-09-05 08:48:34 +00:00
|
|
|
CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
static double Abs(Isolate* isolate, int x) {
|
2008-08-22 13:33:59 +00:00
|
|
|
Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
|
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
|
|
|
SetGlobalProperty("x", Smi::FromInt(x));
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, nullptr).Check();
|
2014-04-09 12:21:47 +00:00
|
|
|
return GetGlobalProperty("result")->Number();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Abs) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-09-05 08:48:34 +00:00
|
|
|
CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
static double Sum(Isolate* isolate, int n) {
|
2008-08-22 13:33:59 +00:00
|
|
|
Handle<JSFunction> fun =
|
|
|
|
Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
|
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
|
|
|
SetGlobalProperty("n", Smi::FromInt(n));
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, nullptr).Check();
|
2014-04-09 12:21:47 +00:00
|
|
|
return GetGlobalProperty("result")->Number();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Sum) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-09-05 08:48:34 +00:00
|
|
|
CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Print) {
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2019-01-16 17:31:37 +00:00
|
|
|
v8::Local<v8::Context> context = CcTest::NewContext({PRINT_EXTENSION_ID});
|
2013-09-19 13:30:47 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2008-08-22 13:33:59 +00:00
|
|
|
const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
|
|
|
|
Handle<JSFunction> fun = Compile(source);
|
|
|
|
if (fun.is_null()) return;
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(CcTest::i_isolate()->context().global_object(),
|
|
|
|
fun->GetIsolate());
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(CcTest::i_isolate(), fun, global, 0, nullptr).Check();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The following test method stems from my coding efforts today. It
|
|
|
|
// tests all the functionality I have added to the compiler today
|
|
|
|
TEST(Stuff) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
const char* source =
|
|
|
|
"r = 0;\n"
|
|
|
|
"a = new Object;\n"
|
|
|
|
"if (a == a) r+=1;\n" // 1
|
|
|
|
"if (a != new Object()) r+=2;\n" // 2
|
|
|
|
"a.x = 42;\n"
|
|
|
|
"if (a.x == 42) r+=4;\n" // 4
|
|
|
|
"function foo() { var x = 87; return x; }\n"
|
|
|
|
"if (foo() == 87) r+=8;\n" // 8
|
|
|
|
"function bar() { var x; x = 99; return x; }\n"
|
|
|
|
"if (bar() == 99) r+=16;\n" // 16
|
|
|
|
"function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
|
|
|
|
"if (baz() == 6) r+=32;\n" // 32
|
|
|
|
"function Cons0() { this.x = 42; this.y = 87; }\n"
|
|
|
|
"if (new Cons0().x == 42) r+=64;\n" // 64
|
|
|
|
"if (new Cons0().y == 87) r+=128;\n" // 128
|
|
|
|
"function Cons2(x, y) { this.sum = x + y; }\n"
|
|
|
|
"if (new Cons2(3,4).sum == 7) r+=256;"; // 256
|
|
|
|
|
|
|
|
Handle<JSFunction> fun = Compile(source);
|
|
|
|
CHECK(!fun.is_null());
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(CcTest::i_isolate()->context().global_object(),
|
|
|
|
fun->GetIsolate());
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(CcTest::i_isolate(), fun, global, 0, nullptr).Check();
|
2014-04-09 12:21:47 +00:00
|
|
|
CHECK_EQ(511.0, GetGlobalProperty("r")->Number());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(UncaughtThrow) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
const char* source = "throw 42;";
|
|
|
|
Handle<JSFunction> fun = Compile(source);
|
|
|
|
CHECK(!fun.is_null());
|
2013-02-25 14:46:09 +00:00
|
|
|
Isolate* isolate = fun->GetIsolate();
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2017-10-13 16:33:03 +00:00
|
|
|
CHECK(Execution::Call(isolate, fun, global, 0, nullptr).is_null());
|
2014-04-08 09:44:24 +00:00
|
|
|
CHECK_EQ(42.0, isolate->pending_exception().Number());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests calling a builtin function from C/C++ code, and the builtin function
|
|
|
|
// performs GC. It creates a stack frame looks like following:
|
|
|
|
// | C (PerformGC) |
|
|
|
|
// | JS-to-C |
|
|
|
|
// | JS |
|
|
|
|
// | C-to-JS |
|
|
|
|
TEST(C2JSFrames) {
|
2014-01-20 15:44:03 +00:00
|
|
|
FLAG_expose_gc = true;
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2013-09-19 13:30:47 +00:00
|
|
|
v8::Local<v8::Context> context =
|
2019-01-16 17:31:37 +00:00
|
|
|
CcTest::NewContext({PRINT_EXTENSION_ID, GC_EXTENSION_ID});
|
2013-09-19 13:30:47 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
const char* source = "function foo(a) { gc(), print(a); }";
|
|
|
|
|
|
|
|
Handle<JSFunction> fun0 = Compile(source);
|
|
|
|
CHECK(!fun0.is_null());
|
2013-02-25 14:46:09 +00:00
|
|
|
Isolate* isolate = fun0->GetIsolate();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Run the generated code to populate the global object with 'foo'.
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<JSObject> global(isolate->context().global_object(), isolate);
|
2017-10-13 16:33:03 +00:00
|
|
|
Execution::Call(isolate, fun0, global, 0, nullptr).Check();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2016-03-08 17:29:05 +00:00
|
|
|
Handle<Object> fun1 =
|
|
|
|
JSReceiver::GetProperty(isolate, isolate->global_object(), "foo")
|
|
|
|
.ToHandleChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(fun1->IsJSFunction());
|
|
|
|
|
2019-01-02 14:19:23 +00:00
|
|
|
Handle<Object> argv[] = {
|
2019-05-22 16:18:05 +00:00
|
|
|
isolate->factory()->InternalizeString(StaticCharVector("hello"))};
|
2013-09-05 08:48:34 +00:00
|
|
|
Execution::Call(isolate,
|
|
|
|
Handle<JSFunction>::cast(fun1),
|
2011-10-06 09:31:38 +00:00
|
|
|
global,
|
2014-08-26 09:19:24 +00:00
|
|
|
arraysize(argv),
|
2014-04-11 10:41:09 +00:00
|
|
|
argv).Check();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2009-02-24 13:33:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Regression 236. Calling InitLineEnds on a Script with undefined
|
|
|
|
// source resulted in crash.
|
|
|
|
TEST(Regression236) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-02-24 13:33:49 +00:00
|
|
|
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<Script> script = factory->NewScript(factory->empty_string());
|
2018-07-04 09:10:05 +00:00
|
|
|
script->set_source(ReadOnlyRoots(CcTest::heap()).undefined_value());
|
2014-04-16 13:28:11 +00:00
|
|
|
CHECK_EQ(-1, Script::GetLineNumber(script, 0));
|
|
|
|
CHECK_EQ(-1, Script::GetLineNumber(script, 100));
|
|
|
|
CHECK_EQ(-1, Script::GetLineNumber(script, -1));
|
2009-02-24 13:33:49 +00:00
|
|
|
}
|
2010-02-16 12:08:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(GetScriptLineNumber) {
|
2013-09-19 13:30:47 +00:00
|
|
|
LocalContext context;
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str("test"));
|
2010-02-16 12:08:10 +00:00
|
|
|
const char function_f[] = "function f() {}";
|
|
|
|
const int max_rows = 1000;
|
|
|
|
const int buffer_size = max_rows + sizeof(function_f);
|
|
|
|
ScopedVector<char> buffer(buffer_size);
|
2019-04-29 11:06:49 +00:00
|
|
|
memset(buffer.begin(), '\n', buffer_size - 1);
|
2010-02-16 12:08:10 +00:00
|
|
|
buffer[buffer_size - 1] = '\0';
|
|
|
|
|
|
|
|
for (int i = 0; i < max_rows; ++i) {
|
|
|
|
if (i > 0)
|
|
|
|
buffer[i - 1] = '\n';
|
2014-05-27 07:57:22 +00:00
|
|
|
MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
|
2019-04-29 11:06:49 +00:00
|
|
|
v8::Local<v8::String> script_body = v8_str(buffer.begin());
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Script::Compile(context.local(), script_body, &origin)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(context.local())
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
|
|
|
|
context->Global()->Get(context.local(), v8_str("f")).ToLocalChecked());
|
2010-02-16 12:08:10 +00:00
|
|
|
CHECK_EQ(i, f->GetScriptLineNumber());
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 14:16:24 +00:00
|
|
|
|
|
|
|
|
2014-04-30 10:51:01 +00:00
|
|
|
TEST(FeedbackVectorPreservedAcrossRecompiles) {
|
2017-04-28 13:33:43 +00:00
|
|
|
if (i::FLAG_always_opt || !i::FLAG_opt) return;
|
2014-04-30 10:51:01 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
CcTest::InitializeVM();
|
2017-04-28 15:36:46 +00:00
|
|
|
if (!CcTest::i_isolate()->use_optimizer()) return;
|
2014-04-30 10:51:01 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
2014-04-30 10:51:01 +00:00
|
|
|
|
|
|
|
// Make sure function f has a call that uses a type feedback slot.
|
2019-04-30 11:04:41 +00:00
|
|
|
CompileRun(
|
|
|
|
"function fun() {};"
|
|
|
|
"fun1 = fun;"
|
|
|
|
"%PrepareFunctionForOptimization(f);"
|
|
|
|
"function f(a) { a(); } f(fun1);");
|
2014-04-30 10:51:01 +00:00
|
|
|
|
2015-11-20 12:56:19 +00:00
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(
|
|
|
|
v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
|
|
|
|
CcTest::global()->Get(context, v8_str("f")).ToLocalChecked())));
|
2014-04-30 10:51:01 +00:00
|
|
|
|
|
|
|
// Verify that we gathered feedback.
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<FeedbackVector> feedback_vector(f->feedback_vector(), f->GetIsolate());
|
2015-10-01 13:48:05 +00:00
|
|
|
CHECK(!feedback_vector->is_empty());
|
2017-02-07 15:19:35 +00:00
|
|
|
FeedbackSlot slot_for_a(0);
|
2018-10-26 00:23:24 +00:00
|
|
|
MaybeObject object = feedback_vector->Get(slot_for_a);
|
2018-05-23 06:53:49 +00:00
|
|
|
{
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(object->GetHeapObjectIfWeak(&heap_object));
|
2018-05-23 06:53:49 +00:00
|
|
|
CHECK(heap_object.IsJSFunction());
|
|
|
|
}
|
2014-04-30 10:51:01 +00:00
|
|
|
|
|
|
|
CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);");
|
|
|
|
|
|
|
|
// Verify that the feedback is still "gathered" despite a recompilation
|
|
|
|
// of the full code.
|
2020-08-11 09:24:47 +00:00
|
|
|
CHECK(f->HasAttachedOptimizedCode());
|
2016-05-27 08:09:12 +00:00
|
|
|
object = f->feedback_vector().Get(slot_for_a);
|
2018-05-23 06:53:49 +00:00
|
|
|
{
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(object->GetHeapObjectIfWeak(&heap_object));
|
2018-05-23 06:53:49 +00:00
|
|
|
CHECK(heap_object.IsJSFunction());
|
|
|
|
}
|
2014-04-30 10:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FeedbackVectorUnaffectedByScopeChanges) {
|
2018-12-19 13:17:05 +00:00
|
|
|
if (i::FLAG_always_opt || !i::FLAG_lazy || i::FLAG_lite_mode) {
|
2016-03-24 18:37:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-30 10:51:01 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
2014-04-30 10:51:01 +00:00
|
|
|
|
|
|
|
CompileRun("function builder() {"
|
|
|
|
" call_target = function() { return 3; };"
|
|
|
|
" return (function() {"
|
|
|
|
" eval('');"
|
|
|
|
" return function() {"
|
|
|
|
" 'use strict';"
|
|
|
|
" call_target();"
|
|
|
|
" }"
|
|
|
|
" })();"
|
|
|
|
"}"
|
|
|
|
"morphing_call = builder();");
|
|
|
|
|
2015-11-20 12:56:19 +00:00
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Function>::Cast(CcTest::global()
|
|
|
|
->Get(context, v8_str("morphing_call"))
|
|
|
|
.ToLocalChecked())));
|
2014-04-30 10:51:01 +00:00
|
|
|
|
2016-03-24 18:37:56 +00:00
|
|
|
// If we are compiling lazily then it should not be compiled, and so no
|
|
|
|
// feedback vector allocated yet.
|
2014-04-30 10:51:01 +00:00
|
|
|
CHECK(!f->shared().is_compiled());
|
|
|
|
|
|
|
|
CompileRun("morphing_call();");
|
|
|
|
|
2019-05-13 12:49:01 +00:00
|
|
|
// Now a feedback vector / closure feedback cell array is allocated.
|
2014-11-14 08:21:13 +00:00
|
|
|
CHECK(f->shared().is_compiled());
|
2019-05-13 12:49:01 +00:00
|
|
|
CHECK(f->has_feedback_vector() || f->has_closure_feedback_cell_array());
|
2014-04-30 10:51:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 10:54:31 +00:00
|
|
|
// Test that optimized code for different closures is actually shared.
|
2015-06-26 09:07:30 +00:00
|
|
|
TEST(OptimizedCodeSharing1) {
|
2013-06-25 11:16:13 +00:00
|
|
|
FLAG_stress_compaction = false;
|
2012-06-18 13:26:43 +00:00
|
|
|
FLAG_allow_natives_syntax = true;
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2015-08-07 13:38:55 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2012-06-18 13:26:43 +00:00
|
|
|
LocalContext env;
|
2015-11-20 12:56:19 +00:00
|
|
|
env->Global()
|
|
|
|
->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
|
|
|
|
.FromJust();
|
2015-06-26 09:07:30 +00:00
|
|
|
CompileRun(
|
|
|
|
"function MakeClosure() {"
|
|
|
|
" return function() { return x; };"
|
|
|
|
"}"
|
|
|
|
"var closure0 = MakeClosure();"
|
2017-02-10 15:19:19 +00:00
|
|
|
"var closure1 = MakeClosure();" // We only share optimized code
|
|
|
|
// if there are at least two closures.
|
2019-04-30 11:04:41 +00:00
|
|
|
"%PrepareFunctionForOptimization(closure0);"
|
2015-06-26 09:07:30 +00:00
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());"
|
2017-02-10 15:19:19 +00:00
|
|
|
"closure1();"
|
2016-04-13 10:54:31 +00:00
|
|
|
"var closure2 = MakeClosure(); closure2();");
|
2015-10-23 12:26:49 +00:00
|
|
|
Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
|
|
|
|
v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
|
2015-11-20 12:56:19 +00:00
|
|
|
env->Global()
|
|
|
|
->Get(env.local(), v8_str("closure1"))
|
|
|
|
.ToLocalChecked())));
|
2015-10-23 12:26:49 +00:00
|
|
|
Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
|
|
|
|
v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
|
2015-11-20 12:56:19 +00:00
|
|
|
env->Global()
|
|
|
|
->Get(env.local(), v8_str("closure2"))
|
|
|
|
.ToLocalChecked())));
|
2020-08-11 09:24:47 +00:00
|
|
|
CHECK(fun1->HasAttachedOptimizedCode() ||
|
|
|
|
!CcTest::i_isolate()->use_optimizer());
|
|
|
|
CHECK(fun2->HasAttachedOptimizedCode() ||
|
|
|
|
!CcTest::i_isolate()->use_optimizer());
|
2012-06-18 13:26:43 +00:00
|
|
|
CHECK_EQ(fun1->code(), fun2->code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 15:15:29 +00:00
|
|
|
TEST(CompileFunctionInContext) {
|
2017-12-18 08:10:06 +00:00
|
|
|
if (i::FLAG_always_opt) return;
|
2015-02-09 15:15:29 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
CompileRun("var r = 10;");
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Object> math = v8::Local<v8::Object>::Cast(
|
|
|
|
env->Global()->Get(env.local(), v8_str("Math")).ToLocalChecked());
|
2015-02-09 15:15:29 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(
|
|
|
|
"a = PI * r * r;"
|
|
|
|
"x = r * cos(PI);"
|
|
|
|
"y = r * sin(PI / 2);"));
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
|
2017-10-13 16:33:03 +00:00
|
|
|
0, nullptr, 1, &math)
|
2015-11-20 12:56:19 +00:00
|
|
|
.ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
2017-12-18 08:10:06 +00:00
|
|
|
|
|
|
|
i::DisallowCompilation no_compile(CcTest::i_isolate());
|
2017-10-13 16:33:03 +00:00
|
|
|
fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(env->Global()->Has(env.local(), v8_str("a")).FromJust());
|
|
|
|
v8::Local<v8::Value> a =
|
|
|
|
env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(a->IsNumber());
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(env->Global()->Has(env.local(), v8_str("x")).FromJust());
|
|
|
|
v8::Local<v8::Value> x =
|
|
|
|
env->Global()->Get(env.local(), v8_str("x")).ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(x->IsNumber());
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(env->Global()->Has(env.local(), v8_str("y")).FromJust());
|
|
|
|
v8::Local<v8::Value> y =
|
|
|
|
env->Global()->Get(env.local(), v8_str("y")).ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(y->IsNumber());
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK_EQ(314.1592653589793, a->NumberValue(env.local()).FromJust());
|
|
|
|
CHECK_EQ(-10.0, x->NumberValue(env.local()).FromJust());
|
|
|
|
CHECK_EQ(10.0, y->NumberValue(env.local()).FromJust());
|
2015-02-09 15:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CompileFunctionInContextComplex) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
CompileRun(
|
|
|
|
"var x = 1;"
|
|
|
|
"var y = 2;"
|
|
|
|
"var z = 4;"
|
|
|
|
"var a = {x: 8, y: 16};"
|
|
|
|
"var b = {x: 32};");
|
|
|
|
v8::Local<v8::Object> ext[2];
|
2015-11-20 12:56:19 +00:00
|
|
|
ext[0] = v8::Local<v8::Object>::Cast(
|
|
|
|
env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
|
|
|
|
ext[1] = v8::Local<v8::Object>::Cast(
|
|
|
|
env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked());
|
2015-02-09 15:15:29 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z"));
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
|
2017-10-13 16:33:03 +00:00
|
|
|
0, nullptr, 2, ext)
|
2015-11-20 12:56:19 +00:00
|
|
|
.ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
2017-10-13 16:33:03 +00:00
|
|
|
fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(result->IsNumber());
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK_EQ(52.0, result->NumberValue(env.local()).FromJust());
|
2015-02-09 15:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-12 14:38:54 +00:00
|
|
|
TEST(CompileFunctionInContextArgs) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
CompileRun("var a = {x: 23};");
|
|
|
|
v8::Local<v8::Object> ext[1];
|
2015-11-20 12:56:19 +00:00
|
|
|
ext[0] = v8::Local<v8::Object>::Cast(
|
|
|
|
env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
|
2018-12-21 13:56:50 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("result = x + abc"));
|
|
|
|
v8::Local<v8::String> arg = v8_str("abc");
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
|
|
|
|
1, &arg, 1, ext)
|
|
|
|
.ToLocalChecked();
|
2017-12-18 08:10:06 +00:00
|
|
|
CHECK_EQ(1, fun->Get(env.local(), v8_str("length"))
|
|
|
|
.ToLocalChecked()
|
|
|
|
->ToInt32(env.local())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Value());
|
2018-12-21 13:56:50 +00:00
|
|
|
v8::Local<v8::Value> arg_value = v8::Number::New(CcTest::isolate(), 42.0);
|
|
|
|
fun->Call(env.local(), env->Global(), 1, &arg_value).ToLocalChecked();
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
|
2015-02-12 14:38:54 +00:00
|
|
|
CHECK(result->IsNumber());
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
|
2015-02-12 14:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CompileFunctionInContextComments) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
CompileRun("var a = {x: 23, y: 1, z: 2};");
|
|
|
|
v8::Local<v8::Object> ext[1];
|
2015-11-20 12:56:19 +00:00
|
|
|
ext[0] = v8::Local<v8::Object>::Cast(
|
|
|
|
env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
|
2018-12-21 13:56:50 +00:00
|
|
|
v8::Local<v8::String> source =
|
|
|
|
CompileRun("'result = /* y + */ x + a\\u4e00 // + z'").As<v8::String>();
|
|
|
|
v8::ScriptCompiler::Source script_source(source);
|
|
|
|
v8::Local<v8::String> arg = CompileRun("'a\\u4e00'").As<v8::String>();
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
|
|
|
|
1, &arg, 1, ext)
|
|
|
|
.ToLocalChecked();
|
2015-02-12 14:38:54 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
2018-12-21 13:56:50 +00:00
|
|
|
v8::Local<v8::Value> arg_value = v8::Number::New(CcTest::isolate(), 42.0);
|
|
|
|
fun->Call(env.local(), env->Global(), 1, &arg_value).ToLocalChecked();
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
|
2015-02-12 14:38:54 +00:00
|
|
|
CHECK(result->IsNumber());
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
|
2015-02-12 14:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CompileFunctionInContextNonIdentifierArgs) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("result = 1"));
|
|
|
|
v8::Local<v8::String> arg = v8_str("b }");
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(v8::ScriptCompiler::CompileFunctionInContext(
|
2017-10-13 16:33:03 +00:00
|
|
|
env.local(), &script_source, 1, &arg, 0, nullptr)
|
2015-11-20 12:56:19 +00:00
|
|
|
.IsEmpty());
|
2015-02-12 14:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 08:10:06 +00:00
|
|
|
TEST(CompileFunctionInContextRenderCallSite) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
static const char* source1 =
|
|
|
|
"try {"
|
|
|
|
" var a = [];"
|
|
|
|
" a[0]();"
|
|
|
|
"} catch (e) {"
|
|
|
|
" return e.toString();"
|
|
|
|
"}";
|
|
|
|
static const char* expect1 = "TypeError: a[0] is not a function";
|
|
|
|
static const char* source2 =
|
|
|
|
"try {"
|
|
|
|
" (function() {"
|
|
|
|
" var a = [];"
|
|
|
|
" a[0]();"
|
|
|
|
" })()"
|
|
|
|
"} catch (e) {"
|
|
|
|
" return e.toString();"
|
|
|
|
"}";
|
|
|
|
static const char* expect2 = "TypeError: a[0] is not a function";
|
|
|
|
{
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source1));
|
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr)
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK(!fun.IsEmpty());
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
|
|
|
|
CHECK(result->IsString());
|
|
|
|
CHECK(v8::Local<v8::String>::Cast(result)
|
|
|
|
->Equals(env.local(), v8_str(expect1))
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source2));
|
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr)
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
|
|
|
|
CHECK(result->IsString());
|
|
|
|
CHECK(v8::Local<v8::String>::Cast(result)
|
|
|
|
->Equals(env.local(), v8_str(expect2))
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileFunctionInContextQuirks) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
{
|
|
|
|
static const char* source =
|
|
|
|
"[x, y] = ['ab', 'cd'];"
|
|
|
|
"return x + y";
|
|
|
|
static const char* expect = "abcd";
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source));
|
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr)
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
fun->Call(env.local(), env->Global(), 0, nullptr).ToLocalChecked();
|
|
|
|
CHECK(result->IsString());
|
|
|
|
CHECK(v8::Local<v8::String>::Cast(result)
|
|
|
|
->Equals(env.local(), v8_str(expect))
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
static const char* source = "'use strict'; var a = 077";
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source));
|
|
|
|
v8::TryCatch try_catch(CcTest::isolate());
|
|
|
|
CHECK(v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr)
|
|
|
|
.IsEmpty());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
static const char* source = "{ let x; { var x } }";
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source));
|
|
|
|
v8::TryCatch try_catch(CcTest::isolate());
|
|
|
|
CHECK(v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr)
|
|
|
|
.IsEmpty());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 14:38:54 +00:00
|
|
|
|
2015-07-16 12:08:01 +00:00
|
|
|
TEST(CompileFunctionInContextScriptOrigin) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"),
|
|
|
|
v8::Integer::New(CcTest::isolate(), 22),
|
|
|
|
v8::Integer::New(CcTest::isolate(), 41));
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
|
2019-07-17 05:54:12 +00:00
|
|
|
Local<ScriptOrModule> script;
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun =
|
2019-07-17 05:54:12 +00:00
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr,
|
|
|
|
v8::ScriptCompiler::CompileOptions::kNoCompileOptions,
|
|
|
|
v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason, &script)
|
2015-11-20 12:56:19 +00:00
|
|
|
.ToLocalChecked();
|
2015-07-16 12:08:01 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
2019-07-17 05:54:12 +00:00
|
|
|
CHECK(!script.IsEmpty());
|
|
|
|
CHECK(script->GetResourceName()->StrictEquals(v8_str("test")));
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::TryCatch try_catch(CcTest::isolate());
|
2015-07-16 12:08:01 +00:00
|
|
|
CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
|
2017-10-13 16:33:03 +00:00
|
|
|
CHECK(fun->Call(env.local(), env->Global(), 0, nullptr).IsEmpty());
|
2015-07-16 12:08:01 +00:00
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(!try_catch.Exception().IsEmpty());
|
|
|
|
v8::Local<v8::StackTrace> stack =
|
|
|
|
v8::Exception::GetStackTrace(try_catch.Exception());
|
|
|
|
CHECK(!stack.IsEmpty());
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_GT(stack->GetFrameCount(), 0);
|
2018-07-24 14:42:13 +00:00
|
|
|
v8::Local<v8::StackFrame> frame = stack->GetFrame(CcTest::isolate(), 0);
|
2015-07-16 12:08:01 +00:00
|
|
|
CHECK_EQ(23, frame->GetLineNumber());
|
|
|
|
CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
|
|
|
|
}
|
|
|
|
|
2017-12-18 08:10:06 +00:00
|
|
|
void TestCompileFunctionInContextToStringImpl() {
|
2019-05-10 18:55:22 +00:00
|
|
|
#define CHECK_NOT_CAUGHT(__local_context__, try_catch, __op__) \
|
|
|
|
do { \
|
|
|
|
const char* op = (__op__); \
|
|
|
|
v8::Local<v8::Context> context = (__local_context__); \
|
|
|
|
if (try_catch.HasCaught()) { \
|
|
|
|
v8::String::Utf8Value error( \
|
|
|
|
CcTest::isolate(), \
|
|
|
|
try_catch.Exception()->ToString(context).ToLocalChecked()); \
|
|
|
|
FATAL("Unexpected exception thrown during %s:\n\t%s\n", op, *error); \
|
|
|
|
} \
|
2018-09-19 17:41:30 +00:00
|
|
|
} while (false)
|
2017-04-03 12:48:26 +00:00
|
|
|
|
2017-12-18 08:10:06 +00:00
|
|
|
{ // NOLINT
|
2017-04-03 12:48:26 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
LocalContext env;
|
|
|
|
|
|
|
|
// Regression test for v8:6190
|
|
|
|
{
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"), v8_int(22), v8_int(41));
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("return event"), origin);
|
|
|
|
|
|
|
|
v8::Local<v8::String> params[] = {v8_str("event")};
|
|
|
|
v8::TryCatch try_catch(CcTest::isolate());
|
|
|
|
v8::MaybeLocal<v8::Function> maybe_fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, arraysize(params), params, 0,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
CHECK_NOT_CAUGHT(env.local(), try_catch,
|
|
|
|
"v8::ScriptCompiler::CompileFunctionInContext");
|
|
|
|
|
|
|
|
v8::Local<v8::Function> fun = maybe_fun.ToLocalChecked();
|
|
|
|
CHECK(!fun.IsEmpty());
|
|
|
|
CHECK(!try_catch.HasCaught());
|
|
|
|
v8::Local<v8::String> result =
|
|
|
|
fun->ToString(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::String> expected = v8_str(
|
2017-12-18 08:10:06 +00:00
|
|
|
"function (event) {\n"
|
|
|
|
"return event\n"
|
2017-04-03 12:48:26 +00:00
|
|
|
"}");
|
|
|
|
CHECK(expected->Equals(env.local(), result).FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
// With no parameters:
|
|
|
|
{
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"), v8_int(17), v8_int(31));
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("return 0"), origin);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch(CcTest::isolate());
|
|
|
|
v8::MaybeLocal<v8::Function> maybe_fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr);
|
|
|
|
|
|
|
|
CHECK_NOT_CAUGHT(env.local(), try_catch,
|
|
|
|
"v8::ScriptCompiler::CompileFunctionInContext");
|
|
|
|
|
|
|
|
v8::Local<v8::Function> fun = maybe_fun.ToLocalChecked();
|
|
|
|
CHECK(!fun.IsEmpty());
|
|
|
|
CHECK(!try_catch.HasCaught());
|
|
|
|
v8::Local<v8::String> result =
|
|
|
|
fun->ToString(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::String> expected = v8_str(
|
2017-12-18 08:10:06 +00:00
|
|
|
"function () {\n"
|
|
|
|
"return 0\n"
|
2017-04-03 12:48:26 +00:00
|
|
|
"}");
|
|
|
|
CHECK(expected->Equals(env.local(), result).FromJust());
|
2017-12-20 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// With a name:
|
|
|
|
{
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"), v8_int(17), v8_int(31));
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("return 0"), origin);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch(CcTest::isolate());
|
|
|
|
v8::MaybeLocal<v8::Function> maybe_fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(
|
|
|
|
env.local(), &script_source, 0, nullptr, 0, nullptr);
|
|
|
|
|
|
|
|
CHECK_NOT_CAUGHT(env.local(), try_catch,
|
|
|
|
"v8::ScriptCompiler::CompileFunctionInContext");
|
|
|
|
|
|
|
|
v8::Local<v8::Function> fun = maybe_fun.ToLocalChecked();
|
|
|
|
CHECK(!fun.IsEmpty());
|
|
|
|
CHECK(!try_catch.HasCaught());
|
|
|
|
|
|
|
|
fun->SetName(v8_str("onclick"));
|
|
|
|
|
|
|
|
v8::Local<v8::String> result =
|
|
|
|
fun->ToString(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::String> expected = v8_str(
|
|
|
|
"function onclick() {\n"
|
|
|
|
"return 0\n"
|
|
|
|
"}");
|
|
|
|
CHECK(expected->Equals(env.local(), result).FromJust());
|
2017-04-03 12:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef CHECK_NOT_CAUGHT
|
|
|
|
}
|
2015-07-16 12:08:01 +00:00
|
|
|
|
2017-12-18 08:10:06 +00:00
|
|
|
TEST(CompileFunctionInContextFunctionToString) {
|
|
|
|
TestCompileFunctionInContextToStringImpl();
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:20:08 +00:00
|
|
|
TEST(InvocationCount) {
|
2018-12-19 13:17:05 +00:00
|
|
|
if (FLAG_lite_mode) return;
|
2016-09-14 10:20:08 +00:00
|
|
|
FLAG_allow_natives_syntax = true;
|
|
|
|
FLAG_always_opt = false;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"function bar() {};"
|
2019-05-03 14:32:12 +00:00
|
|
|
"%EnsureFeedbackVectorForFunction(bar);"
|
2016-09-14 10:20:08 +00:00
|
|
|
"function foo() { return bar(); };"
|
2019-05-03 14:32:12 +00:00
|
|
|
"%EnsureFeedbackVectorForFunction(foo);"
|
2016-09-14 10:20:08 +00:00
|
|
|
"foo();");
|
|
|
|
Handle<JSFunction> foo = Handle<JSFunction>::cast(GetGlobalProperty("foo"));
|
|
|
|
CHECK_EQ(1, foo->feedback_vector().invocation_count());
|
|
|
|
CompileRun("foo()");
|
|
|
|
CHECK_EQ(2, foo->feedback_vector().invocation_count());
|
|
|
|
CompileRun("bar()");
|
|
|
|
CHECK_EQ(2, foo->feedback_vector().invocation_count());
|
|
|
|
CompileRun("foo(); foo()");
|
|
|
|
CHECK_EQ(4, foo->feedback_vector().invocation_count());
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
[turbofan] Skip arguments adaptor when target cannot observe arguments.
When calling a known function from optimized code, where the number of
actual arguments does not match the number of expected arguments,
TurboFan has to call indirectly via the arguments adaptor trampoline,
which creates an argument adaptor frame underneath the activation record
for the callee. This is done so that the callee can still get to the
actual arguments, using either
1. the arguments object, or
2. rest parameters (to get to superfluous arguments), or
3. the non-standard Function.arguments accessor (for sloppy mode
functions), or
4. direct eval(), where we don't know whether there's a use of the
arguments object hiding somewhere in the string.
However going through the arguments adaptor trampoline is quite
expensive usually, it seems to be responsible for over 60% of the
call overhead in those cases.
So this adds a fast path for the case of calling strict mode functions
where we have an arguments mismatch, but where we are sure that the
callee cannot observe the actual arguments. We use a bit on the
SharedFunctionInfo to indicate that this is safe, which is controlled
by hints from the Parser which knows whether the callee uses either
arguments object or rest parameters.
In those cases we use a direct call from optimized code, passing the
expected arguments instead of the actual arguments. This improves the
benchmark on the document below by around 60-65%, which is exactly
the overhead of the arguments adaptor trampoline that we save in this
case.
This also adds a runtime flag --fast_calls_with_arguments_mismatches,
which can be used to turn off the new behavior. This might be handy
for checking the performance impact via Finch.
Bug: v8:8895
Change-Id: Idea51dba7ee6cb989e86e0742eaf3516e5afe3c4
Cq-Include-Trybots: luci.chromium.try:linux-blink-rel
Doc: http://bit.ly/v8-faster-calls-with-arguments-mismatch
Reviewed-on: https://chromium-review.googlesource.com/c/1482735
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59825}
2019-02-25 10:55:53 +00:00
|
|
|
TEST(SafeToSkipArgumentsAdaptor) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
CompileRun(
|
|
|
|
"function a() { \"use strict\"; }; a();"
|
|
|
|
"function b() { }; b();"
|
|
|
|
"function c() { \"use strict\"; return arguments; }; c();"
|
|
|
|
"function d(...args) { return args; }; d();"
|
|
|
|
"function e() { \"use strict\"; return eval(\"\"); }; e();"
|
|
|
|
"function f(x, y) { \"use strict\"; return x + y; }; f(1, 2);");
|
|
|
|
Handle<JSFunction> a = Handle<JSFunction>::cast(GetGlobalProperty("a"));
|
|
|
|
CHECK(a->shared().is_safe_to_skip_arguments_adaptor());
|
|
|
|
Handle<JSFunction> b = Handle<JSFunction>::cast(GetGlobalProperty("b"));
|
|
|
|
CHECK(!b->shared().is_safe_to_skip_arguments_adaptor());
|
|
|
|
Handle<JSFunction> c = Handle<JSFunction>::cast(GetGlobalProperty("c"));
|
|
|
|
CHECK(!c->shared().is_safe_to_skip_arguments_adaptor());
|
|
|
|
Handle<JSFunction> d = Handle<JSFunction>::cast(GetGlobalProperty("d"));
|
|
|
|
CHECK(!d->shared().is_safe_to_skip_arguments_adaptor());
|
|
|
|
Handle<JSFunction> e = Handle<JSFunction>::cast(GetGlobalProperty("e"));
|
|
|
|
CHECK(!e->shared().is_safe_to_skip_arguments_adaptor());
|
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(GetGlobalProperty("f"));
|
|
|
|
CHECK(f->shared().is_safe_to_skip_arguments_adaptor());
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:08:45 +00:00
|
|
|
TEST(ShallowEagerCompilation) {
|
|
|
|
i::FLAG_always_opt = false;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext env;
|
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::String> source = v8_str(
|
|
|
|
"function f(x) {"
|
|
|
|
" return x + x;"
|
|
|
|
"}"
|
|
|
|
"f(2)");
|
|
|
|
v8::ScriptCompiler::Source script_source(source);
|
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
v8::ScriptCompiler::Compile(env.local(), &script_source,
|
|
|
|
v8::ScriptCompiler::kEagerCompile)
|
|
|
|
.ToLocalChecked();
|
|
|
|
{
|
|
|
|
v8::internal::DisallowCompilation no_compile_expected(isolate);
|
|
|
|
v8::Local<v8::Value> result = script->Run(env.local()).ToLocalChecked();
|
|
|
|
CHECK_EQ(4, result->Int32Value(env.local()).FromJust());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DeepEagerCompilation) {
|
|
|
|
i::FLAG_always_opt = false;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext env;
|
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::String> source = v8_str(
|
|
|
|
"function f(x) {"
|
|
|
|
" function g(x) {"
|
|
|
|
" function h(x) {"
|
|
|
|
" return x ** x;"
|
|
|
|
" }"
|
|
|
|
" return h(x) * h(x);"
|
|
|
|
" }"
|
|
|
|
" return g(x) + g(x);"
|
|
|
|
"}"
|
|
|
|
"f(2)");
|
|
|
|
v8::ScriptCompiler::Source script_source(source);
|
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
v8::ScriptCompiler::Compile(env.local(), &script_source,
|
|
|
|
v8::ScriptCompiler::kEagerCompile)
|
|
|
|
.ToLocalChecked();
|
|
|
|
{
|
|
|
|
v8::internal::DisallowCompilation no_compile_expected(isolate);
|
|
|
|
v8::Local<v8::Value> result = script->Run(env.local()).ToLocalChecked();
|
|
|
|
CHECK_EQ(32, result->Int32Value(env.local()).FromJust());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 06:51:53 +00:00
|
|
|
TEST(DeepEagerCompilationPeakMemory) {
|
|
|
|
i::FLAG_always_opt = false;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::String> source = v8_str(
|
|
|
|
"function f() {"
|
|
|
|
" function g1() {"
|
|
|
|
" function h1() {"
|
|
|
|
" function i1() {}"
|
|
|
|
" function i2() {}"
|
|
|
|
" }"
|
|
|
|
" function h2() {"
|
|
|
|
" function i1() {}"
|
|
|
|
" function i2() {}"
|
|
|
|
" }"
|
|
|
|
" }"
|
|
|
|
" function g2() {"
|
|
|
|
" function h1() {"
|
|
|
|
" function i1() {}"
|
|
|
|
" function i2() {}"
|
|
|
|
" }"
|
|
|
|
" function h2() {"
|
|
|
|
" function i1() {}"
|
|
|
|
" function i2() {}"
|
|
|
|
" }"
|
|
|
|
" }"
|
|
|
|
"}");
|
|
|
|
v8::ScriptCompiler::Source script_source(source);
|
2019-09-18 18:00:44 +00:00
|
|
|
CcTest::i_isolate()->compilation_cache()->DisableScriptAndEval();
|
2018-11-08 06:51:53 +00:00
|
|
|
|
|
|
|
v8::HeapStatistics heap_statistics;
|
|
|
|
CcTest::isolate()->GetHeapStatistics(&heap_statistics);
|
|
|
|
size_t peak_mem_1 = heap_statistics.peak_malloced_memory();
|
|
|
|
printf("peak memory after init: %8zu\n", peak_mem_1);
|
|
|
|
|
|
|
|
v8::ScriptCompiler::Compile(env.local(), &script_source,
|
|
|
|
v8::ScriptCompiler::kNoCompileOptions)
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
|
|
|
CcTest::isolate()->GetHeapStatistics(&heap_statistics);
|
|
|
|
size_t peak_mem_2 = heap_statistics.peak_malloced_memory();
|
|
|
|
printf("peak memory after lazy compile: %8zu\n", peak_mem_2);
|
|
|
|
|
|
|
|
v8::ScriptCompiler::Compile(env.local(), &script_source,
|
|
|
|
v8::ScriptCompiler::kNoCompileOptions)
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
|
|
|
CcTest::isolate()->GetHeapStatistics(&heap_statistics);
|
|
|
|
size_t peak_mem_3 = heap_statistics.peak_malloced_memory();
|
|
|
|
printf("peak memory after lazy compile: %8zu\n", peak_mem_3);
|
|
|
|
|
|
|
|
v8::ScriptCompiler::Compile(env.local(), &script_source,
|
|
|
|
v8::ScriptCompiler::kEagerCompile)
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
|
|
|
CcTest::isolate()->GetHeapStatistics(&heap_statistics);
|
|
|
|
size_t peak_mem_4 = heap_statistics.peak_malloced_memory();
|
|
|
|
printf("peak memory after eager compile: %8zu\n", peak_mem_4);
|
|
|
|
|
|
|
|
CHECK_LE(peak_mem_1, peak_mem_2);
|
|
|
|
CHECK_EQ(peak_mem_2, peak_mem_3);
|
|
|
|
CHECK_LE(peak_mem_3, peak_mem_4);
|
2018-11-08 09:02:14 +00:00
|
|
|
// Check that eager compilation does not cause significantly higher (+100%)
|
2018-11-08 06:51:53 +00:00
|
|
|
// peak memory than lazy compilation.
|
2018-11-08 09:02:14 +00:00
|
|
|
CHECK_LE(peak_mem_4 - peak_mem_3, peak_mem_3);
|
2018-11-08 06:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 12:26:41 +00:00
|
|
|
// TODO(mslekova): Remove the duplication with test-heap.cc
|
|
|
|
static int AllocationSitesCount(Heap* heap) {
|
|
|
|
int count = 0;
|
2018-12-25 00:19:47 +00:00
|
|
|
for (Object site = heap->allocation_sites_list(); site.IsAllocationSite();) {
|
2018-12-14 22:15:17 +00:00
|
|
|
AllocationSite cur = AllocationSite::cast(site);
|
2018-12-13 12:26:41 +00:00
|
|
|
CHECK(cur.HasWeakNext());
|
|
|
|
site = cur.weak_next();
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test simulates a specific race-condition if GC is triggered just
|
|
|
|
// before CompilationDependencies::Commit is finished, and this changes
|
|
|
|
// the pretenuring decision, thus causing a deoptimization.
|
|
|
|
TEST(DecideToPretenureDuringCompilation) {
|
|
|
|
// The test makes use of optimization and relies on deterministic
|
|
|
|
// compilation.
|
2020-07-15 06:14:54 +00:00
|
|
|
if (!i::FLAG_opt || i::FLAG_always_opt || i::FLAG_minor_mc ||
|
|
|
|
i::FLAG_stress_incremental_marking || i::FLAG_optimize_for_size ||
|
2020-09-03 14:30:07 +00:00
|
|
|
i::FLAG_turbo_nci || i::FLAG_turbo_nci_as_midtier ||
|
|
|
|
i::FLAG_stress_concurrent_allocation) {
|
2018-12-13 12:26:41 +00:00
|
|
|
return;
|
2020-07-15 06:14:54 +00:00
|
|
|
}
|
2018-12-13 12:26:41 +00:00
|
|
|
|
|
|
|
FLAG_stress_gc_during_compilation = true;
|
|
|
|
FLAG_allow_natives_syntax = true;
|
|
|
|
FLAG_allocation_site_pretenuring = true;
|
2019-01-03 17:13:10 +00:00
|
|
|
FLAG_flush_bytecode = false;
|
2018-12-13 12:26:41 +00:00
|
|
|
|
|
|
|
// We want to trigger exactly 1 optimization.
|
|
|
|
FLAG_use_osr = false;
|
|
|
|
|
|
|
|
// We'll do manual initialization.
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
|
|
|
|
// This setting ensures Heap::MaximumSizeScavenge will return `true`.
|
|
|
|
// We need to initialize the heap with at least 1 page, while keeping the
|
|
|
|
// limit low, to ensure the new space fills even on 32-bit architectures.
|
2019-06-05 21:08:15 +00:00
|
|
|
create_params.constraints.set_max_young_generation_size_in_bytes(
|
|
|
|
3 * Page::kPageSize);
|
2018-12-13 12:26:41 +00:00
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(create_params);
|
|
|
|
|
|
|
|
isolate->Enter();
|
|
|
|
{
|
|
|
|
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
|
|
|
Heap* heap = i_isolate->heap();
|
|
|
|
GlobalHandles* global_handles = i_isolate->global_handles();
|
|
|
|
HandleScope handle_scope(i_isolate);
|
|
|
|
|
|
|
|
// The allocation site at the head of the list is ours.
|
|
|
|
Handle<AllocationSite> site;
|
|
|
|
{
|
|
|
|
LocalContext context(isolate);
|
|
|
|
v8::HandleScope scope(context->GetIsolate());
|
|
|
|
|
|
|
|
int count = AllocationSitesCount(heap);
|
|
|
|
CompileRun(
|
|
|
|
"let arr = [];"
|
|
|
|
"function foo(shouldKeep) {"
|
|
|
|
" let local_array = new Array();"
|
|
|
|
" if (shouldKeep) arr.push(local_array);"
|
|
|
|
"}"
|
|
|
|
"function bar(shouldKeep) {"
|
|
|
|
" for (let i = 0; i < 10000; i++) {"
|
|
|
|
" foo(shouldKeep);"
|
|
|
|
" }"
|
|
|
|
"}"
|
2019-04-30 11:04:41 +00:00
|
|
|
"%PrepareFunctionForOptimization(bar);"
|
2018-12-13 12:26:41 +00:00
|
|
|
"bar();");
|
|
|
|
|
|
|
|
// This number should be >= kPretenureRatio * 10000,
|
|
|
|
// where 10000 is the number of iterations in `bar`,
|
|
|
|
// in order to make the ratio in DigestPretenuringFeedback close to 1.
|
|
|
|
const int memento_found_bump = 8500;
|
|
|
|
|
|
|
|
// One allocation site should have been created.
|
|
|
|
int new_count = AllocationSitesCount(heap);
|
|
|
|
CHECK_EQ(new_count, (count + 1));
|
|
|
|
site = Handle<AllocationSite>::cast(global_handles->Create(
|
|
|
|
AllocationSite::cast(heap->allocation_sites_list())));
|
|
|
|
site->set_memento_found_count(memento_found_bump);
|
|
|
|
|
|
|
|
CompileRun("%OptimizeFunctionOnNextCall(bar);");
|
|
|
|
CompileRun("bar(true);");
|
|
|
|
|
|
|
|
// The last call should have caused `foo` to bail out of compilation
|
|
|
|
// due to dependency change (the pretenuring decision in this case).
|
|
|
|
// This will cause recompilation.
|
|
|
|
|
|
|
|
// Check `bar` can get optimized again, meaning the compiler state is
|
|
|
|
// recoverable from this point.
|
2019-04-30 11:04:41 +00:00
|
|
|
CompileRun(
|
|
|
|
"%PrepareFunctionForOptimization(bar);"
|
|
|
|
"%OptimizeFunctionOnNextCall(bar);");
|
2018-12-13 12:26:41 +00:00
|
|
|
CompileRun("bar();");
|
|
|
|
|
|
|
|
Handle<Object> foo_obj =
|
|
|
|
JSReceiver::GetProperty(i_isolate, i_isolate->global_object(), "bar")
|
|
|
|
.ToHandleChecked();
|
|
|
|
Handle<JSFunction> bar = Handle<JSFunction>::cast(foo_obj);
|
|
|
|
|
2020-08-11 09:24:47 +00:00
|
|
|
CHECK(bar->HasAttachedOptimizedCode());
|
2018-12-13 12:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
isolate->Exit();
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:26:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Dummy external source stream which returns the whole source in one go.
|
|
|
|
class DummySourceStream : public v8::ScriptCompiler::ExternalSourceStream {
|
|
|
|
public:
|
|
|
|
explicit DummySourceStream(const char* source) : done_(false) {
|
|
|
|
source_length_ = static_cast<int>(strlen(source));
|
|
|
|
source_buffer_ = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetMoreData(const uint8_t** dest) override {
|
|
|
|
if (done_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t* buf = new uint8_t[source_length_ + 1];
|
|
|
|
memcpy(buf, source_buffer_, source_length_ + 1);
|
|
|
|
*dest = buf;
|
|
|
|
done_ = true;
|
|
|
|
return source_length_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int source_length_;
|
|
|
|
const char* source_buffer_;
|
|
|
|
bool done_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Tests that doing something that causes source positions to need to be
|
|
|
|
// collected after a background compilation task has started does result in
|
|
|
|
// source positions being collected.
|
|
|
|
TEST(ProfilerEnabledDuringBackgroundCompile) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
const char* source = "var a = 0;";
|
|
|
|
|
|
|
|
v8::ScriptCompiler::StreamedSource streamed_source(
|
|
|
|
std::make_unique<DummySourceStream>(source),
|
|
|
|
v8::ScriptCompiler::StreamedSource::UTF8);
|
|
|
|
std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask> task(
|
|
|
|
v8::ScriptCompiler::StartStreamingScript(isolate, &streamed_source));
|
|
|
|
|
|
|
|
// Run the background compilation task on the main thread.
|
|
|
|
task->Run();
|
|
|
|
|
|
|
|
// Enable the CPU profiler.
|
|
|
|
auto* cpu_profiler = v8::CpuProfiler::New(isolate, v8::kStandardNaming);
|
|
|
|
v8::Local<v8::String> profile = v8_str("profile");
|
|
|
|
cpu_profiler->StartProfiling(profile);
|
|
|
|
|
|
|
|
// Finalize the background compilation task ensuring it completed
|
|
|
|
// successfully.
|
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
v8::ScriptCompiler::Compile(isolate->GetCurrentContext(),
|
|
|
|
&streamed_source, v8_str(source),
|
|
|
|
v8::ScriptOrigin(v8_str("foo")))
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
|
|
|
i::Handle<i::Object> obj = Utils::OpenHandle(*script);
|
|
|
|
CHECK(i::JSFunction::cast(*obj).shared().AreSourcePositionsAvailable());
|
|
|
|
|
|
|
|
cpu_profiler->StopProfiling(profile);
|
|
|
|
}
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|