Skip to content

Commit

Permalink
Merge pull request 'fibers' (dart-lang#21) from fibers into main
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbashir committed Nov 11, 2024
2 parents 41018d5 + aeb1a7d commit 6735573
Show file tree
Hide file tree
Showing 46 changed files with 831 additions and 813 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ logs/results.json
doc/api/

build/
build-sdk-debug.sh
build-sdk.sh
build-fiber-test.sh
*.exe
*.aot
*.S
Expand Down
5 changes: 5 additions & 0 deletions build-fiber-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -e

dart --enable_mirrors=true compile aot-snapshot runtime/tests/vm/dart/fiber/fiber_test.dart
3 changes: 3 additions & 0 deletions build-sdk-debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
export CPATH=""
./tools/build.py -m debug -a x64 runtime dart_precompiled_runtime ddc dartanalyzer analysis_server create_common_sdk create_platform_sdk
3 changes: 3 additions & 0 deletions build-sdk-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
export CPATH=""
./tools/build.py -m release -a x64 runtime dart_precompiled_runtime ddc dartanalyzer analysis_server create_common_sdk create_platform_sdk
401 changes: 133 additions & 268 deletions pkg/analysis_server/lib/src/computer/computer_highlights.dart

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions pkg/analysis_server/lib/src/lsp/semantic_tokens/mapping.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ final highlightRegionTokenTypes = {
HighlightRegionType.IMPORT_PREFIX: SemanticTokenTypes.variable,
HighlightRegionType.INSTANCE_FIELD_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.INSTANCE_FIELD_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.INSTANCE_GETTER_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.INSTANCE_GETTER_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.INSTANCE_GETTER_DECLARATION: SemanticTokenTypes.function,
HighlightRegionType.INSTANCE_GETTER_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.INSTANCE_METHOD_DECLARATION: SemanticTokenTypes.method,
HighlightRegionType.INSTANCE_METHOD_REFERENCE: SemanticTokenTypes.method,
HighlightRegionType.INSTANCE_METHOD_TEAR_OFF: SemanticTokenTypes.method,
HighlightRegionType.INSTANCE_SETTER_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.INSTANCE_SETTER_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.INSTANCE_SETTER_DECLARATION: SemanticTokenTypes.function,
HighlightRegionType.INSTANCE_SETTER_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.KEYWORD: SemanticTokenTypes.keyword,
HighlightRegionType.LIBRARY_NAME: SemanticTokenTypes.namespace,
HighlightRegionType.LITERAL_BOOLEAN: CustomSemanticTokenTypes.boolean,
Expand All @@ -152,21 +152,21 @@ final highlightRegionTokenTypes = {
HighlightRegionType.PARAMETER_DECLARATION: SemanticTokenTypes.parameter,
HighlightRegionType.PARAMETER_REFERENCE: SemanticTokenTypes.parameter,
HighlightRegionType.STATIC_FIELD_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.STATIC_GETTER_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.STATIC_GETTER_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.STATIC_GETTER_DECLARATION: SemanticTokenTypes.function,
HighlightRegionType.STATIC_GETTER_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.STATIC_METHOD_DECLARATION: SemanticTokenTypes.method,
HighlightRegionType.STATIC_METHOD_REFERENCE: SemanticTokenTypes.method,
HighlightRegionType.STATIC_METHOD_TEAR_OFF: SemanticTokenTypes.method,
HighlightRegionType.STATIC_SETTER_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.STATIC_SETTER_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.STATIC_SETTER_DECLARATION: SemanticTokenTypes.function,
HighlightRegionType.STATIC_SETTER_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_FUNCTION_DECLARATION:
SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_FUNCTION_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_FUNCTION_TEAR_OFF: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_GETTER_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.TOP_LEVEL_GETTER_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.TOP_LEVEL_SETTER_DECLARATION: SemanticTokenTypes.property,
HighlightRegionType.TOP_LEVEL_SETTER_REFERENCE: SemanticTokenTypes.property,
HighlightRegionType.TOP_LEVEL_GETTER_DECLARATION: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_GETTER_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_SETTER_DECLARATION: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_SETTER_REFERENCE: SemanticTokenTypes.function,
HighlightRegionType.TOP_LEVEL_VARIABLE: SemanticTokenTypes.property,
HighlightRegionType.TOP_LEVEL_VARIABLE_DECLARATION:
SemanticTokenTypes.property,
Expand Down
1 change: 1 addition & 0 deletions runtime/lib/coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ DEFINE_NATIVE_ENTRY(Coroutine_factory, 0, 3) {
return Coroutine::New(size.Value(), trampoline.function());
}
} // namespace dart

1 change: 1 addition & 0 deletions runtime/platform/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <functional>
#include <sys/types.h>
#include <cassert> // For assert() in constant expressions.

Expand Down
4 changes: 2 additions & 2 deletions runtime/tests/vm/dart/fiber/fiber_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var fibers = 100;
var latency = 0.0;

void main() {
Fiber.launch(benchmark, terminate: true);
Fiber.launch(benchmark);
}

void benchmark() {
final jobs = <Fiber>[];
for (var i = 0; i < fibers; i++) {
Fiber.schedule(Fiber.current());
Fiber.schedule(Fiber.current);
jobs.add(Fiber.spawn(scheduling));
}
for (var i = 0; i < fibers; i++) {
Expand Down
86 changes: 86 additions & 0 deletions runtime/tests/vm/dart/fiber/fiber_captures_suite.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:fiber';
import 'dart:async';
import 'package:expect/expect.dart';

final tests = [
testGlobalState,
testClosureState,
];

var globalStateValue = "";
void testGlobalState() {
void child() {
globalStateValue += "child -> ";
Fiber.reschedule();
globalStateValue += "child";
}

void main() {
globalStateValue = "";
globalStateValue += "main -> ";
Fiber.schedule(Fiber.current);
Fiber.spawn(child);
globalStateValue += "main -> ";
Fiber.reschedule();
Expect.equals("main -> child -> main -> child", globalStateValue);
}

Fiber.launch(main);
}

void testClosureState() {
var localState = "localState";
Fiber.launch(
() {
Expect.equals("localState", localState);
localState = "after fiber";
},
);
Expect.equals("after fiber", localState);

localState = "localState";
Fiber.launch(
() {
Expect.equals("localState", localState);
localState = "after main fiber";
Fiber.schedule(Fiber.current);
Fiber.spawn(
() {
Expect.equals("after main fiber", localState);
localState = "after child fiber";
Fiber.reschedule();
Expect.equals("after child fiber after main fiber", localState);
localState = "finish";
},
name: "child",
);
Expect.equals("after child fiber", localState);
localState = "after child fiber after main fiber";
Fiber.suspend();
},
);
Expect.equals("finish", localState);

localState = "level 1";
Fiber.launch(
() {
Expect.equals("level 1", localState);
localState = "level 2";
Fiber.spawn(
() {
Expect.equals("level 2", localState);
localState = "level 3";
Fiber.spawn(
() {
Expect.equals("level 3", localState);
localState = "level 4";
},
name: "child",
);
},
name: "child",
);
},
);
Expect.equals("level 4", localState);
}
162 changes: 88 additions & 74 deletions runtime/tests/vm/dart/fiber/fiber_exceptions_suite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,97 @@ import 'dart:fiber';
import 'dart:async';
import 'package:expect/expect.dart';

void main() {
_run(mainException: true);
_run(childException: true);
_run(childException: true, mainCatchChild: true);
_run(childYield: true);
_run(childYield: true, mainCatchChild: true);
}
final tests = [
testIdleException,
testMainException,
testChildException,
testChildSuspend,
testMainSuspend,
];

class FiberException implements Exception {
class _FiberException implements Exception {
final String message;
const FiberException(this.message);
const _FiberException(this.message);
}

void _run({mainException = false, childException = false, mainCatchChild = false, childYield = false, mainYield = false}) {
print("_run: mainException = $mainException, childException = $childException, mainCatchChild = $mainCatchChild, childYield = $childYield, mainYield = $mainYield");
if (mainYield) {
final child = Fiber.child(() {
Fiber.suspend();
throw FiberException("child");
}, name: "child");
if (mainCatchChild) {
final main = Fiber.main(
() => Expect.equals(
Expect.throws<FiberException>(
() {
Fiber.fork(child);
Fiber.suspend();
},
).message,
"child",
),
);
main.start();
return;
}
final main = Fiber.main(() {
Fiber.fork(child);
Fiber.suspend();
});
Expect.equals(Expect.throws<FiberException>(main.start).message, "child");
return;
}
if (childYield) {
final child = Fiber.child(() => Fiber.suspend(), name: "child");
if (mainCatchChild) {
final main = Fiber.main(
() => Expect.equals(
Expect.throws<FiberException>(() {
Fiber.fork(child);
throw FiberException("main");
}).message,
"main",
),
void testIdleException() {
Expect.throws<StateError>(
() => Fiber.launch(() => Fiber.spawn(() => Fiber.reschedule()), idle: () => throw StateError("Empty idle")),
(error) => error.message == "Empty idle",
);
}

void testMainException() {
Expect.throws<_FiberException>(
() => Fiber.launch(() => throw _FiberException("main")),
(e) => e.message == "main",
);
}

void testChildException() {
Expect.throws<_FiberException>(
() => Fiber.launch(() => Fiber.spawn(() => throw _FiberException("child"))),
(e) => e.message == "child",
);

Fiber.launch(
() => Expect.throws<_FiberException>(
() => Fiber.spawn(() => throw _FiberException("child")),
(e) => e.message == "child",
),
);
}

void testChildSuspend() {
Expect.throws<_FiberException>(
() => Fiber.launch(
() {
Fiber.spawn(Fiber.reschedule);
throw _FiberException("main");
Fiber.reschedule();
},
),
(e) => e.message == "main",
);

Fiber.launch(
() => Expect.throws<_FiberException>(
() {
Fiber.spawn(Fiber.reschedule);
throw _FiberException("main");
Fiber.reschedule();
},
(e) => e.message == "main",
),
);
}

void testMainSuspend() {
Expect.throws<_FiberException>(
() {
Fiber.launch(() {
Fiber.spawn(() {
Fiber.reschedule();
throw _FiberException("child");
});
Fiber.reschedule();
});
},
(e) => e.message == "child",
);

Fiber.launch(
() {
Expect.throws<_FiberException>(
() {
Fiber.spawn(() {
Fiber.reschedule();
throw _FiberException("child");
});
Fiber.reschedule();
},
(e) => e.message == "child",
);
main.start();
return;
}
final main = Fiber.main(() {
Fiber.fork(child);
throw FiberException("main");
});
Expect.equals(Expect.throws<FiberException>(main.start).message, "main");
return;
}
if (childException) {
final child = Fiber.child(() => throw FiberException("child"), name: "child");
if (mainCatchChild) {
final main = Fiber.main(() => Expect.equals(Expect.throws<FiberException>(() => Fiber.fork(child)).message, "child"));
main.start();
return;
}
final main = Fiber.main(() => Fiber.fork(child));
Expect.equals(Expect.throws<FiberException>(main.start).message, "child");
return;
}
if (mainException) {
final main = Fiber.main(() => throw FiberException("main"));
Expect.equals(Expect.throws<FiberException>(main.start).message, "main");
}
},
);
}
Loading

0 comments on commit 6735573

Please sign in to comment.