Skip to content

Commit

Permalink
Added base64 wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
kodjodevf committed Nov 15, 2023
1 parent 0049047 commit d409d3d
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 0 deletions.
12 changes: 12 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 base64Url = Base64Codec();
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,12 @@ class DartConvertPlugin implements EvalPlugin {
runtime.registerBridgeFunc(
'dart:convert', 'Utf8Decoder.', $Utf8Decoder.$new);
runtime.registerBridgeFunc('dart:convert', 'Utf8Codec.', $Utf8Codec.$new);
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', 'JsonDecoder.', $JsonDecoder.$new);
runtime.registerBridgeFunc(
Expand Down
216 changes: 216 additions & 0 deletions lib/src/eval/shared/stdlib/convert/base64.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
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)))
},
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());
}

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());
}

@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);
}

0 comments on commit d409d3d

Please sign in to comment.