fastdb4ts 0.0.2 → 0.2.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.
package/dist/column.d.ts CHANGED
@@ -18,6 +18,7 @@ export declare class StridedColumn {
18
18
  private normalizeIndex;
19
19
  private getArrayConstructor;
20
20
  private getDataView;
21
+ private writeDirect;
21
22
  }
22
23
  type TypedArray = Uint8Array | Uint16Array | Uint32Array | Int32Array | Float32Array | Float64Array;
23
24
  export {};
package/dist/column.js CHANGED
@@ -63,8 +63,15 @@ export class StridedColumn {
63
63
  if (values.length !== this.length) {
64
64
  throw new FastdbRuntimeError(`Column fill length mismatch: expected ${this.length}, got ${values.length}.`);
65
65
  }
66
+ if (this.kind === this.module.ftU8n || this.kind === this.module.ftU16n) {
67
+ for (let i = 0; i < this.length; i += 1) {
68
+ this.set(i, values[i] ?? 0);
69
+ }
70
+ return;
71
+ }
72
+ const view = this.getDataView();
66
73
  for (let i = 0; i < this.length; i += 1) {
67
- this.set(i, values[i] ?? 0);
74
+ this.writeDirect(view, this.basePtr + i * this.stride, values[i] ?? 0);
68
75
  }
69
76
  }
70
77
  toArray() {
@@ -119,4 +126,31 @@ export class StridedColumn {
119
126
  // any previously captured ArrayBuffer reference becomes detached.
120
127
  return new DataView(this.module.HEAPU8.buffer);
121
128
  }
129
+ writeDirect(view, offset, value) {
130
+ if (this.kind === this.module.ftU8) {
131
+ view.setUint8(offset, Math.trunc(Number(value)));
132
+ return;
133
+ }
134
+ if (this.kind === this.module.ftU16) {
135
+ view.setUint16(offset, Math.trunc(Number(value)), true);
136
+ return;
137
+ }
138
+ if (this.kind === this.module.ftU32) {
139
+ view.setUint32(offset, Math.trunc(Number(value)), true);
140
+ return;
141
+ }
142
+ if (this.kind === this.module.ftI32) {
143
+ view.setInt32(offset, Math.trunc(Number(value)), true);
144
+ return;
145
+ }
146
+ if (this.kind === this.module.ftF32) {
147
+ view.setFloat32(offset, Number(value), true);
148
+ return;
149
+ }
150
+ if (this.kind === this.module.ftF64) {
151
+ view.setFloat64(offset, Number(value), true);
152
+ return;
153
+ }
154
+ throw new FastdbRuntimeError(`Field index ${this.fieldIndex} is not a directly writable numeric column.`);
155
+ }
122
156
  }
