-//
-// Tizen Web Device API
-// Copyright (c) 2012 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Apache License, Version 2.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-/**
- * @file JSAbstractFilterArray.cpp
- * @author Kisub Song (kisubs.song@samsung.com)
- * @version 0.1
- * @brief
- */
-
-#include <algorithm>
-#include <dpl/log/log.h>
-#include <CommonsJavaScript/ScopedJSStringRef.h>
-#include <JSTizenExceptionFactory.h>
-#include <JSTizenException.h>
-#include "FilterConverter.h"
-#include "JSAbstractFilterArray.h"
-
-#define FUNCTION_CONCAT "concat"
-#define FUNCTION_JOIN "join"
-#define FUNCTION_POP "pop"
-#define FUNCTION_PUSH "push"
-#define FUNCTION_REVERSE "reverse"
-#define FUNCTION_SHIFT "shift"
-#define FUNCTION_SLICE "slice"
-#define FUNCTION_SORT "sort"
-#define FUNCTION_SPLICE "splice"
-#define FUNCTION_TOSTRING "toString"
-#define FUNCTION_UNSHIFT "unshift"
-#define FUNCTION_VALUEOF "valueOf"
-#define ARRAY "Array"
-#define ATTRIBUTE_LENGTH "length"
-#define FUNCTION_CONSTRUCTOR "constructor"
-
-namespace DeviceAPI {\rnamespace Tizen {
-
-using namespace DeviceAPI::Common;
-using namespace DeviceAPI::Tizen;
-using namespace WrtDeviceApis::CommonsJavaScript;
-
-JSClassDefinition JSAbstractFilterArray::m_classInfo = {
- 0,
- kJSClassAttributeNone,
- ARRAY,
- 0,
- m_property,
- m_function,
- initialize,
- finalize,
- hasProperty,
- getProperty,
- setProperty,
- NULL, //deleteProperty,
- getPropertyNames,
- NULL, //callAsFunction,
- NULL, //callAsConstructor,
- NULL, //hasInstance,
- NULL, //convertToType,
-};
-
-JSStaticValue JSAbstractFilterArray::m_property[] = {
- { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { 0, 0, 0, 0 }
-};
-
-JSStaticFunction JSAbstractFilterArray::m_function[] = {
- { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { FUNCTION_CONSTRUCTOR, constructor, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
- { 0, 0, 0 }
-};
-
-JSClassRef JSAbstractFilterArray::m_jsClassRef = JSClassCreate(
- JSAbstractFilterArray::getClassInfo());
-
-JSValueRef JSAbstractFilterArray::getLength(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName,
- JSValueRef* exception)
-{
- //LogDebug("enter");
- Try
- {
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- FilterArrayPtr filters = priv->getObject();
- if (filters) {
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- return converter->toJSValueRef(filters->size());
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("invalid conversion");
- }
- return JSValueMakeUndefined(context);
-}
-
-bool JSAbstractFilterArray::isObjectOfClass(JSContextRef context, JSValueRef value)
-{
- return JSValueIsObjectOfClass(context, value, getClassRef());
-}
-
-FilterArrayPtr JSAbstractFilterArray::getAbstractFilterArray(JSContextRef context, JSValueRef value)
-{
- if (!isObjectOfClass(context, value)) {
- Throw(WrtDeviceApis::Commons::InvalidArgumentException);
- }
- JSObjectRef object = JSValueToObject(context, value, NULL);
- if (!object) {
- Throw(WrtDeviceApis::Commons::InvalidArgumentException);
- }
- JSAbstractFilterArrayPriv *priv = static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- return priv->getObject();
-}
-
-JSObjectRef JSAbstractFilterArray::createArray(JSContextRef context,
- const FilterArrayPtr &filters)
-{
- JSAbstractFilterArrayPriv *priv = new JSAbstractFilterArrayPriv(context, filters);
- return JSObjectMake(context, getClassRef(), priv);
-}
-
-const JSClassDefinition* JSAbstractFilterArray::getClassInfo()
-{
- return &(m_classInfo);
-}
-
-JSClassRef JSAbstractFilterArray::getClassRef()
-{
- if (!m_jsClassRef) {
- m_jsClassRef = JSClassCreate(&m_classInfo);
- }
- return m_jsClassRef;
-}
-
-void JSAbstractFilterArray::initialize(JSContextRef context,
- JSObjectRef object)
-{
- //LogDebug("enter");
-}
-
-void JSAbstractFilterArray::finalize(JSObjectRef object)
-{
- //LogDebug("enter");
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- delete priv;
- JSObjectSetPrivate(object, NULL);
-}
-
-bool JSAbstractFilterArray::hasProperty(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName)
-{
- //LogDebug("enter");
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- Try
- {
- size_t index = converter->toSizeT(propertyName);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- FilterArrayPtr filters = priv->getObject();
- if (index < filters->size()) {
- return true;
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- //not reporting error is intended
- }
- return false;
-}
-
-JSValueRef JSAbstractFilterArray::getProperty(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName,
- JSValueRef* exception)
-{
- //LogDebug("enter");
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- Try
- {
- size_t index = converter->toSizeT(propertyName);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- FilterArrayPtr filters = priv->getObject();
- if (index < filters->size()) {
- FilterPtr result = filters->at(index);
- if (result) {
- return converter->toJSValueRef(result);
- }
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("invalid property");
- }
- return JSValueMakeUndefined(context);
-}
-
-bool JSAbstractFilterArray::setProperty(JSContextRef context,
- JSObjectRef object,
- JSStringRef propertyName,
- JSValueRef value,
- JSValueRef* exception)
-{
- //LogDebug("enter");
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- Try
- {
- size_t index = converter->toSizeT(propertyName);
- FilterPtr filter;
- if (!JSValueIsUndefined(context, value)) {
- filter = converter->toFilter(value);
- }
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- FilterArrayPtr filters = priv->getObject();
- if (!filters) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- if (filters->size() <= index) {
- filters->resize(index + 1);
- }
- (*filters)[index] = filter;
- return true;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
- }
- return false;
-}
-
-void JSAbstractFilterArray::getPropertyNames(JSContextRef context,
- JSObjectRef object,
- JSPropertyNameAccumulatorRef propertyNames)
-{
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- Try
- {
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
- if (!priv) {
- Throw(WrtDeviceApis::Commons::NullPointerException);
- }
- FilterArrayPtr filters = priv->getObject();
-
- int count = filters->size();
-
- for(int i=0; i < count; i++)
- {
- ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
- JSPropertyNameAccumulatorAddName(propertyNames, name.get());
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("invalid property");
- }
-}
-
-JSValueRef JSAbstractFilterArray::concat(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("enter");
- Try
- {
- FilterArrayPtr filters = FilterArrayPtr(new FilterArray());
- JSAbstractFilterArrayPriv *newPrivateObject = new JSAbstractFilterArrayPriv(context, filters);
- JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
-
- //copy current filters
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- for (size_t i = 0; i < currentWebSites->size(); ++i) {
- filters->push_back(currentWebSites->at(i));
- }
-
- //copy submitted arrays
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- for (size_t i = 0; i < argumentCount; ++i) {
- if (!JSIsArrayValue(context, arguments[i])) {
- Throw(WrtDeviceApis::Commons::ConversionException);
- }
- // process array of strings
- JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
- unsigned int len = JSGetArrayLength(context, arrayObj);
- for (unsigned int e = 0; e < len; ++e) {
- JSValueRef att = JSGetArrayElement(context, arrayObj, e);
- filters->push_back(converter->toFilter(att));
- }
- }
- return result;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSAbstractFilterArray::join(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- Try
- {
- std::string result;
- std::string separator(",");
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
- separator = converter->toString(arguments[0]);
- }
- for (size_t i = 0; i < currentWebSites->size(); ++i) {
- if (i != 0) {
- result += separator;
- }
- //FIXME : to be changed to support join
- //result += currentWebSites->at(i);
- }
- return converter->toJSValueRef(result);
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSAbstractFilterArray::pop(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- Try
- {
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- if (currentWebSites->size() > 0) {
- FilterPtr result = currentWebSites->at(
- currentWebSites->size() - 1);
- currentWebSites->pop_back();
- return converter->toJSValueRef(result);
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::push(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- Try
- {
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- for (size_t i = 0; i < argumentCount; ++i) {
- currentWebSites->push_back(converter->toFilter(arguments[i]));
- }
- return converter->toJSValueRef(currentWebSites->size());
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSAbstractFilterArray::reverse(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- Try
- {
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- std::reverse(currentWebSites->begin(), currentWebSites->end());
- return thisObject;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::shift(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- Try
- {
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- if (currentWebSites->size() > 0) {
- FilterPtr result = currentWebSites->at(0);
- currentWebSites->erase(currentWebSites->begin());
- return converter->toJSValueRef(result);
- }
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::slice(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("enter");
- Try
- {
- if (argumentCount < 1) {
- return JSValueMakeUndefined(context);
- }
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- FilterArrayPtr filters = FilterArrayPtr(new FilterArray());
- JSAbstractFilterArrayPriv *newPrivateObject = new JSAbstractFilterArrayPriv(
- context,
- filters);
- JSValueRef result = JSObjectMake(context,
- getClassRef(), newPrivateObject);
-
- //copy current filters
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- std::size_t first = converter->toSizeT(arguments[0]);
- std::size_t last = currentWebSites->size() - 1;
- if (argumentCount > 1) {
- last = converter->toSizeT(arguments[1]);
- if (last >= currentWebSites->size()) {
- last = currentWebSites->size() - 1;
- }
- }
- for (size_t i = first; i <= last; ++i) {
- filters->push_back(currentWebSites->at(i));
- }
-
- return result;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
-}
-
-JSValueRef JSAbstractFilterArray::sort(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- Try
- {
- FilterConverterFactory::ConverterType converter =
- FilterConverterFactory::getConverter(context);
- JSAbstractFilterArrayPriv* priv =
- static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
- FilterArrayPtr currentWebSites = priv->getObject();
- std::sort(currentWebSites->begin(), currentWebSites->end());
- return thisObject;
- }
- Catch(WrtDeviceApis::Commons::Exception)
- {
- LogError("error occured");
- }
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::splice(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::toString(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- return join(context, function, thisObject, 0, arguments, exception);
-}
-
-JSValueRef JSAbstractFilterArray::unshift(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::valueOf(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- //LogDebug("entered");
- return JSValueMakeUndefined(context);
-}
-
-JSValueRef JSAbstractFilterArray::constructor(JSContextRef context,
- JSObjectRef function,
- JSObjectRef thisObject,
- size_t argumentCount,
- const JSValueRef arguments[],
- JSValueRef* exception)
-{
- // To determine this object as array
- WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef json(JSStringCreateWithUTF8CString("[]"));
- JSValueRef array = JSValueMakeFromJSONString(context, json.get());
- WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef constructorStr(JSStringCreateWithUTF8CString("constructor"));
- JSObjectRef arrayObject = JSValueToObject(context, array, NULL);
- JSValueRef constructorValue = JSObjectGetProperty(context, arrayObject, constructorStr.get(), NULL);
- return constructorValue;
-}
-
-} // Tizen
-} // DeviceAPI
-