I am translating a program from c++ to typescript, and I face a strange behaviour trying to empty an array using the splice technique (How do I empty an array in JavaScript?) to empty an array.
Here is an excerpt of my code in typescript
"use strict"
class UniformGridGeometry<ItemT> extends Array<ItemT> {
itemType: { new (): ItemT; }
constructor(itemType: { new (): ItemT; }) {
// constructor(itemType: { new (): ItemT; }, uGeomTemplate: UniformGridGeometry<any>) // any : Vorton, Particle, Vec*, Mat*, ...
// constructor(itemType: { new (): ItemT; }, uNumElements: number, vMin: Vec3, vMax: Vec3, bPowerOf2: boolean)
// constructor(itemType: { new (): ItemT; }, arg?: any, vMin?: Vec3, vMax?: Vec3, bPowerOf2?: boolean) {
super(); // Array
this.itemType = itemType;
// (...)
}
}
class UniformGrid<ItemT> extends UniformGridGeometry<ItemT> {
constructor(itemType: { new (): ItemT; }) {
// constructor(itemType: { new (): ItemT; }, uGeomTemplate: UniformGridGeometry<any>) // any : Vorton, Particle, Vec*, Mat*, ...
// constructor(itemType: { new (): ItemT; }, uNumElements: number, vMin: Vec3, vMax: Vec3, bPowerOf2: boolean)
// constructor(itemType: { new (): ItemT; }, arg?: any, vMin?: Vec3, vMax?: Vec3, bPowerOf2?: boolean) {
super(itemType);
// (...)
}
}
class NestedGrid<ItemT> extends Array<UniformGrid<ItemT>> {
constructor(src?: UniformGrid<ItemT>) {
super();
if (src) {
this.Init(src);
}
}
Init(src: UniformGrid<ItemT>) {
this.splice(0, this.length) // mUniformGrids.Clear() ;
console.assert(typeof src === 'object', typeof src);
// let numUniformGrids = this.PrecomputeNumUniformGrids( src ) ;
// this.mUniformGrids.Reserve( numUniformGrids ) ; // Preallocate number of UniformGrids to avoid reallocation during PushBack.
let uniformGrid = new UniformGrid<ItemT>(src.itemType);
// uniformGrid.Decimate( src , 1 ) ;
// uniformGrid.Init() ;
this.push(uniformGrid);
// (...)
}
}
function doTests() {
console.info("Test > NestedGrid ; UniformGrid");
let mInfluenceTree: NestedGrid<any> = new NestedGrid<any>(); // Influence tree
let ugSkeleton = new UniformGrid<any>(null);
mInfluenceTree.Init(ugSkeleton);
console.log(mInfluenceTree);
mInfluenceTree.Init(ugSkeleton);
console.log(mInfluenceTree);
}
doTests();
that generates (ES6 target) the following Javascript :
"use strict";
class UniformGridGeometry extends Array {
constructor(itemType) {
super();
this.itemType = itemType;
}
}
class UniformGrid extends UniformGridGeometry {
constructor(itemType) {
super(itemType);
}
}
class NestedGrid extends Array {
constructor(src) {
super();
if (src) {
this.Init(src);
}
}
Init(src) {
this.splice(0, this.length);
console.assert(typeof src === 'object', typeof src);
let uniformGrid = new UniformGrid(src.itemType);
this.push(uniformGrid);
}
}
function doTests() {
console.info("Test > NestedGrid ; UniformGrid");
let mInfluenceTree = new NestedGrid();
let ugSkeleton = new UniformGrid(null);
mInfluenceTree.Init(ugSkeleton);
console.log(mInfluenceTree);
mInfluenceTree.Init(ugSkeleton);
console.log(mInfluenceTree);
}
doTests();
The same code, on firefox or as code snippet works well, but on chromium the assertion fails, the argument 'src' becomes a number (the size of the array in fact) what am I doing wrong ? (the two Init calls simulates the processing in the WebGL loop)
Thanks.