You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Force unwrapping in `class var constructor` may crash for classes that are unavailable in the current environment. For example, after swiftwasm/WebAPIKit#38 was merged, it led to crashes in `getCanvas` calls due to these optional casts relying on `class var constructor` always succeeding:
```swift
public static func construct(from value: JSValue) -> Self? {
if let canvasRenderingContext2D: CanvasRenderingContext2D = value.fromJSValue() {
return .canvasRenderingContext2D(canvasRenderingContext2D)
}
if let gpuCanvasContext: GPUCanvasContext = value.fromJSValue() {
return .gpuCanvasContext(gpuCanvasContext)
}
if let imageBitmapRenderingContext: ImageBitmapRenderingContext = value.fromJSValue() {
return .imageBitmapRenderingContext(imageBitmapRenderingContext)
}
if let webGL2RenderingContext: WebGL2RenderingContext = value.fromJSValue() {
return .webGL2RenderingContext(webGL2RenderingContext)
}
if let webGLRenderingContext: WebGLRenderingContext = value.fromJSValue() {
return .webGLRenderingContext(webGLRenderingContext)
}
return nil
}
```
`if let gpuCanvasContext: GPUCanvasContext = value.fromJSValue()` branch crashes on browsers that don't have `GPUCanvasContext` enabled.
As we currently don't have a better way to handle unavailable features, I propose making the result type of `static var constructor` requirement optional. This means you can still declare classes that are unavailable in the host JS environment. Conditional type casts are also available as they were, they will just always return `nil`, and initializers for these classes will return `nil` as well.
0 commit comments