@@ -1142,11 +1142,7 @@ export class ArrayType<T extends Type> extends AbstractType<schema.ArraySchema<S
1142
1142
ctx . inc ( MaxEncodingOverhead . Array ) ;
1143
1143
const rLen = codegen . var ( `${ value . use ( ) } .length` ) ;
1144
1144
const type = this . type ;
1145
- codegen . js (
1146
- `size += ${
1147
- MaxEncodingOverhead . ArrayElement === 1 ? `${ rLen } ` : `${ MaxEncodingOverhead . ArrayElement } * ${ rLen } `
1148
- } ;`,
1149
- ) ;
1145
+ codegen . js ( `size += ${ MaxEncodingOverhead . ArrayElement } * ${ rLen } ` ) ;
1150
1146
// TODO: Use ".capacityEstimator()" here.
1151
1147
const fn = type . compileCapacityEstimator ( {
1152
1148
system : ctx . options . system ,
@@ -2005,31 +2001,19 @@ export class MapType<T extends Type> extends AbstractType<schema.MapSchema<Schem
2005
2001
}
2006
2002
2007
2003
public codegenCapacityEstimator ( ctx : CapacityEstimatorCodegenContext , value : JsExpression ) : void {
2008
- ctx . codegen . js ( `size += maxEncodingCapacity(${ value . use ( ) } );` ) ;
2009
- // throw new Error('TODO CAP');
2010
- // const codegen = ctx.codegen;
2011
- // ctx.inc(MaxEncodingOverhead.Array);
2012
- // const rLen = codegen.var(`${value.use()}.length`);
2013
- // const type = this.type;
2014
- // codegen.js(
2015
- // `size += ${
2016
- // MaxEncodingOverhead.ArrayElement === 1 ? `${rLen}` : `${MaxEncodingOverhead.ArrayElement} * ${rLen}`
2017
- // };`,
2018
- // );
2019
- // // TODO: Use ".capacityEstimator()" here.
2020
- // const fn = type.compileCapacityEstimator({
2021
- // system: ctx.options.system,
2022
- // name: ctx.options.name,
2023
- // });
2024
- // const isConstantSizeType = type instanceof ConstType || type instanceof BooleanType || type instanceof NumberType;
2025
- // if (isConstantSizeType) {
2026
- // codegen.js(`size += ${rLen} * ${fn(null)};`);
2027
- // } else {
2028
- // const r = codegen.var(value.use());
2029
- // const rFn = codegen.linkDependency(fn);
2030
- // const ri = codegen.getRegister();
2031
- // codegen.js(`for(var ${ri} = ${rLen}; ${ri}-- !== 0;) size += ${rFn}(${r}[${ri}]);`);
2032
- // }
2004
+ const codegen = ctx . codegen ;
2005
+ ctx . inc ( MaxEncodingOverhead . Object ) ;
2006
+ const r = codegen . var ( value . use ( ) ) ;
2007
+ const rKeys = codegen . var ( `Object.keys(${ r } )` ) ;
2008
+ const rKey = codegen . var ( ) ;
2009
+ const rLen = codegen . var ( `${ rKeys } .length` ) ;
2010
+ codegen . js ( `size += ${ MaxEncodingOverhead . ObjectElement } * ${ rLen } ` ) ;
2011
+ const ri = codegen . var ( '0' ) ;
2012
+ codegen . js ( `for (; ${ ri } < ${ rLen } ; ${ ri } ++) {` ) ;
2013
+ codegen . js ( `${ rKey } = ${ rKeys } [${ ri } ];` ) ;
2014
+ codegen . js ( `size += ${ MaxEncodingOverhead . String } + ${ MaxEncodingOverhead . StringLengthMultiplier } * ${ rKey } .length;` ) ;
2015
+ this . type . codegenCapacityEstimator ( ctx , new JsExpression ( ( ) => `${ r } [${ rKey } ]` ) ) ;
2016
+ codegen . js ( `}` ) ;
2033
2017
}
2034
2018
2035
2019
public random ( ) : Record < string , unknown > {
0 commit comments