@native-sdk/cli 0.8.4 → 0.9.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.
- package/build/app.zig +1178 -74
- package/build.zig +715 -17
- package/build.zig.zon +1 -0
- package/native-sdk.d.ts +10 -0
- package/package.json +12 -11
- package/packages/core/compile-surface/core.ts +435 -3
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/compiler_typecheck.mjs +116 -15
- package/packages/core/scripts/gen_service_surface.mjs +268 -0
- package/packages/core/scripts/merge_static_archives.mjs +78 -0
- package/packages/core/scripts/npm_static_spike.mjs +64 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +122 -6
- package/packages/core/scripts/run_external_service_compiler.mjs +280 -0
- package/packages/core/scripts/stage_external_core.mjs +9 -1
- package/packages/core/scripts/stage_external_services.mjs +172 -0
- package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
- package/packages/core/scripts/vendor_service_packages.mjs +189 -0
- package/packages/core/sdk/core.d.ts +224 -0
- package/packages/core/sdk/core.ts +694 -22
- package/packages/core/sdk/events.d.ts +89 -0
- package/packages/core/sdk/events.ts +128 -0
- package/packages/core/src/checker.ts +936 -15
- package/packages/core/src/cli.ts +156 -3
- package/packages/core/src/contract.ts +28 -5
- package/packages/core/src/devhost.ts +1876 -16
- package/packages/core/src/devhost_journal.mjs +183 -0
- package/packages/core/src/devhost_resolver.mjs +17 -1
- package/packages/core/src/devhost_service_worker.mjs +90 -0
- package/packages/core/src/diagnostics.ts +179 -22
- package/packages/core/src/frontend.ts +210 -34
- package/packages/core/src/infer.ts +24 -0
- package/packages/core/src/modules.ts +129 -80
- package/packages/core/src/service_contract.ts +597 -0
- package/packages/core/src/sqlite_cli.ts +93 -0
- package/packages/core/src/sqlite_codegen.ts +806 -0
- package/packages/core/src/sqlite_runtime_policy.ts +364 -0
- package/packages/core/src/typed_ast.ts +36 -8
- package/skill-data/automation/SKILL.md +1 -1
- package/skill-data/core/references/project-anatomy.md +1 -1
- package/skill-data/native-ui/SKILL.md +58 -5
- package/skill-data/ts-core/SKILL.md +123 -41
- package/skill-data/ts-services/SKILL.md +126 -0
- package/skill-data/ts-services/references/service-surface.md +604 -0
- package/skill-data/zig/SKILL.md +1 -1
- package/skills/native-sdk/SKILL.md +1 -1
- package/src/app_runner/no_migrations.zig +3 -0
- package/src/app_runner/no_services.zig +20 -0
- package/src/app_runner/root.zig +133 -4
- package/src/app_runner/ts_core_main.zig +254 -8
- package/src/app_runner/ts_core_mobile.zig +241 -0
- package/src/automation/protocol.zig +5 -1
- package/src/automation/snapshot.zig +31 -11
- package/src/embed/app_exports.zig +12 -1
- package/src/embed/c_api.zig +48 -2
- package/src/embed/conversions.zig +4 -5
- package/src/embed/host.zig +109 -1
- package/src/embed/root.zig +8 -0
- package/src/embed/tests.zig +241 -0
- package/src/embed/types.zig +51 -0
- package/src/embed/ui_host.zig +185 -5
- package/src/platform/android/NativeSdkActivity.java +131 -0
- package/src/platform/android/android_host.c +182 -1
- package/src/platform/android/native_sdk_app.h +17 -0
- package/src/platform/ios/native_sdk_app.h +17 -0
- package/src/platform/ios/uikit_host.m +115 -0
- package/src/platform/linux/cef_host.cpp +8 -2
- package/src/platform/linux/gtk_host.c +289 -22
- package/src/platform/linux/gtk_host.h +5 -1
- package/src/platform/linux/root.zig +109 -22
- package/src/platform/macos/appkit_host.h +24 -7
- package/src/platform/macos/appkit_host.m +686 -64
- package/src/platform/macos/cef_host.mm +400 -66
- package/src/platform/macos/root.zig +304 -47
- package/src/platform/null_platform.zig +435 -48
- package/src/platform/null_platform_tests.zig +113 -0
- package/src/platform/root.zig +19 -0
- package/src/platform/types.zig +281 -28
- package/src/platform/windows/root.zig +207 -27
- package/src/platform/windows/webview2_host.cpp +334 -51
- package/src/primitives/app_manifest/comptime_scene.zig +2 -0
- package/src/primitives/app_manifest/root.zig +4 -0
- package/src/primitives/app_manifest/tests.zig +3 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- package/src/primitives/app_manifest/validation.zig +19 -1
- package/src/root.zig +53 -0
- package/src/runtime/api.zig +19 -0
- package/src/runtime/automation_commands.zig +35 -2
- package/src/runtime/automation_snapshot.zig +32 -20
- package/src/runtime/bridge_permission_tests.zig +58 -3
- package/src/runtime/bridge_responses.zig +4 -2
- package/src/runtime/builtin_bridge.zig +11 -1
- package/src/runtime/canvas_frame.zig +18 -11
- package/src/runtime/canvas_frame_animation_tests.zig +4 -0
- package/src/runtime/clock.zig +13 -1
- package/src/runtime/core.zig +145 -7
- package/src/runtime/credentials_store.zig +115 -0
- package/src/runtime/effects.zig +3701 -231
- package/src/runtime/effects_credentials_tests.zig +332 -0
- package/src/runtime/effects_file_tests.zig +587 -0
- package/src/runtime/effects_host_tests.zig +208 -1
- package/src/runtime/effects_notification_tests.zig +11 -0
- package/src/runtime/effects_relational_tests.zig +314 -0
- package/src/runtime/effects_store_tests.zig +211 -0
- package/src/runtime/file_access.zig +400 -0
- package/src/runtime/file_effect_crash_helper.zig +28 -0
- package/src/runtime/file_effect_crash_tests.zig +37 -0
- package/src/runtime/flow.zig +35 -10
- package/src/runtime/gpu_surface_events.zig +6 -1
- package/src/runtime/native_command_event_tests.zig +86 -2
- package/src/runtime/persist_store.zig +666 -0
- package/src/runtime/pty.zig +5 -2
- package/src/runtime/record_store.zig +788 -0
- package/src/runtime/relational_store.zig +854 -0
- package/src/runtime/root.zig +54 -0
- package/src/runtime/runtime_event_tests.zig +70 -0
- package/src/runtime/service_host.zig +711 -0
- package/src/runtime/service_pool.zig +1314 -0
- package/src/runtime/session_blobs.zig +4 -1
- package/src/runtime/session_journal.zig +158 -5
- package/src/runtime/session_record.zig +215 -0
- package/src/runtime/session_replay.zig +206 -11
- package/src/runtime/session_tests.zig +81 -0
- package/src/runtime/shell_layout.zig +1 -0
- package/src/runtime/shell_layout_tests.zig +120 -0
- package/src/runtime/sqlite_engine.zig +474 -0
- package/src/runtime/state.zig +16 -0
- package/src/runtime/system_services.zig +170 -38
- package/src/runtime/tests.zig +4 -0
- package/src/runtime/ts_core_host.zig +1067 -95
- package/src/runtime/ts_core_host_tests.zig +325 -11
- package/src/runtime/ts_ui_app.zig +953 -2
- package/src/runtime/ui_app.zig +434 -96
- package/src/runtime/ui_app_tests.zig +287 -11
- package/src/runtime/ui_app_window_tests.zig +61 -7
- package/src/runtime/validation.zig +42 -0
- package/src/runtime/view.zig +9 -0
- package/src/runtime/window_storage.zig +1 -0
- package/src/runtime/window_view_runtime.zig +2 -3
- package/src/runtime/window_views.zig +19 -0
- package/src/tooling/android.zig +21 -16
- package/src/tooling/db.zig +160 -0
- package/src/tooling/ios.zig +7 -1
- package/src/tooling/manifest.zig +214 -2
- package/src/tooling/package.zig +116 -0
- package/src/tooling/raw_manifest.zig +30 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +189 -4
- package/src/tooling/ts_core.zig +366 -17
- package/src/tooling/verbs.zig +7 -0
- package/third_party/sqlite/README.md +13 -0
- package/third_party/sqlite/sqlite3.c +269649 -0
- package/third_party/sqlite/sqlite3.h +14349 -0
- package/tools/corewire/core_abi.zig +14 -2
- package/tools/corewire/emit.zig +98 -29
- package/tools/corewire/emit_facade.zig +442 -27
- package/tools/corewire/emit_profile.zig +6 -2
- package/tools/corewire/emit_service.zig +924 -0
- package/tools/corewire/main.zig +122 -0
- package/tools/corewire/service_contract.zig +250 -0
- package/tools/corewire/sidecar.zig +26 -15
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//! The native host consumer for compiled TypeScript app cores: bridges
|
|
2
2
|
//! the versioned command/subscription wire format a compiled core
|
|
3
|
-
//! emits (`cmd_format_version`
|
|
3
|
+
//! emits (`cmd_format_version` 7) onto the real effect engine
|
|
4
4
|
//! (`effects.zig`). The TypeScript tier's core module is a pure
|
|
5
5
|
//! Model/Msg/update core whose effects are INERT BYTES — this module is
|
|
6
6
|
//! the one place those bytes become engine calls, so the entire
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
//! arms' declaration-order indices)
|
|
20
20
|
//! core.initialModel() `*const Model`, or `InitResult{ model, cmd }`
|
|
21
21
|
//! for a boot command
|
|
22
|
+
//! core.bootCommand() the boot `Cmd` without reinitializing the core,
|
|
23
|
+
//! required when `initialModel` returns `InitResult`
|
|
22
24
|
//! core.update(m, msg) `*const Model`, or `UpdateResult{ model, cmd }`
|
|
23
25
|
//! core.commitModelRoot the frame-end commit walker
|
|
24
26
|
//! core.subscriptions optional: `fn (*const Model) []const u8`
|
|
@@ -36,9 +38,10 @@
|
|
|
36
38
|
//! command and subscription bytes are frame-arena resident and must be
|
|
37
39
|
//! consumed before the reset. Wire records map onto the engine as:
|
|
38
40
|
//!
|
|
39
|
-
//! persist -> `fx.hostSend("core.persist",
|
|
40
|
-
//!
|
|
41
|
-
//!
|
|
41
|
+
//! persist -> `fx.hostSend("core.persist", core.persistenceSnapshot())`
|
|
42
|
+
//! — no model bytes ride the Cmd wire; the host asks
|
|
43
|
+
//! the already-committed core for canonical bytes while
|
|
44
|
+
//! walking the command.
|
|
42
45
|
//! now -> `fx.wallMs()` (the journaled clock read) captured
|
|
43
46
|
//! during the walk; the named arm dispatches with that
|
|
44
47
|
//! time SYNCHRONOUSLY — immediately after the issuing
|
|
@@ -294,6 +297,12 @@
|
|
|
294
297
|
//! activate (the tray "Open" consequence of the
|
|
295
298
|
//! menu-bar-app loop). No result Msg; the window's own
|
|
296
299
|
//! frame event carries the state.
|
|
300
|
+
//! window_hide -> `fx.hideWindow(label)` — retain the live window and
|
|
301
|
+
//! its views while ordering it out; window_show is the
|
|
302
|
+
//! inverse.
|
|
303
|
+
//! dock_presence -> `fx.setDockPresence(visible)` — macOS regular/
|
|
304
|
+
//! accessory activation-policy switch; unsupported
|
|
305
|
+
//! hosts safely ignore it.
|
|
297
306
|
//! quit_app -> `fx.quitApp()` — the graceful terminate through the
|
|
298
307
|
//! same shutdown path a last-window close takes.
|
|
299
308
|
//! show_notification -> `fx.showNotification` fire-and-forget; invalid or
|
|
@@ -423,6 +432,13 @@ pub fn videoKeyForTag(event_tag: u8) u64 {
|
|
|
423
432
|
/// families' one engine key space without ever colliding on a key.
|
|
424
433
|
pub const pty_key_base: u64 = 0x5453_5054_0000_0000;
|
|
425
434
|
|
|
435
|
+
/// Engine-key namespace for relational effects ("TSDB"). The DB family has
|
|
436
|
+
/// its own engine table, so these never consume the general request slots.
|
|
437
|
+
pub const db_key_base: u64 = 0x5453_4442_0000_0000;
|
|
438
|
+
|
|
439
|
+
/// Dedicated raw-file stream key namespace ("TSFS").
|
|
440
|
+
pub const file_stream_key_base: u64 = 0x5453_4653_0000_0000;
|
|
441
|
+
|
|
426
442
|
/// The spawn wire record's "no line routing" tag sentinel (the wire
|
|
427
443
|
/// format's shared constant).
|
|
428
444
|
pub const spawn_no_line_tag: u8 = 0xFF;
|
|
@@ -446,6 +462,15 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
446
462
|
pub const Msg = core.Msg;
|
|
447
463
|
pub const Fx = runtime_effects.Effects(Msg);
|
|
448
464
|
|
|
465
|
+
/// Generated service contracts bind their operation table and the
|
|
466
|
+
/// canonical result decoder here. Ordinary HostCallBinding users do
|
|
467
|
+
/// not install this seam, so raw Cmd.request keeps its byte result.
|
|
468
|
+
pub const ServiceResultBinding = struct {
|
|
469
|
+
index_fn: *const fn (name: []const u8) ?u16,
|
|
470
|
+
streaming_fn: *const fn (operation: u16) bool,
|
|
471
|
+
decode_fn: *const fn (operation: u16, tag: u8, bytes: []const u8) Msg,
|
|
472
|
+
};
|
|
473
|
+
|
|
449
474
|
const msg_arms = @typeInfo(Msg).@"union".fields;
|
|
450
475
|
|
|
451
476
|
const update_returns_cmd = @typeInfo(@TypeOf(core.update)).@"fn".return_type.? != *const Model;
|
|
@@ -463,6 +488,9 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
463
488
|
key: [max_wire_key_bytes]u8 = undefined,
|
|
464
489
|
ok_tag: u8 = 0,
|
|
465
490
|
err_tag: u8 = 0,
|
|
491
|
+
service_operation: ?u16 = null,
|
|
492
|
+
service_channel: ?u64 = null,
|
|
493
|
+
ok_void: bool = false,
|
|
466
494
|
|
|
467
495
|
fn wireKey(entry: *const RequestEntry) []const u8 {
|
|
468
496
|
return entry.key[0..entry.key_len];
|
|
@@ -549,6 +577,21 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
549
577
|
}
|
|
550
578
|
};
|
|
551
579
|
|
|
580
|
+
const FileStreamEntry = struct {
|
|
581
|
+
used: bool = false,
|
|
582
|
+
sink: bool = false,
|
|
583
|
+
busy: bool = false,
|
|
584
|
+
key_len: usize = 0,
|
|
585
|
+
key: [max_wire_key_bytes]u8 = undefined,
|
|
586
|
+
chunk_tag: u8 = 0,
|
|
587
|
+
done_tag: u8 = 0,
|
|
588
|
+
err_tag: u8 = 0,
|
|
589
|
+
cancelling: bool = false,
|
|
590
|
+
fn wireKey(entry: *const FileStreamEntry) []const u8 {
|
|
591
|
+
return entry.key[0..entry.key_len];
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
|
|
552
595
|
/// The single audio stream entry (one player is the whole
|
|
553
596
|
/// engine surface). Non-retiring: audio_ctl `stop` closes it, a
|
|
554
597
|
/// new audio_play re-keys and re-routes it in place.
|
|
@@ -638,6 +681,22 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
638
681
|
}
|
|
639
682
|
};
|
|
640
683
|
|
|
684
|
+
const DbEntry = struct {
|
|
685
|
+
used: bool = false,
|
|
686
|
+
query: bool = true,
|
|
687
|
+
live: bool = false,
|
|
688
|
+
signature: u64 = 0,
|
|
689
|
+
key_len: usize = 0,
|
|
690
|
+
key: [max_wire_key_bytes]u8 = undefined,
|
|
691
|
+
page_tag: u8 = 0,
|
|
692
|
+
done_tag: u8 = 0,
|
|
693
|
+
err_tag: u8 = 0,
|
|
694
|
+
|
|
695
|
+
fn wireKey(entry: *const DbEntry) []const u8 {
|
|
696
|
+
return entry.key[0..entry.key_len];
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
|
|
641
700
|
var model_root: *const Model = undefined;
|
|
642
701
|
/// The platform caches directory for URL audio sources, set by
|
|
643
702
|
/// the wiring (`TsUiApp`'s `audio_cache_dir`, or `setAudioCacheDir`
|
|
@@ -648,17 +707,19 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
648
707
|
var audio_cache_dir_len: usize = 0;
|
|
649
708
|
var audio_cache_dir_buf: [runtime_effects.max_effect_audio_path_bytes]u8 = undefined;
|
|
650
709
|
var audio_cache_path_buf: [runtime_effects.max_effect_audio_path_bytes]u8 = undefined;
|
|
651
|
-
var requests: [runtime_effects.max_effects]RequestEntry = @splat(.{});
|
|
710
|
+
var requests: [runtime_effects.max_effects + runtime_effects.max_store_effects + runtime_effects.max_credentials_effects]RequestEntry = @splat(.{});
|
|
652
711
|
var timers: [runtime_effects.max_effect_timers]TimerEntry = @splat(.{});
|
|
653
712
|
var effects_table: [runtime_effects.max_effects]EffectEntry = @splat(.{});
|
|
654
713
|
var delays: [runtime_effects.max_effect_timers]DelayEntry = @splat(.{});
|
|
655
714
|
var streams: [runtime_effects.max_effects]StreamEntry = @splat(.{});
|
|
715
|
+
var file_streams: [runtime_effects.max_effect_file_streams]FileStreamEntry = @splat(.{});
|
|
656
716
|
var audio_entry: AudioEntry = .{};
|
|
657
717
|
var video_entry: VideoEntry = .{};
|
|
658
718
|
var images: [runtime_effects.max_effects]ImageEntry = @splat(.{});
|
|
659
719
|
var channels: [runtime_effects.max_effect_channels]ChannelEntry = @splat(.{});
|
|
660
720
|
var audio_captures: [runtime_effects.max_effect_channels]AudioCaptureEntry = @splat(.{});
|
|
661
721
|
var ptys: [runtime_effects.max_effect_ptys]PtyEntry = @splat(.{});
|
|
722
|
+
var dbs: [runtime_effects.max_db_effects]DbEntry = @splat(.{});
|
|
662
723
|
/// The platform caches directory for URL image sources, the
|
|
663
724
|
/// audio cache dir's twin (`setImageCacheDir` / `TsUiApp`'s
|
|
664
725
|
/// `image_cache_dir`): bridge-side derivation of the
|
|
@@ -676,6 +737,12 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
676
737
|
/// `TsCoreHost.drain` and the `TsUiApp` update_fx seam — keep
|
|
677
738
|
/// that adjacency).
|
|
678
739
|
var swallow_next_dispatch: bool = false;
|
|
740
|
+
var service_results: ?ServiceResultBinding = null;
|
|
741
|
+
var pending_service_channel_close: ?u64 = null;
|
|
742
|
+
|
|
743
|
+
pub fn bindServiceResults(binding: ?ServiceResultBinding) void {
|
|
744
|
+
service_results = binding;
|
|
745
|
+
}
|
|
679
746
|
|
|
680
747
|
/// A `now` record captured during the command walk, dispatched
|
|
681
748
|
/// after the issuing cycle's frame reset.
|
|
@@ -734,6 +801,7 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
734
801
|
channels = @splat(.{});
|
|
735
802
|
audio_captures = @splat(.{});
|
|
736
803
|
ptys = @splat(.{});
|
|
804
|
+
dbs = @splat(.{});
|
|
737
805
|
clip_write_counter = 0;
|
|
738
806
|
audio_cache_dir_len = 0;
|
|
739
807
|
image_cache_dir_len = 0;
|
|
@@ -745,15 +813,13 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
745
813
|
|
|
746
814
|
/// The effects half of `init`: perform the boot command and
|
|
747
815
|
/// reconcile the initial subscriptions against the committed
|
|
748
|
-
/// boot model. The
|
|
749
|
-
///
|
|
750
|
-
///
|
|
751
|
-
///
|
|
752
|
-
/// own guarantee, so the bytes are identical by construction.
|
|
816
|
+
/// boot model. The generated shim re-materializes only the boot
|
|
817
|
+
/// command through `bootCommand`; calling `initialModel` here would
|
|
818
|
+
/// reinitialize the compiled core and discard a model restored
|
|
819
|
+
/// between `boot` and `performBoot`.
|
|
753
820
|
pub fn performBoot(fx: *Fx) void {
|
|
754
821
|
if (comptime init_returns_cmd) {
|
|
755
|
-
|
|
756
|
-
finishCycle(fx, again.cmd, 0);
|
|
822
|
+
finishCycle(fx, core.bootCommand(), 0);
|
|
757
823
|
} else {
|
|
758
824
|
finishCycle(fx, "", 0);
|
|
759
825
|
}
|
|
@@ -764,6 +830,26 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
764
830
|
return model_root;
|
|
765
831
|
}
|
|
766
832
|
|
|
833
|
+
/// Replace the committed core model from canonical snapshot bytes and
|
|
834
|
+
/// refresh the host mirror. Generated external cores expose the inverse
|
|
835
|
+
/// of `modelSnapshot`; hand-written test cores intentionally do not.
|
|
836
|
+
pub fn restoreSnapshot(snapshot: []const u8) void {
|
|
837
|
+
if (comptime !@hasDecl(core, "restoreModel")) {
|
|
838
|
+
@panic("ts core host: this core exposes no restoreModel entry - regenerate it with core ABI version 2");
|
|
839
|
+
} else {
|
|
840
|
+
model_root = core.restoreModel(snapshot);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/// Run the generated pure migration seam. The returned bytes are a
|
|
845
|
+
/// host-owned current-format snapshot and must be freed by `allocator`.
|
|
846
|
+
pub fn migrateSnapshot(snapshot: []const u8, from_version: u64, allocator: std.mem.Allocator) ?[]u8 {
|
|
847
|
+
if (comptime @hasDecl(core, "migrateModel")) {
|
|
848
|
+
return core.migrateModel(snapshot, from_version, allocator);
|
|
849
|
+
}
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
|
|
767
853
|
/// Configure the caches directory audio-cache derivation uses.
|
|
768
854
|
/// Call after `boot` (which clears it) and before any dispatch —
|
|
769
855
|
/// wiring-time configuration, exactly like soundboard's boot-time
|
|
@@ -833,6 +919,10 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
833
919
|
swallow_next_dispatch = false;
|
|
834
920
|
return;
|
|
835
921
|
}
|
|
922
|
+
if (pending_service_channel_close) |channel_key| {
|
|
923
|
+
pending_service_channel_close = null;
|
|
924
|
+
fx.closeChannel(channel_key);
|
|
925
|
+
}
|
|
836
926
|
dispatchDepth(fx, msg, 0);
|
|
837
927
|
}
|
|
838
928
|
|
|
@@ -874,7 +964,8 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
874
964
|
var nows: [max_nows_per_cmd]PendingNow = undefined;
|
|
875
965
|
var now_count: usize = 0;
|
|
876
966
|
runCmd(fx, cmd, &nows, &now_count);
|
|
877
|
-
|
|
967
|
+
reconcileSubscriptions(fx);
|
|
968
|
+
fx.flushDbSubscriptions();
|
|
878
969
|
core.rt.frameReset();
|
|
879
970
|
for (nows[0..now_count]) |pending| {
|
|
880
971
|
dispatchDepth(fx, msgFromTagNumber(pending.tag, @floatFromInt(pending.ms)), depth + 1);
|
|
@@ -897,7 +988,10 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
897
988
|
at += 1;
|
|
898
989
|
switch (op) {
|
|
899
990
|
// persist [op]
|
|
900
|
-
0x01 =>
|
|
991
|
+
0x01 => {
|
|
992
|
+
const snapshot = if (comptime @hasDecl(core, "persistenceSnapshot")) core.persistenceSnapshot() else "";
|
|
993
|
+
fx.hostSend("core.persist", snapshot);
|
|
994
|
+
},
|
|
901
995
|
// now [op][msg_tag]
|
|
902
996
|
0x02 => {
|
|
903
997
|
const tag = takeByte(cmd, &at);
|
|
@@ -923,14 +1017,16 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
923
1017
|
fx.hostSend(name, payload);
|
|
924
1018
|
},
|
|
925
1019
|
// request [op][name_len][name][key_len][key]
|
|
926
|
-
// [ok_tag][err_tag][len u32 LE][payload]
|
|
1020
|
+
// [ok_tag][err_tag][typed_service][len u32 LE][payload]
|
|
927
1021
|
0x05 => {
|
|
928
1022
|
const name = takeShortBytes(cmd, &at);
|
|
929
1023
|
const key = takeShortBytes(cmd, &at);
|
|
930
1024
|
const ok_tag = takeByte(cmd, &at);
|
|
931
1025
|
const err_tag = takeByte(cmd, &at);
|
|
1026
|
+
const typed_service = takeByte(cmd, &at);
|
|
1027
|
+
if (typed_service > 1) @panic("ts core host: invalid typed-service request flag");
|
|
932
1028
|
const payload = takeLongBytes(cmd, &at);
|
|
933
|
-
issueRequest(fx, name, key, ok_tag, err_tag, payload);
|
|
1029
|
+
issueRequest(fx, name, key, ok_tag, err_tag, typed_service == 1, payload);
|
|
934
1030
|
},
|
|
935
1031
|
// cancel [op][key_len][key]
|
|
936
1032
|
0x06 => {
|
|
@@ -941,8 +1037,9 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
941
1037
|
0x07 => {
|
|
942
1038
|
const head = takeRoutedHead(cmd, &at);
|
|
943
1039
|
const file_path = takeLongBytes(cmd, &at);
|
|
1040
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
944
1041
|
fx.readFile(.{
|
|
945
|
-
.key = effect_key_base +
|
|
1042
|
+
.key = effect_key_base + effect_index,
|
|
946
1043
|
.path = file_path,
|
|
947
1044
|
.on_result = fileResultMsg,
|
|
948
1045
|
});
|
|
@@ -953,13 +1050,53 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
953
1050
|
const head = takeRoutedHead(cmd, &at);
|
|
954
1051
|
const file_path = takeLongBytes(cmd, &at);
|
|
955
1052
|
const bytes = takeLongBytes(cmd, &at);
|
|
1053
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
956
1054
|
fx.writeFile(.{
|
|
957
|
-
.key = effect_key_base +
|
|
1055
|
+
.key = effect_key_base + effect_index,
|
|
958
1056
|
.path = file_path,
|
|
959
1057
|
.bytes = bytes,
|
|
960
1058
|
.on_result = fileResultMsg,
|
|
961
1059
|
});
|
|
962
1060
|
},
|
|
1061
|
+
// append/stat and streaming raw-file effects.
|
|
1062
|
+
0x2B => {
|
|
1063
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1064
|
+
const file_path = takeLongBytes(cmd, &at);
|
|
1065
|
+
const bytes = takeLongBytes(cmd, &at);
|
|
1066
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
1067
|
+
fx.appendFile(.{ .key = effect_key_base + effect_index, .path = file_path, .bytes = bytes, .on_result = fileResultMsg });
|
|
1068
|
+
},
|
|
1069
|
+
0x2C => {
|
|
1070
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1071
|
+
const file_path = takeLongBytes(cmd, &at);
|
|
1072
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
1073
|
+
fx.statFile(.{ .key = effect_key_base + effect_index, .path = file_path, .on_result = fileResultMsg });
|
|
1074
|
+
},
|
|
1075
|
+
0x32 => {
|
|
1076
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1077
|
+
const file_path = takeLongBytes(cmd, &at);
|
|
1078
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
1079
|
+
fx.deleteFile(.{ .key = effect_key_base + effect_index, .path = file_path, .on_result = fileResultMsg });
|
|
1080
|
+
},
|
|
1081
|
+
0x2D => {
|
|
1082
|
+
const key = takeShortBytes(cmd, &at);
|
|
1083
|
+
const chunk_tag = takeByte(cmd, &at);
|
|
1084
|
+
const done_tag = takeByte(cmd, &at);
|
|
1085
|
+
const err_tag = takeByte(cmd, &at);
|
|
1086
|
+
const file_path = takeLongBytes(cmd, &at);
|
|
1087
|
+
issueReadFileStream(fx, key, chunk_tag, done_tag, err_tag, file_path);
|
|
1088
|
+
},
|
|
1089
|
+
0x2E => {
|
|
1090
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1091
|
+
const file_path = takeLongBytes(cmd, &at);
|
|
1092
|
+
issueWriteFileStream(fx, head, file_path);
|
|
1093
|
+
},
|
|
1094
|
+
0x2F => {
|
|
1095
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1096
|
+
const bytes = takeLongBytes(cmd, &at);
|
|
1097
|
+
issueWriteFileChunk(fx, head, bytes);
|
|
1098
|
+
},
|
|
1099
|
+
0x30 => issueWriteFileClose(fx, takeRoutedHead(cmd, &at)),
|
|
963
1100
|
// fetch [op][key_len][key][ok][err][method u8][timeout u32 LE]
|
|
964
1101
|
// [url_len u32 LE][url][header_count u8]
|
|
965
1102
|
// ([name_len u8][name][value_len u32 LE][value])*
|
|
@@ -987,11 +1124,12 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
987
1124
|
// otherwise find this named op first and leave the
|
|
988
1125
|
// stream running. The live stream owns the key, so
|
|
989
1126
|
// reject the newcomer through its own err arm.
|
|
990
|
-
if (head.key.len > 0 and findStream(head.key) != null) {
|
|
1127
|
+
if (head.key.len > 0 and (findStream(head.key) != null or fileStreamOccupiesKey(head.key))) {
|
|
991
1128
|
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
992
1129
|
} else {
|
|
1130
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
993
1131
|
fx.fetch(.{
|
|
994
|
-
.key = effect_key_base +
|
|
1132
|
+
.key = effect_key_base + effect_index,
|
|
995
1133
|
.method = method,
|
|
996
1134
|
.url = url,
|
|
997
1135
|
.headers = headers[0..header_count],
|
|
@@ -1019,8 +1157,9 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1019
1157
|
// clip_read [op][key_len][key][ok][err]
|
|
1020
1158
|
0x0B => {
|
|
1021
1159
|
const head = takeRoutedHead(cmd, &at);
|
|
1160
|
+
const effect_index = allocEffectEntry(fx, head) orelse continue;
|
|
1022
1161
|
fx.readClipboard(.{
|
|
1023
|
-
.key = effect_key_base +
|
|
1162
|
+
.key = effect_key_base + effect_index,
|
|
1024
1163
|
.on_result = clipboardResultMsg,
|
|
1025
1164
|
});
|
|
1026
1165
|
},
|
|
@@ -1132,12 +1271,13 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1132
1271
|
const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
|
|
1133
1272
|
runImageUnregister(fx, id_value);
|
|
1134
1273
|
},
|
|
1135
|
-
// channel_open [op][key f64 LE][event_tag]
|
|
1274
|
+
// channel_open [op][key f64 LE][event_tag][max_pending]
|
|
1136
1275
|
0x15 => {
|
|
1137
1276
|
const key_bits = takeBytes(cmd, &at, 8);
|
|
1138
1277
|
const key_value: f64 = @bitCast(std.mem.readInt(u64, key_bits[0..8], .little));
|
|
1139
1278
|
const event_tag = takeByte(cmd, &at);
|
|
1140
|
-
|
|
1279
|
+
const max_pending = takeByte(cmd, &at);
|
|
1280
|
+
_ = issueChannelOpen(fx, key_value, event_tag, max_pending);
|
|
1141
1281
|
},
|
|
1142
1282
|
// channel_close [op][key f64 LE]
|
|
1143
1283
|
0x16 => {
|
|
@@ -1275,6 +1415,24 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1275
1415
|
.body = body,
|
|
1276
1416
|
});
|
|
1277
1417
|
},
|
|
1418
|
+
// actionable_notification [op 0x31][id/title/subtitle/body/
|
|
1419
|
+
// action_label/action_command as u32-length bytes]
|
|
1420
|
+
0x31 => {
|
|
1421
|
+
const notification_id = takeLongBytes(cmd, &at);
|
|
1422
|
+
const title = takeLongBytes(cmd, &at);
|
|
1423
|
+
const subtitle = takeLongBytes(cmd, &at);
|
|
1424
|
+
const body = takeLongBytes(cmd, &at);
|
|
1425
|
+
const action_label = takeLongBytes(cmd, &at);
|
|
1426
|
+
const action_command = takeLongBytes(cmd, &at);
|
|
1427
|
+
fx.showNotification(.{
|
|
1428
|
+
.id = notification_id,
|
|
1429
|
+
.title = title,
|
|
1430
|
+
.subtitle = subtitle,
|
|
1431
|
+
.body = body,
|
|
1432
|
+
.action_label = action_label,
|
|
1433
|
+
.action_command = action_command,
|
|
1434
|
+
});
|
|
1435
|
+
},
|
|
1278
1436
|
// audio_capture_start [op][key f64 LE][source u8]
|
|
1279
1437
|
// [sample_rate u32 LE][channels u8]
|
|
1280
1438
|
// [event_tag u8]
|
|
@@ -1337,6 +1495,177 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1337
1495
|
.max_line_bytes = if (max_line_bytes == 0) runtime_effects.max_effect_line_bytes else max_line_bytes,
|
|
1338
1496
|
});
|
|
1339
1497
|
},
|
|
1498
|
+
// window_hide [op][label_len][label]
|
|
1499
|
+
0x21 => {
|
|
1500
|
+
const label = takeShortBytes(cmd, &at);
|
|
1501
|
+
fx.hideWindow(label);
|
|
1502
|
+
},
|
|
1503
|
+
// dock_presence [op][visible u8]
|
|
1504
|
+
0x22 => fx.setDockPresence(takeByte(cmd, &at) != 0),
|
|
1505
|
+
// service_stream_request [op][channel_key f64 LE]
|
|
1506
|
+
// [event_tag][max_pending]
|
|
1507
|
+
// [name_len][name][key_len][key]
|
|
1508
|
+
// [ok_tag][err_tag]
|
|
1509
|
+
// [payload_len u32 LE][payload]
|
|
1510
|
+
0x28 => {
|
|
1511
|
+
const channel_bits = takeBytes(cmd, &at, 8);
|
|
1512
|
+
const channel_value: f64 = @bitCast(std.mem.readInt(u64, channel_bits[0..8], .little));
|
|
1513
|
+
const event_tag = takeByte(cmd, &at);
|
|
1514
|
+
const max_pending = takeByte(cmd, &at);
|
|
1515
|
+
const name = takeShortBytes(cmd, &at);
|
|
1516
|
+
const key = takeShortBytes(cmd, &at);
|
|
1517
|
+
const ok_tag = takeByte(cmd, &at);
|
|
1518
|
+
const err_tag = takeByte(cmd, &at);
|
|
1519
|
+
const payload = takeLongBytes(cmd, &at);
|
|
1520
|
+
issueServiceStreamRequest(fx, name, key, ok_tag, err_tag, channel_value, event_tag, max_pending, payload);
|
|
1521
|
+
},
|
|
1522
|
+
// store_set [op][route][scope u32][key bytes][value bytes]
|
|
1523
|
+
0x23 => {
|
|
1524
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1525
|
+
const scope = takeU32(cmd, &at);
|
|
1526
|
+
if (scope != 0) @panic("ts core host: record-store scope is reserved and must be zero in cmd format v3");
|
|
1527
|
+
const record_key = takeLongBytes(cmd, &at);
|
|
1528
|
+
const bytes = takeLongBytes(cmd, &at);
|
|
1529
|
+
const request_key = allocStoreRequestEntry(fx, head, true) orelse continue;
|
|
1530
|
+
fx.storeSet(.{
|
|
1531
|
+
.key = request_key,
|
|
1532
|
+
.record_key = record_key,
|
|
1533
|
+
.bytes = bytes,
|
|
1534
|
+
.on_result = hostResultMsg,
|
|
1535
|
+
});
|
|
1536
|
+
},
|
|
1537
|
+
// store_get [op][route][scope u32][key bytes]
|
|
1538
|
+
0x24 => {
|
|
1539
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1540
|
+
const scope = takeU32(cmd, &at);
|
|
1541
|
+
if (scope != 0) @panic("ts core host: record-store scope is reserved and must be zero in cmd format v3");
|
|
1542
|
+
const record_key = takeLongBytes(cmd, &at);
|
|
1543
|
+
const request_key = allocStoreRequestEntry(fx, head, false) orelse continue;
|
|
1544
|
+
fx.storeGet(.{
|
|
1545
|
+
.key = request_key,
|
|
1546
|
+
.record_key = record_key,
|
|
1547
|
+
.on_result = hostResultMsg,
|
|
1548
|
+
});
|
|
1549
|
+
},
|
|
1550
|
+
// store_delete [op][route][scope u32][key bytes]
|
|
1551
|
+
0x25 => {
|
|
1552
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1553
|
+
const scope = takeU32(cmd, &at);
|
|
1554
|
+
if (scope != 0) @panic("ts core host: record-store scope is reserved and must be zero in cmd format v3");
|
|
1555
|
+
const record_key = takeLongBytes(cmd, &at);
|
|
1556
|
+
const request_key = allocStoreRequestEntry(fx, head, true) orelse continue;
|
|
1557
|
+
fx.storeDelete(.{
|
|
1558
|
+
.key = request_key,
|
|
1559
|
+
.record_key = record_key,
|
|
1560
|
+
.on_result = hostResultMsg,
|
|
1561
|
+
});
|
|
1562
|
+
},
|
|
1563
|
+
// store_scan [op][route][scope u32][prefix bytes]
|
|
1564
|
+
// [limit u32][after bytes]
|
|
1565
|
+
0x26 => {
|
|
1566
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1567
|
+
const scope = takeU32(cmd, &at);
|
|
1568
|
+
if (scope != 0) @panic("ts core host: record-store scope is reserved and must be zero in cmd format v3");
|
|
1569
|
+
const prefix = takeLongBytes(cmd, &at);
|
|
1570
|
+
const limit = takeU32(cmd, &at);
|
|
1571
|
+
const after = takeLongBytes(cmd, &at);
|
|
1572
|
+
const request_key = allocStoreRequestEntry(fx, head, false) orelse continue;
|
|
1573
|
+
fx.storeScan(.{
|
|
1574
|
+
.key = request_key,
|
|
1575
|
+
.prefix = prefix,
|
|
1576
|
+
.limit = limit,
|
|
1577
|
+
.after = after,
|
|
1578
|
+
.on_result = hostResultMsg,
|
|
1579
|
+
});
|
|
1580
|
+
},
|
|
1581
|
+
// store_set_many [op][route][scope u32][count u32]
|
|
1582
|
+
// [count * (key bytes,value bytes)]
|
|
1583
|
+
0x27 => {
|
|
1584
|
+
const head = takeRoutedHead(cmd, &at);
|
|
1585
|
+
const scope = takeU32(cmd, &at);
|
|
1586
|
+
if (scope != 0) @panic("ts core host: record-store scope is reserved and must be zero in cmd format v3");
|
|
1587
|
+
const count: usize = @intCast(takeU32(cmd, &at));
|
|
1588
|
+
var entries: [runtime_effects.max_effect_store_batch_entries]Fx.StoreEntry = undefined;
|
|
1589
|
+
var kept: usize = 0;
|
|
1590
|
+
for (0..count) |_| {
|
|
1591
|
+
const record_key = takeLongBytes(cmd, &at);
|
|
1592
|
+
const bytes = takeLongBytes(cmd, &at);
|
|
1593
|
+
if (kept < entries.len) {
|
|
1594
|
+
entries[kept] = .{ .key = record_key, .bytes = bytes };
|
|
1595
|
+
kept += 1;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
const request_key = allocStoreRequestEntry(fx, head, true) orelse continue;
|
|
1599
|
+
fx.storeSetMany(.{
|
|
1600
|
+
.key = request_key,
|
|
1601
|
+
.entries = if (count <= entries.len) entries[0..kept] else &.{},
|
|
1602
|
+
.on_result = hostResultMsg,
|
|
1603
|
+
});
|
|
1604
|
+
},
|
|
1605
|
+
// db_query [op][key][page][done][err][sql bytes]
|
|
1606
|
+
// [param count u32][tagged params]
|
|
1607
|
+
0x29 => {
|
|
1608
|
+
const key = takeShortBytes(cmd, &at);
|
|
1609
|
+
const page_tag = takeByte(cmd, &at);
|
|
1610
|
+
const done_tag = takeByte(cmd, &at);
|
|
1611
|
+
const err_tag = takeByte(cmd, &at);
|
|
1612
|
+
const sql = takeLongBytes(cmd, &at);
|
|
1613
|
+
const count: usize = @intCast(takeU32(cmd, &at));
|
|
1614
|
+
var params: [runtime_effects.max_effect_db_parameters]runtime_effects.EffectDbValue = undefined;
|
|
1615
|
+
var kept: usize = 0;
|
|
1616
|
+
for (0..count) |_| {
|
|
1617
|
+
const value = takeDbValue(cmd, &at);
|
|
1618
|
+
if (kept < params.len) {
|
|
1619
|
+
params[kept] = value;
|
|
1620
|
+
kept += 1;
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
const index = allocDbEntry(fx, key, true, page_tag, done_tag, err_tag) orelse continue;
|
|
1624
|
+
fx.dbQuery(.{
|
|
1625
|
+
.key = db_key_base + index,
|
|
1626
|
+
.sql = if (count <= params.len) sql else "",
|
|
1627
|
+
.params = if (count <= params.len) params[0..kept] else &.{},
|
|
1628
|
+
.on_result = dbResultMsg,
|
|
1629
|
+
});
|
|
1630
|
+
},
|
|
1631
|
+
// db_exec [op][key][ok][err][statement count u32]
|
|
1632
|
+
// [statement sql bytes][param count u32][params]...
|
|
1633
|
+
0x2A => {
|
|
1634
|
+
const key = takeShortBytes(cmd, &at);
|
|
1635
|
+
const ok_tag = takeByte(cmd, &at);
|
|
1636
|
+
const err_tag = takeByte(cmd, &at);
|
|
1637
|
+
const count: usize = @intCast(takeU32(cmd, &at));
|
|
1638
|
+
var statements: [runtime_effects.max_effect_db_exec_statements]runtime_effects.EffectDbStatement = undefined;
|
|
1639
|
+
var values: [runtime_effects.max_effect_db_exec_statements * runtime_effects.max_effect_db_parameters]runtime_effects.EffectDbValue = undefined;
|
|
1640
|
+
var statement_kept: usize = 0;
|
|
1641
|
+
var value_kept: usize = 0;
|
|
1642
|
+
var valid = count <= statements.len;
|
|
1643
|
+
for (0..count) |_| {
|
|
1644
|
+
const sql = takeLongBytes(cmd, &at);
|
|
1645
|
+
const param_count: usize = @intCast(takeU32(cmd, &at));
|
|
1646
|
+
const start = value_kept;
|
|
1647
|
+
if (param_count > runtime_effects.max_effect_db_parameters) valid = false;
|
|
1648
|
+
for (0..param_count) |_| {
|
|
1649
|
+
const value = takeDbValue(cmd, &at);
|
|
1650
|
+
if (value_kept < values.len) {
|
|
1651
|
+
values[value_kept] = value;
|
|
1652
|
+
value_kept += 1;
|
|
1653
|
+
} else {
|
|
1654
|
+
valid = false;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
if (statement_kept < statements.len and start + param_count <= value_kept) {
|
|
1658
|
+
statements[statement_kept] = .{ .sql = sql, .params = values[start .. start + param_count] };
|
|
1659
|
+
statement_kept += 1;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
const index = allocDbEntry(fx, key, false, 0, ok_tag, err_tag) orelse continue;
|
|
1663
|
+
fx.dbExec(.{
|
|
1664
|
+
.key = db_key_base + index,
|
|
1665
|
+
.statements = if (valid and count == statement_kept) statements[0..statement_kept] else &.{},
|
|
1666
|
+
.on_result = dbResultMsg,
|
|
1667
|
+
});
|
|
1668
|
+
},
|
|
1340
1669
|
else => @panic("ts core host: unknown command wire record - the core and this runtime disagree on cmd_format_version"),
|
|
1341
1670
|
}
|
|
1342
1671
|
}
|
|
@@ -1352,6 +1681,24 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1352
1681
|
return .{ .key = key, .ok_tag = ok_tag, .err_tag = err_tag };
|
|
1353
1682
|
}
|
|
1354
1683
|
|
|
1684
|
+
fn takeDbValue(cmd: []const u8, at: *usize) runtime_effects.EffectDbValue {
|
|
1685
|
+
return switch (takeByte(cmd, at)) {
|
|
1686
|
+
0 => .null_value,
|
|
1687
|
+
1 => blk: {
|
|
1688
|
+
const raw = takeBytes(cmd, at, 8);
|
|
1689
|
+
const number: f64 = @bitCast(std.mem.readInt(u64, raw[0..8], .little));
|
|
1690
|
+
if (std.math.isFinite(number) and @trunc(number) == number and number >= -9_007_199_254_740_991.0 and number <= 9_007_199_254_740_991.0) {
|
|
1691
|
+
break :blk .{ .integer = @intFromFloat(number) };
|
|
1692
|
+
}
|
|
1693
|
+
break :blk .{ .real = number };
|
|
1694
|
+
},
|
|
1695
|
+
2 => .{ .text = takeLongBytes(cmd, at) },
|
|
1696
|
+
3 => .{ .blob = takeLongBytes(cmd, at) },
|
|
1697
|
+
4 => .{ .integer = if (takeByte(cmd, at) == 0) 0 else 1 },
|
|
1698
|
+
else => @panic("ts core host: unknown SQLite parameter tag - the core and runtime disagree on cmd_format_version"),
|
|
1699
|
+
};
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1355
1702
|
fn fetchMethod(wire: u8) std.http.Method {
|
|
1356
1703
|
return switch (wire) {
|
|
1357
1704
|
0 => .GET,
|
|
@@ -1372,8 +1719,12 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1372
1719
|
/// mirrors the engine's slot count, which cannot hold more in
|
|
1373
1720
|
/// flight either (a dropped entry holds its slot only until
|
|
1374
1721
|
/// its `.cancelled` terminal drains).
|
|
1375
|
-
fn allocEffectEntry(fx: *Fx, head: RoutedHead) u64 {
|
|
1722
|
+
fn allocEffectEntry(fx: *Fx, head: RoutedHead) ?u64 {
|
|
1376
1723
|
if (head.key.len > 0) {
|
|
1724
|
+
if (fileStreamOccupiesKey(head.key)) {
|
|
1725
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
1726
|
+
return null;
|
|
1727
|
+
}
|
|
1377
1728
|
if (findEffect(head.key)) |existing| dropEffectEntry(fx, existing);
|
|
1378
1729
|
}
|
|
1379
1730
|
const index = freeEffectIndex() orelse
|
|
@@ -1417,6 +1768,10 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1417
1768
|
/// slot: the engine timer replaces in place under the same
|
|
1418
1769
|
/// engine key and restarts from now — the debounce discipline.
|
|
1419
1770
|
fn armDelay(fx: *Fx, key: []const u8, after_ms: f64, tag: u8) void {
|
|
1771
|
+
// A delay has no err arm. Preserve an incumbent file stream and
|
|
1772
|
+
// fail closed instead of creating a second owner that Cmd.cancel
|
|
1773
|
+
// could not address unambiguously.
|
|
1774
|
+
if (fileStreamOccupiesKey(key)) return;
|
|
1420
1775
|
const index = blk: {
|
|
1421
1776
|
if (key.len > 0) {
|
|
1422
1777
|
if (findDelay(key)) |existing| break :blk existing;
|
|
@@ -1470,7 +1825,7 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1470
1825
|
argv: []const []const u8,
|
|
1471
1826
|
stdin: []const u8,
|
|
1472
1827
|
) void {
|
|
1473
|
-
if (head.key.len > 0 and findStream(head.key) != null) {
|
|
1828
|
+
if (head.key.len > 0 and (findStream(head.key) != null or fileStreamOccupiesKey(head.key))) {
|
|
1474
1829
|
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
1475
1830
|
return;
|
|
1476
1831
|
}
|
|
@@ -1517,7 +1872,7 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1517
1872
|
/// two HTTP responses into one app-owned stream.
|
|
1518
1873
|
fn issueFetchStream(fx: *Fx, head: SpawnHead, options: FetchStreamOptions) void {
|
|
1519
1874
|
if (head.key.len > 0 and
|
|
1520
|
-
(findStream(head.key) != null or findEffect(head.key) != null))
|
|
1875
|
+
(findStream(head.key) != null or findEffect(head.key) != null or fileStreamOccupiesKey(head.key)))
|
|
1521
1876
|
{
|
|
1522
1877
|
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
1523
1878
|
return;
|
|
@@ -1564,6 +1919,148 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1564
1919
|
return null;
|
|
1565
1920
|
}
|
|
1566
1921
|
|
|
1922
|
+
fn findFileStream(key: []const u8) ?usize {
|
|
1923
|
+
for (&file_streams, 0..) |*entry, index| {
|
|
1924
|
+
if (entry.used and std.mem.eql(u8, entry.wireKey(), key)) return index;
|
|
1925
|
+
}
|
|
1926
|
+
return null;
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
fn fileStreamOccupiesKey(key: []const u8) bool {
|
|
1930
|
+
return key.len > 0 and findFileStream(key) != null;
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
/// Every string-keyed command family shares one authored key surface.
|
|
1934
|
+
/// File streams use a distinct numeric engine namespace, so admission
|
|
1935
|
+
/// must consult the bridge tables explicitly before claiming a slot.
|
|
1936
|
+
fn wireKeyOccupiedOutsideFileStreams(key: []const u8) bool {
|
|
1937
|
+
if (key.len == 0) return false;
|
|
1938
|
+
return findRequest(key) != null or
|
|
1939
|
+
findEffect(key) != null or
|
|
1940
|
+
findStream(key) != null or
|
|
1941
|
+
findDelay(key) != null or
|
|
1942
|
+
findPty(key) != null or
|
|
1943
|
+
findDb(key) != null;
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
fn freeFileStreamIndex() ?usize {
|
|
1947
|
+
for (&file_streams, 0..) |*entry, index| if (!entry.used) return index;
|
|
1948
|
+
return null;
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
fn issueReadFileStream(fx: *Fx, key: []const u8, chunk_tag: u8, done_tag: u8, err_tag: u8, path: []const u8) void {
|
|
1952
|
+
if (wireKeyOccupiedOutsideFileStreams(key)) {
|
|
1953
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
1954
|
+
return;
|
|
1955
|
+
}
|
|
1956
|
+
const index = if (key.len > 0 and findFileStream(key) != null) replace: {
|
|
1957
|
+
const existing = findFileStream(key).?;
|
|
1958
|
+
if (file_streams[existing].sink) {
|
|
1959
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
1960
|
+
return;
|
|
1961
|
+
}
|
|
1962
|
+
// The engine retires the old read generation silently when
|
|
1963
|
+
// this same engine key is reissued. Reuse the bridge slot so
|
|
1964
|
+
// the replacement's tags become authoritative atomically.
|
|
1965
|
+
break :replace existing;
|
|
1966
|
+
} else freeFileStreamIndex() orelse {
|
|
1967
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
1968
|
+
return;
|
|
1969
|
+
};
|
|
1970
|
+
const entry = &file_streams[index];
|
|
1971
|
+
entry.* = .{ .used = true, .key_len = key.len, .chunk_tag = chunk_tag, .done_tag = done_tag, .err_tag = err_tag };
|
|
1972
|
+
@memcpy(entry.key[0..key.len], key);
|
|
1973
|
+
fx.readFileStream(.{ .key = file_stream_key_base + index, .path = path, .on_result = fileStreamResultMsg });
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
fn issueWriteFileStream(fx: *Fx, head: RoutedHead, path: []const u8) void {
|
|
1977
|
+
if (head.key.len == 0 or findFileStream(head.key) != null or wireKeyOccupiedOutsideFileStreams(head.key)) {
|
|
1978
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
const index = freeFileStreamIndex() orelse {
|
|
1982
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
1983
|
+
return;
|
|
1984
|
+
};
|
|
1985
|
+
const entry = &file_streams[index];
|
|
1986
|
+
entry.* = .{ .used = true, .sink = true, .busy = true, .key_len = head.key.len, .done_tag = head.ok_tag, .err_tag = head.err_tag };
|
|
1987
|
+
@memcpy(entry.key[0..head.key.len], head.key);
|
|
1988
|
+
fx.writeFileStream(.{ .key = file_stream_key_base + index, .path = path, .on_result = fileStreamResultMsg });
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
fn issueWriteFileChunk(fx: *Fx, head: RoutedHead, bytes: []const u8) void {
|
|
1992
|
+
const index = findFileStream(head.key) orelse {
|
|
1993
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "sink_missing"));
|
|
1994
|
+
return;
|
|
1995
|
+
};
|
|
1996
|
+
if (!file_streams[index].sink) {
|
|
1997
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "sink_missing"));
|
|
1998
|
+
return;
|
|
1999
|
+
}
|
|
2000
|
+
if (file_streams[index].cancelling) {
|
|
2001
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "sink_missing"));
|
|
2002
|
+
return;
|
|
2003
|
+
}
|
|
2004
|
+
if (file_streams[index].busy) {
|
|
2005
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "out_of_order"));
|
|
2006
|
+
return;
|
|
2007
|
+
}
|
|
2008
|
+
file_streams[index].busy = true;
|
|
2009
|
+
file_streams[index].done_tag = head.ok_tag;
|
|
2010
|
+
file_streams[index].err_tag = head.err_tag;
|
|
2011
|
+
fx.writeFileChunk(.{ .key = file_stream_key_base + index, .bytes = bytes, .on_result = fileStreamResultMsg });
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
fn issueWriteFileClose(fx: *Fx, head: RoutedHead) void {
|
|
2015
|
+
const index = findFileStream(head.key) orelse {
|
|
2016
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "sink_missing"));
|
|
2017
|
+
return;
|
|
2018
|
+
};
|
|
2019
|
+
if (!file_streams[index].sink) {
|
|
2020
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "sink_missing"));
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
2023
|
+
if (file_streams[index].cancelling) {
|
|
2024
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "sink_missing"));
|
|
2025
|
+
return;
|
|
2026
|
+
}
|
|
2027
|
+
if (file_streams[index].busy) {
|
|
2028
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "out_of_order"));
|
|
2029
|
+
return;
|
|
2030
|
+
}
|
|
2031
|
+
file_streams[index].busy = true;
|
|
2032
|
+
file_streams[index].done_tag = head.ok_tag;
|
|
2033
|
+
file_streams[index].err_tag = head.err_tag;
|
|
2034
|
+
fx.writeFileClose(.{ .key = file_stream_key_base + index, .on_result = fileStreamResultMsg });
|
|
2035
|
+
}
|
|
2036
|
+
|
|
2037
|
+
fn fileStreamResultMsg(result: runtime_effects.EffectFileResult) Msg {
|
|
2038
|
+
if (result.key < file_stream_key_base) @panic("ts core host: file stream result outside its namespace");
|
|
2039
|
+
const index = result.key - file_stream_key_base;
|
|
2040
|
+
if (index >= file_streams.len or !file_streams[index].used) @panic("ts core host: untracked file stream result");
|
|
2041
|
+
const entry = &file_streams[index];
|
|
2042
|
+
if (entry.cancelling and result.outcome == .cancelled) {
|
|
2043
|
+
entry.used = false;
|
|
2044
|
+
return msgFromTagBytes(entry.err_tag, "cancelled");
|
|
2045
|
+
}
|
|
2046
|
+
if (result.op == .read_stream and result.event == .chunk and result.outcome == .ok) return msgFromTagBytes(entry.chunk_tag, result.bytes);
|
|
2047
|
+
if (result.op == .read_stream and result.event == .done and result.outcome == .ok) {
|
|
2048
|
+
entry.used = false;
|
|
2049
|
+
return msgFromTagNumber(entry.done_tag, @floatFromInt(result.total));
|
|
2050
|
+
}
|
|
2051
|
+
if (result.outcome == .ok) {
|
|
2052
|
+
entry.busy = false;
|
|
2053
|
+
if (result.op == .write_stream_close) entry.used = false;
|
|
2054
|
+
return msgFromTagVoid(entry.done_tag);
|
|
2055
|
+
}
|
|
2056
|
+
if (result.op == .write_stream_chunk and (result.outcome == .rejected or result.outcome == .out_of_order)) {
|
|
2057
|
+
entry.busy = false;
|
|
2058
|
+
return msgFromTagBytes(entry.err_tag, @tagName(result.outcome));
|
|
2059
|
+
}
|
|
2060
|
+
entry.used = false;
|
|
2061
|
+
return msgFromTagBytes(entry.err_tag, @tagName(result.outcome));
|
|
2062
|
+
}
|
|
2063
|
+
|
|
1567
2064
|
/// The stream entry an engine spawn/fetch result names — looked up
|
|
1568
2065
|
/// WITHOUT retiring (lines flow through it repeatedly; only the
|
|
1569
2066
|
/// terminal retires it).
|
|
@@ -1926,22 +2423,23 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1926
2423
|
fx: *Fx,
|
|
1927
2424
|
key_value: f64,
|
|
1928
2425
|
event_tag: u8,
|
|
1929
|
-
|
|
2426
|
+
max_pending: u8,
|
|
2427
|
+
) bool {
|
|
1930
2428
|
const representable = std.math.isFinite(key_value) and
|
|
1931
2429
|
key_value >= 1 and key_value < 9007199254740992.0 and
|
|
1932
2430
|
@floor(key_value) == key_value;
|
|
1933
2431
|
if (!representable) {
|
|
1934
2432
|
fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = 0, .kind = .rejected }));
|
|
1935
|
-
return;
|
|
2433
|
+
return false;
|
|
1936
2434
|
}
|
|
1937
2435
|
const key: u64 = @intFromFloat(key_value);
|
|
1938
2436
|
if (findChannel(key) != null) {
|
|
1939
2437
|
fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = key, .kind = .rejected }));
|
|
1940
|
-
return;
|
|
2438
|
+
return false;
|
|
1941
2439
|
}
|
|
1942
2440
|
const index = freeChannelIndex() orelse {
|
|
1943
2441
|
fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = key, .kind = .rejected }));
|
|
1944
|
-
return;
|
|
2442
|
+
return false;
|
|
1945
2443
|
};
|
|
1946
2444
|
const entry = &channels[index];
|
|
1947
2445
|
entry.used = true;
|
|
@@ -1950,7 +2448,13 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
1950
2448
|
_ = fx.openChannel(.{
|
|
1951
2449
|
.key = key,
|
|
1952
2450
|
.on_event = channelEventMsg,
|
|
2451
|
+
.max_pending = max_pending,
|
|
1953
2452
|
});
|
|
2453
|
+
// A non-null lookup means the engine accepted the occupancy in
|
|
2454
|
+
// both live execution and replay (whose accepted handle is inert).
|
|
2455
|
+
// Validation and capacity refusals stage their rejection without
|
|
2456
|
+
// leaving an open slot.
|
|
2457
|
+
return fx.channelHandle(key) != null;
|
|
1954
2458
|
}
|
|
1955
2459
|
|
|
1956
2460
|
fn findChannel(key: u64) ?usize {
|
|
@@ -2123,7 +2627,7 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2123
2627
|
term: []const u8,
|
|
2124
2628
|
argv: []const []const u8,
|
|
2125
2629
|
) void {
|
|
2126
|
-
if (key.len > 0 and findPty(key) != null) {
|
|
2630
|
+
if (key.len > 0 and (findPty(key) != null or fileStreamOccupiesKey(key))) {
|
|
2127
2631
|
// The rejection is STAGED (delivered a later frame), so its
|
|
2128
2632
|
// key must be self-contained: the wire key points into this
|
|
2129
2633
|
// dispatch's command buffer, gone by delivery, so intern
|
|
@@ -2228,6 +2732,86 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2228
2732
|
return msgFromTagPty(entry.event_tag, wire_key, false, event);
|
|
2229
2733
|
}
|
|
2230
2734
|
|
|
2735
|
+
fn allocDbEntry(
|
|
2736
|
+
fx: *Fx,
|
|
2737
|
+
key: []const u8,
|
|
2738
|
+
query: bool,
|
|
2739
|
+
page_tag: u8,
|
|
2740
|
+
done_tag: u8,
|
|
2741
|
+
err_tag: u8,
|
|
2742
|
+
) ?usize {
|
|
2743
|
+
if (fileStreamOccupiesKey(key)) {
|
|
2744
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
2745
|
+
return null;
|
|
2746
|
+
}
|
|
2747
|
+
const index = blk: {
|
|
2748
|
+
if (key.len > 0) {
|
|
2749
|
+
if (findDb(key)) |existing| {
|
|
2750
|
+
// One-shot queries replace only earlier one-shot
|
|
2751
|
+
// queries. A live subscription owns its slot until
|
|
2752
|
+
// subscription reconciliation removes or re-arms it;
|
|
2753
|
+
// a command with the same wire key must reject rather
|
|
2754
|
+
// than silently erase that subscription.
|
|
2755
|
+
if (query and dbs[existing].query and !dbs[existing].live) break :blk existing;
|
|
2756
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
2757
|
+
return null;
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
break :blk freeDbIndex() orelse {
|
|
2761
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
2762
|
+
return null;
|
|
2763
|
+
};
|
|
2764
|
+
};
|
|
2765
|
+
const entry = &dbs[index];
|
|
2766
|
+
entry.used = true;
|
|
2767
|
+
entry.query = query;
|
|
2768
|
+
entry.live = false;
|
|
2769
|
+
entry.signature = 0;
|
|
2770
|
+
entry.key_len = key.len;
|
|
2771
|
+
@memcpy(entry.key[0..key.len], key);
|
|
2772
|
+
entry.page_tag = page_tag;
|
|
2773
|
+
entry.done_tag = done_tag;
|
|
2774
|
+
entry.err_tag = err_tag;
|
|
2775
|
+
return index;
|
|
2776
|
+
}
|
|
2777
|
+
|
|
2778
|
+
fn findDb(key: []const u8) ?usize {
|
|
2779
|
+
if (key.len == 0) return null;
|
|
2780
|
+
for (&dbs, 0..) |*entry, index| {
|
|
2781
|
+
if (entry.used and std.mem.eql(u8, entry.wireKey(), key)) return index;
|
|
2782
|
+
}
|
|
2783
|
+
return null;
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
fn freeDbIndex() ?usize {
|
|
2787
|
+
for (&dbs, 0..) |*entry, index| if (!entry.used) return index;
|
|
2788
|
+
return null;
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
fn dbResultMsg(result: runtime_effects.EffectDbResult) Msg {
|
|
2792
|
+
if (result.key < db_key_base) @panic("ts core host: a relational result arrived outside the bridge DB key namespace");
|
|
2793
|
+
const index = result.key - db_key_base;
|
|
2794
|
+
if (index >= dbs.len or !dbs[index].used) @panic("ts core host: a relational result arrived for an untracked command");
|
|
2795
|
+
const entry = &dbs[index];
|
|
2796
|
+
if (result.outcome != .ok) {
|
|
2797
|
+
if (!entry.live) entry.used = false;
|
|
2798
|
+
return msgFromTagBytes(entry.err_tag, @tagName(result.outcome));
|
|
2799
|
+
}
|
|
2800
|
+
return switch (result.kind) {
|
|
2801
|
+
.page => msgFromTagBytes(entry.page_tag, result.bytes),
|
|
2802
|
+
.done => blk: {
|
|
2803
|
+
if (!entry.query) @panic("ts core host: a query terminal reached an exec route");
|
|
2804
|
+
if (!entry.live) entry.used = false;
|
|
2805
|
+
break :blk msgFromTagVoid(entry.done_tag);
|
|
2806
|
+
},
|
|
2807
|
+
.exec => blk: {
|
|
2808
|
+
if (entry.query) @panic("ts core host: an exec terminal reached a query route");
|
|
2809
|
+
entry.used = false;
|
|
2810
|
+
break :blk msgFromTagVoid(entry.done_tag);
|
|
2811
|
+
},
|
|
2812
|
+
};
|
|
2813
|
+
}
|
|
2814
|
+
|
|
2231
2815
|
/// The wire `cancel` record: first match wins across the four
|
|
2232
2816
|
/// keyed tables — requests (silent drop), named engine ops
|
|
2233
2817
|
/// (silent drop: the entry is marked dropped, the engine's
|
|
@@ -2241,8 +2825,13 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2241
2825
|
fn cancelWireKey(fx: *Fx, key: []const u8) void {
|
|
2242
2826
|
if (key.len == 0) return;
|
|
2243
2827
|
if (findRequest(key)) |index| {
|
|
2828
|
+
const entry = requests[index];
|
|
2244
2829
|
fx.cancelHostRequest(request_key_base + index);
|
|
2245
2830
|
requests[index].used = false;
|
|
2831
|
+
if (entry.service_channel) |channel_key| {
|
|
2832
|
+
fx.closeChannel(channel_key);
|
|
2833
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(entry.err_tag, "cancelled"));
|
|
2834
|
+
}
|
|
2246
2835
|
return;
|
|
2247
2836
|
}
|
|
2248
2837
|
if (findEffect(key)) |index| {
|
|
@@ -2255,24 +2844,72 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2255
2844
|
fx.cancel(spawn_key_base + index);
|
|
2256
2845
|
return;
|
|
2257
2846
|
}
|
|
2847
|
+
if (findFileStream(key)) |index| {
|
|
2848
|
+
if (!file_streams[index].sink) {
|
|
2849
|
+
// Read streams are file-style: cancel is silent and the
|
|
2850
|
+
// bridge entry retires immediately. Sinks remain loud.
|
|
2851
|
+
file_streams[index].used = false;
|
|
2852
|
+
} else {
|
|
2853
|
+
file_streams[index].cancelling = true;
|
|
2854
|
+
}
|
|
2855
|
+
fx.cancel(file_stream_key_base + index);
|
|
2856
|
+
return;
|
|
2857
|
+
}
|
|
2258
2858
|
if (findDelay(key)) |index| {
|
|
2259
2859
|
fx.cancelTimer(delay_key_base + index);
|
|
2260
2860
|
delays[index].used = false;
|
|
2861
|
+
return;
|
|
2261
2862
|
}
|
|
2863
|
+
if (findDb(key)) |index| {
|
|
2864
|
+
// Declarative live queries belong exclusively to subscription
|
|
2865
|
+
// reconciliation. Cmd.cancel may share their public key, but
|
|
2866
|
+
// it must neither hide the bridge entry nor strand the
|
|
2867
|
+
// engine's `.live` slot; dropping the Sub below performs the
|
|
2868
|
+
// matching dbUnsubscribe.
|
|
2869
|
+
if (dbs[index].query and !dbs[index].live) {
|
|
2870
|
+
fx.cancelDbQuery(db_key_base + index);
|
|
2871
|
+
dbs[index].used = false;
|
|
2872
|
+
}
|
|
2873
|
+
// Transactions are already synchronous host work by the time
|
|
2874
|
+
// the next record in a batch is walked. Cancel never hides
|
|
2875
|
+
// their terminal (or makes the bridge forget its route).
|
|
2876
|
+
return;
|
|
2877
|
+
}
|
|
2878
|
+
}
|
|
2879
|
+
|
|
2880
|
+
const RequestPool = enum { host, store, credentials };
|
|
2881
|
+
|
|
2882
|
+
fn requestPoolAt(index: usize) RequestPool {
|
|
2883
|
+
if (index < runtime_effects.max_effects) return .host;
|
|
2884
|
+
if (index < runtime_effects.max_effects + runtime_effects.max_store_effects) return .store;
|
|
2885
|
+
return .credentials;
|
|
2262
2886
|
}
|
|
2263
2887
|
|
|
2264
|
-
///
|
|
2265
|
-
///
|
|
2266
|
-
///
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2888
|
+
/// Allocate a routed request slot. Host, record-store, and credential
|
|
2889
|
+
/// pools are disjoint, while wire keys still share one replacement
|
|
2890
|
+
/// namespace.
|
|
2891
|
+
fn allocRequestEntry(
|
|
2892
|
+
fx: *Fx,
|
|
2893
|
+
key: []const u8,
|
|
2894
|
+
ok_tag: u8,
|
|
2895
|
+
err_tag: u8,
|
|
2896
|
+
ok_void: bool,
|
|
2897
|
+
pool: RequestPool,
|
|
2898
|
+
) ?u64 {
|
|
2899
|
+
if (fileStreamOccupiesKey(key)) return null;
|
|
2270
2900
|
const index = blk: {
|
|
2271
2901
|
if (key.len > 0) {
|
|
2272
|
-
if (findRequest(key)) |existing|
|
|
2902
|
+
if (findRequest(key)) |existing| {
|
|
2903
|
+
if (requestPoolAt(existing) == pool) break :blk existing;
|
|
2904
|
+
fx.cancelHostRequest(request_key_base + existing);
|
|
2905
|
+
requests[existing].used = false;
|
|
2906
|
+
}
|
|
2273
2907
|
}
|
|
2274
|
-
break :blk
|
|
2275
|
-
|
|
2908
|
+
break :blk switch (pool) {
|
|
2909
|
+
.host => freeRequestIndex(),
|
|
2910
|
+
.store => freeStoreRequestIndex(),
|
|
2911
|
+
.credentials => freeCredentialsRequestIndex(),
|
|
2912
|
+
} orelse return null;
|
|
2276
2913
|
};
|
|
2277
2914
|
const entry = &requests[index];
|
|
2278
2915
|
entry.used = true;
|
|
@@ -2280,6 +2917,57 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2280
2917
|
@memcpy(entry.key[0..key.len], key);
|
|
2281
2918
|
entry.ok_tag = ok_tag;
|
|
2282
2919
|
entry.err_tag = err_tag;
|
|
2920
|
+
entry.ok_void = ok_void;
|
|
2921
|
+
entry.service_operation = null;
|
|
2922
|
+
entry.service_channel = null;
|
|
2923
|
+
return index;
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2926
|
+
fn allocStoreRequestEntry(fx: *Fx, head: RoutedHead, ok_void: bool) ?u64 {
|
|
2927
|
+
const index = allocRequestEntry(fx, head.key, head.ok_tag, head.err_tag, ok_void, .store) orelse {
|
|
2928
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
|
|
2929
|
+
return null;
|
|
2930
|
+
};
|
|
2931
|
+
return request_key_base + index;
|
|
2932
|
+
}
|
|
2933
|
+
|
|
2934
|
+
fn allocCredentialsRequestEntry(fx: *Fx, key: []const u8, ok_tag: u8, err_tag: u8, ok_void: bool) ?u64 {
|
|
2935
|
+
const index = allocRequestEntry(fx, key, ok_tag, err_tag, ok_void, .credentials) orelse {
|
|
2936
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
2937
|
+
return null;
|
|
2938
|
+
};
|
|
2939
|
+
return request_key_base + index;
|
|
2940
|
+
}
|
|
2941
|
+
|
|
2942
|
+
/// Issue (or replace) a routed request. Raw keyed requests reuse their
|
|
2943
|
+
/// live table entry when the bound host supports replacement. Typed
|
|
2944
|
+
/// services and reject-on-duplicate host bindings refuse before
|
|
2945
|
+
/// touching the original entry, so its eventual terminal still owns
|
|
2946
|
+
/// the route and table slot it started with. Unkeyed requests each
|
|
2947
|
+
/// take a fresh entry.
|
|
2948
|
+
fn issueRequest(fx: *Fx, name: []const u8, key: []const u8, ok_tag: u8, err_tag: u8, typed_service: bool, payload: []const u8) void {
|
|
2949
|
+
if (!typed_service and std.mem.startsWith(u8, name, "core.credentials.")) {
|
|
2950
|
+
issueCredentialsRequest(fx, name, key, ok_tag, err_tag, payload);
|
|
2951
|
+
return;
|
|
2952
|
+
}
|
|
2953
|
+
if (key.len > 0 and findRequest(key) != null and (typed_service or fx.rejectsDuplicateHostRequestKeys())) {
|
|
2954
|
+
stageRequestRejected(fx, err_tag);
|
|
2955
|
+
return;
|
|
2956
|
+
}
|
|
2957
|
+
const index = allocRequestEntry(fx, key, ok_tag, err_tag, false, .host) orelse {
|
|
2958
|
+
stageRequestRejected(fx, err_tag);
|
|
2959
|
+
return;
|
|
2960
|
+
};
|
|
2961
|
+
const entry = &requests[index];
|
|
2962
|
+
if (typed_service) if (service_results) |binding| {
|
|
2963
|
+
entry.service_operation = binding.index_fn(name);
|
|
2964
|
+
};
|
|
2965
|
+
if (entry.service_operation) |operation| {
|
|
2966
|
+
const binding = service_results.?;
|
|
2967
|
+
if (binding.streaming_fn(operation) and payload.len >= 8) {
|
|
2968
|
+
entry.service_channel = exactEngineKey(readF64(payload, 0));
|
|
2969
|
+
}
|
|
2970
|
+
}
|
|
2283
2971
|
fx.hostRequest(.{
|
|
2284
2972
|
.key = request_key_base + index,
|
|
2285
2973
|
.name = name,
|
|
@@ -2288,6 +2976,130 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2288
2976
|
});
|
|
2289
2977
|
}
|
|
2290
2978
|
|
|
2979
|
+
fn issueCredentialsRequest(fx: *Fx, name: []const u8, key: []const u8, ok_tag: u8, err_tag: u8, payload: []const u8) void {
|
|
2980
|
+
const is_set = std.mem.eql(u8, name, "core.credentials.set");
|
|
2981
|
+
const is_get = std.mem.eql(u8, name, "core.credentials.get");
|
|
2982
|
+
const is_delete = std.mem.eql(u8, name, "core.credentials.delete");
|
|
2983
|
+
if (!is_set and !is_get and !is_delete) {
|
|
2984
|
+
stageRequestRejected(fx, err_tag);
|
|
2985
|
+
return;
|
|
2986
|
+
}
|
|
2987
|
+
var at: usize = 0;
|
|
2988
|
+
const credential_key = takeCredentialBytes(payload, &at) orelse {
|
|
2989
|
+
stageRequestRejected(fx, err_tag);
|
|
2990
|
+
return;
|
|
2991
|
+
};
|
|
2992
|
+
if (is_set) {
|
|
2993
|
+
const secret = takeCredentialBytes(payload, &at) orelse {
|
|
2994
|
+
stageRequestRejected(fx, err_tag);
|
|
2995
|
+
return;
|
|
2996
|
+
};
|
|
2997
|
+
if (at != payload.len) {
|
|
2998
|
+
stageRequestRejected(fx, err_tag);
|
|
2999
|
+
return;
|
|
3000
|
+
}
|
|
3001
|
+
const request_key = allocCredentialsRequestEntry(fx, key, ok_tag, err_tag, true) orelse return;
|
|
3002
|
+
fx.credentialsSet(.{
|
|
3003
|
+
.key = request_key,
|
|
3004
|
+
.credential_key = credential_key,
|
|
3005
|
+
.secret = secret,
|
|
3006
|
+
.host_result = hostResultMsg,
|
|
3007
|
+
});
|
|
3008
|
+
return;
|
|
3009
|
+
}
|
|
3010
|
+
if (at != payload.len) {
|
|
3011
|
+
stageRequestRejected(fx, err_tag);
|
|
3012
|
+
return;
|
|
3013
|
+
}
|
|
3014
|
+
const request_key = allocCredentialsRequestEntry(
|
|
3015
|
+
fx,
|
|
3016
|
+
key,
|
|
3017
|
+
ok_tag,
|
|
3018
|
+
err_tag,
|
|
3019
|
+
is_delete,
|
|
3020
|
+
) orelse return;
|
|
3021
|
+
if (is_get) {
|
|
3022
|
+
fx.credentialsGet(.{
|
|
3023
|
+
.key = request_key,
|
|
3024
|
+
.credential_key = credential_key,
|
|
3025
|
+
.host_result = hostResultMsg,
|
|
3026
|
+
});
|
|
3027
|
+
} else {
|
|
3028
|
+
fx.credentialsDelete(.{
|
|
3029
|
+
.key = request_key,
|
|
3030
|
+
.credential_key = credential_key,
|
|
3031
|
+
.host_result = hostResultMsg,
|
|
3032
|
+
});
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
|
|
3036
|
+
/// Credential records can also arrive through public `Cmd.request`,
|
|
3037
|
+
/// so their inner fields are untrusted even though the outer command
|
|
3038
|
+
/// wire was emitted correctly. Decode them fallibly and route a
|
|
3039
|
+
/// normal rejection instead of turning authored bytes into a panic.
|
|
3040
|
+
fn takeCredentialBytes(bytes: []const u8, at: *usize) ?[]const u8 {
|
|
3041
|
+
if (at.* > bytes.len or bytes.len - at.* < 4) return null;
|
|
3042
|
+
const len: usize = std.mem.readInt(u32, bytes[at.*..][0..4], .little);
|
|
3043
|
+
at.* += 4;
|
|
3044
|
+
if (len > bytes.len - at.*) return null;
|
|
3045
|
+
const field = bytes[at.* .. at.* + len];
|
|
3046
|
+
at.* += len;
|
|
3047
|
+
return field;
|
|
3048
|
+
}
|
|
3049
|
+
|
|
3050
|
+
/// Streaming services are one admission, not a channel-open/request
|
|
3051
|
+
/// batch. A duplicate route key or an already-open channel rejects
|
|
3052
|
+
/// before either table changes; otherwise the ordinary channel and
|
|
3053
|
+
/// request issuers retain their journal/replay behavior. An engine-side
|
|
3054
|
+
/// channel refusal is followed by the service err arm's matching
|
|
3055
|
+
/// rejection without starting the service transport.
|
|
3056
|
+
fn issueServiceStreamRequest(
|
|
3057
|
+
fx: *Fx,
|
|
3058
|
+
name: []const u8,
|
|
3059
|
+
key: []const u8,
|
|
3060
|
+
ok_tag: u8,
|
|
3061
|
+
err_tag: u8,
|
|
3062
|
+
channel_value: f64,
|
|
3063
|
+
event_tag: u8,
|
|
3064
|
+
max_pending: u8,
|
|
3065
|
+
payload: []const u8,
|
|
3066
|
+
) void {
|
|
3067
|
+
if (key.len > 0 and (findRequest(key) != null or fileStreamOccupiesKey(key))) {
|
|
3068
|
+
stageRequestRejected(fx, err_tag);
|
|
3069
|
+
return;
|
|
3070
|
+
}
|
|
3071
|
+
// Fail the existing request-table bound before opening a channel;
|
|
3072
|
+
// an admission failure must not leave an orphaned stream.
|
|
3073
|
+
if (freeRequestIndex() == null) {
|
|
3074
|
+
@panic("ts core host: more than 16 host requests in flight - the request table mirrors the engine's max_effects slots");
|
|
3075
|
+
}
|
|
3076
|
+
|
|
3077
|
+
const channel_key = exactEngineKey(channel_value);
|
|
3078
|
+
if (channel_key) |value| {
|
|
3079
|
+
// `findChannel` covers every TS bridge occupancy, including a
|
|
3080
|
+
// rejected terminal waiting to drain. `channelHandle` also
|
|
3081
|
+
// sees an open embedder-owned channel and replay's parked
|
|
3082
|
+
// occupancy. Never let this service acquire either one.
|
|
3083
|
+
if (findChannel(value) != null or fx.channelHandle(value) != null) {
|
|
3084
|
+
fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = value, .kind = .rejected }));
|
|
3085
|
+
stageRequestRejected(fx, err_tag);
|
|
3086
|
+
return;
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
|
|
3090
|
+
// Validation/table refusals generated by the bridge are final for
|
|
3091
|
+
// this combined command, so no service request may follow them.
|
|
3092
|
+
if (!issueChannelOpen(fx, channel_value, event_tag, max_pending)) {
|
|
3093
|
+
stageRequestRejected(fx, err_tag);
|
|
3094
|
+
return;
|
|
3095
|
+
}
|
|
3096
|
+
issueRequest(fx, name, key, ok_tag, err_tag, true, payload);
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
fn stageRequestRejected(fx: *Fx, err_tag: u8) void {
|
|
3100
|
+
fx.stageLoopMsg(msgFromTagStaticBytes(err_tag, "rejected"));
|
|
3101
|
+
}
|
|
3102
|
+
|
|
2291
3103
|
fn findRequest(key: []const u8) ?usize {
|
|
2292
3104
|
for (&requests, 0..) |*entry, index| {
|
|
2293
3105
|
if (entry.used and std.mem.eql(u8, entry.wireKey(), key)) return index;
|
|
@@ -2296,7 +3108,24 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2296
3108
|
}
|
|
2297
3109
|
|
|
2298
3110
|
fn freeRequestIndex() ?usize {
|
|
2299
|
-
for (
|
|
3111
|
+
for (requests[0..runtime_effects.max_effects], 0..) |*entry, index| {
|
|
3112
|
+
if (!entry.used) return index;
|
|
3113
|
+
}
|
|
3114
|
+
return null;
|
|
3115
|
+
}
|
|
3116
|
+
|
|
3117
|
+
fn freeStoreRequestIndex() ?usize {
|
|
3118
|
+
const first = runtime_effects.max_effects;
|
|
3119
|
+
const end = first + runtime_effects.max_store_effects;
|
|
3120
|
+
for (requests[first..end], first..) |*entry, index| {
|
|
3121
|
+
if (!entry.used) return index;
|
|
3122
|
+
}
|
|
3123
|
+
return null;
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3126
|
+
fn freeCredentialsRequestIndex() ?usize {
|
|
3127
|
+
const first = runtime_effects.max_effects + runtime_effects.max_store_effects;
|
|
3128
|
+
for (requests[first..], first..) |*entry, index| {
|
|
2300
3129
|
if (!entry.used) return index;
|
|
2301
3130
|
}
|
|
2302
3131
|
return null;
|
|
@@ -2317,6 +3146,15 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2317
3146
|
}
|
|
2318
3147
|
const entry = &requests[index];
|
|
2319
3148
|
entry.used = false;
|
|
3149
|
+
if (entry.service_channel) |channel_key| pending_service_channel_close = channel_key;
|
|
3150
|
+
if (result.ok) {
|
|
3151
|
+
if (entry.service_operation) |operation| {
|
|
3152
|
+
const binding = service_results orelse
|
|
3153
|
+
@panic("ts core host: a typed service result arrived after its decoder was unbound");
|
|
3154
|
+
return binding.decode_fn(operation, entry.ok_tag, result.bytes);
|
|
3155
|
+
}
|
|
3156
|
+
}
|
|
3157
|
+
if (result.ok and entry.ok_void) return msgFromTagVoid(entry.ok_tag);
|
|
2320
3158
|
return msgFromTagBytes(if (result.ok) entry.ok_tag else entry.err_tag, result.bytes);
|
|
2321
3159
|
}
|
|
2322
3160
|
|
|
@@ -2358,11 +3196,35 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2358
3196
|
if (tags.dropped) return swallowedMsg(tags.err_tag);
|
|
2359
3197
|
if (result.outcome == .ok) {
|
|
2360
3198
|
if (result.op == .read) return msgFromTagBytes(tags.ok_tag, result.bytes);
|
|
3199
|
+
if (result.op == .stat) return msgFromTagFileStat(tags.ok_tag, result);
|
|
2361
3200
|
return msgFromTagVoid(tags.ok_tag);
|
|
2362
3201
|
}
|
|
2363
3202
|
return msgFromTagBytes(tags.err_tag, @tagName(result.outcome));
|
|
2364
3203
|
}
|
|
2365
3204
|
|
|
3205
|
+
fn msgFromTagFileStat(tag: u8, result: runtime_effects.EffectFileResult) Msg {
|
|
3206
|
+
inline for (msg_arms, 0..) |arm, index| {
|
|
3207
|
+
if (tag == index) {
|
|
3208
|
+
const info = @typeInfo(arm.type);
|
|
3209
|
+
if (comptime info == .@"struct" and info.@"struct".fields.len == 3) {
|
|
3210
|
+
var payload: arm.type = undefined;
|
|
3211
|
+
inline for (info.@"struct".fields) |field| {
|
|
3212
|
+
if (comptime std.mem.eql(u8, field.name, "exists") and field.type == bool) {
|
|
3213
|
+
@field(payload, field.name) = result.exists;
|
|
3214
|
+
} else if (comptime std.mem.eql(u8, field.name, "size") and (field.type == i64 or field.type == u64 or field.type == f64)) {
|
|
3215
|
+
@field(payload, field.name) = if (comptime field.type == f64) @floatFromInt(result.total) else @intCast(result.total);
|
|
3216
|
+
} else if (comptime std.mem.eql(u8, field.name, "mtimeMs") and (field.type == i64 or field.type == u64 or field.type == f64)) {
|
|
3217
|
+
@field(payload, field.name) = if (comptime field.type == f64) @floatFromInt(result.mtime_ms) else @intCast(result.mtime_ms);
|
|
3218
|
+
} else @panic("ts core host: stat_file ok arm has the wrong fields");
|
|
3219
|
+
}
|
|
3220
|
+
return @unionInit(Msg, arm.name, payload);
|
|
3221
|
+
}
|
|
3222
|
+
@panic("ts core host: stat_file ok arm must be { exists, size, mtimeMs }");
|
|
3223
|
+
}
|
|
3224
|
+
}
|
|
3225
|
+
@panic("ts core host: stat_file ok tag is outside Msg");
|
|
3226
|
+
}
|
|
3227
|
+
|
|
2366
3228
|
/// `ResponseMsgFn` for fetch: an `.ok` un-truncated response
|
|
2367
3229
|
/// routes the ok arm as `{ status, body }`; everything else —
|
|
2368
3230
|
/// truncation included, so a cut body never parses as whole —
|
|
@@ -2408,7 +3270,7 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2408
3270
|
return msgFromTagNumber(delays[index].tag, ms);
|
|
2409
3271
|
}
|
|
2410
3272
|
|
|
2411
|
-
//
|
|
3273
|
+
// ----------------------------------------------- subscriptions
|
|
2412
3274
|
|
|
2413
3275
|
/// Reconcile the declarative subscription set against the fixed
|
|
2414
3276
|
/// timer table — the same algorithm as the @native-sdk/core package's
|
|
@@ -2416,73 +3278,167 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
2416
3278
|
/// keys into the first free slot, re-arm on interval change,
|
|
2417
3279
|
/// re-route on tag change, cancel the missing. Slot order
|
|
2418
3280
|
/// everywhere, so record/replay walk identical tables.
|
|
2419
|
-
fn
|
|
3281
|
+
fn reconcileSubscriptions(fx: *Fx) void {
|
|
2420
3282
|
if (comptime !has_subscriptions) return;
|
|
2421
3283
|
const subs = core.subscriptions(model_root);
|
|
2422
|
-
var
|
|
3284
|
+
var seen_timers = [_]bool{false} ** timers.len;
|
|
3285
|
+
var seen_db = [_]bool{false} ** dbs.len;
|
|
3286
|
+
|
|
3287
|
+
// Free live DB slots whose wire keys disappeared before the pass
|
|
3288
|
+
// allocates replacements. Otherwise two independently valid sets
|
|
3289
|
+
// can overflow the shared family at their transient union (for
|
|
3290
|
+
// example, sixteen old keys replaced by one new key).
|
|
3291
|
+
const retained_db = retainedDbSubscriptions(subs);
|
|
3292
|
+
for (&dbs, 0..) |*entry, index| {
|
|
3293
|
+
if (entry.used and entry.live and !retained_db[index]) {
|
|
3294
|
+
fx.dbUnsubscribe(db_key_base + index);
|
|
3295
|
+
entry.used = false;
|
|
3296
|
+
}
|
|
3297
|
+
}
|
|
3298
|
+
|
|
2423
3299
|
var at: usize = 0;
|
|
2424
3300
|
while (at < subs.len) {
|
|
3301
|
+
const record_start = at;
|
|
2425
3302
|
const op = takeByte(subs, &at);
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
.
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
3303
|
+
switch (op) {
|
|
3304
|
+
// timer [op][key_len][key][every_ms f64 LE][msg_tag]
|
|
3305
|
+
0x01 => {
|
|
3306
|
+
const key = takeShortBytes(subs, &at);
|
|
3307
|
+
const every_bits = takeBytes(subs, &at, 8);
|
|
3308
|
+
const every_ms: f64 = @bitCast(std.mem.readInt(u64, every_bits[0..8], .little));
|
|
3309
|
+
const tag = takeByte(subs, &at);
|
|
3310
|
+
if (!(every_ms >= 1) or !(every_ms <= 31_536_000_000.0)) {
|
|
3311
|
+
@panic("ts core host: Sub.timer interval must be between 1ms and one year");
|
|
3312
|
+
}
|
|
3313
|
+
var slot: ?usize = null;
|
|
3314
|
+
for (&timers, 0..) |*entry, index| {
|
|
3315
|
+
if (entry.used and std.mem.eql(u8, entry.wireKey(), key)) slot = index;
|
|
3316
|
+
}
|
|
3317
|
+
if (slot) |index| {
|
|
3318
|
+
seen_timers[index] = true;
|
|
3319
|
+
const entry = &timers[index];
|
|
3320
|
+
if (entry.every_ms != every_ms) {
|
|
3321
|
+
entry.every_ms = every_ms;
|
|
3322
|
+
fx.startTimer(.{
|
|
3323
|
+
.key = timer_key_base + index,
|
|
3324
|
+
.interval_ms = intervalMs(every_ms),
|
|
3325
|
+
.mode = .repeating,
|
|
3326
|
+
.on_fire = timerFireMsg,
|
|
3327
|
+
});
|
|
3328
|
+
}
|
|
3329
|
+
entry.tag = tag;
|
|
3330
|
+
} else {
|
|
3331
|
+
const index = freeTimerIndex() orelse
|
|
3332
|
+
@panic("ts core host: more than 16 subscription timers - the timer table mirrors the engine's max_effect_timers");
|
|
3333
|
+
seen_timers[index] = true;
|
|
3334
|
+
const entry = &timers[index];
|
|
3335
|
+
entry.used = true;
|
|
3336
|
+
entry.key_len = key.len;
|
|
3337
|
+
@memcpy(entry.key[0..key.len], key);
|
|
3338
|
+
entry.every_ms = every_ms;
|
|
3339
|
+
entry.tag = tag;
|
|
3340
|
+
fx.startTimer(.{
|
|
3341
|
+
.key = timer_key_base + index,
|
|
3342
|
+
.interval_ms = intervalMs(every_ms),
|
|
3343
|
+
.mode = .repeating,
|
|
3344
|
+
.on_fire = timerFireMsg,
|
|
3345
|
+
});
|
|
3346
|
+
}
|
|
3347
|
+
},
|
|
3348
|
+
// live query [op][key][page][done][err][sql bytes]
|
|
3349
|
+
// [param count][tagged params]
|
|
3350
|
+
// [table count][short table names]
|
|
3351
|
+
0x02 => {
|
|
3352
|
+
const key = takeShortBytes(subs, &at);
|
|
3353
|
+
if (key.len == 0) @panic("ts core host: a live query requires a non-empty subscription key");
|
|
3354
|
+
const page_tag = takeByte(subs, &at);
|
|
3355
|
+
const done_tag = takeByte(subs, &at);
|
|
3356
|
+
const err_tag = takeByte(subs, &at);
|
|
3357
|
+
const sql = takeLongBytes(subs, &at);
|
|
3358
|
+
const param_count: usize = @intCast(takeU32(subs, &at));
|
|
3359
|
+
var params: [runtime_effects.max_effect_db_parameters]runtime_effects.EffectDbValue = undefined;
|
|
3360
|
+
if (param_count > params.len) @panic("ts core host: a live query carries too many SQL parameters");
|
|
3361
|
+
for (0..param_count) |index| params[index] = takeDbValue(subs, &at);
|
|
3362
|
+
const table_count: usize = @intCast(takeU32(subs, &at));
|
|
3363
|
+
var tables: [runtime_effects.max_effect_db_live_tables][]const u8 = undefined;
|
|
3364
|
+
if (table_count == 0 or table_count > tables.len) @panic("ts core host: a live query carries an invalid dependency table set");
|
|
3365
|
+
for (0..table_count) |index| tables[index] = takeShortBytes(subs, &at);
|
|
3366
|
+
|
|
3367
|
+
const signature = std.hash.Wyhash.hash(0, subs[record_start..at]);
|
|
3368
|
+
const index = findDb(key) orelse freeDbIndex() orelse
|
|
3369
|
+
@panic("ts core host: more relational commands and live queries are active than the database slot family can hold");
|
|
3370
|
+
const entry = &dbs[index];
|
|
3371
|
+
if (entry.used and !entry.live) @panic("ts core host: a live-query key collides with an in-flight database command");
|
|
3372
|
+
if (seen_db[index]) @panic("ts core host: duplicate live-query subscription key");
|
|
3373
|
+
seen_db[index] = true;
|
|
3374
|
+
if (!entry.used or entry.signature != signature) {
|
|
3375
|
+
if (entry.used) fx.dbUnsubscribe(db_key_base + index);
|
|
3376
|
+
entry.* = .{
|
|
3377
|
+
.used = true,
|
|
3378
|
+
.query = true,
|
|
3379
|
+
.live = true,
|
|
3380
|
+
.signature = signature,
|
|
3381
|
+
.key_len = key.len,
|
|
3382
|
+
.page_tag = page_tag,
|
|
3383
|
+
.done_tag = done_tag,
|
|
3384
|
+
.err_tag = err_tag,
|
|
3385
|
+
};
|
|
3386
|
+
@memcpy(entry.key[0..key.len], key);
|
|
3387
|
+
fx.dbSubscribe(.{
|
|
3388
|
+
.key = db_key_base + index,
|
|
3389
|
+
.sql = sql,
|
|
3390
|
+
.params = params[0..param_count],
|
|
3391
|
+
.tables = tables[0..table_count],
|
|
3392
|
+
.on_result = dbResultMsg,
|
|
3393
|
+
});
|
|
3394
|
+
}
|
|
3395
|
+
},
|
|
3396
|
+
else => @panic("ts core host: unknown subscription wire record - the core and this runtime disagree on cmd_format_version"),
|
|
2476
3397
|
}
|
|
2477
3398
|
}
|
|
2478
3399
|
for (&timers, 0..) |*entry, index| {
|
|
2479
|
-
if (entry.used and !
|
|
3400
|
+
if (entry.used and !seen_timers[index]) {
|
|
2480
3401
|
entry.used = false;
|
|
2481
3402
|
fx.cancelTimer(timer_key_base + index);
|
|
2482
3403
|
}
|
|
2483
3404
|
}
|
|
2484
3405
|
}
|
|
2485
3406
|
|
|
3407
|
+
/// Parse the inert subscription stream without mutating it and mark
|
|
3408
|
+
/// the currently-live DB entries whose public keys remain declared.
|
|
3409
|
+
/// Reconciliation uses this pre-pass to retire genuinely stale slots
|
|
3410
|
+
/// before it allocates any new ones.
|
|
3411
|
+
fn retainedDbSubscriptions(subs: []const u8) [runtime_effects.max_db_effects]bool {
|
|
3412
|
+
var retained = [_]bool{false} ** runtime_effects.max_db_effects;
|
|
3413
|
+
var at: usize = 0;
|
|
3414
|
+
while (at < subs.len) switch (takeByte(subs, &at)) {
|
|
3415
|
+
0x01 => {
|
|
3416
|
+
_ = takeShortBytes(subs, &at);
|
|
3417
|
+
_ = takeBytes(subs, &at, 8);
|
|
3418
|
+
_ = takeByte(subs, &at);
|
|
3419
|
+
},
|
|
3420
|
+
0x02 => {
|
|
3421
|
+
const key = takeShortBytes(subs, &at);
|
|
3422
|
+
if (key.len == 0) @panic("ts core host: a live query requires a non-empty subscription key");
|
|
3423
|
+
_ = takeByte(subs, &at);
|
|
3424
|
+
_ = takeByte(subs, &at);
|
|
3425
|
+
_ = takeByte(subs, &at);
|
|
3426
|
+
_ = takeLongBytes(subs, &at);
|
|
3427
|
+
const param_count: usize = @intCast(takeU32(subs, &at));
|
|
3428
|
+
if (param_count > runtime_effects.max_effect_db_parameters) @panic("ts core host: a live query carries too many SQL parameters");
|
|
3429
|
+
for (0..param_count) |_| _ = takeDbValue(subs, &at);
|
|
3430
|
+
const table_count: usize = @intCast(takeU32(subs, &at));
|
|
3431
|
+
if (table_count == 0 or table_count > runtime_effects.max_effect_db_live_tables) @panic("ts core host: a live query carries an invalid dependency table set");
|
|
3432
|
+
for (0..table_count) |_| _ = takeShortBytes(subs, &at);
|
|
3433
|
+
if (findDb(key)) |index| {
|
|
3434
|
+
if (dbs[index].live) retained[index] = true;
|
|
3435
|
+
}
|
|
3436
|
+
},
|
|
3437
|
+
else => @panic("ts core host: unknown subscription wire record - the core and this runtime disagree on cmd_format_version"),
|
|
3438
|
+
};
|
|
3439
|
+
return retained;
|
|
3440
|
+
}
|
|
3441
|
+
|
|
2486
3442
|
fn freeTimerIndex() ?usize {
|
|
2487
3443
|
for (&timers, 0..) |*entry, index| {
|
|
2488
3444
|
if (!entry.used) return index;
|
|
@@ -3135,6 +4091,16 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
3135
4091
|
|
|
3136
4092
|
// ------------------------------------------------- wire cursor
|
|
3137
4093
|
|
|
4094
|
+
fn readF64(bytes: []const u8, at: usize) f64 {
|
|
4095
|
+
if (at + 8 > bytes.len) @panic("ts core host: truncated f64 service metadata");
|
|
4096
|
+
return @bitCast(std.mem.readInt(u64, bytes[at..][0..8], .little));
|
|
4097
|
+
}
|
|
4098
|
+
|
|
4099
|
+
fn exactEngineKey(value: f64) ?u64 {
|
|
4100
|
+
if (!std.math.isFinite(value) or value < 1 or value >= 9007199254740992.0 or @floor(value) != value) return null;
|
|
4101
|
+
return @intFromFloat(value);
|
|
4102
|
+
}
|
|
4103
|
+
|
|
3138
4104
|
fn takeByte(bytes: []const u8, at: *usize) u8 {
|
|
3139
4105
|
if (at.* >= bytes.len) @panic("ts core host: truncated wire record");
|
|
3140
4106
|
const value = bytes[at.*];
|
|
@@ -3149,6 +4115,12 @@ pub fn TsCoreHost(comptime core: type) type {
|
|
|
3149
4115
|
return slice;
|
|
3150
4116
|
}
|
|
3151
4117
|
|
|
4118
|
+
/// A fixed-width little-endian integer.
|
|
4119
|
+
fn takeU32(bytes: []const u8, at: *usize) u32 {
|
|
4120
|
+
const raw = takeBytes(bytes, at, 4);
|
|
4121
|
+
return std.mem.readInt(u32, raw[0..4], .little);
|
|
4122
|
+
}
|
|
4123
|
+
|
|
3152
4124
|
/// A one-byte-length-prefixed field (names and keys).
|
|
3153
4125
|
fn takeShortBytes(bytes: []const u8, at: *usize) []const u8 {
|
|
3154
4126
|
const len: usize = takeByte(bytes, at);
|