@elurjs/android 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/README.md +10 -2
  2. package/index.mjs +11 -0
  3. package/package.json +29 -4
  4. package/template/.gitkeep +0 -0
  5. package/template/app/build.gradle.kts +192 -0
  6. package/template/app/proguard-rules-elur.pro +12 -0
  7. package/template/app/proguard-rules.pro +3 -0
  8. package/template/app/src/main/AndroidManifest.xml +81 -0
  9. package/template/app/src/main/assets/oss-notices.txt +153 -0
  10. package/template/app/src/main/assets/tck_pixel.png +0 -0
  11. package/template/app/src/main/cpp/CMakeLists.txt +117 -0
  12. package/template/app/src/main/cpp/dlEncoder.h +117 -0
  13. package/template/app/src/main/cpp/dlEncoderJsi.cpp +315 -0
  14. package/template/app/src/main/cpp/dlEncoderQjs.cpp +394 -0
  15. package/template/app/src/main/cpp/dlOps.inc +145 -0
  16. package/template/app/src/main/cpp/elurBridge.cpp +1350 -0
  17. package/template/app/src/main/cpp/elurBridgeHermes.cpp +1294 -0
  18. package/template/app/src/main/cpp/elurSkia.cpp +287 -0
  19. package/template/app/src/main/cpp/elurSkiaTree.cpp +448 -0
  20. package/template/app/src/main/cpp/elurUiWorklet.cpp +1233 -0
  21. package/template/app/src/main/cpp/elurYoga.cpp +1060 -0
  22. package/template/app/src/main/cpp/elurYoga.h +82 -0
  23. package/template/app/src/main/cpp/hermes-libs/LICENSE +21 -0
  24. package/template/app/src/main/cpp/hermes-libs/arm64-v8a/libhermesvm.so +0 -0
  25. package/template/app/src/main/cpp/hermes-libs/include/hermes/AsyncDebuggerAPI.h +276 -0
  26. package/template/app/src/main/cpp/hermes-libs/include/hermes/CompileJS.h +97 -0
  27. package/template/app/src/main/cpp/hermes-libs/include/hermes/DebuggerAPI.h +502 -0
  28. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/CrashManager.h +107 -0
  29. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/CtorConfig.h +145 -0
  30. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/DebuggerTypes.h +193 -0
  31. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/GCConfig.h +211 -0
  32. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/GCTripwireContext.h +40 -0
  33. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/HermesExport.h +14 -0
  34. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/JSOutOfMemoryError.h +27 -0
  35. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/RuntimeConfig.h +141 -0
  36. package/template/app/src/main/cpp/hermes-libs/include/hermes/Public/SamplingProfiler.h +270 -0
  37. package/template/app/src/main/cpp/hermes-libs/include/hermes/RuntimeTaskRunner.h +64 -0
  38. package/template/app/src/main/cpp/hermes-libs/include/hermes/SynthTrace.h +1566 -0
  39. package/template/app/src/main/cpp/hermes-libs/include/hermes/SynthTraceParser.h +37 -0
  40. package/template/app/src/main/cpp/hermes-libs/include/hermes/ThreadSafetyAnalysis.h +75 -0
  41. package/template/app/src/main/cpp/hermes-libs/include/hermes/TimerStats.h +23 -0
  42. package/template/app/src/main/cpp/hermes-libs/include/hermes/TraceInterpreter.h +334 -0
  43. package/template/app/src/main/cpp/hermes-libs/include/hermes/TracingRuntime.h +328 -0
  44. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/CDPAgent.h +131 -0
  45. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/CDPDebugAPI.h +73 -0
  46. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/CallbackOStream.h +89 -0
  47. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/ConsoleMessage.h +135 -0
  48. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/DebuggerDomainAgent.h +280 -0
  49. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/DebuggerDomainCoordinator.h +137 -0
  50. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/DomainAgent.h +107 -0
  51. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/DomainState.h +140 -0
  52. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/HeapProfilerDomainAgent.h +71 -0
  53. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/JSONValueInterfaces.h +40 -0
  54. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/MessageConverters.h +82 -0
  55. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/MessageInterfaces.h +72 -0
  56. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/MessageTypes.h +1279 -0
  57. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/MessageTypesInlines.h +313 -0
  58. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/ProfilerDomainAgent.h +38 -0
  59. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/RemoteObjectConverters.h +77 -0
  60. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/RemoteObjectsTable.h +127 -0
  61. package/template/app/src/main/cpp/hermes-libs/include/hermes/cdp/RuntimeDomainAgent.h +138 -0
  62. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/Dummy.h +24 -0
  63. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/Extensions.h +20 -0
  64. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/ExtensionsBytecode.h +23 -0
  65. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/Intrinsics.h +44 -0
  66. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/JSIUtils.h +123 -0
  67. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/TextEncoder.h +29 -0
  68. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/contrib/ContribDummy.h +24 -0
  69. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/contrib/ContribExtensions.h +23 -0
  70. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/contrib/TextDecoder.h +30 -0
  71. package/template/app/src/main/cpp/hermes-libs/include/hermes/extensions/contrib/TextDecoderUtils.h +113 -0
  72. package/template/app/src/main/cpp/hermes-libs/include/hermes/hermes.h +242 -0
  73. package/template/app/src/main/cpp/hermes-libs/include/hermes/hermes_tracing.h +52 -0
  74. package/template/app/src/main/cpp/hermes-libs/include/hermes_abi/HermesABIHelpers.h +295 -0
  75. package/template/app/src/main/cpp/hermes-libs/include/hermes_abi/HermesABIRuntimeWrapper.h +25 -0
  76. package/template/app/src/main/cpp/hermes-libs/include/hermes_abi/hermes_abi.h +642 -0
  77. package/template/app/src/main/cpp/hermes-libs/include/hermes_abi/hermes_vtable.h +29 -0
  78. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/HermesSandboxRuntime.h +41 -0
  79. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/hermes_sandbox_impl_compiled.h +12 -0
  80. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/hermes_sandbox_impl_dbg_compiled-impl.h +8516 -0
  81. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/hermes_sandbox_impl_dbg_compiled.h +137 -0
  82. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/hermes_sandbox_impl_opt_compiled-impl.h +3945 -0
  83. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/hermes_sandbox_impl_opt_compiled.h +119 -0
  84. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/wasm-rt-fb.h +124 -0
  85. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/wasm-rt-impl.h +66 -0
  86. package/template/app/src/main/cpp/hermes-libs/include/hermes_sandbox/external/wasm-rt.h +456 -0
  87. package/template/app/src/main/cpp/hermes-libs/include/jsi/JSIDynamic.h +26 -0
  88. package/template/app/src/main/cpp/hermes-libs/include/jsi/decorator.h +1157 -0
  89. package/template/app/src/main/cpp/hermes-libs/include/jsi/hermes-interfaces.h +242 -0
  90. package/template/app/src/main/cpp/hermes-libs/include/jsi/instrumentation.h +132 -0
  91. package/template/app/src/main/cpp/hermes-libs/include/jsi/jsi-inl.h +434 -0
  92. package/template/app/src/main/cpp/hermes-libs/include/jsi/jsi.h +2266 -0
  93. package/template/app/src/main/cpp/hermes-libs/include/jsi/jsilib.h +59 -0
  94. package/template/app/src/main/cpp/hermes-libs/include/jsi/threadsafe.h +79 -0
  95. package/template/app/src/main/cpp/hermes-libs/jsi-impl/jsi.cpp +1111 -0
  96. package/template/app/src/main/cpp/hermes-libs/x86_64/libhermesvm.so +0 -0
  97. package/template/app/src/main/java/com/elur/universal/BinaryCodec.kt +121 -0
  98. package/template/app/src/main/java/com/elur/universal/DlDecoder.kt +260 -0
  99. package/template/app/src/main/java/com/elur/universal/DlOpsDecode.kt +158 -0
  100. package/template/app/src/main/java/com/elur/universal/ElurBoxDrawable.kt +161 -0
  101. package/template/app/src/main/java/com/elur/universal/ElurBridge.kt +238 -0
  102. package/template/app/src/main/java/com/elur/universal/ElurBridgeHermes.kt +223 -0
  103. package/template/app/src/main/java/com/elur/universal/ElurCanvasView.kt +202 -0
  104. package/template/app/src/main/java/com/elur/universal/ElurCryptoModule.kt +206 -0
  105. package/template/app/src/main/java/com/elur/universal/ElurCustomLayout.kt +185 -0
  106. package/template/app/src/main/java/com/elur/universal/ElurEditText.kt +17 -0
  107. package/template/app/src/main/java/com/elur/universal/ElurFontRegistry.kt +190 -0
  108. package/template/app/src/main/java/com/elur/universal/ElurHttpModule.kt +306 -0
  109. package/template/app/src/main/java/com/elur/universal/ElurImageService.kt +348 -0
  110. package/template/app/src/main/java/com/elur/universal/ElurLicensesActivity.kt +84 -0
  111. package/template/app/src/main/java/com/elur/universal/ElurListView.kt +954 -0
  112. package/template/app/src/main/java/com/elur/universal/ElurManifest.kt +117 -0
  113. package/template/app/src/main/java/com/elur/universal/ElurMetrics.kt +325 -0
  114. package/template/app/src/main/java/com/elur/universal/ElurModuleRegistrar.kt +14 -0
  115. package/template/app/src/main/java/com/elur/universal/ElurPermissionGate.kt +36 -0
  116. package/template/app/src/main/java/com/elur/universal/ElurRenderer.kt +6164 -0
  117. package/template/app/src/main/java/com/elur/universal/ElurRowYoga.kt +130 -0
  118. package/template/app/src/main/java/com/elur/universal/ElurScrollView.kt +240 -0
  119. package/template/app/src/main/java/com/elur/universal/ElurShellView.kt +117 -0
  120. package/template/app/src/main/java/com/elur/universal/ElurSkiaCanvasView.kt +162 -0
  121. package/template/app/src/main/java/com/elur/universal/ElurSkiaTreeRenderer.kt +968 -0
  122. package/template/app/src/main/java/com/elur/universal/ElurStateModule.kt +69 -0
  123. package/template/app/src/main/java/com/elur/universal/ElurSwipeDriver.kt +34 -0
  124. package/template/app/src/main/java/com/elur/universal/ElurTckModule.kt +99 -0
  125. package/template/app/src/main/java/com/elur/universal/ElurTextMeasure.kt +16 -0
  126. package/template/app/src/main/java/com/elur/universal/ElurTrace.kt +24 -0
  127. package/template/app/src/main/java/com/elur/universal/ElurUiModule.kt +48 -0
  128. package/template/app/src/main/java/com/elur/universal/ElurUiWorklet.kt +305 -0
  129. package/template/app/src/main/java/com/elur/universal/JsBridge.kt +67 -0
  130. package/template/app/src/main/java/com/elur/universal/JsThread.kt +189 -0
  131. package/template/app/src/main/java/com/elur/universal/MainActivity.kt +844 -0
  132. package/template/app/src/main/java/com/elur/universal/ModuleRegistry.kt +701 -0
  133. package/template/app/src/main/java/com/elur/universal/YogaRowScope.kt +97 -0
  134. package/template/app/src/main/res/values/ids.xml +10 -0
  135. package/template/app/src/main/res/values/strings.xml +8 -0
  136. package/template/app/src/main/res/values-es/strings.xml +5 -0
  137. package/template/app/src/main/res/xml/network_security_config.xml +10 -0
  138. package/template/build.gradle.kts +5 -0
  139. package/template/gradle/libs.versions.toml +12 -0
  140. package/template/gradle/wrapper/gradle-wrapper.jar +0 -0
  141. package/template/gradle/wrapper/gradle-wrapper.properties +8 -0
  142. package/template/gradle.properties +23 -0
  143. package/template/gradlew +251 -0
  144. package/template/gradlew.bat +94 -0
  145. package/template/settings.gradle.kts +32 -0
