types

package
v0.0.0-...-d03bbad Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2018 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package types implements the language built in primitypes types, the Object type and the Object wrappers for each primitive type.

Documentation: https://es5.github.io/#x8

Index

Constants

This section is empty.

Variables

View Source
var (
	DefValue        = Undefined
	DefWritable     = False
	DefGet          = Undefined
	DefSet          = Undefined
	DefEnumerable   = False
	DefConfigurable = False
)

Default attribute values Table 7 in https://es5.github.io/#x8.6.1

View Source
var Null = null(utf16.S("null"))
View Source
var (
	// S is an alias to make utf-16 strings.
	S = utf16.S
)
View Source
var Undefined = undefined(utf16.S("undefined")) // undefined undefinedness undefining undefined =P

Functions

func CopyProperties

func CopyProperties(dst, src *PropertyDescriptor)

CopyProperties from descriptor src to dst.

func IsPrimitive

func IsPrimitive(val Value) bool

IsPrimitive tells if val is a primitive value.

func IsSameDescriptor

func IsSameDescriptor(a, b *PropertyDescriptor) bool

IsSameDescriptor compares if descriptors are the same.

func IsSameDescriptorSettings

func IsSameDescriptorSettings(a, b *PropertyDescriptor) bool

IsSameDescriptorSettings compares if descriptors are the same ignoring the value (Looking only at the configs).

func StrictEqual

func StrictEqual(a, b Value) bool

StrictEqual compares values a and b using ECMAScript === (strict) rules.

Types

type Bool

type Bool bool

Bool is the ECMAScript Boolean type.

const False Bool = false

False value

const True Bool = true

True value

func NewBool

func NewBool(b bool) Bool

func (Bool) Equal

func (b Bool) Equal(a Bool) bool

func (Bool) IsFalse

func (b Bool) IsFalse() bool

func (Bool) IsTrue

func (b Bool) IsTrue() bool

func (Bool) Kind

func (_ Bool) Kind() Kind

func (Bool) ToBool

func (b Bool) ToBool() Bool

func (Bool) ToNumber

func (b Bool) ToNumber() Number

func (Bool) ToObject

func (b Bool) ToObject() (Object, error)

func (Bool) ToPrimitive

func (b Bool) ToPrimitive(hint Kind) (Value, error)

func (Bool) ToString

func (b Bool) ToString() String

type Builtinfn

type Builtinfn struct {
	*UserFunction
	// contains filtered or unexported fields
}

func NewBuiltinfn

func NewBuiltinfn(fn Execfn) *Builtinfn

func (*Builtinfn) Call

func (f *Builtinfn) Call(this Object, args []Value) Value

func (*Builtinfn) ToObject

func (f *Builtinfn) ToObject() (Object, error)

type DataObject

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

DataObject is a collection of named values.

func NewBaseDataObject

func NewBaseDataObject() *DataObject

NewBaseDataObject is the same as ecmascript code:

Object.create(null);

This is the root of the prototype chain.

func NewDataObject

func NewDataObject(proto Value) *DataObject

NewDataObject creates a new DataObject using proto as prototype.

func NewDataObjectP

func NewDataObjectP(proto *PropertyDescriptor) *DataObject

NewDataObjectP creates an object with PropertyDescriptor proto as prototype attribute. The *everything is an object* concept makes impossible to defineOwnProperties work by passing an object to define an object (recursive definition).

func (*DataObject) CanPut

func (o *DataObject) CanPut(name utf16.Str) bool

func (*DataObject) Class

func (o *DataObject) Class() string

Class returns the object class

func (*DataObject) DefaultValue

func (o *DataObject) DefaultValue(hint Kind) (Value, error)

https://es5.github.io/#x8.12.8

func (*DataObject) DefineOwnProperty

func (o *DataObject) DefineOwnProperty(
	name utf16.Str, desc Value, throw bool,
) (bool, error)

func (*DataObject) DefineOwnPropertyP

func (o *DataObject) DefineOwnPropertyP(
	name utf16.Str, desc *PropertyDescriptor, throw bool,
) (bool, error)

https://es5.github.io/#x8.12.9

func (*DataObject) Get

func (o *DataObject) Get(name utf16.Str) (Value, error)

Get is the default [[Get]] implementation for objects. https://es5.github.io/#x8.12.3

func (*DataObject) GetOwnProperty

func (o *DataObject) GetOwnProperty(name utf16.Str) Value

func (*DataObject) GetProperty

func (o *DataObject) GetProperty(name utf16.Str) Value

func (*DataObject) HasProperty

func (o *DataObject) HasProperty(name utf16.Str) bool

func (*DataObject) IsFalse

func (o *DataObject) IsFalse() bool

IsFalse SHALL return false for objects.

func (*DataObject) IsTrue

func (o *DataObject) IsTrue() bool

IsTrue SHALL return true for objects.

func (*DataObject) Kind

func (*DataObject) Kind() Kind

Kind of type

func (*DataObject) NotExtensible

