@flighthq/abc 0.5.1-next.920.da8f530 → 0.6.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/types.js ADDED
@@ -0,0 +1,194 @@
1
+ export const AbcMultinameKind = {
2
+ Multiname: 0x09,
3
+ MultinameA: 0x0e,
4
+ MultinameL: 0x1b,
5
+ MultinameLA: 0x1c,
6
+ QName: 0x07,
7
+ QNameA: 0x0d,
8
+ RtqName: 0x0f,
9
+ RtqNameA: 0x10,
10
+ RtqNameL: 0x11,
11
+ RtqNameLA: 0x12,
12
+ TypeName: 0x1d,
13
+ };
14
+ export const AbcTraitKind = {
15
+ Class: 4,
16
+ Const: 6,
17
+ Function: 5,
18
+ Getter: 2,
19
+ Method: 1,
20
+ Setter: 3,
21
+ Slot: 0,
22
+ };
23
+ // The AVM2 instruction set, by opcode byte. Transcribed from the published bytecode format description —
24
+ // an opcode's number and the shape of its operands are facts about the format, and nothing here derives
25
+ // from any implementation of it.
26
+ export const AbcOpcode = {
27
+ Add: 0xa0,
28
+ AddInt: 0xc5,
29
+ ApplyType: 0x53,
30
+ AsType: 0x86,
31
+ AsTypeLate: 0x87,
32
+ BitAnd: 0xa8,
33
+ BitNot: 0x97,
34
+ BitOr: 0xa9,
35
+ BitXor: 0xaa,
36
+ Breakpoint: 0x01,
37
+ BreakpointLine: 0xf2,
38
+ Call: 0x41,
39
+ CallMethod: 0x43,
40
+ CallProperty: 0x46,
41
+ CallPropLex: 0x4c,
42
+ CallPropVoid: 0x4f,
43
+ CallStatic: 0x44,
44
+ CallSuper: 0x45,
45
+ CallSuperVoid: 0x4e,
46
+ CheckFilter: 0x78,
47
+ Coerce: 0x80,
48
+ CoerceAny: 0x82,
49
+ // The typed coerce forms are deprecated in favour of `coerce` with an explicit type, but compilers
50
+ // still emit them and a walk that does not know them desynchronizes.
51
+ CoerceBoolean: 0x81,
52
+ CoerceDouble: 0x84,
53
+ CoerceInt: 0x83,
54
+ CoerceObject: 0x89,
55
+ CoerceString: 0x85,
56
+ CoerceUint: 0x88,
57
+ Construct: 0x42,
58
+ ConstructProp: 0x4a,
59
+ ConstructSuper: 0x49,
60
+ ConvertBoolean: 0x76,
61
+ ConvertDouble: 0x75,
62
+ ConvertInt: 0x73,
63
+ ConvertObject: 0x77,
64
+ ConvertString: 0x70,
65
+ ConvertUint: 0x74,
66
+ Debug: 0xef,
67
+ DebugFile: 0xf1,
68
+ DebugLine: 0xf0,
69
+ DecLocal: 0x94,
70
+ DecLocalInt: 0xc3,
71
+ Decrement: 0x93,
72
+ DecrementInt: 0xc1,
73
+ DeleteProperty: 0x6a,
74
+ Divide: 0xa3,
75
+ Dup: 0x2a,
76
+ Dxns: 0x06,
77
+ DxnsLate: 0x07,
78
+ Equals: 0xab,
79
+ FindDef: 0x5f,
80
+ FindProperty: 0x5e,
81
+ FindPropStrict: 0x5d,
82
+ GetDescendants: 0x59,
83
+ GetGlobalScope: 0x64,
84
+ GetGlobalSlot: 0x6e,
85
+ GetLex: 0x60,
86
+ GetLocal: 0x62,
87
+ GetLocal0: 0xd0,
88
+ GetLocal1: 0xd1,
89
+ GetLocal2: 0xd2,
90
+ GetLocal3: 0xd3,
91
+ GetProperty: 0x66,
92
+ GetScopeObject: 0x65,
93
+ GetSlot: 0x6c,
94
+ GetSuper: 0x04,
95
+ GreaterEquals: 0xb0,
96
+ GreaterThan: 0xaf,
97
+ HasNext: 0x1f,
98
+ HasNext2: 0x32,
99
+ IfEq: 0x13,
100
+ IfFalse: 0x12,
101
+ IfGe: 0x18,
102
+ IfGt: 0x17,
103
+ IfLe: 0x16,
104
+ IfLt: 0x15,
105
+ IfNe: 0x14,
106
+ IfNge: 0x0f,
107
+ IfNgt: 0x0e,
108
+ IfNle: 0x0d,
109
+ IfNlt: 0x0c,
110
+ IfStrictEq: 0x19,
111
+ IfStrictNe: 0x1a,
112
+ IfTrue: 0x11,
113
+ In: 0xb4,
114
+ IncLocal: 0x92,
115
+ IncLocalInt: 0xc2,
116
+ Increment: 0x91,
117
+ IncrementInt: 0xc0,
118
+ InitProperty: 0x68,
119
+ InstanceOf: 0xb1,
120
+ IsType: 0xb2,
121
+ IsTypeLate: 0xb3,
122
+ Jump: 0x10,
123
+ Kill: 0x08,
124
+ Label: 0x09,
125
+ LessEquals: 0xae,
126
+ LessThan: 0xad,
127
+ LoadFloat32: 0x38,
128
+ LoadFloat64: 0x39,
129
+ LoadInt16: 0x36,
130
+ LoadInt32: 0x37,
131
+ LoadInt8: 0x35,
132
+ LookupSwitch: 0x1b,
133
+ LShift: 0xa5,
134
+ Modulo: 0xa4,
135
+ Multiply: 0xa2,
136
+ MultiplyInt: 0xc7,
137
+ Negate: 0x90,
138
+ NegateInt: 0xc4,
139
+ NewActivation: 0x57,
140
+ NewArray: 0x56,
141
+ NewCatch: 0x5a,
142
+ NewClass: 0x58,
143
+ NewFunction: 0x40,
144
+ NewObject: 0x55,
145
+ NextName: 0x1e,
146
+ NextValue: 0x23,
147
+ Nop: 0x02,
148
+ Not: 0x96,
149
+ Pop: 0x29,
150
+ PopScope: 0x1d,
151
+ PushByte: 0x24,
152
+ PushDouble: 0x2f,
153
+ PushFalse: 0x27,
154
+ PushInt: 0x2d,
155
+ PushNamespace: 0x31,
156
+ PushNan: 0x28,
157
+ PushNull: 0x20,
158
+ PushScope: 0x30,
159
+ PushShort: 0x25,
160
+ PushString: 0x2c,
161
+ PushTrue: 0x26,
162
+ PushUint: 0x2e,
163
+ PushUndefined: 0x21,
164
+ PushWith: 0x1c,
165
+ ReturnValue: 0x48,
166
+ ReturnVoid: 0x47,
167
+ RShift: 0xa6,
168
+ SetGlobalSlot: 0x6f,
169
+ SetLocal: 0x63,
170
+ SetLocal0: 0xd4,
171
+ SetLocal1: 0xd5,
172
+ SetLocal2: 0xd6,
173
+ SetLocal3: 0xd7,
174
+ SetProperty: 0x61,
175
+ SetSlot: 0x6d,
176
+ SetSuper: 0x05,
177
+ SignExtend1: 0x50,
178
+ SignExtend16: 0x52,
179
+ SignExtend8: 0x51,
180
+ StoreFloat32: 0x3d,
181
+ StoreFloat64: 0x3e,
182
+ StoreInt16: 0x3b,
183
+ StoreInt32: 0x3c,
184
+ StoreInt8: 0x3a,
185
+ StrictEquals: 0xac,
186
+ Subtract: 0xa1,
187
+ SubtractInt: 0xc6,
188
+ Swap: 0x2b,
189
+ Throw: 0x03,
190
+ Timestamp: 0xf3,
191
+ TypeOf: 0x95,
192
+ URShift: 0xa7,
193
+ };
194
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAgKA,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,SAAS,EAAE,IAAI;IACf,UAAU,EAAE,IAAI;IAChB,UAAU,EAAE,IAAI;IAChB,WAAW,EAAE,IAAI;IACjB,KAAK,EAAE,IAAI;IACX,MAAM,EAAE,IAAI;IACZ,OAAO,EAAE,IAAI;IACb,QAAQ,EAAE,IAAI;IACd,QAAQ,EAAE,IAAI;IACd,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,IAAI;CACN,CAAC;AAIX,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;IACR,QAAQ,EAAE,CAAC;IACX,MAAM,EAAE,CAAC;IACT,MAAM,EAAE,CAAC;IACT,MAAM,EAAE,CAAC;IACT,IAAI,EAAE,CAAC;CACC,CAAC;AAcX,yGAAyG;AACzG,wGAAwG;AACxG,iCAAiC;AACjC,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,GAAG,EAAE,IAAI;IACT,MAAM,EAAE,IAAI;IACZ,SAAS,EAAE,IAAI;IACf,MAAM,EAAE,IAAI;IACZ,UAAU,EAAE,IAAI;IAChB,MAAM,EAAE,IAAI;IACZ,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,IAAI;IACX,MAAM,EAAE,IAAI;IACZ,UAAU,EAAE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ANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flighthq/abc",
3
- "version": "0.5.1-next.920.da8f530",
3
+ "version": "0.6.0",
4
4
  "author": "Joshua Granick and other contributors",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,16 +32,15 @@
