GithubHelp home page GithubHelp logo

hasura / hs-duktape Goto Github PK

View Code? Open in Web Editor NEW

This project forked from valpackett/hs-duktape

0.0 1.0 0.0 943 KB

Haskell bindings for a very compact embedded JavaScript engine

License: Other

Haskell 100.00%

hs-duktape's Introduction

Hackage Linux Build Status Windows Build Status MIT License

hs-duktape

Haskell bindings for duktape, a very compact embedded ECMAScript (JavaScript) engine.

Usage

Here's a simple REPL example:

module Main where

import Scripting.Duktape
import Control.Monad (forever)
import Data.ByteString.Char8 (pack)
import System.IO (hFlush, stdout)

main :: IO ()
main = do
  dukm <- createDuktapeCtx
  case dukm of
    Nothing -> putStrLn "I can't even (start Duktape)"
    Just duk -> forever $ do
      putStr "duktape> "
      hFlush stdout
      retVal <- evalDuktape duk =<< return . pack =<< getLine
      case retVal of
        Left e -> putStrLn $ "Duktape error: " ++ e
        Right Nothing -> putStrLn "No result"
        Right (Just v) -> print v

Aeson's Value type is used for exchanging values between Haskell and ECMAScript.
lens-aeson is a good library for working with Value, um, values.

You can also call functions that are on the global object (or any object that's on the global object):

dukm <- createDuktapeCtx
bresult <- callDuktape (fromJust dukm) Nothing "boolTest" [Bool True, Bool True, Bool False] -- boolTest(true, true, false)
aresult <- callDuktape (fromJust dukm) (Just "NumFuns") "sum" [Number 1, Number 2] -- NumFuns.sum(1, 2)

And expose Haskell functions (same as with calls: set on global or a property of global):

dukm <- createDuktapeCtx
let dbl (Number x) = return $ Number $ x * 2 :: IO Value
    dbl _ = return $ String "wtf"
reD <- exposeFnDuktape (fromJust ctx) Nothing "double" dbl 

The functions must be of type IO (), IO Value, Value -> IO Value, Value -> Value -> IO Value... and so on. (Or with any ToJSON/FromJSON values instead of Value)

Advanced: Script execution timeout

Instead of createDuktapeCtx, you can use createGovernedHeap to enforce termination via a timeout:

allowQuarterSecond :: IO (IO Bool)
allowQuarterSecond = do
  initializedAt <- getCurrentTime
  return $ do
    now <- getCurrentTime
    let diff = diffUTCTime now initializedAt
    return $ diff > 0.25


guvnor <- allowQuarterSecond
ctx <- createGovernedHeap nullFunPtr nullFunPtr nullFunPtr guvnor nullFunPtr
result <- evalDuktape (fromJust ctx) "while (true) {}"
-- Left "RangeError: execution timeout"

Development

Note: The duktape configure script invoked by Cabal/Stack requires python 2 with the PyYAML library installed. pip install PyYAML is one way to achieve this.

Use stack to build.

$ stack build

$ stack test && rm tests.tix

Contributing

Please feel free to submit pull requests!

By participating in this project you agree to follow the Contributor Code of Conduct.

The list of contributors is available on GitHub.

License

Licensed under the MIT license (see to the LICENSE file).
Haskell bindings: Copyright (c) 2015-2018 Greg V [email protected]
Duktape: Copyright (c) 2013-2016 by Duktape authors (see duktape/AUTHORS.rst)

hs-duktape's People

Contributors

valpackett avatar jeremyjh avatar matthiaskauer avatar vikrem avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.