func (o *DataObject) NotExtensible() bool

func (*DataObject) Put

func (o *DataObject) Put(name utf16.Str, val Value, throw bool) error

Put is the default [[Put]] implementation for Object.

func (*DataObject) String

func (o *DataObject) String() string

func (*DataObject) ToBool

func (*DataObject) ToBool() Bool

ToBool SHALL return a True value for object.

func (*DataObject) ToNumber

func (o *DataObject) ToNumber() Number

ToNumber tries to convert the object into a number using several rules (see ToNumber in the spec) but most important rule is look into the valueOf attribute of the object.

func (*DataObject) ToObject

func (o *DataObject) ToObject() (Object, error)

ToObject returns itself.

func (*DataObject) ToPrimitive

func (o *DataObject) ToPrimitive(hint Kind) (Value, error)

func (*DataObject) ToPropertyDescriptor

func (o *DataObject) ToPropertyDescriptor() *PropertyDescriptor

ToPropertyDescriptor creates a PropertyDescriptor from a DataObject. This is required because property descriptors are defined in ECMAScript using objects.

func (*DataObject) ToString

func (o *DataObject) ToString() String

ToString tries to convert the object into a string. It would look into toString method or the valueOf attribute. See the spec.

type ECMAObject

type ECMAObject interface {
	Get(name utf16.Str) (Value, error)
	CanPut(name utf16.Str) bool
	Put(name utf16.Str, value Value, throw bool) error
	DefineOwnProperty(n utf16.Str, v Value, throw bool) (bool, error)
}

type Execfn

type Execfn func(this Object, args []Value) Value

type Function

type Function interface {
	Object

	Call(this Object, args []Value) Value
}

Function is a kind of Object that's executable, ie. it has a Call method.

type Kind

type Kind int

Kind of type

const (
	KindUndefined Kind = iota
	KindNull
	KindNumber
	KindString
	KindBool
	KindObject
)

func (Kind) String

func (k Kind) String() string

type Number

type Number float64

func NewNumber

func NewNumber(a float64) Number

func (Number) Equal

func (a Number) Equal(b Number) bool

func (Number) IsFalse

func (a Number) IsFalse() bool

func (Number) IsTrue

func (a Number) IsTrue() bool

https://es5.github.io/#x9.2

func (Number) Kind

func (_ Number) Kind() Kind

func (Number) String

func (a Number) String() string

func (Number) ToBool

func (a Number) ToBool() Bool

ToBool returns a Boolean according to: https://es5.github.io/#x9.2

func (Number) ToNumber

func (a Number) ToNumber() Number

ToNumber retrieves the number. https://es5.github.io/#x9.3

func (Number) ToObject

func (a Number) ToObject() (Object, error)

func (Number) ToPrimitive

func (a Number) ToPrimitive(hint Kind) (Value, error)

func (Number) ToString

func (a Number) ToString() String

ToString converts the number to string. Check https://es5.github.io/#x9.8 TODO(i4k): revisit this.

func (Number) Value

func (a Number) Value() float64

type Object

type Object interface {
	ECMAObject

	Class() string

	String() string
	// contains filtered or unexported methods
}

Object is everything that's not a primitive value.

type PropertyDescriptor

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

PropertyDescriptor describes an Object property. An Object is a collection of properties and each property is either a `Named Data Property`, a `Named Acessor Property` or an internal property. PropertyDescriptor describes all of them, why? By ECMA 8.6, the property type should be interpreted by their attributes set (see IsAcessorDescriptor in 8.10.1 and IsDataDescriptor in 8.10.2). A Named Data Property associates a name with an ECMAScript value. Eg.:

this.name = "value";

The code above is the same as:

Object.defineOwnProperty(this.name, {
    value: "value",
    writable: true,
    enumerable: true,
    configurable: true
});

func DefaultPrototypeDesc

func DefaultPrototypeDesc() *PropertyDescriptor

DefaultPrototypeDesc is a base prototype object that extends Null. The root of the prototype-based type hierarchy.

func NewAcessorPropDesc

func NewAcessorPropDesc(get, set Value, enum, cfg bool) *PropertyDescriptor

NewAcessorPropDesc creates a new Acessor Property Descriptor. https://es5.github.io/#x8.10.1

func NewDataPropDesc

func NewDataPropDesc(value Value, wrt, enum, cfg bool) *PropertyDescriptor

NewDataPropDesc creates a new Data Property Descriptor. https://es5.github.io/#x8.10.2

func NewGenericPropDesc

func NewGenericPropDesc() *PropertyDescriptor

NewGenericPropDesc creates a new generic (empty) property descriptor. https://es5.github.io/#x8.10.3

func (*PropertyDescriptor) Cfg

func (p *PropertyDescriptor) Cfg() Value

Cfg returns the [[configurable]] property

func (*PropertyDescriptor) Enum

func (p *PropertyDescriptor) Enum() Value

Enum returns the [[enumerable]] property

