OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/native_entry_test.h" | 5 #include "vm/native_entry_test.h" |
6 | 6 |
7 #include "vm/assembler.h" | 7 #include "vm/assembler.h" |
8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
9 #include "vm/native_entry.h" | 9 #include "vm/native_entry.h" |
10 #include "vm/object.h" | 10 #include "vm/object.h" |
11 #include "vm/stack_frame.h" | 11 #include "vm/stack_frame.h" |
12 #include "vm/stub_code.h" | 12 #include "vm/stub_code.h" |
| 13 #include "vm/unit_test.h" |
13 | 14 |
14 namespace dart { | 15 namespace dart { |
15 | 16 |
16 struct NativeTestEntries { | |
17 const char* name_; | |
18 Dart_NativeFunction function_; | |
19 int argument_count_; | |
20 } TestEntries[] = { | |
21 REGISTER_NATIVE_ENTRY(TestSmiSub, 2) | |
22 REGISTER_NATIVE_ENTRY(TestSmiSum, 6) | |
23 REGISTER_NATIVE_ENTRY(TestStaticCallPatching, 0) | |
24 }; | |
25 | |
26 | |
27 Dart_NativeFunction NativeTestEntry_Lookup(const String& name, | |
28 int argument_count) { | |
29 int num_entries = sizeof(TestEntries) / sizeof(struct NativeTestEntries); | |
30 for (int i = 0; i < num_entries; i++) { | |
31 struct NativeTestEntries* entry = &(TestEntries[i]); | |
32 if (name.Equals(entry->name_)) { | |
33 if (entry->argument_count_ == argument_count) { | |
34 return entry->function_; | |
35 } else { | |
36 // Wrong number of arguments. | |
37 return NULL; | |
38 } | |
39 } | |
40 } | |
41 return NULL; | |
42 } | |
43 | |
44 | 17 |
45 // A native call for test purposes. | 18 // A native call for test purposes. |
46 // Arg0: a smi. | 19 // Arg0: a smi. |
47 // Arg1: a smi. | 20 // Arg1: a smi. |
48 // Result: a smi representing arg0 - arg1. | 21 // Result: a smi representing arg0 - arg1. |
49 DEFINE_NATIVE_ENTRY(TestSmiSub, 2) { | 22 void TestSmiSub(Dart_NativeArguments args) { |
50 const Smi& left = Smi::CheckedHandle(arguments->At(0)); | 23 Dart_EnterScope(); |
51 const Smi& right = Smi::CheckedHandle(arguments->At(1)); | 24 Dart_Handle left = Dart_GetNativeArgument(args, 0); |
| 25 Dart_Handle right = Dart_GetNativeArgument(args, 1); |
| 26 int64_t left_value = -1; |
| 27 int64_t right_value = -1; |
| 28 EXPECT_VALID(Dart_IntegerToInt64(left, &left_value)); |
| 29 EXPECT_VALID(Dart_IntegerToInt64(right, &right_value)); |
| 30 |
52 // Ignoring overflow in the calculation below. | 31 // Ignoring overflow in the calculation below. |
53 intptr_t result = left.Value() - right.Value(); | 32 int64_t result = left_value - right_value; |
54 arguments->SetReturn(Smi::Handle(Smi::New(result))); | 33 Dart_SetReturnValue(args, Dart_NewInteger(result)); |
| 34 Dart_ExitScope(); |
55 } | 35 } |
56 | 36 |
57 | 37 |
58 // A native call for test purposes. | 38 // A native call for test purposes. |
59 // Arg0-4: 5 smis. | 39 // Arg0-4: 5 smis. |
60 // Result: a smi representing the sum of all arguments. | 40 // Result: a smi representing the sum of all arguments. |
61 DEFINE_NATIVE_ENTRY(TestSmiSum, 5) { | 41 void TestSmiSum(Dart_NativeArguments args) { |
62 intptr_t result = 0; | 42 Dart_EnterScope(); |
63 for (int i = 0; i < arguments->Count(); i++) { | 43 int64_t result = 0; |
64 const Smi& arg = Smi::CheckedHandle(arguments->At(i)); | 44 int arg_count = Dart_GetNativeArgumentCount(args); |
| 45 for (int i = 0; i < arg_count; i++) { |
| 46 Dart_Handle arg = Dart_GetNativeArgument(args, i); |
| 47 int64_t arg_value = -1; |
| 48 EXPECT_VALID(Dart_IntegerToInt64(arg, &arg_value)); |
| 49 |
65 // Ignoring overflow in the addition below. | 50 // Ignoring overflow in the addition below. |
66 result += arg.Value(); | 51 result += arg_value; |
67 } | 52 } |
68 arguments->SetReturn(Smi::Handle(Smi::New(result))); | 53 Dart_SetReturnValue(args, Dart_NewInteger(result)); |
| 54 Dart_ExitScope(); |
69 } | 55 } |
70 | 56 |
71 | 57 |
72 // Test code patching. | 58 // Test code patching. |
73 DEFINE_NATIVE_ENTRY(TestStaticCallPatching, 0) { | 59 void TestStaticCallPatching(Dart_NativeArguments args) { |
| 60 Dart_EnterScope(); |
74 uword target_address = 0; | 61 uword target_address = 0; |
75 Function& target_function = Function::Handle(); | 62 Function& target_function = Function::Handle(); |
76 DartFrameIterator iterator; | 63 DartFrameIterator iterator; |
77 iterator.NextFrame(); // Skip native call. | 64 iterator.NextFrame(); // Skip native call. |
78 StackFrame* static_caller_frame = iterator.NextFrame(); | 65 StackFrame* static_caller_frame = iterator.NextFrame(); |
79 CodePatcher::GetStaticCallAt(static_caller_frame->pc(), | 66 CodePatcher::GetStaticCallAt(static_caller_frame->pc(), |
80 &target_function, | 67 &target_function, |
81 &target_address); | 68 &target_address); |
82 EXPECT(String::Handle(target_function.name()). | 69 EXPECT(String::Handle(target_function.name()). |
83 Equals(String::Handle(String::New("NativePatchStaticCall")))); | 70 Equals(String::Handle(String::New("NativePatchStaticCall")))); |
84 const uword function_entry_address = | 71 const uword function_entry_address = |
85 Code::Handle(target_function.CurrentCode()).EntryPoint(); | 72 Code::Handle(target_function.CurrentCode()).EntryPoint(); |
86 EXPECT_EQ(function_entry_address, target_address); | 73 EXPECT_EQ(function_entry_address, target_address); |
| 74 Dart_ExitScope(); |
87 } | 75 } |
88 | 76 |
89 } // namespace dart | 77 } // namespace dart |
OLD | NEW |