2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file JSAbstractFilterArray.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
26 #include <dpl/log/log.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
30 #include "FilterConverter.h"
31 #include "JSAbstractFilterArray.h"
33 #define FUNCTION_CONCAT "concat"
34 #define FUNCTION_JOIN "join"
35 #define FUNCTION_POP "pop"
36 #define FUNCTION_PUSH "push"
37 #define FUNCTION_REVERSE "reverse"
38 #define FUNCTION_SHIFT "shift"
39 #define FUNCTION_SLICE "slice"
40 #define FUNCTION_SORT "sort"
41 #define FUNCTION_SPLICE "splice"
42 #define FUNCTION_TOSTRING "toString"
43 #define FUNCTION_UNSHIFT "unshift"
44 #define FUNCTION_VALUEOF "valueOf"
46 #define ATTRIBUTE_LENGTH "length"
47 #define FUNCTION_CONSTRUCTOR "constructor"
49 namespace DeviceAPI {
\rnamespace Tizen {
51 using namespace DeviceAPI::Common;
52 using namespace DeviceAPI::Tizen;
53 using namespace WrtDeviceApis::CommonsJavaScript;
55 JSClassDefinition JSAbstractFilterArray::m_classInfo = {
57 kJSClassAttributeNone,
67 NULL, //deleteProperty,
69 NULL, //callAsFunction,
70 NULL, //callAsConstructor,
72 NULL, //convertToType,
75 JSStaticValue JSAbstractFilterArray::m_property[] = {
76 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
80 JSStaticFunction JSAbstractFilterArray::m_function[] = {
81 { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
82 { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
83 { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
84 { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
85 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
86 { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
87 { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
88 { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
89 { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
90 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
91 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
92 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
93 { FUNCTION_CONSTRUCTOR, constructor, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
97 JSClassRef JSAbstractFilterArray::m_jsClassRef = JSClassCreate(
98 JSAbstractFilterArray::getClassInfo());
100 JSValueRef JSAbstractFilterArray::getLength(JSContextRef context,
102 JSStringRef propertyName,
103 JSValueRef* exception)
108 JSAbstractFilterArrayPriv* priv =
109 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
111 Throw(WrtDeviceApis::Commons::NullPointerException);
113 FilterArrayPtr filters = priv->getObject();
115 FilterConverterFactory::ConverterType converter =
116 FilterConverterFactory::getConverter(context);
117 return converter->toJSValueRef(filters->size());
120 Catch(WrtDeviceApis::Commons::Exception)
122 LogError("invalid conversion");
124 return JSValueMakeUndefined(context);
127 bool JSAbstractFilterArray::isObjectOfClass(JSContextRef context, JSValueRef value)
129 return JSValueIsObjectOfClass(context, value, getClassRef());
132 FilterArrayPtr JSAbstractFilterArray::getAbstractFilterArray(JSContextRef context, JSValueRef value)
134 if (!isObjectOfClass(context, value)) {
135 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
137 JSObjectRef object = JSValueToObject(context, value, NULL);
139 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
141 JSAbstractFilterArrayPriv *priv = static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
143 Throw(WrtDeviceApis::Commons::NullPointerException);
145 return priv->getObject();
148 JSObjectRef JSAbstractFilterArray::createArray(JSContextRef context,
149 const FilterArrayPtr &filters)
151 JSAbstractFilterArrayPriv *priv = new JSAbstractFilterArrayPriv(context, filters);
152 return JSObjectMake(context, getClassRef(), priv);
155 const JSClassDefinition* JSAbstractFilterArray::getClassInfo()
157 return &(m_classInfo);
160 JSClassRef JSAbstractFilterArray::getClassRef()
163 m_jsClassRef = JSClassCreate(&m_classInfo);
168 void JSAbstractFilterArray::initialize(JSContextRef context,
174 void JSAbstractFilterArray::finalize(JSObjectRef object)
177 JSAbstractFilterArrayPriv* priv =
178 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
180 JSObjectSetPrivate(object, NULL);
183 bool JSAbstractFilterArray::hasProperty(JSContextRef context,
185 JSStringRef propertyName)
188 FilterConverterFactory::ConverterType converter =
189 FilterConverterFactory::getConverter(context);
192 size_t index = converter->toSizeT(propertyName);
193 JSAbstractFilterArrayPriv* priv =
194 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
196 Throw(WrtDeviceApis::Commons::NullPointerException);
198 FilterArrayPtr filters = priv->getObject();
199 if (index < filters->size()) {
203 Catch(WrtDeviceApis::Commons::Exception)
205 //not reporting error is intended
210 JSValueRef JSAbstractFilterArray::getProperty(JSContextRef context,
212 JSStringRef propertyName,
213 JSValueRef* exception)
216 FilterConverterFactory::ConverterType converter =
217 FilterConverterFactory::getConverter(context);
220 size_t index = converter->toSizeT(propertyName);
221 JSAbstractFilterArrayPriv* priv =
222 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
224 Throw(WrtDeviceApis::Commons::NullPointerException);
226 FilterArrayPtr filters = priv->getObject();
227 if (index < filters->size()) {
228 FilterPtr result = filters->at(index);
230 return converter->toJSValueRef(result);
234 Catch(WrtDeviceApis::Commons::Exception)
236 LogError("invalid property");
238 return JSValueMakeUndefined(context);
241 bool JSAbstractFilterArray::setProperty(JSContextRef context,
243 JSStringRef propertyName,
245 JSValueRef* exception)
248 FilterConverterFactory::ConverterType converter =
249 FilterConverterFactory::getConverter(context);
252 size_t index = converter->toSizeT(propertyName);
254 if (!JSValueIsUndefined(context, value)) {
255 filter = converter->toFilter(value);
257 JSAbstractFilterArrayPriv* priv =
258 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
260 Throw(WrtDeviceApis::Commons::NullPointerException);
262 FilterArrayPtr filters = priv->getObject();
264 Throw(WrtDeviceApis::Commons::NullPointerException);
266 if (filters->size() <= index) {
267 filters->resize(index + 1);
269 (*filters)[index] = filter;
272 Catch(WrtDeviceApis::Commons::Exception)
274 LogError("error occured");
275 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
280 void JSAbstractFilterArray::getPropertyNames(JSContextRef context,
282 JSPropertyNameAccumulatorRef propertyNames)
284 FilterConverterFactory::ConverterType converter =
285 FilterConverterFactory::getConverter(context);
288 JSAbstractFilterArrayPriv* priv =
289 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(object));
291 Throw(WrtDeviceApis::Commons::NullPointerException);
293 FilterArrayPtr filters = priv->getObject();
295 int count = filters->size();
297 for(int i=0; i < count; i++)
299 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
300 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
303 Catch(WrtDeviceApis::Commons::Exception)
305 LogError("invalid property");
309 JSValueRef JSAbstractFilterArray::concat(JSContextRef context,
310 JSObjectRef function,
311 JSObjectRef thisObject,
312 size_t argumentCount,
313 const JSValueRef arguments[],
314 JSValueRef* exception)
319 FilterArrayPtr filters = FilterArrayPtr(new FilterArray());
320 JSAbstractFilterArrayPriv *newPrivateObject = new JSAbstractFilterArrayPriv(context, filters);
321 JSValueRef result = JSObjectMake(context, getClassRef(), newPrivateObject);
323 //copy current filters
324 JSAbstractFilterArrayPriv* priv =
325 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
326 FilterArrayPtr currentWebSites = priv->getObject();
327 for (size_t i = 0; i < currentWebSites->size(); ++i) {
328 filters->push_back(currentWebSites->at(i));
331 //copy submitted arrays
332 FilterConverterFactory::ConverterType converter =
333 FilterConverterFactory::getConverter(context);
334 for (size_t i = 0; i < argumentCount; ++i) {
335 if (!JSIsArrayValue(context, arguments[i])) {
336 Throw(WrtDeviceApis::Commons::ConversionException);
338 // process array of strings
339 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
340 unsigned int len = JSGetArrayLength(context, arrayObj);
341 for (unsigned int e = 0; e < len; ++e) {
342 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
343 filters->push_back(converter->toFilter(att));
348 Catch(WrtDeviceApis::Commons::Exception)
350 LogError("error occured");
352 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
355 JSValueRef JSAbstractFilterArray::join(JSContextRef context,
356 JSObjectRef function,
357 JSObjectRef thisObject,
358 size_t argumentCount,
359 const JSValueRef arguments[],
360 JSValueRef* exception)
362 //LogDebug("entered");
366 std::string separator(",");
367 FilterConverterFactory::ConverterType converter =
368 FilterConverterFactory::getConverter(context);
369 JSAbstractFilterArrayPriv* priv =
370 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
371 FilterArrayPtr currentWebSites = priv->getObject();
372 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
373 separator = converter->toString(arguments[0]);
375 for (size_t i = 0; i < currentWebSites->size(); ++i) {
379 //FIXME : to be changed to support join
380 //result += currentWebSites->at(i);
382 return converter->toJSValueRef(result);
384 Catch(WrtDeviceApis::Commons::Exception)
386 LogError("error occured");
388 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
391 JSValueRef JSAbstractFilterArray::pop(JSContextRef context,
392 JSObjectRef function,
393 JSObjectRef thisObject,
394 size_t argumentCount,
395 const JSValueRef arguments[],
396 JSValueRef* exception)
398 //LogDebug("entered");
401 FilterConverterFactory::ConverterType converter =
402 FilterConverterFactory::getConverter(context);
403 JSAbstractFilterArrayPriv* priv =
404 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
405 FilterArrayPtr currentWebSites = priv->getObject();
406 if (currentWebSites->size() > 0) {
407 FilterPtr result = currentWebSites->at(
408 currentWebSites->size() - 1);
409 currentWebSites->pop_back();
410 return converter->toJSValueRef(result);
413 Catch(WrtDeviceApis::Commons::Exception)
415 LogError("error occured");
417 return JSValueMakeUndefined(context);
420 JSValueRef JSAbstractFilterArray::push(JSContextRef context,
421 JSObjectRef function,
422 JSObjectRef thisObject,
423 size_t argumentCount,
424 const JSValueRef arguments[],
425 JSValueRef* exception)
427 //LogDebug("entered");
430 FilterConverterFactory::ConverterType converter =
431 FilterConverterFactory::getConverter(context);
432 JSAbstractFilterArrayPriv* priv =
433 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
434 FilterArrayPtr currentWebSites = priv->getObject();
435 for (size_t i = 0; i < argumentCount; ++i) {
436 currentWebSites->push_back(converter->toFilter(arguments[i]));
438 return converter->toJSValueRef(currentWebSites->size());
440 Catch(WrtDeviceApis::Commons::Exception)
442 LogError("error occured");
444 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
447 JSValueRef JSAbstractFilterArray::reverse(JSContextRef context,
448 JSObjectRef function,
449 JSObjectRef thisObject,
450 size_t argumentCount,
451 const JSValueRef arguments[],
452 JSValueRef* exception)
454 //LogDebug("entered");
457 FilterConverterFactory::ConverterType converter =
458 FilterConverterFactory::getConverter(context);
459 JSAbstractFilterArrayPriv* priv =
460 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
461 FilterArrayPtr currentWebSites = priv->getObject();
462 std::reverse(currentWebSites->begin(), currentWebSites->end());
465 Catch(WrtDeviceApis::Commons::Exception)
467 LogError("error occured");
469 return JSValueMakeUndefined(context);
472 JSValueRef JSAbstractFilterArray::shift(JSContextRef context,
473 JSObjectRef function,
474 JSObjectRef thisObject,
475 size_t argumentCount,
476 const JSValueRef arguments[],
477 JSValueRef* exception)
479 //LogDebug("entered");
482 FilterConverterFactory::ConverterType converter =
483 FilterConverterFactory::getConverter(context);
484 JSAbstractFilterArrayPriv* priv =
485 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
486 FilterArrayPtr currentWebSites = priv->getObject();
487 if (currentWebSites->size() > 0) {
488 FilterPtr result = currentWebSites->at(0);
489 currentWebSites->erase(currentWebSites->begin());
490 return converter->toJSValueRef(result);
493 Catch(WrtDeviceApis::Commons::Exception)
495 LogError("error occured");
497 return JSValueMakeUndefined(context);
500 JSValueRef JSAbstractFilterArray::slice(JSContextRef context,
501 JSObjectRef function,
502 JSObjectRef thisObject,
503 size_t argumentCount,
504 const JSValueRef arguments[],
505 JSValueRef* exception)
510 if (argumentCount < 1) {
511 return JSValueMakeUndefined(context);
513 FilterConverterFactory::ConverterType converter =
514 FilterConverterFactory::getConverter(context);
515 FilterArrayPtr filters = FilterArrayPtr(new FilterArray());
516 JSAbstractFilterArrayPriv *newPrivateObject = new JSAbstractFilterArrayPriv(
519 JSValueRef result = JSObjectMake(context,
520 getClassRef(), newPrivateObject);
522 //copy current filters
523 JSAbstractFilterArrayPriv* priv =
524 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
525 FilterArrayPtr currentWebSites = priv->getObject();
526 std::size_t first = converter->toSizeT(arguments[0]);
527 std::size_t last = currentWebSites->size() - 1;
528 if (argumentCount > 1) {
529 last = converter->toSizeT(arguments[1]);
530 if (last >= currentWebSites->size()) {
531 last = currentWebSites->size() - 1;
534 for (size_t i = first; i <= last; ++i) {
535 filters->push_back(currentWebSites->at(i));
540 Catch(WrtDeviceApis::Commons::Exception)
542 LogError("error occured");
544 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
547 JSValueRef JSAbstractFilterArray::sort(JSContextRef context,
548 JSObjectRef function,
549 JSObjectRef thisObject,
550 size_t argumentCount,
551 const JSValueRef arguments[],
552 JSValueRef* exception)
554 //LogDebug("entered");
557 FilterConverterFactory::ConverterType converter =
558 FilterConverterFactory::getConverter(context);
559 JSAbstractFilterArrayPriv* priv =
560 static_cast<JSAbstractFilterArrayPriv*>(JSObjectGetPrivate(thisObject));
561 FilterArrayPtr currentWebSites = priv->getObject();
562 std::sort(currentWebSites->begin(), currentWebSites->end());
565 Catch(WrtDeviceApis::Commons::Exception)
567 LogError("error occured");
569 return JSValueMakeUndefined(context);
572 JSValueRef JSAbstractFilterArray::splice(JSContextRef context,
573 JSObjectRef function,
574 JSObjectRef thisObject,
575 size_t argumentCount,
576 const JSValueRef arguments[],
577 JSValueRef* exception)
579 //LogDebug("entered");
580 return JSValueMakeUndefined(context);
583 JSValueRef JSAbstractFilterArray::toString(JSContextRef context,
584 JSObjectRef function,
585 JSObjectRef thisObject,
586 size_t argumentCount,
587 const JSValueRef arguments[],
588 JSValueRef* exception)
590 //LogDebug("entered");
591 return join(context, function, thisObject, 0, arguments, exception);
594 JSValueRef JSAbstractFilterArray::unshift(JSContextRef context,
595 JSObjectRef function,
596 JSObjectRef thisObject,
597 size_t argumentCount,
598 const JSValueRef arguments[],
599 JSValueRef* exception)
601 //LogDebug("entered");
602 return JSValueMakeUndefined(context);
605 JSValueRef JSAbstractFilterArray::valueOf(JSContextRef context,
606 JSObjectRef function,
607 JSObjectRef thisObject,
608 size_t argumentCount,
609 const JSValueRef arguments[],
610 JSValueRef* exception)
612 //LogDebug("entered");
613 return JSValueMakeUndefined(context);
616 JSValueRef JSAbstractFilterArray::constructor(JSContextRef context,
617 JSObjectRef function,
618 JSObjectRef thisObject,
619 size_t argumentCount,
620 const JSValueRef arguments[],
621 JSValueRef* exception)
623 // To determine this object as array
624 WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef json(JSStringCreateWithUTF8CString("[]"));
625 JSValueRef array = JSValueMakeFromJSONString(context, json.get());
626 WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef constructorStr(JSStringCreateWithUTF8CString("constructor"));
627 JSObjectRef arrayObject = JSValueToObject(context, array, NULL);
628 JSValueRef constructorValue = JSObjectGetProperty(context, arrayObject, constructorStr.get(), NULL);
629 return constructorValue;