OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 seeds->set(i + 4, static_cast<byte>(state1 >> (i * kBitsPerByte))); | 45 seeds->set(i + 4, static_cast<byte>(state1 >> (i * kBitsPerByte))); |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 | 49 |
50 void TestSeeds(Handle<JSFunction> fun, | 50 void TestSeeds(Handle<JSFunction> fun, |
51 Handle<Context> context, | 51 Handle<Context> context, |
52 uint32_t state0, | 52 uint32_t state0, |
53 uint32_t state1) { | 53 uint32_t state1) { |
54 bool has_pending_exception; | 54 bool has_pending_exception; |
55 Handle<JSObject> global(context->global()); | 55 Handle<JSObject> global(context->global_object()); |
56 Handle<ByteArray> seeds(context->random_seed()); | 56 Handle<ByteArray> seeds(context->random_seed()); |
57 | 57 |
58 SetSeeds(seeds, state0, state1); | 58 SetSeeds(seeds, state0, state1); |
59 Handle<Object> value = | 59 Handle<Object> value = |
60 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 60 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
61 CHECK(value->IsHeapNumber()); | 61 CHECK(value->IsHeapNumber()); |
62 CHECK(fun->IsOptimized()); | 62 CHECK(fun->IsOptimized()); |
63 double crankshaft_value = HeapNumber::cast(*value)->value(); | 63 double crankshaft_value = HeapNumber::cast(*value)->value(); |
64 | 64 |
65 SetSeeds(seeds, state0, state1); | 65 SetSeeds(seeds, state0, state1); |
66 V8::FillHeapNumberWithRandom(*value, *context); | 66 V8::FillHeapNumberWithRandom(*value, *context); |
67 double runtime_value = HeapNumber::cast(*value)->value(); | 67 double runtime_value = HeapNumber::cast(*value)->value(); |
68 CHECK_EQ(runtime_value, crankshaft_value); | 68 CHECK_EQ(runtime_value, crankshaft_value); |
69 } | 69 } |
70 | 70 |
71 | 71 |
72 TEST(CrankshaftRandom) { | 72 TEST(CrankshaftRandom) { |
73 if (env.IsEmpty()) env = v8::Context::New(); | 73 if (env.IsEmpty()) env = v8::Context::New(); |
74 // Skip test if crankshaft is disabled. | 74 // Skip test if crankshaft is disabled. |
75 if (!V8::UseCrankshaft()) return; | 75 if (!V8::UseCrankshaft()) return; |
76 v8::HandleScope scope; | 76 v8::HandleScope scope; |
77 env->Enter(); | 77 env->Enter(); |
78 | 78 |
79 Handle<Context> context(Isolate::Current()->context()); | 79 Handle<Context> context(Isolate::Current()->context()); |
80 Handle<JSObject> global(context->global()); | 80 Handle<JSObject> global(context->global_object()); |
81 Handle<ByteArray> seeds(context->random_seed()); | 81 Handle<ByteArray> seeds(context->random_seed()); |
82 bool has_pending_exception; | 82 bool has_pending_exception; |
83 | 83 |
84 CompileRun("function f() { return Math.random(); }"); | 84 CompileRun("function f() { return Math.random(); }"); |
85 | 85 |
86 Object* symbol = FACTORY->LookupAsciiSymbol("f")->ToObjectChecked(); | 86 Object* symbol = FACTORY->LookupAsciiSymbol("f")->ToObjectChecked(); |
87 MaybeObject* fun_object = | 87 MaybeObject* fun_object = |
88 context->global()->GetProperty(String::cast(symbol)); | 88 context->global_object()->GetProperty(String::cast(symbol)); |
89 Handle<JSFunction> fun(JSFunction::cast(fun_object->ToObjectChecked())); | 89 Handle<JSFunction> fun(JSFunction::cast(fun_object->ToObjectChecked())); |
90 | 90 |
91 // Optimize function. | 91 // Optimize function. |
92 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 92 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
93 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(); | 94 if (!fun->IsOptimized()) fun->MarkForLazyRecompilation(); |
95 | 95 |
96 // Test with some random values. | 96 // Test with some random values. |
97 TestSeeds(fun, context, 0xC0C0AFFE, 0x31415926); | 97 TestSeeds(fun, context, 0xC0C0AFFE, 0x31415926); |
98 TestSeeds(fun, context, 0x01020304, 0xFFFFFFFF); | 98 TestSeeds(fun, context, 0x01020304, 0xFFFFFFFF); |
99 TestSeeds(fun, context, 0x00000001, 0x00000000); | 99 TestSeeds(fun, context, 0x00000001, 0x00000000); |
100 | 100 |
101 // Test that we bail out to runtime when seeds are uninitialized (zeros). | 101 // Test that we bail out to runtime when seeds are uninitialized (zeros). |
102 SetSeeds(seeds, 0, 0); | 102 SetSeeds(seeds, 0, 0); |
103 Handle<Object> value = | 103 Handle<Object> value = |
104 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 104 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
105 CHECK(value->IsHeapNumber()); | 105 CHECK(value->IsHeapNumber()); |
106 CHECK(fun->IsOptimized()); | 106 CHECK(fun->IsOptimized()); |
107 double crankshaft_value = HeapNumber::cast(*value)->value(); | 107 double crankshaft_value = HeapNumber::cast(*value)->value(); |
108 CHECK_NE(0.0, crankshaft_value); | 108 CHECK_NE(0.0, crankshaft_value); |
109 } | 109 } |
OLD | NEW |