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 |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "vm/native_entry_test.h" |
6 | |
7 | #include "vm/code_patcher.h" |
8 | #include "vm/compiler/assembler/assembler.h" |
9 | #include "vm/dart_api_impl.h" |
10 | #include "vm/native_entry.h" |
11 | #include "vm/object.h" |
12 | #include "vm/stack_frame.h" |
13 | #include "vm/stub_code.h" |
14 | #include "vm/unit_test.h" |
15 | |
16 | namespace dart { |
17 | |
18 | // A native call for test purposes. |
19 | // Arg0: a smi. |
20 | // Arg1: a smi. |
21 | // Result: a smi representing arg0 - arg1. |
22 | void TestSmiSub(Dart_NativeArguments args) { |
23 | Dart_Handle left = Dart_GetNativeArgument(args, 0); |
24 | Dart_Handle right = Dart_GetNativeArgument(args, 1); |
25 | int64_t left_value = -1; |
26 | int64_t right_value = -1; |
27 | EXPECT_VALID(Dart_IntegerToInt64(left, &left_value)); |
28 | EXPECT_VALID(Dart_IntegerToInt64(right, &right_value)); |
29 | |
30 | // Ignoring overflow in the calculation below. |
31 | int64_t result = left_value - right_value; |
32 | Dart_SetReturnValue(args, Dart_NewInteger(result)); |
33 | } |
34 | |
35 | // A native call for test purposes. |
36 | // Arg0-4: 5 smis. |
37 | // Result: a smi representing the sum of all arguments. |
38 | void TestSmiSum(Dart_NativeArguments args) { |
39 | int64_t result = 0; |
40 | int arg_count = Dart_GetNativeArgumentCount(args); |
41 | for (int i = 0; i < arg_count; i++) { |
42 | Dart_Handle arg = Dart_GetNativeArgument(args, i); |
43 | int64_t arg_value = -1; |
44 | EXPECT_VALID(Dart_IntegerToInt64(arg, &arg_value)); |
45 | |
46 | // Ignoring overflow in the addition below. |
47 | result += arg_value; |
48 | } |
49 | Dart_SetReturnValue(args, Dart_NewInteger(result)); |
50 | } |
51 | |
52 | // Test for accepting null arguments in native function. |
53 | // Arg0-4: 5 smis or null. |
54 | // Result: a smi representing the sum of all non-null arguments. |
55 | void TestNonNullSmiSum(Dart_NativeArguments args) { |
56 | int64_t result = 0; |
57 | int arg_count = Dart_GetNativeArgumentCount(args); |
58 | // Test the lower level macro GET_NATIVE_ARGUMENT. |
59 | NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
60 | Zone* zone = Thread::Current()->zone(); // Used by GET_NATIVE_ARGUMENT. |
61 | for (int i = 0; i < arg_count; i++) { |
62 | Dart_Handle arg = Dart_GetNativeArgument(args, i); |
63 | GET_NATIVE_ARGUMENT(Integer, argument, arguments->NativeArgAt(i)); |
64 | EXPECT(argument.IsInteger()); // May be null. |
65 | EXPECT_EQ(Api::UnwrapHandle(arg), argument.raw()); // May be null. |
66 | int64_t arg_value = -1; |
67 | if (argument.IsNull()) { |
68 | EXPECT_ERROR(Dart_IntegerToInt64(arg, &arg_value), |
69 | "Dart_IntegerToInt64 expects argument 'integer' " |
70 | "to be non-null." ); |
71 | } else { |
72 | EXPECT_VALID(Dart_IntegerToInt64(arg, &arg_value)); |
73 | EXPECT_EQ(arg_value, argument.AsInt64Value()); |
74 | // Ignoring overflow in the addition below. |
75 | result += arg_value; |
76 | } |
77 | } |
78 | Dart_SetReturnValue(args, Dart_NewInteger(result)); |
79 | } |
80 | |
81 | } // namespace dart |
82 | |