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