Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 55 lines (50 sloc) 1.575 kb
2764ff9c »
2008-09-29 import
1 import qualified Data.ByteString as BS
2 import qualified V8
e4d27654 »
2008-10-06 extracting args
3 import Control.Monad
da149820 »
2008-09-30 basic js runner
4 import System.Environment
2764ff9c »
2008-09-29 import
5
9b61917f »
2008-10-06 basic callback into haskell
6 printValue :: V8.Handle V8.Value -> IO ()
7 printValue v = do
8 str <- V8.valueToUtf8 v
e4d27654 »
2008-10-06 extracting args
9 BS.putStr str
9b61917f »
2008-10-06 basic callback into haskell
10
3b779658 »
2008-09-30 TryCatch and converting empty handles to maybes
11 printError :: V8.TryCatch -> String -> IO ()
12 printError trycatch msg = do
13 putStr $ msg ++ ": "
14 exn <- V8.tryCatchException trycatch
15 case exn of
e4d27654 »
2008-10-06 extracting args
16 Just exn -> printValue exn >> putStrLn ""
9b61917f »
2008-10-06 basic callback into haskell
17 Nothing -> putStrLn "unknown exception"
18
19 printCallback :: V8.Arguments -> IO (V8.Handle ())
20 printCallback args = do
e4d27654 »
2008-10-06 extracting args
21 argc <- V8.argumentsLength args
22 putStr "print("
23 forM_ [0..argc-1] $ \i -> do
24 arg <- V8.argumentsGet args i
25 case arg of
26 Just arg -> do
27 when (i > 0) $ putStr ", "
28 printValue arg
29 Nothing -> return ()
30 putStrLn ")"
9b61917f »
2008-10-06 basic callback into haskell
31 return V8.undefined
3b779658 »
2008-09-30 TryCatch and converting empty handles to maybes
32
2764ff9c »
2008-09-29 import
33 main = do
da149820 »
2008-09-30 basic js runner
34 args <- getArgs
35 let code = case args of
36 [code] -> code
37 [] -> "'this is a sample ' + 'string of code'"
2764ff9c »
2008-09-29 import
38 V8.withHandleScope $ do
9b61917f »
2008-10-06 basic callback into haskell
39 print_cb <- V8.functionTemplateNew printCallback
da149820 »
2008-09-30 basic js runner
40 global <- V8.objectTemplateNew
9b61917f »
2008-10-06 basic callback into haskell
41 V8.templateSet global "print" print_cb
e5b2e63f »
2008-10-06 withContext
42 V8.withContext global $ do
43 V8.withTryCatch $ \trycatch -> do
44 script <- V8.scriptCompile code
45 case script of
46 Nothing -> printError trycatch "compile error"
47 Just script -> do
48 result <- V8.scriptRun script
49 case result of
50 Nothing -> printError trycatch "run error"
51 Just result -> do
52 putStr " => "
53 printValue result
54 putStrLn ""
Something went wrong with that request. Please try again.