Jerryscript::Value Class Reference

Represents a Jerryscript value. More...

#include <Types.h>

Inheritance diagram for Jerryscript::Value:

Classes

struct  As
 Used by as() method. More...
 

Public Types

using List = Vector< Value >
 

Public Member Functions

 Value ()
 Construct An empty (unused) value. More...
 
 Value (const OwnedValue &value)
 Construct a Value and take ownership of the given native value. More...
 
 Value (const CopyValue &value)
 Construct a Value using a copy (or reference to) the given native value. More...
 
 Value (const StringValue &value)
 Construct a string Value from the given native value. More...
 
 Value (const Undefined &value)
 
 Value (const Null &value)
 
 Value (const Value &value)
 Copy constructor. More...
 
 Value (Value &&value)
 Move constructor. More...
 
 ~Value ()
 
Valueoperator= (const Value &value)
 Assignment copy operator. More...
 
Valueoperator= (Value &&value)
 Assignment move operator. More...
 
Valuereset (jerry_value_t value=jerry_value_t(Ecma::VALUE_EMPTY))
 Reset contents of object to new value (default is unassigned) More...
 
jerry_value_t release ()
 Get raw/native value and release ownership. More...
 
Type type () const
 Get value type. More...
 
Object toObject () const
 Create a new object from this value. More...
 
Value toString () const
 Create a new string value from this value. More...
 
size_t readString (unsigned offset, char *buffer, size_t length) const
 Get content from within a string value. More...
 
String subString (unsigned offset, size_t length) const
 Get content from within a string value. More...
 
 operator String () const
 Support intrinsic cast to Wiring String. More...
 
template<typename T >
as () const
 Get value of object with specific type. e.g. value.as<int>(). More...
 
Construct a value from a simple type
 Value (int value)
 Integer. More...
 
 Value (unsigned value)
 Unsigned integer. More...
 
 Value (double value)
 floating-point More...
 
 Value (bool value)
 Boolean. More...
 
 Value (const String &s)
 Wiring String. More...
 
 Value (const char *s)
 NUL-terminated 'C' string. More...
 
 Value (const FSTR::String &s)
 Flash String. More...
 
Get raw/native value for use with jerryscript C API
const jerry_value_t & get () const
 const get() More...
 
jerry_value_t & get ()
 get() More...
 
Checks on value type
bool isCallable () const
 Is this object a function? If so, can cast to Callable type. More...
 
bool isArray () const
 Can this object be accessed as an array? If so, can cast to Array type. More...
 
bool isError () const
 Determine if value represents an error. If so, can cast to Error type. More...
 
bool isEmpty () const
 An empty Value contains nothing, i.e. no javascript type has been assigned. This gets interpreted as 'undefined' if any attempt is made to use it in javascript. More...
 
bool isDefined () const
 Contains a javascript value, but contents undefined. More...
 
bool isBoolean () const
 A true/false value type. More...
 
bool isFalse () const
 Is this a Boolean type set to False? More...
 
bool isTrue () const
 Is this a Boolean type set to True? More...
 
bool isNull () const
 Is this a NULL value? More...
 
bool isString () const
 Is this a String? More...
 
bool isObject () const
 Is this an Object type? If so, can cast to Object class. More...
 
bool isNumber () const
 Does this value contain a Number? More...
 

Detailed Description

Represents a Jerryscript value.

This class resembles std::unique_ptr to manage a raw/native jerry_value_t value. The get(), reset() and release() methods all behave in the same manner.

In addition, it provides intrinsic casts from numeric/boolean C++ types plus String.

You can therefore create a value like this::

Value value = 12;
Value str = "My String";
Value str2 = F("My other String");
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:113
Value()
Construct An empty (unused) value.
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:160
#define str(s)
Definition: testrunner.h:124

If you need to working with floating point values be sure to compile with JERRY_COMPACT_PROFILE=0.

IMPORTANT: When dealing with raw/native values, ALWAYS use either OwnedValue or CopyValue casts.

  • This is correct: Value value = OwnedValue{jerry_create_object()};
  • This is wrong: Value value = jerry_create_object(); // Ends up with memory leak plus garbage

Member Typedef Documentation

◆ List

Constructor & Destructor Documentation

◆ Value() [1/15]

Jerryscript::Value::Value ( )
inline

Construct An empty (unused) value.

◆ Value() [2/15]

Jerryscript::Value::Value ( const OwnedValue value)
inline

Construct a Value and take ownership of the given native value.

◆ Value() [3/15]

Jerryscript::Value::Value ( const CopyValue value)
inline

Construct a Value using a copy (or reference to) the given native value.

◆ Value() [4/15]

Jerryscript::Value::Value ( const StringValue value)
inline

Construct a string Value from the given native value.

◆ Value() [5/15]

