Skip to content

Commit

Permalink
Account for typedef nullability (#257)
Browse files Browse the repository at this point in the history
Account for typedef nullability

_desugarTypedef accepts a _RawType and returns its
underlying type if it's a typedef. However, it doesn't
account for whether the _RawType is nullable or not.
In order to accurately desugar a use of a typedef, we
should union the _RawType's nullability with the underlying
type's nullability. For example, `SomeTypedef?` should
always be nullable after being desugared.
  • Loading branch information
srujzs authored Jun 18, 2024
1 parent f6a7d38 commit 6b8a465
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
to avoid users accidentally downcasting `num`, which has different semantics
depending on whether you compile to JS or Wasm. See issue [#57][] for more
details.
- Fix an issue where some union types didn't account for typedef nullability.

[#57]: https://github.com/dart-lang/web/issues/57

Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/webgl1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ extension type WebGLRenderingContext._(JSObject _) implements JSObject {
/// buffer object's data store.
external void bufferData(
GLenum target,
JSAny dataOrSize,
JSAny? dataOrSize,
GLenum usage,
);

Expand Down
12 changes: 6 additions & 6 deletions lib/src/dom/webgl2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
GLint border,
GLenum format,
GLenum type,
JSAny pboOffsetOrSourceOrSrcData, [
JSAny? pboOffsetOrSourceOrSrcData, [
int srcOffset,
]);

Expand All @@ -1287,7 +1287,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
GLsizei depth,
GLenum format,
GLenum type,
JSAny pboOffsetOrSourceOrSrcData, [
JSAny? pboOffsetOrSourceOrSrcData, [
int srcOffset,
]);

Expand Down Expand Up @@ -1946,7 +1946,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
/// creates and initializes the buffer object's data store.
external void bufferData(
GLenum target,
JSAny sizeOrSrcData,
JSAny? sizeOrSrcData,
GLenum usage, [
int srcOffset,
GLuint length,
Expand All @@ -1972,7 +1972,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
JSAny borderOrSource, [
GLenum format,
GLenum type,
JSAny pboOffsetOrPixelsOrSourceOrSrcData,
JSAny? pboOffsetOrPixelsOrSourceOrSrcData,
int srcOffset,
]);
external void texSubImage2D(
Expand All @@ -1984,7 +1984,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
JSAny heightOrType,
JSAny formatOrSource, [
GLenum type,
JSAny pboOffsetOrPixelsOrSourceOrSrcData,
JSAny? pboOffsetOrPixelsOrSourceOrSrcData,
int srcOffset,
]);
external void compressedTexImage2D(
Expand Down Expand Up @@ -2086,7 +2086,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
GLsizei height,
GLenum format,
GLenum type,
JSAny dstDataOrOffset, [
JSAny? dstDataOrOffset, [
int dstOffset,
]);
external JSObject get canvas;
Expand Down
11 changes: 7 additions & 4 deletions tool/generator/translator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,23 @@ class _Library {
}

/// If [rawType] corresponds to an IDL type that we declare as a typedef,
/// desugars the typedef.
/// desugars the typedef, accounting for nullability along the way.
///
/// Otherwise, returns null.
_RawType? _desugarTypedef(_RawType rawType) {
final decl = Translator.instance!._typeToDeclaration[rawType.type];
return switch (decl?.type) {
'typedef' => _getRawType((decl as idl.Typedef).idlType),
'typedef' => _getRawType((decl as idl.Typedef).idlType)
..nullable |= rawType.nullable,
// TODO(srujzs): If we ever add a generic JS function type, we should
// maybe leverage that here so we have stronger type-checking of
// callbacks.
'callback' || 'callback interface' => _RawType('JSFunction', false),
'callback' ||
'callback interface' =>
_RawType('JSFunction', rawType.nullable),
// TODO(srujzs): Enums in the WebIDL are just strings, but we could make
// them easier to work with on the Dart side.
'enum' => _RawType('JSString', false),
'enum' => _RawType('JSString', rawType.nullable),
_ => null
};
}
Expand Down

0 comments on commit 6b8a465

Please sign in to comment.