@@ -0,0 +1,82 @@
1
+ // elurYoga.h — árbol Yoga único en C++ (P1-TREE, ADR-0018)
2
+ // Fuente de verdad del layout: YGNodeRef por id, mantenido por los bridges
3
+ // observando el stream de ops en flushOps. calculateLayout corre en el
4
+ // hilo JS (fuera del UI thread); los resultados se emiten como ops
5
+ // `layout` (ABI 17) que Kotlin aplica — y el path Skia consumirá nativo.
6
+ // Engine-agnostic: solo JNI + Yoga C API.
7
+
8
+ #pragma once
9
+
10
+ #include "dlEncoder.h"
11
+ #include <jni.h>
12
+ #include <string>
13
+
14
+ namespace elur {
15
+
16
+ // Inicializa el módulo (llamado una vez por init del bridge).
17
+ void yogaInit(JavaVM *jvm, jobject renderer);
18
+
19
+ // Libera todo el árbol y el renderer (reset entre inits).
20
+ void yogaReset();
21
+
22
+ // Mirror de ops del Display List sobre el árbol.
23
+ void yogaCreateNode(int id, const std::string &type);
24
+ void yogaInsertChild(int parentId, int childId, int index);
25
+ void yogaRemoveChild(int parentId, int childId);
26
+ void yogaMoveChild(int parentId, int childId, int index);
27
+ void yogaDeleteNode(int id);
28
+ // Re-medida post-apply (Kotlin → nativeMarkLeavesDirty): el contenido
29
+ // de update-text/update-props aterriza en la View DESPUÉS del layout
30
+ // del batch — sin este segundo dirty la hoja conserva la medida vacía
31
+ // (h≈0) cacheada para siempre. No-op en nodos sin measureFunc.
32
+ void yogaMarkLeafDirty(int id);
33
+ // style entry: value es num en unidades LÓGICAS si isNum (ADR-0120 —
34
+ // sin ×density), o string a resolver. Los ops `layout` emitidos llevan
35
+ // coordenadas lógicas: applyLayout convierte a px en Kotlin.
36
+ void yogaSetStyle(int id, const std::string &key, double num,
37
+ const std::string &str, bool isNum);
38
+
39
+ // Viewport reportado por Kotlin (px reales) + dirección ambiente
40
+ // (layoutDirection RTL — force_rtl, locale árabe/hebreo). Marca dirty
41
+ // y, si hay cambios pendientes, calcula y emite layout ops directo.
42
+ void yogaSetViewport(float w, float h, float density, bool rtl);
43
+
44
+ // Si el árbol está dirty y hay viewport: calculateLayout y append de
45
+ // ops `layout` al buffer. Devuelve cuántos ops emitió.
46
+ int yogaLayoutIfDirty(DLBuf &out);
47
+
48
+ // ADR-0121: árboles Yoga de fila para ElurListView — la plantilla se
49
+ // registra al inflar el holder y sus hojas miden la View reciclada real
50
+ // vía ElurRowYoga.measureRowNode. Estilos en unidades lógicas; rects
51
+ // devueltos en px (la conversión vive en C++).
52
+ int yogaRowTreeCreate(JNIEnv *env, jobject rowYoga);
53
+ void yogaRowNode(int treeId, int key, int parentKey, const std::string &type);
54
+ void yogaRowStyle(int treeId, int key, const std::string &name, bool isNum,
55
+ double num, const std::string &str);
56
+ void yogaRowStyleReset(int treeId, int key, const std::string &name);
57
+ jintArray yogaRowLayout(JNIEnv *env, int treeId, float wPx, bool forceWidth);
58
+ void yogaRowFree(int treeId);
59
+
60
+ // ADR-0122: delegado de layout por subárbol. `layout` prop marca el
61
+ // nodo (los hijos siguen adjuntos — Yoga 3.2.1 prohíbe measure func en
62
+ // nodos con hijos): post-pasada, ElurRenderer.customMeasure fija las
63
+ // dims auto (SetWidth/Height + recálculo) y customLayout aporta los
64
+ // rects de hijos → g_customRects. yogaMeasureChild corre
65
+ // CalculateLayout del subárbol de un hijo al ancho que el delegado da.
66
+ void yogaSetCustomLayout(int id, bool on);
67
+ bool yogaMeasureChild(int id, float wPx, float hPx, float out[2]);
68
+
69
+ // Nodos vivos del árbol (g_nodes.size()) — métrica de registro real.
70
+ // SOLO llamarlo en el JsThread: g_nodes no tiene sincronización, una
71
+ // lectura concurrente desde main es data race (V13/ADR-0111).
72
+ int yogaNodeCount();
73
+
74
+ // Emite los layout ops calculados como un frame propio al renderer.
75
+ // R4.4/ADR-0085: reqSeq > 0 liga la respuesta al requestLeafRemeasure
76
+ // #reqSeq del renderer (id de frame = -reqSeq — respuestas con seq
77
+ // superado por un request más nuevo se descartan como stale).
78
+ // reqSeq <= 0: frame host-initiated sin request (resize de viewport) —
79
+ // toma un id del contador interno (base 1e6, nunca colisiona).
80
+ void yogaFlushLayoutFrame(int reqSeq);
81
+
82
+ } // namespace elur
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,276 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #ifdef HERMES_ENABLE_DEBUGGER
11
+
12
+ #include <condition_variable>
13
+ #include <mutex>
14
+ #include <optional>
15
+ #include <queue>
16
+ #include <thread>
17
+
18
+ #include <hermes/DebuggerAPI.h>
19
+ #include <hermes/Public/HermesExport.h>
20
+ #include <hermes/hermes.h>
21
+
22
+ #if defined(__clang__) && (!defined(SWIG)) && defined(_LIBCPP_VERSION) && \
23
+ defined(_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS)
24
+ #include <hermes/ThreadSafetyAnalysis.h>
25
+ #else
26
+ #ifndef TSA_GUARDED_BY
27
+ #define TSA_GUARDED_BY(x)
28
+ #endif
29
+ #ifndef TSA_NO_THREAD_SAFETY_ANALYSIS
30
+ #define TSA_NO_THREAD_SAFETY_ANALYSIS
31
+ #endif
32
+ #endif
33
+
34
+ namespace facebook {
35
+ namespace hermes {
36
+ namespace debugger {
37
+
38
+ class AsyncDebuggerAPI;
39
+
40
+ enum class DebuggerEventType {
41
+ // Informational Events
42
+ ScriptLoaded, /// A script file was loaded, and the debugger has requested
43
+ /// pausing after script load.
44
+ Exception, /// An Exception was thrown.
45
+ Resumed, /// Script execution has resumed.
46
+
47
+ // Events Requiring Next Command
48
+ DebuggerStatement, /// A debugger; statement was hit.
49
+ Breakpoint, /// A breakpoint was hit.
50
+ StepFinish, /// A Step operation completed.
51
+ ExplicitPause, /// A pause requested using Explicit AsyncBreak
52
+ };
53
+
54
+ /// This represents the list of possible commands that can be given to
55
+ /// \p resumeFromPaused. This is used instead of DebuggerAPI's Command class in
56
+ /// order to prevent callers from constructing an eval Command. The eval
57
+ /// functionality is implemented as a separate mechansim with
58
+ /// \p evalWhilePaused.
59
+ enum class AsyncDebugCommand {
60
+ Continue, /// Continues execution
61
+ StepInto, /// Perform a step into and then pause again
62
+ StepOver, /// Steps over the current instruction and then pause again
63
+ StepOut, /// Step out from the current scope and then pause again
64
+ };
65
+
66
+ using DebuggerEventCallback = std::function<void(
67
+ HermesRuntime &runtime,
68
+ AsyncDebuggerAPI &asyncDebugger,
69
+ DebuggerEventType event)>;
70
+ using InterruptCallback = std::function<void(HermesRuntime &runtime)>;
71
+ using EvalCompleteCallback = std::function<
72
+ void(HermesRuntime &runtime, const debugger::EvalResult &result)>;
73
+
74
+ /// This class wraps the DebuggerAPI to expose an asynchronous didPause
75
+ /// functionality as well as an interrupt API. This class must be constructed at
76
+ /// the same time as HermesRuntime.
77
+ ///
78
+ /// Functions in this class with the suffix "_TS" (Thread-Safe) are the only
79
+ /// functions that are safe to call on any thread. All other functions must be
80
+ /// called on the runtime thread.
81
+ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
82
+ public:
83
+ /// Constructs an AsyncDebuggerAPI for use with the provided HermesRuntime.
84
+ /// This should be called and created at the same time as creating
85
+ /// HermesRuntime.
86
+ explicit AsyncDebuggerAPI(HermesRuntime &runtime);
87
+
88
+ /// Must be destroyed on the runtime thread or when you're sure nothing is
89
+ /// interacting with the runtime. Must be destroyed before destroying
90
+ /// HermesRuntime.
91
+ ~AsyncDebuggerAPI() override;
92
+
93
+ /// Set a callback function to invoke when the runtime pauses due to various
94
+ /// conditions such as hitting a "debugger;" statement. Can be called from any
95
+ /// thread. If there is no DebuggerEventCallback, then any reason that might
96
+ /// trigger a pause, such as a "debugger;" statement or breakpoints, will not
97
+ /// actually pause and will simply continue execution. Any caller that sets an
98
+ /// event callback cannot just be observing events and never call
99
+ /// \p resumeFromPaused in any of its code paths. The caller must either
100
+ /// expose UI enabling human action for controlling the debugger, or it must
101
+ /// have programmatic logic that controls the debugger via
102
+ /// \p resumeFromPaused.
103
+ ///
104
+ /// The provided callback must be non-empty. Use \p
105
+ /// clearDebuggerEventCallback_TS to clear the callback.
106
+ void setDebuggerEventCallback_TS(DebuggerEventCallback callback);
107
+
108
+ /// Clear the debugger event callback. Can be called from any thread.
109
+ void clearDebuggerEventCallback_TS();
110
+
111
+ /// Whether the runtime is currently paused waiting for the next action.
112
+ /// Should only be called from the runtime thread.
113
+ bool isWaitingForCommand();
114
+
115
+ /// Whether the runtime is currently paused for any reason (e.g. script
116
+ /// parsed, running interrupts, or waiting for a command).
117
+ /// Should only be called from the runtime thread.
118
+ bool isPaused() const;
119
+
120
+ /// Provide the next action to perform. Should only be called from the runtime
121
+ /// thread and only if the next command is expected to be set.
122
+ bool resumeFromPaused(AsyncDebugCommand command);
123
+
124
+ /// Evaluate JavaScript code \p expression in the frame at index
125
+ /// \p frameIndex. Receives evaluation result in the \p callback. Should only
126
+ /// be called from the runtime thread and only if debugger is paused waiting
127
+ /// for the next action.
128
+ bool evalWhilePaused(
129
+ const std::string &expression,
130
+ uint32_t frameIndex,
131
+ EvalCompleteCallback callback);
132
+
133
+ /// Request to interrupt the runtime at a convenient time and get a callback
134
+ /// on the runtime thread. Guaranteed to run "exactly once". This function can
135
+ /// be called from any thread, but cannot be called while inside a
136
+ /// DebuggerEventCallback.
137
+ void triggerInterrupt_TS(InterruptCallback callback);
138
+
139
+ /// EventObserver implementation
140
+ debugger::Command didPause(debugger::Debugger &debugger) override;
141
+
142
+ private:
143
+ /// This function infinite loops and uses \p signal_ to block the runtime
144
+ /// thread. It gets woken up if new InterruptCallback is queued or if
145
+ /// DebuggerEventCallback changes.
146
+ void processInterruptWhilePaused() TSA_NO_THREAD_SAFETY_ANALYSIS;
147
+
148
+ /// Dequeues the next InterruptCallback if any.
149
+ std::optional<InterruptCallback> takeNextInterruptCallback();
150
+
151
+ /// If \p ignoreNextCommand is true, then runs every InterruptCallback that
152
+ /// has been queued up so far. If \p ignoreNextCommand is false, then attempt
153
+ /// to run all interrupts, but will stop if any interrupt sets a next command.
154
+ void runInterrupts(bool ignoreNextCommand = true);
155
+
156
+ /// Runs the DebuggerEventCallback that has been registered (if any).
157
+ void runEventCallback(DebuggerEventType event);
158
+
159
+ HermesRuntime &runtime_;
160
+
161
+ /// Whether the runtime thread is currently paused in \p didPause and needs to
162
+ /// be told what action to take next.
163
+ bool isWaitingForCommand_;
164
+
165
+ /// Stores the command to return from \p didPause.
166
+ debugger::Command nextCommand_;
167
+
168
+ /// Callback function to invoke after getting EvalResult from EvalComplete in
169
+ /// didPause. Used once and then cleared out.
170
+ EvalCompleteCallback oneTimeEvalCompleteCallback_{};
171
+
172
+ /// Tracks whether we are already in a didPause callback to detect recursive
173
+ /// calls to didPause.
174
+ bool inDidPause_ = false;
175
+
176
+ /// The debugger event callback to invoke when the runtime pauses.
177
+ DebuggerEventCallback eventCallback_ TSA_GUARDED_BY(mutex_){};
178
+
179
+ /// Queue of interrupt callback functions to invoke.
180
+ std::queue<InterruptCallback> interruptCallbacks_ TSA_GUARDED_BY(mutex_){};
181
+
182
+ /// Used as a mechanism to block the runtime thread in \p didPause and for
183
+ /// protecting variables used across threads.
184
+ std::mutex mutex_{};
185
+ /// Used to implement \p triggerInterrupt while \p didPause is holding onto
186
+ /// the runtime thread.
187
+ std::condition_variable signal_{};
188
+ };
189
+
190
+ } // namespace debugger
191
+ } // namespace hermes
192
+ } // namespace facebook
193
+
194
+ #else // !HERMES_ENABLE_DEBUGGER
195
+
196
+ #include <hermes/DebuggerAPI.h>
197
+ #include <hermes/Public/HermesExport.h>
198
+ #include <hermes/hermes.h>
199
+
200
+ namespace facebook {
201
+ namespace hermes {
202
+ namespace debugger {
203
+
204
+ class AsyncDebuggerAPI;
205
+
206
+ enum class DebuggerEventType {
207
+ // Informational Events
208
+ ScriptLoaded, /// A script file was loaded, and the debugger has requested
209
+ /// pausing after script load.
210
+ Exception, /// An Exception was thrown.
211
+ Resumed, /// Script execution has resumed.
212
+
213
+ // Events Requiring Next Command
214
+ DebuggerStatement, /// A debugger; statement was hit.
215
+ Breakpoint, /// A breakpoint was hit.
216
+ StepFinish, /// A Step operation completed.
217
+ ExplicitPause, /// A pause requested using Explicit AsyncBreak
218
+ };
219
+
220
+ /// This represents the list of possible commands that can be given to
221
+ /// \p resumeFromPaused. This is used instead of DebuggerAPI's Command class in
222
+ /// order to prevent callers from constructing an eval Command. The eval
223
+ /// functionality is implemented as a separate mechansim with
224
+ /// \p evalWhilePaused.
225
+ enum class AsyncDebugCommand {
226
+ Continue, /// Continues execution
227
+ StepInto, /// Perform a step into and then pause again
228
+ StepOver, /// Steps over the current instruction and then pause again
229
+ StepOut, /// Step out from the current scope and then pause again
230
+ };
231
+
232
+ using DebuggerEventCallback = std::function<void(
233
+ HermesRuntime &runtime,
234
+ AsyncDebuggerAPI &asyncDebugger,
235
+ DebuggerEventType event)>;
236
+ using InterruptCallback = std::function<void(HermesRuntime &runtime)>;
237
+ using EvalCompleteCallback = std::function<
238
+ void(HermesRuntime &runtime, const debugger::EvalResult &result)>;
239
+
240
+ class HERMES_EXPORT AsyncDebuggerAPI {
241
+ public:
242
+ explicit AsyncDebuggerAPI(HermesRuntime &) {}
243
+
244
+ ~AsyncDebuggerAPI() {}
245
+
246
+ void setDebuggerEventCallback_TS(DebuggerEventCallback callback) {}
247
+
248
+ void clearDebuggerEventCallback_TS() {}
249
+
250
+ bool isWaitingForCommand() {
251
+ return false;
252
+ }
253
+
254
+ bool isPaused() const {
255
+ return false;
256
+ }
257
+
258
+ bool resumeFromPaused(AsyncDebugCommand command) {
259
+ return false;
260
+ }
261
+
262
+ bool evalWhilePaused(
263
+ const std::string &expression,
264
+ uint32_t frameIndex,
265
+ EvalCompleteCallback callback) {
266
+ return false;
267
+ }
268
+
269
+ void triggerInterrupt_TS(InterruptCallback callback) {}
270
+ };
271
+
272
+ } // namespace debugger
273
+ } // namespace hermes
274
+ } // namespace facebook
275
+
276
+ #endif // !HERMES_ENABLE_DEBUGGER
@@ -0,0 +1,97 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include <optional>
11
+ #include <string>
12
+ #include <vector>
13
+
14
+ namespace hermes {
15
+
16
+ /// Interface for receiving errors, warnings and notes produced by compileJS.
17
+ class DiagnosticHandler {
18
+ public:
19
+ enum Kind {
20
+ Error,
21
+ Warning,
22
+ Note,
23
+ };
24
+
25
+ struct Diagnostic {
26
+ Kind kind;
27
+ int line; /// 1-based index
28
+ int column; /// 1-based index
29
+ std::string message;
30
+ /// 0-based char indices in half-open intervals
31
+ std::vector<std::pair<unsigned, unsigned>> ranges;
32
+ };
33
+
34
+ /// Called once for each diagnostic message produced during compilation.
35
+ virtual void handle(const Diagnostic &diagnostic) = 0;
36
+ virtual ~DiagnosticHandler() = default;
37
+ };
38
+
39
+ /// Compiles JS source \p str and if compilation is successful, returns true
40
+ /// and outputs to \p bytecode otherwise returns false.
41
+ /// \param sourceURL this will be used as the "file name" of the buffer for
42
+ /// errors, stack traces, etc.
43
+ /// \param optimize this will enable optimizations.
44
+ /// \param emitAsyncBreakCheck this will make the bytecode interruptable.
45
+ /// \param diagHandler if not null, receives any and all errors, warnings and
46
+ /// notes produced during compilation.
47
+ /// \param sourceMapBuf optional source map string.
48
+ /// \param debug Wether to generate debugging information in generated bytecode.
49
+ bool compileJS(
50
+ const std::string &str,
51
+ const std::string &sourceURL,
52
+ std::string &bytecode,
53
+ bool optimize,
54
+ bool emitAsyncBreakCheck,
55
+ DiagnosticHandler *diagHandler,
56
+ std::optional<std::string_view> sourceMapBuf = std::nullopt,
57
+ bool debug = false);
58
+
59
+ bool compileJS(
60
+ const std::string &str,
61
+ std::string &bytecode,
62
+ bool optimize = true);
63
+
64
+ bool compileJS(
65
+ const std::string &str,
66
+ const std::string &sourceURL,
67
+ std::string &bytecode,
68
+ bool optimize = true);
69
+
70
+ /// Options for overload of compileJS that accepts CompileJSOptions.
71
+ struct CompileJSOptions {
72
+ /// If true, the bytecode will be optimized.
73
+ bool optimize{true};
74
+ /// Maximum number of instructions (in addition to parameter handling)
75
+ /// that is allowed for inlining of small functions.
76
+ unsigned inlineMaxSize{50};
77
+ /// If true, the bytecode will be interruptable.
78
+ bool emitAsyncBreakCheck{false};
79
+ /// If true, debugging information will be generated in the bytecode.
80
+ bool debug{false};
81
+ /// Enable ES6 block scoping support.
82
+ bool enableES6BlockScoping{false};
83
+ /// Enable async generators support.
84
+ bool enableAsyncGenerators{false};
85
+ };
86
+
87
+ /// Like the other compileJS overloads, but takes a struct of options with some
88
+ /// additional configurability.
89
+ bool compileJS(
90
+ const std::string &str,
91
+ const std::string &sourceURL,
92
+ std::string &bytecode,
93
+ const CompileJSOptions &options,
94
+ DiagnosticHandler *diagHandler,
95
+ std::optional<std::string_view> sourceMapBuf = std::nullopt);
96
+
97
+ } // namespace hermes