-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fadb99a
commit a807601
Showing
2 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,53 @@ | ||
// Copyright 2023 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package newWasmer | ||
package wasmer | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
|
||
wasmgo "github.com/wasmerio/wasmer-go/wasmer" | ||
) | ||
|
||
var errGrowMemory = errors.New("failed to grow memory") | ||
var ( | ||
errCantGrowMemory = errors.New("failed to grow memory") | ||
errMemoryValueOutOfBounds = errors.New("memory value is out of bounds") | ||
) | ||
|
||
// Memory is a thin wrapper around Wasmer memory to support | ||
// Gossamer runtime.Memory interface | ||
type Memory struct { | ||
memory *wasmgo.Memory | ||
} | ||
|
||
func checkBounds(value uint64) (uint32, error) { | ||
if value > math.MaxUint32 { | ||
return 0, fmt.Errorf("%w", errMemoryValueOutOfBounds) | ||
} | ||
return uint32(value), nil | ||
} | ||
|
||
// Data returns the memory's data | ||
func (m Memory) Data() []byte { | ||
return m.memory.Data() | ||
} | ||
|
||
// Length returns the memory's length | ||
func (m Memory) Length() uint32 { | ||
return uint32(m.memory.DataSize()) | ||
value, err := checkBounds(uint64(m.memory.DataSize())) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return value | ||
} | ||
|
||
// Grow grows the memory by the given number of pages | ||
func (m Memory) Grow(numPages uint32) error { | ||
ok := m.memory.Grow(wasmgo.Pages(numPages)) | ||
if !ok { | ||
return fmt.Errorf("%w", errGrowMemory) | ||
return fmt.Errorf("%w", errCantGrowMemory) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters