-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pack guest return data * fix guest func return * code cleanup * update README * change Read signature * rename packeDataArray * MultiPackedData * packedDataArray * simplified mdk * naming refactoring * refactoring * remove auto freeing memories from * remove allocationMAps from hf * remove allocationMAp pkg * test * comment tests * add xPack methods * fix tests * update README
- Loading branch information
1 parent
4901623
commit 1157257
Showing
19 changed files
with
978 additions
and
849 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
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 +1,56 @@ | ||
package wasify | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/wasify-io/wasify-go/internal/types" | ||
"github.com/wasify-io/wasify-go/internal/utils" | ||
) | ||
|
||
type GuestFunctionResult struct { | ||
multiPackedData uint64 | ||
memory Memory | ||
} | ||
|
||
// ReadPacks decodes the packedData from a GuestFunctionResult instance and retrieves a sequence of packed datas. | ||
// NOTE: Frees multiPackedData, which means ReadPacks should be called once. | ||
func (r GuestFunctionResult) ReadPacks() ([]PackedData, error) { | ||
|
||
if r.multiPackedData == 0 { | ||
return nil, errors.New("packedData is empty") | ||
} | ||
|
||
t, offsetU32, size := utils.UnpackUI64(uint64(r.multiPackedData)) | ||
|
||
if t != types.ValueTypePack { | ||
err := fmt.Errorf("Can't unpack host data, the type is not a valueTypePack. expected %d, got %d", types.ValueTypePack, t) | ||
return nil, err | ||
} | ||
|
||
bytes, err := r.memory.ReadBytes(offsetU32, size) | ||
if err != nil { | ||
err := errors.Join(errors.New("ReadPacks error, can't read bytes:"), err) | ||
return nil, err | ||
} | ||
|
||
err = r.memory.FreePack(PackedData(r.multiPackedData)) | ||
if err != nil { | ||
err := errors.Join(errors.New("ReadPacks error, can't free multiPackedData:"), err) | ||
return nil, err | ||
} | ||
|
||
packedDataArray := utils.BytesToUint64Array(bytes) | ||
|
||
// calculate the number of elements in the array | ||
count := size / 8 | ||
|
||
results := make([]PackedData, count) | ||
|
||
// Iterate over the packedData, unpack and read data of each element into a Result | ||
for i, pd := range packedDataArray { | ||
results[i] = PackedData(pd) | ||
} | ||
|
||
return results, 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
Oops, something went wrong.