Skip to content

Commit

Permalink
Merge pull request #145 from kodjodevf/master
Browse files Browse the repository at this point in the history
Add base64 wrapper, String.fromCharCodes, utf8.decode() and utf8.encode(), Added more methods for Uri
  • Loading branch information
ethanblake4 authored Nov 17, 2023
2 parents 2634531 + d9091db commit 18d6a88
Show file tree
Hide file tree
Showing 6 changed files with 892 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/src/eval/shared/stdlib/convert.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/src/eval/shared/stdlib/convert/base64.dart';
import 'package:dart_eval/src/eval/shared/stdlib/convert/codec.dart';
import 'package:dart_eval/src/eval/shared/stdlib/convert/converter.dart';
import 'package:dart_eval/src/eval/shared/stdlib/convert/encoding.dart';
Expand All @@ -9,6 +10,8 @@ import 'package:dart_eval/src/eval/shared/stdlib/convert/utf.dart';
const convertSource = '''
final utf8 = Utf8Codec();
final json = JsonCodec();
final Base64Codec base64Url = Base64Codec.urlSafe();
final base64 = Base64Codec();
''';

/// [EvalPlugin] for the `dart:convert` library
Expand All @@ -23,6 +26,9 @@ class DartConvertPlugin implements EvalPlugin {
registry.defineBridgeClass($Encoding.$declaration);
registry.defineBridgeClass($Utf8Decoder.$declaration);
registry.defineBridgeClass($Utf8Codec.$declaration);
registry.defineBridgeClass($Base64Encoder.$declaration);
registry.defineBridgeClass($Base64Decoder.$declaration);
registry.defineBridgeClass($Base64Codec.$declaration);
registry.defineBridgeClass($JsonDecoder.$declaration);
registry.defineBridgeClass($JsonEncoder.$declaration);
registry.defineBridgeClass($JsonCodec.$declaration);
Expand All @@ -34,6 +40,7 @@ class DartConvertPlugin implements EvalPlugin {
runtime.registerBridgeFunc(
'dart:convert', 'Utf8Decoder.', $Utf8Decoder.$new);
runtime.registerBridgeFunc('dart:convert', 'Utf8Codec.', $Utf8Codec.$new);
$Base64Codec.configureForRuntime(runtime);
runtime.registerBridgeFunc(
'dart:convert', 'JsonDecoder.', $JsonDecoder.$new);
runtime.registerBridgeFunc(
Expand Down
237 changes: 237 additions & 0 deletions lib/src/eval/shared/stdlib/convert/base64.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import 'dart:convert';
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/src/eval/shared/stdlib/convert/codec.dart';
import 'package:dart_eval/src/eval/shared/stdlib/convert/converter.dart';
import 'package:dart_eval/stdlib/core.dart';

/// dart_eval wrapper for [Base64Decoder]
class $Base64Decoder implements $Instance {
/// Compile-time bridge type reference for [$Base64Decoder]
static const $type = BridgeTypeRef(ConvertTypes.base64Decoder);

/// Compile-time bridge class declaration for [$Base64Decoder]
static const $declaration = BridgeClassDef(
BridgeClassType($type,
$extends: BridgeTypeRef(ConvertTypes.converter, [
BridgeTypeRef(CoreTypes.string),
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef(CoreTypes.int)]),
])),
constructors: {
'': BridgeConstructorDef(
BridgeFunctionDef(returns: BridgeTypeAnnotation($type)))
},
methods: {},
getters: {},
setters: {},
fields: {},
wrap: true);

/// Wrap a [Base64Decoder] in a [$Base64Decoder].
$Base64Decoder.wrap(this.$value);
static $Base64Decoder $new(
Runtime runtime, $Value? target, List<$Value?> args) {
return $Base64Decoder.wrap(Base64Decoder());
}

late final $Instance _superclass = $Converter.wrap($value);
@override
final Base64Decoder $value;
@override
get $reified => $value;
@override
$Value? $getProperty(Runtime runtime, String identifier) {
switch (identifier) {
default:
return _superclass.$getProperty(runtime, identifier);
}
}

@override
void $setProperty(Runtime runtime, String identifier, $Value value) {
switch (identifier) {
default:
return _superclass.$setProperty(runtime, identifier, value);
}
}

@override
int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);
}

/// dart_eval wrapper for [Base64Encoder]
class $Base64Encoder implements $Instance {
/// Compile-time bridge type reference for [$Base64Encoder]
static const $type = BridgeTypeRef(ConvertTypes.base64Encoder);

/// Compile-time bridge class declaration for [$Base64Encoder]
static const $declaration = BridgeClassDef(
BridgeClassType($type,
$extends: BridgeTypeRef(ConvertTypes.converter, [
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef(CoreTypes.int)]),
BridgeTypeRef(CoreTypes.string),
])),
constructors: {
'': BridgeConstructorDef(
BridgeFunctionDef(returns: BridgeTypeAnnotation($type)))
},
methods: {},
getters: {},
setters: {},
fields: {},
wrap: true);

/// Wrap a [Base64Encoder] in a [$Base64Encoder].
$Base64Encoder.wrap(this.$value);
static $Base64Encoder $new(
Runtime runtime, $Value? target, List<$Value?> args) {
return $Base64Encoder.wrap(Base64Encoder());
}

late final $Instance _superclass = $Converter.wrap($value);
@override
final Base64Encoder $value;
@override
get $reified => $value;
@override
$Value? $getProperty(Runtime runtime, String identifier) {
switch (identifier) {
default:
return _superclass.$getProperty(runtime, identifier);
}
}

@override
void $setProperty(Runtime runtime, String identifier, $Value value) {
switch (identifier) {
default:
return _superclass.$setProperty(runtime, identifier, value);
}
}

@override
int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);
}

/// dart_eval wrapper for [Base64Codec]
class $Base64Codec implements $Instance {
/// Compile-time bridge type reference for [$Base64Codec]
static const $type = BridgeTypeRef(ConvertTypes.base64Codec);

/// Compile-time bridge class declaration for [$Base64Codec]
static const $declaration = BridgeClassDef(
BridgeClassType($type, $extends: BridgeTypeRef(ConvertTypes.encoding)),
constructors: {
'': BridgeConstructorDef(
BridgeFunctionDef(returns: BridgeTypeAnnotation($type))),
'urlSafe': BridgeConstructorDef(
BridgeFunctionDef(
returns:
BridgeTypeAnnotation(BridgeTypeRef(ConvertTypes.base64Codec)),
),
isFactory: true),
},
methods: {
'encode': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
params: [
BridgeParameter(
'input',
BridgeTypeAnnotation(BridgeTypeRef(
CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])),
false),
])),
'decode': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])),
params: [
BridgeParameter('input',
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), false),
])),
},
getters: {
'decoder': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef(ConvertTypes.base64Decoder)))),
'encoder': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef(ConvertTypes.base64Encoder)))),
},
setters: {},
fields: {},
wrap: true);

/// Wrap a [Base64Codec] in a [$Base64Codec].
$Base64Codec.wrap(this.$value);
static $Base64Codec $new(
Runtime runtime, $Value? target, List<$Value?> args) {
return $Base64Codec.wrap(Base64Codec());
}

static void configureForRuntime(Runtime runtime) {
runtime.registerBridgeFunc(
'dart:convert', 'Base64Encoder.', $Base64Encoder.$new);
runtime.registerBridgeFunc(
'dart:convert', 'Base64Decoder.', $Base64Decoder.$new);
runtime.registerBridgeFunc(
'dart:convert', 'Base64Codec.', $Base64Codec.$new);
runtime.registerBridgeFunc('dart:convert', 'Base64Codec.urlSafe', _urlSafe);
}

late final $Instance _superclass = $Codec.wrap($value);
@override
final Base64Codec $value;
@override
get $reified => $value;
@override
$Value? $getProperty(Runtime runtime, String identifier) {
switch (identifier) {
case 'decoder':
return $Base64Decoder.wrap($value.decoder);
case 'encoder':
return $Base64Encoder.wrap($value.encoder);
case 'encode':
return __encode;
case 'decode':
return __decode;
default:
return _superclass.$getProperty(runtime, identifier);
}
}

static const $Function __encode = $Function(_encode);
static $Value? _encode(
final Runtime runtime, final $Value? target, final List<$Value?> args) {
final input = (args[0]!.$value as List)
.map((e) => (e is $Value ? e.$reified : e) as int)
.toList();
return $String((target as $Base64Codec).$value.encode(input));
}

static const $Function __decode = $Function(_decode);
static $Value? _decode(
final Runtime runtime, final $Value? target, final List<$Value?> args) {
return $List.wrap((target as $Base64Codec)
.$value
.decode(args[0]!.$value)
.map((e) => $int(e))
.toList());
}

static $Value? _urlSafe(
final Runtime runtime, final $Value? target, final List<$Value?> args) {
return $Base64Codec.wrap(Base64Codec.urlSafe());
}

@override
void $setProperty(Runtime runtime, String identifier, $Value value) {
switch (identifier) {
default:
return _superclass.$setProperty(runtime, identifier, value);
}
}

@override
int $getRuntimeType(Runtime runtime) =>
runtime.lookupType(ConvertTypes.base64Codec);
}
53 changes: 48 additions & 5 deletions lib/src/eval/shared/stdlib/convert/utf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,32 @@ class $Utf8Codec implements $Instance {
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), true)
]))
},
methods: {},
methods: {
'encode': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])),
params: [
BridgeParameter('input',
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), false),
])),
'decode': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
params: [
BridgeParameter(
'codeUnits',
BridgeTypeAnnotation(BridgeTypeRef(
CoreTypes.list, [BridgeTypeRef(CoreTypes.int)])),
false),
],
namedParams: [
BridgeParameter('allowMalformed',
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), true)
])),
},
getters: {
'decoder': BridgeMethodDef(BridgeFunctionDef(
returns:
BridgeTypeAnnotation(BridgeTypeRef(ConvertTypes.utf8Decoder, [
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef(CoreTypes.int)]),
BridgeTypeRef(CoreTypes.string),
])))),
BridgeTypeAnnotation(BridgeTypeRef(ConvertTypes.utf8Decoder)))),
},
setters: {},
fields: {},
Expand All @@ -120,11 +138,36 @@ class $Utf8Codec implements $Instance {
switch (identifier) {
case 'decoder':
return $Utf8Decoder.wrap($value.decoder);
case 'decode':
return __decode;
case 'encode':
return __encode;
default:
return _superclass.$getProperty(runtime, identifier);
}
}

static const $Function __decode = $Function(_decode);
static $Value? _decode(
final Runtime runtime, final $Value? target, final List<$Value?> args) {
final codeUnits = (args[0]!.$value as List)
.map((e) => (e is $Value ? e.$reified : e) as int)
.toList();
return $String((target as $Utf8Codec)
.$value
.decode(codeUnits, allowMalformed: args[1]?.$value));
}

static const $Function __encode = $Function(_encode);
static $Value? _encode(
final Runtime runtime, final $Value? target, final List<$Value?> args) {
return $List.wrap((target as $Utf8Codec)
.$value
.encode(args[0]!.$value)
.map((e) => $int(e))
.toList());
}

@override
void $setProperty(Runtime runtime, String identifier, $Value value) {
switch (identifier) {
Expand Down
Loading

0 comments on commit 18d6a88

Please sign in to comment.