Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up macro examples and README.md, remove run.dart script #3582

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions working/macros/example/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
**DISCLAIMER**: All code in this package is experimental and should be treated
as such.

This package some example macros (under `lib`), as well as some utilities to try
actually running those examples.
as such. The examples are unstable and may or may not work at any given time,
depending on the implementation status.

## Setup

Your SDK will need to match roughly the commit pinned in the pubspec.yaml file
of this package (see the `ref` lines). Otherwise you will get a kernel version
mismatch.

## Examples

The example macros live under `lib/` and there are programs using them under
`bin/`. To try and run an example, you need to enable the `macros` experiment,
`dart --enable-experiment=macros <script>`, but the implementations do not yet
support all the examples here so you should expect errors.

## Benchmarks

There is a basic benchmark at `benchmark/simple.dart`. You can run this tool
Expand All @@ -19,12 +24,3 @@ environment (compiler).

This benchmark uses a synthetic program, and only benchmarks the overhead of
running the macro itself, and the communication to and from the host program.

## Examples

There is an example program at `bin/user_main.dart`. This _cannot_ be directly
executed but you can compile and execute it with the `bin/run.dart` script.

**NOTE**: This is not meant to be a representative example of how a script using
macros would be compiled and ran in the real world, but it does allow you to
execute the program.
1 change: 0 additions & 1 deletion working/macros/example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ analyzer:
# TODO: remove these when the analyzer supports macros enough to run them
- bin/checks_main.dart
- bin/injectable_main.dart
- bin/user_main.dart
- bin/json_serializable_main.dart
37 changes: 37 additions & 0 deletions working/macros/example/bin/auto_dispose_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:macro_proposal/auto_dispose.dart';
jakemac53 marked this conversation as resolved.
Show resolved Hide resolved

void main() {
var state = MyState(a: ADisposable(), b: BDisposable(), c: 'hello world');
state.dispose();
}

@AutoDispose()
class MyState extends State {
final ADisposable a;
final ADisposable? a2;
final BDisposable b;
final String c;

MyState({required this.a, this.a2, required this.b, required this.c});

@override
String toString() => 'MyState!';
}

class State {
void dispose() {
print('disposing of $this');
}
}

class ADisposable implements Disposable {
void dispose() {
print('disposing of ADisposable');
}
}

class BDisposable implements Disposable {
void dispose() {
print('disposing of BDisposable');
}
}
25 changes: 25 additions & 0 deletions working/macros/example/bin/data_class_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
jakemac53 marked this conversation as resolved.
Show resolved Hide resolved
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:macro_proposal/data_class.dart';

void main() {
var joe = User(age: 25, name: 'Joe', username: 'joe1234');
print(joe);

var phoenix = joe.copyWith(name: 'Phoenix', age: 23);
print(phoenix);
}

@DataClass()
class User {
final int age;
final String name;
final String username;
}

@DataClass()
class Manager extends User {
final List<User> reports;
}
152 changes: 0 additions & 152 deletions working/macros/example/bin/run.dart

This file was deleted.

112 changes: 0 additions & 112 deletions working/macros/example/bin/user_main.dart

This file was deleted.

6 changes: 5 additions & 1 deletion working/macros/example/lib/auto_dispose.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ macro class AutoDispose implements ClassDeclarationsMacro, ClassDefinitionMacro
}

builder.declareInType(DeclarationCode.fromParts([
// TODO: Remove external once the CFE supports it.
'external void dispose();',
]));
}
Expand Down Expand Up @@ -62,7 +63,10 @@ macro class AutoDispose implements ClassDeclarationsMacro, ClassDefinitionMacro
var disposeBuilder = await builder.buildMethod(disposeMethod.identifier);
disposeBuilder.augment(FunctionBodyCode.fromParts([
'{\n',
if (!disposeMethod.hasBody) 'super.dispose();' else 'augmented();',
if (disposeMethod.hasExternal || !disposeMethod.hasBody)
'super.dispose();'
else
'augmented();',
...disposeCalls,
'}',
]));
Expand Down
Loading