@@ -52,9 +52,14 @@ toHeader = catMaybes . map go
5252 Nothing
5353 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty _))) =
5454 Just $ CppFunction name [] rty [] CppNoOp
55+
5556 -- Generate thunks for top-level values
56− go (CppVariableIntroduction (name, _) _ (Just _)) =
57− Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
57+ go (CppVariableIntroduction (name, vty) _ (Just cpp)) =
58+ case cpp of
59+ CppObjectLiteral CppInstance vals ->
60+ Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
61+ _ ->
62+ Just $ CppVariableIntroduction (name, vty) [CppExtern] Nothing
5863 go cpp@(CppStruct {}) =
5964 Just cpp
6065 go (CppComment comms cpp')
@@ -115,15 +120,18 @@ toBody = catMaybes . map go
115120 Nothing
116121 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty body))) =
117122 Just $ CppFunction name [] rty [] body
118− -- Generate thunks for top-level values
119− go (CppVariableIntroduction (name, _) _ (Just cpp)) =
120− Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
123+ go (CppVariableIntroduction (name, vty) _ (Just cpp)) =
124+ case cpp of
125+ CppObjectLiteral CppInstance vals ->
126+ Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
127+ _ ->
128+ Just $ CppVariableIntroduction (name, vty) [] (Just lambda)
121129 where
122130 val = CppVariableIntroduction ("$value$", Just $ CppAny [CppConst]) [CppStatic] (Just $ addCaptures cpp)
123131 block = CppBlock [val, CppReturn (CppVar "$value$")]
124132 lambda = CppLambda [] [("", Just $ thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) block
125133 addCaptures :: Cpp -> Cpp
126− addCaptures (CppObjectLiteral objs) = CppObjectLiteral (objlam <$> objs)
134+ addCaptures (CppObjectLiteral t objs) = CppObjectLiteral t (objlam <$> objs)
127135 where
128136 objlam :: (String, Cpp) -> (String, Cpp)
129137 objlam (name', (CppLambda _ args rty body)) = (name' , CppLambda [] args rty $ addCaptures body)