Skip to content

Commit a0a6977

Browse files
committed
Add additional definitions to JSSymbol
1 parent 65f16e3 commit a0a6977

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
but `false` on Windows. Use `FileSystemEntity.typeSync()` instead to get
1717
portable behavior.
1818

19+
#### `dart:js_interop`
20+
21+
- Added a constructor to `JSSymbol`, as well as `JSSymbol.key`,
22+
`JSSymbol.description`, and static methods for all well-known ECMAScript
23+
symbols.
24+
1925
#### `dart:js_util`
2026

2127
- dart2wasm no longer supports `dart:js_util`. Any code that imports

sdk/lib/js_interop/js_interop.dart

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,142 @@ extension type JSBoolean._(JSBooleanRepType _jsBoolean) implements JSAny {}
529529
/// A JavaScript string.
530530
extension type JSString._(JSStringRepType _jsString) implements JSAny {}
531531

532+
@JS('Symbol')
533+
external JSSymbol _constructSymbol([String? description]);
534+
532535
/// A JavaScript `Symbol`.
533-
extension type JSSymbol._(JSSymbolRepType _jsSymbol) implements JSAny {}
536+
@JS('Symbol')
537+
extension type JSSymbol._(JSSymbolRepType _jsSymbol) implements JSAny {
538+
// TODO(srujzs): See if this can be made `const` so it can be used in similar
539+
// situations to a Dart symbol literal.
540+
/// Creates a new, unique JavaScript `Symbol`.
541+
///
542+
/// If [description] is provided, it's used for debugging but not to access
543+
/// the symbol itself.
544+
@Since('3.11')
545+
JSSymbol([String? description])
546+
: _jsSymbol =
547+
(description == null
548+
? _constructSymbol()
549+
: _constructSymbol(description))
550+
._jsSymbol;
551+
552+
/// Searches for an existing symbol in a runtime-wide symbol registry with the
553+
/// given key and returns it if found.
554+
///
555+
/// Otherwise, creates a new symbol with this key, adds it to the global
556+
/// registry, and returns it.
557+
@Since('3.11')
558+
@JS('for')
559+
external static JSSymbol forKey(String key);
560+
561+
/// `Symbol.asyncDispose` from the ECMAScript [explicit resource management]
562+
/// feature.
563+
///
564+
/// [explicit resource management]: https://github.com/tc39/proposal-explicit-resource-management
565+
@Since('3.11')
566+
external static JSSymbol get asyncDispose;
567+
568+
/// See [`Symbol.asyncIterator`].
569+
///
570+
/// [`Symbol.asyncIterator`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
571+
@Since('3.11')
572+
external static JSSymbol get asyncIterator;
573+
574+
/// `Symbol.dispose` from the ECMAScript [explicit resource management]
575+
/// feature.
576+
///
577+
/// [explicit resource management]: https://github.com/tc39/proposal-explicit-resource-management
578+
@Since('3.11')
579+
external static JSSymbol get dispose;
580+
581+
/// See [`Symbol.hasInstance`].
582+
///
583+
/// [`Symbol.hasInstance`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance
584+
@Since('3.11')
585+
external static JSSymbol get hasInstance;
586+
587+
/// See [`Symbol.isConcatSpreadable`].
588+
///
589+
/// [`Symbol.isConcatSpreadable`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/isConcatSpreadable
590+
@Since('3.11')
591+
external static JSSymbol get isConcatSpreadable;
592+
593+
/// See [`Symbol.iterator`].
594+
///
595+
/// [`Symbol.iterator`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
596+
@Since('3.11')
597+
external static JSSymbol get iterator;
598+
599+
/// See [`Symbol.match`].
600+
///
601+
/// [`Symbol.match`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/match
602+
@Since('3.11')
603+
external static JSSymbol get match;
604+
605+
/// See [`Symbol.matchAll`].
606+
///
607+
/// [`Symbol.matchAll`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/matchAll
608+
@Since('3.11')
609+
external static JSSymbol get matchAll;
610+
611+
/// See [`Symbol.replace`].
612+
///
613+
/// [`Symbol.replace`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/replace
614+
@Since('3.11')
615+
external static JSSymbol get replace;
616+
617+
/// See [`Symbol.search`].
618+
///
619+
/// [`Symbol.search`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/search
620+
@Since('3.11')
621+
external static JSSymbol get search;
622+
623+
/// See [`Symbol.species`].
624+
///
625+
/// [`Symbol.species`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/species
626+
@Since('3.11')
627+
external static JSSymbol get species;
628+
629+
/// See [`Symbol.split`].
630+
///
631+
/// [`Symbol.split`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/split
632+
@Since('3.11')
633+
external static JSSymbol get split;
634+
635+
/// See [`Symbol.toPrimitive`].
636+
///
637+
/// [`Symbol.toPrimitive`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive
638+
@Since('3.11')
639+
external static JSSymbol get toPrimitive;
640+
641+
/// See [`Symbol.toStringTag`].
642+
///
643+
/// [`Symbol.toStringTag`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
644+
@Since('3.11')
645+
external static JSSymbol get toStringTag;
646+
647+
/// See [`Symbol.unscopables`].
648+
///
649+
/// [`Symbol.unscopables`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/unscopables
650+
@Since('3.11')
651+
external static JSSymbol get unscopables;
652+
653+
@Since('3.11')
654+
@JS('keyFor')
655+
external static String? _keyFor(JSSymbol symbol);
656+
657+
/// Returns the shared symbol key from the global symbol registry for this
658+
/// symbol (as registered with [forKey]), if this symbol was created with
659+
/// [Symbol.forKey].
660+
@Since('3.11')
661+
String? get key => _keyFor(this);
662+
663+
/// A string containing the description of the symbol, as passed to [new
664+
/// Symbol].
665+
@Since('3.11')
666+
external String get description;
667+
}
534668

535669
/// A JavaScript `BigInt`.
536670
extension type JSBigInt._(JSBigIntRepType _jsBigInt) implements JSAny {}

0 commit comments

Comments
 (0)