Skip to content
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

Jm/ext - Go and Rust extension support. #112

Merged
merged 35 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
254c8aa
Initial extension generator code
jimmyaxod Sep 11, 2023
dbb33de
First version with guest/host generated in cli working
jimmyaxod Sep 11, 2023
1ea93c4
Couple changes
jimmyaxod Sep 14, 2023
56febe7
First ver compile golang guest
jimmyaxod Sep 14, 2023
7be8265
Updated to first version working guest+host in go
jimmyaxod Sep 15, 2023
b07d9bb
Extensions now get reset at start of a run
jimmyaxod Sep 18, 2023
ba0ed47
Updated golang ext generator test to prevent regression
jimmyaxod Sep 19, 2023
2b6c75f
Updated to use scale-extension-interfaces
jimmyaxod Sep 20, 2023
d40f11f
Updated extension host go.mod to include correct ver of scale-extensi…
jimmyaxod Sep 20, 2023
fcd2b97
Started on rust extension impl
jimmyaxod Sep 21, 2023
18c965d
Fixup from merge error
jimmyaxod Sep 21, 2023
0e6a79d
Fixup for golang extension
jimmyaxod Sep 22, 2023
0efa15b
Initial rust generator for types from signature to extension
jimmyaxod Sep 22, 2023
e89b8c8
Updated ext generator for rust
jimmyaxod Sep 22, 2023
7a0be85
Fixed issue with accessors missing
jimmyaxod Sep 25, 2023
4346b4c
Updated for rust compile
jimmyaxod Sep 25, 2023
07ddf3f
Latest updates
jimmyaxod Sep 28, 2023
ac49685
Latest rust guest ext working
jimmyaxod Sep 28, 2023
dc4c13f
Added error handling in go ext
jimmyaxod Sep 29, 2023
4be72ba
Updated rust ext to deal with errors correctly
jimmyaxod Sep 29, 2023
7c36219
Updated to use extension hash rather than name in wasm fns
jimmyaxod Oct 3, 2023
20524df
Fixing for tests
ShivanshVij Oct 11, 2023
68b2742
Fixing for tests
ShivanshVij Oct 11, 2023
ab306de
Renaming extension info temporarily
ShivanshVij Oct 11, 2023
527aa33
Pulled in changes from staging, and fixed things up. e2e tests workin…
jimmyaxod Nov 22, 2023
0bd2407
Removed unnecessary templates etc
jimmyaxod Nov 22, 2023
925f3aa
Added integration tests for golang host, golang guest and rust guest …
jimmyaxod Nov 23, 2023
489363c
Added tags/comment header
jimmyaxod Nov 23, 2023
bd6be24
Fixed ext int test
jimmyaxod Nov 23, 2023
8d58983
Added generator for extension int tests
jimmyaxod Nov 23, 2023
6d37cce
Small fix
jimmyaxod Nov 23, 2023
f1ffcc0
Lint fix
jimmyaxod Nov 23, 2023
83dca76
Lint
jimmyaxod Nov 23, 2023
8453759
Changed to copy replacements from go.mod rather than explicitly outpu…
jimmyaxod Nov 29, 2023
f9a4e18
Fixed go mod replace for versions
jimmyaxod Nov 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type LocalGolangOptions struct {

// Args are the optional arguments to pass to the compiler
Args []string

Extensions []extension.Info
}

