core

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

Package core holds the part of PHP's standard library that computes: strings, arrays, json, the language constructs exposed as functions, the tokenizer, the environment and the platform surface a program expects to find before it does anything interesting. It also holds the phpscript extensions that are each too small to be worth a package: SharedMemory, defer and register_shutdown_function.

The line against stdlib/compat is behavioural, not historical: compat holds the surface whose behaviour is the interpreter's (output buffering, preg_*, the date functions), core holds the surface whose behaviour is its own.

Every file registers its own area from its own init(), so an area is added, moved or dropped without touching another file. Importing the package is what wires them in, the way a program imports a database/sql driver:

import _ "github.com/titpetric/phpscript/stdlib/core"

stdlib/imports.go does that for you.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterDefer

func RegisterDefer(rt *runner.Runtime)

RegisterDefer installs defer() in the global function namespace.

func RegisterSharedMemory

func RegisterSharedMemory(rt *runner.Runtime)

RegisterSharedMemory installs SharedMemory in the runtime.

func RegisterShutdown

func RegisterShutdown(rt *runner.Runtime)

RegisterShutdown installs register_shutdown_function() in the global function namespace. Callbacks run in registration order when Runtime.Run finishes, including after exit or an execution error.

func SharedMemoryContext

func SharedMemoryContext(ctx context.Context, s *SharedMemory) context.Context

SharedMemoryContext binds an SharedMemory instance into the context.

Types

type JSONDecoder added in v0.5.0

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

JSONDecoder reads JSON values from a stream, one call per value.

func NewJSONDecoder added in v0.5.0

func NewJSONDecoder(stream io.Reader) (*JSONDecoder, error)

NewJSONDecoder returns a decoder reading from $stream.

UseNumber is on, as it is in json_decode(): without it Go reads every number as a float, and the 7 in a document would come back as 7.0. With it a whole number is an int and only a fractional one is a float, which is what php answers and what a row written back out has to preserve.

func (*JSONDecoder) Decode added in v0.5.0

func (d *JSONDecoder) Decode() (any, error)

Decode reads the next value from the stream and returns it, or throws at the end of the stream - a null is a value JSON carries, so `while ($d->more())` is the loop rather than a test against what this returned.

It decodes through json_decode()'s own walk rather than json.Decoder.Decode, which would build a map and lose the key order the two spellings agree on.

func (*JSONDecoder) More added in v0.5.0

func (d *JSONDecoder) More() bool

More is json.Decoder.More: another value is waiting in the stream. It is what ends a decode loop.

type JSONEncoder added in v0.5.0

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

JSONEncoder writes JSON values to a stream. It is Go's json.Encoder with the one difference PHP forces: the value it is given is a PHP value, so it is converted the way json_encode() converts one, and the two spellings answer the same JSON for the same array.

func NewJSONEncoder added in v0.5.0

func NewJSONEncoder(stream io.Writer) (*JSONEncoder, error)

NewJSONEncoder returns an encoder writing to $stream.

func (*JSONEncoder) Encode added in v0.5.0

func (e *JSONEncoder) Encode(value any) error

encode writes $value to the stream as JSON, followed by a newline.

The newline is Go's, not php's: an Encoder writes a stream of values, and the newline is what separates one from the next. A single value followed by a newline is still valid JSON to any reader.

func (*JSONEncoder) SetIndent added in v0.5.0

func (e *JSONEncoder) SetIndent(prefix, indent string)

SetIndent is json.Encoder.SetIndent: each value is written across several lines, $indent per level under $prefix, and two empty strings go back to one line per value.

type SharedMemory

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

SharedMemory is a thread-safe in-memory key-value and atomic counter store exposed as SharedMemory to scripts.

func NewSharedMemory

func NewSharedMemory() *SharedMemory

NewSharedMemory returns a new empty SharedMemory instance.

func NewSharedMemoryBinding

func NewSharedMemoryBinding(ctx context.Context) (*SharedMemory, error)

NewSharedMemoryBinding is a key-value and counter store shared across requests: `new SharedMemory` returns the store the host bound into the runtime context, or a fresh empty store when none is bound.

func (*SharedMemory) Clear

func (s *SharedMemory) Clear(_ context.Context)

Clear resets all keys and counters.

func (*SharedMemory) Count

func (s *SharedMemory) Count(_ context.Context, key string) string

Count returns a counter as a formatted string.

func (*SharedMemory) Delete

func (s *SharedMemory) Delete(_ context.Context, key string) bool

Delete removes a key from storage.

func (*SharedMemory) Get

func (s *SharedMemory) Get(_ context.Context, key string) string

Get retrieves a string value by key, or returns empty string if missing.

func (*SharedMemory) Has

func (s *SharedMemory) Has(_ context.Context, key string) bool

Has checks if a key exists in data or counters.

func (*SharedMemory) Incr

func (s *SharedMemory) Incr(_ context.Context, key string) int64

Incr atomically increments and returns a counter.

func (*SharedMemory) Set

func (s *SharedMemory) Set(_ context.Context, key, value string)

Set stores a string value.

func (*SharedMemory) Usage added in v0.6.0

func (s *SharedMemory) Usage() (entries, counters int, bytes int64)

Usage reports what the store holds: how many entries, how many counters, and the bytes of the keys and values behind them.

The byte figure is the strings themselves, not the map overhead around them, which is the same basis memory_get_usage() reports a PHP value on.

Jump to

Keyboard shortcuts

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