@dongmingzhixiu/vuexir-compiler 0.3.0 → 0.3.1

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.
@@ -93,6 +93,7 @@ class MainActivity : ComponentActivity() {
93
93
  super.onCreate(savedInstanceState)
94
94
  Store.init(applicationContext)
95
95
  VrnBridge.init(this)
96
+ VrnJs.init()
96
97
  // R-SYSTEMBAR:应用固定深色主题(InkBg/InkBar),与系统明暗设置无关。
97
98
  // enableEdgeToEdge() 默认用 SystemBarStyle.auto(...),会跟随系统明暗切换图标明暗——
98
99
  // 系统处于浅色模式时给出深色图标,落在深色顶栏/底栏上不可读(真机反馈:系统栏文字看不清)。
@@ -191,6 +192,75 @@ object Store {
191
192
  fun remove(key: String) { prefs?.edit()?.remove(key)?.apply() }
192
193
  }
193
194
 
195
+ /**
196
+ * R95 P1 Android JS 运行时(与 iOS JavaScriptCore 对位)。
197
+ *
198
+ * 设计:QuickJS 里跑业务逻辑,结果通过全局对象 __vrnOut 回传给原生——
199
+ * 不用 Java/JS 互调反射(QuickJS-Android 的反射桥对复杂类型支持有限,且易在混淆后失效)。
200
+ *
201
+ * VrnJs.eval("1+1") → "2"
202
+ * VrnJs.eval("__vrnOut.chars = countChars(raw)") -> 之后 VrnJs.drain() 取走 {"chars":"12"}
203
+ *
204
+ * drain() 会清空 __vrnOut,原生据此批量更新 Compose State。
205
+ */
206
+ object VrnJs {
207
+ private var js: app.cash.quickjs.QuickJs? = null
208
+ private val lock = Any()
209
+
210
+ /** 预置宿主 shim:console / localStorage(落 SharedPreferences)/ 输出对象 */
211
+ private const val PRELUDE = """
212
+ var __vrnOut = {};
213
+ var __vrnStore = {};
214
+ var localStorage = {
215
+ getItem: function (k) { return __vrnStore[k] === undefined ? null : __vrnStore[k]; },
216
+ setItem: function (k, v) { __vrnStore[k] = String(v); __vrnOut['__ls:' + k] = String(v); },
217
+ removeItem: function (k) { delete __vrnStore[k]; __vrnOut['__ls:' + k] = ''; }
218
+ };
219
+ var console = { log: function () {}, warn: function () {}, error: function () {} };
220
+ """
221
+
222
+ fun init() {
223
+ synchronized(lock) {
224
+ if (js == null) {
225
+ js = app.cash.quickjs.QuickJs.create()
226
+ js?.evaluate(PRELUDE)
227
+ }
228
+ }
229
+ }
230
+
231
+ /** 执行一段 JS;返回最后一个表达式的值(字符串化),异常返回 null 并打日志 */
232
+ fun eval(code: String): String? = synchronized(lock) {
233
+ val engine = js ?: return null
234
+ return try {
235
+ val v = engine.evaluate(code)
236
+ v?.toString()
237
+ } catch (e: Throwable) {
238
+ android.util.Log.w("VrnJs", "eval 失败: " + e.message)
239
+ null
240
+ }
241
+ }
242
+
243
+ /** 取走 JS 侧累积的状态变更(JSON 文本),并清空 */
244
+ fun drain(): String = synchronized(lock) {
245
+ val engine = js ?: return "{}"
246
+ val json = try {
247
+ engine.evaluate("(function(){var s=JSON.stringify(__vrnOut);__vrnOut={};return s;})()")?.toString()
248
+ } catch (e: Throwable) {
249
+ null
250
+ } ?: "{}"
251
+ json.ifEmpty { "{}" }
252
+ }
253
+
254
+ fun close() = synchronized(lock) {
255
+ try {
256
+ js?.close()
257
+ } catch (e: Throwable) {
258
+ // 忽略
259
+ }
260
+ js = null
261
+ }
262
+ }
263
+
194
264
  /**
195
265
  * R81 原生桥(Android)——编译期注入;Vue 侧 bridge.toast(...) 由编译器翻译为 VrnBridge.toast(...)
196
266
  * 与 @dongmingzhixiu/vuexir-templates 的 VRN_BRIDGE_KT 保持一致
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongmingzhixiu/vuexir-compiler",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Vue SFC → IR → 原生源码(SwiftUI / Compose)编译器核心:纯函数、零 IO",
5
5
  "type": "module",
6
6
  "license": "MIT",