OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 6 |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
(...skipping 1072 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 DARTSCOPE(isolate); | 1083 DARTSCOPE(isolate); |
1084 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); | 1084 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); |
1085 return obj.IsNumber(); | 1085 return obj.IsNumber(); |
1086 } | 1086 } |
1087 | 1087 |
1088 | 1088 |
1089 // --- Integers ---- | 1089 // --- Integers ---- |
1090 | 1090 |
1091 | 1091 |
1092 DART_EXPORT bool Dart_IsInteger(Dart_Handle object) { | 1092 DART_EXPORT bool Dart_IsInteger(Dart_Handle object) { |
| 1093 // Fast path for Smis. |
| 1094 if (Api::IsSmi(object)) { |
| 1095 return true; |
| 1096 } |
| 1097 |
1093 Isolate* isolate = Isolate::Current(); | 1098 Isolate* isolate = Isolate::Current(); |
1094 DARTSCOPE(isolate); | 1099 DARTSCOPE(isolate); |
1095 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); | 1100 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); |
1096 return obj.IsInteger(); | 1101 return obj.IsInteger(); |
1097 } | 1102 } |
1098 | 1103 |
1099 | 1104 |
1100 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer, | 1105 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer, |
1101 bool* fits) { | 1106 bool* fits) { |
| 1107 // Fast path for Smis. |
1102 Isolate* isolate = Isolate::Current(); | 1108 Isolate* isolate = Isolate::Current(); |
1103 DARTSCOPE(isolate); | 1109 CHECK_ISOLATE(isolate); |
| 1110 if (Api::IsSmi(integer)) { |
| 1111 *fits = true; |
| 1112 return Api::Success(isolate); |
| 1113 } |
| 1114 |
| 1115 DARTSCOPE_NOCHECKS(isolate); |
1104 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); | 1116 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); |
1105 if (int_obj.IsNull()) { | 1117 if (int_obj.IsNull()) { |
1106 RETURN_TYPE_ERROR(isolate, integer, Integer); | 1118 RETURN_TYPE_ERROR(isolate, integer, Integer); |
1107 } | 1119 } |
1108 if (int_obj.IsSmi() || int_obj.IsMint()) { | 1120 if (int_obj.IsSmi() || int_obj.IsMint()) { |
1109 *fits = true; | 1121 *fits = true; |
1110 } else { | 1122 } else { |
1111 ASSERT(int_obj.IsBigint()); | 1123 ASSERT(int_obj.IsBigint()); |
1112 #if defined(DEBUG) | 1124 #if defined(DEBUG) |
1113 Bigint& bigint = Bigint::Handle(isolate); | 1125 Bigint& bigint = Bigint::Handle(isolate); |
1114 bigint ^= int_obj.raw(); | 1126 bigint ^= int_obj.raw(); |
1115 ASSERT(!BigintOperations::FitsIntoMint(bigint)); | 1127 ASSERT(!BigintOperations::FitsIntoMint(bigint)); |
1116 #endif | 1128 #endif |
1117 *fits = false; | 1129 *fits = false; |
1118 } | 1130 } |
1119 return Api::Success(isolate); | 1131 return Api::Success(isolate); |
1120 } | 1132 } |
1121 | 1133 |
1122 | 1134 |
1123 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer, | 1135 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer, |
1124 bool* fits) { | 1136 bool* fits) { |
| 1137 // Fast path for Smis. |
1125 Isolate* isolate = Isolate::Current(); | 1138 Isolate* isolate = Isolate::Current(); |
1126 DARTSCOPE(isolate); | 1139 CHECK_ISOLATE(isolate); |
| 1140 if (Api::IsSmi(integer)) { |
| 1141 *fits = (Api::SmiValue(integer) >= 0); |
| 1142 return Api::Success(isolate); |
| 1143 } |
| 1144 |
| 1145 DARTSCOPE_NOCHECKS(isolate); |
1127 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); | 1146 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); |
1128 if (int_obj.IsNull()) { | 1147 if (int_obj.IsNull()) { |
1129 RETURN_TYPE_ERROR(isolate, integer, Integer); | 1148 RETURN_TYPE_ERROR(isolate, integer, Integer); |
1130 } | 1149 } |
1131 if (int_obj.IsSmi() || int_obj.IsMint()) { | 1150 if (int_obj.IsSmi() || int_obj.IsMint()) { |
1132 *fits = !int_obj.IsNegative(); | 1151 *fits = !int_obj.IsNegative(); |
1133 } else { | 1152 } else { |
1134 ASSERT(int_obj.IsBigint()); | 1153 ASSERT(int_obj.IsBigint()); |
1135 Bigint& bigint = Bigint::Handle(isolate); | 1154 Bigint& bigint = Bigint::Handle(isolate); |
1136 bigint ^= int_obj.raw(); | 1155 bigint ^= int_obj.raw(); |
1137 *fits = BigintOperations::FitsIntoUint64(bigint); | 1156 *fits = BigintOperations::FitsIntoUint64(bigint); |
1138 } | 1157 } |
1139 return Api::Success(isolate); | 1158 return Api::Success(isolate); |
1140 } | 1159 } |
1141 | 1160 |
1142 | 1161 |
1143 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) { | 1162 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) { |
| 1163 // Fast path for Smis. |
1144 Isolate* isolate = Isolate::Current(); | 1164 Isolate* isolate = Isolate::Current(); |
1145 DARTSCOPE(isolate); | 1165 CHECK_ISOLATE(isolate); |
| 1166 if (Smi::IsValid64(value)) { |
| 1167 NOHANDLESCOPE(isolate); |
| 1168 return Api::NewHandle(isolate, Smi::New(value)); |
| 1169 } |
| 1170 |
| 1171 DARTSCOPE_NOCHECKS(isolate); |
1146 return Api::NewHandle(isolate, Integer::New(value)); | 1172 return Api::NewHandle(isolate, Integer::New(value)); |
1147 } | 1173 } |
1148 | 1174 |
1149 | 1175 |
1150 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) { | 1176 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) { |
1151 Isolate* isolate = Isolate::Current(); | 1177 Isolate* isolate = Isolate::Current(); |
1152 DARTSCOPE(isolate); | 1178 DARTSCOPE(isolate); |
1153 const String& str_obj = String::Handle(isolate, String::New(str)); | 1179 const String& str_obj = String::Handle(isolate, String::New(str)); |
1154 return Api::NewHandle(isolate, Integer::New(str_obj)); | 1180 return Api::NewHandle(isolate, Integer::New(str_obj)); |
1155 } | 1181 } |
1156 | 1182 |
1157 | 1183 |
1158 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer, | 1184 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer, |
1159 int64_t* value) { | 1185 int64_t* value) { |
| 1186 // Fast path for Smis. |
1160 Isolate* isolate = Isolate::Current(); | 1187 Isolate* isolate = Isolate::Current(); |
1161 DARTSCOPE(isolate); | 1188 CHECK_ISOLATE(isolate); |
| 1189 if (Api::IsSmi(integer)) { |
| 1190 *value = Api::SmiValue(integer); |
| 1191 return Api::Success(isolate); |
| 1192 } |
| 1193 |
| 1194 DARTSCOPE_NOCHECKS(isolate); |
1162 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); | 1195 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); |
1163 if (int_obj.IsNull()) { | 1196 if (int_obj.IsNull()) { |
1164 RETURN_TYPE_ERROR(isolate, integer, Integer); | 1197 RETURN_TYPE_ERROR(isolate, integer, Integer); |
1165 } | 1198 } |
1166 if (int_obj.IsSmi() || int_obj.IsMint()) { | 1199 if (int_obj.IsSmi() || int_obj.IsMint()) { |
1167 *value = int_obj.AsInt64Value(); | 1200 *value = int_obj.AsInt64Value(); |
1168 return Api::Success(isolate); | 1201 return Api::Success(isolate); |
1169 } else { | 1202 } else { |
1170 ASSERT(int_obj.IsBigint()); | 1203 ASSERT(int_obj.IsBigint()); |
1171 Bigint& bigint = Bigint::Handle(isolate); | 1204 Bigint& bigint = Bigint::Handle(isolate); |
1172 bigint ^= int_obj.raw(); | 1205 bigint ^= int_obj.raw(); |
1173 if (BigintOperations::FitsIntoMint(bigint)) { | 1206 if (BigintOperations::FitsIntoMint(bigint)) { |
1174 *value = BigintOperations::ToMint(bigint); | 1207 *value = BigintOperations::ToMint(bigint); |
1175 return Api::Success(isolate); | 1208 return Api::Success(isolate); |
1176 } | 1209 } |
1177 } | 1210 } |
1178 return Api::NewError("%s: Integer %s cannot be represented as an int64_t.", | 1211 return Api::NewError("%s: Integer %s cannot be represented as an int64_t.", |
1179 CURRENT_FUNC, int_obj.ToCString()); | 1212 CURRENT_FUNC, int_obj.ToCString()); |
1180 } | 1213 } |
1181 | 1214 |
1182 | 1215 |
1183 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer, | 1216 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer, |
1184 uint64_t* value) { | 1217 uint64_t* value) { |
| 1218 // Fast path for Smis. |
1185 Isolate* isolate = Isolate::Current(); | 1219 Isolate* isolate = Isolate::Current(); |
1186 DARTSCOPE(isolate); | 1220 CHECK_ISOLATE(isolate); |
| 1221 if (Api::IsSmi(integer)) { |
| 1222 intptr_t smi_value = Api::SmiValue(integer); |
| 1223 if (smi_value >= 0) { |
| 1224 *value = smi_value; |
| 1225 return Api::Success(isolate); |
| 1226 } |
| 1227 } |
| 1228 |
| 1229 DARTSCOPE_NOCHECKS(isolate); |
1187 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); | 1230 const Integer& int_obj = Api::UnwrapIntegerHandle(isolate, integer); |
1188 if (int_obj.IsNull()) { | 1231 if (int_obj.IsNull()) { |
1189 RETURN_TYPE_ERROR(isolate, integer, Integer); | 1232 RETURN_TYPE_ERROR(isolate, integer, Integer); |
1190 } | 1233 } |
1191 if (int_obj.IsSmi() || int_obj.IsMint()) { | 1234 if (int_obj.IsSmi() || int_obj.IsMint()) { |
1192 if (!int_obj.IsNegative()) { | 1235 if (!int_obj.IsNegative()) { |
1193 *value = int_obj.AsInt64Value(); | 1236 *value = int_obj.AsInt64Value(); |
1194 return Api::Success(isolate); | 1237 return Api::Success(isolate); |
1195 } | 1238 } |
1196 } else { | 1239 } else { |
(...skipping 2202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3399 *buffer = NULL; | 3442 *buffer = NULL; |
3400 } | 3443 } |
3401 delete debug_region; | 3444 delete debug_region; |
3402 } else { | 3445 } else { |
3403 *buffer = NULL; | 3446 *buffer = NULL; |
3404 *buffer_size = 0; | 3447 *buffer_size = 0; |
3405 } | 3448 } |
3406 } | 3449 } |
3407 | 3450 |
3408 } // namespace dart | 3451 } // namespace dart |
OLD | NEW |