Skip to content

Commit 9939965

Browse files
authored
Fix JS interop annotations (#1042)
In a declaration like @js('JSON') extension type _JSON._(JSObject _) implements JSObject { @js('JSON.stringify') external static JSString _stringify(JSObject value); } When I call `_JSON._stringify(...)`, dart2js calls `JSON.stringify`, but dart2wasm calls `JSON.JSON.stringify`, which is wrong. Reading [1] it looks like the annotation `JSON.stringify` should just be `stringify`. If I do that the code works with both dart2wasm and dart2js. (I've reported dart2wasm's handling of the annotations above and we'll work on a fix for it on dart2wasm.) This change is not tested on the CI because when compiling with dart2wasm we use the JS decoder library used by the VM, which doesn't use JS interop. I tested this manually with the patch: diff --git a/protobuf/lib/src/protobuf/json/json.dart b/protobuf/lib/src/protobuf/json/json.dart index 05d1ac1..70b6a7c 100644 --- a/protobuf/lib/src/protobuf/json/json.dart +++ b/protobuf/lib/src/protobuf/json/json.dart @@ -13,7 +13,7 @@ import '../utils.dart'; // Use json_vm.dart with VM and dart2wasm, json_web.dart with dart2js. // json_web.dart uses JS interop for parsing, and JS interop is too slow on // Wasm. VM's patch performs better in Wasm. -export 'json_vm.dart' if (dart.library.html) 'json_web.dart'; +export 'json_vm.dart' if (dart.library.js_interop) 'json_web.dart'; Map<String, dynamic> writeToJsonMap(FieldSet fs) { dynamic convertToMap(dynamic fieldValue, int fieldType) { [1]: https://dart.dev/interop/js-interop/usage#js
1 parent da354a2 commit 9939965

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

protobuf/lib/src/protobuf/json/json_web.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import '../utils.dart';
1414

1515
@JS('JSON')
1616
extension type _JSON._(JSObject _) implements JSObject {
17-
@JS('JSON.stringify')
17+
@JS('stringify')
1818
external static JSString _stringify(JSObject value);
1919

20-
@JS('JSON.parse')
20+
@JS('parse')
2121
external static JSAny? _parse(JSString text);
2222
}
2323

2424
@JS('Number')
2525
extension type _Number._(JSObject _) implements JSObject {
26-
@JS('Number.isInteger')
26+
@JS('isInteger')
2727
external static bool _isInteger(JSAny value);
2828
}
2929

0 commit comments

Comments
 (0)