on master help sign in
javascript: avoid name capture
803dd59raichoo
src/IRTS/CodegenJavaScript.hs+8−89
@@ -294,9 +294,11 @@ codegenJS_all target definitions includes filename outputType = do
294 main = compileJS $
294 main = compileJS $
295 JSAlloc "main" $ Just $ JSFunction [] (
295 JSAlloc "main" $ Just $ JSFunction [] (
296 JSSeq [ JSAlloc "vm" (Just $ JSNew "i$VM" [])
296 JSSeq [ JSAlloc "vm" (Just $ JSNew "i$VM" [])
297+ , JSApp (
298+ JSIdent (translateName (sMN 0 "runMain"))
297− , JSApp (JSIdent "mrunMain0") [ JSIdent "vm"
299+ ) [ JSIdent "vm"
298− , JSNum (JSInt 0)
300+ , JSNum (JSInt 0)
299− ]
301+ ]
300 ]
302 ]
301 )
303 )
302
304
@@ -387,92 +389,11 @@ translateChar ch
387 '\BS', '\HT', '\LF', '\VT', '\FF', '\CR', '\SO', '\SI',
389 '\BS', '\HT', '\LF', '\VT', '\FF', '\CR', '\SO', '\SI',
388 '\DLE', '\DC1', '\DC2', '\DC3', '\DC4', '\NAK', '\SYN', '\ETB',
390 '\DLE', '\DC1', '\DC2', '\DC3', '\DC4', '\NAK', '\SYN', '\ETB',
389 '\CAN', '\EM', '\SUB', '\ESC', '\FS', '\GS', '\RS', '\US']
391 '\CAN', '\EM', '\SUB', '\ESC', '\FS', '\GS', '\RS', '\US']
390−translateIdentifier :: String -> String
391−translateIdentifier =
392− replaceReserved . concatMap replaceBadChars
393− where replaceBadChars :: Char -> String
394− replaceBadChars c
395− | ' ' <- c = "_"
396− | '_' <- c = "__"
397− | isDigit c = '_' : show (ord c)
398− | not (isLetter c && isAscii c) = '_' : show (ord c)
399− | otherwise = [c]
400− replaceReserved s
401− | s `elem` reserved = '_' : s
402− | otherwise = s
403−
404−
405− reserved = [ "break"
406− , "case"
407− , "catch"
408− , "continue"
409− , "debugger"
410− , "default"
411− , "delete"
412− , "do"
413− , "else"
414− , "finally"
415− , "for"
416− , "function"
417− , "if"
418− , "in"
419− , "instanceof"
420− , "new"
421− , "return"
422− , "switch"
423− , "this"
424− , "throw"
425− , "try"
426− , "typeof"
427− , "var"
428− , "void"
429− , "while"
430− , "with"
431−
432− , "class"
433− , "enum"
434− , "export"
435− , "extends"
436− , "import"
437− , "super"
438−
439− , "implements"
440− , "interface"
441− , "let"
442− , "package"
443− , "private"
444− , "protected"
445− , "public"
446− , "static"
447− , "yield"
448− ]
449−
450−translateNamespace :: Name -> String
451−translateNamespace (UN _) = idrNamespace
452−translateNamespace (NS _ ns) = idrNamespace ++ concatMap (translateIdentifier . str) ns
453−translateNamespace (MN _ _) = idrNamespace
454−translateNamespace (SN name) = idrNamespace ++ translateSpecialName name
455−translateNamespace NErased = idrNamespace
456
392
457translateName :: Name -> String
393translateName :: Name -> String
458−translateName (UN name) = 'u' : translateIdentifier (str name)
394+translateName n = "_idris_" ++ concatMap cchar (showCG n)
459−translateName (NS name _) = 'n' : translateName name
395+ where cchar x | isAlpha x || isDigit x = [x]
460−translateName (MN i name) = 'm' : translateIdentifier (str name) ++ show i
396+ | otherwise = "_" ++ show (fromEnum x) ++ "_"
461−translateName (SN name) = 's' : translateSpecialName name
462−translateName NErased = "e"
463−
464−translateSpecialName :: SpecialName -> String
465−translateSpecialName name
466− | WhereN i m n <- name =
467− 'w' : translateName m ++ translateName n ++ show i
468− | InstanceN n s <- name =
469− 'i' : translateName n ++ concatMap (translateIdentifier . str) s
470− | ParentN n s <- name =
471− 'p' : translateName n ++ translateIdentifier (str s)
472− | MethodN n <- name =
473− 'm' : translateName n
474− | CaseN n <- name =
475− 'c' : translateName n
476
397
477jsASSIGN :: Reg -> Reg -> JS
398jsASSIGN :: Reg -> Reg -> JS
478jsASSIGN r1 r2 = JSAssign (translateReg r1) (translateReg r2)
399jsASSIGN r1 r2 = JSAssign (translateReg r1) (translateReg r2)
@@ -589,7 +510,6 @@ jsBigInt js = JSNum $ JSInteger $ JSBigIntExpr js
589jsUnPackBits :: JS -> JS
510jsUnPackBits :: JS -> JS
590jsUnPackBits js = JSIndex js $ JSNum (JSInt 0)
511jsUnPackBits js = JSIndex js $ JSNum (JSInt 0)
591
512
592−
593jsPackUBits8 :: JS -> JS
513jsPackUBits8 :: JS -> JS
594jsPackUBits8 js = JSNew "Uint8Array" [JSArray [js]]
514jsPackUBits8 js = JSNew "Uint8Array" [JSArray [js]]
595
515
@@ -608,7 +528,6 @@ jsPackSBits16 js = JSNew "Int16Array" [JSArray [js]]
608jsPackSBits32 :: JS -> JS
528jsPackSBits32 :: JS -> JS
609jsPackSBits32 js = JSNew "Int32Array" [JSArray [js]]
529jsPackSBits32 js = JSNew "Int32Array" [JSArray [js]]
610
530
611−-- TODO unsafe
612jsCASE :: Bool -> Reg -> [(Int, [BC])] -> Maybe [BC] -> JS
531jsCASE :: Bool -> Reg -> [(Int, [BC])] -> Maybe [BC] -> JS
613jsCASE safe reg cases def =
532jsCASE safe reg cases def =
614 JSSwitch (tag safe $ translateReg reg) (
533 JSSwitch (tag safe $ translateReg reg) (