Skip to content

Commit 73b31d4

Browse files
committed
Automatic now handles lists and tuples. Works for Array <=> Automatic a, Automatic [a], and Automatic (,) (fixes #251)
1 parent 25cc9c6 commit 73b31d4

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

fay.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extra-source-files:
4747
tests/Api/Records.hs
4848
tests/asPatternMatch.hs
4949
tests/asPatternMatch.res
50+
tests/AutomaticList.hs
51+
tests/AutomaticList.res
5052
tests/baseFixities.hs
5153
tests/baseFixities.res
5254
tests/basicFunctions.hs

js/runtime.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ function Fay$$fayToJs(type,fayObj){
198198
fayObj = Fay$$_(fayObj.cdr);
199199
}
200200
jsObj = arr;
201-
202201
}
203202
else if(base == "tuple") {
204203
// Serialize Fay tuple to JavaScript array.
@@ -240,8 +239,20 @@ function Fay$$fayToJs(type,fayObj){
240239
else if(base == "automatic" || base == "user") {
241240
if(fayObj instanceof Fay$$$)
242241
fayObj = Fay$$_(fayObj);
243-
var fayToJsFun = Fay$$fayToJsHash[fayObj.constructor.name];
244-
jsObj = fayToJsFun ? fayToJsFun(type,type[2],fayObj) : fayObj;
242+
243+
if(fayObj instanceof Fay$$Cons){
244+
// Serialize Fay list to JavaScript array.
245+
var arr = [];
246+
fayObj = Fay$$_(fayObj);
247+
while(fayObj instanceof Fay$$Cons) {
248+
arr.push(Fay$$fayToJs(["automatic"],fayObj.car));
249+
fayObj = Fay$$_(fayObj.cdr);
250+
}
251+
jsObj = arr;
252+
} else {
253+
var fayToJsFun = Fay$$fayToJsHash[fayObj.constructor.name];
254+
jsObj = fayToJsFun ? fayToJsFun(type,type[2],fayObj) : fayObj;
255+
}
245256
}
246257
else
247258
throw new Error("Unhandled Fay->JS translation type: " + base);
@@ -380,6 +391,13 @@ function Fay$$jsToFay(type,jsObj){
380391
var jsToFayFun = Fay$$jsToFayHash[jsObj["instance"]];
381392
fayObj = jsToFayFun ? jsToFayFun(type,type[2],jsObj) : jsObj;
382393
}
394+
else if (jsObj instanceof Array) {
395+
var list = null;
396+
for (var i = jsObj.length - 1; i >= 0; i--) {
397+
list = new Fay$$Cons(Fay$$jsToFay([base], jsObj[i]), list);
398+
}
399+
fayObj = list;
400+
}
383401
else
384402
fayObj = jsObj;
385403

tests/AutomaticList.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module AutomaticList where
2+
3+
import Prelude
4+
import FFI
5+
6+
main :: Fay ()
7+
main = do
8+
printA [1,2,3]
9+
printA [[1],[2],[3]]
10+
printA (1,2)
11+
printA ([1],[2])
12+
printArr [1,2,3]
13+
printArr [[1],[2],[3]]
14+
printT (1,2)
15+
printT ([1],[2])
16+
printA . tail $ readA "[1,2,3]"
17+
printA . tail $ readArr "[1,2,3]"
18+
printA . snd $ readT "[1,2]"
19+
20+
printA :: Automatic a -> Fay ()
21+
printA = ffi "console.log(%1)"
22+
23+
printArr :: Automatic [a] -> Fay ()
24+
printArr = ffi "console.log(%1)"
25+
26+
printT :: (Automatic a, Automatic b) -> Fay ()
27+
printT = ffi "console.log(%1)"
28+
29+
readA :: String -> Automatic a
30+
readA = ffi "JSON.parse(%1)"
31+
32+
readArr :: String -> Automatic [a]
33+
readArr = ffi "JSON.parse(%1)"
34+
35+
readT :: String -> Automatic (a,b)
36+
readT = ffi "JSON.parse(%1)"

tests/AutomaticList.res

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[ 1, 2, 3 ]
2+
[ [ 1 ], [ 2 ], [ 3 ] ]
3+
[ 1, 2 ]
4+
[ [ 1 ], [ 2 ] ]
5+
[ 1, 2, 3 ]
6+
[ [ 1 ], [ 2 ], [ 3 ] ]
7+
[ 1, 2 ]
8+
[ [ 1 ], [ 2 ] ]
9+
[ 2, 3 ]
10+
[ 2, 3 ]
11+
2

tests/asPatternMatch.res

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
{ car: { car: 1, cdr: { car: 2, cdr: [Object] } },
2-
cdr: { car: { car: 1, cdr: [Object] }, cdr: null } }
3-
{ car: { car: 1, cdr: { car: 2, cdr: [Object] } },
4-
cdr: { car: 1, cdr: { car: [Object], cdr: null } } }
5-
{ car: { car: 1, cdr: { car: 2, cdr: [Object] } },
6-
cdr: { car: 1, cdr: { car: [Object], cdr: null } } }
7-
{ car: 'o', cdr: { car: 'k', cdr: null } }
1+
[ [ 1, 2, 3 ], [ 1, 2, 3 ] ]
2+
[ [ 1, 2, 3 ], 1, [ 2, 3 ] ]
3+
[ [ 1, 2, 3 ], 1, [ 2, 3 ] ]
4+
[ 'o', 'k' ]

0 commit comments

Comments
 (0)