32
32
  "scripts": {
33
33
  "build": "tsc -b",
34
34
  "clean": "tsc -b --clean",
35
+ "generate": "tsx ../../scripts/generate-rust-package.ts --crate flight-abc --package abc",
36
+ "generate:check": "tsx ../../scripts/generate-rust-package.ts --crate flight-abc --package abc --check",
35
37
  "test": "vitest run --config vitest.config.ts",
36
38
  "test:watch": "vitest --watch --config vitest.config.ts",
37
39
  "prepack": "npm run clean && npm run clean:dist && npm run build",
38
40
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
39
41
  },
40
- "dependencies": {
41
- "@flighthq/types": "0.5.1-next.920.da8f530"
42
- },
43
42
  "devDependencies": {
44
- "typescript": "^5.3.0"
43
+ "typescript": "^7.0.2"
45
44
  },
46
45
  "description": "ABC (AVM2) bytecode container parsing into plain data",
47
46
  "sideEffects": false
@@ -1,214 +1,218 @@
1
- import { AbcMultinameKind, AbcTraitKind } from '@flighthq/types/contract';
1
+ // Generated from crates/flight-abc/tests/abc_file_test.rs by flight-compiler.
2
+ // Rust is authoritative; run `npm run generate` after changing it.
2
3
 
