From fef86bffd8e5ec3fdf802289cd90d33d067f6c68 Mon Sep 17 00:00:00 2001 From: Zyrouge Date: Wed, 11 Dec 2024 02:01:29 +0530 Subject: [PATCH] refactor(vm): class op --- .../beize_compiler/example/project/dev.beize | 14 ++--- .../beize_compiler/lib/compiler/compiler.dart | 17 ++++++ .../lib/compiler/parser/parser.dart | 60 ++++++++++++++++--- .../lib/compiler/parser/rules.dart | 1 + .../lib/disassembler/disassembler.dart | 11 ++++ packages/beize_compiler/lib/lexer/utils.dart | 2 +- .../lib/scanner/rules/identifier.dart | 5 +- .../lib/scanner/rules/rules.dart | 1 + packages/beize_compiler/lib/token/tokens.dart | 4 ++ .../lib/bytecode/constants/class.dart | 23 ------- .../lib/bytecode/constants/exports.dart | 1 - .../beize_shared/lib/bytecode/op_codes.dart | 1 + packages/beize_vm/lib/values/class/class.dart | 9 --- .../lib/values/class/native_class.dart | 9 +++ .../beize_vm/lib/values/class/vm_class.dart | 19 +++--- .../lib/values/function/function.dart | 5 +- .../lib/values/object/object_class.dart | 2 +- packages/beize_vm/lib/vm/interpreter.dart | 29 ++++++++- packages/beize_vm/lib/vm/natives/convert.dart | 36 +++++++---- .../beize_vm/lib/vm/natives/datetime.dart | 6 +- packages/beize_vm/lib/vm/natives/fiber.dart | 2 +- packages/beize_vm/lib/vm/natives/math.dart | 2 +- packages/beize_vm/lib/vm/natives/regexp.dart | 6 +- packages/beize_vm/lib/vm/stack.dart | 3 + 24 files changed, 181 insertions(+), 87 deletions(-) delete mode 100644 packages/beize_shared/lib/bytecode/constants/class.dart diff --git a/packages/beize_compiler/example/project/dev.beize b/packages/beize_compiler/example/project/dev.beize index e88ed2f..d4e537c 100644 --- a/packages/beize_compiler/example/project/dev.beize +++ b/packages/beize_compiler/example/project/dev.beize @@ -1,10 +1,8 @@ -printHello := -> async { - print("Hello World"); +test := $ { + hi: "hi", + static hi: "hi static", }; -main := -> async { - printHello().await; -}; - -main().await; -print(Function is null); +print(test.hi); +print(test().hi); +print(test() is test); diff --git a/packages/beize_compiler/lib/compiler/compiler.dart b/packages/beize_compiler/lib/compiler/compiler.dart index 5019b88..8e15420 100644 --- a/packages/beize_compiler/lib/compiler/compiler.dart +++ b/packages/beize_compiler/lib/compiler/compiler.dart @@ -10,6 +10,7 @@ import 'state.dart'; enum BeizeCompilerMode { function, + clazz, script, } @@ -87,6 +88,22 @@ class BeizeCompiler { return derived; } + BeizeCompiler createClassCompiler() { + final BeizeCompiler derived = BeizeCompiler._( + scanner, + mode: BeizeCompilerMode.clazz, + root: root, + modulePath: modulePath, + moduleIndex: moduleIndex, + modules: modules, + constants: constants, + parent: this, + options: options, + ); + derived.prepare(isAsync: false); + return derived; + } + Future createModuleCompiler( final int moduleIndex, final String path, { diff --git a/packages/beize_compiler/lib/compiler/parser/parser.dart b/packages/beize_compiler/lib/compiler/parser/parser.dart index 4dda7a9..1551792 100644 --- a/packages/beize_compiler/lib/compiler/parser/parser.dart +++ b/packages/beize_compiler/lib/compiler/parser/parser.dart @@ -625,6 +625,46 @@ abstract class BeizeParser { compiler.copyTokenState(functionCompiler); } + static void parseClass(final BeizeCompiler compiler) { + final bool hasParentClass = !compiler.check(BeizeTokens.braceLeft); + if (hasParentClass) { + parseExpression(compiler); + } + final BeizeCompiler classCompiler = compiler.createClassCompiler(); + classCompiler.emitOpCode(BeizeOpCodes.opBeginScope); + compiler.emitConstant(classCompiler.currentFunction); + int staticCount = 0; + int objectCount = 0; + bool cont = true; + compiler.consume(BeizeTokens.braceLeft); + while (cont && !compiler.check(BeizeTokens.braceRight)) { + final bool isStatic = compiler.match(BeizeTokens.staticKw); + if (isStatic) { + parseObjectKey(compiler); + compiler.consume(BeizeTokens.colon); + parseExpression(compiler); + staticCount++; + } else { + classCompiler.copyTokenState(compiler); + parseObjectKey(classCompiler); + classCompiler.consume(BeizeTokens.colon); + parseExpression(classCompiler); + compiler.copyTokenState(classCompiler); + objectCount++; + } + cont = compiler.match(BeizeTokens.comma); + } + classCompiler.copyTokenState(compiler); + classCompiler.emitOpCode(BeizeOpCodes.opObject); + classCompiler.emitCode(objectCount); + classCompiler.emitOpCode(BeizeOpCodes.opReturn); + classCompiler.emitOpCode(BeizeOpCodes.opEndScope); + compiler.consume(BeizeTokens.braceRight); + compiler.emitOpCode(BeizeOpCodes.opClass); + compiler.emitCode(hasParentClass ? 1 : 0); + compiler.emitCode(staticCount); + } + static void parseCall(final BeizeCompiler compiler) { int count = 0; bool cont = true; @@ -651,18 +691,22 @@ abstract class BeizeParser { compiler.emitCode(count); } + static void parseObjectKey(final BeizeCompiler compiler) { + if (compiler.match(BeizeTokens.bracketLeft)) { + parseExpression(compiler); + compiler.consume(BeizeTokens.bracketRight); + return; + } + compiler.consume(BeizeTokens.identifier); + final String key = compiler.previousToken.literal as String; + compiler.emitConstant(key); + } + static void parseObject(final BeizeCompiler compiler) { int count = 0; bool cont = true; while (cont && !compiler.check(BeizeTokens.braceRight)) { - if (compiler.match(BeizeTokens.bracketLeft)) { - parseExpression(compiler); - compiler.consume(BeizeTokens.bracketRight); - } else { - compiler.consume(BeizeTokens.identifier); - final String key = compiler.previousToken.literal as String; - compiler.emitConstant(key); - } + parseObjectKey(compiler); compiler.consume(BeizeTokens.colon); parseExpression(compiler); count++; diff --git a/packages/beize_compiler/lib/compiler/parser/rules.dart b/packages/beize_compiler/lib/compiler/parser/rules.dart index f4363a4..ed9bc3f 100644 --- a/packages/beize_compiler/lib/compiler/parser/rules.dart +++ b/packages/beize_compiler/lib/compiler/parser/rules.dart @@ -128,6 +128,7 @@ class BeizeParseRule { precedence: BeizePrecedence.caret, infix: BeizeParser.parseBinaryExpression, ), + BeizeTokens.dollar: BeizeParseRule(prefix: BeizeParser.parseClass), BeizeTokens.identifier: BeizeParseRule(prefix: BeizeParser.parseIdentifier), BeizeTokens.number: BeizeParseRule(prefix: BeizeParser.parseNumber), BeizeTokens.string: BeizeParseRule(prefix: BeizeParser.parseString), diff --git a/packages/beize_compiler/lib/disassembler/disassembler.dart b/packages/beize_compiler/lib/disassembler/disassembler.dart index bcb814b..3202996 100644 --- a/packages/beize_compiler/lib/disassembler/disassembler.dart +++ b/packages/beize_compiler/lib/disassembler/disassembler.dart @@ -98,6 +98,17 @@ class BeizeDisassembler { ); return 1; + case BeizeOpCodes.opClass: + final bool hasParentClass = chunk.codeAt(ip + 1) == 1; + final int popCount = chunk.codeAt(ip + 2); + writeInstruction( + opCode, + ip, + chunk.lineAt(ip), + '{so}(parent = $hasParentClass, popCount = $popCount)', + ); + return 2; + default: writeInstruction(opCode, ip, chunk.lineAt(ip)); return 0; diff --git a/packages/beize_compiler/lib/lexer/utils.dart b/packages/beize_compiler/lib/lexer/utils.dart index 8262524..8fb9bfd 100644 --- a/packages/beize_compiler/lib/lexer/utils.dart +++ b/packages/beize_compiler/lib/lexer/utils.dart @@ -6,7 +6,7 @@ class BeizeLexerUtils { static bool isQuote(final String char) => char == "'" || char == '"' || char == '`'; static bool isAlpha(final String char) => - r'$_abcdefghijklmnopqrstuvwxyz'.contains(char.toLowerCase()); + '_abcdefghijklmnopqrstuvwxyz'.contains(char.toLowerCase()); static bool isAlphaNumeric(final String char) => isAlpha(char) || isNumeric(char); } diff --git a/packages/beize_compiler/lib/scanner/rules/identifier.dart b/packages/beize_compiler/lib/scanner/rules/identifier.dart index d3745ef..d1f1669 100644 --- a/packages/beize_compiler/lib/scanner/rules/identifier.dart +++ b/packages/beize_compiler/lib/scanner/rules/identifier.dart @@ -31,6 +31,7 @@ abstract class BeizeIdentifierScanner { BeizeTokens.awaitKw, BeizeTokens.onlyKw, BeizeTokens.isKw, + BeizeTokens.staticKw, }; static final Map keywordsMap = keywords.asNameMap().map( @@ -52,7 +53,9 @@ abstract class BeizeIdentifierScanner { BeizeInputIteration current = start; while (!scanner.input.isEndOfLine()) { current = scanner.input.peek(); - if (!BeizeLexerUtils.isAlphaNumeric(current.char)) break; + if (!BeizeLexerUtils.isAlphaNumeric(current.char)) { + break; + } buffer.write(current.char); scanner.input.advance(); } diff --git a/packages/beize_compiler/lib/scanner/rules/rules.dart b/packages/beize_compiler/lib/scanner/rules/rules.dart index 270a76d..6eeb793 100644 --- a/packages/beize_compiler/lib/scanner/rules/rules.dart +++ b/packages/beize_compiler/lib/scanner/rules/rules.dart @@ -55,6 +55,7 @@ class BeizeScannerRules { constructOffset1ScanFn(BeizeTokens.bang), constructOffset1ScanFn(BeizeTokens.lesserThan), constructOffset1ScanFn(BeizeTokens.greaterThan), + constructOffset1ScanFn(BeizeTokens.dollar), ], ); diff --git a/packages/beize_compiler/lib/token/tokens.dart b/packages/beize_compiler/lib/token/tokens.dart index c507204..1664d3d 100644 --- a/packages/beize_compiler/lib/token/tokens.dart +++ b/packages/beize_compiler/lib/token/tokens.dart @@ -58,6 +58,7 @@ enum BeizeTokens { logicalOrEqual, // ||= caretEqual, // ^= nullOrEqual, // ??= + dollar, // $ trueKw, // true falseKw, // false @@ -81,6 +82,7 @@ enum BeizeTokens { awaitKw, // await onlyKw, // only isKw, // is + staticKw, // static } const Map _tokensCodeMap = { @@ -141,6 +143,7 @@ const Map _tokensCodeMap = { BeizeTokens.logicalOrEqual: '||=', BeizeTokens.caretEqual: '^=', BeizeTokens.nullOrEqual: '??=', + BeizeTokens.dollar: r'$', BeizeTokens.trueKw: 'true', BeizeTokens.falseKw: 'false', BeizeTokens.ifKw: 'if', @@ -163,6 +166,7 @@ const Map _tokensCodeMap = { BeizeTokens.awaitKw: 'await', BeizeTokens.onlyKw: 'only', BeizeTokens.isKw: 'is', + BeizeTokens.staticKw: 'static', }; extension OutreTokensUtils on BeizeTokens { diff --git a/packages/beize_shared/lib/bytecode/constants/class.dart b/packages/beize_shared/lib/bytecode/constants/class.dart deleted file mode 100644 index bfef19c..0000000 --- a/packages/beize_shared/lib/bytecode/constants/class.dart +++ /dev/null @@ -1,23 +0,0 @@ -import '../chunk.dart'; -import 'constant.dart'; - -class BeizeClassConstant { - BeizeClassConstant({ - required this.moduleIndex, - required this.chunk, - }); - - factory BeizeClassConstant.deserialize( - final BeizeSerializedConstant serialized, - ) => - BeizeClassConstant( - moduleIndex: serialized[0] as int, - chunk: BeizeChunk.deserialize(serialized[1] as BeizeSerializedConstant), - ); - - final int moduleIndex; - final BeizeChunk chunk; - - BeizeSerializedConstant serialize() => - [moduleIndex, chunk.serialize()]; -} diff --git a/packages/beize_shared/lib/bytecode/constants/exports.dart b/packages/beize_shared/lib/bytecode/constants/exports.dart index b02b0f5..e749ae3 100644 --- a/packages/beize_shared/lib/bytecode/constants/exports.dart +++ b/packages/beize_shared/lib/bytecode/constants/exports.dart @@ -1,4 +1,3 @@ -export 'class.dart'; export 'constant.dart'; export 'function.dart'; export 'program.dart'; diff --git a/packages/beize_shared/lib/bytecode/op_codes.dart b/packages/beize_shared/lib/bytecode/op_codes.dart index 69cd74b..6a45bb1 100644 --- a/packages/beize_shared/lib/bytecode/op_codes.dart +++ b/packages/beize_shared/lib/bytecode/op_codes.dart @@ -39,6 +39,7 @@ enum BeizeOpCodes { opSetProperty, opList, opObject, + opClass, opThrow, opBeginTry, opEndTry, diff --git a/packages/beize_vm/lib/values/class/class.dart b/packages/beize_vm/lib/values/class/class.dart index 00311d2..0db2899 100644 --- a/packages/beize_vm/lib/values/class/class.dart +++ b/packages/beize_vm/lib/values/class/class.dart @@ -1,18 +1,9 @@ -import '../../vm/exports.dart'; import '../exports.dart'; abstract class BeizeClassValue extends BeizeNativeObjectValue implements BeizeCallableValue { bool kInstance(final BeizeObjectValue value); - BeizeObjectValue kInstantiate(final BeizeCallableCall call); - - @override - BeizeInterpreterResult kCall(final BeizeCallableCall call) { - final BeizeObjectValue value = kInstantiate(call); - return BeizeInterpreterResult.success(value); - } - @override BeizeClassValue get kClass => this; } diff --git a/packages/beize_vm/lib/values/class/native_class.dart b/packages/beize_vm/lib/values/class/native_class.dart index a1aa43f..57e57af 100644 --- a/packages/beize_vm/lib/values/class/native_class.dart +++ b/packages/beize_vm/lib/values/class/native_class.dart @@ -1,9 +1,18 @@ +import '../../vm/exports.dart'; import '../exports.dart'; abstract class BeizeNativeClassValue extends BeizeClassValue { @override final BeizeValueKind kind = BeizeValueKind.nativeClazz; + BeizeObjectValue kInstantiate(final BeizeCallableCall call); + + @override + BeizeInterpreterResult kCall(final BeizeCallableCall call) { + final BeizeObjectValue value = kInstantiate(call); + return BeizeInterpreterResult.success(value); + } + @override String kToString() => ''; diff --git a/packages/beize_vm/lib/values/class/vm_class.dart b/packages/beize_vm/lib/values/class/vm_class.dart index dac4588..74f267e 100644 --- a/packages/beize_vm/lib/values/class/vm_class.dart +++ b/packages/beize_vm/lib/values/class/vm_class.dart @@ -1,10 +1,9 @@ -import '../../bytecode.dart'; +import '../../vm/exports.dart'; import '../exports.dart'; class BeizeVMClassValue extends BeizeClassValue { - BeizeVMClassValue(this.constant); - - final BeizeClassConstant constant; + late final BeizeClassValue? parentClass; + late final BeizeCallableValue constructor; @override final BeizeValueKind kind = BeizeValueKind.clazz; @@ -13,12 +12,14 @@ class BeizeVMClassValue extends BeizeClassValue { bool kInstance(final BeizeObjectValue value) => value is BeizeClassValue; @override - BeizeObjectValue kInstantiate(final BeizeCallableCall call) { - throw UnimplementedError(); - } + BeizeInterpreterResult kCall(final BeizeCallableCall call) => + constructor.kCall(call); @override - BeizeVMClassValue kClone() => BeizeVMClassValue(constant)..kCopyFrom(this); + BeizeVMClassValue kClone() => BeizeVMClassValue() + ..parentClass = parentClass + ..constructor = constructor + ..kCopyFrom(this); @override String kToString() => ''; @@ -27,5 +28,5 @@ class BeizeVMClassValue extends BeizeClassValue { bool get isTruthy => true; @override - int get kHashCode => constant.hashCode; + int get kHashCode => Object.hash(parentClass, constructor); } diff --git a/packages/beize_vm/lib/values/function/function.dart b/packages/beize_vm/lib/values/function/function.dart index 97c8734..768f8e1 100644 --- a/packages/beize_vm/lib/values/function/function.dart +++ b/packages/beize_vm/lib/values/function/function.dart @@ -20,8 +20,9 @@ class BeizeFunctionValue extends BeizeNativeObjectValue @override BeizeInterpreterResult kCall(final BeizeCallableCall call) { if (!constant.isAsync) { - final BeizeInterpreterResult result = BeizeInterpreter(call.frame).run(); - return result; + final BeizeCallFrame frame = + call.frame.prepareCallFunctionValue(call.arguments, this); + return BeizeInterpreter(frame).run(); } final BeizeUnawaitedValue value = BeizeUnawaitedValue( call.arguments, diff --git a/packages/beize_vm/lib/values/object/object_class.dart b/packages/beize_vm/lib/values/object/object_class.dart index db81610..5c1154d 100644 --- a/packages/beize_vm/lib/values/object/object_class.dart +++ b/packages/beize_vm/lib/values/object/object_class.dart @@ -7,7 +7,7 @@ class BeizeObjectClassValue extends BeizeNativeClassValue { BeizeNativeFunctionValue.sync( (final BeizeCallableCall call) { final BeizeListValue value = call.argumentAt(0); - final BeizeMapValue nValue = BeizeMapValue(); + final BeizeObjectValue nValue = BeizeVMObjectValue(); for (final MapEntry x in value.entries()) { nValue.set(x.key, x.value); } diff --git a/packages/beize_vm/lib/vm/interpreter.dart b/packages/beize_vm/lib/vm/interpreter.dart index 693b378..c0657f7 100644 --- a/packages/beize_vm/lib/vm/interpreter.dart +++ b/packages/beize_vm/lib/vm/interpreter.dart @@ -85,7 +85,9 @@ class BeizeInterpreter { default: result = interpretCurrent(); } - if (result != null) break; + if (result != null) { + break; + } } return result ?? BeizeInterpreterResult.success(BeizeNullValue.value); } @@ -96,7 +98,9 @@ class BeizeInterpreter { frame.sip = frame.ip; frame.ip++; result = interpretCurrent(); - if (result != null) break; + if (result != null) { + break; + } } return result ?? BeizeInterpreterResult.success(BeizeNullValue.value); } @@ -406,10 +410,29 @@ class BeizeInterpreter { } stack.push(BeizeListValue(values)); + case BeizeOpCodes.opClass: + final bool hasParentClass = chunk.codeAt(frame.ip) == 1; + final int count = chunk.codeAt(frame.ip + 1); + frame.ip += 2; + final BeizeVMClassValue clazz = BeizeVMClassValue(); + for (int i = 0; i < count; i++) { + final BeizeValue value = stack.pop(); + final BeizeValue key = stack.pop(); + if (value is BeizeFunctionValue) { + value.namespace.assign('this', clazz); + } + clazz.set(key, value); + } + clazz.constructor = stack.pop(); + if (hasParentClass) { + clazz.parentClass = stack.pop(); + } + stack.push(clazz); + case BeizeOpCodes.opObject: final int count = chunk.codeAt(frame.ip); frame.ip++; - final BeizeMapValue obj = BeizeMapValue(); + final BeizeObjectValue obj = BeizeVMObjectValue(); for (int i = 0; i < count; i++) { final BeizeValue value = stack.pop(); final BeizeValue key = stack.pop(); diff --git a/packages/beize_vm/lib/vm/natives/convert.dart b/packages/beize_vm/lib/vm/natives/convert.dart index d045164..3430239 100644 --- a/packages/beize_vm/lib/vm/natives/convert.dart +++ b/packages/beize_vm/lib/vm/natives/convert.dart @@ -5,7 +5,7 @@ import '../namespace.dart'; abstract class BeizeConvertNatives { static void bind(final BeizeNamespace namespace) { - final BeizeMapValue value = BeizeMapValue(); + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('newBytesList'), BeizeNativeFunctionValue.sync( @@ -38,7 +38,7 @@ abstract class BeizeConvertNatives { BeizeStringValue('decodeAscii'), BeizeNativeFunctionValue.sync( (final BeizeCallableCall call) { - final BeizeMapValue input = call.argumentAt(0); + final BeizeObjectValue input = call.argumentAt(0); return BeizeStringValue(ascii.decode(toBytes(input))); }, ), @@ -47,7 +47,7 @@ abstract class BeizeConvertNatives { BeizeStringValue('encodeBase64'), BeizeNativeFunctionValue.sync( (final BeizeCallableCall call) { - final BeizeMapValue input = call.argumentAt(0); + final BeizeObjectValue input = call.argumentAt(0); return BeizeStringValue(base64Encode(toBytes(input))); }, ), @@ -74,7 +74,7 @@ abstract class BeizeConvertNatives { BeizeStringValue('decodeLatin1'), BeizeNativeFunctionValue.sync( (final BeizeCallableCall call) { - final BeizeMapValue input = call.argumentAt(0); + final BeizeObjectValue input = call.argumentAt(0); return BeizeStringValue(latin1.decode(toBytes(input))); }, ), @@ -92,7 +92,7 @@ abstract class BeizeConvertNatives { BeizeStringValue('decodeUtf8'), BeizeNativeFunctionValue.sync( (final BeizeCallableCall call) { - final BeizeMapValue input = call.argumentAt(0); + final BeizeObjectValue input = call.argumentAt(0); return BeizeStringValue(utf8.decode(toBytes(input))); }, ), @@ -118,7 +118,7 @@ abstract class BeizeConvertNatives { namespace.declare('Convert', value); } - static List toBytes(final BeizeMapValue bytesList) { + static List toBytes(final BeizeObjectValue bytesList) { if (bytesList.internals.containsKey('bytes')) { return bytesList.internals['bytes'] as List; } @@ -126,7 +126,7 @@ abstract class BeizeConvertNatives { } static BeizeValue newBytesList(final List bytes) { - final BeizeMapValue bytesList = BeizeMapValue(); + final BeizeObjectValue bytesList = BeizeVMObjectValue(); bytesList.internals['bytes'] = bytes; bytesList.set( BeizeStringValue('bytes'), @@ -140,11 +140,21 @@ abstract class BeizeConvertNatives { } static BeizeValue fromJson(final Object? json) { - if (json is bool) return BeizeBooleanValue(json); - if (json == null) return BeizeNullValue.value; - if (json is int) return BeizeNumberValue(json.toDouble()); - if (json is double) return BeizeNumberValue(json); - if (json is String) return BeizeStringValue(json); + if (json is bool) { + return BeizeBooleanValue(json); + } + if (json == null) { + return BeizeNullValue.value; + } + if (json is int) { + return BeizeNumberValue(json.toDouble()); + } + if (json is double) { + return BeizeNumberValue(json); + } + if (json is String) { + return BeizeStringValue(json); + } if (json is List) { final BeizeListValue list = BeizeListValue(); for (final Object? x in json) { @@ -153,7 +163,7 @@ abstract class BeizeConvertNatives { return list; } if (json is Map) { - final BeizeMapValue obj = BeizeMapValue(); + final BeizeObjectValue obj = BeizeVMObjectValue(); for (final MapEntry x in json.entries) { final BeizeValue key = fromJson(x.key); final BeizeValue value = fromJson(x.value); diff --git a/packages/beize_vm/lib/vm/natives/datetime.dart b/packages/beize_vm/lib/vm/natives/datetime.dart index dc9863f..c8bb6c6 100644 --- a/packages/beize_vm/lib/vm/natives/datetime.dart +++ b/packages/beize_vm/lib/vm/natives/datetime.dart @@ -3,7 +3,7 @@ import '../namespace.dart'; abstract class BeizeDateTimeNatives { static void bind(final BeizeNamespace namespace) { - final BeizeMapValue value = BeizeMapValue(); + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('fromMillisecondsSinceEpoch'), BeizeNativeFunctionValue.sync( @@ -33,8 +33,8 @@ abstract class BeizeDateTimeNatives { namespace.declare('DateTime', value); } - static BeizeMapValue newDateTimeInst(final DateTime dateTime) { - final BeizeMapValue value = BeizeMapValue(); + static BeizeObjectValue newDateTimeInst(final DateTime dateTime) { + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('day'), BeizeNumberValue(dateTime.day.toDouble()), diff --git a/packages/beize_vm/lib/vm/natives/fiber.dart b/packages/beize_vm/lib/vm/natives/fiber.dart index 67dd16b..7401204 100644 --- a/packages/beize_vm/lib/vm/natives/fiber.dart +++ b/packages/beize_vm/lib/vm/natives/fiber.dart @@ -4,7 +4,7 @@ import '../exports.dart'; abstract class BeizeFiberNatives { static void bind(final BeizeNamespace namespace) { - final BeizeMapValue value = BeizeMapValue(); + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('wait'), BeizeNativeFunctionValue.async( diff --git a/packages/beize_vm/lib/vm/natives/math.dart b/packages/beize_vm/lib/vm/natives/math.dart index dbfcdfb..4561025 100644 --- a/packages/beize_vm/lib/vm/natives/math.dart +++ b/packages/beize_vm/lib/vm/natives/math.dart @@ -7,7 +7,7 @@ abstract class BeizeMathNatives { static Random random = Random(); static void bind(final BeizeNamespace namespace) { - final BeizeMapValue value = BeizeMapValue(); + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('random'), BeizeNativeFunctionValue.sync( diff --git a/packages/beize_vm/lib/vm/natives/regexp.dart b/packages/beize_vm/lib/vm/natives/regexp.dart index 4866b3b..8c487c2 100644 --- a/packages/beize_vm/lib/vm/natives/regexp.dart +++ b/packages/beize_vm/lib/vm/natives/regexp.dart @@ -3,7 +3,7 @@ import '../exports.dart'; abstract class BeizeRegExpNatives { static void bind(final BeizeNamespace namespace) { - final BeizeMapValue value = BeizeMapValue(); + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('new'), BeizeNativeFunctionValue.sync( @@ -28,7 +28,7 @@ abstract class BeizeRegExpNatives { multiLine: flags.contains('m'), unicode: flags.contains('u'), ); - final BeizeMapValue value = BeizeMapValue(); + final BeizeObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('isCaseInsensitive'), BeizeBooleanValue(!regex.isCaseSensitive), @@ -138,7 +138,7 @@ abstract class BeizeRegExpNatives { } static BeizeValue newRegExpMatch(final RegExpMatch match) { - final BeizeMapValue value = BeizeMapValue(); + final BeizeVMObjectValue value = BeizeVMObjectValue(); value.set( BeizeStringValue('input'), BeizeStringValue(match.input), diff --git a/packages/beize_vm/lib/vm/stack.dart b/packages/beize_vm/lib/vm/stack.dart index 637c8cd..93e2742 100644 --- a/packages/beize_vm/lib/vm/stack.dart +++ b/packages/beize_vm/lib/vm/stack.dart @@ -7,6 +7,9 @@ class BeizeStack { T pop() => values.removeLast().cast(); T top() => values.last.cast(); + T peekFromBottom(final int offset) => + values[length - offset - 1].cast(); + int get length => values.length; @override