Skip to content

Commit

Permalink
refactor: bytecode changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 3, 2023
1 parent b09f257 commit 0e8ce3d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
14 changes: 5 additions & 9 deletions packages/beize_shared/lib/bytecode/chunk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ import 'constants/exports.dart';
import 'op_codes.dart';

class BeizeChunk {
const BeizeChunk({
required this.moduleIndex,
BeizeChunk({
required this.codes,
required this.lines,
});

factory BeizeChunk.empty(final int moduleIndex) =>
BeizeChunk(moduleIndex: moduleIndex, codes: <int>[], lines: <int>[]);
factory BeizeChunk.empty() => BeizeChunk(codes: <int>[], lines: <int>[]);

factory BeizeChunk.deserialize(final BeizeSerializedConstant serialized) =>
BeizeChunk(
moduleIndex: serialized[0] as int,
codes: (serialized[1] as List<dynamic>).cast<int>(),
lines: (serialized[2] as List<dynamic>).cast<int>(),
codes: (serialized[0] as List<dynamic>).cast<int>(),
lines: (serialized[1] as List<dynamic>).cast<int>(),
);

final int moduleIndex;
final List<int> codes;
final List<int> lines;

Expand All @@ -37,7 +33,7 @@ class BeizeChunk {

int lineAt(final int index) => lines[index];

BeizeSerializedConstant serialize() => <dynamic>[moduleIndex, codes, lines];
BeizeSerializedConstant serialize() => <dynamic>[codes, lines];

int get length => codes.length;
}
2 changes: 1 addition & 1 deletion packages/beize_shared/lib/bytecode/constants/program.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'constant.dart';
import 'function.dart';

class BeizeProgramConstant {
const BeizeProgramConstant({
BeizeProgramConstant({
required this.modules,
required this.constants,
});
Expand Down
6 changes: 4 additions & 2 deletions packages/beize_vm/lib/vm/call_frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'vm.dart';
class BeizeCallFrame {
BeizeCallFrame({
required this.vm,
required this.moduleIndex,
required this.function,
required this.namespace,
this.parent,
Expand All @@ -21,6 +22,7 @@ class BeizeCallFrame {

final BeizeVM vm;
final BeizeCallFrame? parent;
final int moduleIndex;
final BeizeFunctionConstant function;
final BeizeNamespace namespace;

Expand Down Expand Up @@ -74,6 +76,7 @@ class BeizeCallFrame {
final BeizeCallFrame frame = BeizeCallFrame(
vm: vm,
parent: this,
moduleIndex: moduleIndex,
function: function.constant,
namespace: namespace,
);
Expand Down Expand Up @@ -110,8 +113,7 @@ class BeizeCallFrame {
vm.program.constantAt(function.chunk.codeAt(index));

String toStackTraceLine(final int depth) {
final String moduleName =
vm.program.moduleNameAt(function.chunk.moduleIndex);
final String moduleName = vm.program.moduleNameAt(moduleIndex);
final int line = function.chunk.lineAt(sip);
return '#$depth $moduleName at line $line';
}
Expand Down
1 change: 1 addition & 0 deletions packages/beize_vm/lib/vm/vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class BeizeVM {
modules[moduleIndex] = value;
final BeizeCallFrame frame = BeizeCallFrame(
vm: this,
moduleIndex: moduleIndex,
function: program.moduleFunctionAt(moduleIndex),
namespace: namespace,
parent: !isEntrypoint ? topFrame : null,
Expand Down

0 comments on commit 0e8ce3d

Please sign in to comment.