Initialization.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* SpiderMonkey initialization and shutdown APIs. */
  6. #ifndef js_Initialization_h
  7. #define js_Initialization_h
  8. #include "jstypes.h"
  9. namespace JS {
  10. namespace detail {
  11. enum class InitState { Uninitialized = 0, Running, ShutDown };
  12. /**
  13. * SpiderMonkey's initialization status is tracked here, and it controls things
  14. * that should happen only once across all runtimes. It's an API requirement
  15. * that JS_Init (and JS_ShutDown, if called) be called in a thread-aware
  16. * manner, so this (internal -- embedders, don't use!) variable doesn't need to
  17. * be atomic.
  18. */
  19. extern JS_PUBLIC_DATA(InitState)
  20. libraryInitState;
  21. extern JS_PUBLIC_API(const char*)
  22. InitWithFailureDiagnostic(bool isDebugBuild);
  23. } // namespace detail
  24. } // namespace JS
  25. // These are equivalent to ICU's |UMemAllocFn|, |UMemReallocFn|, and
  26. // |UMemFreeFn| types. The first argument (called |context| in the ICU docs)
  27. // will always be nullptr and should be ignored.
  28. typedef void* (*JS_ICUAllocFn)(const void*, size_t size);
  29. typedef void* (*JS_ICUReallocFn)(const void*, void* p, size_t size);
  30. typedef void (*JS_ICUFreeFn)(const void*, void* p);
  31. /**
  32. * This function can be used to track memory used by ICU. If it is called, it
  33. * *must* be called before JS_Init. Don't use it unless you know what you're
  34. * doing!
  35. */
  36. extern JS_PUBLIC_API(bool)
  37. JS_SetICUMemoryFunctions(JS_ICUAllocFn allocFn,
  38. JS_ICUReallocFn reallocFn,
  39. JS_ICUFreeFn freeFn);
  40. /**
  41. * Initialize SpiderMonkey, returning true only if initialization succeeded.
  42. * Once this method has succeeded, it is safe to call JS_NewRuntime and other
  43. * JSAPI methods.
  44. *
  45. * This method must be called before any other JSAPI method is used on any
  46. * thread. Once it has been used, it is safe to call any JSAPI method, and it
  47. * remains safe to do so until JS_ShutDown is correctly called.
  48. *
  49. * It is currently not possible to initialize SpiderMonkey multiple times (that
  50. * is, calling JS_Init/JSAPI methods/JS_ShutDown in that order, then doing so
  51. * again). This restriction may eventually be lifted.
  52. */
  53. inline bool
  54. JS_Init(void)
  55. {
  56. #ifdef DEBUG
  57. return !JS::detail::InitWithFailureDiagnostic(true);
  58. #else
  59. return !JS::detail::InitWithFailureDiagnostic(false);
  60. #endif
  61. }
  62. /**
  63. * A variant of JS_Init. On success it returns nullptr. On failure it returns a
  64. * pointer to a string literal that describes how initialization failed, which
  65. * can be useful for debugging purposes.
  66. */
  67. inline const char*
  68. JS_InitWithFailureDiagnostic(void)
  69. {
  70. #ifdef DEBUG
  71. return JS::detail::InitWithFailureDiagnostic(true);
  72. #else
  73. return JS::detail::InitWithFailureDiagnostic(false);
  74. #endif
  75. }
  76. /*
  77. * Returns true if SpiderMonkey has been initialized successfully, even if it has
  78. * possibly been shut down.
  79. *
  80. * Note that it is the responsibility of the embedder to call JS_Init() and
  81. * JS_ShutDown() at the correct times, and therefore this API should ideally not
  82. * be necessary to use. This is only intended to be used in cases where the
  83. * embedder isn't in full control of deciding whether to initialize SpiderMonkey
  84. * or hand off the task to another consumer.
  85. */
  86. inline bool
  87. JS_IsInitialized(void)
  88. {
  89. return JS::detail::libraryInitState != JS::detail::InitState::Uninitialized;
  90. }
  91. /**
  92. * Destroy free-standing resources allocated by SpiderMonkey, not associated
  93. * with any runtime, context, or other structure.
  94. *
  95. * This method should be called after all other JSAPI data has been properly
  96. * cleaned up: every new runtime must have been destroyed, every new context
  97. * must have been destroyed, and so on. Calling this method before all other
  98. * resources have been destroyed has undefined behavior.
  99. *
  100. * Failure to call this method, at present, has no adverse effects other than
  101. * leaking memory. This may not always be the case; it's recommended that all
  102. * embedders call this method when all other JSAPI operations have completed.
  103. *
  104. * It is currently not possible to initialize SpiderMonkey multiple times (that
  105. * is, calling JS_Init/JSAPI methods/JS_ShutDown in that order, then doing so
  106. * again). This restriction may eventually be lifted.
  107. */
  108. extern JS_PUBLIC_API(void)
  109. JS_ShutDown(void);
  110. #endif /* js_Initialization_h */