Skip to content

Commit

Permalink
feat: add version flag for the plugin spec
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Feb 22, 2024
1 parent 9a118b6 commit 2806b54
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 5 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/chai2010/jsonv v1.1.3
github.com/chai2010/protorpc v1.1.4
github.com/coreos/pkg v0.0.0-20240122114842-bbd7aa9bf6fb
github.com/getkin/kin-openapi v0.123.0
github.com/goccy/go-yaml v1.11.3
github.com/gofrs/flock v0.8.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ github.com/containerd/containerd v1.7.0 h1:G/ZQr3gMZs6ZT0qPUZ15znx5QSdQdASW11nXT
github.com/containerd/containerd v1.7.0/go.mod h1:QfR7Efgb/6X2BDpTPJRvPTYDE9rsF0FsXX9J8sIs/sc=
github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg=
github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM=
github.com/coreos/pkg v0.0.0-20240122114842-bbd7aa9bf6fb h1:GIzvVQ9UkUlOhSDlqmrQAAAUd6R3E+caIisNEyWXvNE=
github.com/coreos/pkg v0.0.0-20240122114842-bbd7aa9bf6fb/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
Expand Down
85 changes: 85 additions & 0 deletions pkg/3rdparty/dlopen/dlopen_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2016 CoreOS, Inc.
//
// 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.

//go:build linux || darwin
// +build linux darwin

// Package dlopen provides some convenience functions to dlopen a library and
// get its symbols.
package dlopen

// #cgo LDFLAGS: -ldl
// #include <stdlib.h>
// #include <dlfcn.h>
import "C"
import (
"errors"
"fmt"
"unsafe"
)

var ErrSoNotFound = errors.New("unable to open a handle to the library")

// LibHandle represents an open handle to a library (.so)
type LibHandle struct {
Handle unsafe.Pointer
Libname string
}

// GetHandle tries to get a handle to a library (.so), attempting to access it
// by the names specified in libs and returning the first that is successfully
// opened. Callers are responsible for closing the handler. If no library can
// be successfully opened, an error is returned.
func GetHandle(libs []string) (*LibHandle, error) {
for _, name := range libs {
libname := C.CString(name)
defer C.free(unsafe.Pointer(libname))
handle := C.dlopen(libname, C.RTLD_LAZY)
if handle != nil {
h := &LibHandle{
Handle: handle,
Libname: name,
}
return h, nil
}
}
return nil, ErrSoNotFound
}

// GetSymbolPointer takes a symbol name and returns a pointer to the symbol.
func (l *LibHandle) GetSymbolPointer(symbol string) (unsafe.Pointer, error) {
sym := C.CString(symbol)
defer C.free(unsafe.Pointer(sym))

C.dlerror()
p := C.dlsym(l.Handle, sym)
e := C.dlerror()
if e != nil {
return nil, fmt.Errorf("error resolving symbol %q: %v", symbol, errors.New(C.GoString(e)))
}

return p, nil
}

// Close closes a LibHandle.
func (l *LibHandle) Close() error {
C.dlerror()
C.dlclose(l.Handle)
e := C.dlerror()
if e != nil {
return fmt.Errorf("error closing %v: %v", l.Libname, errors.New(C.GoString(e)))
}

return nil
}
46 changes: 46 additions & 0 deletions pkg/3rdparty/dlopen/dlopen_windws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build windows
// +build windows

// Package dlopen provides some convenience functions to dlopen a library and
// get its symbols.
package dlopen

import (
"errors"
"unsafe"

"syscall"
)

var ErrSoNotFound = errors.New("unable to open a handle to the library")

// LibHandle represents an open handle to a library (.so)
type LibHandle struct {
Handle unsafe.Pointer
Libname string
}

func GetHandle(libs []string) (*LibHandle, error) {
if len(libs) == 0 {
return nil, ErrSoNotFound
}
name := libs[0]
dll, err := syscall.LoadDLL(name)
if err != nil {
return nil, err
}
return LibHandle{

Check failure on line 32 in pkg/3rdparty/dlopen/dlopen_windws.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use LibHandle{…} (value of type LibHandle) as *LibHandle value in return statement
Handle: dll,

Check failure on line 33 in pkg/3rdparty/dlopen/dlopen_windws.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use dll (variable of type *syscall.DLL) as unsafe.Pointer value in struct literal
Libname: name,
}, nil
}

// GetSymbolPointer takes a symbol name and returns a pointer to the symbol.
func (l *LibHandle) GetSymbolPointer(symbol string) (unsafe.Pointer, error) {
return syscall.GetProcAddress(l.Handle, symbol)

Check failure on line 40 in pkg/3rdparty/dlopen/dlopen_windws.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use syscall.GetProcAddress(l.Handle, symbol) (value of type uintptr) as unsafe.Pointer value in return statement

Check failure on line 40 in pkg/3rdparty/dlopen/dlopen_windws.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use l.Handle (variable of type unsafe.Pointer) as syscall.Handle value in argument to syscall.GetProcAddress
}

// Close closes a LibHandle.
func (l *LibHandle) Close() error {
return syscall.FreeLibrary(l.Handle)

Check failure on line 45 in pkg/3rdparty/dlopen/dlopen_windws.go

View workflow job for this annotation

GitHub Actions / build-and-test

cannot use l.Handle (variable of type unsafe.Pointer) as syscall.Handle value in argument to syscall.FreeLibrary
}
2 changes: 1 addition & 1 deletion pkg/native/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"sync"
"unsafe"

"github.com/coreos/pkg/dlopen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"kcl-lang.io/kcl-go/pkg/3rdparty/dlopen"
"kcl-lang.io/kcl-go/pkg/plugin"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/native/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"runtime"

"github.com/coreos/pkg/dlopen"
"kcl-lang.io/kcl-go/pkg/3rdparty/dlopen"
kcl_runtime "kcl-lang.io/kcl-go/pkg/runtime"
)

Expand Down

0 comments on commit 2806b54

Please sign in to comment.