object-input-stream 0.1.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/src/ois-ast.ts ADDED
@@ -0,0 +1,147 @@
1
+ import ObjectInputStream, { type OisOptions } from ".";
2
+ import type * as ast from './ast';
3
+ import * as exc from "./exceptions";
4
+
5
+
6
+ export class ObjectInputStreamAST extends ObjectInputStream {
7
+ protected ast: ast.Ast
8
+ protected nodeStack: ast.Node[];
9
+
10
+ constructor(data: Uint8Array, options?: OisOptions) {
11
+ super(data, options);
12
+
13
+ this.ast = {data, root: {
14
+ type: "root",
15
+ span: {start: 0, end: this.data.length},
16
+ children: [
17
+ {type: "magic", value: this.STREAM_MAGIC, span: {start: 0, end: 2}, children: null},
18
+ {type: "version", value: this.STREAM_VERSION, span: {start: 2, end: 4}, children: null},
19
+ {type: "contents", span: {start: 4, end: this.data.length}, children:[]},
20
+ ]
21
+ }}
22
+ this.nodeStack = [this.ast.root, this.ast.root.children[2]];
23
+ }
24
+
25
+ protected get curNode() {
26
+ return this.nodeStack[this.nodeStack.length-1];
27
+ }
28
+
29
+ getAST(): ast.Ast {
30
+ return this.ast;
31
+ }
32
+
33
+ @primitiveAST("boolean")
34
+ readBoolean(): boolean { return super.readBoolean(); }
35
+ @primitiveAST("byte")
36
+ readByte(): number { return super.readByte(); }
37
+ @primitiveAST("unsigned-byte")
38
+ readUnsignedByte(): number { return super.readUnsignedByte(); }
39
+ @primitiveAST("char")
40
+ readChar(): string { return super.readChar(); }
41
+ @primitiveAST("short")
42
+ readShort(): number { return super.readShort(); }
43
+ @primitiveAST("unsigned-short")
44
+ readUnsignedShort(): number { return super.readUnsignedShort(); }
45
+ @primitiveAST("int")
46
+ readInt(): number { return super.readInt(); }
47
+ @primitiveAST("long")
48
+ readLong(): bigint { return super.readLong(); }
49
+ @primitiveAST("float")
50
+ readFloat(): number { return super.readFloat(); }
51
+ @primitiveAST("double")
52
+ readDouble(): number { return super.readDouble(); }
53
+
54
+ @nodeAST("tc")
55
+ protected readTC(): number { return super.readTC(); }
56
+ }
57
+
58
+ function nodeAST<T>(type: string) {return (fun: () => T) => {
59
+ return function decorated(this: ObjectInputStreamAST) {
60
+ if (!(this instanceof ObjectInputStreamAST))
61
+ throw new exc.InternalError();
62
+
63
+ const start = this.offset;
64
+ const result = fun.apply(this);
65
+ const end = this.offset;
66
+
67
+ if (this.nodeStack === undefined)
68
+ return result;
69
+
70
+ const span = {start, end};
71
+
72
+ const node: ast.Node = {
73
+ type: type as any,
74
+ span: span,
75
+ children: null,
76
+ }
77
+
78
+ if (this.curNode.children === null)
79
+ throw new exc.InternalError();
80
+
81
+ // @ts-expect-error
82
+ this.curNode.children.push(node);
83
+
84
+ return result;
85
+ }
86
+ }}
87
+
88
+ function primitiveAST<T extends number | string | boolean | bigint>(dataType: string) {return (fun: () => T) => {
89
+ return function decorated(this: ObjectInputStreamAST) {
90
+ if (!(this instanceof ObjectInputStreamAST))
91
+ throw new exc.InternalError();
92
+
93
+ const start = this.offset;
94
+ const result = fun.apply(this);
95
+ const end = this.offset;
96
+
97
+ if (this.nodeStack === undefined)
98
+ return result;
99
+
100
+ const span = {start, end};
101
+
102
+ const node: ast.PrimitiveNode = {
103
+ type: "primitive",
104
+ // @ts-expect-error
105
+ dataType: dataType,
106
+ value: result,
107
+ span: span,
108
+ children: null,
109
+ }
110
+
111
+ if (this.curNode.type === "blockdata") {
112
+ const sequence = this.nodeStack[this.nodeStack.length-2];
113
+ if (sequence.type !== "blockdata-sequence")
114
+ throw new exc.InternalError();
115
+ sequence.values.push(node);
116
+ } else {
117
+ if (this.curNode.children === null)
118
+ throw new exc.InternalError();
119
+ // @ts-expect-error
120
+ this.curNode.children.push(node);
121
+ }
122
+
123
+ return result;
124
+ }
125
+ }}
126
+
127
+
128
+ const ois_ast = new ObjectInputStreamAST(new Uint8Array([
129
+ 172, 237, 0, 5, 119, 30, 69, 39, 20,
130
+ 78, 206, 109, 85, 58, 3, 21, 127, 242,
131
+ 227, 49, 35, 0, 0, 113, 52, 105, 0,
132
+ 0, 0, 0, 0, 0, 0, 207, 199, 1
133
+ ]));
134
+
135
+ ois_ast.readByte()
136
+ ois_ast.readChar()
137
+ ois_ast.readDouble()
138
+ ois_ast.readFloat()
139
+ ois_ast.readInt()
140
+ ois_ast.readLong()
141
+ ois_ast.readShort()
142
+ ois_ast.readBoolean()
143
+
144
+
145
+ const myAst: any = ois_ast.getAST();
146
+ myAst.data = [...myAst.data];
147
+ console.log(JSON.stringify(myAst, (key, value) => typeof value === "bigint" ? ''+value : value, 2));