func.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. try{
  16. $.wiki(`<<include '${passage}'>>`);
  17. }
  18. catch(e){
  19. console.warn("FUNC ERROR",passage,e);
  20. }
  21. finally{
  22. State.variables.ARGSstack.pop();
  23. setup.qsp_callStack.pop();
  24. }
  25. const $result = QSP.$result[0];
  26. const result = QSP.result[0];
  27. QSP.$result[0] = old$Result;
  28. QSP.result[0] = oldResult;
  29. const endTime = (new Date()).getTime();
  30. const executionTime = endTime - startTime;
  31. if(setup.userStyle?.logFUNC){
  32. setup.qsp_func_statistics[passage] ??= [];
  33. setup.qsp_func_statistics[passage].push({
  34. executionTime: executionTime,
  35. args: args,
  36. callStack: clone(setup.qsp_callStack),
  37. });
  38. }
  39. if(result){
  40. if(setup.userStyle?.logFUNC)
  41. console.log("FUNC", passage, args, result);
  42. return result;
  43. }
  44. if(setup.userStyle?.logFUNC)
  45. console.log("$FUNC", passage,args, $result);
  46. return $result;
  47. }