Yate
JsObject Class Reference

Javascript Object. More...

#include <yatescript.h>

Inheritance diagram for JsObject:
ScriptContext RefObject ExpExtender GenObject JsArray JsFunction JsRegExp

Public Types

enum  DumpFlags {
  DumpFunc = 0x01 , DumpProp = 0x02 , DumpRecursive = 0x10 , DumpType = 0x20 ,
  DumpProto = 0x40 , DumpPropObjType = 0x80 , DumpFuncOnly = DumpRecursive | DumpProto | DumpFunc , DumpPropOnly = DumpRecursive | DumpPropObjType | DumpProp
}
 

Public Member Functions

 JsObject (const char *name="Object", ScriptMutex *mtx=0, bool frozen=false)
 
 JsObject (ScriptMutex *mtx, const char *name, unsigned int line, bool frozen=0)
 
 JsObject (GenObject *context, unsigned int line, ScriptMutex *mtx=0, bool frozen=false)
 
virtual ~JsObject ()
 
virtual ScriptMutexmutex ()
 
virtual JsObjectclone (const char *name, const ExpOperation &oper) const
 
JsObjectclone (const ExpOperation &oper) const
 
void setPrototype (GenObject *context, const String &objName)
 
virtual JsObjectcopy (ScriptMutex *mtx, const ExpOperation &oper) const
 
virtual void fillFieldNames (ObjList &names)
 
virtual const HashListgetHashListParams () const
 
virtual bool hasField (ObjList &stack, const String &name, GenObject *context) const
 
virtual NamedStringgetField (ObjList &stack, const String &name, GenObject *context) const
 
virtual void initConstructor (JsFunction *construct)
 
virtual JsObjectrunConstructor (ObjList &stack, const ExpOperation &oper, GenObject *context)
 
virtual bool runFunction (ObjList &stack, const ExpOperation &oper, GenObject *context)
 
virtual bool runField (ObjList &stack, const ExpOperation &oper, GenObject *context)
 
virtual bool runAssign (ObjList &stack, const ExpOperation &oper, GenObject *context)
 
virtual ExpOperationpopValue (ObjList &stack, GenObject *context=0)
 
virtual void clearField (const String &name)
 
bool setIntField (const char *name, int64_t val)
 
bool setBoolField (const char *name, bool val)
 
bool setStringField (const char *name, const char *val)
 
bool setObjField (const char *name, JsObject *obj)
 
bool getIntField (const String &name, int64_t &val)
 
bool getBoolField (const String &name, bool &val)
 
bool getStringField (const String &name, String &val)
 
bool getObjField (const String &name, JsObject *&obj)
 
bool frozen () const
 
void freeze ()
 
void lineNo (unsigned int line)
 
unsigned int lineNo () const
 
int extractArgs (ObjList &stack, const ExpOperation &oper, GenObject *context, ObjList &arguments)
 
- Public Member Functions inherited from ScriptContext
 ScriptContext (const char *name=0)
 
NamedListparams ()
 
const NamedListparams () const
 
virtual NamedListnativeParams () const
 
virtual const StringtoString () const
 
virtual void * getObject (const String &name) const
 
virtual RefObjectrefObj ()
 
virtual bool copyFields (ObjList &stack, const ScriptContext &original, GenObject *context)
 
virtual void addFields (const NamedList &list, const char *skipPrefix="__")
 
bool runMatchingField (ObjList &stack, const ExpOperation &oper, GenObject *context)
 
virtual void createdObj (GenObject *obj)
 
virtual void deletedObj (GenObject *obj)
 
virtual void trackObjs (unsigned int track=0)
 
virtual ObjListcountAllocations ()
 
- Public Member Functions inherited from RefObject
 RefObject ()
 
virtual ~RefObject ()
 
virtual bool alive () const
 
bool ref ()
 
bool deref ()
 
int refcount () const
 
virtual void destruct ()
 
- Public Member Functions inherited from GenObject
 GenObject ()
 
virtual ~GenObject ()
 
virtual const StringtraceId () const
 
NamedCountergetObjCounter () const
 
NamedCountersetObjCounter (NamedCounter *counter)
 
- Public Member Functions inherited from ExpExtender
virtual ~ExpExtender ()
 

Static Public Member Functions

