storeObject.ts 1.6 KB

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