Skip to content

Commit

Permalink
Clean up code loader in exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
smoynes committed Nov 22, 2023
1 parent a167b9e commit e6bee76
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions internal/cli/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,12 @@ func (ex *executor) Run(ctx context.Context, args []string, stdout io.Writer, lo
log.LogLevel.Set(ex.logLevel)

// Code translated is encoded in a hex-based encoding.
hex := encoding.HexEncoding{}

code, err := ex.loadCode(args[0])
if err != nil {
logger.Error("Error loading code", "err", err)
return -1
}

if err = hex.UnmarshalText(code); err != nil {
logger.Error(err.Error())
return -2
}

ctx, cancel := context.WithCancelCause(ctx)
defer cancel(context.Canceled)

Expand All @@ -89,8 +82,8 @@ func (ex *executor) Run(ctx context.Context, args []string, stdout io.Writer, lo
loader := vm.NewLoader(machine)
count := uint16(0)

for i := range hex.Code {
n, err := loader.Load(hex.Code[i])
for i := range code {
n, err := loader.Load(code[i])
count += n

if err != nil {
Expand Down Expand Up @@ -152,21 +145,28 @@ func (ex *executor) Run(ctx context.Context, args []string, stdout io.Writer, lo
}
}

func (ex executor) loadCode(fn string) (program []byte, err error) {
func (ex executor) loadCode(fn string) ([]vm.ObjectCode, error) {
ex.log.Debug("Loading executable", "file", fn)

file, err := os.Open(fn)
if err != nil {
return
return nil, err
}

program, err = io.ReadAll(file)
code, err := io.ReadAll(file)
if err != nil {
ex.log.Error(err.Error())
return
return nil, err
}

ex.log.Debug("Loaded file", "bytes", len(program))
ex.log.Debug("Loaded file", "bytes", len(code))

hex := encoding.HexEncoding{}

if err = hex.UnmarshalText(code); err != nil {
ex.log.Error(err.Error())
return nil, err
}

return program, nil
return hex.Code, nil
}

0 comments on commit e6bee76

Please sign in to comment.