Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Cannot access null value from map #217

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions lib/src/eval/runtime/ops/objects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ class InvokeDynamic implements EvcOp {
runtime._prOffset = object.offset;
return;
}
final method = ((object as $Instance).$getProperty(runtime, _method)
as EvalFunction);
try {
runtime.returnValue = method.call(runtime, object, runtime.args.cast());
} catch (e) {
runtime.$throw(e);

if (object == null) {
object = $Null();
}

if (object is $Instance) {
final method = (object.$getProperty(runtime, _method) as EvalFunction);
try {
runtime.returnValue =
method.call(runtime, object, runtime.args.cast());
} catch (e) {
runtime.$throw(e);
}
} else {
runtime.returnValue = $null();
}

runtime.args = [];
return;
}
Expand Down Expand Up @@ -156,7 +166,14 @@ class CheckEq implements EvcOp {
return;
}

runtime.returnValue = v1 == v2;
if (v2 is $Value && v1 is! $Value) {
runtime.returnValue = v2.$value == v1;
} else if (v1 is $Value && v2 is! $Value) {
runtime.returnValue = v1.$value == v2;
} else {
runtime.returnValue = v1 == v2;
}

return;
}
}
Expand Down Expand Up @@ -383,7 +400,7 @@ class IsType implements EvcOp {

@override
void run(Runtime runtime) {
final value = runtime.frame[_objectOffset] as $Value;
final value = (runtime.frame[_objectOffset] ?? $null()) as $Value;
final type = value.$getRuntimeType(runtime);
if (type < 0) {
final result = type == _type;
Expand Down
8 changes: 7 additions & 1 deletion lib/src/eval/runtime/ops/primitives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,13 @@ class Unbox implements EvcOp {

@override
void run(Runtime runtime) {
runtime.frame[_reg] = (runtime.frame[_reg] as $Value).$value;
dynamic v = runtime.frame[_reg];

if (v is $Value) {
v = v.$value;
}

runtime.frame[_reg] = v;
}

@override
Expand Down
1 change: 1 addition & 0 deletions lib/src/eval/shared/stdlib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class DartCorePlugin implements EvalPlugin {
configureIdenticalForRuntime(runtime);
$String.configureForRuntime(runtime);
$List.configureForRuntime(runtime);
$Map.configureForRuntime(runtime);
$MapEntry.configureForRuntime(runtime);
$Iterable.configureForRuntime(runtime);
$Duration.configureForRuntime(runtime);
Expand Down
68 changes: 68 additions & 0 deletions lib/src/eval/shared/stdlib/core/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,71 @@ class $String implements $Instance {
@override
int get hashCode => $value.hashCode;
}

/// dart_eval [$Instance] representation of a [Null]
class $Null implements $Instance {
$Null() : _superclass = $Object(_Null());

static const $declaration = BridgeClassDef(
BridgeClassType(BridgeTypeRef(CoreTypes.nullType), isAbstract: true),
constructors: {},
methods: {
// Other Null methods defined in builtins.dart
},
wrap: true);

final $Instance _superclass;

@override
$Value? $getProperty(Runtime runtime, String identifier) {
try {
return _superclass.$getProperty(runtime, identifier);
} catch (e) {
return $Function((
Runtime runtime,
$Value? target,
List<$Value?> args,
) {
return $null();
});
}
}

@override
void $setProperty(Runtime runtime, String identifier, $Value value) {}

@override
_Null get $reified => $value;

@override
bool operator ==(Object other) {
return identical(this, other) ||
other is $Null &&
runtimeType == other.runtimeType &&
$value == other.$value;
}

@override
int get hashCode => $value.hashCode;

@override
String toString() {
return '\$${$value}';
}

@override
int $getRuntimeType(Runtime runtime) =>
runtime.lookupType(CoreTypes.nullType);

@override
get $value => _Null();
}

@pragma("vm:entry-point")
class _Null extends Object {
String toString() => "null";

dynamic noSuchMethod(Invocation invocation) {
print("_Null.noSuchMethod(): $invocation");
}
}
38 changes: 35 additions & 3 deletions lib/src/eval/shared/stdlib/core/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,34 @@ class $Map<K, V> implements Map<K, V>, $Instance {
/// Wrap a [Map] in a [$Map]
$Map.wrap(this.$value);

static void configureForRuntime(Runtime runtime) {
return runtime.registerBridgeFunc(
'dart:core', 'Map.from', __$Map$from.call);
}

static const $type = BridgeTypeRef(CoreTypes.map);

static const $declaration = BridgeClassDef(
BridgeClassType(BridgeTypeRef(CoreTypes.map),
generics: {'K': BridgeGenericParam(), 'V': BridgeGenericParam()}),
constructors: {},
BridgeClassType(
$type,
generics: {'K': BridgeGenericParam(), 'V': BridgeGenericParam()},
),
constructors: {
'from': BridgeConstructorDef(
BridgeFunctionDef(
returns: BridgeTypeAnnotation($type),
params: [
BridgeParameter(
'other',
BridgeTypeAnnotation($type, nullable: false),
false,
)
],
generics: {'K': BridgeGenericParam(), 'V': BridgeGenericParam()},
),
isFactory: true,
),
},
methods: {
'[]': BridgeMethodDef(
BridgeFunctionDef(params: [
Expand Down Expand Up @@ -126,6 +150,14 @@ class $Map<K, V> implements Map<K, V>, $Instance {
return _superclass.$setProperty(runtime, identifier, value);
}

static const __$Map$from = $Function(_$Map$from);
static $Value? _$Map$from(
Runtime runtime, $Value? target, List<$Value?> args) {
final other = args[0]?.$value as Map;

return $Map.wrap(Map.from(other));
}

static const $Function __indexGet = $Function(_indexGet);

static $Value? _indexGet(
Expand Down
15 changes: 15 additions & 0 deletions lib/src/eval/shared/stdlib/core/num.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class $num<T extends num> implements $Instance {
nullable: true),
params: []),
isStatic: false),
'toDouble': BridgeMethodDef(
BridgeFunctionDef(
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double),
nullable: true),
params: []),
isStatic: false),
'ceil': BridgeMethodDef(
BridgeFunctionDef(
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.int),
Expand Down Expand Up @@ -129,6 +135,8 @@ class $num<T extends num> implements $Instance {
return __compareTo;
case 'toInt':
return __toInt;
case 'toDouble':
return __toDouble;
case 'ceil':
return __ceil;
}
Expand Down Expand Up @@ -276,6 +284,13 @@ class $num<T extends num> implements $Instance {
return $int(_evalResult);
}

static const $Function __toDouble = $Function(_toDouble);
static $Value? _toDouble(
Runtime runtime, $Value? target, List<$Value?> args) {
final _evalResult = (target!.$value as num).toDouble();
return $double(_evalResult);
}

static const $Function __ceil = $Function(_ceil);
static $Value? _ceil(Runtime runtime, $Value? target, List<$Value?> args) {
final _evalResult = (target!.$value as num).ceil();
Expand Down
Loading