@@ -0,0 +1,20 @@
1
+ import { type FastdbModule, type WxDatabaseHandle } from './wasm-loader.js';
2
+ export declare class FastdbOwnedBytes {
3
+ private readonly module;
4
+ private readonly size;
5
+ private ptr;
6
+ constructor(module: FastdbModule, ptr: number, size: number);
7
+ get byteLength(): number;
8
+ get dataPtr(): number;
9
+ get view(): Uint8Array;
10
+ release(): void;
11
+ takeForDatabase(): {
12
+ module: FastdbModule;
13
+ ptr: number;
14
+ byteLength: number;
15
+ };
16
+ private requireOwned;
17
+ }
18
+ export type FastdbDatabaseBytes = Uint8Array | ArrayBuffer | FastdbOwnedBytes;
19
+ export declare function allocateFastdbOwnedBytes(module: FastdbModule, byteLength: number): FastdbOwnedBytes;
20
+ export declare function loadDatabaseFromBytes(module: FastdbModule, bytes: FastdbDatabaseBytes): WxDatabaseHandle;
@@ -0,0 +1,101 @@
1
+ import { FastdbRuntimeError } from './errors.js';
2
+ export class FastdbOwnedBytes {
3
+ module;
4
+ size;
5
+ ptr;
6
+ constructor(module, ptr, size) {
7
+ this.module = module;
8
+ this.size = size;
9
+ if (!Number.isSafeInteger(ptr) || ptr <= 0) {
10
+ throw new FastdbRuntimeError('fastdb owned buffer pointer must be a positive WASM heap address.');
11
+ }
12
+ if (!Number.isSafeInteger(size) || size <= 0) {
13
+ throw new FastdbRuntimeError('fastdb owned buffer byte length must be a positive integer.');
14
+ }
15
+ this.ptr = ptr;
16
+ }
17
+ get byteLength() {
18
+ return this.size;
19
+ }
20
+ get dataPtr() {
21
+ return this.requireOwned();
22
+ }
23
+ get view() {
24
+ const ptr = this.requireOwned();
25
+ return this.module.HEAPU8.subarray(ptr, ptr + this.size);
26
+ }
27
+ release() {
28
+ if (this.ptr === null) {
29
+ return;
30
+ }
31
+ const ptr = this.ptr;
32
+ this.ptr = null;
33
+ this.module._free(ptr);
34
+ }
35
+ takeForDatabase() {
36
+ const ptr = this.requireOwned();
37
+ this.ptr = null;
38
+ return {
39
+ module: this.module,
40
+ ptr,
41
+ byteLength: this.size,
42
+ };
43
+ }
44
+ requireOwned() {
45
+ if (this.ptr === null) {
46
+ throw new FastdbRuntimeError('fastdb owned buffer has already been transferred or released.');
47
+ }
48
+ return this.ptr;
49
+ }
50
+ }
51
+ export function allocateFastdbOwnedBytes(module, byteLength) {
52
+ if (!Number.isSafeInteger(byteLength) || byteLength <= 0) {
53
+ throw new FastdbRuntimeError('fastdb owned buffer byte length must be a positive integer.');
54
+ }
55
+ const ptr = module._malloc(byteLength);
56
+ if (ptr === 0) {
57
+ throw new FastdbRuntimeError(`Failed to allocate ${byteLength} bytes in the fastdb WASM heap.`);
58
+ }
59
+ return new FastdbOwnedBytes(module, ptr, byteLength);
60
+ }
61
+ export function loadDatabaseFromBytes(module, bytes) {
62
+ if (bytes instanceof FastdbOwnedBytes) {
63
+ const owned = bytes.takeForDatabase();
64
+ if (owned.module !== module) {
65
+ owned.module._free(owned.ptr);
66
+ throw new FastdbRuntimeError('fastdb owned buffer was allocated by a different WASM module.');
67
+ }
68
+ return loadDatabaseFromOwnedHeap(module, owned.ptr, owned.byteLength);
69
+ }
70
+ if (bytes instanceof ArrayBuffer) {
71
+ return loadDatabaseFromBytes(module, new Uint8Array(bytes));
72
+ }
73
+ if (bytes.byteLength === 0) {
74
+ throw new FastdbRuntimeError('Cannot load an empty fastdb buffer.');
75
+ }
76
+ const owned = allocateFastdbOwnedBytes(module, bytes.byteLength);
77
+ try {
78
+ owned.view.set(bytes);
79
+ const payload = owned.takeForDatabase();
80
+ return loadDatabaseFromOwnedHeap(module, payload.ptr, payload.byteLength);
81
+ }
82
+ finally {
83
+ owned.release();
84
+ }
85
+ }
86
+ function loadDatabaseFromOwnedHeap(module, ptr, byteLength) {
87
+ let ownedByDatabase = false;
88
+ try {
89
+ const db = module.WxDatabase.loadFromOwnedHeap(ptr, byteLength);
90
+ if (!db) {
91
+ throw new FastdbRuntimeError('Failed to load fastdb buffer.');
92
+ }
93
+ ownedByDatabase = true;
94
+ return db;
95
+ }
96
+ finally {
97
+ if (!ownedByDatabase) {
98
+ module._free(ptr);
99
+ }
100
+ }
101
+ }
package/dist/feature.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type ClassSchema } from './schema.js';
2
- import type { WxDatabaseBuildHandle, WxDatabaseHandle, WxFeatureHandle } from './wasm-loader.js';
2
+ import { type WxDatabaseBuildHandle, type WxDatabaseHandle, type WxFeatureHandle } from './wasm-loader.js';
3
3
  declare const PROXY_SYMBOL: unique symbol;
