on master help sign in
Merge pull request #1175 from elm-lang/pr/1164
b9f3244Evan Czaplicki
src/Generate/JavaScript/Port.hs+27−6
@@ -27,6 +27,7 @@ task name expr portType =
27
27
28data JSType
28data JSType
29 = JSNumber
29 = JSNumber
30+ | JSInt
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+ JSInt -> "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"
@@ -81,12 +83,31 @@ check x jsType continue =
81
83
82 checks =
84 checks =
83 case jsType of
85 case jsType of
86+ JSNumber ->
84− JSNumber -> [typeof "number"]
87+ [typeof "number"]
88+
89+ JSInt ->
90+ [jsFold OpLAnd intChecks]
91+
92+ JSBoolean ->
85− JSBoolean -> [typeof "boolean"]
93+ [typeof "boolean"]
94+
95+ JSString ->
86− JSString -> [typeof "string", instanceof "String"]
96+ [typeof "string", instanceof "String"]
97+
98+ JSArray ->
87− JSArray -> [instanceof "Array"]
99+ [instanceof "Array"]
100+
88 JSObject fields ->
101 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+ ]
90
111
91
112
92-- INBOUND
113-- INBOUND
@@ -127,8 +148,8 @@ toType tipe x =
127 toType (T.dealias args t) x
148 toType (T.dealias args t) x
128
149
129 T.Type (Var.Canonical Var.BuiltIn name)
150 T.Type (Var.Canonical Var.BuiltIn name)
130− | name == "Int" -> from JSNumber
131 | name == "Float" -> from JSNumber
151 | name == "Float" -> from JSNumber
152+ | name == "Int" -> from JSInt
132 | name == "Bool" -> from JSBoolean
153 | name == "Bool" -> from JSBoolean
133 | name == "String" -> from JSString
154 | name == "String" -> from JSString
134 where
155 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