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 JSAttributesOfInterestArray.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
26 #include <CommonsJavaScript/Converter.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
30 #include "ContactConverter.h"
31 #include "JSAttributesOfInterestArray.h"
34 #define FUNCTION_CONCAT "concat"
35 #define FUNCTION_JOIN "join"
36 #define FUNCTION_POP "pop"
37 #define FUNCTION_PUSH "push"
38 #define FUNCTION_REVERSE "reverse"
39 #define FUNCTION_SHIFT "shift"
40 #define FUNCTION_SLICE "slice"
41 #define FUNCTION_SORT "sort"
42 #define FUNCTION_SPLICE "splice"
43 #define FUNCTION_TOSTRING "toString"
44 #define FUNCTION_UNSHIFT "unshift"
45 #define FUNCTION_VALUEOF "valueOf"
47 #define ATTRIBUTE_LENGTH "length"
52 using namespace DeviceAPI::Common;
53 using namespace WrtDeviceApis::CommonsJavaScript;
55 JSClassDefinition JSAttributesOfInterestArray::m_classInfo = {
57 kJSClassAttributeNone,
67 NULL, //deleteProperty,
69 NULL, //callAsFunction,
70 NULL, //callAsConstructor,
72 NULL, //convertToType,
75 JSStaticValue JSAttributesOfInterestArray::m_property[] = {
76 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
80 JSStaticFunction JSAttributesOfInterestArray::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 },
96 JSClassRef JSAttributesOfInterestArray::m_jsClassRef = JSClassCreate(
97 JSAttributesOfInterestArray::getClassInfo());
99 JSValueRef JSAttributesOfInterestArray::getLength(JSContextRef context,
101 JSStringRef propertyName,
102 JSValueRef* exception)
107 JSAttributesOfInterestArrayPriv* priv =
108 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
110 Throw(WrtDeviceApis::Commons::NullPointerException);
112 AttributesOfInterestArrayPtr typeArray = priv->getObject();
114 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
115 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
116 return converter->toJSValueRef(typeArray->size());
119 Catch(WrtDeviceApis::Commons::Exception)
121 LoggerE("invalid conversion");
123 return JSValueMakeUndefined(context);
126 JSObjectRef JSAttributesOfInterestArray::createArray(JSContextRef context,
127 const AttributesOfInterestArrayPtr &typeArray)
129 JSAttributesOfInterestArrayPriv *priv = new JSAttributesOfInterestArrayPriv(context, typeArray);
130 return JSObjectMake(context, getClassRef(), priv);
133 const JSClassDefinition* JSAttributesOfInterestArray::getClassInfo()
135 return &(m_classInfo);
138 JSClassRef JSAttributesOfInterestArray::getClassRef()
141 m_jsClassRef = JSClassCreate(&m_classInfo);
146 bool JSAttributesOfInterestArray::isObjectOfClass(JSContextRef context, JSValueRef value)
148 return JSValueIsObjectOfClass(context, value, getClassRef());
151 AttributesOfInterestArrayPtr JSAttributesOfInterestArray::getAttributesOfInterestArray(JSContextRef context, JSValueRef value)
153 if (!isObjectOfClass(context, value)) {
154 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
156 JSObjectRef object = JSValueToObject(context, value, NULL);
158 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
160 JSAttributesOfInterestArrayPriv *priv = static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
162 Throw(WrtDeviceApis::Commons::NullPointerException);
164 return priv->getObject();
167 void JSAttributesOfInterestArray::initialize(JSContextRef context,
173 void JSAttributesOfInterestArray::finalize(JSObjectRef object)
176 JSAttributesOfInterestArrayPriv* priv =
177 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
179 JSObjectSetPrivate(object, NULL);
182 bool JSAttributesOfInterestArray::hasProperty(JSContextRef context,
184 JSStringRef propertyName)
187 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
188 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
191 size_t index = converter->toSizeT(propertyName);
192 JSAttributesOfInterestArrayPriv* priv =
193 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
195 Throw(WrtDeviceApis::Commons::NullPointerException);
197 AttributesOfInterestArrayPtr typeArray = priv->getObject();
198 if (index < typeArray->size()) {
202 Catch(WrtDeviceApis::Commons::Exception)
204 //not reporting error is intended
209 JSValueRef JSAttributesOfInterestArray::getProperty(JSContextRef context,
211 JSStringRef propertyName,
212 JSValueRef* exception)
215 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
216 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
219 size_t index = converter->toSizeT(propertyName);
220 JSAttributesOfInterestArrayPriv* priv =
221 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
223 Throw(WrtDeviceApis::Commons::NullPointerException);
225 AttributesOfInterestArrayPtr typeArray = priv->getObject();
226 if (index < typeArray->size()) {
227 std::string result = typeArray->at(index);
228 if (!result.empty()) {
229 return converter->toJSValueRef(result);
233 Catch(WrtDeviceApis::Commons::Exception)
235 LoggerE("invalid property");
237 return JSValueMakeUndefined(context);
240 bool JSAttributesOfInterestArray::setProperty(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
247 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
248 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
251 size_t index = converter->toSizeT(propertyName);
253 if (!JSValueIsUndefined(context, value)) {
254 str = converter->toString(value);
256 JSAttributesOfInterestArrayPriv* priv =
257 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
259 Throw(WrtDeviceApis::Commons::NullPointerException);
261 AttributesOfInterestArrayPtr typeArray = priv->getObject();
263 Throw(WrtDeviceApis::Commons::NullPointerException);
265 if (typeArray->size() <= index) {
266 typeArray->resize(index + 1);
268 (*typeArray)[index] = str;
271 Catch(WrtDeviceApis::Commons::Exception)
273 LoggerE("error occured");
274 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
279 void JSAttributesOfInterestArray::getPropertyNames(JSContextRef context,
281 JSPropertyNameAccumulatorRef propertyNames)
283 ContactConverterFactory::ConverterType converter =
284 ContactConverterFactory::getConverter(context);
287 JSAttributesOfInterestArrayPriv* priv =
288 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
290 Throw(WrtDeviceApis::Commons::NullPointerException);
292 AttributesOfInterestArrayPtr interests = priv->getObject();
294 int count = interests->size();
296 for(int i=0; i < count; i++)
298 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
299 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
302 Catch(WrtDeviceApis::Commons::Exception)
304 LoggerE("invalid property");
308 JSValueRef JSAttributesOfInterestArray::concat(JSContextRef context,
309 JSObjectRef function,
310 JSObjectRef thisObject,
311 size_t argumentCount,
312 const JSValueRef arguments[],
313 JSValueRef* exception)
318 AttributesOfInterestArrayPtr typeArray =
319 AttributesOfInterestArrayPtr(new AttributesOfInterestArray());
320 JSAttributesOfInterestArrayPriv *newPrivateObject =
321 new JSAttributesOfInterestArrayPriv(context, typeArray);
322 JSValueRef result = JSObjectMake(context,
323 getClassRef(), newPrivateObject);
325 //copy current typeArray
326 JSAttributesOfInterestArrayPriv* priv =
327 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
328 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
329 for (size_t i = 0; i < currentStrings->size(); ++i) {
330 typeArray->push_back(currentStrings->at(i));
333 //copy submitted arrays
334 ContactConverterFactory::ConverterType converter =
335 ContactConverterFactory::getConverter(context);
336 for (size_t i = 0; i < argumentCount; ++i) {
337 if (!JSIsArrayValue(context, arguments[i])) {
338 Throw(WrtDeviceApis::Commons::ConversionException);
340 // process array of strings
341 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
342 unsigned int len = JSGetArrayLength(context, arrayObj);
343 for (unsigned int e = 0; e < len; ++e) {
344 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
345 typeArray->push_back(converter->toString(att));
350 Catch(WrtDeviceApis::Commons::Exception)
352 LoggerE("error occured");
354 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
357 JSValueRef JSAttributesOfInterestArray::join(JSContextRef context,
358 JSObjectRef function,
359 JSObjectRef thisObject,
360 size_t argumentCount,
361 const JSValueRef arguments[],
362 JSValueRef* exception)
364 //LoggerD("entered");
368 std::string separator(",");
369 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
370 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
371 JSAttributesOfInterestArrayPriv* priv =
372 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
373 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
374 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
375 separator = converter->toString(arguments[0]);
377 for (size_t i = 0; i < currentStrings->size(); ++i) {
381 result += currentStrings->at(i);
383 return converter->toJSValueRef(result);
385 Catch(WrtDeviceApis::Commons::Exception)
387 LoggerE("error occured");
389 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
392 JSValueRef JSAttributesOfInterestArray::pop(JSContextRef context,
393 JSObjectRef function,
394 JSObjectRef thisObject,
395 size_t argumentCount,
396 const JSValueRef arguments[],
397 JSValueRef* exception)
399 //LoggerD("entered");
402 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
403 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
404 JSAttributesOfInterestArrayPriv* priv =
405 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
406 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
407 if (currentStrings->size() > 0) {
408 std::string result = currentStrings->at(
409 currentStrings->size() - 1);
410 currentStrings->pop_back();
411 return converter->toJSValueRef(result);
414 Catch(WrtDeviceApis::Commons::Exception)
416 LoggerE("error occured");
418 return JSValueMakeUndefined(context);
421 JSValueRef JSAttributesOfInterestArray::push(JSContextRef context,
422 JSObjectRef function,
423 JSObjectRef thisObject,
424 size_t argumentCount,
425 const JSValueRef arguments[],
426 JSValueRef* exception)
428 //LoggerD("entered");
431 ContactConverterFactory::ConverterType converter =
432 ContactConverterFactory::getConverter(context);
433 JSAttributesOfInterestArrayPriv* priv =
434 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
435 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
436 for (size_t i = 0; i < argumentCount; ++i) {
437 currentStrings->push_back(converter->toString(arguments[i]));
439 return converter->toJSValueRef(currentStrings->size());
441 Catch(WrtDeviceApis::Commons::Exception)
443 LoggerE("error occured");
445 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
448 JSValueRef JSAttributesOfInterestArray::reverse(JSContextRef context,
449 JSObjectRef function,
450 JSObjectRef thisObject,
451 size_t argumentCount,
452 const JSValueRef arguments[],
453 JSValueRef* exception)
455 //LoggerD("entered");
458 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
459 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
460 JSAttributesOfInterestArrayPriv* priv =
461 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
462 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
463 std::reverse(currentStrings->begin(), currentStrings->end());
466 Catch(WrtDeviceApis::Commons::Exception)
468 LoggerE("error occured");
470 return JSValueMakeUndefined(context);
473 JSValueRef JSAttributesOfInterestArray::shift(JSContextRef context,
474 JSObjectRef function,
475 JSObjectRef thisObject,
476 size_t argumentCount,
477 const JSValueRef arguments[],
478 JSValueRef* exception)
480 //LoggerD("entered");
483 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
484 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
485 JSAttributesOfInterestArrayPriv* priv =
486 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
487 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
488 if (currentStrings->size() > 0) {
489 std::string result = currentStrings->at(0);
490 currentStrings->erase(currentStrings->begin());
491 return converter->toJSValueRef(result);
494 Catch(WrtDeviceApis::Commons::Exception)
496 LoggerE("error occured");
498 return JSValueMakeUndefined(context);
501 JSValueRef JSAttributesOfInterestArray::slice(JSContextRef context,
502 JSObjectRef function,
503 JSObjectRef thisObject,
504 size_t argumentCount,
505 const JSValueRef arguments[],
506 JSValueRef* exception)
511 if (argumentCount < 1) {
512 return JSValueMakeUndefined(context);
514 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
515 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
516 AttributesOfInterestArrayPtr typeArray = AttributesOfInterestArrayPtr(new StringArray());
517 JSAttributesOfInterestArrayPriv *newPrivateObject = new JSAttributesOfInterestArrayPriv(
520 JSValueRef result = JSObjectMake(context,
521 getClassRef(), newPrivateObject);
523 //copy current typeArray
524 JSAttributesOfInterestArrayPriv* priv =
525 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
526 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
527 std::size_t first = converter->toSizeT(arguments[0]);
528 std::size_t last = currentStrings->size() - 1;
529 if (argumentCount > 1) {
530 last = converter->toSizeT(arguments[1]);
531 if (last >= currentStrings->size()) {
532 last = currentStrings->size() - 1;
535 for (size_t i = first; i <= last; ++i) {
536 typeArray->push_back(currentStrings->at(i));
541 Catch(WrtDeviceApis::Commons::Exception)
543 LoggerE("error occured");
545 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
548 JSValueRef JSAttributesOfInterestArray::sort(JSContextRef context,
549 JSObjectRef function,
550 JSObjectRef thisObject,
551 size_t argumentCount,
552 const JSValueRef arguments[],
553 JSValueRef* exception)
555 //LoggerD("entered");
558 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
559 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
560 JSAttributesOfInterestArrayPriv* priv =
561 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
562 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
563 std::sort(currentStrings->begin(), currentStrings->end());
566 Catch(WrtDeviceApis::Commons::Exception)
568 LoggerE("error occured");
570 return JSValueMakeUndefined(context);
573 JSValueRef JSAttributesOfInterestArray::splice(JSContextRef context,
574 JSObjectRef function,
575 JSObjectRef thisObject,
576 size_t argumentCount,
577 const JSValueRef arguments[],
578 JSValueRef* exception)
580 //LoggerD("entered");
581 return JSValueMakeUndefined(context);
584 JSValueRef JSAttributesOfInterestArray::toString(JSContextRef context,
585 JSObjectRef function,
586 JSObjectRef thisObject,
587 size_t argumentCount,
588 const JSValueRef arguments[],
589 JSValueRef* exception)
591 //LoggerD("entered");
592 return join(context, function, thisObject, 0, arguments, exception);
595 JSValueRef JSAttributesOfInterestArray::unshift(JSContextRef context,
596 JSObjectRef function,
597 JSObjectRef thisObject,
598 size_t argumentCount,
599 const JSValueRef arguments[],
600 JSValueRef* exception)
602 //LoggerD("entered");
603 return JSValueMakeUndefined(context);
606 JSValueRef JSAttributesOfInterestArray::valueOf(JSContextRef context,
607 JSObjectRef function,
608 JSObjectRef thisObject,
609 size_t argumentCount,
610 const JSValueRef arguments[],
611 JSValueRef* exception)
613 //LoggerD("entered");
614 return JSValueMakeUndefined(context);