janet

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 14 Imported by: 0

README

janet

Package janet contains a Janet virtual machine for interoperation between Go and Janet code. Users of its API can register callbacks, define symbols in the Janet environment, execute Janet code, and convert between Go and Janet values.

This code is, admittedly, a little terrifying. Suffice it to say that Janet was not designed to be used from Go, and as a result there are a lot of silly tricks this library uses to ensure we only access memory used by the Janet VM in the goroutine where it was initialized.

Updating Janet

To update the janet version, clone the janet-lang/janet repository and run make, then copy build/c/janet.c, src/include/janet.h, and src/conf/janetconf.h to this directory.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DEFAULT_CALL_OPTIONS = CallOptions{
	UpdateEnv: true,
}
View Source
var ERROR_FREED = fmt.Errorf("cannot use freed value")
View Source
var GO_BOOT_FILE []byte
View Source
var JANET_TYPE_TO_STRING map[C.JanetType]string = map[C.JanetType]string{
	C.JANET_NUMBER:    "number",
	C.JANET_NIL:       "nil",
	C.JANET_BOOLEAN:   "boolean",
	C.JANET_FIBER:     "fiber",
	C.JANET_STRING:    "string",
	C.JANET_SYMBOL:    "symbol",
	C.JANET_KEYWORD:   "keyword",
	C.JANET_ARRAY:     "array",
	C.JANET_TUPLE:     "tuple",
	C.JANET_TABLE:     "table",
	C.JANET_STRUCT:    "struct",
	C.JANET_BUFFER:    "buffer",
	C.JANET_FUNCTION:  "function",
	C.JANET_CFUNCTION: "cfunction",
	C.JANET_ABSTRACT:  "abstract",
	C.JANET_POINTER:   "pointer",
}
View Source
var MARSHALABLE = reflect.TypeOf((*Marshalable)(nil)).Elem()
View Source
var (
	METHOD_REGEX = regexp.MustCompile("^# doc: (\\w+)$")
)
View Source
var UNMARSHALABLE = reflect.TypeOf((*Unmarshalable)(nil)).Elem()

Functions

func IsValidType added in v0.1.6

func IsValidType(value interface{}) bool

IsValidType returns true if the given value can be translated to a Janet value.

Types

type Call

type Call struct {
	Code       []byte
	SourcePath string
	Options    CallOptions
}

func CallBytes

func CallBytes(data []byte) Call

func CallString

func CallString(code string) Call

type CallOptions

type CallOptions struct {
	// Whether to allow the code to mutate the current environment.
	UpdateEnv bool
}

type Callback

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

func (*Callback) Source added in v0.1.17

func (c *Callback) Source() (file string, line int)

type Docstrings added in v0.1.6

type Docstrings interface {
	Docstrings() map[string]string
}

Docstrings provides docstrings for module methods. This is a mapping from the method's Go name to its Janet docstring.

type Documented added in v0.1.6

type Documented interface {
	Documentation() string
}

Documented provides documentation in the form of a Markdown-formatted string. All of the lines following a top-level Markdown header with a title that matches a Go method name will be included as the Janet documentation for that method.

For example, providing a string that looks like this: ``` # doc: SomeMethod This is some documentation # doc: SomeMethodB This is some other documentation ``` Will result in SomeMethod and SomeMethodB having those docstrings.

type Fiber

type Fiber struct {
	*Value
	// contains filtered or unexported fields
}

type Function

type Function struct {
	*Value
	// contains filtered or unexported fields
}

func (*Function) Call

func (f *Function) Call(ctx context.Context, params ...interface{}) error

func (*Function) CallContext

func (f *Function) CallContext(
	ctx context.Context,
	user interface{},
	params ...interface{},
) error

func (*Function) CallResult added in v0.8.0

func (f *Function) CallResult(
	ctx context.Context,
	user interface{},
	params ...interface{},
) (*Value, error)

type Keyword added in v0.1.5

type Keyword string

type Marshalable added in v0.4.0

type Marshalable interface {
	MarshalJanet() interface{}
}

