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
|
|
|
|
2013-05-02 20:18:42 +00:00
|
|
|
// TODO(dcarney): remove
|
|
|
|
#define V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
|
|
|
|
#define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "compiler.h"
|
2011-05-11 14:16:24 +00:00
|
|
|
#include "disasm.h"
|
|
|
|
#include "disassembler.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
#include "execution.h"
|
|
|
|
#include "factory.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
2008-10-03 08:30:22 +00:00
|
|
|
// --- P r i n t E x t e n s i o n ---
|
|
|
|
|
|
|
|
class PrintExtension : public v8::Extension {
|
|
|
|
public:
|
|
|
|
PrintExtension() : v8::Extension("v8/print", kSource) { }
|
|
|
|
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
|
|
|
|
v8::Handle<v8::String> name);
|
|
|
|
static v8::Handle<v8::Value> Print(const v8::Arguments& args);
|
|
|
|
private:
|
|
|
|
static const char* kSource;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const char* PrintExtension::kSource = "native function print();";
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction(
|
|
|
|
v8::Handle<v8::String> str) {
|
|
|
|
return v8::FunctionTemplate::New(PrintExtension::Print);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) {
|
|
|
|
for (int i = 0; i < args.Length(); i++) {
|
|
|
|
if (i != 0) printf(" ");
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(args.GetIsolate());
|
2012-09-13 15:06:15 +00:00
|
|
|
v8::String::Utf8Value str(args[i]);
|
|
|
|
if (*str == NULL) return v8::Undefined();
|
|
|
|
printf("%s", *str);
|
2008-10-03 08:30:22 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PrintExtension kPrintExtension;
|
|
|
|
v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
|
|
|
|
|
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
static MaybeObject* GetGlobalProperty(const char* name) {
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> internalized_name = FACTORY->InternalizeUtf8String(name);
|
|
|
|
return Isolate::Current()->context()->global_object()->GetProperty(
|
|
|
|
*internalized_name);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SetGlobalProperty(const char* name, Object* value) {
|
2012-11-22 07:58:59 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
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());
|
2013-02-28 17:03:34 +00:00
|
|
|
SetProperty(isolate, global, internalized_name, object, NONE, kNonStrictMode);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<JSFunction> Compile(const char* source) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source)));
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_function =
|
2010-03-17 08:14:59 +00:00
|
|
|
Compiler::Compile(source_code,
|
|
|
|
Handle<String>(),
|
|
|
|
0,
|
|
|
|
0,
|
2012-08-28 10:49:23 +00:00
|
|
|
Handle<Context>(Isolate::Current()->native_context()),
|
2010-03-17 08:14:59 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
Handle<String>::null(),
|
|
|
|
NOT_NATIVES_CODE);
|
2011-03-18 20:35:07 +00:00
|
|
|
return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function,
|
2012-08-17 09:03:08 +00:00
|
|
|
Isolate::Current()->native_context());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static double Inc(int x) {
|
|
|
|
const char* source = "result = %d + 1;";
|
2008-09-11 14:34:48 +00:00
|
|
|
EmbeddedVector<char, 512> buffer;
|
|
|
|
OS::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;
|
|
|
|
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
2010-10-25 15:22:03 +00:00
|
|
|
return GetGlobalProperty("result")->ToObjectChecked()->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());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(4.0, Inc(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static double Add(int x, int y) {
|
|
|
|
Handle<JSFunction> fun = Compile("result = x + y;");
|
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
|
|
|
SetGlobalProperty("x", Smi::FromInt(x));
|
|
|
|
SetGlobalProperty("y", Smi::FromInt(y));
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
2010-10-25 15:22:03 +00:00
|
|
|
return GetGlobalProperty("result")->ToObjectChecked()->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());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(5.0, Add(2, 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static double Abs(int x) {
|
|
|
|
Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
|
|
|
|
if (fun.is_null()) return -1;
|
|
|
|
|
|
|
|
SetGlobalProperty("x", Smi::FromInt(x));
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
2010-10-25 15:22:03 +00:00
|
|
|
return GetGlobalProperty("result")->ToObjectChecked()->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());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(3.0, Abs(-3));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static double Sum(int n) {
|
|
|
|
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));
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
2010-10-25 15:22:03 +00:00
|
|
|
return GetGlobalProperty("result")->ToObjectChecked()->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());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(5050.0, Sum(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Print) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM(PRINT_EXTENSION);
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
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;
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
2010-10-25 15:22:03 +00:00
|
|
|
CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->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());
|
|
|
|
bool has_pending_exception;
|
2013-02-25 14:46:09 +00:00
|
|
|
Isolate* isolate = fun->GetIsolate();
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2012-01-24 16:36:55 +00:00
|
|
|
Execution::Call(fun, global, 0, NULL, &has_pending_exception);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(has_pending_exception);
|
2013-02-25 14:46:09 +00:00
|
|
|
CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->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) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION);
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
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'.
|
|
|
|
bool has_pending_exception;
|
2012-08-17 12:59:00 +00:00
|
|
|
Handle<JSObject> global(Isolate::Current()->context()->global_object());
|
2008-08-22 13:33:59 +00:00
|
|
|
Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Object* foo_string =
|
|
|
|
FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("foo"))->
|
2012-12-17 15:56:16 +00:00
|
|
|
ToObjectChecked();
|
2013-02-25 14:46:09 +00:00
|
|
|
MaybeObject* fun1_object = isolate->context()->global_object()->
|
2013-02-28 17:03:34 +00:00
|
|
|
GetProperty(String::cast(foo_string));
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(fun1->IsJSFunction());
|
|
|
|
|
2012-12-17 15:56:16 +00:00
|
|
|
Handle<Object> argv[] =
|
2013-02-28 17:03:34 +00:00
|
|
|
{ FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("hello")) };
|
2011-10-06 09:31:38 +00:00
|
|
|
Execution::Call(Handle<JSFunction>::cast(fun1),
|
|
|
|
global,
|
|
|
|
ARRAY_SIZE(argv),
|
|
|
|
argv,
|
2008-08-22 13:33:59 +00:00
|
|
|
&has_pending_exception);
|
|
|
|
CHECK(!has_pending_exception);
|
|
|
|
}
|
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();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2009-02-24 13:33:49 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string());
|
|
|
|
script->set_source(HEAP->undefined_value());
|
2009-03-10 15:08:45 +00:00
|
|
|
CHECK_EQ(-1, GetScriptLineNumber(script, 0));
|
|
|
|
CHECK_EQ(-1, GetScriptLineNumber(script, 100));
|
|
|
|
CHECK_EQ(-1, GetScriptLineNumber(script, -1));
|
2009-02-24 13:33:49 +00:00
|
|
|
}
|
2010-02-16 12:08:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(GetScriptLineNumber) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2010-02-16 12:08:10 +00:00
|
|
|
v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
|
|
|
|
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';
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
|
2010-02-16 12:08:10 +00:00
|
|
|
v8::Handle<v8::String> script_body = v8::String::New(buffer.start());
|
|
|
|
v8::Script::Compile(script_body, &origin)->Run();
|
|
|
|
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::env()->Global()->Get(v8::String::New("f")));
|
2010-02-16 12:08:10 +00:00
|
|
|
CHECK_EQ(i, f->GetScriptLineNumber());
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 14:16:24 +00:00
|
|
|
|
|
|
|
|
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;
|
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;
|
|
|
|
env->Global()->Set(v8::String::New("x"), v8::Integer::New(i));
|
|
|
|
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"))));
|
2012-06-19 12:54:24 +00:00
|
|
|
CHECK(fun1->IsOptimized() || !fun1->IsOptimizable());
|
|
|
|
CHECK(fun2->IsOptimized() || !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);
|
|
|
|
OS::SNPrintF(smi_hex_buffer, "0x%lx", 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-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2011-05-11 14:16:24 +00:00
|
|
|
|
|
|
|
CompileRun("function f() { a = 12345678 }; f();");
|
2013-04-10 08:29:39 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
CompileRun("function f(x) { a = 12345678 + x}; f(1);");
|
2013-04-10 08:29:39 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
|
2013-04-10 08:29:39 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
|
2013-04-10 08:29:39 +00:00
|
|
|
CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
|
2011-05-11 14:16:24 +00:00
|
|
|
}
|
|
|
|
#endif
|