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
|
|
|
|
2016-09-02 17:48:31 +00:00
|
|
|
#include "src/api.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/compiler.h"
|
|
|
|
#include "src/disasm.h"
|
2016-09-02 17:48:31 +00:00
|
|
|
#include "src/factory.h"
|
2016-05-19 12:27:18 +00:00
|
|
|
#include "src/interpreter/interpreter.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2017-10-16 10:55:06 +00:00
|
|
|
LanguageMode::kSloppy)
|
|
|
|
.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(
|
|
|
|
source_code, MaybeHandle<String>(), 0, 0, v8::ScriptOriginOptions(),
|
|
|
|
MaybeHandle<Object>(), Handle<Context>(isolate->native_context()),
|
2017-10-13 16:33:03 +00:00
|
|
|
nullptr, nullptr, v8::ScriptCompiler::kNoCompileOptions,
|
2017-11-01 16:16:35 +00:00
|
|
|
ScriptCompiler::kNoCacheNoReason, NOT_NATIVES_CODE,
|
|
|
|
MaybeHandle<FixedArray>())
|
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
|
|
|
|
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());
|
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));
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
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));
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
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));
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
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());
|
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());
|
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());
|
2013-09-05 08:48:34 +00:00
|
|
|
Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
|
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();
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
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 =
|
|
|
|
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());
|
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());
|
|
|
|
|
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) {
|
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.
|
|
|
|
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
|
|
|
|
|
|
|
// Verify that we gathered feedback.
|
2017-06-13 13:56:11 +00:00
|
|
|
Handle<FeedbackVector> feedback_vector(f->feedback_vector());
|
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);
|
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-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-11-02 11:17:01 +00:00
|
|
|
if (i::FLAG_always_opt || !i::FLAG_lazy) {
|
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();");
|
|
|
|
|
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();"
|
2017-02-10 15:19:19 +00:00
|
|
|
"var closure1 = MakeClosure();" // We only share optimized code
|
|
|
|
// if there are at least two closures.
|
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())));
|
2017-04-28 15:36:46 +00:00
|
|
|
CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_optimizer());
|
|
|
|
CHECK(fun2->IsOptimized() || !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());
|
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();
|
2017-12-18 08:10:06 +00:00
|
|
|
CHECK_EQ(1, fun->Get(env.local(), v8_str("length"))
|
|
|
|
.ToLocalChecked()
|
|
|
|
->ToInt32(env.local())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Value());
|
2015-02-12 14:38:54 +00:00
|
|
|
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(
|
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);
|
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, 0, nullptr)
|
2015-11-20 12:56:19 +00:00
|
|
|
.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);
|
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);
|
2015-07-16 12:08:01 +00:00
|
|
|
v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
|
|
|
|
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() {
|
2017-04-03 12:48:26 +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( \
|
2017-08-24 21:49:48 +00:00
|
|
|
CcTest::isolate(), \
|
2017-04-03 12:48:26 +00:00
|
|
|
try_catch.Exception()->ToString(context).ToLocalChecked()); \
|
|
|
|
V8_Fatal(__FILE__, __LINE__, \
|
|
|
|
"Unexpected exception thrown during %s:\n\t%s\n", op, *error); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
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(CompileFunctionInContextHarmonyFunctionToString) {
|
|
|
|
v8::internal::FLAG_harmony_function_tostring = true;
|
|
|
|
TestCompileFunctionInContextToStringImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileFunctionInContextFunctionToString) {
|
|
|
|
TestCompileFunctionInContextToStringImpl();
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:20:08 +00:00
|
|
|
TEST(InvocationCount) {
|
|
|
|
FLAG_allow_natives_syntax = true;
|
|
|
|
FLAG_always_opt = false;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"function bar() {};"
|
|
|
|
"function foo() { return bar(); };"
|
|
|
|
"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
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|