Jerryscript::Value::Value ( const Undefined value)
inline

◆ Value() [6/15]

Jerryscript::Value::Value ( const Null value)
inline

◆ Value() [7/15]

Jerryscript::Value::Value ( const Value value)
inline

Copy constructor.

◆ Value() [8/15]

Jerryscript::Value::Value ( Value &&  value)
inline

Move constructor.

◆ Value() [9/15]

Jerryscript::Value::Value ( int  value)

Integer.

◆ Value() [10/15]

Jerryscript::Value::Value ( unsigned  value)

Unsigned integer.

◆ Value() [11/15]

Jerryscript::Value::Value ( double  value)
inline

floating-point

◆ Value() [12/15]

Jerryscript::Value::Value ( bool  value)
inline

Boolean.

◆ Value() [13/15]

Jerryscript::Value::Value ( const String s)
inline

Wiring String.

◆ Value() [14/15]

Jerryscript::Value::Value ( const char *  s)
inline

NUL-terminated 'C' string.

◆ Value() [15/15]

Jerryscript::Value::Value ( const FSTR::String s)
inline

Flash String.

◆ ~Value()

Jerryscript::Value::~Value ( )
inline

Member Function Documentation

◆ as()

template<typename T >
T Jerryscript::Value::as ( ) const
inline

Get value of object with specific type. e.g. value.as<int>().

Return value will revert to sensible default if conversion cannot be completed. If in doubt, check type first.

◆ get() [1/2]

jerry_value_t& Jerryscript::Value::get ( )
inline

◆ get() [2/2]

const jerry_value_t& Jerryscript::Value::get ( ) const
inline

const get()

◆ isArray()

bool Jerryscript::Value::isArray ( ) const
inline

Can this object be accessed as an array? If so, can cast to Array type.

◆ isBoolean()

bool Jerryscript::Value::isBoolean ( ) const
inline

A true/false value type.

◆ isCallable()

bool Jerryscript::Value::isCallable ( ) const
inline

Is this object a function? If so, can cast to Callable type.

◆ isDefined()

bool Jerryscript::Value::isDefined ( ) const
inline

Contains a javascript value, but contents undefined.

◆ isEmpty()

bool Jerryscript::Value::isEmpty ( ) const
inline

An empty Value contains nothing, i.e. no javascript type has been assigned. This gets interpreted as 'undefined' if any attempt is made to use it in javascript.

◆ isError()

bool Jerryscript::Value::isError ( ) const
inline

Determine if value represents an error. If so, can cast to Error type.

◆ isFalse()

bool Jerryscript::Value::isFalse ( ) const
inline

Is this a Boolean type set to False?

◆ isNull()

bool Jerryscript::Value::isNull ( ) const
inline

Is this a NULL value?

◆ isNumber()

bool Jerryscript::Value::isNumber ( ) const
inline

Does this value contain a Number?

◆ isObject()

bool Jerryscript::Value::isObject ( ) const
inline

Is this an Object type? If so, can cast to Object class.

◆ isString()

bool Jerryscript::Value::isString ( ) const
inline

Is this a String?

◆ isTrue()

bool Jerryscript::Value::isTrue ( ) const
inline

Is this a Boolean type set to True?

◆ operator String()

Jerryscript::Value::operator String ( ) const

Support intrinsic cast to Wiring String.

◆ operator=() [1/2]

Value& Jerryscript::Value::operator= ( const Value value)
inline

Assignment copy operator.

◆ operator=() [2/2]

Value& Jerryscript::Value::operator= ( Value &&  value)
inline

Assignment move operator.

◆ readString()

size_t Jerryscript::Value::readString ( unsigned  offset,
char *  buffer,
size_t  length 
) const

Get content from within a string value.

Parameters
offsetFirst character position to read, starting from 0
bufferWhere to store character data
lengthNumber of characters to read
Return values
size_tNumber of characters read

◆ release()

jerry_value_t Jerryscript::Value::release ( )
inline

Get raw/native value and release ownership.

◆ reset()

Value& Jerryscript::Value::reset ( jerry_value_t  value = jerry_value_t(Ecma::VALUE_EMPTY))
inline

Reset contents of object to new value (default is unassigned)

◆ subString()

String Jerryscript::Value::subString ( unsigned  offset,
size_t  length 
) const

Get content from within a string value.

Parameters
offsetFirst character position to read, starting from 0
lengthNumber of characters to read
Return values
StringRequested range, or nullptr if value is not a string

◆ toObject()

Object Jerryscript::Value::toObject ( ) const

Create a new object from this value.

◆ toString()

Value Jerryscript::Value::toString ( ) const
inline

Create a new string value from this value.

◆ type()

Type Jerryscript::Value::type ( ) const
inline

Get value type.


The documentation for this class was generated from the following file: