on master help sign in
Disallow non-integer numbers to be passed to Int ports
3b5542dJan Dudek
src/Generate/JavaScript/Port.hs+10−1
@@ -27,6 +27,7 @@ task name expr portType =
27
27
28data JSType
28data JSType
29 = JSNumber
29 = JSNumber
30+ | JSIntNumber
30 | JSBoolean
31 | JSBoolean
31 | JSString
32 | JSString
32 | JSArray
33 | JSArray
@@ -37,6 +38,7 @@ typeToString :: JSType -> String
37typeToString tipe =
38typeToString tipe =
38 case tipe of
39 case tipe of
39 JSNumber -> "a number"
40 JSNumber -> "a number"
41+ JSIntNumber -> "an integer"
40 JSBoolean -> "a boolean (true or false)"
42 JSBoolean -> "a boolean (true or false)"
41 JSString -> "a string"
43 JSString -> "a string"
42 JSArray -> "an array"
44 JSArray -> "an array"
@@ -82,12 +84,19 @@ check x jsType continue =
82 checks =
84 checks =
83 case jsType of
85 case jsType of
84 JSNumber -> [typeof "number"]
86 JSNumber -> [typeof "number"]
87+ JSIntNumber ->
88+ [jsFold OpLAnd (typeof "number" : intChecks)]
85 JSBoolean -> [typeof "boolean"]
89 JSBoolean -> [typeof "boolean"]
86 JSString -> [typeof "string", instanceof "String"]
90 JSString -> [typeof "string", instanceof "String"]
87 JSArray -> [instanceof "Array"]
91 JSArray -> [instanceof "Array"]
88 JSObject fields ->
92 JSObject fields ->
89 [jsFold OpLAnd (typeof "object" : map member fields)]
93 [jsFold OpLAnd (typeof "object" : map member fields)]
90
94
95+ intChecks =
96+ [ \x -> (ref "isFinite") `call` [x]
97+ , \x -> equal (obj ["Math","floor"] `call` [x]) x
98+ ]
99+
91
100
92-- INBOUND
101-- INBOUND
93
102
@@ -127,8 +136,8 @@ toType tipe x =
127 toType (T.dealias args t) x
136 toType (T.dealias args t) x
128
137
129 T.Type (Var.Canonical Var.BuiltIn name)
138 T.Type (Var.Canonical Var.BuiltIn name)
130− | name == "Int" -> from JSNumber
131 | name == "Float" -> from JSNumber
139 | name == "Float" -> from JSNumber
140+ | name == "Int" -> from JSIntNumber
132 | name == "Bool" -> from JSBoolean
141 | name == "Bool" -> from JSBoolean
133 | name == "String" -> from JSString
142 | name == "String" -> from JSString
134 where
143 where
tests/test-files/good-expected-js/Declarations/Ports.elm.js+3−2
@@ -31,7 +31,7 @@ Elm.Main.make = function (_elm) {
31 v.y)} : _U.badPort("an object with fields `x`, `y`",v);
31 v.y)} : _U.badPort("an object with fields `x`, `y`",v);
32 });
32 });
33 var array = Elm.Native.Port.make(_elm).inbound("array",
33 var array = Elm.Native.Port.make(_elm).inbound("array",
34− "List Int",
34+ "List Float",
35 function (v) {
35 function (v) {
36 return typeof v === "object" && v instanceof Array ? Elm.Native.List.make(_elm).fromArray(v.map(function (v) {
36 return typeof v === "object" && v instanceof Array ? Elm.Native.List.make(_elm).fromArray(v.map(function (v) {
37 return typeof v === "number" ? v : _U.badPort("a number",v);
37 return typeof v === "number" ? v : _U.badPort("a number",v);
@@ -49,7 +49,8 @@ Elm.Main.make = function (_elm) {
49 var number = Elm.Native.Port.make(_elm).inbound("number",
49 var number = Elm.Native.Port.make(_elm).inbound("number",
50 "Int",
50 "Int",
51 function (v) {
51 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);
53 });
54 });
54 var userID = Elm.Native.Port.make(_elm).inbound("userID",
55 var userID = Elm.Native.Port.make(_elm).inbound("userID",
55 "String",
56 "String",
tests/test-files/good/Declarations/Ports.elm+1−1
@@ -3,7 +3,7 @@
3port userID : String
3port userID : String
4port number : Int
4port number : Int
5port tuple : (Float,Bool)
5port tuple : (Float,Bool)
6−port array : List Int
6+port array : List Float
7port record : { x:Float, y:Float }
7port record : { x:Float, y:Float }
8
8
9-- outgoing
9-- outgoing