diff --git a/lib/src/eval/shared/stdlib/core/uri.dart b/lib/src/eval/shared/stdlib/core/uri.dart index 1aaad34..1cd6c60 100644 --- a/lib/src/eval/shared/stdlib/core/uri.dart +++ b/lib/src/eval/shared/stdlib/core/uri.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:dart_eval/dart_eval.dart'; import 'package:dart_eval/dart_eval_bridge.dart'; import 'package:dart_eval/src/eval/utils/wap_helper.dart'; @@ -11,6 +13,28 @@ class $Uri implements $Instance { runtime.registerBridgeFunc('dart:core', 'Uri.tryParse', $tryParse); runtime.registerBridgeFunc('dart:core', 'Uri.encodeFull', $encodeFull); runtime.registerBridgeFunc('dart:core', 'Uri.decodeFull', $decodeFull); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.encodeComponent', $encodeComponent); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.decodeComponent', $decodeComponent); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.decodeQueryComponent', $decodeQueryComponent); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.encodeQueryComponent', $encodeQueryComponent); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.dataFromBytes', $dataFromBytes); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.dataFromString', $dataFromString); + runtime.registerBridgeFunc('dart:core', 'Uri.directory', $directory); + runtime.registerBridgeFunc('dart:core', 'Uri.file', $file); + runtime.registerBridgeFunc('dart:core', 'Uri.http', $http); + runtime.registerBridgeFunc('dart:core', 'Uri.https', $https); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.parseIPv4Address', $parseIPv4Address); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.parseIPv6Address', $parseIPv6Address); + runtime.registerBridgeFunc( + 'dart:core', 'Uri.splitQueryString', $splitQueryString); } /// Bridge type spec for [$Uri] @@ -59,6 +83,284 @@ class $Uri implements $Instance { ], namedParams: []), isStatic: true), + 'encodeComponent': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'component', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false) + ], + namedParams: []), + isStatic: true), + 'decodeComponent': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'encodedComponent', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false) + ], + namedParams: []), + isStatic: true), + 'decodeQueryComponent': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'encodedComponent', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: [ + BridgeParameter( + 'encoding', + BridgeTypeAnnotation( + BridgeTypeRef(ConvertTypes.encoding)), + true) + ]), + isStatic: true), + 'encodeQueryComponent': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'component', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: [ + BridgeParameter( + 'encoding', + BridgeTypeAnnotation( + BridgeTypeRef(ConvertTypes.encoding)), + true) + ]), + isStatic: true), + 'dataFromBytes': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'bytes', + BridgeTypeAnnotation(BridgeTypeRef( + CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])), + false), + ], + namedParams: [ + BridgeParameter( + 'mimeType', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + true), + BridgeParameter( + 'parameters', + BridgeTypeAnnotation( + BridgeTypeRef(CoreTypes.map, [ + BridgeTypeRef(CoreTypes.string), + BridgeTypeRef(CoreTypes.string) + ]), + ), + true), + BridgeParameter( + 'percentEncoded', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), + true), + ]), + isStatic: true), + 'dataFromString': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'content', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: [ + BridgeParameter( + 'mimeType', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + true), + BridgeParameter( + 'parameters', + BridgeTypeAnnotation( + BridgeTypeRef(CoreTypes.map, [ + BridgeTypeRef(CoreTypes.string), + BridgeTypeRef(CoreTypes.string) + ]), + nullable: true), + true), + BridgeParameter( + 'base64', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), + true), + ]), + isStatic: true), + 'directory': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'path', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: [ + BridgeParameter('windows', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), true) + ]), + isStatic: true), + 'file': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'path', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: [ + BridgeParameter('windows', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), true) + ]), + isStatic: true), + 'http': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'authority', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + BridgeParameter( + 'unencodedPath', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + true), + BridgeParameter( + 'queryParameters', + BridgeTypeAnnotation( + BridgeTypeRef(CoreTypes.map, [ + BridgeTypeRef(CoreTypes.string), + BridgeTypeRef(CoreTypes.dynamic) + ]), + nullable: true), + true), + ], + namedParams: []), + isStatic: true), + 'https': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + params: [ + BridgeParameter( + 'authority', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + BridgeParameter( + 'unencodedPath', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + true), + BridgeParameter( + 'queryParameters', + BridgeTypeAnnotation( + BridgeTypeRef(CoreTypes.map, [ + BridgeTypeRef(CoreTypes.string), + BridgeTypeRef(CoreTypes.dynamic) + ]), + nullable: true), + true), + ], + namedParams: []), + isStatic: true), + 'parseIPv4Address': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef( + CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])), + params: [ + BridgeParameter( + 'host', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: []), + isStatic: true), + 'parseIPv6Address': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef( + CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])), + params: [ + BridgeParameter( + 'host', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + BridgeParameter( + 'start', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + true), + BridgeParameter( + 'end', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string), + nullable: true), + true), + ], + namedParams: []), + isStatic: true), + 'splitQueryString': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.map, [ + BridgeTypeRef(CoreTypes.string), + BridgeTypeRef(CoreTypes.string) + ])), + params: [ + BridgeParameter( + 'query', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: [ + BridgeParameter( + 'encoding', + BridgeTypeAnnotation( + BridgeTypeRef(ConvertTypes.encoding)), + true) + ]), + isStatic: true), + 'resolve': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.uri)), + params: [ + BridgeParameter( + 'reference', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: []), + isStatic: true), + 'normalizePath': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.uri)), + params: [], + namedParams: []), + isStatic: true), + 'removeFragment': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.uri)), + params: [], + namedParams: []), + isStatic: true), + 'resolveUri': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.uri)), + params: [ + BridgeParameter( + 'reference', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), + false), + ], + namedParams: []), + isStatic: true), }, getters: { 'scheme': BridgeMethodDef(BridgeFunctionDef( @@ -165,6 +467,14 @@ class $Uri implements $Instance { return $bool($value.hasAbsolutePath); case 'origin': return $String($value.origin); + case 'resolve': + return __resolve; + case 'normalizePath': + return __normalizePath; + case 'removeFragment': + return __removeFragment; + case 'resolveUri': + return __resolveUri; default: return _superclass.$getProperty(runtime, identifier); @@ -176,6 +486,30 @@ class $Uri implements $Instance { return _superclass.$setProperty(runtime, identifier, value); } + static const $Function __resolve = $Function(_resolve); + static $Value? _resolve( + final Runtime runtime, final $Value? target, final List<$Value?> args) { + return $Uri.wrap((target as $Uri).$value.resolve(args[0]!.$value)); + } + + static const $Function __normalizePath = $Function(_normalizePath); + static $Value? _normalizePath( + final Runtime runtime, final $Value? target, final List<$Value?> args) { + return $Uri.wrap((target as $Uri).$value.normalizePath()); + } + + static const $Function __removeFragment = $Function(_removeFragment); + static $Value? _removeFragment( + final Runtime runtime, final $Value? target, final List<$Value?> args) { + return $Uri.wrap((target as $Uri).$value.removeFragment()); + } + + static const $Function __resolveUri = $Function(_resolveUri); + static $Value? _resolveUri( + final Runtime runtime, final $Value? target, final List<$Value?> args) { + return $Uri.wrap((target as $Uri).$value.resolveUri(args[0]!.$value)); + } + static $Value? $parse(Runtime runtime, $Value? target, List<$Value?> args) { final uri = args[0]!.$value as String; return $Uri.wrap(Uri.parse(uri)); @@ -200,6 +534,98 @@ class $Uri implements $Instance { return $String(Uri.decodeFull(uri)); } + static $Value? $encodeComponent( + Runtime runtime, $Value? target, List<$Value?> args) { + return $String(Uri.encodeComponent(args[0]!.$value)); + } + + static $Value? $decodeComponent( + Runtime runtime, $Value? target, List<$Value?> args) { + return $String(Uri.decodeComponent(args[0]!.$value)); + } + + static $Value? $decodeQueryComponent( + Runtime runtime, $Value? target, List<$Value?> args) { + return $String(Uri.decodeQueryComponent(args[0]!.$value, + encoding: (args[1]?.$value as Encoding?) ?? utf8)); + } + + static $Value? $encodeQueryComponent( + Runtime runtime, $Value? target, List<$Value?> args) { + return $String(Uri.encodeQueryComponent(args[0]!.$value, + encoding: (args[1]?.$value as Encoding?) ?? utf8)); + } + + static $Value? $dataFromBytes( + Runtime runtime, $Value? target, List<$Value?> args) { + final bytes = (args[0]!.$value as List) + .map((e) => (e is $Value ? e.$reified : e) as int) + .toList(); + final parameters = (args[2]?.$value as Map?)?.map((key, value) => + MapEntry(key.$reified.toString(), value.$reified.toString())); + return $Uri.wrap(Uri.dataFromBytes(bytes, + mimeType: args[1]?.$value ?? "application/octet-stream", + parameters: parameters, + percentEncoded: args[3]?.$value ?? false)); + } + + static $Value? $dataFromString( + Runtime runtime, $Value? target, List<$Value?> args) { + final parameters = (args[2]?.$value as Map?)?.map((key, value) => + MapEntry(key.$reified.toString(), value.$reified.toString())); + return $Uri.wrap(Uri.dataFromString(args[0]!.$value, + mimeType: args[1]?.$value ?? "application/octet-stream", + parameters: parameters, + base64: args[3]?.$value ?? false)); + } + + static $Value? $directory( + Runtime runtime, $Value? target, List<$Value?> args) { + return $Uri.wrap(Uri.directory(args[0]!.$value, + windows: (args[1]?.$value as bool?) ?? false)); + } + + static $Value? $file(Runtime runtime, $Value? target, List<$Value?> args) { + return $Uri.wrap(Uri.file(args[0]!.$value, + windows: (args[1]?.$value as bool?) ?? false)); + } + + static $Value? $http(Runtime runtime, $Value? target, List<$Value?> args) { + final queryParameters = (args[2]?.$value as Map?)?.map( + (key, value) => MapEntry(key.$reified.toString(), value.$reified)); + return $Uri.wrap( + Uri.http(args[0]!.$value, args[1]?.$value ?? "", queryParameters)); + } + + static $Value? $https(Runtime runtime, $Value? target, List<$Value?> args) { + final queryParameters = (args[2]?.$value as Map?)?.map( + (key, value) => MapEntry(key.$reified.toString(), value.$reified)); + return $Uri.wrap( + Uri.https(args[0]!.$value, args[1]?.$value ?? "", queryParameters)); + } + + static $Value? $parseIPv4Address( + Runtime runtime, $Value? target, List<$Value?> args) { + return $List.wrap( + Uri.parseIPv4Address(args[0]!.$value).map((e) => $int(e)).toList()); + } + + static $Value? $parseIPv6Address( + Runtime runtime, $Value? target, List<$Value?> args) { + return $List.wrap(Uri.parseIPv6Address( + args[0]!.$value, args[1]?.$value ?? 0, args[2]?.$value) + .map((e) => $int(e)) + .toList()); + } + + static $Value? $splitQueryString( + Runtime runtime, $Value? target, List<$Value?> args) { + return wrapMap( + Uri.splitQueryString(args[0]!.$value, + encoding: (args[1]?.$value as Encoding?) ?? utf8), + (key, value) => MapEntry($String(key), $String(value))); + } + @override int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);