From 7dbc6939c4c7dddb74698f94b6a9f979befd45d0 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 4 Mar 2017 12:43:07 +0100 Subject: [PATCH] - declared most native getters in Actor as const. - made the self pointer of const functions readonly. This seems to work fine. Both calling a non-const function and trying to assign a value to a member fail with an error message. --- src/scripting/thingdef.cpp | 3 +- src/scripting/zscript/zcc_compile.cpp | 9 ++++++ wadsrc/static/zscript/actor.txt | 40 +++++++++++++-------------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/scripting/thingdef.cpp b/src/scripting/thingdef.cpp index d9eca5fcaa..c0e444893f 100644 --- a/src/scripting/thingdef.cpp +++ b/src/scripting/thingdef.cpp @@ -110,12 +110,13 @@ void SetImplicitArgs(TArray *args, TArray *argflags, TArrayPush(NewPointer(cls)); + if (args != nullptr) args->Push(NewPointer(cls, !!(funcflags & VARF_ReadOnly))); if (argflags != nullptr) argflags->Push(VARF_Implicit | VARF_ReadOnly); if (argnames != nullptr) argnames->Push(NAME_self); } if (funcflags & VARF_Action) { + assert(!(funcflags & VARF_ReadOnly)); // implied caller and callingstate pointers if (args != nullptr) { diff --git a/src/scripting/zscript/zcc_compile.cpp b/src/scripting/zscript/zcc_compile.cpp index 2000075c91..f8e9980a25 100644 --- a/src/scripting/zscript/zcc_compile.cpp +++ b/src/scripting/zscript/zcc_compile.cpp @@ -2156,6 +2156,15 @@ void ZCCCompiler::CompileFunction(ZCC_StructWork *c, ZCC_FuncDeclarator *f, bool } if (f->Flags & ZCC_Action) { + if (varflags & VARF_ReadOnly) + { + Error(f, "Action functions cannot be declared const"); + varflags &= ~VARF_ReadOnly; + } + if (varflags & VARF_UI) + { + Error(f, "Action functions cannot be declared UI"); + } // Non-Actors cannot have action functions. if (!c->Type()->IsKindOf(RUNTIME_CLASS(PClassActor))) { diff --git a/wadsrc/static/zscript/actor.txt b/wadsrc/static/zscript/actor.txt index 029aebc0ae..ec5ce71d8f 100644 --- a/wadsrc/static/zscript/actor.txt +++ b/wadsrc/static/zscript/actor.txt @@ -468,15 +468,15 @@ class Actor : Thinker native native bool UpdateWaterLevel (bool splash = true); native bool IsZeroDamage(); native void ClearInterpolation(); - native Vector3 PosRelative(sector sec); + native Vector3 PosRelative(sector sec) const; native void HandleSpawnFlags(); native void ExplodeMissile(line lin = null, Actor target = null, bool onsky = false); native void RestoreDamage(); - native int SpawnHealth(); + native int SpawnHealth() const; native void SetDamage(int dmg); - native double Distance2D(Actor other); - native double Distance3D(Actor other); + native double Distance2D(Actor other) const; + native double Distance3D(Actor other) const; native void SetOrigin(vector3 newpos, bool moving); native void SetXYZ(vector3 newpos); native Actor GetPointer(int aaptr); @@ -511,7 +511,7 @@ class Actor : Thinker native native void BloodSplatter (Vector3 pos, double hitangle, bool axe = false); native bool HitWater (sector sec, Vector3 pos, bool checkabove = false, bool alert = true, bool force = false); native void PlaySpawnSound(Actor missile); - native bool CountsAsKill(); + native bool CountsAsKill() const; native bool Teleport(Vector3 pos, double angle, int flags); native void TraceBleed(int damage, Actor missile); @@ -559,21 +559,21 @@ class Actor : Thinker native native void LinkToWorld(LinkContext ctx = null); native void UnlinkFromWorld(out LinkContext ctx = null); native bool CanSeek(Actor target); - native double AngleTo(Actor target, bool absolute = false); + native double AngleTo(Actor target, bool absolute = false) const; native void AddZ(double zadd, bool moving = true); native void SetZ(double z); - native vector2 Vec2To(Actor other); - native vector3 Vec3To(Actor other); - native vector3 Vec3Offset(double x, double y, double z, bool absolute = false); - native vector3 Vec3Angle(double length, double angle, double z = 0, bool absolute = false); - native vector2 Vec2Angle(double length, double angle, bool absolute = false); - native vector2 Vec2Offset(double x, double y, bool absolute = false); - native vector3 Vec2OffsetZ(double x, double y, double atz, bool absolute = false); - native void VelFromAngle(double speed = 0, double angle = 0); - native void Vel3DFromAngle(double speed, double angle, double pitch); + native vector2 Vec2To(Actor other) const; + native vector3 Vec3To(Actor other) const; + native vector3 Vec3Offset(double x, double y, double z, bool absolute = false) const; + native vector3 Vec3Angle(double length, double angle, double z = 0, bool absolute = false) const; + native vector2 Vec2Angle(double length, double angle, bool absolute = false) const; + native vector2 Vec2Offset(double x, double y, bool absolute = false) const; + native vector3 Vec2OffsetZ(double x, double y, double atz, bool absolute = false) const; + native void VelFromAngle(double speed = 0, double angle = 0) const; + native void Vel3DFromAngle(double speed, double angle, double pitch) const; native void Thrust(double speed = 0, double angle = 0); - native bool isFriend(Actor other); - native bool isHostile(Actor other); + native bool isFriend(Actor other) const; + native bool isHostile(Actor other) const; native void AdjustFloorClip(); native DropItem GetDropItems(); native void CopyFriendliness (Actor other, bool changeTarget, bool resetHealth = true); @@ -588,8 +588,8 @@ class Actor : Thinker native native void Howl(); native void DrawSplash (int count, double angle, int kind); native void GiveSecret(bool printmsg = true, bool playsound = true); - native double GetCameraHeight(); - native double GetGravity(); + native double GetCameraHeight() const; + native double GetGravity() const; native bool CheckClass(class checkclass, int ptr_select = AAPTR_DEFAULT, bool match_superclass = false); native void AddInventory(Inventory inv); @@ -613,7 +613,7 @@ class Actor : Thinker native native double GetDistance(bool checkz, int ptr = AAPTR_TARGET); native double GetAngle(int flags, int ptr = AAPTR_TARGET); native double GetZAt(double px = 0, double py = 0, double angle = 0, int flags = 0, int pick_pointer = AAPTR_DEFAULT); - native int GetSpawnHealth(); + native int GetSpawnHealth() const; native double GetCrouchFactor(int ptr = AAPTR_PLAYER1); native double GetCVar(string cvar); native int GetPlayerInput(int inputnum, int ptr = AAPTR_DEFAULT);