Skip to content

Commit

Permalink
Implement sign()
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Feb 5, 2025
1 parent 81b476a commit b7548e0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Content.Tests/DMProject/Tests/Builtins/sign.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/proc/RunTest()
ASSERT(sign(5.2) == 1)
ASSERT(sign(-5.2) == -1)
ASSERT(sign(0) == 0)
ASSERT(sign(null) == 0)
ASSERT(sign("") == 0)
ASSERT(sign("foo") == 0)
ASSERT(sign(list(1)) == 0)

1 change: 1 addition & 0 deletions DMCompiler/DMStandard/_Standard.dm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ proc/rgb(R, G, B, A, space) as text|null
proc/rgb2num(color, space = COLORSPACE_RGB) as /list
proc/roll(ndice = 1, sides) as num
proc/round(A, B) as num
proc/sign(A) as num
proc/sha1(input) as text|null
proc/shutdown(Addr,Natural = 0)
proc/sleep(Delay)
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_rgb2num);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_roll);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_round);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sign);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sha1);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_shutdown);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sleep);
Expand Down
16 changes: 16 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,22 @@ public static DreamValue NativeProc_roll(NativeProc.Bundle bundle, DreamObject?
return new DreamValue(total);
}

[DreamProc("sign")]
[DreamProcParameter("A", Type = DreamValueTypeFlag.Float)]
public static DreamValue NativeProc_sign(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
if (bundle.Arguments.Length != 1) throw new Exception($"expected 1 argument (found {bundle.Arguments.Length})");
DreamValue arg = bundle.GetArgument(0, "A");

// Any non-num returns 0
if (!arg.TryGetValueAsFloat(out var value)) return new DreamValue(0);

return value switch {
0 => new DreamValue(0),
< 0 => new DreamValue(-1),
_ => new DreamValue(1)
};
}

[DreamProc("sha1")]
[DreamProcParameter("T", Type = DreamValueTypeFlag.String | DreamValueTypeFlag.DreamResource)]
public static DreamValue NativeProc_sha1(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
Expand Down

0 comments on commit b7548e0

Please sign in to comment.