static void addObject (NamedList &params, const char *name, JsObject *obj)
 
static void addConstructor (NamedList &params, const char *name, JsObject *obj)
 
static int extractArgs (JsObject *obj, ObjList &stack, const ExpOperation &oper, GenObject *context, ObjList &arguments)
 
static JsObjectbuildCallContext (ScriptMutex *mtx, JsObject *thisObj=0)
 
static void initialize (ScriptContext *context)
 
static void setLineForObj (JsObject *obj, unsigned int lineNo, bool recursive)
 
static const StringprotoName ()
 
static void deepCopyParams (NamedList &dst, const NamedList &src, ScriptMutex *mtx)
 
static void dumpRecursive (const GenObject *obj, String &buf, unsigned int flags=0xffffffff)
 
static void printRecursive (const GenObject *obj, unsigned int flags=0xffffffff)
 
static ExpOperationtoJSON (const ExpOperation *oper, int spaces)
 
- Static Public Member Functions inherited from ScriptContext
static void fillFieldNames (ObjList &names, const NamedList &list, bool checkDupl=true, const char *skip=0)
 
static void fillFieldNames (ObjList &names, const HashList &list)
 
- Static Public Member Functions inherited from RefObject
static bool alive (const RefObject *obj)
 
static bool efficientIncDec ()
 
- Static Public Member Functions inherited from GenObject
static void * getObject (const String &name, const GenObject *obj)
 
static bool getObjCounting ()
 
static void setObjCounting (bool enable)
 
static NamedCountergetObjCounter (const String &name, bool create=true)
 
static ObjListgetObjCounters ()
 

Protected Member Functions

virtual bool runNative (ObjList &stack, const ExpOperation &oper, GenObject *context)
 
ScriptMutexmutex () const
 
void setMutex (ScriptMutex *mtx)
 
- Protected Member Functions inherited from RefObject
virtual void zeroRefs ()
 
bool resurrect ()
 
virtual void destroyed ()
 

Static Protected Member Functions

static void toJSON (const NamedString *ns, String &buf, int spaces, int indent=0)
 
static String strEscape (const char *str)
 

Friends

class JsFunction
 

Detailed Description

Javascript Object.

Javascript Object class, base for all JS objects

Member Enumeration Documentation

◆ DumpFlags

enum DumpFlags

Dump object flags

Constructor & Destructor Documentation

◆ JsObject() [1/3]

JsObject ( const char * name = "Object",
ScriptMutex * mtx = 0,
bool frozen = false )

Constructor

Parameters
nameName of the object
mtxPointer to the mutex that serializes this object
frozenTrue if the object is to be frozen from creation

References frozen().

Referenced by addConstructor(), addObject(), buildCallContext(), clone(), clone(), JsRegExp::clone(), JsFunction::copy(), JsFunction::copy(), copy(), JsRegExp::copy(), extractArgs(), getObjField(), runConstructor(), JsRegExp::runConstructor(), JsFunction::runDefined(), setLineForObj(), and setObjField().

◆ JsObject() [2/3]

JsObject ( ScriptMutex * mtx,
const char * name,
unsigned int line,
bool frozen = 0 )

Constructor for an empty object

Parameters
mtxPointer to the mutex that serializes this object
nameFull name of the object
lineCode line where this object was created
frozenTrue if the object is to be frozen from creation

References frozen().

◆ JsObject() [3/3]

JsObject ( GenObject * context,
unsigned int line,
ScriptMutex * mtx = 0,
bool frozen = false )

Constructor for an empty object with prototype

Parameters
contextScript context from which Object prototype is obtained
lineCode line where this object was created
mtxPointer to the mutex that serializes this object
frozenTrue if the object is to be frozen from creation

References frozen(), and GenObject::GenObject().

◆ ~JsObject()

virtual ~JsObject ( )
virtual

Destructor

Member Function Documentation

◆ addConstructor()

static void addConstructor ( NamedList & params,
const char * name,
JsObject * obj )
static

Helper static method that adds a constructor to a parent

Parameters
paramsList of parameters where to add the constructor
nameName of the new parameter
objPointer to the prototype object to add

References JsObject(), and ScriptContext::params().

◆ addObject()

static void addObject ( NamedList & params,
const char * name,
JsObject * obj )
static

Helper static method that adds an object to a parent

Parameters
paramsList of parameters where to add the object
nameName of the new parameter
objPointer to the object to add

References JsObject(), and ScriptContext::params().

◆ buildCallContext()

static JsObject * buildCallContext ( ScriptMutex * mtx,
JsObject * thisObj = 0 )
static

Create an empty function call context

Parameters
mtxPointer to the mutex that serializes this object
thisObjOptional object that will be set as "this"
Returns
New empty object usable as call context

References JsObject().

◆ clearField()

virtual void clearField ( const String & name)
inlinevirtual

Delete a field of the object

Parameters
nameName of field to remove

References ScriptContext::params().

◆ clone() [1/2]

virtual JsObject * clone ( const char * name,
const ExpOperation & oper ) const
inlinevirtual

Clone and rename method

Parameters
nameName of the cloned object
operExpOperation that required the clone
Returns
New object instance

Reimplemented in JsArray, and JsRegExp.

References JsObject(), and ExpOperation::lineNumber().

Referenced by clone().

◆ clone() [2/2]

JsObject * clone ( const ExpOperation & oper) const
inline

Clone method

Parameters
operExpOperation that required the clone
Returns
New object instance

References clone(), JsObject(), and ScriptContext::toString().

◆ copy()

virtual JsObject * copy ( ScriptMutex * mtx,
const ExpOperation & oper ) const
virtual

Deep copy method

Parameters
mtxPointer to the mutex that serializes the copied object
operCaller of copying operation
Returns
New object instance, does not keep references to old object

Reimplemented in JsArray, JsFunction, and JsRegExp.

References JsObject().

◆ deepCopyParams()

static void deepCopyParams ( NamedList & dst,
const NamedList & src,
ScriptMutex * mtx )
static

Static helper method that deep copies all parameters

Parameters
dstDestination parameters
srcSource parameters
mtxMutex to be used to synchronize all new objects

◆ dumpRecursive()

static void dumpRecursive ( const GenObject * obj,
String & buf,
unsigned int flags = 0xffffffff )
static

Helper method to return the hierarchical structure of an object

Parameters
objObject to dump structure
bufString to which the structure is added
flagsFlags indicating what to dump

References GenObject::GenObject().

◆ extractArgs() [1/2]

static int extractArgs ( JsObject * obj,
ObjList & stack,
const ExpOperation & oper,
GenObject * context,
ObjList & arguments )
static

Helper static method that pops arguments off a stack to a list in proper order

Parameters
objPointer to the object to use when popping each argument
stackEvaluation stack in use, parameters are popped off this stack
operFunction that is being evaluated
contextPointer to arbitrary object passed from evaluation methods
argumentsList where the arguments are added in proper order
Returns
Number of arguments popped off stack

References GenObject::GenObject(), and JsObject().

Referenced by extractArgs().

◆ extractArgs() [2/2]

int extractArgs ( ObjList & stack,
const ExpOperation & oper,
GenObject * context,
ObjList & arguments )
inline

Helper method that pops arguments off a stack to a list in proper order

Parameters
stackEvaluation stack in use, parameters are popped off this stack
operFunction that is being evaluated
contextPointer to arbitrary object passed from evaluation methods
argumentsList where the arguments are added in proper order
Returns
Number of arguments popped off stack

References extractArgs(), and GenObject::GenObject().

◆ fillFieldNames()

virtual void fillFieldNames ( ObjList & names)
virtual

Fill a list with the unique names of all fields

Parameters
namesList to which key names must be added

Reimplemented from ScriptContext.

◆ freeze()

void freeze ( )
inline

Freeze the Javascript object preventing external changes to it

◆ frozen()

bool frozen ( ) const
inline

Retrieve the object frozen status (cannot modify attributes or methods)

Returns
True if the object is frozen

Referenced by JsArray::JsArray(), JsObject(), JsObject(), JsObject(), JsRegExp::JsRegExp(), and JsRegExp::JsRegExp().

◆ getBoolField()

bool getBoolField ( const String & name,
bool & val )

Get the boolean value of the field with the given name

Parameters
nameName of field to retrieve
valField where to put the retrieved value
Returns
True if field was retrieved, false if not found or not the right type

◆ getField()

virtual NamedString * getField ( ObjList & stack,
const String & name,
GenObject * context ) const
virtual

