OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/execution.h" | 7 #include "src/execution.h" |
8 #include "src/handles.h" | 8 #include "src/handles.h" |
9 #include "src/interpreter/bytecode-array-builder.h" | 9 #include "src/interpreter/bytecode-array-builder.h" |
10 #include "src/interpreter/interpreter.h" | 10 #include "src/interpreter/interpreter.h" |
11 #include "test/cctest/cctest.h" | 11 #include "test/cctest/cctest.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
| 15 namespace interpreter { |
15 | 16 |
16 class InterpreterCallable { | 17 class InterpreterCallable { |
17 public: | 18 public: |
18 InterpreterCallable(Isolate* isolate, Handle<JSFunction> function) | 19 InterpreterCallable(Isolate* isolate, Handle<JSFunction> function) |
19 : isolate_(isolate), function_(function) {} | 20 : isolate_(isolate), function_(function) {} |
20 virtual ~InterpreterCallable() {} | 21 virtual ~InterpreterCallable() {} |
21 | 22 |
22 MaybeHandle<Object> operator()() { | 23 MaybeHandle<Object> operator()() { |
23 return Execution::Call(isolate_, function_, | 24 return Execution::Call(isolate_, function_, |
24 isolate_->factory()->undefined_value(), 0, nullptr, | 25 isolate_->factory()->undefined_value(), 0, nullptr, |
(...skipping 28 matching lines...) Expand all Loading... |
53 Handle<JSFunction> function = v8::Utils::OpenHandle( | 54 Handle<JSFunction> function = v8::Utils::OpenHandle( |
54 *v8::Handle<v8::Function>::Cast(CompileRun("(function(){})"))); | 55 *v8::Handle<v8::Function>::Cast(CompileRun("(function(){})"))); |
55 function->ReplaceCode(*isolate->builtins()->InterpreterEntryTrampoline()); | 56 function->ReplaceCode(*isolate->builtins()->InterpreterEntryTrampoline()); |
56 function->shared()->set_function_data(*bytecode_array); | 57 function->shared()->set_function_data(*bytecode_array); |
57 return function; | 58 return function; |
58 } | 59 } |
59 | 60 |
60 DISALLOW_COPY_AND_ASSIGN(InterpreterTester); | 61 DISALLOW_COPY_AND_ASSIGN(InterpreterTester); |
61 }; | 62 }; |
62 | 63 |
| 64 } // namespace interpreter |
63 } // namespace internal | 65 } // namespace internal |
64 } // namespace v8 | 66 } // namespace v8 |
65 | 67 |
66 using namespace v8::internal; | 68 using v8::internal::BytecodeArray; |
| 69 using v8::internal::Handle; |
| 70 using v8::internal::Object; |
| 71 using v8::internal::Smi; |
67 using namespace v8::internal::interpreter; | 72 using namespace v8::internal::interpreter; |
68 | 73 |
69 TEST(TestInterpreterReturn) { | 74 TEST(TestInterpreterReturn) { |
70 InitializedHandleScope handles; | 75 InitializedHandleScope handles; |
71 Handle<Object> undefined_value = | 76 Handle<Object> undefined_value = |
72 handles.main_isolate()->factory()->undefined_value(); | 77 handles.main_isolate()->factory()->undefined_value(); |
73 | 78 |
74 BytecodeArrayBuilder builder(handles.main_isolate()); | 79 BytecodeArrayBuilder builder(handles.main_isolate()); |
75 builder.set_locals_count(0); | 80 builder.set_locals_count(0); |
76 builder.Return(); | 81 builder.Return(); |
77 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); | 82 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
78 | 83 |
79 InterpreterTester tester(handles.main_isolate(), bytecode_array); | 84 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
80 InterpreterCallable callable(tester.GetCallable()); | 85 InterpreterCallable callable(tester.GetCallable()); |
81 Handle<Object> return_val = callable().ToHandleChecked(); | 86 Handle<Object> return_val = callable().ToHandleChecked(); |
82 CHECK(return_val.is_identical_to(undefined_value)); | 87 CHECK(return_val.is_identical_to(undefined_value)); |
83 } | 88 } |
| 89 |
| 90 |
| 91 TEST(TestInterpreterLoadUndefined) { |
| 92 InitializedHandleScope handles; |
| 93 Handle<Object> undefined_value = |
| 94 handles.main_isolate()->factory()->undefined_value(); |
| 95 |
| 96 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 97 builder.set_locals_count(0); |
| 98 builder.LoadUndefined().Return(); |
| 99 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 100 |
| 101 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 102 InterpreterCallable callable(tester.GetCallable()); |
| 103 Handle<Object> return_val = callable().ToHandleChecked(); |
| 104 CHECK(return_val.is_identical_to(undefined_value)); |
| 105 } |
| 106 |
| 107 |
| 108 TEST(TestInterpreterLoadNull) { |
| 109 InitializedHandleScope handles; |
| 110 Handle<Object> null_value = handles.main_isolate()->factory()->null_value(); |
| 111 |
| 112 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 113 builder.set_locals_count(0); |
| 114 builder.LoadNull().Return(); |
| 115 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 116 |
| 117 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 118 InterpreterCallable callable(tester.GetCallable()); |
| 119 Handle<Object> return_val = callable().ToHandleChecked(); |
| 120 CHECK(return_val.is_identical_to(null_value)); |
| 121 } |
| 122 |
| 123 |
| 124 TEST(TestInterpreterLoadTheHole) { |
| 125 InitializedHandleScope handles; |
| 126 Handle<Object> the_hole_value = |
| 127 handles.main_isolate()->factory()->the_hole_value(); |
| 128 |
| 129 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 130 builder.set_locals_count(0); |
| 131 builder.LoadTheHole().Return(); |
| 132 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 133 |
| 134 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 135 InterpreterCallable callable(tester.GetCallable()); |
| 136 Handle<Object> return_val = callable().ToHandleChecked(); |
| 137 CHECK(return_val.is_identical_to(the_hole_value)); |
| 138 } |
| 139 |
| 140 |
| 141 TEST(TestInterpreterLoadTrue) { |
| 142 InitializedHandleScope handles; |
| 143 Handle<Object> true_value = handles.main_isolate()->factory()->true_value(); |
| 144 |
| 145 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 146 builder.set_locals_count(0); |
| 147 builder.LoadTrue().Return(); |
| 148 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 149 |
| 150 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 151 InterpreterCallable callable(tester.GetCallable()); |
| 152 Handle<Object> return_val = callable().ToHandleChecked(); |
| 153 CHECK(return_val.is_identical_to(true_value)); |
| 154 } |
| 155 |
| 156 |
| 157 TEST(TestInterpreterLoadFalse) { |
| 158 InitializedHandleScope handles; |
| 159 Handle<Object> false_value = handles.main_isolate()->factory()->false_value(); |
| 160 |
| 161 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 162 builder.set_locals_count(0); |
| 163 builder.LoadFalse().Return(); |
| 164 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 165 |
| 166 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 167 InterpreterCallable callable(tester.GetCallable()); |
| 168 Handle<Object> return_val = callable().ToHandleChecked(); |
| 169 CHECK(return_val.is_identical_to(false_value)); |
| 170 } |
| 171 |
| 172 |
| 173 TEST(TestInterpreterLoadLiteral) { |
| 174 InitializedHandleScope handles; |
| 175 for (int i = -128; i < 128; i++) { |
| 176 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 177 builder.set_locals_count(0); |
| 178 builder.LoadLiteral(Smi::FromInt(i)).Return(); |
| 179 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 180 |
| 181 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 182 InterpreterCallable callable(tester.GetCallable()); |
| 183 Handle<Object> return_val = callable().ToHandleChecked(); |
| 184 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(i)); |
| 185 } |
| 186 } |
| 187 |
| 188 |
| 189 TEST(TestInterpreterLoadStoreRegisters) { |
| 190 InitializedHandleScope handles; |
| 191 Handle<Object> true_value = handles.main_isolate()->factory()->true_value(); |
| 192 for (int i = 0; i <= Register::kMaxRegisterIndex; i++) { |
| 193 BytecodeArrayBuilder builder(handles.main_isolate()); |
| 194 builder.set_locals_count(i + 1); |
| 195 Register reg(i); |
| 196 builder.LoadTrue() |
| 197 .StoreAccumulatorInRegister(reg) |
| 198 .LoadFalse() |
| 199 .LoadAccumulatorWithRegister(reg) |
| 200 .Return(); |
| 201 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 202 |
| 203 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 204 InterpreterCallable callable(tester.GetCallable()); |
| 205 Handle<Object> return_val = callable().ToHandleChecked(); |
| 206 CHECK(return_val.is_identical_to(true_value)); |
| 207 } |
| 208 } |
OLD | NEW |