Index: test/cctest/compiler/test-code-stub-assembler.cc |
diff --git a/test/cctest/compiler/test-code-stub-assembler.cc b/test/cctest/compiler/test-code-stub-assembler.cc |
index 0306561020abe41e5342f9cc209062f67bd9d208..2bf785b422627c35a4b946aaec387cbe4575111d 100644 |
--- a/test/cctest/compiler/test-code-stub-assembler.cc |
+++ b/test/cctest/compiler/test-code-stub-assembler.cc |
@@ -13,12 +13,19 @@ namespace compiler { |
class CodeStubAssemblerTester : public CodeStubAssembler { |
public: |
+ // Test generating code for a stub. |
CodeStubAssemblerTester(Isolate* isolate, |
const CallInterfaceDescriptor& descriptor) |
: CodeStubAssembler(isolate, isolate->runtime_zone(), descriptor, |
Code::ComputeFlags(Code::STUB), "test"), |
scope_(isolate) {} |
+ // Test generating code for a JS function (e.g. builtins). |
+ CodeStubAssemblerTester(Isolate* isolate, int parameter_count) |
+ : CodeStubAssembler(isolate, isolate->runtime_zone(), parameter_count, |
+ Code::ComputeFlags(Code::FUNCTION), "test"), |
+ scope_(isolate) {} |
+ |
private: |
HandleScope scope_; |
LocalContext context_; |
@@ -247,6 +254,83 @@ TEST(FixedArrayAccessSmiIndex) { |
CHECK_EQ(733, Handle<Smi>::cast(result.ToHandleChecked())->value()); |
} |
+TEST(LoadHeapNumberValue) { |
+ Isolate* isolate(CcTest::InitIsolateOnce()); |
+ VoidDescriptor descriptor(isolate); |
+ CodeStubAssemblerTester m(isolate, descriptor); |
+ Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(1234); |
+ m.Return(m.SmiTag( |
+ m.ChangeFloat64ToUint32(m.LoadHeapNumberValue(m.HeapConstant(number))))); |
+ Handle<Code> code = m.GenerateCode(); |
+ FunctionTester ft(descriptor, code); |
+ MaybeHandle<Object> result = ft.Call(); |
+ CHECK_EQ(1234, Handle<Smi>::cast(result.ToHandleChecked())->value()); |
+} |
+ |
+TEST(LoadInstanceType) { |
+ Isolate* isolate(CcTest::InitIsolateOnce()); |
+ VoidDescriptor descriptor(isolate); |
+ CodeStubAssemblerTester m(isolate, descriptor); |
+ Handle<HeapObject> undefined = isolate->factory()->undefined_value(); |
+ m.Return(m.SmiTag(m.LoadInstanceType(m.HeapConstant(undefined)))); |
+ Handle<Code> code = m.GenerateCode(); |
+ FunctionTester ft(descriptor, code); |
+ MaybeHandle<Object> result = ft.Call(); |
+ CHECK_EQ(InstanceType::ODDBALL_TYPE, |
+ Handle<Smi>::cast(result.ToHandleChecked())->value()); |
+} |
+ |
+namespace { |
+ |
+class TestBitField : public BitField<unsigned, 3, 3> {}; |
+ |
+} // namespace |
+ |
+TEST(BitFieldDecode) { |
+ Isolate* isolate(CcTest::InitIsolateOnce()); |
+ VoidDescriptor descriptor(isolate); |
+ CodeStubAssemblerTester m(isolate, descriptor); |
+ m.Return(m.SmiTag(m.BitFieldDecode<TestBitField>(m.Int32Constant(0x2f)))); |
+ Handle<Code> code = m.GenerateCode(); |
+ FunctionTester ft(descriptor, code); |
+ MaybeHandle<Object> result = ft.Call(); |
+ // value = 00101111 |
+ // mask = 00111000 |
+ // result = 101 |
+ CHECK_EQ(5, Handle<Smi>::cast(result.ToHandleChecked())->value()); |
+} |
+ |
+namespace { |
+ |
+Handle<JSFunction> CreateFunctionFromCode(int parameter_count_with_receiver, |
+ Handle<Code> code) { |
+ Isolate* isolate = code->GetIsolate(); |
+ Handle<String> name = isolate->factory()->InternalizeUtf8String("test"); |
+ Handle<JSFunction> function = |
+ isolate->factory()->NewFunctionWithoutPrototype(name, code); |
+ function->shared()->set_internal_formal_parameter_count( |
+ parameter_count_with_receiver - 1); // Implicit undefined receiver. |
+ return function; |
+} |
+ |
+} // namespace |
+ |
+TEST(JSFunction) { |
+ const int kNumParams = 3; // Receiver, left, right. |
+ Isolate* isolate(CcTest::InitIsolateOnce()); |
+ CodeStubAssemblerTester m(isolate, kNumParams); |
+ m.Return(m.SmiTag( |
+ m.Int32Add(m.SmiToInt32(m.Parameter(1)), m.SmiToInt32(m.Parameter(2))))); |
+ Handle<Code> code = m.GenerateCode(); |
+ Handle<JSFunction> function = CreateFunctionFromCode(kNumParams, code); |
+ Handle<Object> args[] = {Handle<Smi>(Smi::FromInt(23), isolate), |
+ Handle<Smi>(Smi::FromInt(34), isolate)}; |
+ MaybeHandle<Object> result = |
+ Execution::Call(isolate, function, isolate->factory()->undefined_value(), |
+ arraysize(args), args); |
+ CHECK_EQ(57, Handle<Smi>::cast(result.ToHandleChecked())->value()); |
+} |
+ |
} // namespace compiler |
} // namespace internal |
} // namespace v8 |