erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1 | // 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.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 28 | // TODO(dcarney): remove |
| 29 | #define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT |
| 30 | #define V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 31 | |
| 32 | #include "v8.h" |
| 33 | |
| 34 | #include "cctest.h" |
| 35 | #include "compiler.h" |
| 36 | #include "execution.h" |
| 37 | #include "isolate.h" |
| 38 | |
| 39 | |
| 40 | using namespace v8::internal; |
| 41 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 42 | |
| 43 | void 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 | |
| 51 | void TestSeeds(Handle<JSFunction> fun, |
| 52 | Handle<Context> context, |
| 53 | uint32_t state0, |
| 54 | uint32_t state1) { |
| 55 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 56 | Handle<JSObject> global(context->global_object()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 57 | 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 | |
| 73 | TEST(CrankshaftRandom) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 74 | v8::V8::Initialize(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 75 | // Skip test if crankshaft is disabled. |
| 76 | if (!V8::UseCrankshaft()) return; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 77 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 78 | v8::HandleScope scope(isolate); |
| 79 | v8::Context::Scope context_scope(v8::Context::New(isolate)); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 80 | |
| 81 | Handle<Context> context(Isolate::Current()->context()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 82 | Handle<JSObject> global(context->global_object()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 83 | Handle<ByteArray> seeds(context->random_seed()); |
| 84 | bool has_pending_exception; |
| 85 | |
| 86 | CompileRun("function f() { return Math.random(); }"); |
| 87 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 88 | Object* string = FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("f"))-> |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 89 | ToObjectChecked(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 90 | MaybeObject* fun_object = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 91 | context->global_object()->GetProperty(String::cast(string)); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 92 | 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 | } |