Skip to content

Commit

Permalink
Automatic now handles lists and tuples. Works for Array <=> Automatic…
Browse files Browse the repository at this point in the history
… a, Automatic [a], and Automatic (,) (fixes #251)
  • Loading branch information
bergmark committed Jul 27, 2013
1 parent 25cc9c6 commit 73b31d4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
2 changes: 2 additions & 0 deletions fay.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ extra-source-files:
tests/Api/Records.hs
tests/asPatternMatch.hs
tests/asPatternMatch.res
tests/AutomaticList.hs
tests/AutomaticList.res
tests/baseFixities.hs
tests/baseFixities.res
tests/basicFunctions.hs
Expand Down
24 changes: 21 additions & 3 deletions js/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ function Fay$$fayToJs(type,fayObj){
fayObj = Fay$$_(fayObj.cdr);
}
jsObj = arr;

}
else if(base == "tuple") {
// Serialize Fay tuple to JavaScript array.
Expand Down Expand Up @@ -240,8 +239,20 @@ function Fay$$fayToJs(type,fayObj){
else if(base == "automatic" || base == "user") {
if(fayObj instanceof Fay$$$)
fayObj = Fay$$_(fayObj);
var fayToJsFun = Fay$$fayToJsHash[fayObj.constructor.name];
jsObj = fayToJsFun ? fayToJsFun(type,type[2],fayObj) : fayObj;

if(fayObj instanceof Fay$$Cons){
// Serialize Fay list to JavaScript array.
var arr = [];
fayObj = Fay$$_(fayObj);
while(fayObj instanceof Fay$$Cons) {
arr.push(Fay$$fayToJs(["automatic"],fayObj.car));
fayObj = Fay$$_(fayObj.cdr);
}
jsObj = arr;
} else {
var fayToJsFun = Fay$$fayToJsHash[fayObj.constructor.name];
jsObj = fayToJsFun ? fayToJsFun(type,type[2],fayObj) : fayObj;
}
}
else
throw new Error("Unhandled Fay->JS translation type: " + base);
Expand Down Expand Up @@ -380,6 +391,13 @@ function Fay$$jsToFay(type,jsObj){
var jsToFayFun = Fay$$jsToFayHash[jsObj["instance"]];
fayObj = jsToFayFun ? jsToFayFun(type,type[2],jsObj) : jsObj;
}
else if (jsObj instanceof Array) {
var list = null;
for (var i = jsObj.length - 1; i >= 0; i--) {
list = new Fay$$Cons(Fay$$jsToFay([base], jsObj[i]), list);
}
fayObj = list;
}
else
fayObj = jsObj;

Expand Down
36 changes: 36 additions & 0 deletions tests/AutomaticList.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module AutomaticList where

import Prelude
import FFI

main :: Fay ()
main = do
printA [1,2,3]
printA [[1],[2],[3]]
printA (1,2)
printA ([1],[2])
printArr [1,2,3]
printArr [[1],[2],[3]]
printT (1,2)
printT ([1],[2])
printA . tail $ readA "[1,2,3]"
printA . tail $ readArr "[1,2,3]"
printA . snd $ readT "[1,2]"

printA :: Automatic a -> Fay ()
printA = ffi "console.log(%1)"

printArr :: Automatic [a] -> Fay ()
printArr = ffi "console.log(%1)"

printT :: (Automatic a, Automatic b) -> Fay ()
printT = ffi "console.log(%1)"

readA :: String -> Automatic a
readA = ffi "JSON.parse(%1)"

readArr :: String -> Automatic [a]
readArr = ffi "JSON.parse(%1)"

readT :: String -> Automatic (a,b)
readT = ffi "JSON.parse(%1)"
11 changes: 11 additions & 0 deletions tests/AutomaticList.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ 1, 2, 3 ]
[ [ 1 ], [ 2 ], [ 3 ] ]
[ 1, 2 ]
[ [ 1 ], [ 2 ] ]
[ 1, 2, 3 ]
[ [ 1 ], [ 2 ], [ 3 ] ]
[ 1, 2 ]
[ [ 1 ], [ 2 ] ]
[ 2, 3 ]
[ 2, 3 ]
2
11 changes: 4 additions & 7 deletions tests/asPatternMatch.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{ car: { car: 1, cdr: { car: 2, cdr: [Object] } },
cdr: { car: { car: 1, cdr: [Object] }, cdr: null } }
{ car: { car: 1, cdr: { car: 2, cdr: [Object] } },
cdr: { car: 1, cdr: { car: [Object], cdr: null } } }
{ car: { car: 1, cdr: { car: 2, cdr: [Object] } },
cdr: { car: 1, cdr: { car: [Object], cdr: null } } }
{ car: 'o', cdr: { car: 'k', cdr: null } }
[ [ 1, 2, 3 ], [ 1, 2, 3 ] ]
[ [ 1, 2, 3 ], 1, [ 2, 3 ] ]
[ [ 1, 2, 3 ], 1, [ 2, 3 ] ]
[ 'o', 'k' ]

0 comments on commit 73b31d4

Please sign in to comment.