Skip to content

Commit

Permalink
fix: refactor vm initialization code into method
Browse files Browse the repository at this point in the history
Signed-off-by: Soumil Paranjpay <[email protected]>
  • Loading branch information
Soumil-07 committed Dec 12, 2023
1 parent 672dfeb commit eec817d
Showing 1 changed file with 35 additions and 48 deletions.
83 changes: 35 additions & 48 deletions transformer/external/wasmtransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,7 @@ func (t *WASM) GetConfig() (transformertypes.Transformer, *environment.Environme

// DirectoryDetect runs detect in each sub directory
func (t *WASM) DirectoryDetect(dir string) (map[string][]transformertypes.Artifact, error) {
wasmedge.SetLogErrorLevel()
conf := wasmedge.NewConfigure(wasmedge.WASI)
vm := wasmedge.NewVMWithConfig(conf)

wasi := vm.GetImportModule(wasmedge.WASI)
wasi.InitWasi(
[]string{},
t.prepareEnv(),
[]string{dir + ":" + dir},
)

err := vm.LoadWasmFile(t.WASMConfig.WASMModule)
if err != nil {
return nil, err
}
err = vm.Validate()
if err != nil {
return nil, err
}
err = vm.Instantiate()
if err != nil {
return nil, err
}
_, err = vm.Execute("_start")
vm, err := t.initVm([]string{dir + ":" + dir})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,11 +134,6 @@ func (t *WASM) Transform(newArtifacts []transformertypes.Artifact, alreadySeenAr
}
dataStr := string(dataByt)

wasmedge.SetLogErrorLevel()
conf := wasmedge.NewConfigure(wasmedge.WASI)
vm := wasmedge.NewVMWithConfig(conf)

wasi := vm.GetImportModule(wasmedge.WASI)
preopens := []string{}
for _, artifact := range newArtifacts {
for _, paths := range artifact.Paths {
Expand Down Expand Up @@ -199,28 +171,11 @@ func (t *WASM) Transform(newArtifacts []transformertypes.Artifact, alreadySeenAr
finalPreopens = append(finalPreopens, path+":"+path)
}

wasi.InitWasi(
[]string{},
t.prepareEnv(),
finalPreopens,
)

err = vm.LoadWasmFile(t.WASMConfig.WASMModule)
if err != nil {
return nil, nil, err
}
err = vm.Validate()
if err != nil {
return nil, nil, err
}
err = vm.Instantiate()
if err != nil {
return nil, nil, err
}
_, err = vm.Execute("_start")
vm, err := t.initVm(finalPreopens)
if err != nil {
return nil, nil, err
}

allocateResult, err := vm.Execute("malloc", int32(len(dataStr)+1))
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -253,3 +208,35 @@ func (t *WASM) Transform(newArtifacts []transformertypes.Artifact, alreadySeenAr
createdArtifacts = append(createdArtifacts, output.CreatedArtifacts...)
return pathMappings, createdArtifacts, err
}

func (t *WASM) initVm(preopens []string) (*wasmedge.VM, error) {
wasmedge.SetLogErrorLevel()
conf := wasmedge.NewConfigure(wasmedge.WASI)
vm := wasmedge.NewVMWithConfig(conf)

wasi := vm.GetImportModule(wasmedge.WASI)
wasi.InitWasi(
[]string{},
t.prepareEnv(),
preopens,
)

err := vm.LoadWasmFile(t.WASMConfig.WASMModule)
if err != nil {
return nil, err
}
err = vm.Validate()
if err != nil {
return nil, err
}
err = vm.Instantiate()
if err != nil {
return nil, err
}
_, err = vm.Execute("_start")
if err != nil {
return nil, err
}

return vm, nil
}

0 comments on commit eec817d

Please sign in to comment.