func LocalGolang(options *LocalGolangOptions) (*scalefunc.V1BetaSchema, error) {
Expand Down Expand Up @@ -192,7 +194,24 @@ func LocalGolang(options *LocalGolangOptions) (*scalefunc.V1BetaSchema, error) {
_ = options.Storage.Delete(build)
}()

modfile, err := golang.GenerateGoModfile(signatureInfo, functionInfo)
// Copy over any replacements from the go.mod
replacements := make([]golang.GoModReplacement, 0)

r := manifest.GetReplacements()
for _, resp := range r {
// Check if the target is a local dir...
newPath := resp.New.Path

if !filepath.IsAbs(newPath) {
newPath = filepath.Join(options.SourceDirectory, newPath)
}
replacements = append(replacements, golang.GoModReplacement{
Name: fmt.Sprintf("%s %s", resp.Old.Path, resp.Old.Version),
Path: fmt.Sprintf("%s %s", newPath, resp.New.Version),
})
}

modfile, err := golang.GenerateGoModfile(signatureInfo, functionInfo, replacements)
if err != nil {
return nil, fmt.Errorf("unable to generate go.mod file: %w", err)
}
Expand Down
16 changes: 11 additions & 5 deletions compile/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ import (

var generator *Generator

func GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo) ([]byte, error) {
return generator.GenerateGoModfile(signatureInfo, functionInfo)
type GoModReplacement struct {
Name string
Path string
}

func GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo, replacements []GoModReplacement) ([]byte, error) {
return generator.GenerateGoModfile(signatureInfo, functionInfo, replacements)
}

func GenerateGoMain(scalefileSchema *scalefile.Schema, signatureSchema *signature.Schema, functionInfo *FunctionInfo) ([]byte, error) {
Expand All @@ -50,14 +55,15 @@ func New() *Generator {
}
}

func (g *Generator) GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo) ([]byte, error) {
func (g *Generator) GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo, replacements []GoModReplacement) ([]byte, error) {
signatureInfo.normalize()
functionInfo.normalize()

buf := new(bytes.Buffer)
err := g.template.ExecuteTemplate(buf, "mod.go.templ", map[string]interface{}{
"function": functionInfo,
"signature": signatureInfo,
"function": functionInfo,
"signature": signatureInfo,
"replacements": replacements,
})
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions compile/golang/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func ParseManifest(data []byte) (*Manifest, error) {
}, nil
}

func (m *Manifest) GetReplacements() []*modfile.Replace {
return m.modfile.Replace
}

func (m *Manifest) AddReplacement(oldDependency string, oldVersion string, newDependency string, newVersion string) error {
return m.modfile.AddReplace(oldDependency, oldVersion, newDependency, newVersion)
}
Expand Down
7 changes: 6 additions & 1 deletion compile/golang/templates/mod.go.templ
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module compile

go 1.20

{{ if .signature.Local }}
replace signature v0.1.0 => {{ .signature.ImportPath }}
{{ else }}
Expand All @@ -9,7 +10,11 @@ replace signature v0.1.0 => {{ .signature.ImportPath }} {{ .signature.ImportVers

replace {{ .function.PackageName }} v0.1.0 => {{ .function.ImportPath }}

{{ range $replacements := .replacements }}
replace {{ $replacements.Name }} => {{ $replacements.Path }}
{{ end }}

require (
signature v0.1.0
{{ .function.PackageName }} v0.1.0
)
)
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"regexp"

extension "github.com/loopholelabs/scale-extension-interfaces"
interfaces "github.com/loopholelabs/scale-signature-interfaces"
"github.com/loopholelabs/scale/scalefunc"
)
Expand Down Expand Up @@ -50,6 +51,7 @@ type Config[T interfaces.Signature] struct {
stdout io.Writer
stderr io.Writer
rawOutput bool
extensions []extension.Extension
}

// NewConfig returns a new Scale Runtime Config
Expand Down Expand Up @@ -85,6 +87,11 @@ func (c *Config[T]) validate() error {
return nil
}

func (c *Config[T]) WithExtension(e extension.Extension) *Config[T] {
c.extensions = append(c.extensions, e)
return c
}

