[dart:js_interop] Remove unnecessary casts in patch files

In theory, such casts should be optimized out, but in some
cases, they might not be (DDC or lower optimization flags).
Instead, we should use the primary constructor and
representation field to go between the representation type
and the JS type. Similarly, we do unnecessary casts in
dart2wasm to convert JSValue to the right JS type. We can
avoid those by directly calling the primary constructor.

Change-Id: I8fdac18f6d634b379e70951fe5d3a1ed7ad4bf15
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371423
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Srujan Gaddam <srujzs@google.com>
diff --git a/sdk/lib/_internal/js_shared/lib/js_interop_patch.dart b/sdk/lib/_internal/js_shared/lib/js_interop_patch.dart
index 223dd58..e9f02671 100644
--- a/sdk/lib/_internal/js_shared/lib/js_interop_patch.dart
+++ b/sdk/lib/_internal/js_shared/lib/js_interop_patch.dart
@@ -58,7 +58,7 @@
 extension NullableObjectUtilExtension on Object? {
   @patch
   @pragma('dart2js:prefer-inline')
-  JSAny? jsify() => js_util.jsify(this) as JSAny?;
+  JSAny? jsify() => js_util.jsify(this);
 }
 
 /// [JSExportedDartFunction] <-> [Function]
@@ -68,7 +68,7 @@
   // to be called in Dart.
   @patch
   @pragma('dart2js:prefer-inline')
-  Function get toDart => this as Function;
+  Function get toDart => this._jsFunction;
 }
 
 @patch
@@ -93,12 +93,12 @@
   @patch
   @pragma('dart2js:prefer-inline')
   Object get toDart {
-    final val = js_util.getProperty(this, _jsBoxedDartObjectProperty);
+    final val = js_util.getProperty<Object?>(this, _jsBoxedDartObjectProperty);
     if (val == null) {
       throw 'Expected a wrapped Dart object, but got a JS object or a wrapped '
           'Dart object from a separate runtime instead.';
     }
-    return val as Object;
+    return val;
   }
 }
 
@@ -114,7 +114,7 @@
     // Use JS foreign function to avoid assertInterop check when `this` is a
     // `Function` for `setProperty`.
     foreign_helper.JS('', '#[#]=#', box, _jsBoxedDartObjectProperty, this);
-    return box as JSBoxedDartObject;
+    return JSBoxedDartObject._(box);
   }
 }
 
@@ -131,7 +131,7 @@
   @patch
   @pragma('dart2js:prefer-inline')
   ExternalDartReference get toExternalReference =>
-      this as ExternalDartReference;
+      ExternalDartReference._(this);
 }
 
 /// [JSPromise] -> [Future].
@@ -147,7 +147,7 @@
 extension JSArrayBufferToByteBuffer on JSArrayBuffer {
   @patch
   @pragma('dart2js:prefer-inline')
-  ByteBuffer get toDart => this as ByteBuffer;
+  ByteBuffer get toDart => this._jsArrayBuffer;
 }
 
 @patch
@@ -162,7 +162,7 @@
 extension JSDataViewToByteData on JSDataView {
   @patch
   @pragma('dart2js:prefer-inline')
-  ByteData get toDart => this as ByteData;
+  ByteData get toDart => this._jsDataView;
 }
 
 @patch
@@ -177,7 +177,7 @@
 extension JSInt8ArrayToInt8List on JSInt8Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Int8List get toDart => this as Int8List;
+  Int8List get toDart => this._jsInt8Array;
 }
 
 @patch
@@ -192,7 +192,7 @@
 extension JSUint8ArrayToUint8List on JSUint8Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Uint8List get toDart => this as Uint8List;
+  Uint8List get toDart => this._jsUint8Array;
 }
 
 @patch
@@ -207,7 +207,7 @@
 extension JSUint8ClampedArrayToUint8ClampedList on JSUint8ClampedArray {
   @patch
   @pragma('dart2js:prefer-inline')
-  Uint8ClampedList get toDart => this as Uint8ClampedList;
+  Uint8ClampedList get toDart => this._jsUint8ClampedArray;
 }
 
 @patch
