2011-10-05 05:20:07 +00:00
|
|
|
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "vm/native_entry_test.h"
|
|
|
|
|
|
|
|
#include "vm/code_patcher.h"
|
2017-09-04 14:18:37 +00:00
|
|
|
#include "vm/compiler/assembler/assembler.h"
|
2012-12-10 17:59:40 +00:00
|
|
|
#include "vm/dart_api_impl.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
#include "vm/native_entry.h"
|
|
|
|
#include "vm/object.h"
|
|
|
|
#include "vm/stack_frame.h"
|
|
|
|
#include "vm/stub_code.h"
|
2012-08-30 17:41:19 +00:00
|
|
|
#include "vm/unit_test.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
|
|
|
// A native call for test purposes.
|
|
|
|
// Arg0: a smi.
|
|
|
|
// Arg1: a smi.
|
|
|
|
// Result: a smi representing arg0 - arg1.
|
2012-08-30 17:41:19 +00:00
|
|
|
void TestSmiSub(Dart_NativeArguments args) {
|
|
|
|
Dart_Handle left = Dart_GetNativeArgument(args, 0);
|
|
|
|
Dart_Handle right = Dart_GetNativeArgument(args, 1);
|
|
|
|
int64_t left_value = -1;
|
|
|
|
int64_t right_value = -1;
|
|
|
|
EXPECT_VALID(Dart_IntegerToInt64(left, &left_value));
|
|
|
|
EXPECT_VALID(Dart_IntegerToInt64(right, &right_value));
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
// Ignoring overflow in the calculation below.
|
2012-08-30 17:41:19 +00:00
|
|
|
int64_t result = left_value - right_value;
|
|
|
|
Dart_SetReturnValue(args, Dart_NewInteger(result));
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A native call for test purposes.
|
|
|
|
// Arg0-4: 5 smis.
|
|
|
|
// Result: a smi representing the sum of all arguments.
|
2012-08-30 17:41:19 +00:00
|
|
|
void TestSmiSum(Dart_NativeArguments args) {
|
|
|
|
int64_t result = 0;
|
|
|
|
int arg_count = Dart_GetNativeArgumentCount(args);
|
|
|
|
for (int i = 0; i < arg_count; i++) {
|
|
|
|
Dart_Handle arg = Dart_GetNativeArgument(args, i);
|
|
|
|
int64_t arg_value = -1;
|
|
|
|
EXPECT_VALID(Dart_IntegerToInt64(arg, &arg_value));
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
// Ignoring overflow in the addition below.
|
2012-08-30 17:41:19 +00:00
|
|
|
result += arg_value;
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
2012-08-30 17:41:19 +00:00
|
|
|
Dart_SetReturnValue(args, Dart_NewInteger(result));
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 17:59:40 +00:00
|
|
|
// Test for accepting null arguments in native function.
|
|
|
|
// Arg0-4: 5 smis or null.
|
|
|
|
// Result: a smi representing the sum of all non-null arguments.
|
|
|
|
void TestNonNullSmiSum(Dart_NativeArguments args) {
|
|
|
|
int64_t result = 0;
|
|
|
|
int arg_count = Dart_GetNativeArgumentCount(args);
|
|
|
|
// Test the lower level macro GET_NATIVE_ARGUMENT.
|
|
|
|
NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
|
2015-03-19 17:42:40 +00:00
|
|
|
Zone* zone = Thread::Current()->zone(); // Used by GET_NATIVE_ARGUMENT.
|
2012-12-10 17:59:40 +00:00
|
|
|
for (int i = 0; i < arg_count; i++) {
|
|
|
|
Dart_Handle arg = Dart_GetNativeArgument(args, i);
|
|
|
|
GET_NATIVE_ARGUMENT(Integer, argument, arguments->NativeArgAt(i));
|
2016-11-08 21:54:47 +00:00
|
|
|
EXPECT(argument.IsInteger()); // May be null.
|
2012-12-10 17:59:40 +00:00
|
|
|
EXPECT_EQ(Api::UnwrapHandle(arg), argument.raw()); // May be null.
|
|
|
|
int64_t arg_value = -1;
|
|
|
|
if (argument.IsNull()) {
|
|
|
|
EXPECT_ERROR(Dart_IntegerToInt64(arg, &arg_value),
|
|
|
|
"Dart_IntegerToInt64 expects argument 'integer' "
|
|
|
|
"to be non-null.");
|
|
|
|
} else {
|
|
|
|
EXPECT_VALID(Dart_IntegerToInt64(arg, &arg_value));
|
|
|
|
EXPECT_EQ(arg_value, argument.AsInt64Value());
|
|
|
|
// Ignoring overflow in the addition below.
|
|
|
|
result += arg_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Dart_SetReturnValue(args, Dart_NewInteger(result));
|
|
|
|
}
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
} // namespace dart
|