MCPcopy
hub / github.com/yume-chan/ya-webadb / struct

Function struct

libraries/struct/src/struct.ts:82–248  ·  view source on GitHub ↗
(
    fields: Fields,
    options: {
        littleEndian: boolean;
        extra?: (Extra & ThisType<FieldsValue<Fields>>) | undefined;
        postDeserialize?:
            | ((
                  this: FieldsValue<Fields> & ExtraToIntersection<Extra>,
                  value: FieldsValue<Fields> & ExtraToIntersection<Extra>,
              ) => PostDeserialize)
            | undefined;
    },
)

Source from the content-addressed store, hash-verified

80
81/* #__NO_SIDE_EFFECTS__ */
82export function struct<
83 Fields extends Record<
84 string,
85 | Field<unknown, string, Partial<FieldsValue<Fields>>, unknown>
86 | StructLike<unknown>
87 >,
88 Extra extends Record<PropertyKey, unknown> | undefined = undefined,
89 PostDeserialize = FieldsValue<Fields> & ExtraToIntersection<Extra>,
90>(
91 fields: Fields,
92 options: {
93 littleEndian: boolean;
94 extra?: (Extra & ThisType<FieldsValue<Fields>>) | undefined;
95 postDeserialize?:
96 | ((
97 this: FieldsValue<Fields> & ExtraToIntersection<Extra>,
98 value: FieldsValue<Fields> & ExtraToIntersection<Extra>,
99 ) => PostDeserialize)
100 | undefined;
101 },
102): Struct<Fields, Extra, PostDeserialize> {
103 const fieldList = Object.entries(fields);
104
105 let size = 0;
106 let byob = true;
107 for (const [, field] of fieldList) {
108 size += field.size;
109 if (byob && field.type !== "byob") {
110 byob = false;
111 }
112 }
113
114 const littleEndian = options.littleEndian;
115 const extra = options.extra
116 ? Object.getOwnPropertyDescriptors(options.extra)
117 : undefined;
118
119 return {
120 littleEndian,
121 fields,
122 extra: options.extra,
123
124 type: byob ? "byob" : "default",
125 size,
126 serialize(
127 source: FieldsInit<Fields>,
128 bufferOrContext?: Uint8Array | StructSerializeContext,
129 ): Uint8Array | number {
130 const temp: Record<string, unknown> = { ...source };
131
132 for (const [key, field] of fieldList) {
133 if (key in temp && "init" in field) {
134 const result = field.init?.(temp[key], temp as never);
135 temp[key] = result;
136 }
137 }
138
139 const sizes = new Array<number>(fieldList.length);

Callers 15

logcat.tsFile · 0.90
start-app.tsFile · 0.90
inject-key-code.tsFile · 0.90
inject-text.tsFile · 0.90
empty.tsFile · 0.90
uhid.tsFile · 0.90
inject-touch.tsFile · 0.90

Calls 2

bipedalFunction · 0.85
deserializeMethod · 0.65

Tested by

no test coverage detected