Repository navigation
Expand file tree
/
Copy pathModuleState.purs
More file actions
152 lines (137 loc) 路 5.5 KB
/
Copy pathModuleState.purs
File metadata and controls
152 lines (137 loc) 路 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-- | Per-module output and one publication path for fresh and restored modules.
module Phpurs.ModuleState
( Emission
, ModuleState
, BuildRefs
, newBuildRefs
, renderModuleState
, TranslatedModule
, translateModuleState
, translateModuleStateWithCallbacks
, translateModuleStateWithContracts
, printModuleState
, supportsEmission
, publishModuleState
, publishModuleStateWithProfile
) where
import Prelude
import Data.Array as Array
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..), isJust)
import Data.Newtype (unwrap)
import Data.Set as Set
import Effect (Effect)
import Effect.Aff (Aff, attempt, throwError)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Effect.Ref (Ref)
import Effect.Ref as Ref
import Node.FS.Aff as FS
import Phpurs.BuildProfile as Profile
import Phpurs.Printer.Runtime as Runtime
import Phpurs.CodeGen (translateWithContracts)
import Phpurs.ForeignTraversals (Traversals)
import Phpurs.GenNativeForeign (genForeignModule)
import Phpurs.NativeCallbacks (Callbacks)
import Phpurs.OutputManifest (OutputKind(..), OutputTracker, newOutputTracker, writeOutput)
import Phpurs.PhpAst (PhpFile)
import Phpurs.Printer (printPhpFile)
import PureScript.Backend.Optimizer.Convert (BackendModule)
import PureScript.Backend.Optimizer.CoreFn (ModuleName)
type Emission = { emitModules :: Boolean, emitBundle :: Boolean }
type ModuleState =
{ backend :: BackendModule
, arities :: Map String Int
, modularPhp :: Maybe String
, bundlePhp :: Maybe String
}
type BuildRefs =
{ bundleContentRef :: Ref String
, bundleModulesRef :: Ref (Array { name :: ModuleName, code :: String })
, outputs :: OutputTracker
, globalAritiesRef :: Ref (Map String Int)
}
newBuildRefs :: Effect BuildRefs
newBuildRefs = do
outputs <- newOutputTracker
bundleContentRef <- Ref.new Runtime.bundleHeader
bundleModulesRef <- Ref.new []
globalAritiesRef <- Ref.new Map.empty
pure { bundleContentRef, bundleModulesRef, globalAritiesRef, outputs }
renderModuleState
:: Emission -> Array (Array String) -> String -> Map String Int -> BackendModule -> ModuleState
renderModuleState modes imports foreignSource currentArities backend =
printModuleState modes (translateModuleState imports foreignSource currentArities backend)
type TranslatedModule =
{ backend :: BackendModule
, phpFile :: PhpFile
, foreignCode :: String
, arities :: Map String Int
, allArities :: Map String Int
}
translateModuleState
:: Array (Array String) -> String -> Map String Int -> BackendModule -> TranslatedModule
translateModuleState = translateModuleStateWithCallbacks Set.empty
translateModuleStateWithCallbacks
:: Callbacks
-> Array (Array String)
-> String
-> Map String Int
-> BackendModule
-> TranslatedModule
translateModuleStateWithCallbacks callbacks = translateModuleStateWithContracts callbacks Set.empty
translateModuleStateWithContracts
:: Callbacks
-> Traversals
-> Array (Array String)
-> String
-> Map String Int
-> BackendModule
-> TranslatedModule
translateModuleStateWithContracts callbacks traversals imports foreignSource currentArities backend =
let
phpFile = translateWithContracts callbacks traversals imports backend
foreignModule = genForeignModule
{ moduleName: backend.name, bindings: backend.foreign, source: foreignSource }
-- Store this module's contribution, not a copy of the preceding environment.
arities = Map.union foreignModule.arities phpFile.arities
allArities = Map.union arities currentArities
in
{ backend, phpFile, foreignCode: foreignModule.code, arities, allArities }
printModuleState :: Emission -> TranslatedModule -> ModuleState
printModuleState modes { backend, phpFile, foreignCode, arities, allArities } =
{ backend
, arities
, modularPhp:
if modes.emitModules then Just (printPhpFile false foreignCode allArities phpFile)
else Nothing
, bundlePhp:
if modes.emitBundle then Just (printPhpFile true foreignCode allArities phpFile) else Nothing
}
supportsEmission :: Emission -> ModuleState -> Boolean
supportsEmission modes state =
(not modes.emitModules || isJust state.modularPhp) &&
(not modes.emitBundle || isJust state.bundlePhp)
-- | Call exactly once, in builder order, on hits as well as misses. Output write
-- | failures propagate; cache I/O must not catch them or turn them into misses.
publishModuleState :: Emission -> String -> BuildRefs -> ModuleState -> Aff Unit
publishModuleState = publishModuleStateWithProfile Profile.disabled
publishModuleStateWithProfile
:: Profile.Profile -> Emission -> String -> BuildRefs -> ModuleState -> Aff Unit
publishModuleStateWithProfile profile modes outputDir refs state = do
unless (supportsEmission modes state) $ throwError $ error "Incomplete PHP module state"
let name = state.backend.name
_ <- Profile.measureAff profile "php.mkdir" (unwrap name) \_ -> attempt
(FS.mkdir (outputDir <> "/" <> unwrap name))
liftEffect $ Ref.modify_ (Map.union state.arities) refs.globalAritiesRef
when modes.emitBundle case state.bundlePhp of
Just code -> liftEffect do
Ref.modify_ (\previous -> previous <> code <> "\n") refs.bundleContentRef
Ref.modify_ (\previous -> Array.snoc previous { name: state.backend.name, code })
refs.bundleModulesRef
Nothing -> pure unit
when modes.emitModules case state.modularPhp of
Just code -> Profile.measureAff profile "php.write" (unwrap name) \_ ->
writeOutput refs.outputs outputDir ModularOutput (unwrap name <> "/index.php") code
Nothing -> pure unit