Skip to content

Commit 6b8a465

Browse files
authored
Account for typedef nullability (#257)
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.
1 parent f6a7d38 commit 6b8a465

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
to avoid users accidentally downcasting `num`, which has different semantics
2020
depending on whether you compile to JS or Wasm. See issue [#57][] for more
2121
details.
22+
- Fix an issue where some union types didn't account for typedef nullability.
2223

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

lib/src/dom/webgl1.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ extension type WebGLRenderingContext._(JSObject _) implements JSObject {
16301630
/// buffer object's data store.
16311631
external void bufferData(
16321632
GLenum target,
1633-
JSAny dataOrSize,
1633+
JSAny? dataOrSize,
16341634
GLenum usage,
16351635
);
16361636

lib/src/dom/webgl2.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
12681268
GLint border,
12691269
GLenum format,
12701270
GLenum type,
1271-
JSAny pboOffsetOrSourceOrSrcData, [
1271+
JSAny? pboOffsetOrSourceOrSrcData, [
12721272
int srcOffset,
12731273
]);
12741274

@@ -1287,7 +1287,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
12871287
GLsizei depth,
12881288
GLenum format,
12891289
GLenum type,
1290-
JSAny pboOffsetOrSourceOrSrcData, [
1290+
JSAny? pboOffsetOrSourceOrSrcData, [
12911291
int srcOffset,
12921292
]);
12931293

@@ -1946,7 +1946,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
19461946
/// creates and initializes the buffer object's data store.
19471947
external void bufferData(
19481948
GLenum target,
1949-
JSAny sizeOrSrcData,
1949+
JSAny? sizeOrSrcData,
19501950
GLenum usage, [
19511951
int srcOffset,
19521952
GLuint length,
@@ -1972,7 +1972,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
19721972
JSAny borderOrSource, [
19731973
GLenum format,
19741974
GLenum type,
1975-
JSAny pboOffsetOrPixelsOrSourceOrSrcData,
1975+
JSAny? pboOffsetOrPixelsOrSourceOrSrcData,
19761976
int srcOffset,
19771977
]);
19781978
external void texSubImage2D(
@@ -1984,7 +1984,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
19841984
JSAny heightOrType,
19851985
JSAny formatOrSource, [
19861986
GLenum type,
1987-
JSAny pboOffsetOrPixelsOrSourceOrSrcData,
1987+
JSAny? pboOffsetOrPixelsOrSourceOrSrcData,
19881988
int srcOffset,
19891989
]);
19901990
external void compressedTexImage2D(
@@ -2086,7 +2086,7 @@ extension type WebGL2RenderingContext._(JSObject _) implements JSObject {
20862086
GLsizei height,
20872087
GLenum format,
20882088
GLenum type,
2089-
JSAny dstDataOrOffset, [
2089+
JSAny? dstDataOrOffset, [
20902090
int dstOffset,
20912091
]);
20922092
external JSObject get canvas;

tool/generator/translator.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,23 @@ class _Library {
104104
}
105105

106106
/// If [rawType] corresponds to an IDL type that we declare as a typedef,
107-
/// desugars the typedef.
107+
/// desugars the typedef, accounting for nullability along the way.
108108
///
109109
/// Otherwise, returns null.
110110
_RawType? _desugarTypedef(_RawType rawType) {
111111
final decl = Translator.instance!._typeToDeclaration[rawType.type];
112112
return switch (decl?.type) {
113-
'typedef' => _getRawType((decl as idl.Typedef).idlType),
113+
'typedef' => _getRawType((decl as idl.Typedef).idlType)
114+
..nullable |= rawType.nullable,
114115
// TODO(srujzs): If we ever add a generic JS function type, we should
115116
// maybe leverage that here so we have stronger type-checking of
116117
// callbacks.
117-
'callback' || 'callback interface' => _RawType('JSFunction', false),
118+
'callback' ||
119+
'callback interface' =>
120+
_RawType('JSFunction', rawType.nullable),
118121
// TODO(srujzs): Enums in the WebIDL are just strings, but we could make
119122
// them easier to work with on the Dart side.
120-
'enum' => _RawType('JSString', false),
123+
'enum' => _RawType('JSString', rawType.nullable),
121124
_ => null
122125
};
123126
}

0 commit comments

Comments
 (0)