func (c *Config[T]) WithSignature(newSignature interfaces.New[T]) *Config[T] {
c.newSignature = newSignature
return c
Expand Down
88 changes: 88 additions & 0 deletions extension/generator/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2023 Loophole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generator

import (
"bytes"
"io"
"io/fs"
"os"
"time"

"golang.org/x/mod/zip"
)

var _ zip.File = (*File)(nil)
var _ os.FileInfo = (*File)(nil)

type File struct {
name string
path string
content []byte
reader *bytes.Reader
size int64
}

func NewFile(name string, path string, content []byte) File {
return File{
name: name,
path: path,
content: content,
reader: bytes.NewReader(content),
size: int64(len(content)),
}
}

func (g File) Name() string {
return g.name
}

func (g File) Size() int64 {
return g.size
}

func (g File) Mode() fs.FileMode {
return 0700
}

func (g File) ModTime() time.Time {
return time.Now()
}

func (g File) IsDir() bool {
return false
}

func (g File) Sys() any {
return g.content
}

func (g File) Path() string {
return g.path
}

func (g File) Lstat() (os.FileInfo, error) {
return g, nil
}

func (g File) Open() (io.ReadCloser, error) {
return io.NopCloser(g.reader), nil
}

func (g File) Data() []byte {
return g.content
}
163 changes: 163 additions & 0 deletions extension/generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
Copyright 2023 Loophole Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generator

import (
"bytes"
"encoding/hex"

"github.com/loopholelabs/scale/extension"
"github.com/loopholelabs/scale/extension/generator/golang"
"github.com/loopholelabs/scale/extension/generator/rust"
)

type GuestRegistryPackage struct {
GolangModule *bytes.Buffer
GolangModfile []byte
RustCrate *bytes.Buffer
RustCargofile []byte
TypescriptPackage *bytes.Buffer
TypescriptPackageJSON []byte
}

type GuestLocalPackage struct {
GolangFiles []File
RustFiles []File
TypescriptFiles []File
}

type HostRegistryPackage struct {
GolangModule *bytes.Buffer
GolangModfile []byte
TypescriptPackage *bytes.Buffer
TypescriptPackageJSON []byte
}

type HostLocalPackage struct {
GolangFiles []File
TypescriptFiles []File
}

type Options struct {
Extension *extension.Schema

GolangPackageImportPath string
GolangPackageName string

RustPackageName string
RustPackageVersion string

TypescriptPackageName string
TypescriptPackageVersion string
}

func GenerateGuestLocal(options *Options) (*GuestLocalPackage, error) {
hash, err := options.Extension.Hash()
if err != nil {
return nil, err
}
hashString := hex.EncodeToString(hash)

golangTypes, err := golang.GenerateTypes(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
}

golangGuest, err := golang.GenerateGuest(options.Extension, hashString, options.GolangPackageName)
if err != nil {
return nil, err
}

golangInterfaces, err := golang.GenerateInterfaces(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
}

modfile, err := golang.GenerateModfile(options.GolangPackageName)
if err != nil {
return nil, err
}

golangFiles := []File{
NewFile("types.go", "types.go", golangTypes),
NewFile("guest.go", "guest.go", golangGuest),
NewFile("interfaces.go", "interfaces.go", golangInterfaces),
NewFile("go.mod", "go.mod", modfile),
}

rustTypes, err := rust.GenerateTypes(options.Extension, options.RustPackageName)
if err != nil {
return nil, err
}

rustGuest, err := rust.GenerateGuest(options.Extension, hashString, options.RustPackageName)
if err != nil {
return nil, err
}

cargofile, err := rust.GenerateCargofile(options.RustPackageName, options.RustPackageVersion)
if err != nil {
return nil, err
}

rustFiles := []File{
NewFile("types.rs", "types.rs", rustTypes),
NewFile("guest.rs", "guest.rs", rustGuest),
NewFile("Cargo.toml", "Cargo.toml", cargofile),
}

return &GuestLocalPackage{
GolangFiles: golangFiles,
RustFiles: rustFiles,
}, nil
}

func GenerateHostLocal(options *Options) (*HostLocalPackage, error) {
hash, err := options.Extension.Hash()
if err != nil {
return nil, err
}
hashString := hex.EncodeToString(hash)

golangTypes, err := golang.GenerateTypes(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
}

golangHost, err := golang.GenerateHost(options.Extension, hashString, options.GolangPackageName)
if err != nil {
return nil, err
}

golangInterfaces, err := golang.GenerateInterfaces(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
}

modfile, err := golang.GenerateModfile(options.GolangPackageName)
if err != nil {
return nil, err
}

golangFiles := []File{
NewFile("types.go", "types.go", golangTypes),
NewFile("host.go", "host.go", golangHost),
NewFile("interfaces.go", "interfaces.go", golangInterfaces),
NewFile("go.mod", "go.mod", modfile),
}

return &HostLocalPackage{
GolangFiles: golangFiles,
}, nil
}
Loading