runtime

package
v0.0.0-...-b91d579 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInterrupted = errors.New("interrupted")

ErrInterrupted is the error Err returns once Cancel has been called -- and the value every injected interrupt check panics with (see pkg/compiler's injectInterruptChecks), since Go has no way to forcibly stop a running goroutine: a loop can only be asked to notice and stop itself.

Functions

This section is empty.

Types

type Bundle

type Bundle map[string]any

Bundle maps a MIME type to its payload, mirroring the shape of Jupyter's `data` field exactly so no conversion is needed on the way to the socket. Binary payloads (images) are base64-encoded strings, as the protocol requires.

type Context

type Context struct {
	Registry *Registry
	Types    *TypeRegistry
	// contains filtered or unexported fields
}

Context is the object passed to the Execute(ctx) function of every plugin.

func Current

func Current() *Context

Current returns the Context of the running session, or nil if none has been created. pkg/display is a separate package and has no other way to reach it.

func NewContext

func NewContext(reg *Registry, tr *TypeRegistry) *Context

NewContext initializes an execution Context and binds it as the current one.

The binding lives here rather than behind an exported setter for two reasons: session.New already calls this exactly once, so it cannot be forgotten, and nothing new is exported for cell code to misuse.

func (*Context) Cancel

func (c *Context) Cancel()

Cancel flags the cell currently running against this Context as interrupted. Safe to call from a different goroutine than the one executing the cell (e.g. the ZMQ Control loop handling an interrupt_request while the Shell loop is blocked inside Execute).

func (*Context) Display

func (c *Context) Display(o Output)

Display queues an Output to be published as a display_data message. Unlike the single SetAutoResult slot, this appends: a cell may show as many things as it likes.

func (*Context) DisplayWithID

func (c *Context) DisplayWithID(o Output, displayID string, update bool)

DisplayWithID shows an Output under a handle the frontend can find again. update=false creates the output, update=true replaces whatever is already showing under that id -- including outputs from earlier cells, which is how a single progress bar can be driven from several of them.

func (*Context) Err

func (c *Context) Err() error

Err returns ErrInterrupted if Cancel has been called since the last ResetCancel, nil otherwise. Every injected loop check calls this.

func (*Context) GetPointer

func (c *Context) GetPointer(name string) unsafe.Pointer

GetPointer retrieves the raw memory address of a symbol by its name.

func (*Context) HasInput

func (c *Context) HasInput() bool

HasInput reports whether anything can be prompted. The session uses it to decide whether the cell is running somewhere with a frontend attached.

func (*Context) Input

func (c *Context) Input(prompt string, password bool) (string, error)

Input prompts the user and blocks until they answer.

func (*Context) ResetCancel

func (c *Context) ResetCancel()

ResetCancel clears a previous interrupt before a new cell starts, so a resolved interrupt from one cell can never bleed into the next.

func (*Context) SetAutoResult

func (c *Context) SetAutoResult(v any)

SetAutoResult records a cell's last expression, displayed by the kernel as an execute_result (equivalent to Jupyter's "Out[n]"). Generated code passes the expression itself rather than a pre-rendered string, so the choice between a rich representation and %#v is made here, in one testable place, instead of in the code generator.

func (*Context) SetDisplayHook

func (c *Context) SetDisplayHook(fn func(o Output, displayID string, update bool))

SetDisplayHook installs live publishing. Once set, Display goes out as it is called rather than at the end of the cell -- which is what makes an in-place update mean anything: a progress bar cannot progress if every frame is flushed after the cell has already finished.

func (*Context) SetInputFunc

func (c *Context) SetInputFunc(fn func(prompt string, password bool) (string, error))

SetInputFunc installs the hook that Input uses. Called once at kernel start.

func (*Context) SetPointer

func (c *Context) SetPointer(name string, typeName string, ptr unsafe.Pointer, keepAlive any)

SetPointer records the raw memory address, type name, and a GC KeepAlive reference.

func (*Context) StdContext

func (c *Context) StdContext() context.Context

StdContext returns a standard context.Context, cancelled the moment Cancel is called. Every generated Execute() declares it as the cell-local `_ctx`, so user code can hand it to any idiomatic Go API that accepts one (net/http, os/exec, database/sql, a raw `select` on Done()) -- reaching blocking calls the AST-injected loop checks cannot: those only ever run between iterations of a `for` loop in the cell's own code, never inside a call Go has already descended into.

func (*Context) TakeDisplays

func (c *Context) TakeDisplays() []Output

TakeDisplays returns everything queued since the last call and clears the queue.

Known limitation, the display twin of the captured-stdout bleed-through already documented in the README: draining happens per cell, so an Output queued by a background goroutine between cells is published under whichever cell runs next. The print is never lost, only misattributed.

func (*Context) TakeResult

func (c *Context) TakeResult() (Output, bool)

TakeResult returns the current cell's displayed result and resets the state for the next execution. The kernel calls this method once per cell.

type MIMEBundler

type MIMEBundler interface {
	MIMEBundle() Output
}

MIMEBundler is implemented by types that know how to render themselves. It is declared here, next to the type switch that consumes it, because the switch can only match the interface it was compiled against -- pkg/display re-exports it as an alias so notebook code never has to name this package.

type Output

type Output struct {
	Data Bundle
	Meta map[string]any
}

Output is one thing a cell chose to show: a Bundle plus the optional per-MIME metadata Jupyter carries alongside it. Meta is nil in the common case; it exists because that is where an image's display size lives (`{"image/png": {"width": 400}}`), which a bare Bundle cannot express.

func Text

func Text(s string) Output

Text builds the plain-text Output that every value falls back to.

type Registry

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

Registry manages the thread-safe global symbol table.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new symbol registry.

func (*Registry) AllSymbols

func (r *Registry) AllSymbols() map[string]*Symbol

AllSymbols returns a copy of all registered symbols.

func (*Registry) GetPointer

func (r *Registry) GetPointer(name string) unsafe.Pointer

GetPointer returns the pointer to the data associated with the symbol `name`.

func (*Registry) GetSymbol

func (r *Registry) GetSymbol(name string) (*Symbol, bool)

GetSymbol returns the full symbol and whether it is present.

func (*Registry) SetPointer

func (r *Registry) SetPointer(name string, typeName string, ptr unsafe.Pointer, keepAlive any)

SetPointer registers or updates a symbol in the registry.

type Symbol

type Symbol struct {
	Name      string
	TypeName  string
	Ptr       unsafe.Pointer
	KeepAlive any
	CreatedAt uint64
}

Symbol represents a symbol exported by a cell in memory.

type TypeRegistry

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

TypeRegistry stores type and function/method declarations that can be re-injected.

func NewTypeRegistry

func NewTypeRegistry() *TypeRegistry

NewTypeRegistry creates a new declaration registry.

func (*TypeRegistry) AllTypes

func (tr *TypeRegistry) AllTypes() map[string]string

AllTypes returns a copy of all registered declarations.

func (*TypeRegistry) RegisterType

func (tr *TypeRegistry) RegisterType(name string, code string)

RegisterType registers a type, method, or function declaration.

Jump to

Keyboard shortcuts

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