This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.ts
60 lines (54 loc) · 1.67 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import debugModule from "debug";
const debug = debugModule("codec:memory:allocate");
import type {
MemoryAllocations,
MemoryAllocation,
MemoryMemberAllocation
} from "./types";
import * as Evm from "@truffle/codec/evm";
import type * as Format from "@truffle/codec/format";
export { MemoryAllocations, MemoryAllocation, MemoryMemberAllocation };
export function getMemoryAllocations(
userDefinedTypes: Format.Types.TypesById
): MemoryAllocations {
let allocations: MemoryAllocations = {};
for (const dataType of Object.values(userDefinedTypes)) {
if (dataType.typeClass === "struct") {
allocations[dataType.id] = allocateStruct(dataType);
}
}
return allocations;
}
export function isSkippedInMemoryStructs(dataType: Format.Types.Type): boolean {
if (dataType.typeClass === "mapping") {
return true;
} else if (dataType.typeClass === "array") {
return isSkippedInMemoryStructs(dataType.baseType);
} else {
return false;
}
}
//unlike in storage and calldata, we'll just return the one allocation, nothing fancy
//that's because allocating one struct can never necessitate allocating another
function allocateStruct(dataType: Format.Types.StructType): MemoryAllocation {
let memberAllocations: MemoryMemberAllocation[] = [];
let position = 0;
for (const { name, type: memberType } of dataType.memberTypes) {
const length = isSkippedInMemoryStructs(memberType)
? 0
: Evm.Utils.WORD_SIZE;
memberAllocations.push({
name,
type: memberType,
pointer: {
location: "memory",
start: position,
length
}
});
position += length;
}
return {
members: memberAllocations
};
}