runtime

package module
v0.4.7-rc7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

README

Aspect Runtime

Aspect runtime is a runtime platform for executing aspects. It is a wrapper around the bytecodealliance/wasmtime engine and supports any language that is compatible with WebAssembly (wasm). Building upon the foundation of fixed-length variables supported by wasm, Aspect runtime also enables the use of custom data structures and facilitates communication with the host environment.

System Overview

├── types         // defining all types supported as params.
├── runtime.go    // defining interfaces of aspect runtime.
├── register.go   // defining host functions registered with the runtime.
├── wasmtime.go   // defining wasm implement of interface.
└── wrapper.go    // wrapping registered host functions and handle complex params.

Usage

  1. Register the host functions
    hostFns := runtime.NewHostAPIRegistry()
    hostFns.AddApi("index", "test", "hello", func(arg string) string {
        return "greet to " + arg;
    })
    
  2. Create runtime and pass registry of host function.
    raw := []byte() // raw bytecode
    
    wasmTimeRuntime, err = runtime.NewAspectRuntime(runtime.WASM, raw, hostFns)
    res, err := wasmTimeRuntime.Call("greet", arg)
    if err != nil {
        // handle the error
    }
    output = res.(string) // string is expected
    // handle the output
    wasmTimeRuntime.Destroy() // to destroy the rt, in case of go runtime gc failed
    ...
    
  3. Use the pool.
    To establish a runtime pool with a specific capacity, the pool is designed to cache and provide runtime instances based on their type, bytecode, and host functions. The preRun function is invoked before returning the instance and serves the purpose of clearing any variables or data from the previous run.
    capacity := 10
    pool := NewRuntimePool(capacity)
    
    preRun := "resetFn"
    key, wasmTimeRuntime, err := pool.Runtime(WASM, raw, hostFns, forceClearCache, "resetFn")
    
    
    After using the runtime instance from the pool, it is important to put it back into the pool. Failing to do so would result in the instance being unavailable for subsequent calls that require the same instance.
    pool.PutBack(key, wasmTimeRuntime)
    

License

Copyright © Artela Network, Inc. All rights reserved.

Licensed under the Apache v2 License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAspectRuntime

func NewAspectRuntime(ctx context.Context, logger types.Logger, runtimeType RuntimeType, code []byte, apis *types.HostAPIRegistry) (types.AspectRuntime, error)

NewAspectRuntime is the factory method for creating aspect runtime

Types

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

type EntryList

type EntryList struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewEntryList

func NewEntryList(cap int) *EntryList

func (*EntryList) Len

func (list *EntryList) Len() int

func (*EntryList) PopFront

func (list *EntryList) PopFront(hash Hash) (*Entry, bool)

func (*EntryList) PushFront

func (list *EntryList) PushFront(entry *Entry)

type Hash

type Hash string

type Key

type Key string

type RuntimePool

type RuntimePool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewRuntimePool

func NewRuntimePool(ctx context.Context, logger types.Logger, capacity int) *RuntimePool

func (*RuntimePool) Len

func (pool *RuntimePool) Len() int

func (*RuntimePool) Return

func (pool *RuntimePool) Return(key string, runtime types.AspectRuntime)

Return returns a runtime to the pool

func (*RuntimePool) Runtime

func (pool *RuntimePool) Runtime(ctx context.Context, rtType RuntimeType, code []byte, apis *types.HostAPIRegistry) (string, types.AspectRuntime, error)

type RuntimeType

type RuntimeType int

nolint

const (
	WASM RuntimeType = iota
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL