()
| 1135 | } |
| 1136 | |
| 1137 | readEmbed() { |
| 1138 | // Hoist the serializer lookup into a scope-level const, evaluated once during |
| 1139 | // factory init. Self-recursive structs may still point at a placeholder, so |
| 1140 | // only the fully generated serializer path can hoist derived values below. |
| 1141 | const hoisted = this.scope.declare("ser", this.serializerExpr); |
| 1142 | const scope = this.scope; |
| 1143 | const builder = this.builder; |
| 1144 | const internalTypeId = this.getInternalTypeId(); |
| 1145 | const serializer = builder.resolver.getSerializerByTypeInfo(this.typeInfo); |
| 1146 | const canInlineCompatibleTypeInfo = internalTypeId === TypeId.COMPATIBLE_STRUCT |
| 1147 | || internalTypeId === TypeId.NAMED_COMPATIBLE_STRUCT |
| 1148 | || (internalTypeId === TypeId.NAMED_STRUCT && builder.resolver.isCompatible()); |
| 1149 | const canUseHeaderCacheFastPath = canInlineCompatibleTypeInfo && serializer?._initialized; |
| 1150 | const inlineCompatibleTypeInfo = ( |
| 1151 | onMetaChanged: (changedSerializer: string) => string, |
| 1152 | onMetaUnchanged: () => string, |
| 1153 | ) => { |
| 1154 | if (!canUseHeaderCacheFastPath) { |
| 1155 | const changedSerializer = scope.uniqueName("changedSerializer"); |
| 1156 | return ` |
| 1157 | const ${changedSerializer} = ${hoisted}.readTypeInfo(); |
| 1158 | if (${changedSerializer} !== undefined) { |
| 1159 | ${onMetaChanged(changedSerializer)} |
| 1160 | } else { |
| 1161 | ${onMetaUnchanged()} |
| 1162 | } |
| 1163 | `; |
| 1164 | } |
| 1165 | const changedSerializer = scope.uniqueName("changedSerializer"); |
| 1166 | const hoistedHash = scope.declare("serHash", `${hoisted}.getHash()`); |
| 1167 | return ` |
| 1168 | ${builder.reader.readUint8()}; |
| 1169 | const ${changedSerializer} = ${builder.typeMetaResolver.readCompatibleStructSerializer(hoistedHash, hoisted)}; |
| 1170 | if (${changedSerializer} !== undefined) { |
| 1171 | ${onMetaChanged(changedSerializer)} |
| 1172 | } else { |
| 1173 | ${onMetaUnchanged()} |
| 1174 | } |
| 1175 | `; |
| 1176 | }; |
| 1177 | return new Proxy({}, { |
| 1178 | get: (target, prop: string) => { |
| 1179 | if (prop === "readNoRef") { |
| 1180 | return (accessor: (expr: string) => string, refState: string) => { |
| 1181 | const result = scope.uniqueName("result"); |
| 1182 | return ` |
| 1183 | ${inlineCompatibleTypeInfo( |
| 1184 | changedSerializer => `${accessor(`${changedSerializer}.read(${refState})`)};`, |
| 1185 | () => ` |
| 1186 | ${builder.getReadContextName()}.incReadDepth(); |
| 1187 | let ${result} = ${hoisted}.read(${refState}); |
| 1188 | ${builder.getReadContextName()}.decReadDepth(); |
| 1189 | ${accessor(result)}; |
| 1190 | `, |
| 1191 | )} |
| 1192 | `; |
| 1193 | }; |
| 1194 | } |
nothing calls this directly
no test coverage detected