From 161a7604048b4af8121f0e21a77137c00372021a Mon Sep 17 00:00:00 2001 From: zyrouge Date: Sun, 15 Oct 2023 20:02:28 +0530 Subject: [PATCH] refactor: object accessor better error messages --- packages/beize_vm/lib/vm/interpreter.dart | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/beize_vm/lib/vm/interpreter.dart b/packages/beize_vm/lib/vm/interpreter.dart index 26ae59a..a583108 100644 --- a/packages/beize_vm/lib/vm/interpreter.dart +++ b/packages/beize_vm/lib/vm/interpreter.dart @@ -449,7 +449,12 @@ class BeizeInterpreter { case BeizeOpCodes.opGetProperty: final BeizeValue name = stack.pop(); - final BeizePrimitiveObjectValue obj = stack.pop(); + final BeizeValue obj = stack.pop(); + if (obj is! BeizePrimitiveObjectValue) { + return handleInvalidMemberAccess( + 'Cannot use member accessor on "${obj.kind.code}"', + ); + } final BeizeValue value = obj.get(name); stack.push(value); break; @@ -457,7 +462,12 @@ class BeizeInterpreter { case BeizeOpCodes.opSetProperty: final BeizeValue value = stack.pop(); final BeizeValue name = stack.pop(); - final BeizePrimitiveObjectValue obj = stack.pop(); + final BeizeValue obj = stack.pop(); + if (obj is! BeizePrimitiveObjectValue) { + return handleInvalidMemberAccess( + 'Cannot do member assign on "${obj.kind.code}"', + ); + } obj.set(name, value); stack.push(value); break; @@ -516,6 +526,9 @@ class BeizeInterpreter { return handleException(err); } + BeizeInterpreterResult handleInvalidMemberAccess(final String message) => + handleCustomException('InvalidMemberAccess', message); + BeizeInterpreterResult handleInvalidUnary(final String message) => handleCustomException('InvalidUnaryOperation', message);