Skip to content

Commit

Permalink
Added error handling in go ext
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Moore <[email protected]>
  • Loading branch information
jimmyaxod committed Sep 29, 2023
1 parent a1bf185 commit f3f6819
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
30 changes: 28 additions & 2 deletions extension/generator/golang/templates/guest.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ func (d *_{{ $ifc.Name }}) {{ $fn.Name }}(params *{{ $fn.Params }}) ({{ $fn.Retu
// Now make the call to the host.

{{- if (IsInterface $schema $fn.Return) }}
readBuffer = nil
v := ext_{{ $schema.Name }}_{{ $ifc.Name }}_{{ $fn.Name }}(d.instanceId, off, l)
// IF the return type is an interface return ifc, which contains hidden instanceId.

// TODO: Handle error from host. In this case there'll be an error in the readBuffer
// Handle error from host. In this case there'll be an error in the readBuffer
if (readBuffer!=nil) {
val, err := polyglot.GetDecoder(readBuffer).Error()
if err!=nil {
panic(err)
}
return &_{{ $fn.Return}}{}, val
}

ret := &_{{ $fn.Return }}{
instanceId: v,
Expand All @@ -69,6 +77,11 @@ func (d *_{{ $ifc.Name }}) {{ $fn.Name }}(params *{{ $fn.Params }}) ({{ $fn.Retu

ret := &{{ $fn.Return }}{}
r, err := Decode{{ $fn.Return }}(ret, readBuffer)

if err!=nil {
return {{ $fn.Return }}{}, err
}

return *r, err
{{ end }}
}
Expand Down Expand Up @@ -102,10 +115,18 @@ func {{ $fn.Name }}(params *{{ $fn.Params }}) ({{ $fn.Return }}, error) {
// Now make the call to the host.

{{- if (IsInterface $schema $fn.Return) }}
readBuffer = nil
v := ext_{{ $schema.Name }}_{{ $fn.Name }}(0, off, l)
// IF the return type is an interface return ifc, which contains hidden instanceId.

// TODO: Handle error from host. In this case there'll be an error in the readBuffer
// Handle error from host. In this case there'll be an error in the readBuffer
if (readBuffer!=nil) {
val, err := polyglot.GetDecoder(readBuffer).Error()
if err!=nil {
panic(err)
}
return nil, val
}

ret := &_{{ $fn.Return }}{
instanceId: v,
Expand All @@ -118,6 +139,11 @@ func {{ $fn.Name }}(params *{{ $fn.Params }}) ({{ $fn.Return }}, error) {

ret := &{{ $fn.Return }}{}
r, err := Decode{{ $fn.Return }}(ret, readBuffer)

if err!=nil {
return {{ $fn.Return }}{}, err
}

return *r, err
{{ end }}

Expand Down
9 changes: 9 additions & 0 deletions extension/generator/golang/templates/host.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ func (h *{{ $schema.Name }}Host) host_ext_{{ $schema.Name }}_{{ $fn.Name}}(mem e
cd, err := Decode{{ $fn.Params }}(cd, data)
if err != nil {
hostError(mem, resize, err)
return
}

// Call the implementation
r, err := h.impl.{{ $fn.Name }}(cd)
if err!=nil {
hostError(mem, resize, err)
return
}

{{- if (IsInterface $schema $fn.Return) }}
Expand All @@ -127,10 +129,12 @@ func (h *{{ $schema.Name }}Host) host_ext_{{ $schema.Name }}_{{ $fn.Name}}(mem e

if err != nil {
hostError(mem, resize, err)
return
}

if !mem.Write(uint32(writeBuffer), b.Bytes()) {
hostError(mem, resize, err)
return
}

{{ end }}
Expand All @@ -149,6 +153,7 @@ func (h *{{ $schema.Name }}Host) host_ext_{{ $schema.Name }}_{{ $ifc.Name }}_{{
h.instancesLock_{{ $ifc.Name }}.Unlock()
if !ok {
hostError(mem, resize, errors.New("Instance ID not found!"))
return
}

ptr := uint32(params[1])
Expand All @@ -159,11 +164,13 @@ func (h *{{ $schema.Name }}Host) host_ext_{{ $schema.Name }}_{{ $ifc.Name }}_{{
cd, err := Decode{{ $fn.Params }}(cd, data)
if err != nil {
hostError(mem, resize, err)
return
}

resp, err := r.{{ $fn.Name }}(cd)
if err != nil {
hostError(mem, resize, err)
return
}


Expand All @@ -186,10 +193,12 @@ func (h *{{ $schema.Name }}Host) host_ext_{{ $schema.Name }}_{{ $ifc.Name }}_{{

if err != nil {
hostError(mem, resize, err)
return
}

if !mem.Write(uint32(writeBuffer), b.Bytes()) {
hostError(mem, resize, err)
return
}

{{ end }}
Expand Down

0 comments on commit f3f6819

Please sign in to comment.