Skip to content

Instantly share code, notes, and snippets.

@RudolfVonKrugstein
RudolfVonKrugstein / gist:3086169
Created July 10, 2012 20:53
Hello World with UHC javascript backend
type JSString = PackedString
foreign import prim "primStringToPackedString" toJS :: String -> JSString
data Document
foreign import js "document" getDocument :: IO Document
foreign import js "%1.write(%2)" write :: Document -> JSString -> IO ()
main = do
doc <- getDocument
write doc $ toJS "Hello, World!"
@RudolfVonKrugstein
RudolfVonKrugstein / gist:3086352
Created July 10, 2012 21:28
HelloWorld JavaScript helper for haste
function objWrite(obj, text, _realWorld) {
obj.write(text); // Call the method
return [1,0]; // Return ()
}
function getDocument(_realWorld) {
return [1, 0, document];
}
@RudolfVonKrugstein
RudolfVonKrugstein / gist:3086389
Created July 10, 2012 21:34
Hello World with haste
import Haste
import Haste.Prim
type Document = JSAny
foreign import ccall "objWrite" write :: Document -> JSString -> IO ()
foreign import ccall "getDocument" getDocument :: IO Document
main = do
doc <- getDocument
<!DOCTYPE html><html><head><title>Main</title>
<script type="text/javascript" src="Main.js"></script>
</head>
<body>
</body>
</html>
@RudolfVonKrugstein
RudolfVonKrugstein / gist:3122201
Created July 16, 2012 11:20
Callbacks in haste
function jsSetInterval(msecs, cb, _) {
window.setInterval(function() {A(cb,[0]);}, msecs);
return [1,0];
}
function jsSetOnKeyDown(cb, _) {
window.addEventListener('keydown', function(e) {A(cb,[[1,parseInt(e.keyCode)],0]);}, true);
return [1,0];
}
import UHC.Ptr
foreign import js "wrapper" mkCb :: IO () -> IO (FunPtr (IO ())
@RudolfVonKrugstein
RudolfVonKrugstein / gist:3122457
Created July 16, 2012 12:38
UHC wrapper with key events
data JSKeyEvent
foreign import js "wrapper"
mkKeyEventCb :: (JSKeyEvent -> IO ()) -> IO (FunPtr (JSKeyEvent -> IO ()))
foreign import js "%1.keyCode"
keyCode :: JSKeyEvent -> IO Int
foreign import js "window.addEventListener('keydown',%1,true)"
jsSetOnKeyDown :: FunPtr (JSKeyEvent -> IO ()) -> IO ()
setOnKeyDown (Int -> IO ()) -> IO ()
setOnKeyDown fp = dp
cb <- mkKeyEventCb fp'
jsSetOnKeyDown cb
@RudolfVonKrugstein
RudolfVonKrugstein / gist:3122467
Created July 16, 2012 12:40
haste foreign imports
foreign import ccall jsSetInterval :: Int -> Callback a -> IO ()
setInterval :: Int -> IO () -> IO ()
setInterval time cb =
  jsSetInterval time (mkCallback $! cb)
foreign import ccall jsSetOnKeyDown :: Callback a -> IO ()
foreign import ccall jsSetOnKeyUp :: Callback a -> IO ()
setOnKeyDown :: (Int -> IO ()) -> IO ()
setOnKeyDown cb =
@RudolfVonKrugstein
RudolfVonKrugstein / gist:3122472
Created July 16, 2012 12:41
UHC JavaScript globalObjects
var allObjects = {}
function saveObject(name, obj) {
allObjects[name] = obj;
}
function loadObject(name) {
return allObjects[name];
}