@@ -222,7 +222,7 @@
 extension JSInt16ArrayToInt16List on JSInt16Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Int16List get toDart => this as Int16List;
+  Int16List get toDart => this._jsInt16Array;
 }
 
 @patch
@@ -237,7 +237,7 @@
 extension JSUint16ArrayToInt16List on JSUint16Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Uint16List get toDart => this as Uint16List;
+  Uint16List get toDart => this._jsUint16Array;
 }
 
 @patch
@@ -252,7 +252,7 @@
 extension JSInt32ArrayToInt32List on JSInt32Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Int32List get toDart => this as Int32List;
+  Int32List get toDart => this._jsInt32Array;
 }
 
 @patch
@@ -267,7 +267,7 @@
 extension JSUint32ArrayToUint32List on JSUint32Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Uint32List get toDart => this as Uint32List;
+  Uint32List get toDart => this._jsUint32Array;
 }
 
 @patch
@@ -282,7 +282,7 @@
 extension JSFloat32ArrayToFloat32List on JSFloat32Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Float32List get toDart => this as Float32List;
+  Float32List get toDart => this._jsFloat32Array;
 }
 
 @patch
@@ -297,7 +297,7 @@
 extension JSFloat64ArrayToFloat64List on JSFloat64Array {
   @patch
   @pragma('dart2js:prefer-inline')
-  Float64List get toDart => this as Float64List;
+  Float64List get toDart => this._jsFloat64Array;
 }
 
 @patch
@@ -344,7 +344,7 @@
 extension JSNumberToNumber on JSNumber {
   @patch
   @pragma('dart2js:prefer-inline')
-  double get toDartDouble => this as double;
+  double get toDartDouble => this._jsNumber;
 
   @patch
   @pragma('dart2js:prefer-inline')
@@ -356,7 +356,7 @@
 extension DoubleToJSNumber on double {
   @patch
   @pragma('dart2js:prefer-inline')
-  JSNumber get toJS => this as JSNumber;
+  JSNumber get toJS => JSNumber._(this);
 }
 
 /// [JSBoolean] <-> [bool]
@@ -364,14 +364,14 @@
 extension JSBooleanToBool on JSBoolean {
   @patch
   @pragma('dart2js:prefer-inline')
-  bool get toDart => this as bool;
+  bool get toDart => this._jsBoolean;
 }
 
 @patch
 extension BoolToJSBoolean on bool {
   @patch
   @pragma('dart2js:prefer-inline')
-  JSBoolean get toJS => this as JSBoolean;
+  JSBoolean get toJS => JSBoolean._(this);
 }
 
 /// [JSString] <-> [String]
@@ -379,14 +379,14 @@
 extension JSStringToString on JSString {
   @patch
   @pragma('dart2js:prefer-inline')
-  String get toDart => this as String;
+  String get toDart => this._jsString;
 }
 
 @patch
 extension StringToJSString on String {
   @patch
   @pragma('dart2js:prefer-inline')
-  JSString get toJS => this as JSString;
+  JSString get toJS => JSString._(this);
 }
 
 @patch
@@ -418,39 +418,39 @@
   @patch
   @pragma('dart2js:prefer-inline')
   JSBoolean greaterThan(JSAny? any) =>
-      js_util.greaterThan(this, any) as JSBoolean;
+      JSBoolean._(js_util.greaterThan(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
   JSBoolean greaterThanOrEqualTo(JSAny? any) =>
-      js_util.greaterThanOrEqual(this, any) as JSBoolean;
+      JSBoolean._(js_util.greaterThanOrEqual(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
-  JSBoolean lessThan(JSAny? any) => js_util.lessThan(this, any) as JSBoolean;
+  JSBoolean lessThan(JSAny? any) => JSBoolean._(js_util.lessThan(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
   JSBoolean lessThanOrEqualTo(JSAny? any) =>
-      js_util.lessThanOrEqual(this, any) as JSBoolean;
+      JSBoolean._(js_util.lessThanOrEqual(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
-  JSBoolean equals(JSAny? any) => js_util.equal(this, any) as JSBoolean;
+  JSBoolean equals(JSAny? any) => JSBoolean._(js_util.equal(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
-  JSBoolean notEquals(JSAny? any) => js_util.notEqual(this, any) as JSBoolean;
+  JSBoolean notEquals(JSAny? any) => JSBoolean._(js_util.notEqual(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
   JSBoolean strictEquals(JSAny? any) =>
-      js_util.strictEqual(this, any) as JSBoolean;
+      JSBoolean._(js_util.strictEqual(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
   JSBoolean strictNotEquals(JSAny? any) =>
-      js_util.strictNotEqual(this, any) as JSBoolean;
+      JSBoolean._(js_util.strictNotEqual(this, any));
 
   @patch
   @pragma('dart2js:prefer-inline')
diff --git a/sdk/lib/_internal/wasm/lib/js_interop_patch.dart b/sdk/lib/_internal/wasm/lib/js_interop_patch.dart
index 8e94360..14db7dd 100644
--- a/sdk/lib/_internal/wasm/lib/js_interop_patch.dart
+++ b/sdk/lib/_internal/wasm/lib/js_interop_patch.dart
@@ -13,16 +13,9 @@
 import 'dart:js_util' as js_util;
 import 'dart:typed_data';
 
-/// Some helpers for working with JS types internally. If we implement the JS
-/// types as inline classes then these should go away. We avoid doing a
-/// null-check check if we know the value is guaranteed to be non-nullable.
-/// TODO(joshualitt): Find a way to get rid of the explicit casts.
-T _boxNonNullable<T>(WasmExternRef? ref) => JSValue(ref) as T;
-T _boxNullable<T>(WasmExternRef? ref) => JSValue.box(ref) as T;
-
 @patch
 js_types.JSObjectRepType _createObjectLiteral() =>
-    _boxNonNullable<js_types.JSObjectRepType>(js_helper.newObjectRaw());
+    JSValue(js_helper.newObjectRaw());
 
 // This should match the global context we use in our static interop lowerings.
 @patch
@@ -74,7 +67,7 @@
 @patch
 extension NullableObjectUtilExtension on Object? {
   @patch
-  JSAny? jsify() => js_util.jsify(this) as JSAny?;
+  JSAny? jsify() => js_util.jsify(this);
 }
 
 /// [JSExportedDartFunction] <-> [Function]
@@ -104,8 +97,8 @@
 /// This is a Symbol so that different Dart applications don't share Dart
 /// objects from different Dart runtimes. We expect all [JSBoxedDartObject]s to
 /// have this Symbol.
-final JSSymbol _jsBoxedDartObjectProperty = _boxNonNullable<JSSymbol>(
-    js_helper.JS<WasmExternRef?>('() => Symbol("jsBoxedDartObjectProperty")'));
+final JSSymbol _jsBoxedDartObjectProperty = JSSymbol._(JSValue(
+    js_helper.JS<WasmExternRef?>('() => Symbol("jsBoxedDartObjectProperty")')));
 
 /// [JSBoxedDartObject] <-> [Object]
 @patch
@@ -132,7 +125,7 @@
     final box = JSObject();
     js_helper.JS<WasmExternRef?>('(o,s,v) => o[s] = v', box.toExternRef,
         _jsBoxedDartObjectProperty.toExternRef, jsObjectFromDartObject(this));
-    return box as JSBoxedDartObject;
+    return JSBoxedDartObject._(box._jsObject);
   }
 }
 
@@ -141,14 +134,14 @@
 extension ExternalDartReferenceToObject on ExternalDartReference {
   @patch
   Object get toDartObject =>
-      jsObjectToDartObject((this as JSValue).toExternRef);
+      jsObjectToDartObject(this._externalDartReference.toExternRef);
 }
 
 @patch
 extension ObjectToExternalDartReference on Object {
   @patch
   ExternalDartReference get toExternalReference =>
-      _boxNonNullable<ExternalDartReference>(jsObjectFromDartObject(this));
+      ExternalDartReference._(JSValue(jsObjectFromDartObject(this)));
 }
 
 /// [JSPromise] -> [Future].
@@ -202,9 +195,9 @@
   @patch
   JSArrayBuffer get toJS {
     final t = this;
-    return _boxNonNullable<JSArrayBuffer>(t is js_types.JSArrayBufferImpl
+    return JSArrayBuffer._(JSValue(t is js_types.JSArrayBufferImpl
         ? t.toExternRef
-        : jsArrayBufferFromDartByteBuffer(t));
+        : jsArrayBufferFromDartByteBuffer(t)));
   }
 }
 
@@ -220,9 +213,9 @@
   @patch
   JSDataView get toJS {
     final t = this;
-    return _boxNonNullable<JSDataView>(t is js_types.JSDataViewImpl
+    return JSDataView._(JSValue(t is js_types.JSDataViewImpl
         ? t.toExternRef
-        : jsDataViewFromDartByteData(t, lengthInBytes.toDouble()));
+        : jsDataViewFromDartByteData(t, lengthInBytes.toDouble())));
   }
 }
 
@@ -238,9 +231,9 @@
   @patch
   JSInt8Array get toJS {
     final t = this;
-    return _boxNonNullable<JSInt8Array>(t is js_types.JSInt8ArrayImpl
+    return JSInt8Array._(JSValue(t is js_types.JSInt8ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsInt8ArrayFromDartInt8List(t));
+        : jsInt8ArrayFromDartInt8List(t)));
   }
 }
 
@@ -256,9 +249,9 @@
   @patch
   JSUint8Array get toJS {
     final t = this;
-    return _boxNonNullable<JSUint8Array>(t is js_types.JSUint8ArrayImpl
+    return JSUint8Array._(JSValue(t is js_types.JSUint8ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsUint8ArrayFromDartUint8List(t));
+        : jsUint8ArrayFromDartUint8List(t)));
   }
 }
 
@@ -275,10 +268,9 @@
   @patch
   JSUint8ClampedArray get toJS {
     final t = this;
-    return _boxNonNullable<JSUint8ClampedArray>(
-        t is js_types.JSUint8ClampedArrayImpl
-            ? t.toJSArrayExternRef()
-            : jsUint8ClampedArrayFromDartUint8ClampedList(t));
+    return JSUint8ClampedArray._(JSValue(t is js_types.JSUint8ClampedArrayImpl
+        ? t.toJSArrayExternRef()
+        : jsUint8ClampedArrayFromDartUint8ClampedList(t)));
   }
 }
 
@@ -294,9 +286,9 @@
   @patch
   JSInt16Array get toJS {
     final t = this;
-    return _boxNonNullable<JSInt16Array>(t is js_types.JSInt16ArrayImpl
+    return JSInt16Array._(JSValue(t is js_types.JSInt16ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsInt16ArrayFromDartInt16List(t));
+        : jsInt16ArrayFromDartInt16List(t)));
   }
 }
 
@@ -312,9 +304,9 @@
   @patch
   JSUint16Array get toJS {
     final t = this;
-    return _boxNonNullable<JSUint16Array>(t is js_types.JSUint16ArrayImpl
+    return JSUint16Array._(JSValue(t is js_types.JSUint16ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsUint16ArrayFromDartUint16List(t));
+        : jsUint16ArrayFromDartUint16List(t)));
   }
 }
 
@@ -330,9 +322,9 @@
   @patch
   JSInt32Array get toJS {
     final t = this;
-    return _boxNonNullable<JSInt32Array>(t is js_types.JSInt32ArrayImpl
+    return JSInt32Array._(JSValue(t is js_types.JSInt32ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsInt32ArrayFromDartInt32List(t));
+        : jsInt32ArrayFromDartInt32List(t)));
   }
 }
 
@@ -348,9 +340,9 @@
   @patch
   JSUint32Array get toJS {
     final t = this;
-    return _boxNonNullable<JSUint32Array>(t is js_types.JSUint32ArrayImpl
+    return JSUint32Array._(JSValue(t is js_types.JSUint32ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsUint32ArrayFromDartUint32List(t));
+        : jsUint32ArrayFromDartUint32List(t)));
   }
 }
 
@@ -367,9 +359,9 @@
   @patch
   JSFloat32Array get toJS {
     final t = this;
-    return _boxNonNullable<JSFloat32Array>(t is js_types.JSFloat32ArrayImpl
+    return JSFloat32Array._(JSValue(t is js_types.JSFloat32ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsFloat32ArrayFromDartFloat32List(t));
+        : jsFloat32ArrayFromDartFloat32List(t)));
   }
 }
 
@@ -386,9 +378,9 @@
   @patch
   JSFloat64Array get toJS {
     final t = this;
-    return _boxNonNullable<JSFloat64Array>(t is js_types.JSFloat64ArrayImpl
+    return JSFloat64Array._(JSValue(t is js_types.JSFloat64ArrayImpl
         ? t.toJSArrayExternRef()
-        : jsFloat64ArrayFromDartFloat64List(t));
+        : jsFloat64ArrayFromDartFloat64List(t)));
   }
 }
 
@@ -405,7 +397,7 @@
     final t = this;
     return t is js_types.JSArrayImpl
         // Explicit cast to avoid using the extension method.
-        ? JSValue.boxT<JSArray<T>>((t as js_types.JSArrayImpl).toExternRef)
+        ? JSArray<T>._(JSValue((t as js_types.JSArrayImpl).toExternRef))
         : null;
   }
 
@@ -438,7 +430,7 @@
 @patch
 extension DoubleToJSNumber on double {
   @patch
-  JSNumber get toJS => _boxNonNullable<JSNumber>(toJSNumber(this));
+  JSNumber get toJS => JSNumber._(JSValue(toJSNumber(this)));
 }
 
 /// [JSBoolean] <-> [bool]
@@ -451,7 +443,7 @@
 @patch
 extension BoolToJSBoolean on bool {
   @patch
-  JSBoolean get toJS => _boxNonNullable<JSBoolean>(toJSBoolean(this));
+  JSBoolean get toJS => JSBoolean._(JSValue(toJSBoolean(this)));
 }
 
 /// [JSString] <-> [String]
@@ -466,110 +458,107 @@
   @patch
   JSString get toJS {
     final t = this;
-    return _boxNonNullable<JSString>(
-        t is js_types.JSStringImpl ? t.toExternRef : jsStringFromDartString(t));
+    return JSString._(JSValue(t is js_types.JSStringImpl
+        ? t.toExternRef
+        : jsStringFromDartString(t)));
   }
 }
 
 @patch
 extension JSAnyOperatorExtension on JSAny? {
   @patch
-  JSAny add(JSAny? any) => _boxNonNullable<JSAny>(js_helper.JS<WasmExternRef?>(
-      '(o, a) => o + a', this.toExternRef, any.toExternRef));
+  JSAny add(JSAny? any) => JSAny._(JSValue(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o + a', this.toExternRef, any.toExternRef)));
 
   @patch
-  JSAny subtract(JSAny? any) =>
-      _boxNonNullable<JSAny>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o - a', this.toExternRef, any.toExternRef));
+  JSAny subtract(JSAny? any) => JSAny._(JSValue(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o - a', this.toExternRef, any.toExternRef)));
 
   @patch
-  JSAny multiply(JSAny? any) =>
-      _boxNonNullable<JSAny>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o * a', this.toExternRef, any.toExternRef));
+  JSAny multiply(JSAny? any) => JSAny._(JSValue(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o * a', this.toExternRef, any.toExternRef)));
 
   @patch
-  JSAny divide(JSAny? any) =>
-      _boxNonNullable<JSAny>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o / a', this.toExternRef, any.toExternRef));
+  JSAny divide(JSAny? any) => JSAny._(JSValue(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o / a', this.toExternRef, any.toExternRef)));
 
   @patch
-  JSAny modulo(JSAny? any) =>
-      _boxNonNullable<JSAny>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o % a', this.toExternRef, any.toExternRef));
+  JSAny modulo(JSAny? any) => JSAny._(JSValue(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o % a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSAny exponentiate(JSAny? any) =>
-      _boxNonNullable<JSAny>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o ** a', this.toExternRef, any.toExternRef));
+      JSAny._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o ** a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean greaterThan(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o > a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o > a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean greaterThanOrEqualTo(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o >= a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o >= a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean lessThan(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o < a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o < a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean lessThanOrEqualTo(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o <= a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o <= a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean equals(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o == a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o == a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean notEquals(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o != a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o != a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean strictEquals(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o === a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o === a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSBoolean strictNotEquals(JSAny? any) =>
-      _boxNonNullable<JSBoolean>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o !== a', this.toExternRef, any.toExternRef));
+      JSBoolean._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o !== a', this.toExternRef, any.toExternRef)));
 
   @patch
   JSNumber unsignedRightShift(JSAny? any) =>
-      _boxNonNullable<JSNumber>(js_helper.JS<WasmExternRef?>(
-          '(o, a) => o >>> a', this.toExternRef, any.toExternRef));
+      JSNumber._(JSValue(js_helper.JS<WasmExternRef?>(
+          '(o, a) => o >>> a', this.toExternRef, any.toExternRef)));
 
   @patch
-  JSAny? and(JSAny? any) => _boxNullable<JSAny?>(js_helper.JS<WasmExternRef?>(
-      '(o, a) => o && a', this.toExternRef, any.toExternRef));
+  JSAny? and(JSAny? any) => JSValue.box(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o && a', this.toExternRef, any.toExternRef)) as JSAny?;
 
   @patch
-  JSAny? or(JSAny? any) => _boxNullable<JSAny?>(js_helper.JS<WasmExternRef?>(
-      '(o, a) => o || a', this.toExternRef, any.toExternRef));
+  JSAny? or(JSAny? any) => JSValue.box(js_helper.JS<WasmExternRef?>(
+      '(o, a) => o || a', this.toExternRef, any.toExternRef)) as JSAny?;
 
   @patch
-  bool get not => _boxNonNullable<JSBoolean>(
-          js_helper.JS<WasmExternRef?>('(o) => !o', this.toExternRef))
+  bool get not => JSBoolean._(
+          JSValue(js_helper.JS<WasmExternRef?>('(o) => !o', this.toExternRef)))
       .toDart;
 
   @patch
-  bool get isTruthy => _boxNonNullable<JSBoolean>(
-          js_helper.JS<WasmExternRef?>('(o) => !!o', this.toExternRef))
+  bool get isTruthy => JSBoolean._(
+          JSValue(js_helper.JS<WasmExternRef?>('(o) => !!o', this.toExternRef)))
       .toDart;
 }
 
 @patch
 JSPromise<JSObject> importModule(String moduleName) =>
-    _boxNonNullable<JSPromise<JSObject>>(js_helper.JS<WasmExternRef?>(
-        '(m) => import(m)', moduleName.toJS.toExternRef));
+    JSPromise<JSObject>._(JSValue(js_helper.JS<WasmExternRef?>(
+        '(m) => import(m)', moduleName.toJS.toExternRef)));
 
 @JS('Array')
 @staticInterop
@@ -652,7 +641,7 @@
   final hasIndex = jsExportWrapper['_hasIndex']!.toExternRef;
   final deleteIndex = jsExportWrapper['_deleteIndex']!.toExternRef;
 
-  final proxy = _boxNonNullable<JSArray<T>>(js_helper.JS<WasmExternRef?>('''
+  final proxy = JSArray<T>._(JSValue(js_helper.JS<WasmExternRef?>('''
     (wrapper, getIndex, setIndex, hasIndex, deleteIndex) => new Proxy(wrapper, {
       'get': function (target, prop, receiver) {
         if (typeof prop == 'string') {
@@ -703,7 +692,7 @@
         return Reflect.deleteProperty(target, prop);
       }
     })''', jsExportWrapper.toExternRef, getIndex, setIndex, hasIndex,
-      deleteIndex));
+      deleteIndex)));
   wrapper.proxy = proxy;
   return proxy;
 }