@@ -5261,12 +5261,12 @@ DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library,
5261
5261
return Api::NewHandle (T, cls.RareType ());
5262
5262
}
5263
5263
5264
- DART_EXPORT Dart_Handle Dart_GetType (Dart_Handle library,
5265
- Dart_Handle class_name,
5266
- intptr_t number_of_type_arguments,
5267
- Dart_Handle* type_arguments) {
5264
+ static Dart_Handle GetTypeCommon (Dart_Handle library,
5265
+ Dart_Handle class_name,
5266
+ intptr_t number_of_type_arguments,
5267
+ Dart_Handle* type_arguments,
5268
+ Nullability nullability) {
5268
5269
DARTSCOPE (Thread::Current ());
5269
-
5270
5270
// Validate the input arguments.
5271
5271
const Library& lib = Api::UnwrapLibraryHandle (Z, library);
5272
5272
if (lib.IsNull ()) {
@@ -5288,51 +5288,125 @@ DART_EXPORT Dart_Handle Dart_GetType(Dart_Handle library,
5288
5288
}
5289
5289
cls.EnsureDeclarationLoaded ();
5290
5290
CHECK_ERROR_HANDLE (cls.VerifyEntryPoint ());
5291
+
5292
+ Type& type = Type::Handle ();
5291
5293
if (cls.NumTypeArguments () == 0 ) {
5292
5294
if (number_of_type_arguments != 0 ) {
5293
5295
return Api::NewError (
5294
5296
" Invalid number of type arguments specified, "
5295
5297
" got %" Pd " expected 0" ,
5296
5298
number_of_type_arguments);
5297
5299
}
5298
- return Api::NewHandle (T, Type::NewNonParameterizedType (cls));
5299
- }
5300
- intptr_t num_expected_type_arguments = cls.NumTypeParameters ();
5301
- TypeArguments& type_args_obj = TypeArguments::Handle ();
5302
- if (number_of_type_arguments > 0 ) {
5303
- if (type_arguments == NULL ) {
5304
- RETURN_NULL_ERROR (type_arguments);
5305
- }
5306
- if (num_expected_type_arguments != number_of_type_arguments) {
5307
- return Api::NewError (
5308
- " Invalid number of type arguments specified, "
5309
- " got %" Pd " expected %" Pd,
5310
- number_of_type_arguments, num_expected_type_arguments);
5311
- }
5312
- const Array& array = Api::UnwrapArrayHandle (Z, *type_arguments);
5313
- if (array.IsNull ()) {
5314
- RETURN_TYPE_ERROR (Z, *type_arguments, Array);
5315
- }
5316
- if (array.Length () != num_expected_type_arguments) {
5317
- return Api::NewError (
5318
- " Invalid type arguments specified, expected an "
5319
- " array of len %" Pd " but got an array of len %" Pd,
5320
- number_of_type_arguments, array.Length ());
5321
- }
5322
- // Set up the type arguments array.
5323
- type_args_obj = TypeArguments::New (num_expected_type_arguments);
5324
- AbstractType& type_arg = AbstractType::Handle ();
5325
- for (intptr_t i = 0 ; i < number_of_type_arguments; i++) {
5326
- type_arg ^= array.At (i);
5327
- type_args_obj.SetTypeAt (i, type_arg);
5300
+ type ^= Type::NewNonParameterizedType (cls);
5301
+ type ^= type.ToNullability (nullability, Heap::kOld );
5302
+ } else {
5303
+ intptr_t num_expected_type_arguments = cls.NumTypeParameters ();
5304
+ TypeArguments& type_args_obj = TypeArguments::Handle ();
5305
+ if (number_of_type_arguments > 0 ) {
5306
+ if (type_arguments == NULL ) {
5307
+ RETURN_NULL_ERROR (type_arguments);
5308
+ }
5309
+ if (num_expected_type_arguments != number_of_type_arguments) {
5310
+ return Api::NewError (
5311
+ " Invalid number of type arguments specified, "
5312
+ " got %" Pd " expected %" Pd,
5313
+ number_of_type_arguments, num_expected_type_arguments);
5314
+ }
5315
+ const Array& array = Api::UnwrapArrayHandle (Z, *type_arguments);
5316
+ if (array.IsNull ()) {
5317
+ RETURN_TYPE_ERROR (Z, *type_arguments, Array);
5318
+ }
5319
+ if (array.Length () != num_expected_type_arguments) {
5320
+ return Api::NewError (
5321
+ " Invalid type arguments specified, expected an "
5322
+ " array of len %" Pd " but got an array of len %" Pd,
5323
+ number_of_type_arguments, array.Length ());
5324
+ }
5325
+ // Set up the type arguments array.
5326
+ type_args_obj = TypeArguments::New (num_expected_type_arguments);
5327
+ AbstractType& type_arg = AbstractType::Handle ();
5328
+ for (intptr_t i = 0 ; i < number_of_type_arguments; i++) {
5329
+ type_arg ^= array.At (i);
5330
+ type_args_obj.SetTypeAt (i, type_arg);
5331
+ }
5328
5332
}
5333
+
5334
+ // Construct the type object, canonicalize it and return.
5335
+ type ^=
5336
+ Type::New (cls, type_args_obj, TokenPosition::kNoSource , nullability);
5329
5337
}
5338
+ type ^= ClassFinalizer::FinalizeType (cls, type);
5339
+ return Api::NewHandle (T, type.raw ());
5340
+ }
5341
+
5342
+ DART_EXPORT Dart_Handle Dart_GetType (Dart_Handle library,
5343
+ Dart_Handle class_name,
5344
+ intptr_t number_of_type_arguments,
5345
+ Dart_Handle* type_arguments) {
5346
+ return GetTypeCommon (library, class_name, number_of_type_arguments,
5347
+ type_arguments, Nullability::kLegacy );
5348
+ }
5349
+
5350
+ DART_EXPORT Dart_Handle Dart_GetNullableType (Dart_Handle library,
5351
+ Dart_Handle class_name,
5352
+ intptr_t number_of_type_arguments,
5353
+ Dart_Handle* type_arguments) {
5354
+ return GetTypeCommon (library, class_name, number_of_type_arguments,
5355
+ type_arguments, Nullability::kNullable );
5356
+ }
5357
+
5358
+ DART_EXPORT Dart_Handle
5359
+ Dart_GetNonNullableType (Dart_Handle library,
5360
+ Dart_Handle class_name,
5361
+ intptr_t number_of_type_arguments,
5362
+ Dart_Handle* type_arguments) {
5363
+ return GetTypeCommon (library, class_name, number_of_type_arguments,
5364
+ type_arguments, Nullability::kNonNullable );
5365
+ }
5366
+
5367
+ static Dart_Handle TypeToHelper (Dart_Handle type, Nullability nullability) {
5368
+ DARTSCOPE (Thread::Current ());
5369
+ const Type& ty = Api::UnwrapTypeHandle (Z, type);
5370
+ if (ty.IsNull ()) {
5371
+ RETURN_TYPE_ERROR (Z, type, Type);
5372
+ }
5373
+ if (ty.nullability () == nullability) {
5374
+ return type;
5375
+ }
5376
+ return Api::NewHandle (T, ty.ToNullability (nullability, Heap::kOld ));
5377
+ }
5378
+
5379
+ DART_EXPORT Dart_Handle Dart_TypeToNullableType (Dart_Handle type) {
5380
+ return TypeToHelper (type, Nullability::kNullable );
5381
+ }
5382
+
5383
+ DART_EXPORT Dart_Handle Dart_TypeToNonNullableType (Dart_Handle type) {
5384
+ return TypeToHelper (type, Nullability::kNonNullable );
5385
+ }
5386
+
5387
+ static Dart_Handle IsOfTypeNullabilityHelper (Dart_Handle type,
5388
+ Nullability nullability,
5389
+ bool * result) {
5390
+ DARTSCOPE (Thread::Current ());
5391
+ const Type& ty = Api::UnwrapTypeHandle (Z, type);
5392
+ if (ty.IsNull ()) {
5393
+ *result = false ;
5394
+ RETURN_TYPE_ERROR (Z, type, Type);
5395
+ }
5396
+ *result = (ty.nullability () == nullability);
5397
+ return Api::Success ();
5398
+ }
5399
+
5400
+ DART_EXPORT Dart_Handle Dart_IsNullableType (Dart_Handle type, bool * result) {
5401
+ return IsOfTypeNullabilityHelper (type, Nullability::kNullable , result);
5402
+ }
5403
+
5404
+ DART_EXPORT Dart_Handle Dart_IsNonNullableType (Dart_Handle type, bool * result) {
5405
+ return IsOfTypeNullabilityHelper (type, Nullability::kNonNullable , result);
5406
+ }
5330
5407
5331
- // Construct the type object, canonicalize it and return.
5332
- Type& instantiated_type =
5333
- Type::Handle (Type::New (cls, type_args_obj, TokenPosition::kNoSource ));
5334
- instantiated_type ^= ClassFinalizer::FinalizeType (cls, instantiated_type);
5335
- return Api::NewHandle (T, instantiated_type.raw ());
5408
+ DART_EXPORT Dart_Handle Dart_IsLegacyType (Dart_Handle type, bool * result) {
5409
+ return IsOfTypeNullabilityHelper (type, Nullability::kLegacy , result);
5336
5410
}
5337
5411
5338
5412
DART_EXPORT Dart_Handle Dart_LibraryUrl (Dart_Handle library) {
0 commit comments