Files
OpenRCT2-Web/src/openrct2/scripting/bindings/entity/ScLitter.cpp
T
Michał Janiszewski 8d9aec1090 Fix #26304: Use common prototypes for JS entities (#26321)
Previously JS entities would create prototypes on each allocation in
`Sc*::New`. This change creates a single global protype for each entity
class which then gets copied upon entity allocation in JS realm.

Running test with "Live Ride Measurements" plugin on a park shared for
performance testing I get following results:

|                 | OpenRCT2::Scripting::HookEngine::Call | OpenRCT2::Scripting::ScMap::getAllEntities |
|-----------------|---------------------------------------|--------------------------------------------|
| duktape         | 68ms                                  | 10ms                                       |
| quickjs current | 318ms                                 | 126ms                                      |
| quickjs + fix   | 22ms                                  | 942µs                                      |

Previously each call to `getAllEntities` would result in 1382 calls to
`JS_SetPropertyFunctionList`, each costing 90.5µs.

Park used: https://files.catbox.moe/z6j2g4.park
2026-04-05 09:22:47 +02:00

94 lines
3.1 KiB
C++

/*****************************************************************************
* Copyright (c) 2014-2026 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#ifdef ENABLE_SCRIPTING
#include "ScLitter.hpp"
#include "../../../core/EnumMap.hpp"
#include "../../../entity/Litter.h"
namespace OpenRCT2::Scripting
{
static const EnumMap<Litter::Type> LitterTypeMap(
{
{ "vomit", Litter::Type::vomit },
{ "vomit_alt", Litter::Type::vomitAlt },
{ "empty_can", Litter::Type::emptyCan },
{ "rubbish", Litter::Type::rubbish },
{ "burger_box", Litter::Type::burgerBox },
{ "empty_cup", Litter::Type::emptyCup },
{ "empty_box", Litter::Type::emptyBox },
{ "empty_bottle", Litter::Type::emptyBottle },
{ "empty_bowl_red", Litter::Type::emptyBowlRed },
{ "empty_drink_carton", Litter::Type::emptyDrinkCarton },
{ "empty_juice_cup", Litter::Type::emptyJuiceCup },
{ "empty_bowl_blue", Litter::Type::emptyBowlBlue },
});
ScLitter gScLitter;
JSValue ScLitter::New(JSContext* ctx, EntityId entityId)
{
return gScEntity.NewDerivedInstance(ctx, entityId, gScLitter.GetProto());
}
void ScLitter::Register(JSContext* ctx)
{
static constexpr JSCFunctionListEntry funcs[] = {
JS_CGETSET_DEF("litterType", &ScLitter::litterType_get, &ScLitter::litterType_set),
JS_CGETSET_DEF("creationTick", &ScLitter::creationTick_get, nullptr)
};
gScLitter.RegisterDerived(ctx, gScEntity, funcs);
}
Litter* ScLitter::GetLitter(JSValue thisVal)
{
auto id = GetEntityId(thisVal);
return getGameState().entities.GetEntity<Litter>(id);
}
JSValue ScLitter::litterType_get(JSContext* ctx, JSValue thisVal)
{
auto* litter = GetLitter(thisVal);
if (litter != nullptr)
{
auto it = LitterTypeMap.find(litter->SubType);
if (it != LitterTypeMap.end())
{
return JSFromStdString(ctx, it->first);
}
}
return JS_UNDEFINED;
}
JSValue ScLitter::litterType_set(JSContext* ctx, JSValue thisVal, JSValue jsValue)
{
JS_UNPACK_STR(litterType, ctx, jsValue);
JS_THROW_IF_GAME_STATE_NOT_MUTABLE();
auto it = LitterTypeMap.find(litterType);
if (it != LitterTypeMap.end())
{
auto* litter = GetLitter(thisVal);
litter->SubType = it->second;
litter->Invalidate();
}
return JS_UNDEFINED;
}
JSValue ScLitter::creationTick_get(JSContext* ctx, JSValue thisVal)
{
auto* litter = GetLitter(thisVal);
return JS_NewUint32(ctx, litter == nullptr ? 0 : litter->creationTick);
}
} // namespace OpenRCT2::Scripting
#endif