Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Added argument checking utility. Added Math class. Fixed stack overfl…
…ows when casting to a String.
- Loading branch information
James King
committed
Aug 6, 2013
1 parent
48372af
commit f900646
Showing
9 changed files
with
332 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| function Class:getName() | ||
| return self.__name | ||
| return String.new(self.__name) | ||
| end | ||
|
|
||
| function Class:toString() | ||
| return self:getName() | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --@import see.rt.RuntimeException | ||
| --@extends see.rt.RuntimeException | ||
|
|
||
| function InvalidArgumentException:init(n, expected, got) | ||
| if not isprimitive(expected) then | ||
| expected = exepected:getName() | ||
| end | ||
| if not isprimitive(got) then | ||
| got = got:getName() | ||
| end | ||
|
|
||
| RuntimeException.init(self, String.new("Invalid argument #", n, ". Expected ", expected, ", got ", got, ".")) | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --@import see.rt.InvalidArgumentException | ||
|
|
||
| --[[ | ||
| Ensures an argument is of the correct type. | ||
| @param number:n The argument being checked. | ||
| @param any:value The value being checked. | ||
| @param see.rt.Class|see.base.String:type The type to validate for. | ||
| ]] | ||
| function ArgumentUtils.check(n, value, type) | ||
| local actualType = typeof(value) | ||
| if actualType ~= type then | ||
| throw(InvalidArgumentException.new(n, type, actualType)) | ||
| end | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,158 @@ | ||
| function Math.sin(x) | ||
| -- body | ||
| --@native math | ||
| --@import see.util.ArgumentUtils | ||
|
|
||
| Math.HUGE = math.huge | ||
| Math.PI = math.pi | ||
|
|
||
| function Math.abs(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.abs(x) | ||
| end | ||
|
|
||
| function Math.acos(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.acos(x) | ||
| end | ||
|
|
||
| function Math.asin(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.asin(x) | ||
| end | ||
|
|
||
| function Math.atan(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.atan(x) | ||
| end | ||
|
|
||
| function Math.atan2(y, x) | ||
| ArgumentUtils.check(1, y, "number") | ||
| ArgumentUtils.check(2, x, "number") | ||
| return math.atan2(y, x) | ||
| end | ||
|
|
||
| function Math.ceil(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.ceil(x) | ||
| end | ||
|
|
||
| function Math.cos(x) | ||
| -- body | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.cos(x) | ||
| end | ||
|
|
||
| function Math.cosh(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.cosh(x) | ||
| end | ||
|
|
||
| function Math.deg(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.deg(x) | ||
| end | ||
|
|
||
| function Math.exp(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.exp(x) | ||
| end | ||
|
|
||
| function Math.floor(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.floor(x) | ||
| end | ||
|
|
||
| function Math.frexp(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.frexp(x) | ||
| end | ||
|
|
||
| function Math.ldexp(m, e) | ||
| ArgumentUtils.check(1, m, "number") | ||
| ArgumentUtils.check(2, e, "number") | ||
| return math.ldexp(m, e) | ||
| end | ||
|
|
||
| function Math.log(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.log(x) | ||
| end | ||
|
|
||
| function Math.log10(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.log10(x) | ||
| end | ||
|
|
||
| function Math.max(x, ...) | ||
| local args = {...} | ||
| ArgumentUtils.check(1, x, "number") | ||
| for i = 1, #args do | ||
| ArgumentUtils.check(i + 1, args[i], "number") | ||
| end | ||
| return math.max(x, ...) | ||
| end | ||
|
|
||
| function Math.min(x, ...) | ||
| local args = {...} | ||
| ArgumentUtils.check(1, x, "number") | ||
| for i = 1, #args do | ||
| ArgumentUtils.check(i + 1, args[i], "number") | ||
| end | ||
| return math.min(x, ...) | ||
| end | ||
|
|
||
| function Math.modf(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.modf(x) | ||
| end | ||
|
|
||
| function Math.pow(x, y) | ||
| ArgumentUtils.check(1, x, "number") | ||
| ArgumentUtils.check(2, y, "number") | ||
| return math.pow(x, y) | ||
| end | ||
|
|
||
| function Math.rad(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.rad(x) | ||
| end | ||
|
|
||
| function Math.random(m, n) | ||
| if m then | ||
| ArgumentUtils.check(1, m, "number") | ||
| if n then | ||
| ArgumentUtils.check(2, n, "number") | ||
| return math.random(m, n) | ||
| end | ||
| return math.random(m) | ||
| end | ||
| return math.random() | ||
| end | ||
|
|
||
| function Math.randomseed(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.randomseed(x) | ||
| end | ||
|
|
||
| function Math.sin(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.sin(x) | ||
| end | ||
|
|
||
| function Math.sinh(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.sinh(x) | ||
| end | ||
|
|
||
| function Math.sqrt(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.sqrt(x) | ||
| end | ||
|
|
||
| function Math.tan(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.tan(x) | ||
| end | ||
|
|
||
| function Math.tan( ... ) | ||
| -- body | ||
| function Math.tanh(x) | ||
| ArgumentUtils.check(1, x, "number") | ||
| return math.tanh(x) | ||
| end |
Oops, something went wrong.