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 JSContactPhoneNumberTypeArray.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 "JSContactPhoneNumberTypeArray.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 JSContactPhoneNumberTypeArray::m_classInfo = {
57 kJSClassAttributeNone,
69 NULL, //callAsFunction,
70 NULL, //callAsConstructor,
72 NULL, //convertToType,
75 JSStaticValue JSContactPhoneNumberTypeArray::m_property[] = {
76 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
80 JSStaticFunction JSContactPhoneNumberTypeArray::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 JSContactPhoneNumberTypeArray::m_jsClassRef = JSClassCreate(
97 JSContactPhoneNumberTypeArray::getClassInfo());
99 JSValueRef JSContactPhoneNumberTypeArray::getLength(JSContextRef context,
101 JSStringRef propertyName,
102 JSValueRef* exception)
106 JSContactPhoneNumberTypeArrayPriv* priv =
107 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
109 Throw(WrtDeviceApis::Commons::NullPointerException);
111 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
113 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
114 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
115 return converter->toJSValueRef(typeArray->size());
118 Catch(WrtDeviceApis::Commons::Exception)
120 LoggerE("invalid conversion");
122 return JSValueMakeUndefined(context);
125 JSObjectRef JSContactPhoneNumberTypeArray::createArray(JSContextRef context,
126 const ContactPhoneNumberTypeArrayPtr &typeArray)
128 JSContactPhoneNumberTypeArrayPriv *priv = new JSContactPhoneNumberTypeArrayPriv(context, typeArray);
129 return JSObjectMake(context, getClassRef(), priv);
132 const JSClassDefinition* JSContactPhoneNumberTypeArray::getClassInfo()
134 return &(m_classInfo);
137 JSClassRef JSContactPhoneNumberTypeArray::getClassRef()
140 m_jsClassRef = JSClassCreate(&m_classInfo);
145 bool JSContactPhoneNumberTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
147 return JSValueIsObjectOfClass(context, value, getClassRef());
150 ContactPhoneNumberTypeArrayPtr JSContactPhoneNumberTypeArray::getContactPhoneNumberTypeArray(JSContextRef context, JSValueRef value)
152 if (!isObjectOfClass(context, value)) {
153 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
155 JSObjectRef object = JSValueToObject(context, value, NULL);
157 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
159 JSContactPhoneNumberTypeArrayPriv *priv = static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
161 Throw(WrtDeviceApis::Commons::NullPointerException);
163 return priv->getObject();
166 void JSContactPhoneNumberTypeArray::initialize(JSContextRef context,
171 void JSContactPhoneNumberTypeArray::finalize(JSObjectRef object)
173 JSContactPhoneNumberTypeArrayPriv* priv =
174 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
176 JSObjectSetPrivate(object, NULL);
179 bool JSContactPhoneNumberTypeArray::hasProperty(JSContextRef context,
181 JSStringRef propertyName)
183 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
184 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
187 size_t index = converter->toSizeT(propertyName);
188 JSContactPhoneNumberTypeArrayPriv* priv =
189 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
191 Throw(WrtDeviceApis::Commons::NullPointerException);
193 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
194 if (index < typeArray->size()) {
198 Catch(WrtDeviceApis::Commons::Exception)
200 //not reporting error is intended
205 JSValueRef JSContactPhoneNumberTypeArray::getProperty(JSContextRef context,
207 JSStringRef propertyName,
208 JSValueRef* exception)
210 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
213 size_t index = converter->toSizeT(propertyName);
214 JSContactPhoneNumberTypeArrayPriv* priv =
215 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
217 Throw(WrtDeviceApis::Commons::NullPointerException);
219 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
220 if (index < typeArray->size()) {
221 std::string result = converter->toContactPhoneNumberTypeStr(typeArray->at(index));
222 if (!result.empty()) {
223 return converter->toJSValueRef(result);
227 Catch(WrtDeviceApis::Commons::Exception)
229 LoggerE("invalid property");
231 return JSValueMakeUndefined(context);
234 bool JSContactPhoneNumberTypeArray::setProperty(JSContextRef context,
236 JSStringRef propertyName,
238 JSValueRef* exception)
240 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
243 size_t index = converter->toSizeT(propertyName);
245 if (!JSValueIsUndefined(context, value)) {
246 str = converter->toString(value);
248 JSContactPhoneNumberTypeArrayPriv* priv =
249 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
251 Throw(WrtDeviceApis::Commons::NullPointerException);
253 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
255 Throw(WrtDeviceApis::Commons::NullPointerException);
257 if (typeArray->size() <= index) {
258 typeArray->resize(index + 1);
260 (*typeArray)[index] = converter->toContactPhoneNumberType(str);
263 Catch(WrtDeviceApis::Commons::Exception)
265 LoggerE("error occured");
266 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
271 bool JSContactPhoneNumberTypeArray::deleteProperty(JSContextRef context,
273 JSStringRef propertyName,
274 JSValueRef* exception)
276 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
279 size_t index = converter->toSizeT(propertyName);
280 JSContactPhoneNumberTypeArrayPriv* priv =
281 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
283 Throw(WrtDeviceApis::Commons::NullPointerException);
285 ContactPhoneNumberTypeArrayPtr typeArray = priv->getObject();
287 Throw(WrtDeviceApis::Commons::NullPointerException);
289 if (typeArray->size() > index) {
290 (*typeArray)[index] = converter->toContactPhoneNumberType("");
294 Catch(WrtDeviceApis::Commons::Exception)
296 LoggerE("error occured");
297 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
302 void JSContactPhoneNumberTypeArray::getPropertyNames(JSContextRef context,
304 JSPropertyNameAccumulatorRef propertyNames)
306 ContactConverterFactory::ConverterType converter =
307 ContactConverterFactory::getConverter(context);
310 JSContactPhoneNumberTypeArrayPriv* priv =
311 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(object));
313 Throw(WrtDeviceApis::Commons::NullPointerException);
315 ContactPhoneNumberTypeArrayPtr phoneNumbers = priv->getObject();
317 int count = phoneNumbers->size();
319 for(int i=0; i < count; i++)
321 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
322 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
325 Catch(WrtDeviceApis::Commons::Exception)
327 LoggerE("invalid property");
331 JSValueRef JSContactPhoneNumberTypeArray::concat(JSContextRef context,
332 JSObjectRef function,
333 JSObjectRef thisObject,
334 size_t argumentCount,
335 const JSValueRef arguments[],
336 JSValueRef* exception)
340 ContactPhoneNumberTypeArrayPtr typeArray = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
341 JSContactPhoneNumberTypeArrayPriv *newPrivateObject = new JSContactPhoneNumberTypeArrayPriv(
344 JSValueRef result = JSObjectMake(context,
345 getClassRef(), newPrivateObject);
347 //copy current typeArray
348 JSContactPhoneNumberTypeArrayPriv* priv =
349 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
350 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
351 for (size_t i = 0; i < currentStrings->size(); ++i) {
352 typeArray->push_back(currentStrings->at(i));
355 //copy submitted arrays
356 ContactConverterFactory::ConverterType converter =
357 ContactConverterFactory::getConverter(context);
358 for (size_t i = 0; i < argumentCount; ++i) {
359 if (!JSIsArrayValue(context, arguments[i])) {
360 Throw(WrtDeviceApis::Commons::ConversionException);
362 // process array of strings
363 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
364 unsigned int len = JSGetArrayLength(context, arrayObj);
365 for (unsigned int e = 0; e < len; ++e) {
366 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
367 typeArray->push_back(converter->toContactPhoneNumberType(att));
372 Catch(WrtDeviceApis::Commons::Exception)
374 LoggerE("error occured");
376 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
379 JSValueRef JSContactPhoneNumberTypeArray::join(JSContextRef context,
380 JSObjectRef function,
381 JSObjectRef thisObject,
382 size_t argumentCount,
383 const JSValueRef arguments[],
384 JSValueRef* exception)
389 std::string separator(",");
390 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
391 JSContactPhoneNumberTypeArrayPriv* priv =
392 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
393 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
394 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
395 separator = converter->toString(arguments[0]);
397 for (size_t i = 0; i < currentStrings->size(); ++i) {
401 result += currentStrings->at(i);
403 return converter->toJSValueRef(result);
405 Catch(WrtDeviceApis::Commons::Exception)
407 LoggerE("error occured");
409 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
412 JSValueRef JSContactPhoneNumberTypeArray::pop(JSContextRef context,
413 JSObjectRef function,
414 JSObjectRef thisObject,
415 size_t argumentCount,
416 const JSValueRef arguments[],
417 JSValueRef* exception)
421 ContactConverterFactory::ConverterType converter =
422 ContactConverterFactory::getConverter(context);
423 JSContactPhoneNumberTypeArrayPriv* priv =
424 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
425 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
426 if (currentStrings->size() > 0) {
428 converter->toContactPhoneNumberTypeStr(currentStrings->at(currentStrings->size() - 1));
429 currentStrings->pop_back();
430 return converter->toJSValueRef(result);
433 Catch(WrtDeviceApis::Commons::Exception)
435 LoggerE("error occured");
437 return JSValueMakeUndefined(context);
440 JSValueRef JSContactPhoneNumberTypeArray::push(JSContextRef context,
441 JSObjectRef function,
442 JSObjectRef thisObject,
443 size_t argumentCount,
444 const JSValueRef arguments[],
445 JSValueRef* exception)
449 ContactConverterFactory::ConverterType converter =
450 ContactConverterFactory::getConverter(context);
451 JSContactPhoneNumberTypeArrayPriv* priv =
452 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
453 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
454 for (size_t i = 0; i < argumentCount; ++i) {
455 currentStrings->push_back(converter->toContactPhoneNumberType(arguments[i]));
457 return converter->toJSValueRef(currentStrings->size());
459 Catch(WrtDeviceApis::Commons::Exception)
461 LoggerE("error occured");
463 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
466 JSValueRef JSContactPhoneNumberTypeArray::reverse(JSContextRef context,
467 JSObjectRef function,
468 JSObjectRef thisObject,
469 size_t argumentCount,
470 const JSValueRef arguments[],
471 JSValueRef* exception)
475 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
476 JSContactPhoneNumberTypeArrayPriv* priv =
477 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
478 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
479 std::reverse(currentStrings->begin(), currentStrings->end());
482 Catch(WrtDeviceApis::Commons::Exception)
484 LoggerE("error occured");
486 return JSValueMakeUndefined(context);
489 JSValueRef JSContactPhoneNumberTypeArray::shift(JSContextRef context,
490 JSObjectRef function,
491 JSObjectRef thisObject,
492 size_t argumentCount,
493 const JSValueRef arguments[],
494 JSValueRef* exception)
498 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
499 JSContactPhoneNumberTypeArrayPriv* priv =
500 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
501 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
502 if (currentStrings->size() > 0) {
503 std::string result = converter->toContactPhoneNumberTypeStr(currentStrings->at(0));
504 currentStrings->erase(currentStrings->begin());
505 return converter->toJSValueRef(result);
508 Catch(WrtDeviceApis::Commons::Exception)
510 LoggerE("error occured");
512 return JSValueMakeUndefined(context);
515 JSValueRef JSContactPhoneNumberTypeArray::slice(JSContextRef context,
516 JSObjectRef function,
517 JSObjectRef thisObject,
518 size_t argumentCount,
519 const JSValueRef arguments[],
520 JSValueRef* exception)
524 if (argumentCount < 1) {
525 return JSValueMakeUndefined(context);
527 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
528 ContactPhoneNumberTypeArrayPtr typeArray = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
529 JSContactPhoneNumberTypeArrayPriv *newPrivateObject = new JSContactPhoneNumberTypeArrayPriv(
532 JSValueRef result = JSObjectMake(context,
533 getClassRef(), newPrivateObject);
535 //copy current typeArray
536 JSContactPhoneNumberTypeArrayPriv* priv =
537 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
538 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
539 std::size_t first = converter->toSizeT(arguments[0]);
540 std::size_t last = currentStrings->size() - 1;
541 if (argumentCount > 1) {
542 last = converter->toSizeT(arguments[1]);
543 if (last >= currentStrings->size()) {
544 last = currentStrings->size() - 1;
547 for (size_t i = first; i <= last; ++i) {
548 typeArray->push_back(currentStrings->at(i));
553 Catch(WrtDeviceApis::Commons::Exception)
555 LoggerE("error occured");
557 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
560 JSValueRef JSContactPhoneNumberTypeArray::sort(JSContextRef context,
561 JSObjectRef function,
562 JSObjectRef thisObject,
563 size_t argumentCount,
564 const JSValueRef arguments[],
565 JSValueRef* exception)
569 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
570 JSContactPhoneNumberTypeArrayPriv* priv =
571 static_cast<JSContactPhoneNumberTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
572 ContactPhoneNumberTypeArrayPtr currentStrings = priv->getObject();
573 std::sort(currentStrings->begin(), currentStrings->end());
576 Catch(WrtDeviceApis::Commons::Exception)
578 LoggerE("error occured");
580 return JSValueMakeUndefined(context);
583 JSValueRef JSContactPhoneNumberTypeArray::splice(JSContextRef context,
584 JSObjectRef function,
585 JSObjectRef thisObject,
586 size_t argumentCount,
587 const JSValueRef arguments[],
588 JSValueRef* exception)
590 return JSValueMakeUndefined(context);
593 JSValueRef JSContactPhoneNumberTypeArray::toString(JSContextRef context,
594 JSObjectRef function,
595 JSObjectRef thisObject,
596 size_t argumentCount,
597 const JSValueRef arguments[],
598 JSValueRef* exception)
600 return join(context, function, thisObject, 0, arguments, exception);
603 JSValueRef JSContactPhoneNumberTypeArray::unshift(JSContextRef context,
604 JSObjectRef function,
605 JSObjectRef thisObject,
606 size_t argumentCount,
607 const JSValueRef arguments[],
608 JSValueRef* exception)
610 return JSValueMakeUndefined(context);
613 JSValueRef JSContactPhoneNumberTypeArray::valueOf(JSContextRef context,
614 JSObjectRef function,
615 JSObjectRef thisObject,
616 size_t argumentCount,
617 const JSValueRef arguments[],
618 JSValueRef* exception)
620 return JSValueMakeUndefined(context);