2
0

GS.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. setup.qsp_gs_statistics ??= {};
  2. setup.qsp_performAnal ??= {entries: []};
  3. setup.qsp_performAnal_current ??= setup.qsp_performAnal;
  4. setup.qsp_gs = function(output,...args){
  5. const startTime = (new Date()).getTime();
  6. let destination = '';
  7. let destinationArgs = [];
  8. if(typeof args[0] == 'string'){
  9. destination = args[0];
  10. destinationArgs = args.slice(1);
  11. }else if(Array.isArray(args[0])){
  12. destination = args[0][0];
  13. destinationArgs = args[0].slice(1);
  14. }else{
  15. console.error(`Argument not reckognized in setup.gs()`,args[0]);
  16. }
  17. if(setup.Overwrite.gs(destination.toLowerCase(),destinationArgs) !== undefined)
  18. return;
  19. const oldScope = setup.qsp_performAnal_current;
  20. const myScope = {type:'gs',destination:destination,args:clone(destinationArgs),entries: []};
  21. oldScope.entries.push(myScope);
  22. setup.qsp_performAnal_current = myScope;
  23. State.variables.ARGSstack.push(destinationArgs);
  24. if(setup.userStyle?.logGS)
  25. console.log("GS",args,destination,destinationArgs,clone(State.variables.ARGSstack));
  26. setup.qsp_callStack.push({type:"gs",passage:destination,args:destinationArgs});
  27. var code = `<<include '${destination.toLowerCase()}'>>`;
  28. try{
  29. if(output)
  30. $(output).wiki(code);
  31. else if($(".passage").length)
  32. $(".passage").wiki(code);
  33. else{
  34. $.wiki(code);
  35. }
  36. }
  37. catch(e){
  38. console.error(e);
  39. }
  40. finally{
  41. setup.qsp_performAnal_current = oldScope;
  42. State.variables.ARGSstack.pop();
  43. setup.qsp_callStack.pop();
  44. window.SweetCube.globalBreak = false;
  45. }
  46. setup.updateActions();
  47. const endTime = (new Date()).getTime();
  48. const executionTime = endTime - startTime;
  49. myScope.executionTime = executionTime;
  50. if(setup.userStyle?.logGS){
  51. setup.qsp_gs_statistics[destination.toLowerCase()] ??= [];
  52. setup.qsp_gs_statistics[destination.toLowerCase()].push({executionTime: executionTime,args:destinationArgs,callStack: clone(setup.qsp_callStack)});
  53. }
  54. }
  55. Macro.add('gs', {
  56. skipArgs : false,
  57. handler : function () {
  58. try{
  59. setup.qsp_gs(this.output,...this.args[0]);
  60. }
  61. catch (ex) {
  62. return this.error('ERROR in gs-widget: ' + ex.message);
  63. }
  64. }
  65. });