Skip to content

Commit

Permalink
Merge pull request #165 from Noobware1/master
Browse files Browse the repository at this point in the history
This just fixes some of my issues ;)
  • Loading branch information
ethanblake4 committed Feb 2, 2024
2 parents c876ca4 + 3fcd0d7 commit 20d3663
Show file tree
Hide file tree
Showing 25 changed files with 1,338 additions and 36 deletions.
28 changes: 14 additions & 14 deletions lib/src/eval/compiler/compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,21 +524,21 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
final type = t.key;
typeIds.putIfAbsent(type.file, () => {})[type.name] = t.value;
}

return Program(
_ctx.topLevelDeclarationPositions,
_ctx.instanceDeclarationPositions,
typeIds,
//ctx.typeNames,
_ctx.typeTypes,
_ctx.offsetTracker.apply(_ctx.out),
libraryMapString,
_ctx.bridgeStaticFunctionIndices,
_ctx.constantPool.pool,
_ctx.runtimeTypes.pool,
globalInitializers,
_ctx.enumValueIndices,
_ctx.runtimeOverrideMap);
_ctx.topLevelDeclarationPositions,
_ctx.instanceDeclarationPositions,
typeIds,
//ctx.typeNames,
_ctx.typeTypes,
_ctx.offsetTracker.apply(_ctx.out),
libraryMapString,
_ctx.bridgeStaticFunctionIndices,
_ctx.constantPool.pool,
_ctx.runtimeTypes.pool,
globalInitializers,
_ctx.enumValueIndices,
_ctx.runtimeOverrideMap,
);
}

/// For testing purposes. Compile code, write it to a byte stream, load it,
Expand Down
1 change: 1 addition & 0 deletions lib/src/eval/compiler/statement/variable_declaration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void compileVariableDeclarationList(
' multiple times in the same scope');
}
final init = li.initializer;

if (init != null) {
var res = compileExpression(init, ctx, type);
if (type != null &&
Expand Down
3 changes: 2 additions & 1 deletion lib/src/eval/runtime/ops/memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class PushReturnValue implements EvcOp {

@override
void run(Runtime runtime) {
runtime.frame[runtime.frameOffset++] = runtime.returnValue;
final offset = runtime.frameOffset++;
runtime.frame[offset] = runtime.returnValue;
}

@override
Expand Down
3 changes: 3 additions & 0 deletions lib/src/eval/runtime/ops/objects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ class CheckEq implements EvcOp {
runtime.callStack.add(runtime._prOffset);
runtime.catchStack.add([]);
runtime._prOffset = _offset;

return;
}

if (vx is $Instance) {
final method = vx.$getProperty(runtime, '==') as EvalFunction;

runtime.returnValue = method
.call(runtime, vx, [v2 == null ? null : v2 as $Value])!.$value;
runtime.args = [];

return;
}

Expand Down
1 change: 0 additions & 1 deletion lib/src/eval/runtime/runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,6 @@ class Runtime {
_prOffset = $offset;
callStack.add(-1);
catchStack.add([]);

try {
while (true) {
final op = pr[_prOffset++];
Expand Down
2 changes: 2 additions & 0 deletions lib/src/eval/shared/stdlib/convert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DartConvertPlugin implements EvalPlugin {
registry.defineBridgeClass($JsonEncoder.$declaration);
registry.defineBridgeClass($JsonCodec.$declaration);
registry.addSource(DartSource('dart:convert', convertSource));
$JsonEncodeAndDecode.configureForCompile(registry);
}

@override
Expand All @@ -46,5 +47,6 @@ class DartConvertPlugin implements EvalPlugin {
runtime.registerBridgeFunc(
'dart:convert', 'JsonEncoder.', $JsonEncoder.$new);
runtime.registerBridgeFunc('dart:convert', 'JsonCodec.', $JsonCodec.$new);
$JsonEncodeAndDecode.configureForRuntime(runtime);
}
}
105 changes: 105 additions & 0 deletions lib/src/eval/shared/stdlib/convert/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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/src/eval/shared/stdlib/core/base.dart';

/// dart_eval wrapper for [JsonDecoder]
class $JsonDecoder implements $Instance {
Expand Down Expand Up @@ -218,3 +219,107 @@ class $JsonCodec implements $Instance {
int $getRuntimeType(Runtime runtime) =>
runtime.lookupType(ConvertTypes.jsonCodec);
}

class $JsonEncodeAndDecode {
static const _library = 'dart:convert';

static void configureForCompile(BridgeDeclarationRegistry registry) {
registry.defineBridgeTopLevelFunction(_$jsonEncode);
registry.defineBridgeTopLevelFunction(_$jsonDecode);
}

static void configureForRuntime(Runtime runtime) {
runtime.registerBridgeFunc(_library, 'jsonEncode', __$jsonEncode.call);
runtime.registerBridgeFunc(_library, 'jsonDecode', __$jsonDecode.call);
}

static const _$jsonEncode = BridgeFunctionDeclaration(
_library,
'jsonEncode',
BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.string),
nullable: false,
),
params: [
BridgeParameter(
'object',
BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.object),
nullable: true,
),
false,
)
],
namedParams: [
BridgeParameter(
'toEncodable',
BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.function),
nullable: true,
),
true,
)
],
),
);

