1
0

GS.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  45. setup.updateActions();
  46. const endTime = (new Date()).getTime();
  47. const executionTime = endTime - startTime;
  48. myScope.executionTime = executionTime;
  49. if(setup.userStyle?.logGS){
  50. setup.qsp_gs_statistics[destination.toLowerCase()] ??= [];
  51. setup.qsp_gs_statistics[destination.toLowerCase()].push({executionTime: executionTime,args:destinationArgs,callStack: clone(setup.qsp_callStack)});
  52. }
  53. }
  54. Macro.add('gs', {
  55. skipArgs : false,
  56. handler : function () {
  57. try{
  58. setup.qsp_gs(this.output,...this.args[0]);
  59. }
  60. catch (ex) {
  61. return this.error('ERROR in gs-widget: ' + ex.message);
  62. }
  63. }
  64. });