v8  3.11.10(node0.8.26)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-random.cc
Go to the documentation of this file.
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 
28 
29 #include "v8.h"
30 
31 #include "cctest.h"
32 #include "compiler.h"
33 #include "execution.h"
34 #include "isolate.h"
35 
36 
37 using namespace v8::internal;
38 
40 
41 
42 void SetSeeds(Handle<ByteArray> seeds, uint32_t state0, uint32_t state1) {
43  for (int i = 0; i < 4; i++) {
44  seeds->set(i, static_cast<byte>(state0 >> (i * kBitsPerByte)));
45  seeds->set(i + 4, static_cast<byte>(state1 >> (i * kBitsPerByte)));
46  }
47 }
48 
49 
51  Handle<Context> context,
52  uint32_t state0,
53  uint32_t state1) {
54  bool has_pending_exception;
55  Handle<JSObject> global(context->global());
56  Handle<ByteArray> seeds(context->random_seed());
57 
58  SetSeeds(seeds, state0, state1);
59  Handle<Object> value =
60  Execution::Call(fun, global, 0, NULL, &has_pending_exception);
61  CHECK(value->IsHeapNumber());
62  CHECK(fun->IsOptimized());
63  double crankshaft_value = HeapNumber::cast(*value)->value();
64 
65  SetSeeds(seeds, state0, state1);
66  V8::FillHeapNumberWithRandom(*value, *context);
67  double runtime_value = HeapNumber::cast(*value)->value();
68  CHECK_EQ(runtime_value, crankshaft_value);
69 }
70 
71 
72 TEST(CrankshaftRandom) {
73  if (env.IsEmpty()) env = v8::Context::New();
74  // Skip test if crankshaft is disabled.
75  if (!V8::UseCrankshaft()) return;
76  v8::HandleScope scope;
77  env->Enter();
78 
79  Handle<Context> context(Isolate::Current()->context());
80  Handle<JSObject> global(context->global());
81  Handle<ByteArray> seeds(context->random_seed());
82  bool has_pending_exception;
83 
84  CompileRun("function f() { return Math.random(); }");
85 
86  Object* symbol = FACTORY->LookupAsciiSymbol("f")->ToObjectChecked();
87  MaybeObject* fun_object =
88  context->global()->GetProperty(String::cast(symbol));
89  Handle<JSFunction> fun(JSFunction::cast(fun_object->ToObjectChecked()));
90 
91  // Optimize function.
92  Execution::Call(fun, global, 0, NULL, &has_pending_exception);
93  Execution::Call(fun, global, 0, NULL, &has_pending_exception);
94  if (!fun->IsOptimized()) fun->MarkForLazyRecompilation();
95 
96  // Test with some random values.
97  TestSeeds(fun, context, 0xC0C0AFFE, 0x31415926);
98  TestSeeds(fun, context, 0x01020304, 0xFFFFFFFF);
99  TestSeeds(fun, context, 0x00000001, 0x00000000);
100 
101  // Test that we bail out to runtime when seeds are uninitialized (zeros).
102  SetSeeds(seeds, 0, 0);
103  Handle<Object> value =
104  Execution::Call(fun, global, 0, NULL, &has_pending_exception);
105  CHECK(value->IsHeapNumber());
106  CHECK(fun->IsOptimized());
107  double crankshaft_value = HeapNumber::cast(*value)->value();
108  CHECK_NE(0.0, crankshaft_value);
109 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
static String * cast(Object *obj)
static bool UseCrankshaft()
Definition: v8.h:86
static Object * FillHeapNumberWithRandom(Object *heap_number, Context *context)
Definition: v8.cc:236
void SetSeeds(Handle< ByteArray > seeds, uint32_t state0, uint32_t state1)
Definition: test-random.cc:42
#define CHECK(condition)
Definition: checks.h:56
const int kBitsPerByte
Definition: globals.h:251
#define CHECK_NE(unexpected, value)
Definition: checks.h:223
static Handle< Object > Call(Handle< Object > callable, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *pending_exception, bool convert_receiver=false)
Definition: execution.cc:144
static HeapNumber * cast(Object *obj)
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination trace on stack replacement optimize closures functions with arguments object optimize functions containing for in loops profiler considers IC stability primitive functions trigger their own optimization re try self optimization if it failed insert an interrupt check at function exit execution budget before interrupt is triggered call count before self optimization self_optimization count_based_interrupts weighted_back_edges trace_opt emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 enable use of ARMv7 instructions if enable use of MIPS FPU instructions if NULL
Definition: flags.cc:274
void TestSeeds(Handle< JSFunction > fun, Handle< Context > context, uint32_t state0, uint32_t state1)
Definition: test-random.cc:50
bool IsEmpty() const
Definition: v8.h:208
#define FACTORY
Definition: isolate.h:1409
static Persistent< Context > New(ExtensionConfiguration *extensions=NULL, Handle< ObjectTemplate > global_template=Handle< ObjectTemplate >(), Handle< Value > global_object=Handle< Value >())
Definition: api.cc:4308
static JSFunction * cast(Object *obj)