krillplayer-plugin 0.6.0 → 0.7.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 (150) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +1 -0
  3. package/dist/cli.js +351 -73
  4. package/dist/crypto.js +37 -11
  5. package/dist/engine/engine.c +6 -3
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
  7. package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
  8. package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
  9. package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
  10. package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
  11. package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
  12. package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
  13. package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
  14. package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
  15. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
  16. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
  17. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
  18. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
  19. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
  20. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
  21. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
  22. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
  23. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
  24. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
  25. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
  26. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
  27. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
  28. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
  29. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
  30. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
  31. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
  32. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
  33. package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
  34. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
  35. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
  36. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
  37. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
  38. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
  39. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
  40. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
  41. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
  42. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
  43. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
  44. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
  45. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
  46. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
  47. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
  48. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
  49. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
  50. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
  51. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
  52. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
  53. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
  54. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
  55. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
  56. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
  57. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
  58. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
  59. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
  60. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
  61. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
  62. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
  63. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
  64. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
  65. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
  66. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
  67. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
  68. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
  69. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
  70. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
  71. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
  72. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
  73. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
  74. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
  75. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
  76. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
  77. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
  78. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
  79. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
  80. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
  81. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
  82. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
  83. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
  84. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
  85. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
  86. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
  87. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
  88. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
  89. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
  90. package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
  91. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
  92. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
  93. package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
  94. package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
  95. package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
  96. package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
  97. package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
  98. package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
  99. package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
  100. package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
  101. package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
  102. package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
  103. package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
  104. package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
  105. package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
  106. package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
  107. package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
  108. package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
  109. package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
  110. package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
  111. package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
  112. package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
  113. package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
  114. package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
  115. package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
  116. package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
  117. package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
  118. package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
  119. package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
  120. package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
  121. package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
  122. package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
  123. package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
  124. package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
  125. package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
  126. package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
  127. package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
  128. package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
  129. package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
  130. package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
  131. package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
  132. package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
  133. package/kernel/Tests/KernelTests/CasePins.js +50 -0
  134. package/package.json +2 -2
  135. package/skill/krillplayer-plugin/SKILL.md +2 -0
  136. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
  137. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
  138. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
  139. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
  140. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
  141. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
  142. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
  143. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +8 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +8 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +8 -0
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +26 -9
  147. package/skill/krillplayer-plugin/references/cases.md +19 -1
  148. package/skill/krillplayer-plugin/references/contract.d.ts +104 -11
  149. package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
  150. package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
package/dist/crypto.js CHANGED
@@ -4,6 +4,7 @@ var integer = { kind: "integer" };
4
4
  var number = { kind: "number" };
5
5
  var boolean = { kind: "boolean" };
6
6
  var bytes = { kind: "bytes" };
7
+ var json = { kind: "json" };
7
8
  function constant(value) {
8
9
  return { kind: "constant", value };
9
10
  }
