1
0

JUMPMARKER.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. setup.console = window.console;
  2. Macro.add('JUMPMARKER', {
  3. skipArgs : false,
  4. handler : function () {
  5. try{
  6. //setup.console.log("JM", this);
  7. const markerId = this.args[0];
  8. const myCommand = this.source;
  9. let me = this;
  10. while(me.parent != null && me.parent.parser.source.indexOf(myCommand) >= 0)
  11. me = me.parent;
  12. const completeContext = me.parser.source;
  13. let codeAfterJumpMarker = completeContext.substring(completeContext.indexOf(myCommand)+myCommand.length);
  14. const countOpeningIfs = (codeAfterJumpMarker.match(/<<if\s+/g) || []).length;
  15. const countClosingIfs = (codeAfterJumpMarker.match(/<<\/if>>/g) || []).length;
  16. codeAfterJumpMarker = "<<if true>>".repeat(countClosingIfs-countOpeningIfs)+ codeAfterJumpMarker;
  17. State.temporary.jumpmarkers ??= {};
  18. State.temporary.jumpmarkers[markerId] = codeAfterJumpMarker;
  19. console.log("JUMPMARKER",markerId, State.temporary.jumpmarkers[markerId]);
  20. }
  21. catch (ex) {
  22. return this.error('ERROR in JUMPMARKER-widget: ' + ex.message);
  23. }
  24. }
  25. });
  26. setup.qsp_jump_statistics ??= {};
  27. Macro.add('JUMP', {
  28. skipArgs : false,
  29. handler : function () {
  30. try{
  31. const startTime = (new Date()).getTime();
  32. const markerId = this.args[0];
  33. const jumpId = this.args[1]; //Required to correctly identify ourselves
  34. let codeToExecute = "";
  35. if(!State.temporary.jumpmarkers[markerId]){
  36. //The Jumpmarker is not known. This means that it might appear later in the code. Good thing we don't have a way to skip to there >:-(
  37. console.warn('Unreckognized Jump Marker: "'+markerId+'"');
  38. return;
  39. }
  40. codeToExecute = State.temporary.jumpmarkers[markerId];
  41. if(setup.userStyle?.logJUMP)
  42. console.log("JUMP",markerId,jumpId,codeToExecute,clone(State.temporary.jumpmarkers));
  43. const myCommand = this.source;
  44. const positionOfThisMacro = codeToExecute.indexOf(myCommand);
  45. if(positionOfThisMacro == -1){
  46. console.warn("JUMPING ahead is not allowed! Create an if-statement instead!", markerId);
  47. return;
  48. }
  49. codeToExecute = codeToExecute.substring(0,positionOfThisMacro+myCommand.length); // Cut off anything after the jump-command
  50. const countOpeningIfs = (codeToExecute.match(/<<if\s+/g) || []).length;
  51. const countClosingIfs = (codeToExecute.match(/<<\/if>>/g) || []).length;
  52. codeToExecute += "<</if>>".repeat(countOpeningIfs-countClosingIfs);
  53. //console.log("JUMP",markerId,jumpId,codeToExecute);
  54. $(this.output).wiki(codeToExecute);
  55. const endTime = (new Date()).getTime();
  56. const executionTime = endTime - startTime;
  57. const statisticsId = `${markerId}_${jumpId}`;
  58. if(setup.userStyle?.logJUMP){
  59. setup.qsp_jump_statistics[statisticsId] ??= [];
  60. setup.qsp_jump_statistics[statisticsId].push({executionTime: executionTime,callStack: clone(setup.qsp_callStack)});
  61. }
  62. }
  63. catch (ex) {
  64. return this.error('ERROR in JUMP-widget: ' + ex.message);
  65. }
  66. }
  67. });