@native-sdk/cli 0.8.3 → 0.9.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 (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -29,6 +29,7 @@ const mobileWidgetActions = conversions.mobileWidgetActions;
29
29
  const mobileWidgetActionKindFromInt = conversions.mobileWidgetActionKindFromInt;
30
30
  const native_sdk_app_create = c_api.native_sdk_app_create;
31
31
  const native_sdk_app_destroy = c_api.native_sdk_app_destroy;
32
+ const native_sdk_app_destroy_with_status = c_api.native_sdk_app_destroy_with_status;
32
33
  const native_sdk_app_start = c_api.native_sdk_app_start;
33
34
  const native_sdk_app_activate = c_api.native_sdk_app_activate;
34
35
  const native_sdk_app_deactivate = c_api.native_sdk_app_deactivate;
@@ -266,6 +267,17 @@ test "mobile C ABI destroy returns registered font bytes through the embedded de
266
267
  native_sdk_app_destroy(app);
267
268
  }
268
269
 
270
+ test "mobile C ABI destroy status reports whether callback context must survive" {
271
+ const normal = native_sdk_app_create() orelse return error.TestUnexpectedResult;
272
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_destroy_with_status(normal));
273
+
274
+ const preserved = native_sdk_app_create() orelse return error.TestUnexpectedResult;
275
+ mobileApp(preserved).?.null_platform.channel_wake_abandoned.store(true, .seq_cst);
276
+ // The host is deliberately process-lived after this call, exactly like a
277
+ // real detached callback's context. Do not touch or destroy it again.
278
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_destroy_with_status(preserved));
279
+ }
280
+
269
281
  test "mobile C ABI can load packaged asset source" {
270
282
  const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
271
283
  defer native_sdk_app_destroy(app);
@@ -1019,6 +1031,136 @@ const MobileCounterDef = struct {
1019
1031
 
1020
1032
  const MobileCounterHost = ui_host.UiAppHost(MobileCounterDef);
1021
1033
  const MobileCounterApi = c_api.MobileCApi(MobileCounterHost);
1034
+ const MobileStoreHost = ui_host.UiAppHostWithRecordStore(MobileCounterDef, true);
1035
+ const MobileStoreApi = c_api.MobileCApi(MobileStoreHost);
1036
+ const MobileRelationalHost = ui_host.UiAppHostWithStorage(MobileCounterDef, false, true, &.{});
1037
+ const MobileRelationalApi = c_api.MobileCApi(MobileRelationalHost);
1038
+ const MobileBothStorageHost = ui_host.UiAppHostWithStorage(MobileCounterDef, true, true, &.{});
1039
+ const MobileBothStorageApi = c_api.MobileCApi(MobileBothStorageHost);
1040
+ const MobileCredentialHost = ui_host.UiAppHostWithStorageAndCredentials(
1041
+ MobileCounterDef,
1042
+ false,
1043
+ false,
1044
+ &.{},
1045
+ true,
1046
+ true,
1047
+ "dev.native-sdk.mobile-credentials",
1048
+ );
1049
+ const MobileCredentialApi = c_api.MobileCApi(MobileCredentialHost);
1050
+
1051
+ const MobileCredentialRecorder = struct {
1052
+ secret: [64]u8 = undefined,
1053
+ secret_len: usize = 0,
1054
+ present: bool = false,
1055
+ };
1056
+
1057
+ fn mobileCredentialRecorder(context: ?*anyopaque) *MobileCredentialRecorder {
1058
+ return @ptrCast(@alignCast(context.?));
1059
+ }
1060
+
1061
+ fn mobileCredentialSet(
1062
+ context: ?*anyopaque,
1063
+ service: ?[*]const u8,
1064
+ service_len: usize,
1065
+ account: ?[*]const u8,
1066
+ account_len: usize,
1067
+ secret: ?[*]const u8,
1068
+ secret_len: usize,
1069
+ ) callconv(.c) c_int {
1070
+ _ = service;
1071
+ _ = service_len;
1072
+ _ = account;
1073
+ _ = account_len;
1074
+ const recorder = mobileCredentialRecorder(context);
1075
+ if (secret_len > recorder.secret.len) return -5;
1076
+ if (secret_len > 0) @memcpy(recorder.secret[0..secret_len], secret.?[0..secret_len]);
1077
+ recorder.secret_len = secret_len;
1078
+ recorder.present = true;
1079
+ return 1;
1080
+ }
1081
+
1082
+ fn mobileCredentialGet(
1083
+ context: ?*anyopaque,
1084
+ service: ?[*]const u8,
1085
+ service_len: usize,
1086
+ account: ?[*]const u8,
1087
+ account_len: usize,
1088
+ output: ?[*]u8,
1089
+ output_len: usize,
1090
+ ) callconv(.c) i64 {
1091
+ _ = service;
1092
+ _ = service_len;
1093
+ _ = account;
1094
+ _ = account_len;
1095
+ const recorder = mobileCredentialRecorder(context);
1096
+ if (!recorder.present) return -1;
1097
+ if (recorder.secret_len > output_len) return -4;
1098
+ if (recorder.secret_len > 0) @memcpy(output.?[0..recorder.secret_len], recorder.secret[0..recorder.secret_len]);
1099
+ return @intCast(recorder.secret_len);
1100
+ }
1101
+
1102
+ fn mobileCredentialDelete(
1103
+ context: ?*anyopaque,
1104
+ service: ?[*]const u8,
1105
+ service_len: usize,
1106
+ account: ?[*]const u8,
1107
+ account_len: usize,
1108
+ ) callconv(.c) c_int {
1109
+ _ = service;
1110
+ _ = service_len;
1111
+ _ = account;
1112
+ _ = account_len;
1113
+ const recorder = mobileCredentialRecorder(context);
1114
+ if (!recorder.present) return 0;
1115
+ recorder.present = false;
1116
+ recorder.secret_len = 0;
1117
+ return 1;
1118
+ }
1119
+
1120
+ test "mobile credential service uses app identity and preserves misses" {
1121
+ const app = MobileCredentialApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1122
+ defer MobileCredentialApi.native_sdk_app_destroy(app);
1123
+ const self: *MobileCredentialHost = @ptrCast(@alignCast(app));
1124
+
1125
+ try std.testing.expect(self.embedded.runtime.options.credentials_enabled);
1126
+ try std.testing.expectEqualStrings(
1127
+ "dev.native-sdk.mobile-credentials",
1128
+ self.embedded.runtime.options.platform.app_info.bundle_id,
1129
+ );
1130
+ try std.testing.expect(self.embedded.runtime.options.platform.services.set_credential_fn == null);
1131
+
1132
+ var recorder = MobileCredentialRecorder{};
1133
+ const service: types.MobileCredentialService = .{
1134
+ .set = mobileCredentialSet,
1135
+ .get = mobileCredentialGet,
1136
+ .delete = mobileCredentialDelete,
1137
+ };
1138
+ try std.testing.expectEqual(
1139
+ @as(c_int, 1),
1140
+ MobileCredentialApi.native_sdk_app_set_credential_service(app, &service, &recorder),
1141
+ );
1142
+
1143
+ const services = self.embedded.runtime.options.platform.services;
1144
+ try services.setCredential(.{
1145
+ .service = "dev.native-sdk.mobile-credentials",
1146
+ .account = "token",
1147
+ .secret = "secret-bytes",
1148
+ });
1149
+ var output: [64]u8 = undefined;
1150
+ const secret = try services.getCredential(.{
1151
+ .service = "dev.native-sdk.mobile-credentials",
1152
+ .account = "token",
1153
+ }, &output);
1154
+ try std.testing.expectEqualStrings("secret-bytes", secret);
1155
+ try services.deleteCredential(.{
1156
+ .service = "dev.native-sdk.mobile-credentials",
1157
+ .account = "token",
1158
+ });
1159
+ try std.testing.expectError(error.CredentialNotFound, services.getCredential(.{
1160
+ .service = "dev.native-sdk.mobile-credentials",
1161
+ .account = "token",
1162
+ }, &output));
1163
+ }
1022
1164
 
1023
1165
  fn expectNoUiHostError(app: ?*anyopaque) !void {
1024
1166
  try std.testing.expectEqualStrings("", std.mem.span(MobileCounterApi.native_sdk_app_last_error_name(app)));
@@ -1052,6 +1194,77 @@ fn tapMobileWidget(app: ?*anyopaque, node: MobileWidgetSemantics) !void {
1052
1194
  try expectNoUiHostError(app);
1053
1195
  }
1054
1196
 
1197
+ test "mobile store capability requires and binds the OS app-data root before start" {
1198
+ const app = MobileStoreApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1199
+ defer MobileStoreApi.native_sdk_app_destroy(app);
1200
+ const self: *MobileStoreHost = @ptrCast(@alignCast(app));
1201
+
1202
+ MobileStoreApi.native_sdk_app_start(app);
1203
+ try std.testing.expectEqualStrings("StoreDataDirUnavailable", std.mem.span(MobileStoreApi.native_sdk_app_last_error_name(app)));
1204
+
1205
+ var tmp = std.testing.tmpDir(.{});
1206
+ defer tmp.cleanup();
1207
+ var path_buffer: [256]u8 = undefined;
1208
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/mobile-store", .{tmp.sub_path[0..]});
1209
+ try std.Io.Dir.cwd().createDirPath(std.testing.io, path);
1210
+ try std.testing.expectEqual(@as(c_int, 1), MobileStoreApi.native_sdk_app_set_data_root(app, path.ptr, path.len));
1211
+ try std.testing.expect(self.record_store_open);
1212
+ try std.testing.expect(self.embedded.runtime.options.record_store != null);
1213
+
1214
+ MobileStoreApi.native_sdk_app_start(app);
1215
+ try std.testing.expectEqualStrings("", std.mem.span(MobileStoreApi.native_sdk_app_last_error_name(app)));
1216
+ var surface_token: u8 = 0;
1217
+ MobileStoreApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1218
+ MobileStoreApi.native_sdk_app_frame(app);
1219
+ try self.ui.dispatch(&self.embedded.runtime, 1, .increment);
1220
+ try std.testing.expect(self.ui.effects.record_store_binding != null);
1221
+ MobileStoreApi.native_sdk_app_stop(app);
1222
+ }
1223
+
1224
+ test "mobile relational capability opens app.db from the OS app-data root" {
1225
+ const app = MobileRelationalApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1226
+ defer MobileRelationalApi.native_sdk_app_destroy(app);
1227
+ const self: *MobileRelationalHost = @ptrCast(@alignCast(app));
1228
+
1229
+ MobileRelationalApi.native_sdk_app_start(app);
1230
+ try std.testing.expectEqualStrings("SqliteDataDirUnavailable", std.mem.span(MobileRelationalApi.native_sdk_app_last_error_name(app)));
1231
+
1232
+ var tmp = std.testing.tmpDir(.{});
1233
+ defer tmp.cleanup();
1234
+ var path_buffer: [256]u8 = undefined;
1235
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/mobile-relational", .{tmp.sub_path[0..]});
1236
+ try std.Io.Dir.cwd().createDirPath(std.testing.io, path);
1237
+ try std.testing.expectEqual(@as(c_int, 1), MobileRelationalApi.native_sdk_app_set_data_root(app, path.ptr, path.len));
1238
+ try std.testing.expect(self.relational_store_open);
1239
+ try std.testing.expect(self.embedded.runtime.options.relational_store != null);
1240
+
1241
+ MobileRelationalApi.native_sdk_app_start(app);
1242
+ try std.testing.expectEqualStrings("", std.mem.span(MobileRelationalApi.native_sdk_app_last_error_name(app)));
1243
+ var surface_token: u8 = 0;
1244
+ MobileRelationalApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1245
+ MobileRelationalApi.native_sdk_app_frame(app);
1246
+ try self.ui.dispatch(&self.embedded.runtime, 1, .increment);
1247
+ try std.testing.expect(self.ui.effects.relational_store_binding != null);
1248
+ MobileRelationalApi.native_sdk_app_stop(app);
1249
+ }
1250
+
1251
+ test "mobile host binds record and relational databases together" {
1252
+ const app = MobileBothStorageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1253
+ defer MobileBothStorageApi.native_sdk_app_destroy(app);
1254
+ const self: *MobileBothStorageHost = @ptrCast(@alignCast(app));
1255
+
1256
+ var tmp = std.testing.tmpDir(.{});
1257
+ defer tmp.cleanup();
1258
+ var path_buffer: [256]u8 = undefined;
1259
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/mobile-both-storage", .{tmp.sub_path[0..]});
1260
+ try std.Io.Dir.cwd().createDirPath(std.testing.io, path);
1261
+ try std.testing.expectEqual(@as(c_int, 1), MobileBothStorageApi.native_sdk_app_set_data_root(app, path.ptr, path.len));
1262
+ try std.testing.expect(self.record_store_open);
1263
+ try std.testing.expect(self.relational_store_open);
1264
+ try std.testing.expect(self.embedded.runtime.options.record_store != null);
1265
+ try std.testing.expect(self.embedded.runtime.options.relational_store != null);
1266
+ }
1267
+
1055
1268
  test "mobile C ABI drives a user UiApp canvas scene end to end" {
1056
1269
  const app = MobileCounterApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1057
1270
  defer MobileCounterApi.native_sdk_app_destroy(app);
@@ -267,6 +267,57 @@ pub const MobileAudioService = extern struct {
267
267
  }
268
268
  };
269
269
 
270
+ // ----------------------------------------------------------- credentials
271
+ //
272
+ // The secure credential service over the embed C ABI. Mobile toolkit hosts
273
+ // register an OS-backed implementation before app start: Keychain Services
274
+ // on iOS and an AndroidKeyStore-wrapped private preferences entry on Android.
275
+ // The callback result codes keep the closed core-effect outcomes intact:
276
+ // locked and denied remain distinguishable from ordinary I/O failure, and a
277
+ // missing get/delete remains a normal `miss` rather than an error.
278
+
279
+ pub const MobileCredentialSetFn = *const fn (
280
+ context: ?*anyopaque,
281
+ service: ?[*]const u8,
282
+ service_len: usize,
283
+ account: ?[*]const u8,
284
+ account_len: usize,
285
+ secret: ?[*]const u8,
286
+ secret_len: usize,
287
+ ) callconv(.c) c_int;
288
+
289
+ pub const MobileCredentialGetFn = *const fn (
290
+ context: ?*anyopaque,
291
+ service: ?[*]const u8,
292
+ service_len: usize,
293
+ account: ?[*]const u8,
294
+ account_len: usize,
295
+ output: ?[*]u8,
296
+ output_len: usize,
297
+ ) callconv(.c) i64;
298
+
299
+ pub const MobileCredentialDeleteFn = *const fn (
300
+ context: ?*anyopaque,
301
+ service: ?[*]const u8,
302
+ service_len: usize,
303
+ account: ?[*]const u8,
304
+ account_len: usize,
305
+ ) callconv(.c) c_int;
306
+
307
+ pub const MobileCredentialService = extern struct {
308
+ set: ?MobileCredentialSetFn = null,
309
+ get: ?MobileCredentialGetFn = null,
310
+ delete: ?MobileCredentialDeleteFn = null,
311
+
312
+ pub fn complete(self: MobileCredentialService) bool {
313
+ return self.set != null and self.get != null and self.delete != null;
314
+ }
315
+
316
+ pub fn empty(self: MobileCredentialService) bool {
317
+ return self.set == null and self.get == null and self.delete == null;
318
+ }
319
+ };
320
+
270
321
  // ------------------------------------------------------------ image decode
271
322
  //
272
323
  // The platform image-decode service over the embed C ABI. A mobile shim
@@ -28,6 +28,7 @@ const app_manifest = @import("app_manifest");
28
28
  const canvas = @import("canvas");
29
29
  const runtime = @import("../runtime/root.zig");
30
30
  const platform = @import("../platform/root.zig");
31
+ const security = @import("../security/root.zig");
31
32
  const types = @import("types.zig");
32
33
  const host = @import("host.zig");
33
34
  const conversions = @import("conversions.zig");
@@ -57,7 +58,49 @@ pub const mobile_shell_windows = [_]app_manifest.ShellWindow{.{
57
58
  pub const mobile_shell_scene: app_manifest.ShellConfig = .{ .windows = &mobile_shell_windows };
58
59
 
59
60
  pub fn UiAppHost(comptime AppDef: type) type {
61
+ return UiAppHostWithStorage(AppDef, false, false, &.{});
62
+ }
63
+
64
+ /// Capability-specialized mobile host. The boolean is comptime so a mobile
65
+ /// artifact without `store` never analyzes SQLite open/deinit and carries no
66
+ /// database symbols; `build/app.zig` supplies the manifest-inferred value.
67
+ pub fn UiAppHostWithRecordStore(comptime AppDef: type, comptime record_store_enabled: bool) type {
68
+ return UiAppHostWithStorage(AppDef, record_store_enabled, false, &.{});
69
+ }
70
+
71
+ /// Capability-specialized mobile host for the two SQLite-backed storage
72
+ /// tiers. Both databases use the OS-owned data root but remain separate files.
73
+ pub fn UiAppHostWithStorage(
74
+ comptime AppDef: type,
75
+ comptime record_store_enabled: bool,
76
+ comptime relational_store_enabled: bool,
77
+ comptime relational_migrations: []const runtime.relational_store.Migration,
78
+ ) type {
79
+ return UiAppHostWithStorageAndCredentials(
80
+ AppDef,
81
+ record_store_enabled,
82
+ relational_store_enabled,
83
+ relational_migrations,
84
+ false,
85
+ false,
86
+ "dev.native_sdk.app",
87
+ );
88
+ }
89
+
90
+ /// Capability-specialized mobile host including the OS credential-store
91
+ /// gate and the app identity used as its service namespace.
92
+ pub fn UiAppHostWithStorageAndCredentials(
93
+ comptime AppDef: type,
94
+ comptime record_store_enabled: bool,
95
+ comptime relational_store_enabled: bool,
96
+ comptime relational_migrations: []const runtime.relational_store.Migration,
97
+ comptime credentials_enabled: bool,
98
+ comptime credentials_permitted: bool,
99
+ comptime credentials_service: []const u8,
100
+ ) type {
60
101
  const features: runtime.UiAppFeatures = if (@hasDecl(AppDef, "features")) AppDef.features else .{};
102
+ const RecordStoreType = if (record_store_enabled) runtime.RecordStore else void;
103
+ const RelationalStoreType = if (relational_store_enabled) runtime.RelationalStore else void;
61
104
  return struct {
62
105
  const Self = @This();
63
106
 
@@ -69,6 +112,10 @@ pub fn UiAppHost(comptime AppDef: type) type {
69
112
  /// the host wraps it so ABI-facing counters observe every event.
70
113
  inner_app: runtime.App,
71
114
  embedded: EmbeddedApp,
115
+ record_store: RecordStoreType = undefined,
116
+ record_store_open: bool = false,
117
+ relational_store: RelationalStoreType = undefined,
118
+ relational_store_open: bool = false,
72
119
  started: bool = false,
73
120
  frame_index: u64 = 0,
74
121
  last_error: ?anyerror = null,
@@ -83,6 +130,7 @@ pub fn UiAppHost(comptime AppDef: type) type {
83
130
  automation_io: ?*std.Io.Threaded = null,
84
131
  text_measure: host.MobileTextMeasure = .{},
85
132
  audio: host.MobileAudio = .{},
133
+ credentials: host.MobileCredentials = .{},
86
134
  // Image decode stays declined until the shim registers a real
87
135
  // codec (`native_sdk_app_set_image_service`): the null platform's
88
136
  // strict test decoder is opt-in (`image_decode`, default off), so
@@ -113,7 +161,10 @@ pub fn UiAppHost(comptime AppDef: type) type {
113
161
  const options = AppDef.mobileOptions();
114
162
  if (!std.mem.eql(u8, options.canvas_label, mobile_gpu_surface_label)) return error.InvalidViewOptions;
115
163
  if (!sceneHasMobileSurface(options.scene)) return error.ViewNotFound;
116
- self.null_platform = platform.NullPlatform.init(.{});
164
+ self.null_platform = platform.NullPlatform.initWithOptions(.{}, .system, .{
165
+ .app_name = options.name,
166
+ .bundle_id = credentials_service,
167
+ });
117
168
  self.null_platform.gpu_surfaces = true;
118
169
  // Audio is declined until the shim registers a real service
119
170
  // (`native_sdk_app_set_audio_service`): without one,
@@ -123,11 +174,15 @@ pub fn UiAppHost(comptime AppDef: type) type {
123
174
  // snapshotted below.
124
175
  self.null_platform.audio_playback = false;
125
176
  self.null_platform.audio_streaming = false;
177
+ self.null_platform.microphone_capture = false;
178
+ self.null_platform.system_audio_capture = false;
126
179
  // The null packet presenter records only counts; disabling it
127
180
  // routes presentation through the CPU pixel path so frames
128
181
  // produce real pixels (the buffer M2's surface blit consumes).
129
182
  self.null_platform.gpu_surface_packets = false;
130
183
  self.started = false;
184
+ self.record_store_open = false;
185
+ self.relational_store_open = false;
131
186
  self.frame_index = 0;
132
187
  self.last_error = null;
133
188
  self.command_count = 0;
@@ -141,6 +196,7 @@ pub fn UiAppHost(comptime AppDef: type) type {
141
196
  self.automation_io = null;
142
197
  self.text_measure = .{};
143
198
  self.audio = .{};
199
+ self.credentials = .{};
144
200
  self.image = .{};
145
201
  self.form_factor = .unknown;
146
202
  self.chrome_tabs_projected = false;
@@ -160,6 +216,15 @@ pub fn UiAppHost(comptime AppDef: type) type {
160
216
  .event_fn = hostEvent,
161
217
  .stop_fn = hostStop,
162
218
  }, self.null_platform.platform());
219
+ // The NullPlatform map is for native test only. Installed mobile
220
+ // apps start with no backing until the UIKit/Android shim
221
+ // registers its OS credential service.
222
+ try host.setCredentialService(self, .{}, null);
223
+ self.embedded.runtime.options.credentials_enabled = credentials_enabled;
224
+ self.embedded.runtime.options.security.permissions = if (credentials_permitted)
225
+ &.{security.permission_credentials}
226
+ else
227
+ &.{};
163
228
  // The damage seam: capture pixel presents (chained through
164
229
  // the null platform's recording present, so nonblank
165
230
  // sampling keeps working), drop the packet presenters no
@@ -170,7 +235,11 @@ pub fn UiAppHost(comptime AppDef: type) type {
170
235
  return self;
171
236
  }
172
237
 
173
- pub fn destroy(self: *Self) void {
238
+ /// Returns true when teardown deliberately preserves the host because
239
+ /// an abandoned platform callback may still enter it. Mobile shims
240
+ /// use the companion C ABI result to preserve their callback context
241
+ /// under the same condition.
242
+ pub fn destroy(self: *Self) bool {
174
243
  host.disableAutomation(self);
175
244
  self.render_memo.deinit();
176
245
  self.ui.deinit();
@@ -178,9 +247,15 @@ pub fn UiAppHost(comptime AppDef: type) type {
178
247
  // its heap-owned registrations (registered canvas font
179
248
  // bytes) before the host storage goes.
180
249
  self.embedded.deinit();
250
+ if (comptime record_store_enabled) {
251
+ if (self.record_store_open) self.record_store.deinit();
252
+ }
253
+ if (comptime relational_store_enabled) {
254
+ if (self.relational_store_open) self.relational_store.deinit();
255
+ }
181
256
  // The embedded null platform lives inside `self`, so freeing
182
257
  // `self` IS this path's platform destruction — and an
183
- // abandoned channel wake call may still enter that platform
258
+ // abandoned platform call may still enter that platform
184
259
  // at any later time (see
185
260
  // `PlatformServices.note_channel_wake_abandoned_fn`; the
186
261
  // null platform's own enqueue-only wake never triggers it,
@@ -188,17 +263,58 @@ pub fn UiAppHost(comptime AppDef: type) type {
188
263
  // like every first-party destroy path: skip the free and
189
264
  // leak the host storage, process-lived, with one loud line.
190
265
  if (self.null_platform.channel_wake_abandoned.load(.seq_cst)) {
191
- std.debug.print("mobile ui host teardown: an abandoned channel wake call may still enter the embedded platform; skipping the host free and leaking it, process-lived, so the stale call stays safe\n", .{});
192
- return;
266
+ std.debug.print("mobile ui host teardown: an abandoned platform call may still enter the embedded platform; skipping the host free and leaking it, process-lived, so the stale call stays safe\n", .{});
267
+ return true;
193
268
  }
194
269
  std.heap.page_allocator.destroy(self);
270
+ return false;
195
271
  }
196
272
 
197
273
  pub fn start(self: *Self) anyerror!void {
274
+ if (comptime record_store_enabled) {
275
+ if (!self.record_store_open) return error.StoreDataDirUnavailable;
276
+ }
277
+ if (comptime relational_store_enabled) {
278
+ if (!self.relational_store_open) return error.SqliteDataDirUnavailable;
279
+ }
198
280
  self.started = true;
199
281
  try self.embedded.start();
200
282
  }
201
283
 
284
+ /// Install the OS-owned app-data directory before start. iOS passes
285
+ /// Library/Application Support and Android passes files/, exactly the
286
+ /// `.data` directories resolved by `app_dirs` on those platforms.
287
+ pub fn setDataRoot(self: *Self, data_root: []const u8) !void {
288
+ if (comptime !record_store_enabled and !relational_store_enabled) return;
289
+ if (self.started) return error.AppAlreadyStarted;
290
+ if (data_root.len == 0 or data_root.len > max_mobile_asset_root_bytes) return error.InvalidStoreDataDir;
291
+ if (comptime record_store_enabled) {
292
+ if (self.record_store_open) {
293
+ self.record_store.deinit();
294
+ self.record_store_open = false;
295
+ self.embedded.runtime.options.record_store = null;
296
+ }
297
+ self.record_store = try runtime.RecordStore.open(std.heap.page_allocator, data_root);
298
+ self.record_store_open = true;
299
+ self.embedded.runtime.options.record_store = self.record_store.binding();
300
+ }
301
+ if (comptime relational_store_enabled) {
302
+ if (self.relational_store_open) {
303
+ self.relational_store.deinit();
304
+ self.relational_store_open = false;
305
+ self.embedded.runtime.options.relational_store = null;
306
+ }
307
+ const opened = try runtime.RelationalStore.openMigrated(std.heap.page_allocator, data_root, relational_migrations);
308
+ self.relational_store = switch (opened.outcome) {
309
+ .ok => opened.database.?,
310
+ .migrate_failed => return error.SqliteMigrationFailed,
311
+ .version_unknown => return error.SqliteVersionUnknown,
312
+ };
313
+ self.relational_store_open = true;
314
+ self.embedded.runtime.options.relational_store = self.relational_store.binding();
315
+ }
316
+ }
317
+
202
318
  /// Host-pumped frame step: the shim's display-link (or test) tick.
203
319
  /// Synthesizes the `gpu_surface_frame` event a platform loop would
204
320
  /// deliver for the mobile surface — first tick installs the widget