I’ve run across a possible bug in the kotlinc-js compiler.
It appears under some condition a local object reference is replaced with a reference to ‘this’ in the output javascript. You can see what I mean in the code below, the reference to ‘entity.onComponentAdded’ becomes a reference to ‘this.onComponentAdded’, which then throws a runtime error.
Kotlin:
fun createEntity(name: String): Entity {
val entity = if (_reusableEntities.size > 0) _reusableEntities.removeAt(_reusableEntities.size-1) else Entity(totalComponents)
entity.initialize(name, _creationIndex++)
entity.retain()
entity.onComponentAdded += updateGroupsComponentAddedOrRemoved
entity.onComponentRemoved += updateGroupsComponentAddedOrRemoved
entity.onComponentReplaced += updateGroupsComponentReplaced
entity.onEntityReleased += onEntityReleased
_entities.add(entity)
_entitiesCache.clear()
onEntityCreated(PoolEntityChangedArgs(this, entity))
return entity
}
Output JS:
createEntity_61zpoe$: function (name) {
var entity = this._reusableEntities_qm8b34$.size > 0 ? this._reusableEntities_qm8b34$.removeAt_za3lpa$(this._reusableEntities_qm8b34$.size - 1) : new _.com.darkoverlordofdata.entitas.Entity(this.totalComponents);
entity.initialize_bm4lxs$(name, this._creationIndex_ph540x$++);
entity.retain();
this.onComponentAdded.plusAssign_b6jukf$(this.updateGroupsComponentAddedOrRemoved);
this.onComponentRemoved.plusAssign_b6jukf$(this.updateGroupsComponentAddedOrRemoved);
this.onComponentReplaced.plusAssign_b6jukf$(this.updateGroupsComponentReplaced);
this.onEntityReleased.plusAssign_b6jukf$(this.onEntityReleased);
this._entities_b9aiol$.add_za3rmp$(entity);
this._entitiesCache_kq1rwz$.clear();
this.onEntityCreated.invoke_1n2974$(new _.com.darkoverlordofdata.entitas.PoolEntityChangedArgs(this, entity));
return entity;
},