1
0

storeObject.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { Location } from "../../memory/location";
  2. import { IType } from "../../typeSystem/itype";
  3. import { IStoreValue } from "./value/istore_value";
  4. export class StoreObject {
  5. private _type: IType;
  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: IType, 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. }
  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: IStoreValue) {
  55. return this.type.isCompatible(another.type);
  56. }
  57. destroy () {
  58. return Location.deallocate(this._loc_address);
  59. }
  60. get locAddress () {
  61. return this._loc_address;
  62. }
  63. }