on master help sign in
javascript: better error representation
e0288fdraichoo
src/IRTS/CodegenJavaScript.hs+8−5
@@ -113,7 +113,7 @@ compileJS (JSNew name args) =
113 "new " ++ name ++ "(" ++ intercalate "," (map compileJS args) ++ ")"
113 "new " ++ name ++ "(" ++ intercalate "," (map compileJS args) ++ ")"
114
114
115compileJS (JSError exc) =
115compileJS (JSError exc) =
116− "(function(){throw '" ++ exc ++ "';})()"
116+ "throw new Error(\"" ++ exc ++ "\")"
117
117
118compileJS (JSOp op lhs rhs) =
118compileJS (JSOp op lhs rhs) =
119 compileJS lhs ++ " " ++ op ++ " " ++ compileJS rhs
119 compileJS lhs ++ " " ++ op ++ " " ++ compileJS rhs
@@ -238,6 +238,8 @@ jsLet name value body =
238 )
238 )
239 ) [value]
239 ) [value]
240
240
241+jsError :: String -> JS
242+jsError err = JSApp (JSFunction [] (JSError err)) []
241
243
242jsSubst :: JS -> JS -> JS -> JS
244jsSubst :: JS -> JS -> JS -> JS
243jsSubst var new old
245jsSubst var new old
@@ -373,6 +375,7 @@ isJSConstant js
373 | otherwise = False
375 | otherwise = False
374
376
375inlineJS :: JS -> JS
377inlineJS :: JS -> JS
378+inlineJS (JSReturn (JSApp (JSFunction [] err@(JSError _)) [])) = err
376inlineJS (JSReturn (JSApp (JSFunction ["cse"] body) [val@(JSVar _)])) =
379inlineJS (JSReturn (JSApp (JSFunction ["cse"] body) [val@(JSVar _)])) =
377 inlineJS $ jsSubst (JSIdent "cse") val body
380 inlineJS $ jsSubst (JSIdent "cse") val body
378
381
@@ -1208,7 +1211,7 @@ translateConstant PtrType = JSType JSPtrTy
1208translateConstant Forgot = JSType JSForgotTy
1211translateConstant Forgot = JSType JSForgotTy
1209translateConstant (BI i) = jsBigInt $ JSString (show i)
1212translateConstant (BI i) = jsBigInt $ JSString (show i)
1210translateConstant c =
1213translateConstant c =
1211− JSError $ "Unimplemented Constant: " ++ show c
1214+ jsError $ "Unimplemented Constant: " ++ show c
1212
1215
1213translateDeclaration :: (String, SDecl) -> [JS]
1216translateDeclaration :: (String, SDecl) -> [JS]
1214translateDeclaration (path, SFun name params stackSize body)
1217translateDeclaration (path, SFun name params stackSize body)
@@ -1500,7 +1503,7 @@ translateExpression (SOp op vars)
1500 invokeMeth obj meth args = jsMeth (JSVar obj) meth (map JSVar args)
1503 invokeMeth obj meth args = jsMeth (JSVar obj) meth (map JSVar args)
1501
1504
1502translateExpression (SError msg) =
1505translateExpression (SError msg) =
1503− JSError msg
1506+ jsError msg
1504
1507
1505translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
1508translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
1506 jsCall (idrRTNamespace ++ "print") [JSVar var]
1509 jsCall (idrRTNamespace ++ "print") [JSVar var]
@@ -1554,14 +1557,14 @@ translateExpression (SProj var i) =
1554translateExpression SNothing = JSNull
1557translateExpression SNothing = JSNull
1555
1558
1556translateExpression e =
1559translateExpression e =
1557− JSError $ "Not yet implemented: " ++ filter (/= '\'') (show e)
1560+ jsError $ "Not yet implemented: " ++ filter (/= '\'') (show e)
1558
1561
1559data FFI = FFICode Char | FFIArg Int | FFIError String
1562data FFI = FFICode Char | FFIArg Int | FFIError String
1560
1563
1561ffi :: String -> [String] -> JS
1564ffi :: String -> [String] -> JS
1562ffi code args = let parsed = ffiParse code in
1565ffi code args = let parsed = ffiParse code in
1563 case ffiError parsed of
1566 case ffiError parsed of
1564− Just err -> JSError err
1567+ Just err -> jsError err
1565 Nothing -> JSRaw $ renderFFI parsed args
1568 Nothing -> JSRaw $ renderFFI parsed args
1566 where
1569 where
1567 ffiParse :: String -> [FFI]
1570 ffiParse :: String -> [FFI]