on master help sign in
Merge pull request #1175 from elm-lang/pr/1164

Pr/1164

commitb9f324409d79c34a4981f3cd4ca4ebe431c66dfd authorEvan Czaplicki <info@elm-lang.org> dateTue 17 Nov 2015, 11:50 (11 years ago) parent433e20c45a823f
3 files changed+31−9
src/Generate/JavaScript/Port.hs+27−6 tests/test-files/good-expected-js/Declarations/Ports.elm.js+3−2 tests/test-files/good/Declarations/Ports.elm+1−1
src/Generate/JavaScript/Port.hs+27−6
@@ -27,6 +27,7 @@ task name expr portType =
2727
2828data JSType
2929 = JSNumber
30+ | JSInt
3031 | JSBoolean
3132 | JSString
3233 | JSArray
@@ -37,6 +38,7 @@ typeToString :: JSType -> String
3738typeToString tipe =
3839 case tipe of
3940 JSNumber -> "a number"
41+ JSInt -> "an integer"
4042 JSBoolean -> "a boolean (true or false)"
4143 JSString -> "a string"
4244 JSArray -> "an array"
@@ -81,12 +83,31 @@ check x jsType continue =
8183
8284 checks =
8385 case jsType of
84− JSNumber -> [typeof "number"]
85− JSBoolean -> [typeof "boolean"]
86− JSString -> [typeof "string", instanceof "String"]
87− JSArray -> [instanceof "Array"]
86+ JSNumber ->
87+ [typeof "number"]
88+
89+ JSInt ->
90+ [jsFold OpLAnd intChecks]
91+
92+ JSBoolean ->
93+ [typeof "boolean"]
94+
95+ JSString ->
96+ [typeof "string", instanceof "String"]
97+
98+ JSArray ->
99+ [instanceof "Array"]
100+
88101 JSObject fields ->
89− [jsFold OpLAnd (typeof "object" : map member fields)]
102+ [jsFold OpLAnd (typeof "object" : map member fields)]
103+
104+
105+intChecks :: [Expression () -> Expression ()]
106+intChecks =
107+ [ typeof "number"
108+ , \x -> ref "isFinite" <| x
109+ , \x -> equal (obj ["Math","floor"] <| x) x
110+ ]
90111
91112
92113-- INBOUND
@@ -127,8 +148,8 @@ toType tipe x =
127148 toType (T.dealias args t) x
128149
129150 T.Type (Var.Canonical Var.BuiltIn name)
130− | name == "Int" -> from JSNumber
131151 | name == "Float" -> from JSNumber
152+ | name == "Int" -> from JSInt
132153 | name == "Bool" -> from JSBoolean
133154 | name == "String" -> from JSString
134155 where
tests/test-files/good-expected-js/Declarations/Ports.elm.js+3−2
@@ -31,7 +31,7 @@ Elm.Main.make = function (_elm) {
3131 v.y)} : _U.badPort("an object with fields `x`, `y`",v);
3232 });
3333 var array = Elm.Native.Port.make(_elm).inbound("array",
34− "List Int",
34+ "List Float",
3535 function (v) {
3636 return typeof v === "object" && v instanceof Array ? Elm.Native.List.make(_elm).fromArray(v.map(function (v) {
3737 return typeof v === "number" ? v : _U.badPort("a number",v);
@@ -49,7 +49,8 @@ Elm.Main.make = function (_elm) {
4949 var number = Elm.Native.Port.make(_elm).inbound("number",
5050 "Int",
5151 function (v) {
52− return typeof v === "number" ? v : _U.badPort("a number",v);
52+ return typeof v === "number" && isFinite(v) && Math.floor(v) === v ? v : _U.badPort("an integer",
53+ v);
5354 });
5455 var userID = Elm.Native.Port.make(_elm).inbound("userID",
5556 "String",
tests/test-files/good/Declarations/Ports.elm+1−1
@@ -3,7 +3,7 @@
33port userID : String
44port number : Int
55port tuple : (Float,Bool)
6−port array : List Int
6+port array : List Float
77port record : { x:Float, y:Float }
88
99-- outgoing