on master help sign in
javascript: WIP
d557267raichoo
jsrts/Runtime-common.js+2−4
@@ -30,16 +30,14 @@ var i$RESERVE = function(vm,n) {
30}
30}
31
31
32var __IDRRT__charCode = function(str) {
32var __IDRRT__charCode = function(str) {
33− var type = typeof str;
34− if (type == "string")
33+ if (typeof str == "string")
35 return str.charCodeAt(0);
34 return str.charCodeAt(0);
36 else
35 else
37 return str;
36 return str;
38}
37}
39
38
40var __IDRRT__fromCharCode = function(chr) {
39var __IDRRT__fromCharCode = function(chr) {
41− var type = typeof chr;
42− if (type == "string")
40+ if (typeof chr == "string")
43 return chr;
41 return chr;
44 else
42 else
45 return String.fromCharCode(chr);
43 return String.fromCharCode(chr);
src/IRTS/CodegenJavaScript.hs+18−16
@@ -1,4 +1,4 @@
1−{-# LANGUAGE PatternGuards #-}
1+{-# LANGUAGE PatternGuards #-}
2module IRTS.CodegenJavaScript (codegenJavaScript, codegenNode, JSTarget(..)) where
2module IRTS.CodegenJavaScript (codegenJavaScript, codegenNode, JSTarget(..)) where
3
3
4import Idris.AbsSyntax hiding (TypeCase)
4import Idris.AbsSyntax hiding (TypeCase)
@@ -582,7 +582,9 @@ jsIsNull :: JS -> JS
582jsIsNull js = JSBinOp "==" js JSNull
582jsIsNull js = JSBinOp "==" js JSNull
583
583
584jsBigInt :: JS -> JS
584jsBigInt :: JS -> JS
585+jsBigInt (JSString "0") = JSNum (JSInteger JSBigZero)
586+jsBigInt (JSString "1") = JSNum (JSInteger JSBigOne)
585−jsBigInt = JSNum . JSInteger . JSBigIntExpr
587+jsBigInt js = JSNum $ JSInteger $ JSBigIntExpr js
586
588
587jsUnPackBits :: JS -> JS
589jsUnPackBits :: JS -> JS
588jsUnPackBits js = JSIndex js $ JSNum (JSInt 0)
590jsUnPackBits js = JSIndex js $ JSNum (JSInt 0)
@@ -606,32 +608,32 @@ jsPackSBits16 js = JSNew "Int16Array" [JSArray [js]]
606jsPackSBits32 :: JS -> JS
608jsPackSBits32 :: JS -> JS
607jsPackSBits32 js = JSNew "Int32Array" [JSArray [js]]
609jsPackSBits32 js = JSNew "Int32Array" [JSArray [js]]
608
610
609−jsTAG :: JS -> JS
610−jsTAG js =
611− JSTernary (jsIsNumber js `jsOr` jsIsNull js) (
612− JSNum (JSInt $ negate 1)
613− ) (JSTernary (js `jsInstanceOf` "i$CON") (
614− JSProj js "tag"
615− ) (JSNum (JSInt $ negate 1)))
616−
617−jsCTAG :: JS -> JS
618−jsCTAG js = JSProj js "tag"
619−
620-- TODO unsafe
611-- TODO unsafe
621jsCASE :: Bool -> Reg -> [(Int, [BC])] -> Maybe [BC] -> JS
612jsCASE :: Bool -> Reg -> [(Int, [BC])] -> Maybe [BC] -> JS
622jsCASE safe reg cases def =
613jsCASE safe reg cases def =
623− {-JSSwitch (tag safe (translateReg reg)) (-}
614+ JSSwitch (tag safe $ translateReg reg) (
624− JSSwitch (JSTernary (translateReg reg `jsInstanceOf` "i$CON") (
625− JSProj (translateReg reg) "tag") (JSNum (JSInt $ negate 1))) (
626 map ((JSNum . JSInt) *** prepBranch) cases
615 map ((JSNum . JSInt) *** prepBranch) cases
627 ) (fmap prepBranch def)
616 ) (fmap prepBranch def)
628 where
617 where
629 tag :: Bool -> JS -> JS
618 tag :: Bool -> JS -> JS
630 tag True = jsCTAG
619 tag True = jsCTAG
631 tag False = jsTAG
620 tag False = jsTAG
621+
632 prepBranch :: [BC] -> JS
622 prepBranch :: [BC] -> JS
633 prepBranch bc = JSSeq $ map translateBC bc
623 prepBranch bc = JSSeq $ map translateBC bc
634
624
625+ jsTAG :: JS -> JS
626+ jsTAG js =
627+ JSTernary (jsIsNumber js `jsOr` jsIsNull js) (
628+ JSNum (JSInt $ negate 1)
629+ ) (JSTernary (js `jsInstanceOf` "i$CON") (
630+ JSProj js "tag"
631+ ) (JSNum (JSInt $ negate 1)))
632+
633+ jsCTAG :: JS -> JS
634+ jsCTAG js = JSProj js "tag"
635+
636+
635jsCONSTCASE :: Reg -> [(Const, [BC])] -> Maybe [BC] -> JS
637jsCONSTCASE :: Reg -> [(Const, [BC])] -> Maybe [BC] -> JS
636jsCONSTCASE reg cases def =
638jsCONSTCASE reg cases def =
637 JSCond $ (
639 JSCond $ (