-
Notifications
You must be signed in to change notification settings - Fork 146
chore(lib/runtime/wasmer): refactor Exec and exec methods
#2686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1c27470
6d7caed
123b310
f1dcb18
a6a242b
8658732
fb25185
c67a5be
23151bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,67 +264,45 @@ func (in *Instance) Stop() { | |
| } | ||
| } | ||
|
|
||
| // Store func | ||
| func (in *Instance) store(data []byte, location int32) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did we want to remove these functions?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was me trying to understand everything that was needed in that And these 1 or 2-lines functions were only used once (in prod code, and once in a test), so I just inlined them. So I took the EXECUTIVE DECISION to inline them pretty much 😄 |
||
| mem := in.vm.Memory.Data() | ||
| copy(mem[location:location+int32(len(data))], data) | ||
| } | ||
|
|
||
| // Load load | ||
| func (in *Instance) load(location, length int32) []byte { | ||
| mem := in.vm.Memory.Data() | ||
| return mem[location : location+length] | ||
| } | ||
| var ( | ||
| ErrInstanceIsStopped = errors.New("instance is stopped") | ||
| ErrExportFunctionNotFound = errors.New("export function not found") | ||
| ) | ||
|
|
||
| // Exec calls the given function with the given data | ||
| func (in *Instance) Exec(function string, data []byte) ([]byte, error) { | ||
| return in.exec(function, data) | ||
| } | ||
|
|
||
| // Exec func | ||
| func (in *Instance) exec(function string, data []byte) ([]byte, error) { | ||
| if in.ctx.Storage == nil { | ||
| return nil, runtime.ErrNilStorage | ||
| } | ||
|
|
||
| func (in *Instance) Exec(function string, data []byte) (result []byte, err error) { | ||
| in.Lock() | ||
| defer in.Unlock() | ||
|
|
||
| if in.isClosed { | ||
| return nil, errors.New("instance is stopped") | ||
| return nil, ErrInstanceIsStopped | ||
| } | ||
|
|
||
| ptr, err := in.malloc(uint32(len(data))) | ||
| dataLength := uint32(len(data)) | ||
| inputPtr, err := in.ctx.Allocator.Allocate(dataLength) | ||
| if err != nil { | ||
| return nil, err | ||
| return nil, fmt.Errorf("allocating input memory: %w", err) | ||
| } | ||
|
|
||
| defer in.clear() | ||
| defer in.ctx.Allocator.Clear() | ||
|
|
||
| // Store the data into memory | ||
| in.store(data, int32(ptr)) | ||
| datalen := int32(len(data)) | ||
| memory := in.vm.Memory.Data() | ||
| copy(memory[inputPtr:inputPtr+dataLength], data) | ||
|
|
||
| runtimeFunc, ok := in.vm.Exports[function] | ||
| if !ok { | ||
| return nil, fmt.Errorf("could not find exported function %s", function) | ||
| return nil, fmt.Errorf("%w: %s", ErrExportFunctionNotFound, function) | ||
| } | ||
|
|
||
| res, err := runtimeFunc(int32(ptr), datalen) | ||
| wasmValue, err := runtimeFunc(int32(inputPtr), int32(dataLength)) | ||
|
qdm12 marked this conversation as resolved.
|
||
| if err != nil { | ||
| return nil, err | ||
| return nil, fmt.Errorf("running runtime function: %w", err) | ||
| } | ||
|
|
||
| offset, length := runtime.Int64ToPointerAndSize(res.ToI64()) | ||
| return in.load(offset, length), nil | ||
| } | ||
|
|
||
| func (in *Instance) malloc(size uint32) (uint32, error) { | ||
| return in.ctx.Allocator.Allocate(size) | ||
| } | ||
|
|
||
| func (in *Instance) clear() { | ||
| in.ctx.Allocator.Clear() | ||
| outputPtr, outputLength := runtime.Int64ToPointerAndSize(wasmValue.ToI64()) | ||
| memory = in.vm.Memory.Data() // call Data() again to get larger slice | ||
| return memory[outputPtr : outputPtr+outputLength], nil | ||
| } | ||
|
|
||
| // NodeStorage to get reference to runtime node service | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.