Handle string description in JsDate constructor. Add string representation when stringifying a JsDate. Added toJSON() method to JsDate.

git-svn-id: http://yate.null.ro/svn/yate/trunk@6365 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2019-11-08 11:24:35 +00:00
parent ee79c52c14
commit 1a9603fba7
1 changed files with 31 additions and 2 deletions

View File

@ -70,6 +70,8 @@ public:
params().addParam(new ExpFunction("getUTCMinutes")); params().addParam(new ExpFunction("getUTCMinutes"));
params().addParam(new ExpFunction("getUTCMonth")); params().addParam(new ExpFunction("getUTCMonth"));
params().addParam(new ExpFunction("getUTCSeconds")); params().addParam(new ExpFunction("getUTCSeconds"));
params().addParam(new ExpFunction("toJSON"));
} }
virtual void initConstructor(JsFunction* construct) virtual void initConstructor(JsFunction* construct)
{ {
@ -77,6 +79,12 @@ public:
construct->params().addParam(new ExpFunction("UTC")); construct->params().addParam(new ExpFunction("UTC"));
} }
virtual JsObject* runConstructor(ObjList& stack, const ExpOperation& oper, GenObject* context); virtual JsObject* runConstructor(ObjList& stack, const ExpOperation& oper, GenObject* context);
virtual const String& toString() const {
if (!m_str)
Time::appendTo(m_str,(uint64_t)m_time * 1000000 + (uint64_t)m_msec * 1000,1);
return m_str;
}
protected: protected:
inline JsDate(Mutex* mtx, u_int64_t msecs, bool local = false) inline JsDate(Mutex* mtx, u_int64_t msecs, bool local = false)
: JsObject("Date",mtx), : JsObject("Date",mtx),
@ -93,6 +101,7 @@ private:
unsigned int m_time; unsigned int m_time;
unsigned int m_msec; unsigned int m_msec;
int m_offs; int m_offs;
mutable String m_str;
}; };
// Math class - not really an object, all methods are static // Math class - not really an object, all methods are static
@ -145,6 +154,8 @@ static void dumpRecursiveObj(const GenObject* obj, String& buf, unsigned int dep
type = "JsFunction"; type = "JsFunction";
else if (YOBJECT(JsRegExp,scr)) else if (YOBJECT(JsRegExp,scr))
type = "JsRegExp"; type = "JsRegExp";
else if (YOBJECT(JsDate,scr))
type = "JsDate";
else else
type = "JsObject"; type = "JsObject";
} }
@ -337,6 +348,10 @@ void JsObject::toJSON(const NamedString* ns, String& buf, int spaces, int indent
return; return;
} }
if (jso) { if (jso) {
if (YOBJECT(JsDate,jso)) {
buf << strEscape(jso->toString());
return;
}
switch (jso->params().count()) { switch (jso->params().count()) {
case 1: case 1:
if (!jso->params().getParam(protoName())) if (!jso->params().getParam(protoName()))
@ -1480,8 +1495,17 @@ JsObject* JsDate::runConstructor(ObjList& stack, const ExpOperation& oper, GenOb
case 1: case 1:
{ {
ExpOperation* val = static_cast<ExpOperation*>(args[0]); ExpOperation* val = static_cast<ExpOperation*>(args[0]);
if (val && val->isInteger()) if (val) {
obj = new JsDate(mutex(),val->number()); if (val->isInteger())
obj = new JsDate(mutex(),val->number());
else {
// Check string
uint64_t n = Time::toEpoch(*val,val->length(),1);
if (n == (uint64_t)-1)
return JsParser::nullObject();
obj = new JsDate(mutex(),n);
}
}
} }
break; break;
case 2: case 2:
@ -1710,6 +1734,11 @@ bool JsDate::runNative(ObjList& stack, const ExpOperation& oper, GenObject* cont
else else
return false; return false;
} }
else if (oper.name() == YSTRING("toJSON")) {
if (toString().null())
return false;
ExpEvaluator::pushOne(stack,new ExpOperation(toString()));
}
else else
return JsObject::runNative(stack,oper,context); return JsObject::runNative(stack,oper,context);
return true; return true;