static const _$jsonDecode = BridgeFunctionDeclaration(
_library,
'jsonDecode',
BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.dynamic),
nullable: true,
),
params: [
BridgeParameter(
'source',
BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.string),
),
false,
)
],
namedParams: [
BridgeParameter(
'reviver',
BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.function),
nullable: true,
),
true,
)
],
),
);

static const __$jsonEncode = $Function(_$encode);
static $Value? _$encode(Runtime runtime, $Value? target, List<$Value?> args) {
final toEncodable = args[1]?.$value as EvalCallable?;
return $String(jsonEncode(args[0]?.$reified,
toEncodable: toEncodable == null
? null
: (object) {
return toEncodable.call(runtime, null, [
runtime.wrapPrimitive(object) ?? object as $Value?
])?.$value;
}));
}

static const __$jsonDecode = $Function(_$decode);
static $Value? _$decode(Runtime runtime, $Value? target, List<$Value?> args) {
final reviver = args[1]?.$value as EvalCallable?;
return runtime.wrapRecursive(
jsonDecode(args[0]?.$value,
reviver: reviver == null
? null
: (key, value) {
return reviver.call(runtime, null, [
runtime.wrapPrimitive(key) ?? key as $Value?,
runtime.wrapPrimitive(value) ?? value as $Value?
])?.$value;
}),
);
}
}
9 changes: 9 additions & 0 deletions lib/src/eval/shared/stdlib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:dart_eval/src/eval/shared/stdlib/core/num.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/object.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/pattern.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/regexp.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/stack_trace.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/string_buffer.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/symbol.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/type.dart';
Expand Down Expand Up @@ -66,6 +67,10 @@ class DartCorePlugin implements EvalPlugin {
registry.defineBridgeClass($FormatException.$declaration);
registry.defineBridgeClass($ArgumentError.$declaration);
registry.defineBridgeClass($StateError.$declaration);
$StackTrace.configureForCompile(registry);
$Error.configureForCompile(registry);
$UnimplementedError.configureForCompile(registry);
$UnsupportedError.configureForCompile(registry);
}

@override
Expand Down Expand Up @@ -101,6 +106,10 @@ class DartCorePlugin implements EvalPlugin {
runtime.registerBridgeFunc('dart:core', 'RangeError.checkNotNegative',
$RangeError.$checkNotNegative);
runtime.registerBridgeFunc('dart:core', 'Symbol.', $Symbol.$new);
$StackTrace.configureForRuntime(runtime);
$Error.configureForRuntime(runtime);
$UnimplementedError.configureForRuntime(runtime);
$UnsupportedError.configureForRuntime(runtime);
runtime.registerBridgeFunc(
'dart:core', 'FormatException.', $FormatException.$new);
runtime.registerBridgeFunc(
Expand Down
17 changes: 13 additions & 4 deletions lib/src/eval/shared/stdlib/core/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import 'package:dart_eval/src/eval/utils/wap_helper.dart';
import 'num.dart';

const $dynamicCls = BridgeClassDef(
BridgeClassType(BridgeTypeRef(CoreTypes.dynamic),
isAbstract: true, $extends: null),
constructors: {},
wrap: true);
BridgeClassType(BridgeTypeRef(CoreTypes.dynamic),
isAbstract: true, $extends: null),
constructors: {},
wrap: true,
methods: {
'toString': BridgeMethodDef(
BridgeFunctionDef(
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
params: [],
namedParams: []),
),
},
);

const $voidCls = BridgeClassDef(
BridgeClassType(BridgeTypeRef(CoreTypes.voidType), isAbstract: true),
Expand Down

0 comments on commit 20d3663

Please sign in to comment.