Made JsRegExp object public, added constructor.

git-svn-id: http://yate.null.ro/svn/yate/trunk@5075 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2012-05-28 16:10:41 +00:00
parent a9975c435b
commit 86384d16e5
3 changed files with 105 additions and 7 deletions

View File

@ -488,8 +488,8 @@ bool JsCode::getString(const char*& expr)
}
XDebug(this,DebugInfo,"Regexp '%s' flags '%s%s'",str.c_str(),
(insensitive ? "i" : ""),(extended ? "" : "b"));
addOpcode(str);
//m_opcodes.append(new ExpWrapper(obj));
JsRegExp* obj = new JsRegExp(0,str,str,insensitive,extended);
addOpcode(new ExpWrapper(obj));
return true;
}

View File

@ -288,6 +288,8 @@ void JsObject::initialize(ScriptContext* context)
addConstructor(p,"Function",new JsFunction(mtx));
if (!p.getParam(YSTRING("Array")))
addConstructor(p,"Array",new JsArray(mtx));
if (!p.getParam(YSTRING("RegExp")))
addConstructor(p,"RegExp",new JsRegExp(mtx));
if (!p.getParam(YSTRING("Date")))
addConstructor(p,"Date",new JsDate(mtx));
if (!p.getParam(YSTRING("Math")))
@ -634,6 +636,39 @@ bool JsArray::runNativeSort(ObjList& stack, const ExpOperation& oper, GenObject*
}
JsRegExp::JsRegExp(Mutex* mtx)
: JsObject("RegExp",mtx)
{
params().addParam(new ExpFunction("test"));
}
JsRegExp::JsRegExp(Mutex* mtx, const char* name, const char* rexp, bool extended, bool insensitive, bool frozen)
: JsObject(mtx,name,frozen),
m_regexp(rexp,extended,insensitive)
{
params().addParam(new ExpFunction("test"));
params().addParam("ignoreCase",String::boolText(insensitive));
params().addParam("basicPosix",String::boolText(!extended));
}
bool JsRegExp::runNative(ObjList& stack, const ExpOperation& oper, GenObject* context)
{
XDebug(DebugAll,"JsRegExp::runNative() '%s' in '%s' [%p]",
oper.name().c_str(),toString().c_str(),this);
if (oper.name() == YSTRING("test")) {
if (oper.number() != 1)
return false;
ExpOperation* op = popValue(stack,context);
bool ok = op && regexp().matches(*op);
TelEngine::destruct(op);
ExpEvaluator::pushOne(stack,new ExpOperation(ok));
}
else
return JsObject::runNative(stack,oper,context);
return true;
}
bool JsMath::runNative(ObjList& stack, const ExpOperation& oper, GenObject* context)
{
XDebug(DebugAll,"JsMath::runNative() '%s' in '%s' [%p]",
@ -836,7 +871,7 @@ bool JsFunction::runNative(ObjList& stack, const ExpOperation& oper, GenObject*
bool JsFunction::runDefined(ObjList& stack, const ExpOperation& oper, GenObject* context)
{
XDebug(DebugAll,"JsObject::runDefined() in '%s' [%p]",toString().c_str(),this);
XDebug(DebugAll,"JsFunction::runDefined() in '%s' [%p]",toString().c_str(),this);
JsObject* proto = YOBJECT(JsObject,getField(stack,"prototype",context));
if (proto) {
// found prototype, build object

View File

@ -1664,7 +1664,6 @@ class YSCRIPT_API JsFunction : public JsObject
{
YCLASS(JsFunction,JsObject)
public:
/**
* Constructor
* @param mtx Pointer to the mutex that serializes this object
@ -1711,7 +1710,6 @@ class YSCRIPT_API JsArray : public JsObject
{
YCLASS(JsArray,JsObject)
public:
/**
* Constructor
* @param mtx Pointer to the mutex that serializes this object
@ -1738,8 +1736,8 @@ protected:
* @param name Full name of the object
* @param frozen True if the object is to be frozen from creation
*/
inline JsArray(Mutex* mtx, const char* name)
: JsObject(mtx,name), m_length(0)
inline JsArray(Mutex* mtx, const char* name, bool frozen = false)
: JsObject(mtx,name,frozen), m_length(0)
{ }
/**
@ -1779,6 +1777,71 @@ private:
bool runNativeSort(ObjList& stack, const ExpOperation& oper, GenObject* context);
long m_length;
};
/**
* Javascript RegExp class, implements regular expression matching
* @short Javascript RegExp
*/
class YSCRIPT_API JsRegExp : public JsObject
{
YCLASS(JsRegExp,JsObject)
public:
/*
* Constructor for a RegExp constructor
* @param mtx Pointer to the mutex that serializes this object
*/
JsRegExp(Mutex* mtx = 0);
/*
* Constructor for a RegExp object
* @param mtx Pointer to the mutex that serializes this object
* @param name Full name of the object
* @param rexp Regular expression text
* @param insensitive True to not differentiate case
* @param extended True to use POSIX Extended Regular Expression syntax
* @param frozen True to create an initially frozen object
*/
JsRegExp(Mutex* mtx, const char* name, const char* rexp = 0, bool insensitive = false,
bool extended = true, bool frozen = false);
/**
* Access the internal Regexp object that does the matching
* @return Const reference to the internal Regexp object
*/
inline const Regexp& regexp() const
{ return m_regexp; }
/**
* Access the internal Regexp object that does the matching
* @return Reference to the internal Regexp object
*/
inline Regexp& regexp()
{ return m_regexp; }
protected:
/**
* Clone and rename method
* @param name Name of the cloned object
* @return New object instance
*/
virtual JsObject* clone(const char* name) const
{ return new JsRegExp(mutex(),name,m_regexp.c_str(),
m_regexp.isCaseInsensitive(),m_regexp.isExtended()); }
/**
* Try to evaluate a single native method
* @param stack Evaluation stack in use, parameters are popped off this stack
* and results are pushed back on stack
* @param oper Function to evaluate
* @param context Pointer to arbitrary object passed from evaluation methods
* @return True if evaluation succeeded
*/
bool runNative(ObjList& stack, const ExpOperation& oper, GenObject* context);
private:
Regexp m_regexp;
};
/**
* Javascript parser, takes source code and generates preparsed code
* @short Javascript parser