MCPcopy Create free account
hub / github.com/d1ll0n/stack-packer / getSetField

Function getSetField

src/code-gen/fields.ts:241–273  ·  view source on GitHub ↗
(
  struct: AbiStruct,
  field: AbiStructField,
  offset: number,
  context: FileContext,
  originalName: string,
  maxValueReference: string
)

Source from the content-addressed store, hash-verified

239}
240
241export const getSetField = (
242 struct: AbiStruct,
243 field: AbiStructField,
244 offset: number,
245 context: FileContext,
246 originalName: string,
247 maxValueReference: string
248) => {
249 if (field.type.meta !== "elementary" && field.type.meta !== "enum") throw Error("Unsupported field!");
250 const constantsPrefix = `${struct.name}_${originalName}`;
251 const size = field.type.size;
252 const bitsAfter = 256 - (offset + size);
253
254 const omitMaskName = `${constantsPrefix}_maskOut`;
255 const omitMask = getOmissionMask(offset, size)
256 const maskReference = context.addConstant(omitMaskName, omitMask);
257 const oldValueRemoved = `and(old, ${maskReference})`;
258
259 let fieldReference = intAllowed(field.type) ? `and(${field.name}, ${maxValueReference})` : field.name
260
261 let positioned = field.name;
262 if (bitsAfter > 0) {
263 const bitsAfterName = `${constantsPrefix}_bitsAfter`;
264 const bitsAfterReference = context.addConstant(bitsAfterName, toHex(bitsAfter));
265 positioned = `shl(${bitsAfterReference}, ${fieldReference})`;
266 }
267
268 return {
269 update: `or(${oldValueRemoved}, ${positioned})`,
270 position: positioned,
271 omitMaskReference: maskReference
272 };
273};
274
275export const getReadField = (
276 struct: AbiStruct,

Callers 1

processFieldsFunction · 0.85

Calls 4

getOmissionMaskFunction · 0.90
toHexFunction · 0.90
intAllowedFunction · 0.85
addConstantMethod · 0.80

Tested by

no test coverage detected