Bladeren bron

refactor: load scene-script builtins from a generated header (#608)

The scene-script JS builtins (localStorage/MediaPlaybackEvent/__intervals
shims) were embedded as an inline R"JS(...)JS" literal in ScriptEngine.cpp.
Move the JS into its own builtins.js resource and embed it at build time via a
CMake-generated header (configure_file), so the script can be edited, linted
and syntax-highlighted on its own. No behaviour change: the embedded string is
byte-identical.

Fixes #592
Karashi 3 maanden geleden
bovenliggende
commit
44dfa51b3c

+ 15 - 0
CMakeLists.txt

@@ -259,7 +259,22 @@ set_target_properties(ceflib
 ADD_LOGICAL_TARGET(libcef_lib "${CEF_LIB_DEBUG}" "${CEF_LIB_RELEASE}")
 PRINT_CEF_CONFIG()
 
+# Embed scene-script builtins (builtins.js) into a generated header, so the JS can
+# live in its own file (lint/syntax-highlight friendly) instead of an inline literal.
+# Added to the global include_directories so both the library and the tests target
+# (which recompiles the common sources) can find the generated header.
+set(WPENGINE_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
+set(WPENGINE_BUILTINS_JS "${CMAKE_CURRENT_SOURCE_DIR}/src/WallpaperEngine/Scripting/resources/builtins.js")
+file(READ "${WPENGINE_BUILTINS_JS}" WPENGINE_SCRIPT_BUILTINS)
+configure_file(
+    "${CMAKE_CURRENT_SOURCE_DIR}/src/WallpaperEngine/Scripting/resources/Builtins.generated.h.in"
+    "${WPENGINE_GENERATED_DIR}/WallpaperEngine/Scripting/Builtins.generated.h"
+    @ONLY)
+# re-run configuration (regenerating the header) whenever the .js changes
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${WPENGINE_BUILTINS_JS}")
+
 include_directories(
+    ${WPENGINE_GENERATED_DIR}
     src
     src/External/glslang-WallpaperEngine
     src/External/SPIRV-Cross-WallpaperEngine

+ 5 - 21
src/WallpaperEngine/Scripting/ScriptEngine.cpp

@@ -13,6 +13,7 @@
 #include "WallpaperEngine/Render/CObject.h"
 #include "WallpaperEngine/Render/Objects/CSound.h"
 #include "WallpaperEngine/Render/Wallpapers/CScene.h"
+#include "WallpaperEngine/Scripting/Builtins.generated.h"
 #include "quickjs.h"
 
 #include <algorithm>
@@ -442,27 +443,10 @@ void ScriptEngine::installBuiltins () {
 	return;
     }
 
-    static constexpr const char* builtins = R"JS(
-globalThis.__intervals = Object.create(null);
-globalThis.localStorage = globalThis.localStorage || {
-  __data: Object.create(null),
-  get(key) {
-    key = String(key);
-    return Object.prototype.hasOwnProperty.call(this.__data, key) ? this.__data[key] : null;
-  },
-  set(key, value) { this.__data[String(key)] = String(value); },
-  remove(key) { delete this.__data[String(key)]; },
-  clear() { this.__data = Object.create(null); }
-};
-globalThis.MediaPlaybackEvent = globalThis.MediaPlaybackEvent || {
-  PLAYBACK_STOPPED: 0,
-  PLAYBACK_PLAYING: 1,
-  PLAYBACK_PAUSED: 2
-};
-)JS";
-
-    JSValue result
-	= JS_Eval (this->m_context, builtins, strlen (builtins), "<scene-script-builtins>", JS_EVAL_TYPE_GLOBAL);
+    JSValue result = JS_Eval (
+	this->m_context, SCENE_SCRIPT_BUILTINS, strlen (SCENE_SCRIPT_BUILTINS), "<scene-script-builtins>",
+	JS_EVAL_TYPE_GLOBAL
+    );
     if (JS_IsException (result)) {
 	logJSException (this->m_context, "installBuiltins");
     }

+ 7 - 0
src/WallpaperEngine/Scripting/resources/Builtins.generated.h.in

@@ -0,0 +1,7 @@
+// This file is auto-generated from builtins.js by CMake (configure_file). Do not edit.
+#pragma once
+
+namespace WallpaperEngine::Scripting {
+inline constexpr const char* SCENE_SCRIPT_BUILTINS = R"JS(
+@WPENGINE_SCRIPT_BUILTINS@)JS";
+} // namespace WallpaperEngine::Scripting

+ 16 - 0
src/WallpaperEngine/Scripting/resources/builtins.js

@@ -0,0 +1,16 @@
+globalThis.__intervals = Object.create(null);
+globalThis.localStorage = globalThis.localStorage || {
+  __data: Object.create(null),
+  get(key) {
+    key = String(key);
+    return Object.prototype.hasOwnProperty.call(this.__data, key) ? this.__data[key] : null;
+  },
+  set(key, value) { this.__data[String(key)] = String(value); },
+  remove(key) { delete this.__data[String(key)]; },
+  clear() { this.__data = Object.create(null); }
+};
+globalThis.MediaPlaybackEvent = globalThis.MediaPlaybackEvent || {
+  PLAYBACK_STOPPED: 0,
+  PLAYBACK_PLAYING: 1,
+  PLAYBACK_PAUSED: 2
+};