3
- import { readAbcFile } from './abcFile';
4
+ import { describe, expect, it } from 'vitest';
4
5
 
5
- describe('readAbcFile', () => {
6
- it('reads the version and the whole constant pool, keeping the reserved zero entry', () => {
7
- const file = readAbcFile(buildAbc());
6
+ import { AbcMultinameKind, AbcTraitKind, readAbcFile } from './contract.ts';
7
+ import type { AbcNamespace, AbcOptionalValue } from './contract.ts';
8
8
 
9
- expect(file?.minorVersion).toBe(16);
10
- expect(file?.majorVersion).toBe(46);
11
- // Index 0 of every pool is the placeholder the format reserves, so pool indices in the file's own
12
- // records can be used directly.
13
- expect(file?.constantPool.strings[0]).toBe('');
14
- expect(file?.constantPool.strings.slice(1)).toEqual(['Main', 'stop', 'flash.display']);
15
- expect(file?.constantPool.integers).toEqual([0, -7]);
16
- expect(file?.constantPool.unsignedIntegers).toEqual([0, 9]);
17
- expect(file?.constantPool.doubles[1]).toBeCloseTo(1.5);
9
+ describe('readAbcFile', () => {
10
+ function buildAbc(): Uint8Array {
11
+ let bytes: number[] = [];
12
+ function pushDouble(bytes: number[], value: number): void {
13
+ {
14
+ const source = (() => {
15
+ const __b = new Uint8Array(8);
16
+ new DataView(__b.buffer).setFloat64(0, value, true);
17
+ return __b;
18
+ })();
19
+ for (let index = 0, count = source.length; index < count; index++) {
20
+ bytes.push(source[index]);
21
+ }
22
+ }
23
+ }
24
+ function pushString(bytes: number[], value: string): void {
25
+ pushU30(bytes, value.length);
26
+ {
27
+ const source = new TextEncoder().encode(value);
28
+ for (let index = 0, count = source.length; index < count; index++) {
29
+ bytes.push(source[index]);
30
+ }
31
+ }
32
+ }
33
+ function pushU16(bytes: number[], value: number): void {
34
+ {
35
+ const source = (() => {
36
+ const __b = new Uint8Array(2);
37
+ new DataView(__b.buffer).setUint16(0, value, true);
38
+ return __b;
39
+ })();
40
+ for (let index = 0, count = source.length; index < count; index++) {
41
+ bytes.push(source[index]);
42
+ }
43
+ }
44
+ }
45
+ function pushU30(bytes: number[], value: number): void {
46
+ let remaining = value;
47
+ for (;;) {
48
+ const byte_ = (remaining % 0x80) & 0xff;
49
+ remaining = Math.trunc(remaining / 0x80);
50
+ bytes.push(remaining > 0 ? byte_ | 0x80 : byte_);
51
+ if (remaining === 0) {
52
+ break;
53
+ }
54
+ }
55
+ }
56
+ pushU16(bytes, 16);
57
+ pushU16(bytes, 46);
58
+ pushU30(bytes, 2);
59
+ pushU30(bytes, 0xfffffff9);
60
+ pushU30(bytes, 2);
61
+ pushU30(bytes, 9);
62
+ pushU30(bytes, 2);
63
+ pushDouble(bytes, 1.5);
64
+ pushU30(bytes, 4);
65
+ pushString(bytes, 'Main');
66
+ pushString(bytes, 'stop');
67
+ pushString(bytes, 'flash.display');
68
+ pushU30(bytes, 2);
69
+ bytes.push(0x16);
70
+ pushU30(bytes, 3);
71
+ pushU30(bytes, 2);
72
+ pushU30(bytes, 1);
73
+ pushU30(bytes, 1);
74
+ pushU30(bytes, 3);
75
+ bytes.push(AbcMultinameKind.QName & 0xff);
76
+ pushU30(bytes, 1);
77
+ pushU30(bytes, 1);
78
+ bytes.push(AbcMultinameKind.Multiname & 0xff);
79
+ pushU30(bytes, 2);
80
+ pushU30(bytes, 1);
81
+ pushU30(bytes, 2);
82
+ pushU30(bytes, 0);
83
+ pushU30(bytes, 0);
84
+ pushU30(bytes, 2);
85
+ bytes.push(0);
86
+ pushU30(bytes, 1);
87
+ pushU30(bytes, 0);
88
+ pushU30(bytes, 1);
89
+ pushU30(bytes, 1);
90
+ bytes.push(0x08 | 0x80);
91
+ pushU30(bytes, 1);
92
+ pushU30(bytes, 1);
93
+ bytes.push(0x03);
94
+ pushU30(bytes, 1);
95
+ pushU30(bytes, 0);
96
+ pushU30(bytes, 1);
97
+ pushU30(bytes, 1);
98
+ pushU30(bytes, 1);
99
+ bytes.push(0);
100
+ pushU30(bytes, 0);
101
+ pushU30(bytes, 1);
102
+ pushU30(bytes, 1);
103
+ pushU30(bytes, 2);
104
+ bytes.push(AbcTraitKind.Method & 0xff);
105
+ pushU30(bytes, 0);
106
+ pushU30(bytes, 0);
107
+ pushU30(bytes, 0);
108
+ pushU30(bytes, 0);
109
+ pushU30(bytes, 1);
110
+ pushU30(bytes, 0);
111
+ pushU30(bytes, 0);
112
+ pushU30(bytes, 1);
113
+ pushU30(bytes, 0);
114
+ pushU30(bytes, 2);
115
+ pushU30(bytes, 1);
116
+ pushU30(bytes, 1);
117
+ pushU30(bytes, 2);
118
+ pushU30(bytes, 3);
119
+ bytes.push(0xd0);
120
+ bytes.push(0x30);
121
+ bytes.push(0x47);
122
+ pushU30(bytes, 0);
123
+ pushU30(bytes, 0);
124
+ return new Uint8Array(bytes);
125
+ }
126
+ it('never panics on arbitrary bytes', () => {
127
+ const seed: { value: number } = { value: 0x2f6a51b3 };
128
+ function next(seed: { value: number }): number {
129
+ seed.value = ((Math.imul(seed.value, 1664525) >>> 0) + 1013904223) >>> 0;
130
+ return seed.value >>> 0;
131
+ }
132
+ for (let _ = 0; _ < 500; _++) {
133
+ const length = (next(seed) % 200) >>> 0;
134
+ let bytes = new Array(length).fill(0);
135
+ for (let __i = 0; __i < bytes.length; __i++) {
136
+ bytes[__i] = next(seed) & 0xff;
137
+ }
138
+ readAbcFile(new Uint8Array(bytes));
139
+ }
140
+ for (let _ = 0; _ < 500; _++) {
141
+ let mutant = buildAbc();
142
+ for (let _ = 0; _ < 3; _++) {
143
+ const index = (next(seed) >>> 0) % mutant.length;
144
+ mutant[index] = next(seed) & 0xff;
145
+ }
146
+ readAbcFile(new Uint8Array(mutant));
147
+ }
18
148
  });
19
-
20
- it('reads namespaces, namespace sets, and both multiname shapes', () => {
21
- const file = readAbcFile(buildAbc());
22
-
23
- expect(file?.constantPool.namespaces[1]).toEqual({ kind: 0x16, name: 3 });
24
- expect(file?.constantPool.namespaceSets[1]).toEqual([1]);
25
-
26
- const qualified = file!.constantPool.multinames[1];
27
- expect(qualified.kind).toBe(AbcMultinameKind.QName);
28
- expect(qualified.namespace).toBe(1);
29
- expect(file?.constantPool.strings[qualified.name]).toBe('Main');
30
-
31
- const multiname = file!.constantPool.multinames[2];
32
- expect(multiname.kind).toBe(AbcMultinameKind.Multiname);
33
- expect(file?.constantPool.strings[multiname.name]).toBe('stop');
34
- expect(multiname.namespaceSet).toBe(1);
149
+ it('reads instances and classes as parallel lists sharing one count', () => {
150
+ const file = readAbcFile(new Uint8Array(buildAbc()))!;
151
+ expect(file.instances.length).toEqual(1);
152
+ expect(file.classes.length).toEqual(1);
153
+ expect(file.instances[0].initializer).toEqual(1);
154
+ expect(file.instances[0].name).toEqual(1);
155
+ expect(file.instances[0].superName).toEqual(1);
156
+ expect(file.instances[0].traits[0].kind).toEqual(AbcTraitKind.Method & 0xff);
157
+ expect(file.instances[0].traits[0].methodIndex).toEqual(0);
158
+ expect(file.classes[0].initializer).toEqual(0);
35
159
  });
36
-
37
- it('reads methods with their optional defaults and parameter names', () => {
38
- const file = readAbcFile(buildAbc());
39
-
40
- expect(file?.methods).toHaveLength(2);
41
- expect(file?.methods[0]).toMatchObject({ name: 2, parameterTypes: [], returnType: 0 });
42
- expect(file?.methods[1].optionalValues).toEqual([{ kind: 0x03, value: 1 }]);
43
- expect(file?.methods[1].parameterNames).toEqual([1]);
160
+ it('reads methods with optional defaults and parameter names', () => {
161
+ const file = readAbcFile(new Uint8Array(buildAbc()))!;
162
+ expect(file.methods.length).toEqual(2);
163
+ expect(file.methods[0].name).toEqual(2);
164
+ expect(file.methods[0].parameterTypes).toEqual([] as never[]);
165
+ expect(file.methods[0].returnType).toEqual(0);
166
+ expect(file.methods[1].optionalValues).toEqual([{ kind: 0x03, value: 1 }]);
167
+ expect(file.methods[1].parameterNames).toEqual([1]);
44
168
  });
45
-
46
- it('reads instances and classes as parallel lists sharing one count', () => {
47
- const file = readAbcFile(buildAbc());
48
-
49
- expect(file?.instances).toHaveLength(1);
50
- expect(file?.classes).toHaveLength(1);
51
- expect(file?.instances[0]).toMatchObject({ initializer: 1, name: 1, superName: 1 });
52
- expect(file?.instances[0].traits[0]).toMatchObject({ kind: AbcTraitKind.Method, methodIndex: 0 });
53
- expect(file?.classes[0].initializer).toBe(0);
169
+ it('reads namespaces namespace sets and both multiname shapes', () => {
170
+ const file = readAbcFile(new Uint8Array(buildAbc()))!;
171
+ expect(file.constantPool.namespaces[1]).toEqual({ kind: 0x16, name: 3 });
172
+ expect(file.constantPool.namespaceSets[1]).toEqual([1]);
173
+ const qualified = file.constantPool.multinames[1];
174
+ expect(qualified.kind).toEqual(AbcMultinameKind.QName & 0xff);
175
+ expect(qualified.namespace).toEqual(1);
176
+ expect(file.constantPool.strings[qualified.name]).toEqual('Main');
177
+ const multiname = file.constantPool.multinames[2];
178
+ expect(multiname.kind).toEqual(AbcMultinameKind.Multiname & 0xff);
179
+ expect(file.constantPool.strings[multiname.name]).toEqual('stop');
180
+ expect(multiname.namespaceSet).toEqual(1);
54
181
  });
55
-
56
- it('reads script and method-body tables, keeping instruction bytes unparsed', () => {
57
- const file = readAbcFile(buildAbc());
58
-
59
- expect(file?.scripts[0]).toMatchObject({ initializer: 0 });
60
- expect(file?.methodBodies).toHaveLength(1);
61
- const body = file!.methodBodies[0];
62
- expect(body).toMatchObject({ localCount: 1, maxStack: 2, method: 0 });
63
- // The instruction stream is handed back whole: decoding it needs an opcode table and is a later step.
64
- expect([...body.code]).toEqual([0xd0, 0x30, 0x47]);
65
- expect(body.exceptions).toEqual([]);
182
+ it('reads script and method body tables keeping instruction bytes unparsed', () => {
183
+ const file = readAbcFile(new Uint8Array(buildAbc()))!;
184
+ expect(file.scripts[0].initializer).toEqual(0);
185
+ expect(file.methodBodies.length).toEqual(1);
186
+ const body = file.methodBodies[0];
187
+ expect(body.localCount).toEqual(1);
188
+ expect(body.maxStack).toEqual(2);
189
+ expect(body.method).toEqual(0);
190
+ expect(Array.from(body.code)).toEqual([0xd0, 0x30, 0x47]);
191
+ expect(body.exceptions.length === 0).toBe(true);
66
192
  });
67
-
68
- it('returns null rather than throwing for input that is not a readable container', () => {
69
- expect(readAbcFile(new Uint8Array())).toBeNull();
70
- expect(readAbcFile(new Uint8Array([0x10, 0x00, 0x2e, 0x00]))).toBeNull();
71
- // Truncated part-way through the constant pool.
72
- expect(readAbcFile(buildAbc().subarray(0, 12))).toBeNull();
193
+ it('reads version and whole constant pool with reserved zero entry', () => {
194
+ const file = readAbcFile(new Uint8Array(buildAbc()))!;
195
+ expect(file.minorVersion).toEqual(16);
196
+ expect(file.majorVersion).toEqual(46);
197
+ expect(file.constantPool.strings[0]).toEqual('');
198
+ expect(Array.from(file.constantPool.strings.slice(1))).toEqual(['Main', 'stop', 'flash.display']);
199
+ expect(file.constantPool.integers).toEqual([0, -7]);
200
+ expect(file.constantPool.unsignedIntegers).toEqual([0, 9]);
201
+ expect(Math.abs(file.constantPool.doubles[1] - 1.5) < Number.EPSILON).toBe(true);
73
202
  });
74
-
75
- it('never throws on arbitrary bytes, whatever they happen to encode', () => {
76
- // The reader's whole error contract is a null sentinel, and a container this size is mostly counts
77
- // and indices, so the property worth asserting is that no byte sequence produces an exception.
78
- let seed = 0x2f6a51b3;
79
- const next = (): number => {
80
- seed = (seed * 1664525 + 1013904223) >>> 0;
81
- return seed;
82
- };
83
-
84
- for (let i = 0; i < 500; i++) {
85
- const bytes = new Uint8Array(next() % 200);
86
- for (let b = 0; b < bytes.length; b++) bytes[b] = next() & 0xff;
87
- expect(() => readAbcFile(bytes)).not.toThrow();
88
- }
89
- // And the same for mutations of a file that is otherwise well formed.
90
- for (let i = 0; i < 500; i++) {
91
- const mutant = buildAbc();
92
- for (let f = 0; f < 3; f++) mutant[next() % mutant.length] = next() & 0xff;
93
- expect(() => readAbcFile(mutant)).not.toThrow();
94
- }
203
+ it('rejects a multiname with an undefined kind', () => {
204
+ let bytes = buildAbc();
205
+ const index =
206
+ ((i) => (i === -1 ? undefined : i))(
207
+ bytes.slice(20).findIndex((value) => value === (AbcMultinameKind.QName & 0xff)),
208
+ )! + 20;
209
+ bytes[index] = 0x7f;
210
+ expect(readAbcFile(new Uint8Array(bytes)) === null).toBe(true);
95
211
  });
96
-
97
- it('rejects a multiname whose kind the format does not define', () => {
212
+ it('returns none instead of panicking for an unreadable container', () => {
213
+ expect(readAbcFile(new Uint8Array([])) === null).toBe(true);
214
+ expect(readAbcFile(new Uint8Array([0x10, 0x00, 0x2e, 0x00])) === null).toBe(true);
98
215
  const bytes = buildAbc();
99
- // Overwrite the first multiname's kind byte with one no version of the format uses.
100
- const index = bytes.indexOf(AbcMultinameKind.QName, 20);
101
- bytes[index] = 0x7f;
102
-
103
- expect(readAbcFile(bytes)).toBeNull();
216
+ expect(readAbcFile(new Uint8Array(bytes.slice(0, 12))) === null).toBe(true);
104
217
  });
105
218
  });
106
-
107
- // Builds a small but structurally complete ABC file: three strings, one namespace and set, two multinames,
108
- // two methods (one with an optional default and a parameter name), one class and instance with a method
109
- // trait, one script, and one method body whose code is `getlocal_0; pushscope; returnvoid`.
110
- function buildAbc(): Uint8Array {
111
- const bytes: number[] = [];
112
- const u8 = (value: number): void => void bytes.push(value & 0xff);
113
- const u16 = (value: number): void => {
114
- u8(value);
115
- u8(value >> 8);
116
- };
117
- const u30 = (value: number): void => {
118
- let remaining = value;
119
- do {
120
- const byte = remaining % 0x80;
121
- remaining = Math.floor(remaining / 0x80);
122
- u8(remaining > 0 ? byte | 0x80 : byte);
123
- } while (remaining > 0);
124
- };
125
- const str = (value: string): void => {
126
- const encoded = new TextEncoder().encode(value);
127
- u30(encoded.length);
128
- for (const byte of encoded) u8(byte);
129
- };
130
- const double = (value: number): void => {
131
- const buffer = new ArrayBuffer(8);
132
- new DataView(buffer).setFloat64(0, value, true);
133
- for (const byte of new Uint8Array(buffer)) u8(byte);
134
- };
135
-
136
- u16(16); // minor version
137
- u16(46); // major version
138
-
139
- u30(2); // int pool: one entry after the reserved slot
140
- // A signed constant is written as its full 32-bit two's complement in the same seven-bits-per-byte
141
- // form, so -7 occupies five bytes rather than one.
142
- u30(0xfffffff9);
143
- u30(2); // uint pool
144
- u30(9);
145
- u30(2); // double pool
146
- double(1.5);
147
- u30(4); // string pool
148
- str('Main');
149
- str('stop');
150
- str('flash.display');
151
- u30(2); // namespace pool
152
- u8(0x16);
153
- u30(3);
154
- u30(2); // namespace set pool
155
- u30(1);
156
- u30(1);
157
- u30(3); // multiname pool
158
- u8(AbcMultinameKind.QName);
159
- u30(1);
160
- u30(1);
161
- u8(AbcMultinameKind.Multiname);
162
- u30(2);
163
- u30(1);
164
-
165
- u30(2); // method count
166
- u30(0); // no parameters
167
- u30(0); // return type
168
- u30(2); // name -> 'stop'
169
- u8(0); // flags
170
- u30(1); // one parameter
171
- u30(0);
172
- u30(1);
173
- u30(1);
174
- u8(0x08 | 0x80); // has optional defaults, has parameter names
175
- u30(1);
176
- u30(1); // optional value index
177
- u8(0x03); // optional value kind
178
- u30(1); // parameter name
179
-
180
- u30(0); // metadata count
181
-
182
- u30(1); // class count
183
- u30(1); // instance name
184
- u30(1); // super name
185
- u8(0); // flags
186
- u30(0); // interface count
187
- u30(1); // instance initializer
188
- u30(1); // trait count
189
- u30(2); // trait name
190
- u8(AbcTraitKind.Method);
191
- u30(0); // dispatch id
192
- u30(0); // method index
193
- u30(0); // class initializer
194
- u30(0); // class trait count
195
-
196
- u30(1); // script count
197
- u30(0); // script initializer
198
- u30(0); // script trait count
199
-
200
- u30(1); // method body count
201
- u30(0); // method
202
- u30(2); // max stack
203
- u30(1); // local count
204
- u30(1); // init scope depth
205
- u30(2); // max scope depth
206
- u30(3); // code length
207
- u8(0xd0);
208
- u8(0x30);
209
- u8(0x47);
210
- u30(0); // exception count
211
- u30(0); // trait count
212
-
213
- return new Uint8Array(bytes);
214
- }