do not grow tables on initialization. again

This commit is contained in:
Ilya Rezvov
2018-04-19 16:25:27 -07:00
parent 80f61158d7
commit a8459fb541
+12 -22
View File
@@ -426,8 +426,8 @@ allocMems mems = Vector.fromList <$> mapM allocMem mems
initialize :: ModuleInstance -> Module -> Store -> IO (Either String Store) initialize :: ModuleInstance -> Module -> Store -> IO (Either String Store)
initialize inst Module {elems, datas, start} store = do initialize inst Module {elems, datas, start} store = do
storeWithTables <- Monad.foldM initElem store elems storeWithTables <- Monad.foldM initElem (Right store) elems
storeWithMems <- Monad.foldM initData (Right storeWithTables) datas storeWithMems <- Monad.foldM initData storeWithTables datas
case storeWithMems of case storeWithMems of
Right st -> do Right st -> do
case start of case start of
@@ -438,31 +438,21 @@ initialize inst Module {elems, datas, start} store = do
Nothing -> return $ Right st Nothing -> return $ Right st
Left reason -> return $ Left reason Left reason -> return $ Left reason
where where
fitOrGrowTable :: Address -> Store -> Int -> TableInstance initElem :: Either String Store -> ElemSegment -> IO (Either String Store)
fitOrGrowTable idx st last = initElem (Left err) _ = return $ Left err
let t@(TableInstance elems maxLen) = tableInstances st ! idx in initElem (Right st) ElemSegment {tableIndex, offset, funcIndexes} = do
let len = Vector.length elems in VI32 val <- evalConstExpr inst st offset
let increased = TableInstance (elems Vector.++ (Vector.fromList $ replicate (last - len) Nothing)) maxLen in
if last < len
then t
else case maxLen of
Nothing -> increased
Just max ->
if max < last
then error $ "Max table length reached. Max " ++ show max ++ ", but requested " ++ show last
else increased
initElem :: Store -> ElemSegment -> IO Store
initElem st ElemSegment {tableIndex, offset, funcIndexes} = do
VI32 val <- evalConstExpr inst store offset
let from = fromIntegral val let from = fromIntegral val
let funcs = map ((funcaddrs inst !) . fromIntegral) funcIndexes let funcs = map ((funcaddrs inst !) . fromIntegral) funcIndexes
let idx = tableaddrs inst ! fromIntegral tableIndex let idx = tableaddrs inst ! fromIntegral tableIndex
let last = from + length funcs let last = from + length funcs
let TableInstance elems maxLen = fitOrGrowTable idx st last let TableInstance elems maxLen = tableInstances st ! idx
let len = Vector.length elems let len = Vector.length elems
let table = TableInstance (elems // zip [from..] (map Just funcs)) maxLen if last > len
return $ st { tableInstances = tableInstances st Vector.// [(idx, table)] } then return $ Left "elements segment does not fit"
else do
let table = TableInstance (elems // zip [from..] (map Just funcs)) maxLen
return $ Right st { tableInstances = tableInstances st Vector.// [(idx, table)] }
initData :: Either String Store -> DataSegment -> IO (Either String Store) initData :: Either String Store -> DataSegment -> IO (Either String Store)
initData (Left err) _ = return $ Left err initData (Left err) _ = return $ Left err