diff options
Diffstat (limited to 'src/uscxml/plugins/datamodel/ecmascript/v8')
4 files changed, 43 insertions, 22 deletions
diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp index 017b2eb..ea112f1 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp @@ -4,6 +4,9 @@ #include "V8DOM.h" #include "dom/V8Document.h" #include "dom/V8Node.h" +#include "dom/V8Element.h" +#include "dom/V8Text.h" +#include "dom/V8CDATASection.h" #include "dom/V8SCXMLEvent.h" #include "uscxml/Message.h" @@ -13,6 +16,17 @@ #include <Pluma/Connector.hpp> #endif +#define TO_V8_DOMVALUE(type) \ +v8::Handle<v8::Function> retCtor = V8##type::getTmpl()->GetFunction();\ +v8::Persistent<v8::Object> retObj = v8::Persistent<v8::Object>::New(retCtor->NewInstance());\ +struct V8##type::V8##type##Private* retPrivData = new V8##type::V8##type##Private();\ +retPrivData->dom = _dom;\ +retPrivData->nativeObj = new type<std::string>(node);\ +retObj->SetInternalField(0, V8DOM::toExternal(retPrivData));\ +retObj.MakeWeak(0, V8##type::jsDestructor);\ +return retObj; + + namespace uscxml { using namespace Arabica::XPath; @@ -152,7 +166,7 @@ void V8DataModel::setEvent(const Event& event) { eventObj.MakeWeak(0, V8SCXMLEvent::jsDestructor); if (event.dom) { - eventObj->Set(v8::String::New("data"), getDocumentAsValue(event.dom)); + eventObj->Set(v8::String::New("data"), getNodeAsValue(event.dom)); } else if (event.content.length() > 0) { // _event.data is a string or JSON Data json = Data::fromJSON(event.content); @@ -268,18 +282,26 @@ Data V8DataModel::getValueAsData(const v8::Handle<v8::Value>& value, std::set<v8 return data; } -v8::Handle<v8::Value> V8DataModel::getDocumentAsValue(const Document<std::string>& doc) { - v8::Handle<v8::Function> retCtor = V8Document::getTmpl()->GetFunction(); - v8::Persistent<v8::Object> retObj = v8::Persistent<v8::Object>::New(retCtor->NewInstance()); - - struct V8Document::V8DocumentPrivate* retPrivData = new V8Document::V8DocumentPrivate(); - retPrivData->dom = _dom; - retPrivData->nativeObj = new Document<std::string>(doc); +v8::Handle<v8::Value> V8DataModel::getNodeAsValue(const Node<std::string>& node) { - retObj->SetInternalField(0, V8DOM::toExternal(retPrivData)); - retObj.MakeWeak(0, V8Document::jsDestructor); + switch (node.getNodeType()) { + case Node_base::ELEMENT_NODE: { + TO_V8_DOMVALUE(Element); + } + case Node_base::TEXT_NODE: { + TO_V8_DOMVALUE(Text); + } + case Node_base::CDATA_SECTION_NODE: { + TO_V8_DOMVALUE(CDATASection); + } + case Node_base::DOCUMENT_NODE: { + TO_V8_DOMVALUE(Document); + } + default: { + TO_V8_DOMVALUE(Node); + } + } - return retObj; } v8::Handle<v8::Value> V8DataModel::getDataAsValue(const Data& data) { @@ -459,7 +481,7 @@ double V8DataModel::evalAsNumber(const std::string& expr) { } void V8DataModel::assign(const Element<std::string>& assignElem, - const Document<std::string>& doc, + const Node<std::string>& node, const std::string& content) { v8::Locker locker; v8::HandleScope handleScope; @@ -477,8 +499,8 @@ void V8DataModel::assign(const Element<std::string>& assignElem, if (HAS_ATTR(assignElem, "expr")) { evalAsValue(key + " = " + ATTR(assignElem, "expr")); - } else if (doc) { - global->Set(v8::String::New(key.c_str()), getDocumentAsValue(doc)); + } else if (node) { + global->Set(v8::String::New(key.c_str()), getNodeAsValue(node)); } else if (content.size() > 0) { try { evalAsValue(key + " = " + content); @@ -502,7 +524,7 @@ void V8DataModel::assign(const std::string& location, } void V8DataModel::init(const Element<std::string>& dataElem, - const Document<std::string>& doc, + const Node<std::string>& doc, const std::string& content) { try { assign(dataElem, doc, content); @@ -536,7 +558,6 @@ void V8DataModel::init(const std::string& location, evalAsValue(location + " = undefined", true); throw e; } - } diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h index 9d17093..0dffa27 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h @@ -50,13 +50,13 @@ public: virtual void eval(const Arabica::DOM::Element<std::string>& scriptElem, const std::string& expr); virtual void assign(const Arabica::DOM::Element<std::string>& assignElem, - const Arabica::DOM::Document<std::string>& doc, + const Arabica::DOM::Node<std::string>& node, const std::string& content); virtual void assign(const std::string& location, const Data& data); virtual void init(const Arabica::DOM::Element<std::string>& dataElem, - const Arabica::DOM::Document<std::string>& doc, + const Arabica::DOM::Node<std::string>& node, const std::string& content); virtual void init(const std::string& location, const Data& data); @@ -87,7 +87,7 @@ protected: v8::Handle<v8::Value> evalAsValue(const std::string& expr, bool dontThrow = false); v8::Handle<v8::Value> getDataAsValue(const Data& data); - v8::Handle<v8::Value> getDocumentAsValue(const Arabica::DOM::Document<std::string>& doc); + v8::Handle<v8::Value> getNodeAsValue(const Arabica::DOM::Node<std::string>& node); void throwExceptionEvent(const v8::TryCatch& tryCatch); }; diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEvent.h b/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEvent.h index 3c5e6ee..9cc4ad4 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEvent.h +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEvent.h @@ -41,7 +41,7 @@ public: static bool hasInstance(v8::Handle<v8::Value>); - static v8::Handle<v8::Value> eventTypeCustomAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info); + static v8::Handle<v8::Value> typeCustomAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info); static v8::Handle<v8::Value> nameAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info); static v8::Handle<v8::Value> originAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info); static v8::Handle<v8::Value> origintypeAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info); @@ -63,7 +63,7 @@ public: instance->SetInternalFieldCount(1); - instance->SetAccessor(v8::String::NewSymbol("eventType"), V8SCXMLEvent::eventTypeCustomAttrGetter, 0, + instance->SetAccessor(v8::String::NewSymbol("type"), V8SCXMLEvent::typeCustomAttrGetter, 0, v8::External::New(0), static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None)); instance->SetAccessor(v8::String::NewSymbol("name"), V8SCXMLEvent::nameAttrGetter, 0, v8::External::New(0), static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None)); diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEventCustom.cpp b/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEventCustom.cpp index d02a4cb..a8870cc 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEventCustom.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/dom/V8SCXMLEventCustom.cpp @@ -3,7 +3,7 @@ namespace Arabica { namespace DOM { -v8::Handle<v8::Value> V8SCXMLEvent::eventTypeCustomAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info) { +v8::Handle<v8::Value> V8SCXMLEvent::typeCustomAttrGetter(v8::Local<v8::String> property, const v8::AccessorInfo& info) { v8::Local<v8::Object> self = info.Holder(); V8SCXMLEventPrivate* privData = V8DOM::toClassPtr<V8SCXMLEventPrivate >(self->GetInternalField(0)); |