| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Language.JavaScript.Inline.Core
Synopsis
- data Config = Config {
- nodePath :: FilePath
- nodeExtraArgs :: [String]
- nodeExtraEnv :: [(String, String)]
- nodeModules :: Maybe FilePath
- nodeExportSyncBufferSize :: Int
- defaultConfig :: Config
- data Session
- newSession :: Config -> IO Session
- closeSession :: Session -> IO ()
- killSession :: Session -> IO ()
- withSession :: Config -> (Session -> IO r) -> IO r
- data JSExpr
- data JSVal
- newtype EncodedString = EncodedString {}
- newtype EncodedJSON = EncodedJSON {}
- data RawJSType
- class ToJS a where
- class FromJS a where
- rawJSType :: Proxy a -> RawJSType
- toRawJSType :: Proxy a -> JSExpr
- fromJS :: Session -> ByteString -> IO a
- eval :: forall a. FromJS a => Session -> JSExpr -> IO a
- importCJS :: Session -> FilePath -> IO JSVal
- importMJS :: Session -> FilePath -> IO JSVal
- class Import f
- importJSFunc :: Import f => Session -> JSVal -> f
- class Export f
- export :: Export f => Session -> f -> IO JSVal
- exportSync :: Export f => Session -> f -> IO JSVal
- freeJSVal :: JSVal -> IO ()
- data NodeVersionUnsupported = NodeVersionUnsupported
- newtype EvalError = EvalError {}
- data SessionClosed = SessionClosed
Session management
Constructors
| Config | |
Fields
| |
closeSession :: Session -> IO () Source #
killSession :: Session -> IO () Source #
Terminate the node process immediately. Use this to close the
Session if node doesn't need to run any more. Blocks until node
process exits.
withSession :: Config -> (Session -> IO r) -> IO r Source #
Create a Session with newSession, run the passed computation, then free
the Session with killSession. The return value is forced to WHNF before
freeing the Session to reduce the likelihood of use-after-free errors.
Haskell/JavaScript data marshaling
Represents a JavaScript expression.
Use the IsString instance to convert a String to JSExpr, and the
Semigroup instance for concating JSExpr. It's also possible to embed
other things into JSExpr, e.g. a buffer literal, JSON value or a JSVal.
An opaque reference of a JavaScript value. Each JSVal is registered with
a finalizer which frees the reference on the node side when it's garbage
collected in Haskell. It's also possible to manually free a JSVal using
freeJSVal.
newtype EncodedString Source #
UTF-8 encoded string.
Constructors
| EncodedString | |
Fields | |
Instances
| Show EncodedString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods showsPrec :: Int -> EncodedString -> ShowS # show :: EncodedString -> String # showList :: [EncodedString] -> ShowS # | |
| IsString EncodedString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods fromString :: String -> EncodedString # | |
| FromJS EncodedString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods rawJSType :: Proxy EncodedString -> RawJSType Source # toRawJSType :: Proxy EncodedString -> JSExpr Source # fromJS :: Session -> ByteString -> IO EncodedString Source # | |
| ToJS EncodedString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods toJS :: EncodedString -> JSExpr Source # | |
newtype EncodedJSON Source #
UTF-8 encoded JSON.
Constructors
| EncodedJSON | |
Fields | |
Instances
| Show EncodedJSON Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods showsPrec :: Int -> EncodedJSON -> ShowS # show :: EncodedJSON -> String # showList :: [EncodedJSON] -> ShowS # | |
| IsString EncodedJSON Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods fromString :: String -> EncodedJSON # | |
| FromJS EncodedJSON Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods rawJSType :: Proxy EncodedJSON -> RawJSType Source # toRawJSType :: Proxy EncodedJSON -> JSExpr Source # fromJS :: Session -> ByteString -> IO EncodedJSON Source # | |
| ToJS EncodedJSON Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods toJS :: EncodedJSON -> JSExpr Source # | |
To convert a JavaScript value to Haskell, we need to specify its "raw type", which can be one of the following:
Haskell types which can be converted to JavaScript.
Instances
| ToJS () Source # | |
Defined in Language.JavaScript.Inline.Core.Class | |
| ToJS ByteString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods toJS :: ByteString -> JSExpr Source # | |
| ToJS JSVal Source # | |
| ToJS EncodedJSON Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods toJS :: EncodedJSON -> JSExpr Source # | |
| ToJS EncodedString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods toJS :: EncodedString -> JSExpr Source # | |
Haskell types which can be converted from JavaScript.
Methods
rawJSType :: Proxy a -> RawJSType Source #
The JavaScript value's RawJSType.
toRawJSType :: Proxy a -> JSExpr Source #
A synchronous JavaScript function which encodes a value to its
RawJSType.
fromJS :: Session -> ByteString -> IO a Source #
A Haskell function which decodes the Haskell value from the serialized
RawJSType.
Instances
| FromJS () Source # | |
| FromJS ByteString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods rawJSType :: Proxy ByteString -> RawJSType Source # toRawJSType :: Proxy ByteString -> JSExpr Source # fromJS :: Session -> ByteString -> IO ByteString Source # | |
| FromJS JSVal Source # | |
| FromJS EncodedJSON Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods rawJSType :: Proxy EncodedJSON -> RawJSType Source # toRawJSType :: Proxy EncodedJSON -> JSExpr Source # fromJS :: Session -> ByteString -> IO EncodedJSON Source # | |
| FromJS EncodedString Source # | |
Defined in Language.JavaScript.Inline.Core.Class Methods rawJSType :: Proxy EncodedString -> RawJSType Source # toRawJSType :: Proxy EncodedString -> JSExpr Source # fromJS :: Session -> ByteString -> IO EncodedString Source # | |
Performing evaluation
eval :: forall a. FromJS a => Session -> JSExpr -> IO a Source #
Evaluate a JSExpr and return the result. Evaluation is asynchronous.
When this function returns, the eval request has been sent to the eval
server, but the result may not be sent back yet. The returned value is a
thunk, and forcing it to WHNF will block until the result is sent back.
The caveats of lazy I/O apply here as well. For instance, returning
evaluation result from a with-style function may cause a use-after-free
problem. In case when it's desirable to ensure the evaluation has completed
at a certain point, use evaluate or BangPatterns to
force the result value.
Modeling asynchronousity with laziness enables us to simplify API. Users can easily regain strictness from the lazy API; if we do it the other way around and provide strict-by-default eval functions, we'll need to explicitly decouple sending of eval requests and receiving of eval results, which complicates the API.
On the eval server side, the result value is awaited before further
processing. Therefore if it's a Promise, the eval result will be the
resolved value instead of the Promise itself. If the Promise value needs
to be returned, wrap it in another object (e.g. a single-element array).
importCJS :: Session -> FilePath -> IO JSVal Source #
Import a CommonJS module file and return its module.exports object. The
module file path can be absolute, or relative to the current Haskell process.
The imported module will be retained in the require() loader cache.
importMJS :: Session -> FilePath -> IO JSVal Source #
Import an ECMAScript module file and return its module namespace object. The module file path can be absolute, or relative to the current Haskell process. The imported module will be retained in the ESM loader cache.
Importing JavaScript functions to Haskell
The class of Haskell functions which can be imported from JavaScript
function JSVals. The Haskell function type should be a -> b -> .. -> IO
r, where the arguments a, b, etc are ToJS instances, and the result
r is FromJS instance.
Minimal complete definition
importMonomorphize
Instances
| FromJS r => Import (IO r) Source # | |
Defined in Language.JavaScript.Inline.Core.Import Methods importMonomorphize :: Session -> JSVal -> [JSExpr] -> IO r | |
| (ToJS a, Import b) => Import (a -> b) Source # | |
Defined in Language.JavaScript.Inline.Core.Import Methods importMonomorphize :: Session -> JSVal -> [JSExpr] -> a -> b | |
importJSFunc :: Import f => Session -> JSVal -> f Source #
Import a JavaScript function to a Haskell function.
Exporting Haskell functions to JavaScript
The class of Haskell functions which can be exported as JavaScript
functions. The Haskell function type should be a -> b -> .. -> IO r, where
the arguments a, b, etc are FromJS instances, and the result r is
ToJS instance.
Minimal complete definition
exportArgsFromJS, exportMonomorphize
Instances
| ToJS r => Export (IO r) Source # | |
Defined in Language.JavaScript.Inline.Core.Export Methods exportArgsFromJS :: Proxy (IO r) -> [Dict FromJS] exportMonomorphize :: Session -> IO r -> [ByteString] -> IO JSExpr | |
| (FromJS a, Export b) => Export (a -> b) Source # | |
Defined in Language.JavaScript.Inline.Core.Export Methods exportArgsFromJS :: Proxy (a -> b) -> [Dict FromJS] exportMonomorphize :: Session -> (a -> b) -> [ByteString] -> IO JSExpr | |
export :: Export f => Session -> f -> IO JSVal Source #
Export a Haskell function as a JavaScript async function, and return its
JSVal. Some points to keep in mind:
- The Haskell function itself can call into JavaScript again via
eval, and vice versa. - When called in JavaScript, the Haskell function is run in a forked thread.
- If the Haskell function throws, the JavaScript function will reject with an
Errorwith the exception string. - Unlike
JSVals returned byeval,JSVals returned byexportare not garbage collected, since we don't know when a function is garbage collected on thenodeside. TheseJSVals need to be manually freed usingfreeJSVal.
exportSync :: Export f => Session -> f -> IO JSVal Source #
Export a Haskell function as a JavaScript sync function. This is quite
heavyweight and in most cases, export is preferrable. exportSync can be
useful in certain scenarios when a sync function is desired, e.g. converting
a Haskell function to a WebAssembly import.
Unlike export, exportSync has limited reentrancy:
- The Haskell function may calculate the return value based on the result of calling into JavaScript again, but only synchronous code is supported in this case.
- The exported JavaScript sync function must not invoke other exported JavaScript sync functions, either directly or indirectly(Haskell calling into JavaScript again).
Manual resource management
Exceptions
data NodeVersionUnsupported Source #
Constructors
| NodeVersionUnsupported |
Instances
| Show NodeVersionUnsupported Source # | |
Defined in Language.JavaScript.Inline.Core.Exception Methods showsPrec :: Int -> NodeVersionUnsupported -> ShowS # show :: NodeVersionUnsupported -> String # showList :: [NodeVersionUnsupported] -> ShowS # | |
| Exception NodeVersionUnsupported Source # | |
Defined in Language.JavaScript.Inline.Core.Exception | |
Constructors
| EvalError | |
Fields | |
Instances
| Show EvalError Source # | |
| Exception EvalError Source # | |
Defined in Language.JavaScript.Inline.Core.Exception Methods toException :: EvalError -> SomeException # fromException :: SomeException -> Maybe EvalError # displayException :: EvalError -> String # | |
data SessionClosed Source #
Constructors
| SessionClosed |
Instances
| Show SessionClosed Source # | |
Defined in Language.JavaScript.Inline.Core.Exception Methods showsPrec :: Int -> SessionClosed -> ShowS # show :: SessionClosed -> String # showList :: [SessionClosed] -> ShowS # | |
| Exception SessionClosed Source # | |
Defined in Language.JavaScript.Inline.Core.Exception Methods toException :: SessionClosed -> SomeException # fromException :: SomeException -> Maybe SessionClosed # displayException :: SessionClosed -> String # | |