Skip to content

Instantly share code, notes, and snippets.

@bananu7
Created October 8, 2015 20:05
Show Gist options
  • Save bananu7/32f388d1fef51072ff31 to your computer and use it in GitHub Desktop.
Save bananu7/32f388d1fef51072ff31 to your computer and use it in GitHub Desktop.
-- non-lens version (BROKEN)
addNativeFunction :: String -> FunctionData -> Context -> Context
addNativeFunction name fn ctx = newCtx
where
newRef :: FunctionRef
newRef = (\(FunctionRef i) -> FunctionRef (i+1)) $ fst $ Map.findMax (functions ctx)
ctxWithNewRef = ctx { functions = Map.insert newRef fn (functions ctx) }
insertNewValue :: TableData -> TableData
insertNewValue (TableData td) = TableData $ Map.insert (Str name) (Function newRef) td
newCtx = ctxWithNewRef { tables = Map.adjust insertNewValue (_Gref ctx) (tables ctx) }
-- lens version
addNativeFunction :: String -> FunctionData -> LuaM ()
addNativeFunction name fdata = do
newRef <- uniqueFunctionRef
functions . at newRef .= Just fdata
tables . at gRef . at name .= Just newRef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment