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>
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/compiler.h"
|
|
|
|
#include "src/disasm.h"
|
2016-05-19 12:27:18 +00:00
|
|
|
#include "src/interpreter/interpreter.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/parsing/parser.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-06-27 13:48:37 +00:00
|
|
|
Runtime::SetObjectProperty(isolate, global, internalized_name, object,
|
2014-04-04 12:06:11 +00:00
|
|
|
SLOPPY).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();
|
2016-03-08 12:07:27 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = Compiler::GetSharedFunctionInfoForScript(
|
2015-05-19 03:11:33 +00:00
|
|
|
source_code, Handle<String>(), 0, 0, v8::ScriptOriginOptions(),
|
|
|
|
Handle<Object>(), Handle<Context>(isolate->native_context()), NULL, NULL,
|
2015-02-06 17:52:20 +00:00
|
|
|
v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE, false);
|
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
|
|
|
|
2008-09-11 14:34:48 +00:00
|
|
|
Handle<JSFunction> fun = Compile(buffer.start());
|
2008-08-22 13:33:59 +00:00
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, NULL).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));
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, NULL).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));
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, NULL).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));
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
Execution::Call(isolate, fun, global, 0, NULL).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());
|
2013-09-19 13:30:47 +00:00
|
|
|
v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
|
|
|
|
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;
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
Execution::Call(CcTest::i_isolate(), fun, global, 0, NULL).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());
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
|
|
|
|
Execution::Call(
|
2014-04-11 10:41:09 +00:00
|
|
|
CcTest::i_isolate(), fun, global, 0, NULL).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();
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
CHECK(Execution::Call(isolate, fun, global, 0, NULL).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 =
|
|
|
|
CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION);
|
|
|
|
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'.
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-04-11 10:41:09 +00:00
|
|
|
Execution::Call(isolate, fun0, global, 0, NULL).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());
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<Object> argv[] = {isolate->factory()->InternalizeOneByteString(
|
|
|
|
STATIC_CHAR_VECTOR("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());
|
2013-09-19 09:46:15 +00:00
|
|
|
script->set_source(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);
|
|
|
|
memset(buffer.start(), '\n', buffer_size - 1);
|
|
|
|
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);
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::String> script_body = v8_str(buffer.start());
|
|
|
|
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) {
|
|
|
|
if (i::FLAG_always_opt || !i::FLAG_crankshaft) return;
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
if (!CcTest::i_isolate()->use_crankshaft()) return;
|
|
|
|
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.
|
|
|
|
CompileRun("function fun() {};"
|
|
|
|
"fun1 = fun;"
|
|
|
|
"function f(a) { a(); } f(fun1);");
|
|
|
|
|
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
|
|
|
|
|
|
|
// We shouldn't have deoptimization support. We want to recompile and
|
|
|
|
// verify that our feedback vector preserves information.
|
|
|
|
CHECK(!f->shared()->has_deoptimization_support());
|
2016-05-27 08:09:12 +00:00
|
|
|
Handle<TypeFeedbackVector> feedback_vector(f->feedback_vector());
|
2014-04-30 10:51:01 +00:00
|
|
|
|
|
|
|
// Verify that we gathered feedback.
|
2015-10-01 13:48:05 +00:00
|
|
|
CHECK(!feedback_vector->is_empty());
|
|
|
|
FeedbackVectorSlot slot_for_a(0);
|
2015-02-04 09:46:05 +00:00
|
|
|
Object* object = feedback_vector->Get(slot_for_a);
|
|
|
|
CHECK(object->IsWeakCell() &&
|
|
|
|
WeakCell::cast(object)->value()->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.
|
|
|
|
CHECK(f->IsOptimized());
|
2016-07-13 07:58:40 +00:00
|
|
|
// If the baseline code is bytecode, then it will not have deoptimization
|
|
|
|
// support. has_deoptimization_support() check is only required if the
|
|
|
|
// baseline code is from fullcodegen.
|
|
|
|
CHECK(f->shared()->has_deoptimization_support() || i::FLAG_ignition);
|
2016-05-27 08:09:12 +00:00
|
|
|
object = f->feedback_vector()->Get(slot_for_a);
|
2015-02-04 09:46:05 +00:00
|
|
|
CHECK(object->IsWeakCell() &&
|
|
|
|
WeakCell::cast(object)->value()->IsJSFunction());
|
2014-04-30 10:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FeedbackVectorUnaffectedByScopeChanges) {
|
2016-03-24 18:37:56 +00:00
|
|
|
if (i::FLAG_always_opt || !i::FLAG_lazy ||
|
|
|
|
(FLAG_ignition && FLAG_ignition_eager)) {
|
|
|
|
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());
|
2016-05-27 08:09:12 +00:00
|
|
|
CHECK(f->feedback_vector()->is_empty());
|
2014-04-30 10:51:01 +00:00
|
|
|
|
|
|
|
CompileRun("morphing_call();");
|
|
|
|
|
2014-11-14 08:21:13 +00:00
|
|
|
// Now a feedback vector is allocated.
|
|
|
|
CHECK(f->shared()->is_compiled());
|
2016-05-27 08:09:12 +00:00
|
|
|
CHECK(!f->feedback_vector()->is_empty());
|
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();"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());"
|
2016-04-13 10:54:31 +00:00
|
|
|
"var closure1 = MakeClosure(); closure1();"
|
|
|
|
"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())));
|
2015-05-22 10:05:16 +00:00
|
|
|
CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
2012-06-18 13:26:43 +00:00
|
|
|
CHECK_EQ(fun1->code(), fun2->code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(OptimizedCodeSharing2) {
|
|
|
|
if (FLAG_stress_compaction) return;
|
|
|
|
FLAG_allow_natives_syntax = true;
|
2015-10-07 12:10:49 +00:00
|
|
|
FLAG_native_context_specialization = false;
|
2015-06-26 09:07:30 +00:00
|
|
|
FLAG_turbo_cache_shared_code = true;
|
|
|
|
const char* flag = "--turbo-filter=*";
|
|
|
|
FlagList::SetFlagsFromString(flag, StrLength(flag));
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Script> script = v8_compile(
|
|
|
|
"function MakeClosure() {"
|
|
|
|
" return function() { return x; };"
|
|
|
|
"}");
|
|
|
|
Handle<Code> reference_code;
|
|
|
|
{
|
|
|
|
LocalContext env;
|
2015-11-20 12:56:19 +00:00
|
|
|
env->Global()
|
|
|
|
->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
|
|
|
|
.FromJust();
|
|
|
|
script->GetUnboundScript()
|
|
|
|
->BindToCurrentContext()
|
|
|
|
->Run(env.local())
|
|
|
|
.ToLocalChecked();
|
2015-06-26 09:07:30 +00:00
|
|
|
CompileRun(
|
|
|
|
"var closure0 = MakeClosure();"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());");
|
2015-10-23 12:26:49 +00:00
|
|
|
Handle<JSFunction> fun0 = 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("closure0"))
|
|
|
|
.ToLocalChecked())));
|
2015-06-26 09:07:30 +00:00
|
|
|
CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
reference_code = handle(fun0->code());
|
|
|
|
}
|
2015-08-07 13:38:55 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-06-26 09:07:30 +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();
|
|
|
|
script->GetUnboundScript()
|
|
|
|
->BindToCurrentContext()
|
|
|
|
->Run(env.local())
|
|
|
|
.ToLocalChecked();
|
2015-07-22 11:46:04 +00:00
|
|
|
CompileRun(
|
|
|
|
"var closure0 = MakeClosure();"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());"
|
2016-04-13 10:54:31 +00:00
|
|
|
"var closure1 = MakeClosure(); closure1();"
|
|
|
|
"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())));
|
2015-07-22 11:46:04 +00:00
|
|
|
CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
CHECK_EQ(*reference_code, fun1->code());
|
|
|
|
CHECK_EQ(*reference_code, fun2->code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 10:54:31 +00:00
|
|
|
// Test that optimized code for different closures is actually shared.
|
2015-07-22 11:46:04 +00:00
|
|
|
TEST(OptimizedCodeSharing3) {
|
|
|
|
if (FLAG_stress_compaction) return;
|
|
|
|
FLAG_allow_natives_syntax = true;
|
2015-10-07 12:10:49 +00:00
|
|
|
FLAG_native_context_specialization = false;
|
2015-07-22 11:46:04 +00:00
|
|
|
FLAG_turbo_cache_shared_code = true;
|
|
|
|
const char* flag = "--turbo-filter=*";
|
|
|
|
FlagList::SetFlagsFromString(flag, StrLength(flag));
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Script> script = v8_compile(
|
|
|
|
"function MakeClosure() {"
|
|
|
|
" return function() { return x; };"
|
|
|
|
"}");
|
|
|
|
Handle<Code> reference_code;
|
|
|
|
{
|
|
|
|
LocalContext env;
|
2015-11-20 12:56:19 +00:00
|
|
|
env->Global()
|
|
|
|
->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
|
|
|
|
.FromJust();
|
|
|
|
script->GetUnboundScript()
|
|
|
|
->BindToCurrentContext()
|
|
|
|
->Run(env.local())
|
|
|
|
.ToLocalChecked();
|
2015-07-22 11:46:04 +00:00
|
|
|
CompileRun(
|
|
|
|
"var closure0 = MakeClosure();"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());");
|
2015-10-23 12:26:49 +00:00
|
|
|
Handle<JSFunction> fun0 = 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("closure0"))
|
|
|
|
.ToLocalChecked())));
|
2015-07-22 11:46:04 +00:00
|
|
|
CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
reference_code = handle(fun0->code());
|
|
|
|
// Evict only the context-dependent entry from the optimized code map. This
|
|
|
|
// leaves it in a state where only the context-independent entry exists.
|
|
|
|
fun0->shared()->TrimOptimizedCodeMap(SharedFunctionInfo::kEntryLength);
|
|
|
|
}
|
2015-08-07 13:38:55 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-07-22 11:46:04 +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();
|
|
|
|
script->GetUnboundScript()
|
|
|
|
->BindToCurrentContext()
|
|
|
|
->Run(env.local())
|
|
|
|
.ToLocalChecked();
|
2015-06-26 09:07:30 +00:00
|
|
|
CompileRun(
|
|
|
|
"var closure0 = MakeClosure();"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());"
|
2016-04-13 10:54:31 +00:00
|
|
|
"var closure1 = MakeClosure(); closure1();"
|
|
|
|
"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())));
|
2015-06-26 09:07:30 +00:00
|
|
|
CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
|
|
|
|
CHECK_EQ(*reference_code, fun1->code());
|
|
|
|
CHECK_EQ(*reference_code, fun2->code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 15:15:29 +00:00
|
|
|
TEST(CompileFunctionInContext) {
|
|
|
|
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,
|
|
|
|
0, NULL, 1, &math)
|
|
|
|
.ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
2015-11-20 12:56:19 +00:00
|
|
|
fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
|
|
|
|
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,
|
|
|
|
0, NULL, 2, ext)
|
|
|
|
.ToLocalChecked();
|
2015-02-09 15:15:29 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
2015-11-20 12:56:19 +00:00
|
|
|
fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
|
|
|
|
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());
|
2015-02-12 14:38:54 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
|
|
|
|
v8::Local<v8::String> arg = v8_str("b");
|
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());
|
|
|
|
v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
|
2015-11-20 12:56:19 +00:00
|
|
|
fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
|
|
|
|
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());
|
2015-02-12 14:38:54 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(
|
|
|
|
v8_str("result = /* y + */ x + b // + z"));
|
|
|
|
v8::Local<v8::String> arg = v8_str("b");
|
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());
|
|
|
|
v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
|
2015-11-20 12:56:19 +00:00
|
|
|
fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
|
|
|
|
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(
|
|
|
|
env.local(), &script_source, 1, &arg, 0, NULL)
|
|
|
|
.IsEmpty());
|
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);
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
|
|
|
|
0, NULL, 0, NULL)
|
|
|
|
.ToLocalChecked();
|
2015-07-16 12:08:01 +00:00
|
|
|
CHECK(!fun.IsEmpty());
|
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);
|
2015-11-20 12:56:19 +00:00
|
|
|
CHECK(fun->Call(env.local(), env->Global(), 0, NULL).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());
|
|
|
|
CHECK(stack->GetFrameCount() > 0);
|
|
|
|
v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
|
|
|
|
CHECK_EQ(23, frame->GetLineNumber());
|
|
|
|
CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-11 14:16:24 +00:00
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
2015-11-20 12:56:19 +00:00
|
|
|
static Handle<JSFunction> GetJSFunction(v8::Local<v8::Object> obj,
|
2015-10-23 12:26:49 +00:00
|
|
|
const char* property_name) {
|
2015-11-20 12:56:19 +00:00
|
|
|
v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
|
|
|
|
obj->Get(CcTest::isolate()->GetCurrentContext(), v8_str(property_name))
|
|
|
|
.ToLocalChecked());
|
2015-10-23 12:26:49 +00:00
|
|
|
return Handle<JSFunction>::cast(v8::Utils::OpenHandle(*fun));
|
2011-05-11 14:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
|
|
|
|
// Create a disassembler with default name lookup.
|
|
|
|
disasm::NameConverter name_converter;
|
|
|
|
disasm::Disassembler d(name_converter);
|
|
|
|
|
|
|
|
if (f->code()->kind() == Code::FUNCTION) {
|
|
|
|
Address pc = f->code()->instruction_start();
|
|
|
|
int decode_size =
|
|
|
|
Min(f->code()->instruction_size(),
|
2013-04-10 09:24:31 +00:00
|
|
|
static_cast<int>(f->code()->back_edge_table_offset()));
|
2015-06-04 14:44:00 +00:00
|
|
|
if (FLAG_enable_embedded_constant_pool) {
|
|
|
|
decode_size = Min(decode_size, f->code()->constant_pool_offset());
|
|
|
|
}
|
2011-05-11 14:16:24 +00:00
|
|
|
Address end = pc + decode_size;
|
|
|
|
|
|
|
|
v8::internal::EmbeddedVector<char, 128> decode_buffer;
|
2012-06-26 13:56:48 +00:00
|
|
|
v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
|
|
|
|
Smi* smi = Smi::FromInt(12345678);
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(smi_hex_buffer, "0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(smi));
|
2011-05-11 14:16:24 +00:00
|
|
|
while (pc < end) {
|
2011-07-04 08:57:43 +00:00
|
|
|
int num_const = d.ConstantPoolSizeAt(pc);
|
|
|
|
if (num_const >= 0) {
|
2011-07-22 09:03:55 +00:00
|
|
|
pc += (num_const + 1) * kPointerSize;
|
2011-07-04 08:57:43 +00:00
|
|
|
} else {
|
|
|
|
pc += d.InstructionDecode(decode_buffer, pc);
|
2012-06-26 13:56:48 +00:00
|
|
|
CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
|
2011-07-04 08:57:43 +00:00
|
|
|
}
|
2011-05-11 14:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SplitConstantsInFullCompiler) {
|
2013-09-19 13:30:47 +00:00
|
|
|
LocalContext context;
|
2013-04-10 08:29:39 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-05-11 14:16:24 +00:00
|
|
|
|
|
|
|
CompileRun("function f() { a = 12345678 }; f();");
|
2013-09-19 13:30:47 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
CompileRun("function f(x) { a = 12345678 + x}; f(1);");
|
2013-09-19 13:30:47 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
|
2013-09-19 13:30:47 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
|
2013-09-19 13:30:47 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
2016-05-19 12:27:18 +00:00
|
|
|
|
|
|
|
static void IsBaselineCompiled(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Handle<Object> object = v8::Utils::OpenHandle(*args[0]);
|
|
|
|
Handle<JSFunction> function = Handle<JSFunction>::cast(object);
|
|
|
|
bool is_baseline = function->shared()->code()->kind() == Code::FUNCTION;
|
|
|
|
return args.GetReturnValue().Set(is_baseline);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InstallIsBaselineCompiledHelper(v8::Isolate* isolate) {
|
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
v8::Local<v8::FunctionTemplate> t =
|
|
|
|
v8::FunctionTemplate::New(isolate, IsBaselineCompiled);
|
|
|
|
CHECK(context->Global()
|
|
|
|
->Set(context, v8_str("IsBaselineCompiled"),
|
|
|
|
t->GetFunction(context).ToLocalChecked())
|
|
|
|
.FromJust());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IgnitionBaselineOnReturn) {
|
|
|
|
FLAG_allow_natives_syntax = true;
|
|
|
|
FLAG_always_opt = false;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
FLAG_ignition = true;
|
|
|
|
reinterpret_cast<i::Isolate*>(CcTest::isolate())->interpreter()->Initialize();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
InstallIsBaselineCompiledHelper(CcTest::isolate());
|
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"var is_baseline_in_function, is_baseline_after_return;\n"
|
|
|
|
"var return_val;\n"
|
|
|
|
"function f() {\n"
|
|
|
|
" %CompileBaseline(f);\n"
|
|
|
|
" is_baseline_in_function = IsBaselineCompiled(f);\n"
|
|
|
|
" return 1234;\n"
|
|
|
|
"};\n"
|
|
|
|
"return_val = f();\n"
|
|
|
|
"is_baseline_after_return = IsBaselineCompiled(f);\n");
|
|
|
|
CHECK_EQ(false, GetGlobalProperty("is_baseline_in_function")->BooleanValue());
|
|
|
|
CHECK_EQ(true, GetGlobalProperty("is_baseline_after_return")->BooleanValue());
|
|
|
|
CHECK_EQ(1234.0, GetGlobalProperty("return_val")->Number());
|
|
|
|
}
|