4
4
  export type FeatureDatabaseHandle = WxDatabaseHandle | WxDatabaseBuildHandle;
5
5
  export interface FeatureClass<T extends Feature = Feature> {
package/dist/feature.js CHANGED
@@ -1,6 +1,8 @@
1
+ // Standalone feature metadata; not fastdb.payload.v1 authority.
1
2
  import { FastdbRuntimeError, FastdbSchemaError, FastdbUsageError, } from './errors.js';
2
- import { getClassSchema, getFieldDefinition, } from './schema.js';
3
- import { isNumericField, isRefField, } from './types.js';
3
+ import { getClassSchema, getFieldDefinition, resolveListItem, } from './schema.js';
4
+ import { coerceBoolScalar, isListField, isNumericField, isRefField, } from './types.js';
5
+ import { getInitializedFastdbModule, } from './wasm-loader.js';
4
6
  const PROXY_SYMBOL = Symbol('fastdb4ts.proxy');
5
7
  export class Feature {
6
8
  _origin = null;
@@ -111,16 +113,56 @@ function readMappedField(feature, def) {
111
113
  case 'f64':
112
114
  return origin.getFieldAsFloat(def.index);
113
115
  case 'str':
114
- case 'wstr':
115
116
  return origin.getFieldAsString(def.index);
117
+ case 'wstr':
118
+ return origin.getFieldAsWString(def.index);
116
119
  case 'bytes':
117
- return origin.geometryView();
120
+ return copyChunkToBytes(origin.geometryView());
121
+ case 'list':
122
+ return readMappedListField(origin, def);
118
123
  case 'ref':
119
124
  return feature._cache?.[def.name] ?? null;
120
125
  default:
121
126
  throw new FastdbSchemaError(`Unsupported field kind: ${entry.kind}`);
122
127
  }
123
128
  }
129
+ function copyChunkToBytes(chunk) {
130
+ const module = getInitializedFastdbModule();
131
+ return module.HEAPU8.slice(chunk.data, chunk.data + chunk.size);
132
+ }
133
+ function readMappedListField(origin, def) {
134
+ if (!isListField(def.entry)) {
135
+ throw new FastdbSchemaError(`Field "${def.name}" is not a list field.`);
136
+ }
137
+ const item = resolveListItem(def.entry);
138
+ if (!isFieldType(item) || !item.arrayCtor) {
139
+ throw new FastdbSchemaError(`Mapped list field "${def.name}" can only materialize numeric scalar lists through the columnar runtime.`);
140
+ }
141
+ const bytes = copyChunkToBytes(origin.getFieldAsListView(def.index));
142
+ if (bytes.length === 0) {
143
+ return [];
144
+ }
145
+ const bytesPerElement = item.arrayCtor.BYTES_PER_ELEMENT;
146
+ if (bytes.length % bytesPerElement !== 0) {
147
+ throw new FastdbRuntimeError(`Mapped list field "${def.name}" has ${bytes.length} byte(s), not a multiple of ${bytesPerElement}.`);
148
+ }
149
+ const source = typedArraySource(bytes);
150
+ const typed = new item.arrayCtor(source.buffer, source.byteOffset, bytes.length / bytesPerElement);
151
+ if (item.kind === 'bool') {
152
+ return Array.from(typed, (value) => coerceBoolScalar(value));
153
+ }
154
+ return Array.from(typed);
155
+ }
156
+ function typedArraySource(bytes) {
157
+ if (bytes.buffer instanceof ArrayBuffer) {
158
+ return { buffer: bytes.buffer, byteOffset: bytes.byteOffset };
159
+ }
160
+ const copy = bytes.slice();
161
+ return { buffer: copy.buffer, byteOffset: copy.byteOffset };
162
+ }
163
+ function isFieldType(value) {
164
+ return typeof value === 'object' && value !== null && 'kind' in value && 'originType' in value;
165
+ }
124
166
  function writeMappedField(feature, def, value) {
125
167
  const origin = feature._origin;
126
168
  if (!origin) {
@@ -133,7 +175,7 @@ function writeMappedField(feature, def, value) {
133
175
  return;
134
176
  }
135
177
  if (entry.kind === 'bool') {
136
- origin.setFieldInt(def.index, value ? 1 : 0);
178
+ origin.setFieldInt(def.index, coerceBoolScalar(value) ? 1 : 0);
137
179
  return;
138
180
  }
139
181
  origin.setFieldInt(def.index, Math.trunc(Number(value)));
package/dist/index.d.ts CHANGED
@@ -4,6 +4,8 @@ export { ORM, TableDefn, } from './orm.js';
4
4
  export { Table } from './table.js';
5
5
  export { StridedColumn } from './column.js';
6
6
  export { FastSerializer } from './serializer.js';
7
+ export { FastdbOwnedBytes, allocateFastdbOwnedBytes, loadDatabaseFromBytes, } from './database-buffer.js';
8
+ export type { FastdbDatabaseBytes } from './database-buffer.js';
7
9
  export { defineSchema, getClassSchema, } from './schema.js';
8
10
  export { BOOL, BYTES, F32, F64, I32, REF, STR, U8, U8N, U16, U16N, U32, WSTR, listOf, ref, } from './types.js';
9
11
  export { FastdbError, FastdbRuntimeError, FastdbSchemaError, FastdbUsageError, } from './errors.js';
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export { ORM, TableDefn, } from './orm.js';
4
4
  export { Table } from './table.js';
5
5
  export { StridedColumn } from './column.js';
6
6
  export { FastSerializer } from './serializer.js';
7
+ export { FastdbOwnedBytes, allocateFastdbOwnedBytes, loadDatabaseFromBytes, } from './database-buffer.js';
7
8
  export { defineSchema, getClassSchema, } from './schema.js';
8
9
  export { BOOL, BYTES, F32, F64, I32, REF, STR, U8, U8N, U16, U16N, U32, WSTR, listOf, ref, } from './types.js';
9
10
  export { FastdbError, FastdbRuntimeError, FastdbSchemaError, FastdbUsageError, } from './errors.js';
package/dist/orm.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Feature, type FeatureClass } from './feature.js';
2
2
  import { Table } from './table.js';
3
+ import { type FastdbDatabaseBytes } from './database-buffer.js';
3
4
  export declare class TableDefn<T extends Feature> {
4
5
  readonly featureType: FeatureClass<T>;
5
6
  readonly capacity: number;
@@ -15,8 +16,9 @@ export declare class ORM {
15
16
  get fixed(): boolean;
16
17
  static create(): ORM;
17
18
  static truncate(defns: TableDefn<Feature>[]): ORM;
18
- static fromBuffer(data: Uint8Array | ArrayBuffer): ORM;
19
+ static fromBuffer(data: FastdbDatabaseBytes): ORM;
19
20
  push<T extends Feature>(feature: T, tableName?: string): void;
21
+ ensureTable<T extends Feature>(featureType: FeatureClass<T>, name?: string): Table<T>;
20
22
  combine(): void;
21
23
  toBuffer(): Uint8Array;
22
24
  table<T extends Feature>(featureType: FeatureClass<T>, name?: string): Table<T>;
@@ -25,7 +27,7 @@ export declare class ORM {
25
27
  private static getModule;
26
28
  private getBuildOrigin;
27
29
  private defineTableFields;
30
+ private configureDefaultGeometry;
28
31
  private ensureTruncateCompatible;
29
32
  private copyChunkToBytes;
30
- private disposeBuildTables;
31
33
  }
package/dist/orm.js CHANGED
@@ -1,7 +1,9 @@
1
- import { getClassSchema } from './schema.js';
1
+ import { getClassSchema, resolveListItem } from './schema.js';
2
2
  import { FastdbRuntimeError, FastdbUsageError } from './errors.js';
3
3
  import { Table } from './table.js';
4
+ import { loadDatabaseFromBytes } from './database-buffer.js';
4
5
  import { getInitializedFastdbModule } from './wasm-loader.js';
6
+ import { coerceBoolScalar, isListField } from './types.js';
5
7
  export class TableDefn {
6
8
  featureType;
7
9
  capacity;
@@ -40,7 +42,7 @@ export class ORM {
40
42
  orm.ensureTruncateCompatible(defn.featureType);
41
43
  const layerName = defn.name ?? defn.featureType.name;
42
44
  const table = build.createLayerBegin(layerName);
43
- table.setGeometryType(orm.module.gtAny, orm.module.cfDefault, false);
45
+ orm.configureDefaultGeometry(table, defn.featureType);
44
46
  orm.defineTableFields(table, defn.featureType);
45
47
  build.createLayerEnd();
46
48
  build.truncate(layerName, defn.capacity);
@@ -50,31 +52,13 @@ export class ORM {
50
52
  }
51
53
  static fromBuffer(data) {
52
54
  const module = ORM.getModule();
53
- const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
54
- const ptr = module._malloc(bytes.byteLength);
55
- try {
56
- module.HEAPU8.set(bytes, ptr);
57
- const db = module.WxDatabase.loadFromHeap(ptr, bytes.byteLength);
58
- return new ORM(db, module);
59
- }
60
- finally {
61
- module._free(ptr);
62
- }
55
+ return new ORM(loadDatabaseFromBytes(module, data), module);
63
56
  }
64
57
  push(feature, tableName) {
65
- const build = this.getBuildOrigin();
66
58
  const featureType = feature.constructor;
67
59
  const name = tableName ?? featureType.name;
68
- let table = this.tableMap.get(name);
69
- let origin;
70
- if (!table) {
71
- origin = build.createLayerBegin(name);
72
- origin.setGeometryType(this.module.gtAny, this.module.cfDefault, false);
73
- this.defineTableFields(origin, featureType);
74
- this.tableMap.set(name, new Table(featureType, origin, build, this.module));
75
- table = this.tableMap.get(name);
76
- }
77
- origin = table.origin;
60
+ const table = this.ensureTable(featureType, name);
61
+ const origin = table.origin;
78
62
  origin.addFeatureBegin();
79
63
  try {
80
64
  const schema = getClassSchema(featureType);
@@ -82,7 +66,7 @@ export class ORM {
82
66
  const value = feature[field.name];
83
67
  const kind = field.entry.kind;
84
68
  if (kind === 'bool') {
85
- origin.setFieldInt(field.index, value ? 1 : 0);
69
+ origin.setFieldInt(field.index, coerceBoolScalar(value) ? 1 : 0);
86
70
  }
87
71
  else if (kind === 'u8' ||
88
72
  kind === 'u16' ||
@@ -96,9 +80,24 @@ export class ORM {
96
80
  kind === 'f64') {
97
81
  origin.setFieldDouble(field.index, Number(value));
98
82
  }
99
- else if (kind === 'str' || kind === 'wstr') {
83
+ else if (kind === 'str') {
100
84
  origin.setFieldString(field.index, String(value ?? ''));
101
85
  }
86
+ else if (kind === 'wstr') {
87
+ origin.setFieldWString(field.index, String(value ?? ''));
88
+ }
89
+ else if (kind === 'bytes') {
90
+ const bytes = normalizeBytes(value);
91
+ withHeapBytes(this.module, bytes, (ptr, size) => {
92
+ origin.setGeometryRaw(ptr, size);
93
+ });
94
+ }
95
+ else if (isListField(field.entry)) {
96
+ const bytes = normalizeNumericList(value, listElementFieldType(field), `field "${field.name}"`);
97
+ withHeapBytes(this.module, bytes, (ptr, size) => {
98
+ origin.setFieldListNumeric(field.index, ptr, size);
99
+ });
100
+ }
102
101
  else {
103
102
  throw new FastdbUsageError(`push() does not support field kind "${kind}" yet.`);
104
103
  }
@@ -108,16 +107,30 @@ export class ORM {
108
107
  origin.addFeatureEnd();
109
108
  }
110
109
  }
110
+ ensureTable(featureType, name) {
111
+ const build = this.getBuildOrigin();
112
+ const tableName = name ?? featureType.name;
113
+ const cached = this.tableMap.get(tableName);
114
+ if (cached) {
115
+ return cached;
116
+ }
117
+ const origin = build.createLayerBegin(tableName);
118
+ this.configureDefaultGeometry(origin, featureType);
119
+ this.defineTableFields(origin, featureType);
120
+ const table = new Table(featureType, origin, build, this.module);
121
+ this.tableMap.set(tableName, table);
122
+ return table;
123
+ }
111
124
  combine() {
112
125
  const build = this.getBuildOrigin();
113
126
  const stream = new this.module.WxMemoryStream();
114
127
  build.post(stream);
115
128
  const bytes = this.copyChunkToBytes(stream);
116
129
  stream.delete();
117
- this.disposeBuildTables();
130
+ // Layer handles borrow their native storage from this builder.
131
+ this.tableMap.clear();
118
132
  build.delete();
119
133
  this.origin = ORM.fromBuffer(bytes).origin;
120
- this.tableMap.clear();
121
134
  }
122
135
  toBuffer() {
123
136
  if (this.fixed) {
@@ -176,9 +189,27 @@ export class ORM {
176
189
  }
177
190
  defineTableFields(table, featureType) {
178
191
  const schema = getClassSchema(featureType);
192
+ const bytesFields = schema.fieldList.filter((field) => field.entry.kind === 'bytes');
193
+ if (bytesFields.length > 1) {
194
+ throw new FastdbUsageError(`ORM.push() does not support multiple bytes fields in "${featureType.name}"; fastdb columnar bytes use the feature raw payload.`);
195
+ }
179
196
  for (const field of schema.fieldList) {
180
- table.addField(field.name, field.entry.originType, 0, 1);
197
+ if (isListField(field.entry)) {
198
+ table.addListField(field.name, listElementFieldType(field).originType);
199
+ }
200
+ else {
201
+ table.addField(field.name, field.entry.originType, 0, 1);
202
+ }
203
+ }
204
+ }
205
+ configureDefaultGeometry(table, featureType) {
206
+ const schema = getClassSchema(featureType);
207
+ if (schema.fieldList.some((field) => field.entry.kind === 'bytes')) {
208
+ table.setGeometryType(this.module.gtAny, this.module.cfDefault, false);
209
+ return;
181
210
  }
211
+ table.setGeometryType(this.module.gtPoint, this.module.cfTx32, true);
212
+ table.setExtent(-180, -90, 180, 90);
182
213
  }
183
214
  ensureTruncateCompatible(featureType) {
184
215
  const schema = getClassSchema(featureType);
@@ -192,11 +223,75 @@ export class ORM {
192
223
  const chunk = stream.dataView();
193
224
  return this.module.HEAPU8.slice(chunk.data, chunk.data + chunk.size);
194
225
  }
195
- disposeBuildTables() {
196
- for (const table of this.tableMap.values()) {
197
- if (!table.fixed) {
198
- table.origin.delete();
199
- }
226
+ }
227
+ function normalizeBytes(value) {
228
+ if (value === null || value === undefined) {
229
+ return new Uint8Array(0);
230
+ }
231
+ if (value instanceof Uint8Array) {
232
+ return value;
233
+ }
234
+ if (value instanceof ArrayBuffer) {
235
+ return new Uint8Array(value);
236
+ }
237
+ if (ArrayBuffer.isView(value)) {
238
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
239
+ }
240
+ throw new FastdbUsageError(`push() bytes fields expect Uint8Array or ArrayBuffer, got "${typeof value}".`);
241
+ }
242
+ function listElementFieldType(field) {
243
+ if (!isListField(field.entry)) {
244
+ throw new FastdbUsageError(`Field "${field.name}" is not a list field.`);
245
+ }
246
+ const item = resolveListItem(field.entry);
247
+ if (!isFieldType(item) || !item.arrayCtor || !isSupportedListElementKind(item.kind)) {
248
+ throw new FastdbUsageError(`push() list field "${field.name}" supports only numeric scalar list elements in the columnar runtime.`);
249
+ }
250
+ return item;
251
+ }
252
+ function normalizeNumericList(value, item, context) {
253
+ if (value === null || value === undefined) {
254
+ return new Uint8Array(0);
255
+ }
256
+ const arrayCtor = item.arrayCtor;
257
+ if (!arrayCtor) {
258
+ throw new FastdbUsageError(`${context} is not a numeric fastdb list field.`);
259
+ }
260
+ if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
261
+ const view = value;
262
+ if (value.constructor === arrayCtor) {
263
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
200
264
  }
201
265
  }
266
+ if (!Array.isArray(value) && !(ArrayBuffer.isView(value) && !(value instanceof DataView))) {
267
+ throw new FastdbUsageError(`${context} expects an array or typed array value.`);
268
+ }
269
+ const values = Array.from(value);
270
+ const typed = new arrayCtor(values.length);
271
+ for (let index = 0; index < values.length; index += 1) {
272
+ const numeric = item.kind === 'bool'
273
+ ? (coerceBoolScalar(values[index]) ? 1 : 0)
274
+ : Number(values[index]);
275
+ typed[index] = numeric;
276
+ }
277
+ return new Uint8Array(typed.buffer, typed.byteOffset, typed.byteLength);
278
+ }
279
+ function isSupportedListElementKind(kind) {
280
+ return kind === 'bool' || kind === 'u8' || kind === 'u16' || kind === 'u32' || kind === 'i32' || kind === 'u8n' || kind === 'u16n' || kind === 'f32' || kind === 'f64';
281
+ }
282
+ function isFieldType(value) {
283
+ return typeof value === 'object' && value !== null && 'kind' in value && 'originType' in value;
284
+ }
285
+ function withHeapBytes(module, bytes, fn) {
286
+ if (bytes.length === 0) {
287
+ return fn(0, 0);
288
+ }
289
+ const ptr = module._malloc(bytes.length);
290
+ try {
291
+ module.HEAPU8.set(bytes, ptr);
292
+ return fn(ptr, bytes.length);
293
+ }
294
+ finally {
295
+ module._free(ptr);
296
+ }
202
297
  }
@@ -0,0 +1,27 @@
1
+ import { FastdbModule } from '../wasm-loader.js';
2
+ export declare const ABI_VERSION = 1;
3
+ export declare const SHA256_SIZE = 32;
4
+ export declare const CAPABILITIES_SIZE = 72;
5
+ export declare const POINTER_SIZE = 4;
6
+ export declare const U64_MAX = 18446744073709551615n;
7
+ export declare function initPayloadModule(): Promise<FastdbModule>;
8
+ export declare function payloadModule(): FastdbModule;
9
+ export declare function allocate(module: FastdbModule, size: number): number;
10
+ export declare function withAllocation<T>(module: FastdbModule, size: number, operation: (pointer: number) => T): T;
11
+ export declare function withInputBytes<T>(module: FastdbModule, bytes: Uint8Array, operation: (pointer: number, size: bigint) => T): T;
12
+ export declare function withSha256<T>(module: FastdbModule, digest: Uint8Array, operation: (pointer: number) => T): T;
13
+ export declare function withInputU16<T>(module: FastdbModule, units: Uint16Array, operation: (pointer: number, size: bigint) => T): T;
14
+ export declare function readU32(module: FastdbModule, pointer: number): number;
15
+ export declare function readU64(module: FastdbModule, pointer: number): bigint;
16
+ export declare function writeU32(module: FastdbModule, pointer: number, value: number): void;
17
+ export declare function writeU64(module: FastdbModule, pointer: number, value: bigint): void;
18
+ export declare function copyBytes(module: FastdbModule, pointer: number, size: bigint): Uint8Array;
19
+ export declare function checkedU32(value: number, name: string): number;
20
+ export declare function checkedU16(value: number, name: string): number;
21
+ export declare function checkedU8(value: number, name: string): number;
22
+ export declare function checkedI32(value: number, name: string): number;
23
+ export declare function checkedU64(value: bigint, name: string): bigint;
24
+ export declare function f32Bits(value: number): number;
25
+ export declare function f64Bits(value: number): bigint;
26
+ export declare function f32FromBits(bits: number): number;
27
+ export declare function f64FromBits(bits: bigint): number;