@@ -339,33 +339,36 @@ evalContextExpr (RemoveAtomFunction funcName) = do
339339 else
340340 pure (Just (AtomFunctionNameNotInUseError funcName))
341341
342−evalDatabaseContextIOExpr :: ScriptSession -> DatabaseContext -> DatabaseContextIOExpr -> IO (Either RelationalError DatabaseContext)
343−evalDatabaseContextIOExpr scriptSession currentContext (AddAtomFunction funcName funcType script) = do
344− res <- try $ runGhc (Just libdir) $ do
345− setSession (hscEnv scriptSession)
346− let atomFuncs = atomFunctions currentContext
347− --compile the function
348− eCompiledFunc <- compileAtomFunctionScript scriptSession script
349− pure $ case eCompiledFunc of
350− Left err -> Left (AtomFunctionBodyScriptError err)
351− Right compiledFunc -> do
352− funcAtomType <- mapM (\funcTypeArg -> atomTypeForTypeConstructor funcTypeArg (typeConstructorMapping currentContext)) funcType
353− let updatedFuncs = HS.insert newAtomFunc atomFuncs
354− newContext = currentContext { atomFunctions = updatedFuncs }
355− newAtomFunc = AtomFunction { atomFuncName = funcName,
356− atomFuncType = funcAtomType,
357− atomFuncBody = AtomFunctionBody (Just script) compiledFunc }
342+evalDatabaseContextIOExpr :: Maybe ScriptSession -> DatabaseContext -> DatabaseContextIOExpr -> IO (Either RelationalError DatabaseContext)
343+evalDatabaseContextIOExpr mScriptSession currentContext (AddAtomFunction funcName funcType script) = do
344+ case mScriptSession of
345+ Nothing -> pure (Left (AtomFunctionBodyScriptError ScriptCompilationDisabledError))
346+ Just scriptSession -> do
347+ res <- try $ runGhc (Just libdir) $ do
348+ setSession (hscEnv scriptSession)
349+ let atomFuncs = atomFunctions currentContext
350+ --compile the function
351+ eCompiledFunc <- compileAtomFunctionScript scriptSession script
352+ pure $ case eCompiledFunc of
353+ Left err -> Left (AtomFunctionBodyScriptError err)
354+ Right compiledFunc -> do
355+ funcAtomType <- mapM (\funcTypeArg -> atomTypeForTypeConstructor funcTypeArg (typeConstructorMapping currentContext)) funcType
356+ let updatedFuncs = HS.insert newAtomFunc atomFuncs
357+ newContext = currentContext { atomFunctions = updatedFuncs }
358+ newAtomFunc = AtomFunction { atomFuncName = funcName,
359+ atomFuncType = funcAtomType,
360+ atomFuncBody = AtomFunctionBody (Just script) compiledFunc }
358361
359− -- check if the name is already in use
360− if HS.member funcName (HS.map atomFuncName atomFuncs) then
361− Left (AtomFunctionNameInUseError funcName)
362− else do
363− Right newContext
364− case res of
365− Left (exc :: SomeException) -> pure $ Left (AtomFunctionBodyScriptError (OtherScriptCompilationError (show exc)))
366− Right eContext -> case eContext of
367− Left err -> pure (Left err)
368− Right context' -> pure (Right context')
362+ -- check if the name is already in use
363+ if HS.member funcName (HS.map atomFuncName atomFuncs) then
364+ Left (AtomFunctionNameInUseError funcName)
365+ else do
366+ Right newContext
367+ case res of
368+ Left (exc :: SomeException) -> pure $ Left (AtomFunctionBodyScriptError (OtherScriptCompilationError (show exc)))
369+ Right eContext -> case eContext of
370+ Left err -> pure (Left err)
371+ Right context' -> pure (Right context')
369372
370373updateTupleWithAtomExprs :: (M.Map AttributeName AtomExpr) -> DatabaseContext -> RelationTuple -> Either RelationalError RelationTuple
371374updateTupleWithAtomExprs exprMap context tupIn = do