blob: 0837ab3e360307febecf069582a8914da89e6b04 [file] [log] [blame]
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ulan@chromium.org57ff8812013-05-10 08:16:55 +000028// TODO(dcarney): remove
29#define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
30#define V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000031
32#include "v8.h"
33
34#include "cctest.h"
35#include "compiler.h"
36#include "execution.h"
37#include "isolate.h"
38
39
40using namespace v8::internal;
41
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000042
43void SetSeeds(Handle<ByteArray> seeds, uint32_t state0, uint32_t state1) {
44 for (int i = 0; i < 4; i++) {
45 seeds->set(i, static_cast<byte>(state0 >> (i * kBitsPerByte)));
46 seeds->set(i + 4, static_cast<byte>(state1 >> (i * kBitsPerByte)));
47 }
48}
49
50
51void TestSeeds(Handle<JSFunction> fun,
52 Handle<Context> context,
53 uint32_t state0,
54 uint32_t state1) {
55 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +000056 Handle<JSObject> global(context->global_object());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000057 Handle<ByteArray> seeds(context->random_seed());
58
59 SetSeeds(seeds, state0, state1);
60 Handle<Object> value =
61 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
62 CHECK(value->IsHeapNumber());
63 CHECK(fun->IsOptimized());
64 double crankshaft_value = HeapNumber::cast(*value)->value();
65
66 SetSeeds(seeds, state0, state1);
67 V8::FillHeapNumberWithRandom(*value, *context);
68 double runtime_value = HeapNumber::cast(*value)->value();
69 CHECK_EQ(runtime_value, crankshaft_value);
70}
71
72
73TEST(CrankshaftRandom) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +000074 v8::V8::Initialize();
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000075 // Skip test if crankshaft is disabled.
76 if (!V8::UseCrankshaft()) return;
ulan@chromium.org57ff8812013-05-10 08:16:55 +000077 v8::Isolate* isolate = v8::Isolate::GetCurrent();
78 v8::HandleScope scope(isolate);
79 v8::Context::Scope context_scope(v8::Context::New(isolate));
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000080
81 Handle<Context> context(Isolate::Current()->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +000082 Handle<JSObject> global(context->global_object());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000083 Handle<ByteArray> seeds(context->random_seed());
84 bool has_pending_exception;
85
86 CompileRun("function f() { return Math.random(); }");
87
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000088 Object* string = FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("f"))->
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000089 ToObjectChecked();
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000090 MaybeObject* fun_object =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000091 context->global_object()->GetProperty(String::cast(string));
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000092 Handle<JSFunction> fun(JSFunction::cast(fun_object->ToObjectChecked()));
93
94 // Optimize function.
95 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
96 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
97 if (!fun->IsOptimized()) fun->MarkForLazyRecompilation();
98
99 // Test with some random values.
100 TestSeeds(fun, context, 0xC0C0AFFE, 0x31415926);
101 TestSeeds(fun, context, 0x01020304, 0xFFFFFFFF);
102 TestSeeds(fun, context, 0x00000001, 0x00000000);
103
104 // Test that we bail out to runtime when seeds are uninitialized (zeros).
105 SetSeeds(seeds, 0, 0);
106 Handle<Object> value =
107 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
108 CHECK(value->IsHeapNumber());
109 CHECK(fun->IsOptimized());
110 double crankshaft_value = HeapNumber::cast(*value)->value();
111 CHECK_NE(0.0, crankshaft_value);
112}