-
Notifications
You must be signed in to change notification settings - Fork 32
[interop] Add Support for Function Declarations #392
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
Open
nikeokoronkwo
wants to merge
6
commits into
dart-lang:main
Choose a base branch
from
nikeokoronkwo:nikeokoronkwo/issue389
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8744e5e
Interop Gen: Support Function Declarations
nikeokoronkwo cdd09b2
code resolution
nikeokoronkwo ff152be
changed primitive types from `enum` to `static const` types
nikeokoronkwo 80f6e52
minor changes
nikeokoronkwo fee7ad2
formatting
nikeokoronkwo b9d111a
resolved code issues, added test cases, and refactored primitive type…
nikeokoronkwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// 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:code_builder/code_builder.dart'; | ||
|
||
import '../interop_gen/namer.dart'; | ||
import 'types.dart'; | ||
|
||
class GlobalOptions { | ||
static int variardicArgsCount = 4; | ||
static bool shouldEmitJsTypes = false; | ||
} | ||
|
||
class Options {} | ||
|
||
class DeclarationOptions extends Options { | ||
DeclarationOptions(); | ||
|
||
TypeOptions toTypeOptions({bool nullable = false}) => | ||
TypeOptions(nullable: nullable); | ||
} | ||
|
||
class TypeOptions extends Options { | ||
bool nullable; | ||
|
||
TypeOptions({this.nullable = false}); | ||
} | ||
|
||
class ASTOptions { | ||
bool parameter; | ||
bool emitJSTypes; | ||
int variardicArgsCount; | ||
|
||
ASTOptions( | ||
{this.parameter = false, | ||
this.variardicArgsCount = 4, | ||
this.emitJSTypes = false}); | ||
} | ||
|
||
sealed class Node { | ||
abstract final String? name; | ||
abstract final ID id; | ||
String? get dartName; | ||
|
||
Spec emit([Options? options]); | ||
|
||
Node(); | ||
} | ||
|
||
abstract class Declaration extends Node { | ||
@override | ||
abstract final String name; | ||
|
||
@override | ||
Spec emit([covariant DeclarationOptions? options]); | ||
} | ||
|
||
abstract class NamedDeclaration extends Declaration { | ||
ReferredType asReferredType([List<Type>? typeArgs]) => | ||
ReferredType(name: name, declaration: this, typeParams: typeArgs ?? []); | ||
} | ||
|
||
abstract interface class ExportableDeclaration extends Declaration { | ||
/// Whether this declaration is exported. | ||
bool get exported; | ||
} | ||
|
||
abstract class Type extends Node { | ||
@override | ||
String? dartName; | ||
|
||
@override | ||
Reference emit([covariant TypeOptions? options]); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// 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. | ||
|
||
// ignore_for_file: non_constant_identifier_names | ||
nikeokoronkwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import 'package:code_builder/code_builder.dart'; | ||
|
||
import '../interop_gen/namer.dart'; | ||
import 'base.dart'; | ||
|
||
/// A built in type supported by `dart:js_interop` or by this library | ||
/// (with generated declarations) | ||
class BuiltinType extends Type { | ||
@override | ||
final String name; | ||
|
||
final List<Type> typeParams; | ||
|
||
/// Whether the given type is present in "dart:js_interop" | ||
final bool fromDartJSInterop; | ||
|
||
// TODO(nikeokoronkwo): Types in general should have an `isNullable` | ||
// property on them to indicate nullability for Dart generated code. | ||
final bool? isNullable; | ||
nikeokoronkwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
BuiltinType( | ||
{required this.name, | ||
this.typeParams = const [], | ||
this.fromDartJSInterop = false, | ||
this.isNullable}); | ||
|
||
@override | ||
ID get id => ID(type: 'type', name: name); | ||
|
||
@override | ||
String? get dartName => null; | ||
|
||
@override | ||
Reference emit([TypeOptions? options]) { | ||
options ??= TypeOptions(); | ||
|
||
return TypeReference((t) => t | ||
..symbol = name | ||
..types.addAll(typeParams | ||
// if there is only one type param, and it is void, ignore | ||
.where((p) => typeParams.length != 1 || p != $voidType) | ||
.map((p) => p.emit(TypeOptions()))) | ||
..url = fromDartJSInterop ? 'dart:js_interop' : null | ||
..isNullable = isNullable ?? options!.nullable); | ||
} | ||
|
||
static final BuiltinType $voidType = BuiltinType(name: 'void'); | ||
static final BuiltinType anyType = | ||
nikeokoronkwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BuiltinType(name: 'JSAny', fromDartJSInterop: true, isNullable: true); | ||
|
||
static BuiltinType primitiveType(PrimitiveType typeIdentifier, | ||
{bool? shouldEmitJsType, | ||
bool? isNullable, | ||
List<Type> typeParams = const []}) { | ||
shouldEmitJsType ??= GlobalOptions.shouldEmitJsTypes; | ||
return switch (typeIdentifier) { | ||
PrimitiveType.int => shouldEmitJsType | ||
? BuiltinType( | ||
name: 'JSNumber', fromDartJSInterop: true, isNullable: isNullable) | ||
: BuiltinType(name: 'int', isNullable: isNullable), | ||
PrimitiveType.num => shouldEmitJsType | ||
? BuiltinType( | ||
name: 'JSNumber', fromDartJSInterop: true, isNullable: isNullable) | ||
: BuiltinType(name: 'num', isNullable: isNullable), | ||
PrimitiveType.double => shouldEmitJsType | ||
? BuiltinType( | ||
name: 'JSNumber', fromDartJSInterop: true, isNullable: isNullable) | ||
: BuiltinType(name: 'double', isNullable: isNullable), | ||
PrimitiveType.boolean => shouldEmitJsType | ||
? BuiltinType( | ||
name: 'JSBoolean', | ||
fromDartJSInterop: true, | ||
isNullable: isNullable) | ||
: BuiltinType(name: 'bool', isNullable: isNullable), | ||
PrimitiveType.string => shouldEmitJsType | ||
? BuiltinType( | ||
name: 'JSString', fromDartJSInterop: true, isNullable: isNullable) | ||
: BuiltinType(name: 'String', isNullable: isNullable), | ||
PrimitiveType.$void => $voidType, | ||
PrimitiveType.any => anyType, | ||
PrimitiveType.object => BuiltinType( | ||
name: 'JSObject', fromDartJSInterop: true, isNullable: isNullable), | ||
PrimitiveType.unknown => | ||
BuiltinType(name: 'JSAny', fromDartJSInterop: true, isNullable: true), | ||
PrimitiveType.undefined => | ||
BuiltinType(name: 'JSAny', fromDartJSInterop: true, isNullable: true), | ||
PrimitiveType.array => BuiltinType( | ||
name: 'JSArray', | ||
typeParams: [typeParams.single], | ||
fromDartJSInterop: true, | ||
isNullable: isNullable), | ||
PrimitiveType.promise => BuiltinType( | ||
name: 'JSPromise', | ||
typeParams: [typeParams.single], | ||
fromDartJSInterop: true, | ||
isNullable: isNullable), | ||
PrimitiveType.function => BuiltinType( | ||
name: 'JSFunction', fromDartJSInterop: true, isNullable: isNullable), | ||
}; | ||
} | ||
} | ||
|
||
enum PrimitiveType { | ||
int, | ||
num, | ||
double, | ||
boolean, | ||
string, | ||
$void, | ||
any, | ||
object, | ||
unknown, | ||
undefined, | ||
array, | ||
promise, | ||
function | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.