-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use named constants for dict and segment arena related offs…
…et in hints
- Loading branch information
Showing
11 changed files
with
112 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
import { BuiltinHandler } from './builtin'; | ||
|
||
/** | ||
* Offset to compute the address | ||
* of the info segment pointer. | ||
*/ | ||
export const INFO_PTR_OFFSET = 3; | ||
|
||
/** | ||
* Offset to read the current number | ||
* of allocated dictionaries. | ||
*/ | ||
export const DICT_NUMBER_OFFSET = 2; | ||
|
||
/** | ||
* The segment arena builtin manages Cairo dictionaries. | ||
* | ||
* It works by block of 3 cells: | ||
* - The first cell contains the base address of the info pointer. | ||
* - The second cell contains the current number of allocated dictionaries. | ||
* - The third cell contains the current number of squashed dictionaries. | ||
* | ||
* The Info segment is tightly closed to the segment arena builtin. | ||
* | ||
* It also works by block of 3 cells: | ||
* - The first cell is the base address of a dictionary | ||
* - The second cell is the end address of a dictionary when squashed. | ||
* - The third cell is the current number of squashed dictionaries (i.e. its squashing index). | ||
*/ | ||
export const segmentArenaHandler: BuiltinHandler = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Relocatable } from 'primitives/relocatable'; | ||
|
||
class DictionaryError extends Error {} | ||
|
||
/** Cannot find Dictionary at `address` */ | ||
export class DictNotFound extends DictionaryError { | ||
constructor(address: Relocatable) { | ||
super(`Cannot found any Dictionary at address ${address.toString()}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { describe, test, expect } from 'bun:test'; | ||
|
||
import { DictNotFound } from 'errors/dictionary'; | ||
|
||
import { Felt } from 'primitives/felt'; | ||
import { Relocatable } from 'primitives/relocatable'; | ||
import { VirtualMachine } from 'vm/virtualMachine'; | ||
import { Dictionary } from './dictionary'; | ||
|
||
describe('Dictionary', () => { | ||
test('should properly initialize the dict manager', () => { | ||
const vm = new VirtualMachine(); | ||
expect(vm.dictManager.size).toEqual(0); | ||
}); | ||
|
||
test('should properly create a new dictionary', () => { | ||
const vm = new VirtualMachine(); | ||
const address = vm.newDict(); | ||
expect(address).toEqual(new Relocatable(0, 0)); | ||
expect(vm.getDict(address)).toEqual(new Dictionary(new Felt(0n))); | ||
}); | ||
|
||
test('should throw DictNotFound when accessing a non-existing dictionary', () => { | ||
const vm = new VirtualMachine(); | ||
const address = new Relocatable(2, 3); | ||
expect(() => vm.getDict(address)).toThrow(new DictNotFound(address)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Felt } from 'primitives/felt'; | ||
import { SegmentValue } from 'primitives/segmentValue'; | ||
|
||
export const DICT_ACCESS_SIZE = 3; | ||
|
||
/** Offset to read the key of the entry to update. */ | ||
export const KEY_OFFSET = 3; | ||
|
||
/** | ||
* Offset to read the previous value | ||
* of the entry to read or update. | ||
*/ | ||
export const PREV_VALUE_OFFSET = 1; | ||
|
||
/** | ||
* Helper class to implement Cairo dictionaries. | ||
* | ||
* The `id` attribute is needed to keep track | ||
* of the multiple dictionaries and their | ||
* corresponding segment in memory. | ||
*/ | ||
export class Dictionary extends Map<string, SegmentValue> { | ||
constructor(public readonly id: Felt) { | ||
super(); | ||
this.id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters