@rotu/structview 0.6.1 → 0.7.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.
Files changed (62) hide show
  1. package/README.md +3 -3
  2. package/esm/_dnt.test_shims.d.ts.map +1 -0
  3. package/esm/bigendian.d.ts.map +1 -0
  4. package/esm/bigendian_test.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/assert/1.0.15/almost_equals.d.ts.map +1 -0
  6. package/esm/deps/jsr.io/@std/assert/1.0.15/array_includes.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@std/assert/1.0.15/assert.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/assert/1.0.15/assertion_error.d.ts.map +1 -0
  9. package/esm/deps/jsr.io/@std/assert/1.0.15/equal.d.ts.map +1 -0
  10. package/esm/deps/jsr.io/@std/assert/1.0.15/equals.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/assert/1.0.15/exists.d.ts.map +1 -0
  12. package/esm/deps/jsr.io/@std/assert/1.0.15/fail.d.ts.map +1 -0
  13. package/esm/deps/jsr.io/@std/assert/1.0.15/false.d.ts.map +1 -0
  14. package/esm/deps/jsr.io/@std/assert/1.0.15/greater.d.ts.map +1 -0
  15. package/esm/deps/jsr.io/@std/assert/1.0.15/greater_or_equal.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/assert/1.0.15/instance_of.d.ts.map +1 -0
  17. package/esm/deps/jsr.io/@std/assert/1.0.15/is_error.d.ts.map +1 -0
  18. package/esm/deps/jsr.io/@std/assert/1.0.15/less.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/assert/1.0.15/less_or_equal.d.ts.map +1 -0
  20. package/esm/deps/jsr.io/@std/assert/1.0.15/match.d.ts.map +1 -0
  21. package/esm/deps/jsr.io/@std/assert/1.0.15/mod.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/assert/1.0.15/not_equals.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@std/assert/1.0.15/not_instance_of.d.ts.map +1 -0
  24. package/esm/deps/jsr.io/@std/assert/1.0.15/not_match.d.ts.map +1 -0
  25. package/esm/deps/jsr.io/@std/assert/1.0.15/not_strict_equals.d.ts.map +1 -0
  26. package/esm/deps/jsr.io/@std/assert/1.0.15/object_match.d.ts.map +1 -0
  27. package/esm/deps/jsr.io/@std/assert/1.0.15/rejects.d.ts.map +1 -0
  28. package/esm/deps/jsr.io/@std/assert/1.0.15/strict_equals.d.ts.map +1 -0
  29. package/esm/deps/jsr.io/@std/assert/1.0.15/string_includes.d.ts.map +1 -0
  30. package/esm/deps/jsr.io/@std/assert/1.0.15/throws.d.ts.map +1 -0
  31. package/esm/deps/jsr.io/@std/assert/1.0.15/unimplemented.d.ts.map +1 -0
  32. package/esm/deps/jsr.io/@std/assert/1.0.15/unreachable.d.ts.map +1 -0
  33. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
  34. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
  35. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
  36. package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
  37. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
  38. package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
  39. package/esm/mod.d.ts +161 -0
  40. package/esm/mod.d.ts.map +1 -0
  41. package/esm/mod.js +378 -0
  42. package/esm/mod_test.d.ts.map +1 -0
  43. package/esm/package.json +3 -0
  44. package/package.json +18 -11
  45. package/.github/workflows/deno.yml +0 -32
  46. package/.github/workflows/publish.yml +0 -20
  47. package/.vscode/settings.json +0 -4
  48. package/_dist/bigendian.d.ts +0 -32
  49. package/_dist/bigendian.d.ts.map +0 -1
  50. package/_dist/mod.d.ts +0 -135
  51. package/_dist/mod.d.ts.map +0 -1
  52. package/bigendian.js +0 -122
  53. package/bigendian.js.map +0 -1
  54. package/bigendian.ts +0 -136
  55. package/bigendian_test.ts +0 -70
  56. package/deno.json +0 -18
  57. package/deno.lock +0 -23
  58. package/mod.js +0 -336
  59. package/mod.js.map +0 -1
  60. package/mod.ts +0 -487
  61. package/mod_bench.ts +0 -98
  62. package/mod_test.ts +0 -319
package/bigendian.js DELETED
@@ -1,122 +0,0 @@
1
- /**
2
- * Property descriptor factories for big-endian numbers
3
- * @module
4
- */ import { structDataView } from "./mod.js";
5
- /**
6
- * Field for a big-endian 16-bit unsigned integer
7
- */ export function u16be(fieldOffset) {
8
- return {
9
- enumerable: true,
10
- get () {
11
- return structDataView(this).getUint16(fieldOffset, false);
12
- },
13
- set (value) {
14
- structDataView(this).setUint16(fieldOffset, value, false);
15
- }
16
- };
17
- }
18
- /**
19
- * Field for a big-endian 32-bit unsigned integer
20
- */ export function u32be(fieldOffset) {
21
- return {
22
- enumerable: true,
23
- get () {
24
- return structDataView(this).getUint32(fieldOffset, false);
25
- },
26
- set (value) {
27
- structDataView(this).setUint32(fieldOffset, value, false);
28
- }
29
- };
30
- }
31
- /**
32
- * Field for a big-endian 64-bit unsigned integer
33
- */ export function u64be(fieldOffset) {
34
- return {
35
- enumerable: true,
36
- get () {
37
- return structDataView(this).getBigUint64(fieldOffset, false);
38
- },
39
- set (value) {
40
- structDataView(this).setBigUint64(fieldOffset, value, false);
41
- }
42
- };
43
- }
44
- /**
45
- * Field for a big-endian 16-bit signed integer
46
- */ export function i16be(fieldOffset) {
47
- return {
48
- enumerable: true,
49
- get () {
50
- return structDataView(this).getInt16(fieldOffset, false);
51
- },
52
- set (value) {
53
- structDataView(this).setInt16(fieldOffset, value, false);
54
- }
55
- };
56
- }
57
- /**
58
- * Field for a big-endian 32-bit signed integer
59
- */ export function i32be(fieldOffset) {
60
- return {
61
- enumerable: true,
62
- get () {
63
- return structDataView(this).getInt32(fieldOffset, false);
64
- },
65
- set (value) {
66
- structDataView(this).setInt32(fieldOffset, value, false);
67
- }
68
- };
69
- }
70
- /**
71
- * Field for a big-endian 64-bit signed integer
72
- */ export function i64be(fieldOffset) {
73
- return {
74
- enumerable: true,
75
- get () {
76
- return structDataView(this).getBigInt64(fieldOffset, false);
77
- },
78
- set (value) {
79
- structDataView(this).setBigInt64(fieldOffset, value, false);
80
- }
81
- };
82
- }
83
- /**
84
- * Field for a big-endian 16-bit binary float (float16_t)
85
- */ export function f16be(fieldOffset) {
86
- return {
87
- enumerable: true,
88
- get () {
89
- return structDataView(this).getFloat16(fieldOffset, false);
90
- },
91
- set (value) {
92
- structDataView(this).setFloat16(fieldOffset, value, false);
93
- }
94
- };
95
- }
96
- /**
97
- * Field for a big-endian 32-bit binary float (float32_t)
98
- */ export function f32be(fieldOffset) {
99
- return {
100
- enumerable: true,
101
- get () {
102
- return structDataView(this).getFloat32(fieldOffset, false);
103
- },
104
- set (value) {
105
- structDataView(this).setFloat32(fieldOffset, value, false);
106
- }
107
- };
108
- }
109
- /**
110
- * Field for a big-endian 64-bit binary float (float64_t)
111
- */ export function f64be(fieldOffset) {
112
- return {
113
- enumerable: true,
114
- get () {
115
- return structDataView(this).getFloat64(fieldOffset, false);
116
- },
117
- set (value) {
118
- structDataView(this).setFloat64(fieldOffset, value, false);
119
- }
120
- };
121
- }
122
- //# sourceMappingURL=bigendian.js.map
package/bigendian.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"bigendian.js","sources":["./bigendian.ts"],"names":[],"mappings":"AAAA;;;CAGC,GAED,SAAS,cAAc,mBAAiD;AAExE;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,SAAS,CAAC,aAAa;IACrD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,SAAS,CAAC,aAAa,OAAO;IACrD;EACF;AACF;AACA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,SAAS,CAAC,aAAa;IACrD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,SAAS,CAAC,aAAa,OAAO;IACrD;EACF;AACF;AACA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,YAAY,CAAC,aAAa;IACxD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,YAAY,CAAC,aAAa,OAAO;IACxD;EACF;AACF;AACA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,QAAQ,CAAC,aAAa;IACpD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,QAAQ,CAAC,aAAa,OAAO;IACpD;EACF;AACF;AACA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,QAAQ,CAAC,aAAa;IACpD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,QAAQ,CAAC,aAAa,OAAO;IACpD;EACF;AACF;AACA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,WAAW,CAAC,aAAa;IACvD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,WAAW,CAAC,aAAa,OAAO;IACvD;EACF;AACF;AAEA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,UAAU,CAAC,aAAa;IACtD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,UAAU,CAAC,aAAa,OAAO;IACtD;EACF;AACF;AAEA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,UAAU,CAAC,aAAa;IACtD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,UAAU,CAAC,aAAa,OAAO;IACtD;EACF;AACF;AAEA;;CAEC,GACD,OAAO,SAAS,MAAM,WAAmB;EACvC,OAAO;IACL,YAAY;IACZ;MACE,OAAO,eAAe,IAAI,EAAE,UAAU,CAAC,aAAa;IACtD;IACA,KAAI,KAAK;MACP,eAAe,IAAI,EAAE,UAAU,CAAC,aAAa,OAAO;IACtD;EACF;AACF"}
package/bigendian.ts DELETED
@@ -1,136 +0,0 @@
1
- /**
2
- * Property descriptor factories for big-endian numbers
3
- * @module
4
- */
5
-
6
- import { structDataView, type StructPropertyDescriptor } from "./mod.js"
7
-
8
- /**
9
- * Field for a big-endian 16-bit unsigned integer
10
- */
11
- export function u16be(fieldOffset: number): StructPropertyDescriptor<number> {
12
- return {
13
- enumerable: true,
14
- get() {
15
- return structDataView(this).getUint16(fieldOffset, false)
16
- },
17
- set(value) {
18
- structDataView(this).setUint16(fieldOffset, value, false)
19
- },
20
- }
21
- }
22
- /**
23
- * Field for a big-endian 32-bit unsigned integer
24
- */
25
- export function u32be(fieldOffset: number): StructPropertyDescriptor<number> {
26
- return {
27
- enumerable: true,
28
- get() {
29
- return structDataView(this).getUint32(fieldOffset, false)
30
- },
31
- set(value) {
32
- structDataView(this).setUint32(fieldOffset, value, false)
33
- },
34
- }
35
- }
36
- /**
37
- * Field for a big-endian 64-bit unsigned integer
38
- */
39
- export function u64be(fieldOffset: number): StructPropertyDescriptor<bigint> {
40
- return {
41
- enumerable: true,
42
- get() {
43
- return structDataView(this).getBigUint64(fieldOffset, false)
44
- },
45
- set(value) {
46
- structDataView(this).setBigUint64(fieldOffset, value, false)
47
- },
48
- }
49
- }
50
- /**
51
- * Field for a big-endian 16-bit signed integer
52
- */
53
- export function i16be(fieldOffset: number): StructPropertyDescriptor<number> {
54
- return {
55
- enumerable: true,
56
- get() {
57
- return structDataView(this).getInt16(fieldOffset, false)
58
- },
59
- set(value) {
60
- structDataView(this).setInt16(fieldOffset, value, false)
61
- },
62
- }
63
- }
64
- /**
65
- * Field for a big-endian 32-bit signed integer
66
- */
67
- export function i32be(fieldOffset: number): StructPropertyDescriptor<number> {
68
- return {
69
- enumerable: true,
70
- get() {
71
- return structDataView(this).getInt32(fieldOffset, false)
72
- },
73
- set(value) {
74
- structDataView(this).setInt32(fieldOffset, value, false)
75
- },
76
- }
77
- }
78
- /**
79
- * Field for a big-endian 64-bit signed integer
80
- */
81
- export function i64be(fieldOffset: number): StructPropertyDescriptor<bigint> {
82
- return {
83
- enumerable: true,
84
- get() {
85
- return structDataView(this).getBigInt64(fieldOffset, false)
86
- },
87
- set(value) {
88
- structDataView(this).setBigInt64(fieldOffset, value, false)
89
- },
90
- }
91
- }
92
-
93
- /**
94
- * Field for a big-endian 16-bit binary float (float16_t)
95
- */
96
- export function f16be(fieldOffset: number): StructPropertyDescriptor<number> {
97
- return {
98
- enumerable: true,
99
- get() {
100
- return structDataView(this).getFloat16(fieldOffset, false)
101
- },
102
- set(value) {
103
- structDataView(this).setFloat16(fieldOffset, value, false)
104
- },
105
- }
106
- }
107
-
108
- /**
109
- * Field for a big-endian 32-bit binary float (float32_t)
110
- */
111
- export function f32be(fieldOffset: number): StructPropertyDescriptor<number> {
112
- return {
113
- enumerable: true,
114
- get() {
115
- return structDataView(this).getFloat32(fieldOffset, false)
116
- },
117
- set(value) {
118
- structDataView(this).setFloat32(fieldOffset, value, false)
119
- },
120
- }
121
- }
122
-
123
- /**
124
- * Field for a big-endian 64-bit binary float (float64_t)
125
- */
126
- export function f64be(fieldOffset: number): StructPropertyDescriptor<number> {
127
- return {
128
- enumerable: true,
129
- get() {
130
- return structDataView(this).getFloat64(fieldOffset, false)
131
- },
132
- set(value) {
133
- structDataView(this).setFloat64(fieldOffset, value, false)
134
- },
135
- }
136
- }
package/bigendian_test.ts DELETED
@@ -1,70 +0,0 @@
1
- import {
2
- f16be,
3
- f32be,
4
- f64be,
5
- i16be,
6
- i32be,
7
- i64be,
8
- u16be,
9
- u32be,
10
- u64be,
11
- } from "./bigendian.ts"
12
- import { defineStruct, i16, i32, i64, u16, u32, u64 } from "./mod.ts"
13
-
14
- import { assertEquals } from "jsr:@std/assert@1"
15
-
16
- Deno.test("integers", () => {
17
- const buf = new Uint8Array(16)
18
- const Integers = defineStruct({
19
- as_i16: i16(1),
20
- as_i32: i32(1),
21
- as_i64: i64(1),
22
- as_u16: u16(1),
23
- as_u32: u32(1),
24
- as_u64: u64(1),
25
-
26
- as_i16be: i16be(1),
27
- as_i32be: i32be(1),
28
- as_i64be: i64be(1),
29
- as_u16be: u16be(1),
30
- as_u32be: u32be(1),
31
- as_u64be: u64be(1),
32
- })
33
- const b = new Integers(buf)
34
- b.as_i16be = 0x0102
35
- assertEquals(b.as_i16be, 0x0102)
36
- assertEquals(b.as_i16, 0x0201)
37
- assertEquals(b.as_u16be, 0x0102)
38
- assertEquals(b.as_u16, 0x0201)
39
-
40
- b.as_i32be = 0x01020304
41
- assertEquals(b.as_i32be, 0x01020304)
42
- assertEquals(b.as_i32, 0x04030201)
43
- assertEquals(b.as_u32be, 0x01020304)
44
- assertEquals(b.as_u32, 0x04030201)
45
-
46
- b.as_i64be = 0x0102030405060708n
47
- assertEquals(b.as_i64be, 0x0102030405060708n)
48
- assertEquals(b.as_i64, 0x0807060504030201n)
49
- assertEquals(b.as_u64be, 0x0102030405060708n)
50
- assertEquals(b.as_u64, 0x0807060504030201n)
51
- })
52
-
53
- Deno.test("floats", () => {
54
- const Floats = defineStruct({
55
- f16: f16be(0),
56
- f32: f32be(4),
57
- f64: f64be(8),
58
- })
59
- const bytes = new Uint8Array(16)
60
- const v = new Floats(bytes)
61
- assertEquals(v.f16, 0)
62
- assertEquals(v.f32, 0)
63
- assertEquals(v.f64, 0)
64
- v.f16 = 1 / 3
65
- v.f32 = 1 / 3
66
- v.f64 = 1 / 3
67
- assertEquals(v.f16, Math.f16round(1 / 3))
68
- assertEquals(v.f32, Math.fround(1 / 3))
69
- assertEquals(v.f64, 1 / 3)
70
- })
package/deno.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "name": "@rotu/structview",
3
- "version": "0.6.1",
4
- "license": "MIT",
5
- "tasks": {
6
- "dev": "deno test --watch"
7
- },
8
- "fmt": {
9
- "semiColons": false
10
- },
11
- "imports": {
12
- "@std/assert": "jsr:@std/assert@1"
13
- },
14
- "exports": {
15
- ".": "./mod.ts",
16
- "./bigendian": "./bigendian.ts"
17
- }
18
- }
package/deno.lock DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "version": "5",
3
- "specifiers": {
4
- "jsr:@std/assert@1": "1.0.15",
5
- "jsr:@std/internal@^1.0.12": "1.0.12"
6
- },
7
- "jsr": {
8
- "@std/assert@1.0.15": {
9
- "integrity": "d64018e951dbdfab9777335ecdb000c0b4e3df036984083be219ce5941e4703b",
10
- "dependencies": [
11
- "jsr:@std/internal"
12
- ]
13
- },
14
- "@std/internal@1.0.12": {
15
- "integrity": "972a634fd5bc34b242024402972cd5143eac68d8dffaca5eaa4dba30ce17b027"
16
- }
17
- },
18
- "workspace": {
19
- "dependencies": [
20
- "jsr:@std/assert@1"
21
- ]
22
- }
23
- }
package/mod.js DELETED
@@ -1,336 +0,0 @@
1
- /**
2
- * Strongly typed classes for accessing binary data in a structured way
3
- * @module
4
- */ var _computedKey;
5
- const dataViewSymbol = Symbol.for("Struct.dataview");
6
- /**
7
- * Get the underlying DataView of a struct
8
- * @param struct
9
- * @returns
10
- */ export function structDataView(struct) {
11
- const result = struct[dataViewSymbol];
12
- if (!(result instanceof DataView)) {
13
- throw new TypeError("not a struct");
14
- }
15
- return struct[dataViewSymbol];
16
- }
17
- /**
18
- * Helper method to create a view of a contiguous subregion of a Struct's memory
19
- * @param struct
20
- * @param start byte offset to start
21
- * @param end byte offset of the end of the subrange
22
- * @returns region of the given struct.
23
- */ function structBytes(struct, start, end) {
24
- const dv = structDataView(struct);
25
- start ??= 0;
26
- end ??= dv.byteLength;
27
- console.assert(start <= end);
28
- console.assert(end <= dv.byteLength);
29
- return new Uint8Array(dv.buffer, dv.byteOffset + start, end - start);
30
- }
31
- /**
32
- * Define a descriptor based on a dataview of the struct
33
- * @param fieldGetter function which, given a dataview, returns
34
- * @returns
35
- */ export function fromDataView(fieldGetter) {
36
- return {
37
- enumerable: true,
38
- get () {
39
- const dv = this[dataViewSymbol];
40
- return fieldGetter(dv);
41
- }
42
- };
43
- }
44
- /**
45
- * Field for a 8-bit unsigned integer
46
- */ export function u8(fieldOffset) {
47
- return {
48
- enumerable: true,
49
- get () {
50
- return structDataView(this).getUint8(fieldOffset);
51
- },
52
- set (value) {
53
- structDataView(this).setUint8(fieldOffset, value);
54
- }
55
- };
56
- }
57
- /**
58
- * Field for a little-endian 16-bit unsigned integer
59
- */ export function u16(fieldOffset) {
60
- return {
61
- enumerable: true,
62
- get () {
63
- return structDataView(this).getUint16(fieldOffset, true);
64
- },
65
- set (value) {
66
- structDataView(this).setUint16(fieldOffset, value, true);
67
- }
68
- };
69
- }
70
- /**
71
- * Field for a little-endian 32-bit unsigned integer
72
- */ export function u32(fieldOffset) {
73
- return {
74
- enumerable: true,
75
- get () {
76
- return structDataView(this).getUint32(fieldOffset, true);
77
- },
78
- set (value) {
79
- structDataView(this).setUint32(fieldOffset, value, true);
80
- }
81
- };
82
- }
83
- /**
84
- * Field for a little-endian 64-bit unsigned integer
85
- */ export function u64(fieldOffset) {
86
- return {
87
- enumerable: true,
88
- get () {
89
- return structDataView(this).getBigUint64(fieldOffset, true);
90
- },
91
- set (value) {
92
- structDataView(this).setBigUint64(fieldOffset, value, true);
93
- }
94
- };
95
- }
96
- /**
97
- * Field for a little-endian 8-bit signed integer
98
- */ export function i8(fieldOffset) {
99
- return {
100
- enumerable: true,
101
- get () {
102
- return structDataView(this).getInt8(fieldOffset);
103
- },
104
- set (value) {
105
- structDataView(this).setInt8(fieldOffset, value);
106
- }
107
- };
108
- }
109
- /**
110
- * Field for a little-endian 16-bit signed integer
111
- */ export function i16(fieldOffset) {
112
- return {
113
- enumerable: true,
114
- get () {
115
- return structDataView(this).getInt16(fieldOffset, true);
116
- },
117
- set (value) {
118
- structDataView(this).setInt16(fieldOffset, value, true);
119
- }
120
- };
121
- }
122
- /**
123
- * Field for a little-endian 32-bit signed integer
124
- */ export function i32(fieldOffset) {
125
- return {
126
- enumerable: true,
127
- get () {
128
- return structDataView(this).getInt32(fieldOffset, true);
129
- },
130
- set (value) {
131
- structDataView(this).setInt32(fieldOffset, value, true);
132
- }
133
- };
134
- }
135
- /**
136
- * Field for a little-endian 64-bit signed integer
137
- */ export function i64(fieldOffset) {
138
- return {
139
- enumerable: true,
140
- get () {
141
- return structDataView(this).getBigInt64(fieldOffset, true);
142
- },
143
- set (value) {
144
- structDataView(this).setBigInt64(fieldOffset, value, true);
145
- }
146
- };
147
- }
148
- /**
149
- * Field for a little-endian 16-bit binary float (float16_t)
150
- */ export function f16(fieldOffset) {
151
- return {
152
- enumerable: true,
153
- get () {
154
- return structDataView(this).getFloat16(fieldOffset, true);
155
- },
156
- set (value) {
157
- structDataView(this).setFloat16(fieldOffset, value, true);
158
- }
159
- };
160
- }
161
- /**
162
- * Field for a little-endian 32-bit binary float (float32_t)
163
- */ export function f32(fieldOffset) {
164
- return {
165
- enumerable: true,
166
- get () {
167
- return structDataView(this).getFloat32(fieldOffset, true);
168
- },
169
- set (value) {
170
- structDataView(this).setFloat32(fieldOffset, value, true);
171
- }
172
- };
173
- }
174
- /**
175
- * Field for a little-endian 64-bit binary float (float64_t)
176
- */ export function f64(fieldOffset) {
177
- return {
178
- enumerable: true,
179
- get () {
180
- return structDataView(this).getFloat64(fieldOffset, true);
181
- },
182
- set (value) {
183
- structDataView(this).setFloat64(fieldOffset, value, true);
184
- }
185
- };
186
- }
187
- /**
188
- * Field for a UTF-8 fixed-length string
189
- */ export function string(fieldOffset, byteLength) {
190
- const TEXT_DECODER = new TextDecoder();
191
- const TEXT_ENCODER = new TextEncoder();
192
- return {
193
- enumerable: true,
194
- get () {
195
- const str = TEXT_DECODER.decode(structBytes(this, fieldOffset, fieldOffset + byteLength));
196
- // trim all trailing null characters
197
- return str.replace(/\0+$/, "");
198
- },
199
- set (value) {
200
- const bytes = structBytes(this, fieldOffset, fieldOffset + byteLength);
201
- bytes.fill(0);
202
- TEXT_ENCODER.encodeInto(value, bytes);
203
- }
204
- };
205
- }
206
- /**
207
- * Field for a boolean stored in a byte (0 = false, nonzero = true)
208
- * True will be stored as 1
209
- */ export function bool(fieldOffset) {
210
- return {
211
- enumerable: true,
212
- get () {
213
- return Boolean(structDataView(this).getUint8(fieldOffset));
214
- },
215
- set (value) {
216
- structDataView(this).setUint8(fieldOffset, value ? 1 : 0);
217
- }
218
- };
219
- }
220
- /**
221
- * Field for an embedded struct
222
- * @param ctor constructor for the inner struct
223
- * @param byteOffset where the inner struct starts relative to the outer struct
224
- * @param bytelength the length in bytes of the inner struct
225
- * @returns property descriptor for a struct
226
- */ export function substruct(ctor, byteOffset, bytelength) {
227
- return fromDataView(function(dv) {
228
- const offset2 = dv.byteOffset + (byteOffset ?? 0);
229
- const bytelength2 = bytelength ?? dv.byteLength - (byteOffset ?? 0);
230
- return Reflect.construct(ctor, [
231
- {
232
- buffer: dv.buffer,
233
- byteOffset: offset2,
234
- byteLength: bytelength2
235
- }
236
- ]);
237
- });
238
- }
239
- _computedKey = Symbol.toStringTag;
240
- /**
241
- * Base class for a structured binary object
242
- * Note there are no predeclared string-keyed properties - all property names are reserved for user-defined fields
243
- */ export class Struct {
244
- [dataViewSymbol];
245
- get [_computedKey]() {
246
- return Struct.name;
247
- }
248
- static toDataView(o) {
249
- return o[dataViewSymbol];
250
- }
251
- /**
252
- * Create a new Struct
253
- * @param arg options for creating the struct.
254
- * If options has a `.buffer` property, we will use that as the backing memory (e.g. any TypedArray or DataView).
255
- * If options has no `.buffer` property but has a `.byteLength`, we will allocate a new buffer for the object.
256
- */ constructor(arg){
257
- if (typeof arg !== "object" || arg === null) {
258
- throw new TypeError("Expected argument to be an object");
259
- }
260
- Object.preventExtensions(this);
261
- if (arg.buffer) {
262
- this[dataViewSymbol] = new DataView(arg.buffer, arg.byteOffset, arg.byteLength);
263
- } else if (typeof arg.byteLength === "number") {
264
- this[dataViewSymbol] = new DataView(new ArrayBuffer(arg.byteLength + (arg.byteOffset ?? 0)), arg.byteOffset, arg.byteLength);
265
- } else {
266
- throw new TypeError("Must provide either {buffer} or {byteLength}");
267
- }
268
- }
269
- }
270
- /**
271
- * Subclass a type by adding the given property descriptors
272
- * @param ctor constructor for the base class
273
- * @param propertyDescriptors properties to add to subclass instances
274
- * @returns A new class, inheriting from the base class, with the new property descriptors added
275
- */ function subclassWithProperties(ctor, propertyDescriptors) {
276
- return class extends ctor {
277
- static{
278
- Object.defineProperties(this.prototype, propertyDescriptors);
279
- }
280
- };
281
- }
282
- /**
283
- * Subclass struct by adding the given property descriptors
284
- * @param propertyDescriptors properties to add to subclass instances
285
- * @returns A new class, inheriting from `Struct`, with the new property descriptors added
286
- */ export function defineStruct(propertyDescriptors) {
287
- return subclassWithProperties(Struct, propertyDescriptors);
288
- }
289
- /**
290
- * Create a new struct subclass for an array of structs
291
- * @param arrayOptions
292
- * @returns A new class, inheriting from `Struct` whose elements are statically typed structs
293
- */ export function defineArray(arrayOptions) {
294
- var _computedKey;
295
- const { struct, byteStride, length } = arrayOptions;
296
- // Define a subclass of Struct which translates array-like index access into element access
297
- // x[2] -> x.element(2)
298
- const ProxiedStruct = function(...args) {
299
- return Reflect.construct(Struct, args, new.target);
300
- };
301
- ProxiedStruct.prototype = new Proxy(Struct.prototype, {
302
- get (target, p, receiver) {
303
- if (typeof p === "string") {
304
- const i = parseInt(p);
305
- if (p === String(i)) {
306
- return receiver.element(i);
307
- }
308
- }
309
- return Reflect.get(target, p, receiver);
310
- }
311
- });
312
- _computedKey = Symbol.iterator;
313
- class StructArray extends ProxiedStruct {
314
- #struct = struct;
315
- #length = length;
316
- #byteStride = byteStride;
317
- get length() {
318
- if (typeof this.#length === "number") {
319
- return this.#length;
320
- }
321
- return structDataView(this).byteLength / this.#byteStride;
322
- }
323
- element(i) {
324
- const ctor = this.#struct;
325
- return new ctor(structBytes(this, this.#byteStride * i, this.#byteStride * (i + 1)));
326
- }
327
- *[_computedKey]() {
328
- for(let i = 0; i < this.length; ++i){
329
- yield this.element(i);
330
- }
331
- }
332
- }
333
- // deno-lint-ignore no-explicit-any
334
- return StructArray;
335
- }
336
- //# sourceMappingURL=mod.js.map