1
0

func.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. setup.qsp_func_statistics ??= {};
  2. setup.qsp_callStack ??= [];
  3. setup.qsp_func = (passage,...args) => {
  4. passage = passage.toLowerCase();
  5. let overwrittenResult = setup.Overwrite.func(passage,args);
  6. if(overwrittenResult !== undefined)
  7. return overwrittenResult;
  8. const startTime = (new Date()).getTime();
  9. const old$Result = QSP.$result[0];
  10. const oldResult = QSP.result[0];
  11. State.variables.ARGSstack.push(args);
  12. QSP.$result[0] = "";
  13. QSP.result[0] = 0;
  14. setup.qsp_callStack.push({type:"func",passage:passage,args:args});
  15. const oldScope = setup.qsp_performAnal_current;
  16. const myScope = {type:'func',destination:passage,args:clone(args),entries: []};
  17. oldScope.entries.push(myScope);
  18. setup.qsp_performAnal_current = myScope;
  19. try{
  20. $.wiki(`<<include '${passage}'>>`);
  21. }
  22. catch(e){
  23. console.warn("FUNC ERROR",passage,e);
  24. }
  25. finally{
  26. setup.qsp_performAnal_current = oldScope;
  27. State.variables.ARGSstack.pop();
  28. setup.qsp_callStack.pop();
  29. }
  30. const $result = QSP.$result[0];
  31. const result = QSP.result[0];
  32. QSP.$result[0] = old$Result;
  33. QSP.result[0] = oldResult;
  34. const endTime = (new Date()).getTime();
  35. const executionTime = endTime - startTime;
  36. myScope.executionTime = executionTime;
  37. if(setup.userStyle?.logFUNC){
  38. setup.qsp_func_statistics[passage] ??= [];
  39. setup.qsp_func_statistics[passage].push({
  40. executionTime: executionTime,
  41. args: args,
  42. callStack: clone(setup.qsp_callStack),
  43. });
  44. }
  45. if(result){
  46. if(setup.userStyle?.logFUNC)
  47. console.log("FUNC", passage, args, result);
  48. return result;
  49. }
  50. if(setup.userStyle?.logFUNC)
  51. console.log("$FUNC", passage,args, $result);
  52. return $result;
  53. }