storeObject.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Location } from "../../memory/location";
  2. import { Type } from '../../typeSystem/type';
  3. export class StoreObject {
  4. private _type: Type;
  5. private _loc_address: number;
  6. private _readOnly: boolean;
  7. private _id : String | null;
  8. /**
  9. *
  10. * @param {Type} type
  11. * @param {Number} loc_address
  12. * @param {Boolean} readOnly
  13. */
  14. constructor (type: Type, loc_address: number, readOnly = false) {
  15. this._loc_address = loc_address;
  16. this._type = type;
  17. this._readOnly = readOnly;
  18. this._id = null;
  19. }
  20. setID (id: String | null) {
  21. this._id = id;
  22. Location.updateAddress(this._loc_address, this.value, true);
  23. }
  24. get id () {
  25. return this._id;
  26. }
  27. get inStore () {
  28. return this.id !== null;
  29. }
  30. get type () {
  31. return this._type;
  32. }
  33. /**
  34. * Returns the actual value stored at the loccation address present in this object.
  35. * The returned value is compatible with @prop{StoreObject.type}
  36. */
  37. get value () {
  38. const address = Location.find(this._loc_address);
  39. if (address != null) {
  40. return address.value
  41. } else {
  42. throw new Error("!!!Critical Error: variable "+this.id+" does not have a valid address. Loc-Address "+ this.locAddress);
  43. }
  44. }
  45. get number () {
  46. throw new Error("DOT NOT USE THIS PROPERTY!");
  47. }
  48. get readOnly () {
  49. return this._readOnly;
  50. }
  51. set readOnly (value) {
  52. this._readOnly = value;
  53. }
  54. isCompatible (another: StoreObject) {
  55. return this.type.isCompatible(another.type);
  56. }
  57. destroy () {
  58. return Location.deallocate(this._loc_address);
  59. }
  60. copy () {
  61. const clone = new StoreObject(this.type, Location.allocate(this.value), this.readOnly);
  62. return clone;
  63. }
  64. get locAddress () {
  65. return this._loc_address;
  66. }
  67. }