Skip to content

Commit

Permalink
Simplify the use of JS and VM annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Jan 4, 2025
1 parent 46e7f94 commit 092f4e6
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bin/generate_sequence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Future<void> generateImplementation(int index) async {
out.writeln('/// Converts this [Record] with $index positional values to '
'a new type [R] using');
out.writeln('/// the provided [callback] with $index positional arguments.');
out.writeln('@inlineVm @inlineJs');
out.writeln('@inline');
out.writeln('R map<R>(R Function(${valueTypes.join(', ')}) callback) => '
'callback(${valueNames.join(', ')});');
out.writeln('}');
Expand Down
6 changes: 2 additions & 4 deletions lib/src/core/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ class Context {
final int position;

/// Returns a result indicating a parse success.
@inlineVm
@inlineJs
@inline
@useResult
Success<R> success<R>(R result, [int? position]) =>
Success<R>(buffer, position ?? this.position, result);

/// Returns a result indicating a parse failure.
@inlineVm
@inlineJs
@inline
@useResult
Failure failure(String message, [int? position]) =>
Failure(buffer, position ?? this.position, message);
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ abstract class Parser<R> {
/// makes it possible to wrap the parser without loosing type information.
@internal
@nonVirtual
@inlineVm
@inlineJs
@inline
T captureResultGeneric<T>(T Function<R>(Parser<R> self) callback) =>
callback<R>(this);

Expand Down
4 changes: 4 additions & 0 deletions lib/src/core/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class Success<R> extends Result<R> {
bool get isSuccess => true;

@override
@inlineVm
final R value;

@override
@inlineVm
String get message =>
throw UnsupportedError('Successful parse results do not have a message.');

Expand All @@ -53,9 +55,11 @@ class Failure extends Result<Never> {
bool get isFailure => true;

@override
@inlineVm
Never get value => throw ParserException(this);

@override
@inlineVm
final String message;

@override
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/action/trimming.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class TrimmingParser<R> extends DelegateParser<R, R>
return result < 0 ? -1 : _trim(right, buffer, result);
}

@inlineVm
@inlineJs
@inline
int _trim(Parser parser, String buffer, int position) {
for (;;) {
final result = parser.fastParseOn(buffer, position);
Expand Down
6 changes: 2 additions & 4 deletions lib/src/parser/character/predicates/lookup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ final class LookupCharPredicate extends CharacterPredicate {
bool test(int charCode) =>
start <= charCode && charCode <= stop && _testBit(charCode - start);

@inlineJs
@inlineVm
@noBoundsChecksVm
@noBoundsChecksJs
@inline
@noBoundsChecks
bool _testBit(int value) =>
(bits[value >> _shift] & _mask[value & _offset]) != 0;

Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/character/predicates/ranges.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ final class RangesCharPredicate extends CharacterPredicate {
final Uint32List ranges;

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
bool test(int charCode) {
var min = 0;
var max = ranges.length - 2;
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ class SequenceParser2<R1, R2> extends Parser<(R1, R2)>
extension RecordOfValuesExtension2<T1, T2> on (T1, T2) {
/// Converts this [Record] with 2 positional values to a new type [R] using
/// the provided [callback] with 2 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2) callback) => callback($1, $2);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ class SequenceParser3<R1, R2, R3> extends Parser<(R1, R2, R3)>
extension RecordOfValuesExtension3<T1, T2, T3> on (T1, T2, T3) {
/// Converts this [Record] with 3 positional values to a new type [R] using
/// the provided [callback] with 3 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3) callback) => callback($1, $2, $3);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ class SequenceParser4<R1, R2, R3, R4> extends Parser<(R1, R2, R3, R4)>
extension RecordOfValuesExtension4<T1, T2, T3, T4> on (T1, T2, T3, T4) {
/// Converts this [Record] with 4 positional values to a new type [R] using
/// the provided [callback] with 4 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3, T4) callback) => callback($1, $2, $3, $4);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ class SequenceParser5<R1, R2, R3, R4, R5> extends Parser<(R1, R2, R3, R4, R5)>
extension RecordOfValuesExtension5<T1, T2, T3, T4, T5> on (T1, T2, T3, T4, T5) {
/// Converts this [Record] with 5 positional values to a new type [R] using
/// the provided [callback] with 5 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3, T4, T5) callback) =>
callback($1, $2, $3, $4, $5);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ extension RecordOfValuesExtension6<T1, T2, T3, T4, T5, T6> on (
) {
/// Converts this [Record] with 6 positional values to a new type [R] using
/// the provided [callback] with 6 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3, T4, T5, T6) callback) =>
callback($1, $2, $3, $4, $5, $6);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_7.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ extension RecordOfValuesExtension7<T1, T2, T3, T4, T5, T6, T7> on (
) {
/// Converts this [Record] with 7 positional values to a new type [R] using
/// the provided [callback] with 7 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3, T4, T5, T6, T7) callback) =>
callback($1, $2, $3, $4, $5, $6, $7);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ extension RecordOfValuesExtension8<T1, T2, T3, T4, T5, T6, T7, T8> on (
) {
/// Converts this [Record] with 8 positional values to a new type [R] using
/// the provided [callback] with 8 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3, T4, T5, T6, T7, T8) callback) =>
callback($1, $2, $3, $4, $5, $6, $7, $8);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/combinator/generated/sequence_9.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ extension RecordOfValuesExtension9<T1, T2, T3, T4, T5, T6, T7, T8, T9> on (
) {
/// Converts this [Record] with 9 positional values to a new type [R] using
/// the provided [callback] with 9 positional arguments.
@inlineVm
@inlineJs
@inline
R map<R>(R Function(T1, T2, T3, T4, T5, T6, T7, T8, T9) callback) =>
callback($1, $2, $3, $4, $5, $6, $7, $8, $9);
}
Expand Down
12 changes: 4 additions & 8 deletions lib/src/parser/predicate/single_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class SingleCharacterParser extends CharacterParser {
: super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -40,8 +39,7 @@ class SingleCharacterParser extends CharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
int fastParseOn(String buffer, int position) =>
position < buffer.length && predicate.test(buffer.codeUnitAt(position))
? position + 1
Expand All @@ -60,8 +58,7 @@ class AnySingleCharacterParser extends SingleCharacterParser {
super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -71,8 +68,7 @@ class AnySingleCharacterParser extends SingleCharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
int fastParseOn(String buffer, int position) =>
position < buffer.length ? position + 1 : -1;
}
21 changes: 7 additions & 14 deletions lib/src/parser/predicate/unicode_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class UnicodeCharacterParser extends CharacterParser {
: super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -53,8 +52,7 @@ class UnicodeCharacterParser extends CharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
int fastParseOn(String buffer, int position) {
if (position < buffer.length) {
var codeUnit = buffer.codeUnitAt(position++);
Expand Down Expand Up @@ -85,8 +83,7 @@ class AnyUnicodeCharacterParser extends UnicodeCharacterParser {
super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -104,8 +101,7 @@ class AnyUnicodeCharacterParser extends UnicodeCharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
@noBoundsChecks
int fastParseOn(String buffer, int position) {
if (position < buffer.length) {
if (_isLeadSurrogate(buffer.codeUnitAt(position++)) &&
Expand All @@ -123,17 +119,14 @@ class AnyUnicodeCharacterParser extends UnicodeCharacterParser {
// https://github.com/dart-lang/sdk/blob/1207250b0d5687f9016cf115068addf6593dba58/sdk/lib/core/string.dart#L932-L955

// Tests if the code is a UTF-16 lead surrogate.
@inlineVm
@inlineJs
@inline
bool _isLeadSurrogate(int code) => (code & 0xFC00) == 0xD800;

// Tests if the code is a UTF-16 trail surrogate.
@inlineVm
@inlineJs
@inline
bool _isTrailSurrogate(int code) => (code & 0xFC00) == 0xDC00;

// Combines a lead and a trail surrogate value into a single code point.
@inlineVm
@inlineJs
@inline
int _combineSurrogatePair(int start, int end) =>
0x10000 + ((start & 0x3FF) << 10) + (end & 0x3FF);
9 changes: 7 additions & 2 deletions lib/src/shared/annotations.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/// Tests if the code is targeting JavaScript.
const isJavaScript = identical(1, 1.0);

// Request a function to be inlined.
const inlineVm = pragma('vm:prefer-inline');
const inline = isJavaScript ? inlineJs : inlineVm;
const inlineJs = pragma('dart2js:tryInline');
const inlineVm = pragma('vm:prefer-inline');

// Removes all array bounds checks.
const noBoundsChecksVm = pragma('vm:unsafe:no-bounds-checks');
const noBoundsChecks = isJavaScript ? noBoundsChecksJs : noBoundsChecksVm;
const noBoundsChecksJs = pragma('dart2js:index-bounds:trust');
const noBoundsChecksVm = pragma('vm:unsafe:no-bounds-checks');

0 comments on commit 092f4e6

Please sign in to comment.