func (*PropertyDescriptor) Get

func (p *PropertyDescriptor) Get() Value

Get returns the [[get]] property

func (*PropertyDescriptor) HasCfg

func (p *PropertyDescriptor) HasCfg() bool

HasCfg tells if the property has the 'configurable' property.

func (*PropertyDescriptor) HasEnum

func (p *PropertyDescriptor) HasEnum() bool

HasEnum tells if the property has the 'enumerable' property.

func (*PropertyDescriptor) HasGet

func (p *PropertyDescriptor) HasGet() bool

HasGet tells if the property has the 'get' property.

func (*PropertyDescriptor) HasSet

func (p *PropertyDescriptor) HasSet() bool

HasSet tells if the property has the 'set' property.

func (*PropertyDescriptor) HasValue

func (p *PropertyDescriptor) HasValue() bool

HasValue tells if property has the 'value' attribute.

func (*PropertyDescriptor) HasWritable

func (p *PropertyDescriptor) HasWritable() bool

HasWritable tells if the property has the 'writable' attribute.

func (*PropertyDescriptor) IsAbsentDescriptor

func (p *PropertyDescriptor) IsAbsentDescriptor() bool

func (*PropertyDescriptor) IsAcessorDescriptor

func (p *PropertyDescriptor) IsAcessorDescriptor() bool

func (*PropertyDescriptor) IsDataDescriptor

func (p *PropertyDescriptor) IsDataDescriptor() bool

func (*PropertyDescriptor) IsGenericDescriptor

func (p *PropertyDescriptor) IsGenericDescriptor() bool

IsGenericDescriptor tells if the property is a 'generic descriptor'. Generic descriptor is a descriptor that's not an Acessor Descriptor neither a Data Descriptor.

func (*PropertyDescriptor) Set

func (p *PropertyDescriptor) Set() Value

Set returns the [[set]] property

func (*PropertyDescriptor) SetCfg

func (p *PropertyDescriptor) SetCfg(b Bool)

func (*PropertyDescriptor) SetEnum

func (p *PropertyDescriptor) SetEnum(b Bool)

func (*PropertyDescriptor) SetGet

func (p *PropertyDescriptor) SetGet(get Value)

func (*PropertyDescriptor) SetSet

func (p *PropertyDescriptor) SetSet(set Value)

func (*PropertyDescriptor) SetValue

func (p *PropertyDescriptor) SetValue(v Value)

func (*PropertyDescriptor) SetWritable

func (p *PropertyDescriptor) SetWritable(b Bool)

func (*PropertyDescriptor) ToObject

func (p *PropertyDescriptor) ToObject() *DataObject

func (*PropertyDescriptor) Value

func (p *PropertyDescriptor) Value() Value

Value returns the [[value]] property

func (*PropertyDescriptor) Writable

func (p *PropertyDescriptor) Writable() Value

Writable returns the [[writable]] property

type String

type String utf16.Str

String is the primitive string UTF16-encoded type.

func NewString

func NewString(str string) String

NewString creates a new string from an UTF-8 encoded str.

func (String) Equal

func (a String) Equal(b String) bool

func (String) IsFalse

func (a String) IsFalse() bool

func (String) IsTrue

func (a String) IsTrue() bool

func (String) Kind

func (a String) Kind() Kind

func (String) Length

func (a String) Length() int

func (String) String

func (a String) String() string

func (String) ToBool

func (a String) ToBool() Bool

func (String) ToNumber

func (a String) ToNumber() Number

func (String) ToObject

func (a String) ToObject() (Object, error)

func (String) ToPrimitive

func (a String) ToPrimitive(hint Kind) (Value, error)

func (String) ToString

func (a String) ToString() String

type TypeError

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

func NewTypeError

func NewTypeError(format string, args ...interface{}) TypeError

func (TypeError) Error

func (e TypeError) Error() string

func (TypeError) Exception

func (e TypeError) Exception() bool

type UserFunction

type UserFunction struct {
	*DataObject
	// contains filtered or unexported fields
}

UserFunction is functions defined by user, ie. they are defined in ecmascript code.

func NewUserFunction

func NewUserFunction(
	params []utf16.Str, body *ast.Program, scope interface{}, strict bool,
) *UserFunction

func NewUserFunctionPrototype

func NewUserFunctionPrototype() *UserFunction

func (*UserFunction) Call

func (f *UserFunction) Call(this *Object, params []Value) Value

type Value

type Value interface {
	Kind() Kind

	IsTrue() bool
	IsFalse() bool

	ToPrimitive(hint Kind) (Value, error)
	ToBool() Bool
	ToNumber() Number
	ToString() String
	ToObject() (Object, error)
}

Value is a heavy interface. In JS type coercion is crazy... almost everything could be coerced to something else. Then, better to encapsulate the question regarding what a value can be in the same interface than dealing with concrete types every time. Every type implements the Value interface. It seems to be the recomended approach: http://es5.github.io/#x9

Jump to

Keyboard shortcuts

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