1
0

variables.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. setup.CURRENT_ARGS = function(emptyValue){ return new Proxy({}, {
  2. get(target, prop, receiver) {
  3. const index = parseInt(prop);
  4. const argsSet = State.variables.ARGSstack.last();
  5. //console.log("ARGS GET",prop,argsSet);
  6. if(argsSet.length > index && typeof emptyValue == typeof argsSet[index])
  7. return argsSet[index];
  8. return emptyValue;
  9. },
  10. set(target, prop, val) {
  11. const index = parseInt(prop);
  12. const argsSet = State.variables.ARGSstack.last();
  13. while(argsSet.length <= index)
  14. argsSet.push(emptyValue);
  15. argsSet[index] = val;
  16. return true;
  17. },
  18. });
  19. }
  20. setup.qsp_killvar = (vname, index=-1) => {
  21. try{
  22. const varname = setup.varname(vname);
  23. const dictname = varname+"_indexDict";
  24. if(index == -1){
  25. // DELETE ALL
  26. State.setVar(varname,[]);
  27. if(State.getVar(dictname))
  28. State.setVar(dictname,[]);
  29. }else if(typeof index == "number"){
  30. if(State.getVar(varname))
  31. State.setVar(varname,State.getVar(varname).toSpliced(index, 1));
  32. if(State.getVar(dictname))
  33. State.setVar(dictname,State.getVar(dictname).toSpliced(index, 1));
  34. }else if(typeof index == "string"){
  35. let indexOfindex = -1;
  36. if(State.getVar(dictname))
  37. indexOfindex = State.getVar(dictname).indexOf(index);
  38. if(indexOfindex == -1)
  39. return;
  40. return setup.qsp_killvar(vname, indexOfindex);
  41. }else{
  42. console.error("Type mismatch in qsp_killvar. Expected number|string, received",typeof index);
  43. }
  44. }catch(e){
  45. console.error("ERROR in qsp_killvar:",e);
  46. }
  47. }
  48. setup.varname = (qspVarname) => {
  49. const isString = qspVarname.startsWith('$');
  50. if(qspVarname.startsWith("$QSPVAR_"))
  51. console.error("Recursive Variable Access:",qspVarname);
  52. const varname = "$QSPVAR_"+ (isString ? 's_' : 'n_') + qspVarname;
  53. return varname;
  54. }
  55. window.QSP = new Proxy({},{
  56. get(target, prop, receiver) {
  57. prop = prop.toLowerCase();
  58. if(prop == "args")
  59. return setup.CURRENT_ARGS(0);
  60. if(prop == "$args")
  61. return setup.CURRENT_ARGS("");
  62. const isString = prop.startsWith('$');
  63. const varname = setup.varname(prop);
  64. var value = State.getVar(varname);
  65. return new Proxy({},{
  66. get(target2, prop2, receiver2) {
  67. let overwrittenVar;
  68. if(((overwrittenVar = setup.Overwrite.varGet(prop, prop2)) !== undefined))
  69. return overwrittenVar;
  70. if(value == undefined)
  71. return isString ? '' : 0;
  72. const prop2AsNumber = Number(prop2);
  73. if(Number.isInteger(prop2AsNumber)){
  74. if(Array.isArray(value) && value.length > prop2AsNumber){
  75. return value[parseInt(prop2AsNumber)];
  76. }
  77. }
  78. else{
  79. const indexDictName = varname+"_indexDict";
  80. const indexDict = State.getVar(indexDictName) ?? [];
  81. let index = indexDict.findIndex(entry=>entry==prop2);
  82. if(index == -1){
  83. return isString ? '' : 0;
  84. }
  85. return value[index];
  86. }
  87. return isString ? '' : 0;
  88. },
  89. set(obj2, prop2, value2) {
  90. if(setup.Overwrite.varSet(prop, prop2, value2) !== undefined)
  91. return true;
  92. if(value == undefined){
  93. value = [];
  94. }
  95. if(value2 === true)
  96. value2 = 1;
  97. if(value2 === false)
  98. value2 = 0;
  99. //Insert this as a new value at the end of the array. Used where QSP would do it with `arr[] = "new value"`
  100. if(prop2 == -1){
  101. prop2 = value.length;
  102. }
  103. if(setup.userStyle?.logAssignments)
  104. console.log("SET",prop,prop2,value2,clone(setup.qsp_callStack));
  105. const prop2AsNumber = Number(prop2);
  106. let updateIndex = -1;
  107. const indexDictName = varname+"_indexDict";
  108. let indexDict = State.getVar(indexDictName);
  109. if(Number.isInteger(prop2AsNumber) && !indexDict){
  110. while(value.length <= prop2AsNumber)
  111. value.push(isString ? '' : 0);
  112. updateIndex = prop2;
  113. }
  114. else{
  115. indexDict ??= [];
  116. let index = indexDict.findIndex(entry=>entry==prop2);
  117. if(index == -1){
  118. index = indexDict.length;
  119. indexDict.push(prop2);
  120. State.setVar(indexDictName, indexDict);
  121. }
  122. updateIndex = index;
  123. }
  124. value[updateIndex] = value2;
  125. State.setVar(varname,value);
  126. return true;
  127. },
  128. });
  129. },
  130. });