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"
|
2014-07-08 09:04:08 +00:00
|
|
|
#include "src/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();
|
2014-04-11 12:47:34 +00:00
|
|
|
return Object::GetProperty(
|
2014-04-16 13:28:11 +00:00
|
|
|
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();
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_function = Compiler::CompileScript(
|
|
|
|
source_code, Handle<String>(), 0, 0, false,
|
|
|
|
Handle<Context>(isolate->native_context()), NULL, NULL,
|
|
|
|
v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE);
|
2013-06-04 10:30:05 +00:00
|
|
|
return isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
shared_function, 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
|
|
|
|
2014-04-09 12:21:47 +00:00
|
|
|
Handle<String> foo_string = isolate->factory()->InternalizeOneByteString(
|
|
|
|
STATIC_ASCII_VECTOR("foo"));
|
|
|
|
Handle<Object> fun1 = Object::GetProperty(
|
2014-04-11 12:47:34 +00:00
|
|
|
isolate->global_object(), foo_string).ToHandleChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(fun1->IsJSFunction());
|
|
|
|
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString(
|
|
|
|
STATIC_ASCII_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,
|
|
|
|
ARRAY_SIZE(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());
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::ScriptOrigin origin =
|
|
|
|
v8::ScriptOrigin(v8::String::NewFromUtf8(CcTest::isolate(), "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);
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::String> script_body =
|
|
|
|
v8::String::NewFromUtf8(CcTest::isolate(), buffer.start());
|
2010-02-16 12:08:10 +00:00
|
|
|
v8::Script::Compile(script_body, &origin)->Run();
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Local<v8::Function> f =
|
|
|
|
v8::Local<v8::Function>::Cast(context->Global()->Get(
|
|
|
|
v8::String::NewFromUtf8(CcTest::isolate(), "f")));
|
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());
|
|
|
|
|
|
|
|
// Make sure function f has a call that uses a type feedback slot.
|
|
|
|
CompileRun("function fun() {};"
|
|
|
|
"fun1 = fun;"
|
|
|
|
"function f(a) { a(); } f(fun1);");
|
|
|
|
|
|
|
|
Handle<JSFunction> f =
|
|
|
|
v8::Utils::OpenHandle(
|
|
|
|
*v8::Handle<v8::Function>::Cast(
|
|
|
|
CcTest::global()->Get(v8_str("f"))));
|
|
|
|
|
|
|
|
// We shouldn't have deoptimization support. We want to recompile and
|
|
|
|
// verify that our feedback vector preserves information.
|
|
|
|
CHECK(!f->shared()->has_deoptimization_support());
|
|
|
|
Handle<FixedArray> feedback_vector(f->shared()->feedback_vector());
|
|
|
|
|
|
|
|
// Verify that we gathered feedback.
|
|
|
|
CHECK_EQ(1, feedback_vector->length());
|
|
|
|
CHECK(feedback_vector->get(0)->IsJSFunction());
|
|
|
|
|
|
|
|
CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);");
|
|
|
|
|
|
|
|
// Verify that the feedback is still "gathered" despite a recompilation
|
|
|
|
// of the full code.
|
|
|
|
CHECK(f->IsOptimized());
|
|
|
|
CHECK(f->shared()->has_deoptimization_support());
|
|
|
|
CHECK(f->shared()->feedback_vector()->get(0)->IsJSFunction());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FeedbackVectorUnaffectedByScopeChanges) {
|
|
|
|
if (i::FLAG_always_opt || !i::FLAG_lazy) return;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
CompileRun("function builder() {"
|
|
|
|
" call_target = function() { return 3; };"
|
|
|
|
" return (function() {"
|
|
|
|
" eval('');"
|
|
|
|
" return function() {"
|
|
|
|
" 'use strict';"
|
|
|
|
" call_target();"
|
|
|
|
" }"
|
|
|
|
" })();"
|
|
|
|
"}"
|
|
|
|
"morphing_call = builder();");
|
|
|
|
|
|
|
|
Handle<JSFunction> f =
|
|
|
|
v8::Utils::OpenHandle(
|
|
|
|
*v8::Handle<v8::Function>::Cast(
|
|
|
|
CcTest::global()->Get(v8_str("morphing_call"))));
|
|
|
|
|
|
|
|
// morphing_call should have one feedback vector slot for the call to
|
|
|
|
// call_target().
|
|
|
|
CHECK_EQ(1, f->shared()->feedback_vector()->length());
|
|
|
|
// And yet it's not compiled.
|
|
|
|
CHECK(!f->shared()->is_compiled());
|
|
|
|
|
|
|
|
CompileRun("morphing_call();");
|
|
|
|
|
|
|
|
// The vector should have the same size despite the new scoping.
|
|
|
|
CHECK_EQ(1, f->shared()->feedback_vector()->length());
|
|
|
|
CHECK(f->shared()->is_compiled());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-18 13:26:43 +00:00
|
|
|
// Test that optimized code for different closures is actually shared
|
|
|
|
// immediately by the FastNewClosureStub when run in the same context.
|
|
|
|
TEST(OptimizedCodeSharing) {
|
2012-06-22 12:47:11 +00:00
|
|
|
// Skip test if --cache-optimized-code is not activated by default because
|
|
|
|
// FastNewClosureStub that is baked into the snapshot is incorrect.
|
|
|
|
if (!FLAG_cache_optimized_code) return;
|
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());
|
2012-06-18 13:26:43 +00:00
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
LocalContext env;
|
2013-11-22 12:43:17 +00:00
|
|
|
env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"),
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Integer::New(CcTest::isolate(), i));
|
2012-06-18 13:26:43 +00:00
|
|
|
CompileRun("function MakeClosure() {"
|
|
|
|
" return function() { return x; };"
|
|
|
|
"}"
|
|
|
|
"var closure0 = MakeClosure();"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"%OptimizeFunctionOnNextCall(closure0);"
|
|
|
|
"%DebugPrint(closure0());"
|
|
|
|
"var closure1 = MakeClosure();"
|
|
|
|
"var closure2 = MakeClosure();");
|
|
|
|
Handle<JSFunction> fun1 = v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1"))));
|
|
|
|
Handle<JSFunction> fun2 = v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2"))));
|
2013-10-23 08:57:54 +00:00
|
|
|
CHECK(fun1->IsOptimized()
|
|
|
|
|| !CcTest::i_isolate()->use_crankshaft() || !fun1->IsOptimizable());
|
|
|
|
CHECK(fun2->IsOptimized()
|
|
|
|
|| !CcTest::i_isolate()->use_crankshaft() || !fun2->IsOptimizable());
|
2012-06-18 13:26:43 +00:00
|
|
|
CHECK_EQ(fun1->code(), fun2->code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-11 14:16:24 +00:00
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
|
|
|
static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
|
|
|
|
const char* property_name) {
|
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
|
|
|
|
return v8::Utils::OpenHandle(*fun);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()));
|
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
|