@latticexyz/store 3.0.0-main-fb1cfef0c → 3.0.0-main-69eb63b59
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/dist/chunk-3PAPLPW2.js +2 -0
- package/dist/chunk-3PAPLPW2.js.map +1 -0
- package/dist/codegen.d.ts +27 -13
- package/dist/codegen.js +177 -177
- package/dist/codegen.js.map +1 -1
- package/dist/config/v2.d.ts +3 -3
- package/dist/config/v2.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/{input-48d1368b.d.ts → input-e69ffb2f.d.ts} +9 -3
- package/dist/mud.config.d.ts +4 -4
- package/dist/mud.config.js +1 -1
- package/dist/mud.config.js.map +1 -1
- package/dist/{namespacedTables-8a81e0a2.d.ts → namespacedTables-264f21b1.d.ts} +2 -2
- package/dist/{store-6077eba5.d.ts → store-3b37a8c0.d.ts} +2 -2
- package/out/Callbacks.sol/Callbacks.json +1 -1
- package/out/Callbacks.t.sol/CallbacksTest.json +1 -1
- package/out/Gas.t.sol/GasTest.json +1 -1
- package/out/Gas.t.sol/SolidityStorage.json +1 -1
- package/out/Gas.t.sol/SomeContract.json +1 -1
- package/out/GasStorageLoad.t.sol/GasStorageLoadTest.json +1 -1
- package/out/Hook.sol/HookInstance.json +1 -1
- package/out/Hook.sol/HookLib.json +1 -1
- package/out/Hook.t.sol/HookTest.json +1 -1
- package/out/Hooks.sol/Hooks.json +1 -1
- package/out/KeyEncoding.sol/KeyEncoding.json +1 -1
- package/out/KeyEncoding.t.sol/KeyEncodingTest.json +1 -1
- package/out/MirrorSubscriber.sol/MirrorSubscriber.json +1 -1
- package/out/Mixed.sol/Mixed.json +1 -1
- package/out/Mixed.t.sol/MixedTest.json +1 -1
- package/out/ResourceIds.sol/ResourceIds.json +1 -1
- package/out/Store.sol/Store.json +1 -1
- package/out/StoreCore.sol/StoreCore.json +1 -1
- package/out/StoreCore.sol/StoreCoreInternal.json +1 -1
- package/out/StoreCore.t.sol/StoreCoreTest.json +1 -1
- package/out/StoreCoreDynamic.t.sol/StoreCoreDynamicTest.json +1 -1
- package/out/StoreCoreGas.t.sol/StoreCoreGasTest.json +1 -1
- package/out/StoreHook.t.sol/StoreHookTest.json +1 -1
- package/out/StoreHooks.sol/StoreHooks.json +1 -1
- package/out/StoreHooks.t.sol/StoreHooksTest.json +1 -1
- package/out/StoreHooksColdLoad.t.sol/StoreHooksColdLoadTest.json +1 -1
- package/out/StoreKernel.sol/StoreKernel.json +1 -1
- package/out/StoreMock.sol/StoreMock.json +1 -1
- package/out/StoreMock.t.sol/StoreMockTest.json +1 -1
- package/out/StoreRead.sol/StoreRead.json +1 -1
- package/out/StoreSwitch.sol/StoreSwitch.json +1 -1
- package/out/StoreSwitch.t.sol/MockSystem.json +1 -1
- package/out/StoreSwitch.t.sol/StoreSwitchTest.json +1 -1
- package/out/StoreSwitch.t.sol/StoreSwitchTestStore.json +1 -1
- package/out/Tables.sol/Tables.json +1 -1
- package/out/Vector2.sol/Vector2.json +1 -1
- package/out/Vector2.t.sol/Vector2Test.json +1 -1
- package/out/build-info/{ab717ad6a3f348702f22d62ec82363af.json → 946dc2faf2e29137d1fd87e41e62942e.json} +1 -1
- package/package.json +7 -7
- package/src/codegen/tables/Hooks.sol +1 -1
- package/src/codegen/tables/ResourceIds.sol +1 -1
- package/src/codegen/tables/StoreHooks.sol +1 -1
- package/src/codegen/tables/Tables.sol +3 -3
- package/dist/chunk-JX6B2S4J.js +0 -2
- package/dist/chunk-JX6B2S4J.js.map +0 -1
package/dist/codegen.js
CHANGED
@@ -1,262 +1,262 @@
|
|
1
|
-
import{renderArguments as S,renderCommonData as
|
1
|
+
import{renderArguments as S,renderCommonData as se,renderWithFieldSuffix as _,renderWithStore as D}from"@latticexyz/common/codegen";function K(e){let t=e.storeArgument,{_typedTableId:n,_typedKeyArgs:r,_keyTupleDefinition:s}=se(e),a="";for(let[i,d]of e.fields.entries()){if(!e.withDynamicFieldMethods&&d.isDynamic)continue;let y=`${d.typeWithLocation} ${d.name}`;if(e.withGetters&&(a+=_(e.withSuffixlessFieldMethods,d.name,o=>D(t,({_typedStore:c,_store:p,_commentSuffix:l,_methodNamePrefix:h})=>`
|
2
2
|
/**
|
3
|
-
* @notice Get ${
|
3
|
+
* @notice Get ${d.name}${l}.
|
4
4
|
*/
|
5
|
-
function ${h}get${
|
6
|
-
${
|
7
|
-
${
|
5
|
+
function ${h}get${o}(${S([c,n,r])}) internal view returns (${y}) {
|
6
|
+
${s}
|
7
|
+
${d.isDynamic?`bytes memory _blob = ${p}.getDynamicField(
|
8
8
|
_tableId,
|
9
9
|
_keyTuple,
|
10
|
-
${
|
11
|
-
);`:`bytes32 _blob = ${
|
10
|
+
${i-e.staticFields.length}
|
11
|
+
);`:`bytes32 _blob = ${p}.getStaticField(
|
12
12
|
_tableId,
|
13
13
|
_keyTuple,
|
14
|
-
${
|
14
|
+
${i},
|
15
15
|
_fieldLayout
|
16
16
|
);`}
|
17
|
-
return ${
|
17
|
+
return ${N(d)};
|
18
18
|
}
|
19
|
-
`))),a+=
|
19
|
+
`))),a+=_(e.withSuffixlessFieldMethods,d.name,o=>D(t,({_typedStore:c,_store:p,_commentSuffix:l,_methodNamePrefix:h})=>{let f=S([c,n,r,y]),m=d.isDynamic?"setDynamicField":"setStaticField",u=E(d),L=d.isDynamic?`_tableId, _keyTuple, ${i-e.staticFields.length}, ${u}`:`_tableId, _keyTuple, ${i}, ${u}, _fieldLayout`;return`
|
20
20
|
/**
|
21
|
-
* @notice Set ${
|
21
|
+
* @notice Set ${d.name}${l}.
|
22
22
|
*/
|
23
|
-
function ${h}set${
|
24
|
-
${
|
25
|
-
${
|
23
|
+
function ${h}set${o}(${f}) internal {
|
24
|
+
${s}
|
25
|
+
${p}.${m}(${L});
|
26
26
|
}
|
27
|
-
`})),
|
28
|
-
// The length of ${
|
29
|
-
uint256 constant length${
|
30
|
-
`):a+=
|
27
|
+
`})),d.isDynamic){let o=ce(d),c=i-e.staticFields.length,{typeWrappingData:p}=d;e.withGetters&&(p&&p.kind==="staticArray"?a+=_(e.withSuffixlessFieldMethods,d.name,l=>`
|
28
|
+
// The length of ${d.name}
|
29
|
+
uint256 constant length${l} = ${p.staticLength};
|
30
|
+
`):a+=_(e.withSuffixlessFieldMethods,d.name,l=>D(t,({_typedStore:h,_store:f,_commentSuffix:m,_methodNamePrefix:u})=>`
|
31
31
|
/**
|
32
|
-
* @notice Get the length of ${
|
32
|
+
* @notice Get the length of ${d.name}${m}.
|
33
33
|
*/
|
34
|
-
function ${
|
35
|
-
${
|
36
|
-
uint256 _byteLength = ${f}.getDynamicFieldLength(_tableId, _keyTuple, ${
|
34
|
+
function ${u}length${l}(${S([h,n,r])}) internal view returns (uint256) {
|
35
|
+
${s}
|
36
|
+
uint256 _byteLength = ${f}.getDynamicFieldLength(_tableId, _keyTuple, ${c});
|
37
37
|
unchecked {
|
38
|
-
return _byteLength / ${
|
38
|
+
return _byteLength / ${o.elementLength};
|
39
39
|
}
|
40
40
|
}
|
41
|
-
`)),a+=
|
41
|
+
`)),a+=_(e.withSuffixlessFieldMethods,d.name,l=>D(t,({_typedStore:h,_store:f,_commentSuffix:m,_methodNamePrefix:u})=>`
|
42
42
|
/**
|
43
|
-
* @notice Get an item of ${
|
43
|
+
* @notice Get an item of ${d.name}${m}.
|
44
44
|
* @dev Reverts with Store_IndexOutOfBounds if \`_index\` is out of bounds for the array.
|
45
45
|
*/
|
46
|
-
function ${
|
47
|
-
${
|
46
|
+
function ${u}getItem${l}(${S([h,n,r,"uint256 _index"])}) internal view returns (${o.typeWithLocation}) {
|
47
|
+
${s}
|
48
48
|
|
49
|
-
${
|
50
|
-
uint256 _byteLength = ${f}.getDynamicFieldLength(_tableId, _keyTuple, ${
|
51
|
-
uint256 dynamicLength = _byteLength / ${
|
52
|
-
uint256 staticLength = ${
|
49
|
+
${p&&p.kind==="staticArray"&&d.arrayElement?`
|
50
|
+
uint256 _byteLength = ${f}.getDynamicFieldLength(_tableId, _keyTuple, ${c});
|
51
|
+
uint256 dynamicLength = _byteLength / ${o.elementLength};
|
52
|
+
uint256 staticLength = ${p.staticLength};
|
53
53
|
|
54
54
|
if (_index < staticLength && _index >= dynamicLength) {
|
55
|
-
return ${
|
55
|
+
return ${W(d.arrayElement,`bytes${d.arrayElement.staticByteLength}(new bytes(0))`)};
|
56
56
|
}`:""}
|
57
57
|
|
58
58
|
unchecked {
|
59
59
|
bytes memory _blob = ${f}.getDynamicFieldSlice(
|
60
60
|
_tableId,
|
61
61
|
_keyTuple,
|
62
|
-
${
|
63
|
-
_index * ${
|
64
|
-
(_index + 1) * ${
|
62
|
+
${c},
|
63
|
+
_index * ${o.elementLength},
|
64
|
+
(_index + 1) * ${o.elementLength}
|
65
65
|
);
|
66
|
-
return ${
|
66
|
+
return ${o.decoded};
|
67
67
|
}
|
68
68
|
}
|
69
|
-
`))),(!
|
69
|
+
`))),(!p||p.kind!=="staticArray")&&(a+=_(e.withSuffixlessFieldMethods,d.name,l=>D(t,({_typedStore:h,_store:f,_commentSuffix:m,_methodNamePrefix:u})=>`
|
70
70
|
/**
|
71
|
-
* @notice Push ${
|
71
|
+
* @notice Push ${o.title} to ${d.name}${m}.
|
72
72
|
*/
|
73
|
-
function ${
|
74
|
-
${
|
75
|
-
${f}.pushToDynamicField(_tableId, _keyTuple, ${
|
73
|
+
function ${u}push${l}(${S([h,n,r,`${o.typeWithLocation} ${o.name}`])}) internal {
|
74
|
+
${s}
|
75
|
+
${f}.pushToDynamicField(_tableId, _keyTuple, ${c}, ${o.encoded});
|
76
76
|
}
|
77
|
-
`)),a+=
|
77
|
+
`)),a+=_(e.withSuffixlessFieldMethods,d.name,l=>D(t,({_typedStore:h,_store:f,_commentSuffix:m,_methodNamePrefix:u})=>`
|
78
78
|
/**
|
79
|
-
* @notice Pop ${
|
79
|
+
* @notice Pop ${o.title} from ${d.name}${m}.
|
80
80
|
*/
|
81
|
-
function ${
|
82
|
-
${
|
83
|
-
${f}.popFromDynamicField(_tableId, _keyTuple, ${
|
81
|
+
function ${u}pop${l}(${S([h,n,r])}) internal {
|
82
|
+
${s}
|
83
|
+
${f}.popFromDynamicField(_tableId, _keyTuple, ${c}, ${o.elementLength});
|
84
84
|
}
|
85
|
-
`))),a+=
|
85
|
+
`))),a+=_(e.withSuffixlessFieldMethods,d.name,l=>D(t,({_typedStore:h,_store:f,_commentSuffix:m,_methodNamePrefix:u})=>{let L=S([h,n,r,"uint256 _index",`${o.typeWithLocation} ${o.name}`]),A=`
|
86
86
|
_tableId,
|
87
87
|
_keyTuple,
|
88
|
-
${
|
89
|
-
uint40(_index * ${
|
88
|
+
${c},
|
89
|
+
uint40(_index * ${o.elementLength}),
|
90
90
|
uint40(_encoded.length),
|
91
91
|
_encoded
|
92
92
|
`;return`
|
93
93
|
/**
|
94
|
-
* @notice Update ${
|
94
|
+
* @notice Update ${o.title} of ${d.name}${m} at \`_index\`.
|
95
95
|
*/
|
96
|
-
function ${
|
97
|
-
${
|
96
|
+
function ${u}update${l}(${L}) internal {
|
97
|
+
${s}
|
98
98
|
unchecked {
|
99
|
-
bytes memory _encoded = ${
|
100
|
-
${f}.spliceDynamicData(${
|
99
|
+
bytes memory _encoded = ${o.encoded};
|
100
|
+
${f}.spliceDynamicData(${A});
|
101
101
|
}
|
102
102
|
}
|
103
|
-
`}))}}return a}function
|
104
|
-
SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_${
|
105
|
-
)`:t?`${e.typeWrap}(${e.internalTypeId}(_blob))`:
|
103
|
+
`}))}}return a}function E(e){let t;return e.arrayElement?t="EncodeArray.encode":e.isDynamic?t="bytes":t="abi.encodePacked",`${t}(${e.typeUnwrap}(${e.name}))`}function q(e,t){let{staticByteLength:n}=e,r=`Bytes.getBytes${n}(_blob, ${t})`;return W(e,r)}function W(e,t){let{staticByteLength:n,internalTypeId:r}=e,s=n*8,a;if(r.match(/^uint\d{1,3}$/)||r==="address")a=`${r}(${t})`;else if(r.match(/^int\d{1,3}$/))a=`${r}(uint${s}(${t}))`;else if(r.match(/^bytes\d{1,2}$/))a=t;else if(r==="bool")a=`_toBool(uint8(${t}))`;else throw new Error(`Unknown value type id ${r}`);return`${e.typeWrap}(${a})`}function ce(e){if(e.arrayElement){let t="_element",n={...e.arrayElement,arrayElement:void 0,name:t};return{typeWithLocation:e.arrayElement.typeWithLocation,name:t,encoded:E(n),decoded:N(n),title:"an element",elementLength:e.arrayElement.staticByteLength}}else{let t="_slice",n={...e,name:t};return{typeWithLocation:`${e.typeId} memory`,name:t,encoded:E(n),decoded:N(n),title:"a slice",elementLength:1}}}function N(e){let{isDynamic:t,arrayElement:n}=e;return n?`${e.typeWrap}(
|
104
|
+
SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_${n.internalTypeId}()
|
105
|
+
)`:t?`${e.typeWrap}(${e.internalTypeId}(_blob))`:W(e,`bytes${e.staticByteLength}(_blob)`)}import{renderArguments as g,renderCommonData as V,renderList as Y,renderWithStore as k}from"@latticexyz/common/codegen";function Z(e){let{structName:t,storeArgument:n}=e,{_typedTableId:r,_typedKeyArgs:s,_keyTupleDefinition:a}=V(e),i="";return e.withGetters&&(i+=k(n,({_typedStore:d,_store:y,_commentSuffix:o,_methodNamePrefix:c})=>`
|
106
106
|
/**
|
107
|
-
* @notice Get the full data${
|
107
|
+
* @notice Get the full data${o}.
|
108
108
|
*/
|
109
|
-
function ${
|
109
|
+
function ${c}get(${g([d,r,s])}) internal view returns (${le(e)}) {
|
110
110
|
${a}
|
111
111
|
|
112
112
|
(
|
113
113
|
bytes memory _staticData,
|
114
114
|
EncodedLengths _encodedLengths,
|
115
115
|
bytes memory _dynamicData
|
116
|
-
) = ${
|
116
|
+
) = ${y}.getRecord(_tableId, _keyTuple, _fieldLayout);
|
117
117
|
return decode(_staticData, _encodedLengths, _dynamicData);
|
118
118
|
}
|
119
|
-
`)),
|
119
|
+
`)),i+=k(n,({_typedStore:d,_store:y,_commentSuffix:o,_methodNamePrefix:c,_useExplicitFieldLayout:p})=>{let l=g([d,r,s,g(e.fields.map(({name:f,typeWithLocation:m})=>`${m} ${f}`))]),h="_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData"+(p?", _fieldLayout":"");return`
|
120
120
|
/**
|
121
|
-
* @notice Set the full data using individual values${
|
121
|
+
* @notice Set the full data using individual values${o}.
|
122
122
|
*/
|
123
|
-
function ${
|
124
|
-
${
|
123
|
+
function ${c}set(${l}) internal {
|
124
|
+
${w(e)}
|
125
125
|
|
126
126
|
${a}
|
127
127
|
|
128
|
-
${
|
128
|
+
${y}.setRecord(${h});
|
129
129
|
}
|
130
|
-
`}),t!==void 0&&(
|
130
|
+
`}),t!==void 0&&(i+=k(n,({_typedStore:d,_store:y,_commentSuffix:o,_methodNamePrefix:c,_useExplicitFieldLayout:p})=>{let l=g([d,r,s,`${t} memory _table`]),h="_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData"+(p?", _fieldLayout":"");return`
|
131
131
|
/**
|
132
|
-
* @notice Set the full data using the data struct${
|
132
|
+
* @notice Set the full data using the data struct${o}.
|
133
133
|
*/
|
134
|
-
function ${
|
135
|
-
${
|
134
|
+
function ${c}set(${l}) internal {
|
135
|
+
${w(e,"_table.")}
|
136
136
|
|
137
137
|
${a}
|
138
138
|
|
139
|
-
${
|
139
|
+
${y}.setRecord(${h});
|
140
140
|
}
|
141
|
-
`})),
|
141
|
+
`})),i+=me(e),i}function w(e,t=""){let n="";return e.staticFields.length>0?n+=`
|
142
142
|
bytes memory _staticData = encodeStatic(
|
143
|
-
${
|
143
|
+
${g(e.staticFields.map(({name:r})=>`${t}${r}`))}
|
144
144
|
);
|
145
|
-
`:
|
145
|
+
`:n+="bytes memory _staticData;",e.dynamicFields.length>0?n+=`
|
146
146
|
EncodedLengths _encodedLengths = encodeLengths(
|
147
|
-
${
|
147
|
+
${g(e.dynamicFields.map(({name:r})=>`${t}${r}`))}
|
148
148
|
);
|
149
149
|
bytes memory _dynamicData = encodeDynamic(
|
150
|
-
${
|
150
|
+
${g(e.dynamicFields.map(({name:r})=>`${t}${r}`))}
|
151
151
|
);
|
152
|
-
`:
|
152
|
+
`:n+=`
|
153
153
|
EncodedLengths _encodedLengths;
|
154
154
|
bytes memory _dynamicData;
|
155
|
-
`,
|
155
|
+
`,n}function z(e){let{storeArgument:t}=e,{_typedTableId:n,_typedKeyArgs:r,_keyTupleDefinition:s}=V(e);return k(t,({_typedStore:a,_store:i,_commentSuffix:d,_methodNamePrefix:y,_useExplicitFieldLayout:o})=>{let c=g([a,n,r]),p="_tableId, _keyTuple"+(o?", _fieldLayout":"");return`
|
156
156
|
/**
|
157
|
-
* @notice Delete all data for given keys${
|
157
|
+
* @notice Delete all data for given keys${d}.
|
158
158
|
*/
|
159
|
-
function ${
|
160
|
-
${
|
161
|
-
${
|
159
|
+
function ${y}deleteRecord(${c}) internal {
|
160
|
+
${s}
|
161
|
+
${i}.deleteRecord(${p});
|
162
162
|
}
|
163
|
-
`})}function
|
163
|
+
`})}function me({structName:e,fields:t,staticFields:n,dynamicFields:r}){let s=e?`${e} memory _table`:g(t.map(({name:o,typeWithLocation:c})=>`${c} ${o}`)),a=e?"_table.":"",i=n.map(()=>0),d=0;for(let[o,c]of n.entries())i[o]=d,d+=c.staticByteLength;let y="";return n.length>0&&(y+=`
|
164
164
|
/**
|
165
165
|
* @notice Decode the tightly packed blob of static data using this table's field layout.
|
166
166
|
*/
|
167
|
-
function decodeStatic(bytes memory _blob) internal pure returns (${
|
168
|
-
${
|
169
|
-
${
|
167
|
+
function decodeStatic(bytes memory _blob) internal pure returns (${g(n.map(({name:o,typeWithLocation:c})=>`${c} ${o}`))}) {
|
168
|
+
${Y(n,(o,c)=>`
|
169
|
+
${o.name} = ${q(o,i[c])};
|
170
170
|
`)}
|
171
171
|
}
|
172
|
-
`),
|
172
|
+
`),r.length>0&&(y+=`
|
173
173
|
/**
|
174
174
|
* @notice Decode the tightly packed blob of dynamic data using the encoded lengths.
|
175
175
|
*/
|
176
|
-
function decodeDynamic(EncodedLengths _encodedLengths, bytes memory _blob) internal pure returns (${
|
177
|
-
${
|
176
|
+
function decodeDynamic(EncodedLengths _encodedLengths, bytes memory _blob) internal pure returns (${g(r.map(({name:o,typeWithLocation:c})=>`${c} ${o}`))}) {
|
177
|
+
${Y(r,(o,c)=>c===0?`
|
178
178
|
uint256 _start;
|
179
179
|
uint256 _end;
|
180
180
|
unchecked {
|
181
|
-
_end = _encodedLengths.atIndex(${
|
181
|
+
_end = _encodedLengths.atIndex(${c});
|
182
182
|
}
|
183
|
-
${
|
183
|
+
${o.name} = ${X(o)};
|
184
184
|
`:`
|
185
185
|
_start = _end;
|
186
186
|
unchecked {
|
187
|
-
_end += _encodedLengths.atIndex(${
|
187
|
+
_end += _encodedLengths.atIndex(${c});
|
188
188
|
}
|
189
|
-
${
|
189
|
+
${o.name} = ${X(o)};
|
190
190
|
`)}
|
191
191
|
}
|
192
|
-
`),
|
192
|
+
`),y+=`
|
193
193
|
/**
|
194
194
|
* @notice Decode the tightly packed blobs using this table's field layout.
|
195
|
-
* ${
|
196
|
-
* ${
|
197
|
-
* ${
|
195
|
+
* ${n.length>0?"@param _staticData Tightly packed static fields.":""}
|
196
|
+
* ${r.length>0?"@param _encodedLengths Encoded lengths of dynamic fields.":""}
|
197
|
+
* ${r.length>0?"@param _dynamicData Tightly packed dynamic fields.":""}
|
198
198
|
*/
|
199
199
|
function decode(
|
200
|
-
bytes memory ${
|
201
|
-
EncodedLengths ${
|
202
|
-
bytes memory ${
|
203
|
-
) internal pure returns (${
|
204
|
-
`,
|
205
|
-
(${
|
206
|
-
`),
|
207
|
-
(${
|
208
|
-
`),
|
200
|
+
bytes memory ${n.length>0?"_staticData":""},
|
201
|
+
EncodedLengths ${r.length>0?"_encodedLengths":""},
|
202
|
+
bytes memory ${r.length>0?"_dynamicData":""}
|
203
|
+
) internal pure returns (${s}) {
|
204
|
+
`,n.length>0&&(y+=`
|
205
|
+
(${g(n.map(o=>`${a}${o.name}`))}) = decodeStatic(_staticData);
|
206
|
+
`),r.length>0&&(y+=`
|
207
|
+
(${g(r.map(o=>`${a}${o.name}`))}) = decodeDynamic(_encodedLengths, _dynamicData);
|
208
|
+
`),y+=`
|
209
209
|
}
|
210
|
-
`,
|
211
|
-
SliceLib.getSubslice(_blob, _start, _end).decodeArray_${
|
212
|
-
)`:`${
|
210
|
+
`,y}function le({structName:e,fields:t}){return e?`${e} memory _table`:g(t.map(({name:n,typeWithLocation:r})=>`${r} ${n}`))}function X(e){let{typeId:t,arrayElement:n,typeWrap:r}=e;return n?`${r}(
|
211
|
+
SliceLib.getSubslice(_blob, _start, _end).decodeArray_${n.typeId}()
|
212
|
+
)`:`${r}(
|
213
213
|
${t}(
|
214
214
|
SliceLib.getSubslice(_blob, _start, _end).toBytes()
|
215
215
|
)
|
216
|
-
)`}import{renderArguments as
|
217
|
-
${
|
216
|
+
)`}import{renderArguments as b,renderCommonData as $e,renderList as C,renderImports as ge,renderTableId as Te,renderTypeHelpers as be,renderWithStore as _e,renderedSolidityHeader as Le,renderImportPath as T}from"@latticexyz/common/codegen";var O={TOTAL_LENGTH:240,NUM_STATIC_FIELDS:232,NUM_DYNAMIC_FIELDS:224};function J(e){return`FieldLayout constant _fieldLayout = FieldLayout.wrap(${fe(e)});`}function fe(e){let t=e.filter(({isDynamic:i})=>!i),n=e.length-t.length,r=0n,s=0;if(e.length>28)throw new Error("FieldLayout: too many fields");if(n>5)throw new Error("FieldLayout: too many dynamic fields");for(let i=0;i<t.length;i++){let{isDynamic:d,staticByteLength:y}=e[i];if(d)throw new Error("FieldLayout: static type after dynamic type");s+=y,r|=BigInt(y)<<BigInt((31-4-i)*8)}return r|=BigInt(s)<<BigInt(O.TOTAL_LENGTH),r|=BigInt(t.length)<<BigInt(O.NUM_STATIC_FIELDS),r|=BigInt(n)<<BigInt(O.NUM_DYNAMIC_FIELDS),`0x${r.toString(16).padStart(64,"0")}`}import{keySchemaToHex as Se,valueSchemaToHex as De}from"@latticexyz/protocol-parser/internal";function Q(e){let{imports:t,libraryName:n,structName:r,staticResourceData:s,storeImportPath:a,fields:i,staticFields:d,dynamicFields:y,withRecordMethods:o,storeArgument:c,keyTuple:p}=e,{_typedTableId:l,_typedKeyArgs:h,_keyTupleDefinition:f}=$e(e);return`
|
217
|
+
${Le}
|
218
218
|
|
219
219
|
// Import store internals
|
220
|
-
import { IStore } from "${a
|
221
|
-
import { StoreSwitch } from "${a
|
222
|
-
import { StoreCore } from "${a
|
223
|
-
import { Bytes } from "${a
|
224
|
-
import { Memory } from "${a
|
225
|
-
import { SliceLib } from "${a
|
226
|
-
import { EncodeArray } from "${a
|
227
|
-
import { FieldLayout } from "${a
|
228
|
-
import { Schema } from "${a
|
229
|
-
import { EncodedLengths, EncodedLengthsLib } from "${a
|
230
|
-
import { ResourceId } from "${a
|
220
|
+
import { IStore } from "${T(a,"IStore.sol")}";
|
221
|
+
import { StoreSwitch } from "${T(a,"StoreSwitch.sol")}";
|
222
|
+
import { StoreCore } from "${T(a,"StoreCore.sol")}";
|
223
|
+
import { Bytes } from "${T(a,"Bytes.sol")}";
|
224
|
+
import { Memory } from "${T(a,"Memory.sol")}";
|
225
|
+
import { SliceLib } from "${T(a,"Slice.sol")}";
|
226
|
+
import { EncodeArray } from "${T(a,"tightcoder/EncodeArray.sol")}";
|
227
|
+
import { FieldLayout } from "${T(a,"FieldLayout.sol")}";
|
228
|
+
import { Schema } from "${T(a,"Schema.sol")}";
|
229
|
+
import { EncodedLengths, EncodedLengthsLib } from "${T(a,"EncodedLengths.sol")}";
|
230
|
+
import { ResourceId } from "${T(a,"ResourceId.sol")}";
|
231
231
|
|
232
232
|
${t.length>0?`
|
233
233
|
// Import user types
|
234
|
-
${
|
234
|
+
${ge(t)}
|
235
235
|
`:""}
|
236
236
|
|
237
|
-
${
|
238
|
-
struct ${
|
239
|
-
${
|
237
|
+
${r?`
|
238
|
+
struct ${r} {
|
239
|
+
${C(i,({name:m,typeId:u})=>`${u} ${m};`)}
|
240
240
|
}
|
241
241
|
`:""}
|
242
242
|
|
243
|
-
library ${
|
244
|
-
${
|
243
|
+
library ${n} {
|
244
|
+
${s?Te(s):""}
|
245
245
|
|
246
|
-
${
|
246
|
+
${J(i)}
|
247
247
|
|
248
|
-
// Hex-encoded key schema of (${
|
249
|
-
Schema constant _keySchema = Schema.wrap(${
|
250
|
-
// Hex-encoded value schema of (${
|
251
|
-
Schema constant _valueSchema = Schema.wrap(${
|
248
|
+
// Hex-encoded key schema of (${p.map(m=>m.internalTypeId).join(", ")})
|
249
|
+
Schema constant _keySchema = Schema.wrap(${Se(Object.fromEntries(p.map(m=>[m.name,m.internalTypeId])))});
|
250
|
+
// Hex-encoded value schema of (${i.map(m=>m.internalTypeId).join(", ")})
|
251
|
+
Schema constant _valueSchema = Schema.wrap(${De(Object.fromEntries(i.map(m=>[m.name,m.internalTypeId])))});
|
252
252
|
|
253
253
|
/**
|
254
254
|
* @notice Get the table's key field names.
|
255
255
|
* @return keyNames An array of strings with the names of key fields.
|
256
256
|
*/
|
257
257
|
function getKeyNames() internal pure returns (string[] memory keyNames) {
|
258
|
-
keyNames = new string[](${
|
259
|
-
${
|
258
|
+
keyNames = new string[](${p.length});
|
259
|
+
${C(p,(m,u)=>`keyNames[${u}] = "${m.name}";`)}
|
260
260
|
}
|
261
261
|
|
262
262
|
/**
|
@@ -264,30 +264,30 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
264
264
|
* @return fieldNames An array of strings with the names of value fields.
|
265
265
|
*/
|
266
266
|
function getFieldNames() internal pure returns (string[] memory fieldNames) {
|
267
|
-
fieldNames = new string[](${
|
268
|
-
${
|
267
|
+
fieldNames = new string[](${i.length});
|
268
|
+
${C(i,(m,u)=>`fieldNames[${u}] = "${m.name}";`)}
|
269
269
|
}
|
270
270
|
|
271
|
-
${
|
271
|
+
${_e(c,({_typedStore:m,_store:u,_commentSuffix:L,_methodNamePrefix:A})=>`
|
272
272
|
/**
|
273
|
-
* @notice Register the table with its config${
|
273
|
+
* @notice Register the table with its config${L}.
|
274
274
|
*/
|
275
|
-
function ${
|
276
|
-
${
|
275
|
+
function ${A}register(${b([m,l])}) internal {
|
276
|
+
${u}.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames());
|
277
277
|
}
|
278
278
|
`)}
|
279
279
|
|
280
|
-
${
|
280
|
+
${K(e)}
|
281
281
|
|
282
|
-
${
|
282
|
+
${o?Z(e):""}
|
283
283
|
|
284
|
-
${
|
284
|
+
${z(e)}
|
285
285
|
|
286
|
-
${
|
286
|
+
${Ie(d)}
|
287
287
|
|
288
|
-
${
|
288
|
+
${Ae(y)}
|
289
289
|
|
290
|
-
${
|
290
|
+
${xe(y)}
|
291
291
|
|
292
292
|
/**
|
293
293
|
* @notice Encode all of a record's fields.
|
@@ -295,8 +295,8 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
295
295
|
* @return The lengths of the dynamic fields (packed into a single bytes32 value).
|
296
296
|
* @return The dynamic (variable length) data, encoded into a sequence of bytes.
|
297
297
|
*/
|
298
|
-
function encode(${
|
299
|
-
${
|
298
|
+
function encode(${b(e.fields.map(({name:m,typeWithLocation:u})=>`${u} ${m}`))}) internal pure returns (bytes memory, EncodedLengths, bytes memory) {
|
299
|
+
${w(e)}
|
300
300
|
|
301
301
|
return (_staticData, _encodedLengths, _dynamicData);
|
302
302
|
}
|
@@ -304,47 +304,47 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
304
304
|
/**
|
305
305
|
* @notice Encode keys as a bytes32 array using this table's field layout.
|
306
306
|
*/
|
307
|
-
function encodeKeyTuple(${
|
307
|
+
function encodeKeyTuple(${b([h])}) internal pure returns (bytes32[] memory) {
|
308
308
|
${f}
|
309
309
|
return _keyTuple;
|
310
310
|
}
|
311
311
|
}
|
312
312
|
|
313
|
-
${
|
314
|
-
`}function
|
313
|
+
${be(e)}
|
314
|
+
`}function Ie(e){return e.length===0?"":`
|
315
315
|
/**
|
316
316
|
* @notice Tightly pack static (fixed length) data using this table's schema.
|
317
317
|
* @return The static data, encoded into a sequence of bytes.
|
318
318
|
*/
|
319
|
-
function encodeStatic(${
|
320
|
-
return abi.encodePacked(${
|
319
|
+
function encodeStatic(${b(e.map(({name:t,typeWithLocation:n})=>`${n} ${t}`))}) internal pure returns (bytes memory) {
|
320
|
+
return abi.encodePacked(${b(e.map(({name:t})=>t))});
|
321
321
|
}
|
322
|
-
`}function
|
322
|
+
`}function Ae(e){return e.length===0?"":`
|
323
323
|
/**
|
324
324
|
* @notice Tightly pack dynamic data lengths using this table's schema.
|
325
325
|
* @return _encodedLengths The lengths of the dynamic fields (packed into a single bytes32 value).
|
326
326
|
*/
|
327
|
-
function encodeLengths(${
|
327
|
+
function encodeLengths(${b(e.map(({name:t,typeWithLocation:n})=>`${n} ${t}`))}) internal pure returns (EncodedLengths _encodedLengths) {
|
328
328
|
// Lengths are effectively checked during copy by 2**40 bytes exceeding gas limits
|
329
329
|
unchecked {
|
330
330
|
_encodedLengths = EncodedLengthsLib.pack(
|
331
|
-
${
|
331
|
+
${b(e.map(({name:t,arrayElement:n})=>n?`${t}.length * ${n.staticByteLength}`:`bytes(${t}).length`))}
|
332
332
|
);
|
333
333
|
}
|
334
334
|
}
|
335
|
-
`}function
|
335
|
+
`}function xe(e){return e.length===0?"":`
|
336
336
|
/**
|
337
337
|
* @notice Tightly pack dynamic (variable length) data using this table's schema.
|
338
338
|
* @return The dynamic data, encoded into a sequence of bytes.
|
339
339
|
*/
|
340
|
-
function encodeDynamic(${
|
341
|
-
return abi.encodePacked(${
|
340
|
+
function encodeDynamic(${b(e.map(({name:t,typeWithLocation:n})=>`${n} ${t}`))}) internal pure returns (bytes memory) {
|
341
|
+
return abi.encodePacked(${b(e.map(t=>E(t)))});
|
342
342
|
}
|
343
|
-
`}import{renderEnums as
|
344
|
-
${
|
343
|
+
`}import{renderEnums as Ee}from"@latticexyz/common/codegen";function _t(e){return Ee(e.enums)}import Ue from"node:fs/promises";import B from"node:path";import{formatAndWriteSolidity as G,renderEnums as je}from"@latticexyz/common/codegen";import{renderList as Fe,renderedSolidityHeader as Re}from"@latticexyz/common/codegen";import ee from"node:path/posix";function te(e,t){return`
|
344
|
+
${Re}
|
345
345
|
|
346
|
-
${
|
347
|
-
`}import{
|
346
|
+
${Fe(t,({outputPath:n,tableName:r,renderOptions:{structName:s}})=>{let a=[r];return s&&a.push(s),`import { ${a.join(", ")} } from "./${ee.relative(ee.dirname(e),n)}";`})}
|
347
|
+
`}import{uniqueBy as Ge}from"@latticexyz/common/utils";import ke from"node:path";function v({config:e}){return ke.join(e.sourceDirectory,e.codegen.outputDirectory,e.codegen.userTypesFilename)}import{groupBy as we}from"@latticexyz/common/utils";function ne({config:e}){let t=Object.keys(e.enums).map(a=>({type:"enum",name:a,abiType:"uint8",importPath:"./"+v({config:e})})),n=Object.entries(e.userTypes).map(([a,i])=>({type:"userType",name:a,abiType:i.type,importPath:i.filePath})),r=[...t,...n],s=Array.from(we(r,a=>a.name).entries()).filter(([,a])=>a.length>1).map(([a])=>a);if(s.length>0)throw new Error(`Found enums and user types sharing the same name: ${s.join(", ")}`);return r}import I from"path";import{SchemaTypeArrayToElement as Ne}from"@latticexyz/schema-type/deprecated";import{AbiTypeToSchemaType as F,getStaticByteLength as P,SchemaType as R,SchemaTypeToAbiType as Oe}from"@latticexyz/schema-type/deprecated";function ve(e){let t=e.match(/^(\w+)\[(\d+)\]$/);return t?{elementType:t[1],staticLength:Number.parseInt(t[2])}:null}function U(e,t){if(e in F){let s=F[e];return{schemaType:s,renderType:j(s)}}let n=ve(e);if(n){if(n.elementType in F)return Me(e,n.elementType,n.staticLength);throw new Error("Static arrays of user types are not supported")}let r=t.find(s=>s.name===e);if(!r)throw new Error(`User type "${e}" not found`);return Be(r)}function j(e){let t=P(e),n=t===0,r=Oe[e];return{typeId:r,typeWithLocation:n?r+" memory":r,enumName:R[e],staticByteLength:t,isDynamic:n,typeWrap:"",typeUnwrap:"",internalTypeId:r}}function Be(e){switch(e.type){case"enum":{let t=R.UINT8,n=P(t),r=n===0;return{schemaType:t,renderType:{typeId:e.name,typeWithLocation:e.name,enumName:R[t],staticByteLength:n,isDynamic:r,typeWrap:e.name,typeUnwrap:e.abiType,internalTypeId:e.abiType}}}case"userType":{let t=F[e.abiType];return{schemaType:t,renderType:{typeId:e.name,typeWithLocation:e.name,enumName:R[t],staticByteLength:P(t),isDynamic:!1,typeWrap:`${e.name}.wrap`,typeUnwrap:`${e.name}.unwrap`,internalTypeId:e.abiType}}}}}function Me(e,t,n){let r=t+"[]",s=F[r];return{schemaType:s,renderType:{typeId:e,typeWithLocation:`${e} memory`,enumName:R[s],staticByteLength:0,isDynamic:!0,typeWrap:`toStaticArray_${t}_${n}`,typeUnwrap:`fromStaticArray_${t}_${n}`,typeWrappingData:{kind:"staticArray",elementType:t,staticLength:n},internalTypeId:r}}}import{getKeySchema as We,getValueSchema as Ce}from"@latticexyz/protocol-parser/internal";import{isDefined as Pe}from"@latticexyz/common/utils";function re({tables:e,rootDir:t,codegenDir:n,userTypes:r,storeImportPath:s}){return e.map(i=>{let d=I.join(t,n,i.codegen.outputDirectory,`${i.label}.sol`),y=We(i),o=Ce(i),c=i.codegen.dataStruct,p=c||i.type==="offchainTable"||Object.keys(o).length>1,l=!p&&Object.keys(o).length===1,h=Object.values(i.schema).map($=>r.find(x=>x.name===$.internalType)).filter(Pe).map($=>({symbol:$.name.replace(/\..*$/,""),path:$.importPath.startsWith(".")?"./"+I.relative(I.dirname(d),I.join(t,$.importPath)):$.importPath})),f=Object.entries(y).map(([$,x])=>{let{renderType:M}=U(x.internalType,r);return{...M,name:$,isDynamic:!1}}),m=Object.entries(o).map(([$,x])=>{let{renderType:M,schemaType:de}=U(x.internalType,r),H=Ne[de];return{...M,arrayElement:H!==void 0?j(H):void 0,name:$}}),u=m.filter(({isDynamic:$})=>!$),L=m.filter(({isDynamic:$})=>$),A=i.codegen.tableIdArgument?void 0:{namespace:i.namespace,name:i.name,offchainOnly:i.type==="offchainTable"};return{outputPath:d,tableName:i.label,renderOptions:{imports:h,libraryName:i.label,structName:c?i.label+"Data":void 0,staticResourceData:A,storeImportPath:s.startsWith(".")?"./"+I.relative(I.dirname(d),I.join(t,s)):s,keyTuple:f,fields:m,staticFields:u,dynamicFields:L,withGetters:i.type==="table",withRecordMethods:p,withDynamicFieldMethods:i.type==="table",withSuffixlessFieldMethods:l,storeArgument:i.codegen.storeArgument}}})}async function Xt({rootDir:e,config:t}){let n=ne({config:t});if(Object.keys(t.enums).length>0){let r=B.join(e,v({config:t})),s=je(t.enums);await G(s,r,"Generated types file with enums")}await Promise.all(Object.values(t.namespaces).map(async r=>{let a=t.sourceDirectory,i=B.join(a,t.codegen.outputDirectory),d=Object.values(r.tables);if(d.length===0)return;let y=re({tables:d,rootDir:e,codegenDir:i,userTypes:n,storeImportPath:t.codegen.storeImportPath}),o=Ge(y.map(({outputPath:l})=>B.dirname(l)),l=>l);await Promise.all(o.map(l=>Ue.rm(l,{recursive:!0,force:!0}))),await Promise.all(y.map(async({outputPath:l,renderOptions:h})=>{let f=Q(h);return await G(f,l,"Generated table")}));let c=B.join(e,i,t.codegen.indexFilename),p=te(c,y);await G(p,c,"Generated table index")}))}import{renderedSolidityHeader as He}from"@latticexyz/common/codegen";import{staticAbiTypeToByteLength as Ke,staticAbiTypes as qe}from"@latticexyz/schema-type/internal";import{getLeftPaddingBits as oe}from"@latticexyz/common/codegen";function ie(e){return`
|
348
348
|
/**
|
349
349
|
* @notice Decodes a slice into an array of ${e.internalTypeId}.
|
350
350
|
* @dev Uses TightCoder for initial decoding, and then assembly for memory conversion.
|
@@ -359,13 +359,13 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
359
359
|
bytes32[] memory _genericArray = TightCoder.decode(
|
360
360
|
_input,
|
361
361
|
${e.staticByteLength},
|
362
|
-
${
|
362
|
+
${oe(e)}
|
363
363
|
);
|
364
364
|
assembly {
|
365
365
|
_output := _genericArray
|
366
366
|
}
|
367
367
|
}
|
368
|
-
`}function
|
368
|
+
`}function ae(e){return`
|
369
369
|
|
370
370
|
/**
|
371
371
|
* @notice Encodes an array of ${e.internalTypeId} into a tightly packed bytes representation.
|
@@ -380,11 +380,11 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
380
380
|
return TightCoder.encode(
|
381
381
|
_genericArray,
|
382
382
|
${e.staticByteLength},
|
383
|
-
${
|
383
|
+
${oe(e)}
|
384
384
|
);
|
385
385
|
}
|
386
|
-
`}function
|
387
|
-
${
|
386
|
+
`}function nn(){return`
|
387
|
+
${He}
|
388
388
|
import { TightCoder } from "./TightCoder.sol";
|
389
389
|
import { Slice } from "../Slice.sol";
|
390
390
|
|
@@ -395,11 +395,11 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
395
395
|
* @dev This library provides functions for decoding slices into arrays of basic uint types.
|
396
396
|
*/
|
397
397
|
library DecodeSlice {
|
398
|
-
${
|
398
|
+
${qe.map(e=>ie({internalTypeId:e,staticByteLength:Ke[e]})).join(`
|
399
399
|
`)}
|
400
400
|
}
|
401
|
-
`}import{renderedSolidityHeader as
|
402
|
-
${
|
401
|
+
`}import{renderedSolidityHeader as Ye}from"@latticexyz/common/codegen";import{staticAbiTypeToByteLength as Xe,staticAbiTypes as Ve}from"@latticexyz/schema-type/internal";function sn(){return`
|
402
|
+
${Ye}
|
403
403
|
import { TightCoder } from "./TightCoder.sol";
|
404
404
|
|
405
405
|
/**
|
@@ -408,10 +408,10 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
408
408
|
* @dev This library provides utilities for encoding arrays into tightly packed bytes representations.
|
409
409
|
*/
|
410
410
|
library EncodeArray {
|
411
|
-
${
|
411
|
+
${Ve.map(e=>ae({internalTypeId:e,staticByteLength:Xe[e]})).join(`
|
412
412
|
`)}
|
413
413
|
}
|
414
|
-
`}import{renderedSolidityHeader as
|
414
|
+
`}import{renderedSolidityHeader as Ze}from"@latticexyz/common/codegen";import{staticAbiTypes as ze}from"@latticexyz/schema-type/internal";function Je({typeId:e}){return`
|
415
415
|
function testEncodeDecodeArray_${e}(
|
416
416
|
${e} val0,
|
417
417
|
${e} val1,
|
@@ -431,15 +431,15 @@ import{renderArguments as S,renderCommonData as ae,renderWithFieldSuffix as b,re
|
|
431
431
|
assertEq(decoded[1], val1);
|
432
432
|
assertEq(decoded[2], val2);
|
433
433
|
}
|
434
|
-
`}function
|
435
|
-
${
|
434
|
+
`}function yn(){return`
|
435
|
+
${Ze}
|
436
436
|
|
437
437
|
import { Test } from "forge-std/Test.sol";
|
438
438
|
import { EncodeArray } from "../../src/tightcoder/EncodeArray.sol";
|
439
439
|
import { SliceLib } from "../../src/Slice.sol";
|
440
440
|
|
441
441
|
contract TightCoderAutoTest is Test {
|
442
|
-
${
|
442
|
+
${ze.map(e=>Je({typeId:e})).join("")}
|
443
443
|
}
|
444
|
-
`}export{
|
444
|
+
`}export{j as getSchemaTypeInfo,re as getTableOptions,Be as getUserTypeInfo,nn as renderDecodeSlice,q as renderDecodeValueType,z as renderDeleteRecordMethods,sn as renderEncodeArray,E as renderEncodeFieldSingle,K as renderFieldMethods,w as renderRecordData,Z as renderRecordMethods,Q as renderTable,yn as renderTightCoderAutoTest,Je as renderTightCoderAutoTestFunction,ie as renderTightCoderDecode,ae as renderTightCoderEncode,_t as renderTypesFromConfig,U as resolveAbiOrUserType,Xt as tablegen};
|
445
445
|
//# sourceMappingURL=codegen.js.map
|