diff options
Diffstat (limited to 'src/uscxml/plugins/datamodel/ecmascript')
4 files changed, 53 insertions, 63 deletions
diff --git a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp index c5aec92..142f3ec 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp @@ -292,32 +292,32 @@ JSValueRef JSCDataModel::getDataAsValue(const Data& data) { handleException(exception); return value; } - - switch (data.type) { - case Data::VERBATIM: { - JSStringRef stringRef = JSStringCreateWithUTF8CString(data.atom.c_str()); - JSValueRef value = JSValueMakeString(_ctx, stringRef); - JSStringRelease(stringRef); - return value; - break; - } - case Data::INTERPRETED: { - return evalAsValue(data.atom); - break; + if (data.atom.size() > 0) { + switch (data.type) { + case Data::VERBATIM: { + JSStringRef stringRef = JSStringCreateWithUTF8CString(data.atom.c_str()); + JSValueRef value = JSValueMakeString(_ctx, stringRef); + JSStringRelease(stringRef); + return value; + break; + } + case Data::INTERPRETED: { + return evalAsValue(data.atom); + break; + } + } } - case Data::BINARY: { - uscxml::ArrayBuffer* localInstance = new uscxml::ArrayBuffer((void*)data.atom.c_str(), data.atom.size()); - + if (data.binary) { + uscxml::ArrayBuffer* localInstance = new uscxml::ArrayBuffer(data.binary); + JSClassRef retClass = JSCArrayBuffer::getTmpl(); struct JSCArrayBuffer::JSCArrayBufferPrivate* retPrivData = new JSCArrayBuffer::JSCArrayBufferPrivate(); retPrivData->nativeObj = localInstance; - + JSObjectRef retObj = JSObjectMake(_ctx, retClass, retPrivData); return retObj; - break; } - } - + return JSValueMakeUndefined(_ctx); } Data JSCDataModel::getValueAsData(const JSValueRef value) { @@ -356,6 +356,12 @@ Data JSCDataModel::getValueAsData(const JSValueRef value) { JSObjectRef objValue = JSValueToObject(_ctx, value, &exception); if (exception) handleException(exception); + if (JSValueIsObjectOfClass(_ctx, value, JSCArrayBuffer::getTmpl())) { + // binary data! + JSCArrayBuffer::JSCArrayBufferPrivate* privObj = (JSCArrayBuffer::JSCArrayBufferPrivate*)JSObjectGetPrivate(objValue); + data.binary = privObj->nativeObj->_buffer; + return data; + } std::set<std::string> propertySet; JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(_ctx, objValue); size_t paramCount = JSPropertyNameArrayGetCount(properties); diff --git a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp index aa15353..d24639a 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp @@ -15,31 +15,15 @@ memcpy(_buffer->_data + (_start + index), &value, sizeof(type)); namespace uscxml { -ArrayBuffer::Buffer::~Buffer() { - free(_data); -} - -ArrayBuffer::Buffer::Buffer(size_t size) { - _data = (char*)malloc(size); - memset(_data, 0, size); - _size = size; -} - -ArrayBuffer::Buffer::Buffer(void* data, size_t size) { - _data = (char*)malloc(size); - memcpy(_data, data, size); - _size = size; -} - ArrayBuffer::ArrayBuffer(unsigned long length) { - _buffer = boost::shared_ptr<Buffer>(new Buffer(length)); + _buffer = boost::shared_ptr<Blob>(new Blob(length)); } -ArrayBuffer::ArrayBuffer(boost::shared_ptr<ArrayBuffer::Buffer> buffer) : _buffer(buffer) { +ArrayBuffer::ArrayBuffer(boost::shared_ptr<Blob> buffer) : _buffer(buffer) { } ArrayBuffer::ArrayBuffer(void* data, unsigned int size) { - _buffer = boost::shared_ptr<Buffer>(new Buffer(data, size)); + _buffer = boost::shared_ptr<Blob>(new Blob(data, size)); } unsigned long ArrayBuffer::getByteLength() { diff --git a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h index 37b38a7..5584721 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h +++ b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h @@ -1,6 +1,8 @@ #ifndef TYPEDARRAY_H_99815BLY #define TYPEDARRAY_H_99815BLY +#include "uscxml/Message.h" + #include <string> #include <vector> #include <boost/shared_ptr.hpp> @@ -21,14 +23,6 @@ namespace uscxml { class ArrayBuffer { public: - class Buffer { - public: - ~Buffer(); - Buffer(size_t size); - Buffer(void* data, size_t size); - char* _data; - size_t _size; - }; ArrayBuffer(void* data, unsigned int size); @@ -38,7 +32,7 @@ public: * be allocated an exception is raised. */ ArrayBuffer(unsigned long length); - ArrayBuffer(boost::shared_ptr<ArrayBuffer::Buffer>); + ArrayBuffer(boost::shared_ptr<Blob>); /** * The length of the ArrayBuffer in bytes, as fixed at construction time. @@ -79,7 +73,7 @@ public: // memcpy(_buffer->_data + index * sizeof(unsigned char), &value, sizeof(unsigned char)); // } - boost::shared_ptr<Buffer> _buffer; + boost::shared_ptr<Blob> _buffer; }; class ArrayBufferView { @@ -105,7 +99,7 @@ public: virtual unsigned long getByteLength() = 0; virtual unsigned long getLength() = 0; protected: - boost::shared_ptr<ArrayBuffer::Buffer> _buffer; + boost::shared_ptr<Blob> _buffer; unsigned long _start; unsigned long _end; }; @@ -224,7 +218,7 @@ public: _buffer = buffer->_buffer; } - TypedArray(boost::shared_ptr<ArrayBuffer::Buffer> buffer, unsigned long byteOffset, unsigned long length) { + TypedArray(boost::shared_ptr<Blob> buffer, unsigned long byteOffset, unsigned long length) { if (byteOffset % sizeof(S)) return; @@ -247,7 +241,7 @@ public: TypedArray(unsigned long length) { _start = 0; _end = length; - _buffer = boost::shared_ptr<ArrayBuffer::Buffer>(new ArrayBuffer::Buffer(length * sizeof(S))); + _buffer = boost::shared_ptr<Blob>(new Blob(length * sizeof(S))); } /** @@ -260,7 +254,7 @@ public: TypedArray(std::vector<T> data) { _start = 0; _end = data.size(); - _buffer = boost::shared_ptr<ArrayBuffer::Buffer>(new ArrayBuffer::Buffer(data.size() * sizeof(S))); + _buffer = boost::shared_ptr<Blob>(new Blob(data.size() * sizeof(S))); set(data, 0); } TypedArray(TypedArray* other) { diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp index caf482b..77efe78 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp @@ -282,6 +282,11 @@ Data V8DataModel::getValueAsData(const v8::Handle<v8::Value>& value, std::set<v8 } else if (value->IsNumberObject()) { LOG(ERROR) << "IsNumberObject is unimplemented" << std::endl; } else if (value->IsObject()) { + if (V8ArrayBuffer::hasInstance(value)) { + uscxml::V8ArrayBuffer::V8ArrayBufferPrivate* privObj = V8DOM::toClassPtr<V8ArrayBuffer::V8ArrayBufferPrivate >(value->ToObject()->GetInternalField(0)); + data.binary = privObj->nativeObj->_buffer; + return data; + } v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); v8::Local<v8::Array> properties = object->GetPropertyNames(); for (int i = 0; i < properties->Length(); i++) { @@ -350,15 +355,18 @@ v8::Handle<v8::Value> V8DataModel::getDataAsValue(const Data& data) { } return value; } - switch (data.type) { - case Data::VERBATIM: - return v8::String::New(data.atom.c_str()); - break; - case Data::INTERPRETED: - return evalAsValue(data.atom); - break; - case Data::BINARY: { - uscxml::ArrayBuffer* arrBuffer = new uscxml::ArrayBuffer((void*)data.atom.c_str(), data.atom.size()); + if (data.atom.length() > 0) { + switch (data.type) { + case Data::VERBATIM: + return v8::String::New(data.atom.c_str()); + break; + case Data::INTERPRETED: + return evalAsValue(data.atom); + break; + } + } + if (data.binary) { + uscxml::ArrayBuffer* arrBuffer = new uscxml::ArrayBuffer(data.binary); v8::Handle<v8::Function> retCtor = V8ArrayBuffer::getTmpl()->GetFunction(); v8::Persistent<v8::Object> retObj = v8::Persistent<v8::Object>::New(retCtor->NewInstance()); @@ -368,8 +376,6 @@ v8::Handle<v8::Value> V8DataModel::getDataAsValue(const Data& data) { retObj.MakeWeak(0, V8ArrayBuffer::jsDestructor); return retObj; - break; - } } // this will never be reached return v8::Undefined(); |