runtime.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef _RUNTIME_H_
  2. #define _RUNTIME_H_
  3. #include "console/console.h"
  4. namespace Con
  5. {
  6. struct EvalResult
  7. {
  8. bool valid;
  9. ConsoleValue value;
  10. String error;
  11. public:
  12. EvalResult() { valid = false; error = ""; }
  13. EvalResult(ConsoleValue pValue)
  14. {
  15. valid = true;
  16. value = (pValue);
  17. }
  18. EvalResult(String errorMessage)
  19. {
  20. valid = false;
  21. error = errorMessage;
  22. }
  23. };
  24. struct Error
  25. {
  26. const char* message;
  27. };
  28. struct SyntaxError : Error {};
  29. class Stack
  30. {
  31. class Frame
  32. {
  33. public:
  34. ConsoleValue* lookup(const char*);
  35. };
  36. public:
  37. S32 getDepth();
  38. Frame getFrame(S32 idx);
  39. };
  40. class Runtime
  41. {
  42. private:
  43. Stack mStack;
  44. public:
  45. virtual ~Runtime() = default;
  46. virtual Stack getStack() { return mStack; }
  47. /// Convert from a relative script path to an absolute script path.
  48. ///
  49. /// This is used in (among other places) the exec() script function, which
  50. /// takes a parameter indicating a script file and executes it. Script paths
  51. /// can be one of:
  52. /// - <b>Absolute:</b> <i>fps/foo/bar.tscript</i> Paths of this sort are passed
  53. /// through.
  54. /// - <b>Mod-relative:</b> <i>~/foo/bar.tscript</i> Paths of this sort have their
  55. /// replaced with the name of the current mod.
  56. /// - <b>File-relative:</b> <i>./baz/blip.tscript</i> Paths of this sort are
  57. /// calculated relative to the path of the current scripting file.
  58. ///
  59. /// @note This function determines paths relative to the currently executing
  60. /// CodeBlock. Calling it outside of script execution will result in
  61. /// it directly copying src to filename, since it won't know to what the
  62. /// path is relative!
  63. ///
  64. /// @param filename Pointer to string buffer to fill with absolute path.
  65. /// @param size Size of buffer pointed to by filename.
  66. /// @param src Original, possibly relative script path.
  67. bool expandScriptFilename(char *filename, U32 size, const char *src);
  68. bool expandToolScriptFilename(char *filename, U32 size, const char *src);
  69. bool collapseScriptFilename(char *filename, U32 size, const char *src);
  70. virtual void expandEscapedCharacters(char* dest, const char* src) = 0;
  71. virtual bool collapseEscapedCharacters(char* buf) = 0;
  72. /// Evaluate an arbitrary chunk of code.
  73. ///
  74. /// @param string Buffer containing code to execute.
  75. /// @param echo Should we echo the string to the console?
  76. /// @param fileName Indicate what file this code is coming from; used in error reporting and such.
  77. /// NOTE: This function restores the console stack on return.
  78. virtual EvalResult evaluate(const char* string, bool echo = false, const char *fileName = NULL) = 0;
  79. virtual EvalResult evaluate(const char* string, S32 frame, bool echo = false, const char *fileName = NULL) = 0;
  80. /// Evaluate an arbitrary line of script.
  81. ///
  82. /// This wraps dVsprintf(), so you can substitute parameters into the code being executed.
  83. /// NOTE: This function restores the console stack on return.
  84. virtual EvalResult evaluatef(const char* string, ...) = 0;
  85. /// Executes a script file and compiles it for use in script.
  86. ///
  87. /// @param fileName File name that is the script to be executed and compiled.
  88. /// @param noCalls Deprecated
  89. /// @param journalScript Deprecated
  90. ///
  91. /// @return True if the script was successfully executed, false if not.
  92. virtual bool executeFile(const char* fileName, bool noCalls, bool journalScript) = 0;
  93. };
  94. }
  95. #endif