@@ -52,8 +52,8 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
5252 [ JSVariableIntroduction name Nothing
5353 , JSApp (JSFunction Nothing [name]
5454 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (sortBy typeClassesLast decls))))
55− [JSAssignment (JSAssignVariable name)
56− (JSBinary Or (JSVar name) (JSObjectLiteral []))]
55+ [JSAssignment (JSVar name)
56+ (JSBinary Or (JSVar name) (JSObjectLiteral []))]
5757 ]
5858 where
5959 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) (ExternDeclaration TypeClassDictionaryImport _ _ _) = EQ
@@ -66,12 +66,12 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
6666--
6767declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
6868declToJs opts mp (ValueDeclaration ident _ _ val) e =
69− Just [ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val)),
70− setProperty (identToJs ident) (var ident) mp ]
69+ Just $ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
70+ : setProperty ident (var ident) mp
7171declToJs opts mp (BindingGroupDeclaration vals) e =
7272 Just $ concatMap (\(ident, val) ->
73− [ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val)),
74− setProperty (identToJs ident) (var ident) mp ]
73+ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
74+ : setProperty ident (var ident) mp
7575 ) vals
7676declToJs _ mp (DataDeclaration _ _ ctors) _ =
7777 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), maybeTy) ->
@@ -83,19 +83,22 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
8383 (JSBlock [JSReturn
8484 (JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn)))
8585 , ("value", JSVar "value") ])])
86− in [ ctorJs, setProperty ctor (JSVar ctor) mp ]
86+ in ctorJs : setProperty (Escaped ctor) (JSVar ctor) mp
8787declToJs opts mp (DataBindingGroupDeclaration ds) e =
8888 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
8989declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
90− Just [ js
91− , setProperty (identToJs ident) (var ident) mp ]
90+ Just $ js : setProperty ident (var ident) mp
9291declToJs _ _ _ _ = Nothing
9392
94−setProperty :: String -> JS -> ModuleName -> JS
95−setProperty prop val (ModuleName (ProperName moduleName)) = JSAssignment (JSAssignProperty prop (JSAssignVariable moduleName)) val
93+setProperty :: Ident -> JS -> ModuleName -> [JS]
94+setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
95+ [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val
96+ , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (JSAccessor (identToJs ident) (JSVar moduleName)) ]
97+setProperty ident val (ModuleName (ProperName moduleName)) =
98+ [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val ]
9699
97100var :: Ident -> JS
98−var name = JSVar $ identToJs name
101+var = JSVar . identToJs
99102
100103valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
101104valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
@@ -154,13 +157,15 @@ runtimeTypeChecks arg ty =
154157 arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar "Array")) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
155158
156159varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
157−varToJs m e qual@(Qualified _ ident) = case M.lookup (qualify m qual) (names e) of
158− Just (_, ty) | isExtern ty -> var ident
159− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified (Just aliasModule) aliasIdent)
160− _ -> case qual of
161− Qualified Nothing _ -> var ident
162− _ -> qualifiedToJS identToJs qual
160+varToJs m e qual@(Qualified _ ident) = go qual
163161 where
162+ go qual = case M.lookup (qualify m qual) (names e) of
163+ Just (_, ty) | isExtern ty -> var ident
164+ Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
165+ _ -> case qual of
166+ Qualified Nothing _ -> var ident
167+ Qualified (Just (ModuleName (ProperName mn))) (Op op) -> JSIndexer (JSStringLiteral op) (JSVar mn)
168+ _ -> qualifiedToJS identToJs qual
164169 isExtern (Extern ForeignImport) = True
165170 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
166171 Just (_, ty') -> isExtern ty'
@@ -263,7 +268,7 @@ isOnlyConstructor m e ctor =
263268
264269statementToJs :: Options -> ModuleName -> Environment -> Statement -> JS
265270statementToJs opts m e (VariableIntroduction ident value) = JSVariableIntroduction (identToJs ident) (Just (valueToJs opts m e value))
266−statementToJs opts m e (Assignment target value) = JSAssignment (JSAssignVariable (identToJs target)) (valueToJs opts m e value)
271+statementToJs opts m e (Assignment target value) = JSAssignment (JSVar (identToJs target)) (valueToJs opts m e value)
267272statementToJs opts m e (While cond sts) = JSWhile (valueToJs opts m e cond) (JSBlock (map (statementToJs opts m e) sts))
268273statementToJs opts m e (For ident start end sts) = JSFor (identToJs ident) (valueToJs opts m e start) (valueToJs opts m e end) (JSBlock (map (statementToJs opts m e) sts))
269274statementToJs opts m e (If ifst) = ifToJs ifst