Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 509153003: New bigint implementation in the vm. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "include/dart_mirrors_api.h" 6 #include "include/dart_mirrors_api.h"
7 #include "include/dart_native_api.h" 7 #include "include/dart_native_api.h"
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "vm/bigint_operations.h"
11 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
12 #include "vm/compiler.h" 11 #include "vm/compiler.h"
13 #include "vm/dart.h" 12 #include "vm/dart.h"
14 #include "vm/dart_api_impl.h" 13 #include "vm/dart_api_impl.h"
15 #include "vm/dart_api_message.h" 14 #include "vm/dart_api_message.h"
16 #include "vm/dart_api_state.h" 15 #include "vm/dart_api_state.h"
17 #include "vm/dart_entry.h" 16 #include "vm/dart_entry.h"
18 #include "vm/debuginfo.h" 17 #include "vm/debuginfo.h"
19 #include "vm/exceptions.h" 18 #include "vm/exceptions.h"
20 #include "vm/flags.h" 19 #include "vm/flags.h"
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 return true; 140 return true;
142 } 141 }
143 Isolate* isolate = arguments->isolate(); 142 Isolate* isolate = arguments->isolate();
144 ASSERT(isolate == Isolate::Current()); 143 ASSERT(isolate == Isolate::Current());
145 REUSABLE_OBJECT_HANDLESCOPE(isolate); 144 REUSABLE_OBJECT_HANDLESCOPE(isolate);
146 Object& obj = isolate->ObjectHandle(); 145 Object& obj = isolate->ObjectHandle();
147 obj = arguments->NativeArgAt(arg_index); 146 obj = arguments->NativeArgAt(arg_index);
148 intptr_t cid = obj.GetClassId(); 147 intptr_t cid = obj.GetClassId();
149 if (cid == kBigintCid) { 148 if (cid == kBigintCid) {
150 const Bigint& bigint = Bigint::Cast(obj); 149 const Bigint& bigint = Bigint::Cast(obj);
151 if (BigintOperations::FitsIntoInt64(bigint)) { 150 if (bigint.FitsIntoInt64()) {
152 *value = BigintOperations::ToInt64(bigint); 151 *value = bigint.AsInt64Value();
153 return true; 152 return true;
154 } 153 }
155 } 154 }
156 return false; 155 return false;
157 } 156 }
158 157
159 158
160 static bool GetNativeUnsignedIntegerArgument(NativeArguments* arguments, 159 static bool GetNativeUnsignedIntegerArgument(NativeArguments* arguments,
161 int arg_index, 160 int arg_index,
162 uint64_t* value) { 161 uint64_t* value) {
163 ASSERT(value != NULL); 162 ASSERT(value != NULL);
164 int64_t arg_value = 0; 163 int64_t arg_value = 0;
165 if (Api::GetNativeIntegerArgument(arguments, arg_index, &arg_value)) { 164 if (Api::GetNativeIntegerArgument(arguments, arg_index, &arg_value)) {
166 *value = static_cast<uint64_t>(arg_value); 165 *value = static_cast<uint64_t>(arg_value);
167 return true; 166 return true;
168 } 167 }
169 Isolate* isolate = arguments->isolate(); 168 Isolate* isolate = arguments->isolate();
170 ASSERT(isolate == Isolate::Current()); 169 ASSERT(isolate == Isolate::Current());
171 REUSABLE_OBJECT_HANDLESCOPE(isolate); 170 REUSABLE_OBJECT_HANDLESCOPE(isolate);
172 Object& obj = isolate->ObjectHandle(); 171 Object& obj = isolate->ObjectHandle();
173 obj = arguments->NativeArgAt(arg_index); 172 obj = arguments->NativeArgAt(arg_index);
174 intptr_t cid = obj.GetClassId(); 173 intptr_t cid = obj.GetClassId();
175 if (cid == kBigintCid) { 174 if (cid == kBigintCid) {
176 const Bigint& bigint = Bigint::Cast(obj); 175 const Bigint& bigint = Bigint::Cast(obj);
177 if (BigintOperations::FitsIntoUint64(bigint)) { 176 if (bigint.FitsIntoUint64()) {
178 *value = BigintOperations::ToUint64(bigint); 177 *value = bigint.AsUint64Value();
179 return true; 178 return true;
180 } 179 }
181 } 180 }
182 return false; 181 return false;
183 } 182 }
184 183
185 184
186 static bool GetNativeDoubleArgument(NativeArguments* arguments, 185 static bool GetNativeDoubleArgument(NativeArguments* arguments,
187 int arg_index, 186 int arg_index,
188 double* value) { 187 double* value) {
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 return Smi::Cast(result).Value(); 876 return Smi::Cast(result).Value();
878 } 877 }
879 if (result.IsMint()) { 878 if (result.IsMint()) {
880 const Mint& mint = Mint::Cast(result); 879 const Mint& mint = Mint::Cast(result);
881 if (!mint.IsNegative()) { 880 if (!mint.IsNegative()) {
882 return mint.AsInt64Value(); 881 return mint.AsInt64Value();
883 } 882 }
884 } 883 }
885 if (result.IsBigint()) { 884 if (result.IsBigint()) {
886 const Bigint& bigint = Bigint::Cast(result); 885 const Bigint& bigint = Bigint::Cast(result);
887 if (BigintOperations::FitsIntoUint64(bigint)) { 886 if (bigint.FitsIntoUint64()) {
888 return BigintOperations::ToUint64(bigint); 887 return bigint.AsUint64Value();
889 } 888 }
890 } 889 }
891 return 0; 890 return 0;
892 } 891 }
893 892
894 893
895 DART_EXPORT Dart_Handle Dart_HandleFromPersistent( 894 DART_EXPORT Dart_Handle Dart_HandleFromPersistent(
896 Dart_PersistentHandle object) { 895 Dart_PersistentHandle object) {
897 Isolate* isolate = Isolate::Current(); 896 Isolate* isolate = Isolate::Current();
898 CHECK_ISOLATE(isolate); 897 CHECK_ISOLATE(isolate);
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after
1899 if (class_id == kSmiCid || class_id == kMintCid) { 1898 if (class_id == kSmiCid || class_id == kMintCid) {
1900 *fits = true; 1899 *fits = true;
1901 return Api::Success(); 1900 return Api::Success();
1902 } 1901 }
1903 // Slow path for Mints and Bigints. 1902 // Slow path for Mints and Bigints.
1904 DARTSCOPE(isolate); 1903 DARTSCOPE(isolate);
1905 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); 1904 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1906 if (int_obj.IsNull()) { 1905 if (int_obj.IsNull()) {
1907 RETURN_TYPE_ERROR(isolate, integer, Integer); 1906 RETURN_TYPE_ERROR(isolate, integer, Integer);
1908 } 1907 }
1909 ASSERT(!BigintOperations::FitsIntoInt64(Bigint::Cast(int_obj))); 1908 ASSERT(!Bigint::Cast(int_obj).FitsIntoInt64());
1910 *fits = false; 1909 *fits = false;
1911 return Api::Success(); 1910 return Api::Success();
1912 } 1911 }
1913 1912
1914 1913
1915 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer, 1914 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer,
1916 bool* fits) { 1915 bool* fits) {
1917 // Fast path for Smis. 1916 // Fast path for Smis.
1918 Isolate* isolate = Isolate::Current(); 1917 Isolate* isolate = Isolate::Current();
1919 CHECK_ISOLATE(isolate); 1918 CHECK_ISOLATE(isolate);
1920 if (Api::IsSmi(integer)) { 1919 if (Api::IsSmi(integer)) {
1921 *fits = (Api::SmiValue(integer) >= 0); 1920 *fits = (Api::SmiValue(integer) >= 0);
1922 return Api::Success(); 1921 return Api::Success();
1923 } 1922 }
1924 // Slow path for Mints and Bigints. 1923 // Slow path for Mints and Bigints.
1925 DARTSCOPE(isolate); 1924 DARTSCOPE(isolate);
1926 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); 1925 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1927 if (int_obj.IsNull()) { 1926 if (int_obj.IsNull()) {
1928 RETURN_TYPE_ERROR(isolate, integer, Integer); 1927 RETURN_TYPE_ERROR(isolate, integer, Integer);
1929 } 1928 }
1930 ASSERT(!int_obj.IsSmi()); 1929 ASSERT(!int_obj.IsSmi());
1931 if (int_obj.IsMint()) { 1930 if (int_obj.IsMint()) {
1932 *fits = !int_obj.IsNegative(); 1931 *fits = !int_obj.IsNegative();
1933 } else { 1932 } else {
1934 *fits = BigintOperations::FitsIntoUint64(Bigint::Cast(int_obj)); 1933 *fits = Bigint::Cast(int_obj).FitsIntoUint64();
1935 } 1934 }
1936 return Api::Success(); 1935 return Api::Success();
1937 } 1936 }
1938 1937
1939 1938
1940 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) { 1939 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) {
1941 // Fast path for Smis. 1940 // Fast path for Smis.
1942 Isolate* isolate = Isolate::Current(); 1941 Isolate* isolate = Isolate::Current();
1943 CHECK_ISOLATE(isolate); 1942 CHECK_ISOLATE(isolate);
1944 if (Smi::IsValid(value)) { 1943 if (Smi::IsValid(value)) {
(...skipping 30 matching lines...) Expand all
1975 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); 1974 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
1976 if (int_obj.IsNull()) { 1975 if (int_obj.IsNull()) {
1977 RETURN_TYPE_ERROR(isolate, integer, Integer); 1976 RETURN_TYPE_ERROR(isolate, integer, Integer);
1978 } 1977 }
1979 ASSERT(!int_obj.IsSmi()); 1978 ASSERT(!int_obj.IsSmi());
1980 if (int_obj.IsMint()) { 1979 if (int_obj.IsMint()) {
1981 *value = int_obj.AsInt64Value(); 1980 *value = int_obj.AsInt64Value();
1982 return Api::Success(); 1981 return Api::Success();
1983 } else { 1982 } else {
1984 const Bigint& bigint = Bigint::Cast(int_obj); 1983 const Bigint& bigint = Bigint::Cast(int_obj);
1985 if (BigintOperations::FitsIntoInt64(bigint)) { 1984 if (bigint.FitsIntoInt64()) {
1986 *value = BigintOperations::ToInt64(bigint); 1985 *value = bigint.AsInt64Value();
1987 return Api::Success(); 1986 return Api::Success();
1988 } 1987 }
1989 } 1988 }
1990 return Api::NewError("%s: Integer %s cannot be represented as an int64_t.", 1989 return Api::NewError("%s: Integer %s cannot be represented as an int64_t.",
1991 CURRENT_FUNC, int_obj.ToCString()); 1990 CURRENT_FUNC, int_obj.ToCString());
1992 } 1991 }
1993 1992
1994 1993
1995 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer, 1994 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer,
1996 uint64_t* value) { 1995 uint64_t* value) {
(...skipping 12 matching lines...) Expand all
2009 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); 2008 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
2010 if (int_obj.IsNull()) { 2009 if (int_obj.IsNull()) {
2011 RETURN_TYPE_ERROR(isolate, integer, Integer); 2010 RETURN_TYPE_ERROR(isolate, integer, Integer);
2012 } 2011 }
2013 ASSERT(!int_obj.IsSmi()); 2012 ASSERT(!int_obj.IsSmi());
2014 if (int_obj.IsMint() && !int_obj.IsNegative()) { 2013 if (int_obj.IsMint() && !int_obj.IsNegative()) {
2015 *value = int_obj.AsInt64Value(); 2014 *value = int_obj.AsInt64Value();
2016 return Api::Success(); 2015 return Api::Success();
2017 } else { 2016 } else {
2018 const Bigint& bigint = Bigint::Cast(int_obj); 2017 const Bigint& bigint = Bigint::Cast(int_obj);
2019 if (BigintOperations::FitsIntoUint64(bigint)) { 2018 if (bigint.FitsIntoUint64()) {
2020 *value = BigintOperations::ToUint64(bigint); 2019 *value = bigint.AsUint64Value();
2021 return Api::Success(); 2020 return Api::Success();
2022 } 2021 }
2023 } 2022 }
2024 return Api::NewError("%s: Integer %s cannot be represented as a uint64_t.", 2023 return Api::NewError("%s: Integer %s cannot be represented as a uint64_t.",
2025 CURRENT_FUNC, int_obj.ToCString()); 2024 CURRENT_FUNC, int_obj.ToCString());
2026 } 2025 }
2027 2026
2028 2027
2029 static uword BigintAllocate(intptr_t size) { 2028 static uword BigintAllocate(intptr_t size) {
2030 return Api::TopScope(Isolate::Current())->zone()->AllocUnsafe(size); 2029 return Api::TopScope(Isolate::Current())->zone()->AllocUnsafe(size);
2031 } 2030 }
2032 2031
2033 2032
2034 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer, 2033 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer,
2035 const char** value) { 2034 const char** value) {
2036 Isolate* isolate = Isolate::Current(); 2035 Isolate* isolate = Isolate::Current();
2037 DARTSCOPE(isolate); 2036 DARTSCOPE(isolate);
2038 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); 2037 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer);
2039 if (int_obj.IsNull()) { 2038 if (int_obj.IsNull()) {
2040 RETURN_TYPE_ERROR(isolate, integer, Integer); 2039 RETURN_TYPE_ERROR(isolate, integer, Integer);
2041 } 2040 }
2042 if (int_obj.IsSmi() || int_obj.IsMint()) { 2041 if (int_obj.IsSmi() || int_obj.IsMint()) {
2043 const Bigint& bigint = Bigint::Handle(isolate, 2042 const Bigint& bigint = Bigint::Handle(isolate,
2044 BigintOperations::NewFromInt64(int_obj.AsInt64Value())); 2043 Bigint::NewFromInt64(int_obj.AsInt64Value()));
2045 *value = BigintOperations::ToHexCString(bigint, BigintAllocate); 2044 *value = bigint.ToHexCString(BigintAllocate);
2046 } else { 2045 } else {
2047 *value = BigintOperations::ToHexCString(Bigint::Cast(int_obj), 2046 *value = Bigint::Cast(int_obj).ToHexCString(BigintAllocate);
2048 BigintAllocate);
2049 } 2047 }
2050 return Api::Success(); 2048 return Api::Success();
2051 } 2049 }
2052 2050
2053 2051
2054 DART_EXPORT Dart_Handle Dart_NewDouble(double value) { 2052 DART_EXPORT Dart_Handle Dart_NewDouble(double value) {
2055 Isolate* isolate = Isolate::Current(); 2053 Isolate* isolate = Isolate::Current();
2056 DARTSCOPE(isolate); 2054 DARTSCOPE(isolate);
2057 CHECK_CALLBACK_STATE(isolate); 2055 CHECK_CALLBACK_STATE(isolate);
2058 return Api::NewHandle(isolate, Double::New(value)); 2056 return Api::NewHandle(isolate, Double::New(value));
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
2478 } else if (retval.IsMint() || retval.IsBigint()) { 2476 } else if (retval.IsMint() || retval.IsBigint()) {
2479 if (retval.IsMint()) { 2477 if (retval.IsMint()) {
2480 int64_t mint_value = Mint::Cast(retval).value(); 2478 int64_t mint_value = Mint::Cast(retval).value();
2481 if (mint_value >= kIntptrMin && mint_value <= kIntptrMax) { 2479 if (mint_value >= kIntptrMin && mint_value <= kIntptrMax) {
2482 *len = static_cast<intptr_t>(mint_value); 2480 *len = static_cast<intptr_t>(mint_value);
2483 } 2481 }
2484 } else { 2482 } else {
2485 // Check for a non-canonical Mint range value. 2483 // Check for a non-canonical Mint range value.
2486 ASSERT(retval.IsBigint()); 2484 ASSERT(retval.IsBigint());
2487 const Bigint& bigint = Bigint::Handle(); 2485 const Bigint& bigint = Bigint::Handle();
2488 if (BigintOperations::FitsIntoInt64(bigint)) { 2486 if (bigint.FitsIntoInt64()) {
2489 int64_t bigint_value = bigint.AsInt64Value(); 2487 int64_t bigint_value = bigint.AsInt64Value();
2490 if (bigint_value >= kIntptrMin && bigint_value <= kIntptrMax) { 2488 if (bigint_value >= kIntptrMin && bigint_value <= kIntptrMax) {
2491 *len = static_cast<intptr_t>(bigint_value); 2489 *len = static_cast<intptr_t>(bigint_value);
2492 } 2490 }
2493 } 2491 }
2494 } 2492 }
2495 return Api::NewError("Length of List object is greater than the " 2493 return Api::NewError("Length of List object is greater than the "
2496 "maximum value that 'len' parameter can hold"); 2494 "maximum value that 'len' parameter can hold");
2497 } else if (retval.IsError()) { 2495 } else if (retval.IsError()) {
2498 return Api::NewHandle(isolate, retval.raw()); 2496 return Api::NewHandle(isolate, retval.raw());
(...skipping 2856 matching lines...) Expand 10 before | Expand all | Expand 10 after
5355 5353
5356 5354
5357 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( 5355 DART_EXPORT void Dart_RegisterRootServiceRequestCallback(
5358 const char* name, 5356 const char* name,
5359 Dart_ServiceRequestCallback callback, 5357 Dart_ServiceRequestCallback callback,
5360 void* user_data) { 5358 void* user_data) {
5361 Service::RegisterRootEmbedderCallback(name, callback, user_data); 5359 Service::RegisterRootEmbedderCallback(name, callback, user_data);
5362 } 5360 }
5363 5361
5364 } // namespace dart 5362 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698