@@ -48,6 +49,17 @@ function paged(of) {
48
49
  // contract/contract.ts
49
50
  var STORAGE_LIMITS = { keyBytes: 256, valueBytes: 64 * 1024, pluginBytes: 1024 * 1024 };
50
51
  var RUNTIME_LIMITS = { memoryBytes: 64 * 1024 * 1024, callSeconds: 20, stackBytes: 256 * 1024, callDepth: 64 };
52
+ var WASM_LIMITS = { moduleBytes: 8 * 1024 * 1024, modules: 4 };
53
+ var DECRYPT_LIMITS = {
54
+ contextBytes: 32 * 1024 * 1024,
55
+ pushBytes: 64 * 1024,
56
+ outputBytes: 4 * 1024 * 1024,
57
+ waitingBytes: 1024 * 1024,
58
+ contexts: 4,
59
+ tvContexts: 2,
60
+ segmentBytes: 64 * 1024 * 1024,
61
+ mapBytes: 1024 * 1024
62
+ };
51
63
  var FORMAT_LIMITS = { xmlDepth: 64, guideBytes: 48 * 1024 * 1024, decompressedBytes: 64 * 1024 * 1024 };
52
64
  var VALUE_LIMITS = { depth: 160 };
53
65
  var FILE_LIMITS = { readBytes: 4 * 1024 * 1024 };
@@ -70,7 +82,8 @@ var Capability = enumeration("Capability", [
70
82
  "browseLists",
71
83
  "playerPanels",
72
84
  "cardActions",
73
- "danmakuSendSession"
85
+ "danmakuSendSession",
86
+ "decrypt"
74
87
  ]);
75
88
  var Media = enumeration("Media", ["video", "audio"]);
76
89
  var ChannelKind = enumeration("ChannelKind", ["creator", "station", "podcast"]);
@@ -321,6 +334,12 @@ var PlatformCapabilities = object("PlatformCapabilities", {
321
334
  macOS: optional(array(Capability)),
322
335
  tvOS: optional(array(Capability))
323
336
  });
337
+ var PluginModule = object("PluginModule", {
338
+ name: string,
339
+ url: string,
340
+ sha256: string,
341
+ size: integer
342
+ });
324
343
  var PluginManifest = object("PluginManifest", {
325
344
  id: string,
326
345
  sourceId: string,
@@ -342,6 +361,8 @@ var PluginManifest = object("PluginManifest", {
342
361
  files: optional(FilesProtocol),
343
362
  // 要用哪几把 App 自己的密钥(krillplayer.appKeys,contract.md 6.3)。只有预装插件拿得到;用户添加的插件写了也只拿到 null。
344
363
  appKeys: optional(array(string)),
364
+ // 要用 krillplayer.wasm 的模块,最多 WASM_LIMITS.modules 个(见 PluginModule)。
365
+ modules: optional(array(PluginModule)),
345
366
  parameters: optional(array(Parameter)),
346
367
  settings: optional(array(Parameter)),
347
368
  unsupported: optional(PlatformCapabilities),
@@ -376,17 +397,25 @@ var PlayableSubtitle = object("PlayableSubtitle", {
376
397
  url: string,
377
398
  headers: optional(map(string))
378
399
  });
400
+ var Container = enumeration("Container", ["mp4", "mov", "mkv", "ts", "flv", "hls", "dash", "webm", "mp3", "aac", "flac", "ogg"]);
401
+ var PlayableDecryption = union("PlayableDecryption", "method", [
402
+ object("CencDecryption", { method: constant("cencAesCtr"), key: string, keyId: optional(string) }),
403
+ object("PluginDecryption", { method: constant("plugin"), state: optional(json) })
404
+ ]);
379
405
  var PlayableStream = object("PlayableStream", {
380
406
  quality: string,
381
407
  url: string,
382
408
  headers: optional(map(string)),
383
409
  audioURL: optional(string),
384
- container: optional(enumeration("Container", ["mp4", "mov", "mkv", "ts", "flv", "hls", "dash", "webm", "mp3", "aac", "flac", "ogg"]))
410
+ container: optional(Container),
411
+ // 这一路自己的解密信息,盖过 Playable 上那一个:同一次应答里常有加密档与明文档。
412
+ decryption: optional(PlayableDecryption)
385
413
  });
386
414
  var PlayableLine = object("PlayableLine", {
387
415
  name: string,
388
416
  streams: array(PlayableStream),
389
- qualities: optional(array(string))
417
+ qualities: optional(array(string)),
418
+ loginQualities: optional(array(string))
390
419
  });
391
420
  var Playable = object("Playable", {
392
421
  timing: enumeration("PlayableTiming", ["live", "onDemand"]),
@@ -401,11 +430,7 @@ var Playable = object("Playable", {
401
430
  end: number,
402
431
  title: optional(string)
403
432
  }))),
404
- decryption: optional(object("PlayableDecryption", {
405
- method: enumeration("PlayableDecryptionMethod", ["cencAesCtr"]),
406
- key: string,
407
- keyId: optional(string)
408
- })),
433
+ decryption: optional(PlayableDecryption),
409
434
  subtitles: optional(array(PlayableSubtitle)),
410
435
  // 这一路的原始文件名,给字幕插件与弹幕插件匹配用(contract.md 3.1);不给时内核用条目的文件名或标题。
411
436
  filename: optional(string)
@@ -801,6 +826,7 @@ var SLOT_RETURNS = {
801
826
  account: { schema: LoginAccount, nullable: true }
802
827
  },
803
828
  views: { panel: { schema: PanelPage }, action: { schema: ActionResult } },
829
+ decrypt: { push: { schema: array(bytes) }, finish: { schema: array(bytes) } },
804
830
  parameters: { options: { schema: array(Option) } },
805
831
  resolve: { "": { schema: Resolved, nullable: true } }
806
832
  };
@@ -930,12 +956,12 @@ function pluginJSONText(bytes2) {
930
956
  return void 0;
931
957
  }
932
958
  }
933
- function nestedTooDeep(json, limit = VALUE_LIMITS.depth) {
959
+ function nestedTooDeep(json2, limit = VALUE_LIMITS.depth) {
934
960
  let depth = 0;
935
961
  let inString = false;
936
962
  let escaped = false;
937
- for (let i = 0; i < json.length; i += 1) {
938
- const code = json.charCodeAt(i);
963
+ for (let i = 0; i < json2.length; i += 1) {
964
+ const code = json2.charCodeAt(i);
939
965
  if (inString) {
940
966
  if (escaped) escaped = false;
941
967
  else if (code === 92) escaped = true;
@@ -5,8 +5,9 @@
5
5
  * 用法: engine <内存上限字节> <原生栈预算字节>
6
6
  * 帧(双向同一格式,整数按本机字节序,Node 一侧按小端读):
7
7
  * tag:u8 number:f64 a_len:u32 b_len:u32 a[a_len] b[b_len]
8
- * Node → 引擎:I 安装前导脚本(a),M 加载模块(a 源码,b 名字),D 分发(a JSON,b 字节块),J 跑完待执行的任务,Q 退出;
9
- * number 是距期限的秒数,0 表示不设期限(与 kq_set_deadline 同义,换成本进程的单调时钟)。
8
+ * Node → 引擎:W 登记清单的一个 WebAssembly 模块(a 名字,b 32 字节的 SHA-256,number 字节数,kq_declare_module),
9
+ * I 安装前导脚本(a),M 加载模块(a 源码,b 名字),D 分发(a JSON,b 字节块),J 跑完待执行的任务,Q 退出;
10
+ * 除 W 外 number 是距期限的秒数,0 表示不设期限(与 kq_set_deadline 同义,换成本进程的单调时钟)。
10
11
  * 引擎 → Node:R 一次操作的结果(number 是 KQ_* 状态,a 是错误文本);H 插件调宿主(a JSON,b 字节块),
11
12
  * 引擎阻塞到 Node 回 h(a JSON,b 字节块)或 e(a 错误文本)。
12
13
  * 跑插件的是一条 512 KiB 栈的线程,与 App 里 Swift 并发线程池的线程栈相同(ADR 0012)。 */
@@ -104,7 +105,9 @@ static void *serve(void *unused) {
104
105
  if (frame.tag == 'Q') exit(0);
105
106
  char *error = NULL;
106
107
  int status = KQ_OUT_OF_MEMORY;
107
- if (q) {
108
+ if (q && frame.tag == 'W') {
109
+ status = frame.b_len == 32 && !kq_declare_module(q, (const char *)frame.a, (size_t)frame.number, frame.b) ? KQ_OK : KQ_OUT_OF_MEMORY;
110
+ } else if (q) {
108
111
  kq_set_deadline(q, frame.number > 0 ? kq_monotonic_seconds() + frame.number : 0);
109
112
  switch (frame.tag) {
110
113
  case 'I': status = kq_install(q, (const char *)frame.a, frame.a_len, &error); break;
@@ -31,17 +31,24 @@ typedef void (*KQHost)(void *opaque, const char *json, size_t json_len, const ui
31
31
  KQContext *kq_new(size_t memory_limit, size_t max_stack, KQHost host, void *opaque);
32
32
  void kq_free(KQContext *q);
33
33
 
34
- /* prelude 是一个函数表达式,以 (native, utf8Encode, utf8Decode) 调用,返回分发函数 (json, blob, namespace)。 */
34
+ /* prelude 是一个函数表达式,以 (native, utf8Encode, utf8Decode, instantiate) 调用,返回分发函数 (json, blob, namespace)。
35
+ * instantiate(name, bytes, imports) 是 krillplayer.wasm 的引擎一侧(kernel_wasm.c)。 */
35
36
  int kq_install(KQContext *q, const char *prelude, size_t len, char **error);
36
37
  int kq_load_module(KQContext *q, const char *source, size_t len, const char *name, char **error);
37
38
  int kq_dispatch(KQContext *q, const char *json, size_t json_len, const uint8_t *blob, size_t blob_len, char **error);
38
39
  int kq_drain(KQContext *q, char **error);
39
40
 
41
+ /* 清单 modules 的一项(ADR 0042):宿主在装前导脚本之前逐项登记。instantiate 只编长度与 SHA-256 和这里登记的
42
+ * 那一项一致的字节:字节经插件够得着的 JS 对象才到引擎,核对必须在这里。失败(内存不够)返回非 0。 */
43
+ int kq_declare_module(KQContext *q, const char *name, size_t size, const uint8_t sha256[32]);
44
+
40
45
  /* 单调时钟秒数;0 表示不设期限。中断处理函数到期后中断执行,插件捕获不到。 */
41
46
  void kq_set_deadline(KQContext *q, double deadline);
42
47
  double kq_monotonic_seconds(void);
43
48
  /* 只给测试用:把这个进程里的单调时钟往前拨 seconds 秒(#562)。期限与网络等待都按它算,拨过期限的调用在下一次中断检查时到期。 */
44
49
  void kq_skip_clock(double seconds);
45
50
  void kq_free_error(char *error);
51
+ /* 只给测试用:WAMR 的每个包装文件编完时钉住的各项的取值,每个包装一行「wamr_<名字>.c 名字=值 …」(ADR 0042)。 */
52
+ const char *kq_wasm_compiled_options(void);
46
53
 
47
54
  #endif
@@ -0,0 +1,54 @@
1
+ #ifndef KERNEL_INTERNAL_H
2
+ #define KERNEL_INTERNAL_H
3
+
4
+ /* kernel_qjs.c 与 kernel_wasm.c 共用的内部结构,不进 Swift 看得到的 include/。 */
5
+ #include "kernel_qjs.h"
6
+ #include "vendor/quickjs.h"
7
+
8
+ #include <stdatomic.h>
9
+ #include <stdbool.h>
10
+ #include <stddef.h>
11
+ #include <stdint.h>
12
+
13
+ /* 引擎的全部分配都经这里计数:QuickJS 的、WebAssembly 模块的线性内存与解释器自己的(ADR 0012、0042)。
14
+ * 超过上限的那一次分配失败,并留下标记:插件即使捕获了 out of memory,内核也照样停用它。 */
15
+ typedef struct {
16
+ size_t used;
17
+ size_t limit;
18
+ bool exceeded;
19
+ } KQHeap;
20
+
21
+ bool kq_heap_allows(KQHeap *heap, size_t size);
22
+ size_t kq_block_size(const void *ptr);
23
+
24
+ typedef struct KQWasmInstance KQWasmInstance;
25
+ typedef struct KQModule KQModule;
26
+
27
+ struct KQContext {
28
+ KQHeap heap;
29
+ JSRuntime *rt;
30
+ JSContext *ctx;
31
+ KQHost host;
32
+ void *opaque;
33
+ double deadline;
34
+ bool interrupted;
35
+ JSValue dispatch;
36
+ JSValue namespace;
37
+ size_t max_stack;
38
+ /* 这次进引擎时栈的下限(栈向下长):QuickJS 按它限深,模块的解释器也按它设边界。 */
39
+ uint8_t *stack_limit;
40
+ /* WebAssembly(kernel_wasm.c):实例的类、活着的实例、正在模块里的嵌套层数,与看门狗到期时设的标记。 */
41
+ JSClassID wasm_class;
42
+ KQWasmInstance *wasm_instances;
43
+ int wasm_depth;
44
+ atomic_bool wasm_expired;
45
+ /* 宿主登记的清单 modules(kq_declare_module)。 */
46
+ KQModule *modules;
47
+ };
48
+
49
+ /* kernel_wasm.c:给前导脚本的 instantiate(bytes, imports),与每个上下文建好时登记实例的类。 */
50
+ int kq_wasm_setup(KQContext *q);
51
+ JSValue kq_wasm_instantiate_function(JSContext *ctx);
52
+ void kq_wasm_free_modules(KQContext *q);
53
+
54
+ #endif
@@ -1,5 +1,4 @@
1
- #include "kernel_qjs.h"
2
- #include "vendor/quickjs.h"
1
+ #include "kernel_internal.h"
3
2
 
4
3
  #include <stdlib.h>
5
4
  /* 试跑器在作者机器上用同一份源码现编(plugins/trial-runner/engine.ts),Linux 上没有 malloc_size。 */
@@ -11,28 +10,8 @@
11
10
  #endif
12
11
  #include <string.h>
13
12
  #include <time.h>
14
- #include <stdatomic.h>
15
-
16
- /* 引擎的全部分配都经这里计数。超过上限的那一次分配失败,并留下标记:插件即使捕获了 out of memory,内核也照样停用它。 */
17
- typedef struct {
18
- size_t used;
19
- size_t limit;
20
- bool exceeded;
21
- } KQHeap;
22
-
23
- struct KQContext {
24
- KQHeap heap;
25
- JSRuntime *rt;
26
- JSContext *ctx;
27
- KQHost host;
28
- void *opaque;
29
- double deadline;
30
- bool interrupted;
31
- JSValue dispatch;
32
- JSValue namespace;
33
- };
34
13
 
35
- static bool heap_allows(KQHeap *heap, size_t size) {
14
+ bool kq_heap_allows(KQHeap *heap, size_t size) {
36
15
  if (heap->limit != 0 && heap->used + size > heap->limit) {
37
16
  heap->exceeded = true;
38
17
  return false;
@@ -40,6 +19,10 @@ static bool heap_allows(KQHeap *heap, size_t size) {
40
19
  return true;
41
20
  }
42
21
 
22
+ size_t kq_block_size(const void *ptr) { return malloc_size(ptr); }
23
+
24
+ #define heap_allows kq_heap_allows
25
+
43
26
  static void *heap_admit(KQHeap *heap, void *ptr) {
44
27
  if (ptr) heap->used += malloc_size(ptr);
45
28
  return ptr;
@@ -250,12 +233,14 @@ static JSValue js_utf8_decode(JSContext *ctx, JSValueConst this_val, int argc, J
250
233
 
251
234
  static void enter(KQContext *q) {
252
235
  q->interrupted = false;
236
+ atomic_store(&q->wasm_expired, false);
253
237
  JS_UpdateStackTop(q->rt);
238
+ q->stack_limit = (uint8_t *)__builtin_frame_address(0) - q->max_stack;
254
239
  }
255
240
 
256
241
  static int status_of(KQContext *q) {
257
242
  if (q->heap.exceeded) return KQ_OUT_OF_MEMORY;
258
- if (q->interrupted) return KQ_INTERRUPTED;
243
+ if (q->interrupted || atomic_load(&q->wasm_expired)) return KQ_INTERRUPTED;
259
244
  return KQ_EXCEPTION;
260
245
  }
261
246
 
@@ -289,7 +274,7 @@ static int fail(KQContext *q, char **error) {
289
274
  }
290
275
 
291
276
  static int settle(KQContext *q, char **error) {
292
- if (q->heap.exceeded || q->interrupted) {
277
+ if (q->heap.exceeded || q->interrupted || atomic_load(&q->wasm_expired)) {
293
278
  *error = NULL;
294
279
  return status_of(q);
295
280
  }
@@ -300,6 +285,7 @@ KQContext *kq_new(size_t memory_limit, size_t max_stack, KQHost host, void *opaq
300
285
  KQContext *q = calloc(1, sizeof(KQContext));
301
286
  if (!q) return NULL;
302
287
  q->heap.limit = memory_limit;
288
+ q->max_stack = max_stack;
303
289
  q->host = host;
304
290
  q->opaque = opaque;
305
291
  q->dispatch = JS_UNDEFINED;
@@ -315,7 +301,7 @@ KQContext *kq_new(size_t memory_limit, size_t max_stack, KQHost host, void *opaq
315
301
  if (!q->ctx || JS_AddIntrinsicBaseObjects(q->ctx) || JS_AddIntrinsicDate(q->ctx) || JS_AddIntrinsicEval(q->ctx) ||
316
302
  JS_AddIntrinsicRegExp(q->ctx) || JS_AddIntrinsicJSON(q->ctx) || JS_AddIntrinsicProxy(q->ctx) ||
317
303
  JS_AddIntrinsicMapSet(q->ctx) || JS_AddIntrinsicTypedArrays(q->ctx) || JS_AddIntrinsicPromise(q->ctx) ||
318
- JS_AddIntrinsicWeakRef(q->ctx)) {
304
+ JS_AddIntrinsicWeakRef(q->ctx) || kq_wasm_setup(q)) {
319
305
  kq_free(q);
320
306
  return NULL;
321
307
  }
@@ -331,6 +317,7 @@ void kq_free(KQContext *q) {
331
317
  JS_FreeContext(q->ctx);
332
318
  }
333
319
  JS_FreeRuntime(q->rt);
320
+ kq_wasm_free_modules(q);
334
321
  free(q);
335
322
  }
336
323
 
@@ -339,13 +326,14 @@ int kq_install(KQContext *q, const char *prelude, size_t len, char **error) {
339
326
  JSContext *ctx = q->ctx;
340
327
  JSValue setup = JS_Eval(ctx, prelude, len, "<kernel>", JS_EVAL_TYPE_GLOBAL);
341
328
  if (JS_IsException(setup)) return fail(q, error);
342
- JSValue natives[3] = {
329
+ JSValue natives[4] = {
343
330
  JS_NewCFunction(ctx, js_native, "native", 2),
344
331
  JS_NewCFunction(ctx, js_utf8_encode, "utf8Encode", 1),
345
332
  JS_NewCFunction(ctx, js_utf8_decode, "utf8Decode", 3),
333
+ kq_wasm_instantiate_function(ctx),
346
334
  };
347
- JSValue dispatch = JS_Call(ctx, setup, JS_UNDEFINED, 3, natives);
348
- for (int i = 0; i < 3; i++) JS_FreeValue(ctx, natives[i]);
335
+ JSValue dispatch = JS_Call(ctx, setup, JS_UNDEFINED, 4, natives);
336
+ for (int i = 0; i < 4; i++) JS_FreeValue(ctx, natives[i]);
349
337
  JS_FreeValue(ctx, setup);
350
338
  if (JS_IsException(dispatch)) return fail(q, error);
351
339
  q->dispatch = dispatch;