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 1, 2013
1 parent 13d6e3a commit c2b2221
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
25 changes: 23 additions & 2 deletions js/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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 @@ -228,14 +227,29 @@ function Fay$$fayToJs(type,fayObj){
else if(base == "automatic" || base == "user") {
if(fayObj instanceof Fay$$$)
fayObj = Fay$$_(fayObj);
jsObj = Fay$$fayToJsUserDefined(type,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 {
jsObj = Fay$$fayToJsUserDefined(type,fayObj);
}
}
else
throw new Error("Unhandled Fay->JS translation type: " + base);
return jsObj;
}

function listToArr(args, fayObj){

}

// Specialized serializer for string.
function Fay$$fayToJs_string(fayObj){
// Serialize Fay string to JavaScript string.
Expand Down Expand Up @@ -363,6 +377,13 @@ function Fay$$jsToFay(type,jsObj){
if (jsObj && jsObj['instance']) {
fayObj = Fay$$jsToFayUserDefined(type,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
11 changes: 11 additions & 0 deletions tests/AutomaticList
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
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: 4 additions & 7 deletions tests/asPatternMatch
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 c2b2221

Please sign in to comment.