Marshalable lets you define a custom (pure) transformation to perform on this type prior to translating it into a Janet value.

type Named

type Named[T any] struct {
	// contains filtered or unexported fields
}

func (*Named[T]) Values

func (n *Named[T]) Values() T

func (*Named[T]) WithDefault

func (n *Named[T]) WithDefault(defaults T) T

type Params

type Params struct {
	Context context.Context
	User    interface{}
	Result  chan Result
}

func (Params) Error

func (p Params) Error(err error)

func (Params) Ok

func (p Params) Ok()

func (Params) Out

func (p Params) Out(value *Value)

func (Params) Pipe

func (p Params) Pipe() Params

Make new Params with an overwritten Result channel.

func (Params) WaitErr added in v0.8.0

func (p Params) WaitErr() error

WaitErr waits for a fiber to finish executing, ignoring any values it returns or yields.

func (Params) WaitOut added in v0.9.0

func (p Params) WaitOut() (*Value, error)

WaitOut waits for the result of a fiber. If the fiber yields (instead of just returning a result), WaitOut will return an error.

func (Params) WaitResult added in v0.8.0

func (p Params) WaitResult() (*Result, error)

WaitResult waits for the fiber to produce a Result and returns it.

func (Params) Yield added in v0.9.0

func (p Params) Yield(value *Value)

type PartialCallback

type PartialCallback struct {
	Type reflect.Type
	// contains filtered or unexported fields
}

func (*PartialCallback) Call

func (p *PartialCallback) Call() []reflect.Value

type Renamable

type Renamable interface {
	Renames() map[string]string
}

Renamable provides a mapping from Go method names to Janet function names. This is useful for declaring Janet functions that are not valid Go, such as "boolean?".

type Request

type Request interface{}

type Result

type Result struct {
	Out   *Value
	Yield *Value
	Error error
}

Result is the value produced when a fiber finishes executing. Fibers can halt in three ways: - They can return a value, which is stored in Out. - They can yield (with (yield)), which is stored in Yield. - They can error, the message for which is stored in Error.

type Table

type Table struct {
	*Value
	// contains filtered or unexported fields
}

type Unmarshalable added in v0.4.0

type Unmarshalable interface {
	UnmarshalJanet(value *Value) error
}

Unmarshalable lets you define custom code for turning a Janet value into this type.

type VM

type VM struct {
	deadlock.RWMutex
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context) (*VM, error)

func (*VM) Callback

func (v *VM) Callback(name string, docstring string, callback interface{}) error

func (*VM) Env added in v0.1.6

func (v *VM) Env() *Table

func (*VM) Execute

func (v *VM) Execute(ctx context.Context, code string) error

func (*VM) ExecuteCall

func (v *VM) ExecuteCall(
	ctx context.Context,
	user interface{},
	call Call,
) (*Result, error)

ExecuteCall executes a Call, which is the lowest-level interface for running Janet code.

func (*VM) ExecuteFile

func (v *VM) ExecuteFile(ctx context.Context, path string) error

ExecuteFile executes a file containing Janet code.

func (*VM) Lookup added in v0.1.17

func (v *VM) Lookup(name string) (callback *Callback, ok bool)

Lookup finds a previously registered callback by its Janet name and returns it.

func (*VM) Marshal added in v0.4.0

func (v *VM) Marshal(source interface{}) (value *Value, err error)

Marshal sends a marshalRequest to the VM and waits for the result.

func (*VM) Module

func (v *VM) Module(name string, module interface{}) error

type Value

type Value struct {
	deadlock.RWMutex
	// contains filtered or unexported fields
}

func (*Value) Free

func (v *Value) Free()

func (*Value) IsFree

func (v *Value) IsFree() bool

func (*Value) JSON added in v0.9.0

func (v *Value) JSON() ([]byte, error)

func (*Value) Nil added in v0.5.0

func (v *Value) Nil() bool

func (*Value) Raw added in v0.9.0

func (v *Value) Raw() ([]byte, error)

func (*Value) String added in v0.4.0

func (v *Value) String() string

func (*Value) Unmarshal

func (v *Value) Unmarshal(dest interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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