58 lines
1.7 KiB
Dart
58 lines
1.7 KiB
Dart
// ignore_for_file: undefined_class, undefined_identifier, undefined_method, uri_does_not_exist
|
|
|
|
import 'dart:js_interop';
|
|
import 'dart:js_interop_unsafe';
|
|
|
|
/// Dynamic JavaScript interop helpers for Dart 3.10 and WebAssembly builds.
|
|
Object? getJsProperty(Object object, Object property) {
|
|
final value = (object as JSObject).getProperty<JSAny?>(
|
|
_propertyToJs(property),
|
|
);
|
|
return _toDartValue(value);
|
|
}
|
|
|
|
bool hasJsProperty(Object object, String property) =>
|
|
(object as JSObject).has(property);
|
|
|
|
Object? callJsMethod(
|
|
Object object,
|
|
String method, [
|
|
List<Object?> arguments = const [],
|
|
]) {
|
|
final value = (object as JSObject).callMethodVarArgs<JSAny?>(
|
|
method.toJS,
|
|
arguments.map(_valueToJs).toList(),
|
|
);
|
|
return _toDartValue(value);
|
|
}
|
|
|
|
Object? constructJsObject(
|
|
Object constructor, [
|
|
List<Object?> arguments = const [],
|
|
]) =>
|
|
(constructor as JSFunction).callAsConstructorVarArgs<JSObject>(
|
|
arguments.map(_valueToJs).toList(),
|
|
);
|
|
|
|
JSAny _propertyToJs(Object property) {
|
|
if (property is String) return property.toJS;
|
|
if (property is num) return property.toJS;
|
|
throw ArgumentError.value(property, 'property', 'Must be a string or number');
|
|
}
|
|
|
|
JSAny? _valueToJs(Object? value) {
|
|
if (value == null) return null;
|
|
if (value is JSAny) return value;
|
|
if (value is String) return value.toJS;
|
|
if (value is num) return value.toJS;
|
|
if (value is bool) return value.toJS;
|
|
throw ArgumentError.value(value, 'value', 'Unsupported JavaScript argument');
|
|
}
|
|
|
|
Object? _toDartValue(JSAny? value) {
|
|
if (value is JSString) return value.toDart;
|
|
if (value is JSNumber) return value.toDartDouble;
|
|
if (value is JSBoolean) return value.toDart;
|
|
return value;
|
|
}
|