Get a pointer to a field in the object or its prototype

Parameters
stackEvaluation stack in use
nameName of the field to retrieve
contextPointer to arbitrary object passed from evaluation methods
Returns
Pointer to field, NULL if not present

Reimplemented from ScriptContext.

References GenObject::GenObject().

◆ getHashListParams()

virtual const HashList * getHashListParams ( ) const
virtual

Retrieve enclosed hashed list paramsters

Returns
HashList pointer, NULL if this object don't hold one

◆ getIntField()

bool getIntField ( const String & name,
int64_t & val )

Get the integer value of the field with the given name

Parameters
nameName of field to retrieve
valField where to put the retrieved value
Returns
True if field was retrieved, false if not found or not the right type

◆ getObjField()

bool getObjField ( const String & name,
JsObject *& obj )

Get the object associated with the field with the given name

Parameters
nameName of field to retrieve
objField where to put the retrieved value
Returns
True if field was retrieved, false if not found or not the right type

References JsObject().

◆ getStringField()

bool getStringField ( const String & name,
String & val )

Get the string value of the field with the given name

Parameters
nameName of field to retrieve
valField where to put the retrieved value
Returns
True if field was retrieved, false if not found or not the right type

◆ hasField()

virtual bool hasField ( ObjList & stack,
const String & name,
GenObject * context ) const
virtual

Check if a certain field is assigned in the object or its prototype

Parameters
stackEvaluation stack in use
nameName of the field to test
contextPointer to arbitrary object passed from evaluation methods
Returns
True if the field is present

Reimplemented from ScriptContext.

References GenObject::GenObject().

◆ initConstructor()

virtual void initConstructor ( JsFunction * construct)
inlinevirtual

Native constructor initialization, called by addConstructor on the prototype

Parameters
constructFunction that has this object as prototype

Reimplemented in JsArray, and JsFunction.

◆ initialize()

static void initialize ( ScriptContext * context)
static

Initialize the standard global objects in a context

Parameters
contextScript context to initialize

References ScriptContext::ScriptContext().

◆ lineNo() [1/2]

unsigned int lineNo ( ) const
inline

Get the script line number at which this object was created;

Returns
The line number from the script where this object was created.

◆ lineNo() [2/2]

void lineNo ( unsigned int line)
inline

Set the script line number at which this object was created

Parameters
lineLine number

Referenced by setLineForObj().

◆ mutex() [1/2]

virtual ScriptMutex * mutex ( )
inlinevirtual

Retrieve the Mutex object used to serialize object access

Returns
Pointer to the mutex of the context this object belongs to

Implements ScriptContext.

Referenced by JsArray::clone(), and JsRegExp::clone().

◆ mutex() [2/2]

ScriptMutex * mutex ( ) const
inlineprotected

Retrieve the Mutex object used to serialize object access

Returns
Pointer to the mutex of the context this object belongs to

◆ popValue()

virtual ExpOperation * popValue ( ObjList & stack,
GenObject * context = 0 )
virtual

Pops and evaluate the value of an operand off an evaluation stack, does not pop a barrier

Parameters
stackEvaluation stack to remove the operand from
contextPointer to arbitrary object to be passed to called methods
Returns
Value removed from stack, NULL if stack underflow or field not evaluable

References GenObject::GenObject().

◆ printRecursive()

static void printRecursive ( const GenObject * obj,
unsigned int flags = 0xffffffff )
static

Helper method to display the hierarchical structure of an object

Parameters
objObject to display
flagsFlags indicating what to dump (display)

References GenObject::GenObject().

◆ protoName()

static const String & protoName ( )
inlinestatic

Get the name of the internal property used to track prototypes

Returns
The "__proto__" constant string

◆ runAssign()

virtual bool runAssign ( ObjList & stack,
const ExpOperation & oper,
GenObject * context )
virtual

Try to assign a value to a single field if object is not frozen

Parameters
stackEvaluation stack in use
operField to assign to, contains the field name and new value
contextPointer to arbitrary object passed from evaluation methods
Returns
True if assignment succeeded

Reimplemented from ScriptContext.

Reimplemented in JsArray, and JsRegExp.

References GenObject::GenObject().

◆ runConstructor()

virtual JsObject * runConstructor ( ObjList & stack,
const ExpOperation & oper,
GenObject * context )
virtual

Native object constructor, it's run on the prototype

Parameters
stackEvaluation stack in use
operConstructor function to evaluate
contextPointer to arbitrary object passed from evaluation methods
Returns
New created and populated Javascript object

Reimplemented in JsArray, and JsRegExp.

References GenObject::GenObject(), and JsObject().

◆ runField()

virtual bool runField ( ObjList & stack,
const ExpOperation & oper,
GenObject * context )
virtual

Try to evaluate a single field

Parameters
stackEvaluation stack in use, field value must be pushed on it
operField to evaluate
contextPointer to arbitrary object passed from evaluation methods
Returns
True if evaluation succeeded

Reimplemented from ScriptContext.

Reimplemented in JsArray.

References GenObject::GenObject().

◆ runFunction()

virtual bool runFunction ( ObjList & stack,
const ExpOperation & oper,
GenObject * context )
virtual

Try to evaluate a single method

Parameters
stackEvaluation stack in use, parameters are popped off this stack and results are pushed back on stack
operFunction to evaluate
contextPointer to arbitrary object passed from evaluation methods
Returns
True if evaluation succeeded

Reimplemented from ScriptContext.

References GenObject::GenObject().

◆ runNative()

virtual bool runNative ( ObjList & stack,
const ExpOperation & oper,
GenObject * context )
protectedvirtual

Try to evaluate a single native method

Parameters
stackEvaluation stack in use, parameters are popped off this stack and results are pushed back on stack
operFunction to evaluate
contextPointer to arbitrary object passed from evaluation methods
Returns
True if evaluation succeeded

Reimplemented in JsArray, JsFunction, and JsRegExp.

References GenObject::GenObject().

◆ setBoolField()

bool setBoolField ( const char * name,
bool val )
inline

Set a boolean field in this object

Parameters
nameName of field to set
valBoolean value to set
Returns
True if set was successful

References ScriptContext::params().

◆ setIntField()

bool setIntField ( const char * name,
int64_t val )
inline

Set a integer field in this object

Parameters
nameName of field to set
valInteger value to set
Returns
True if set was successful

References ScriptContext::params().

◆ setLineForObj()

static void setLineForObj ( JsObject * obj,
unsigned int lineNo,
bool recursive )
static

Set the creation line for this object and its properties

Parameters
objObject t set the line for
lineNoThe line number to set
recursiveTrue to set it to its sub-objects, false otherwise

References JsObject(), and lineNo().

◆ setMutex()

void setMutex ( ScriptMutex * mtx)
inlineprotected

Set the Mutex used to serialize this object, set to 0 to reset it

Parameters
mtxMutex to set

◆ setObjField()

bool setObjField ( const char * name,
JsObject * obj )
inline

Set a object field in this object

Parameters
nameName of field to set
objObject value to set
Returns
True if set was successful

References JsObject(), and ScriptContext::params().

◆ setPrototype()

void setPrototype ( GenObject * context,
const String & objName )

Set the object prototype

Parameters
contextPointer to arbitrary object passed from evaluation methods
objNameName of the object prototype to set the this object

References GenObject::GenObject().

◆ setStringField()

bool setStringField ( const char * name,
const char * val )
inline

Set a string field in this object

Parameters
nameName of field to set
valString value to set
Returns
True if set was successful

References ScriptContext::params().

◆ strEscape()

static String strEscape ( const char * str)
staticprotected

Static helper method for escaping special characters when JSON stringifying

Parameters
strString to escape
Returns
Escaped string

References GenObject::GenObject().

◆ toJSON() [1/2]

static ExpOperation * toJSON ( const ExpOperation * oper,
int spaces )
static

Static method to obtain a JSON representation of the given object

Parameters
operObject to represent in JSON format
spacesNumber of spaces used for one indentation level
Returns
String represention of the object

◆ toJSON() [2/2]

static void toJSON ( const NamedString * ns,
String & buf,
int spaces,
int indent = 0 )
inlinestaticprotected

Static method to obtain a JSON representation of the given object

Parameters
nsObject to represent in JSON format
bufString used as output for the JSON represantion
spacesNumber of spaces used for one indentation level
indentCurrent number of spaces used for indentation

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