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 JSContactAddressTypeArray.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 "JSContactAddressTypeArray.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 JSContactAddressTypeArray::m_classInfo = {
57 kJSClassAttributeNone,
69 NULL, //callAsFunction,
70 NULL, //callAsConstructor,
72 NULL, //convertToType,
75 JSStaticValue JSContactAddressTypeArray::m_property[] = {
76 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
80 JSStaticFunction JSContactAddressTypeArray::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 JSContactAddressTypeArray::m_jsClassRef = JSClassCreate(
97 JSContactAddressTypeArray::getClassInfo());
99 JSValueRef JSContactAddressTypeArray::getLength(JSContextRef context,
101 JSStringRef propertyName,
102 JSValueRef* exception)
106 JSContactAddressTypeArrayPriv* priv =
107 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
109 Throw(WrtDeviceApis::Commons::NullPointerException);
111 ContactAddressTypeArrayPtr 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 JSContactAddressTypeArray::createArray(JSContextRef context,
126 const ContactAddressTypeArrayPtr &typeArray)
128 JSContactAddressTypeArrayPriv *priv = new JSContactAddressTypeArrayPriv(context, typeArray);
129 return JSObjectMake(context, getClassRef(), priv);
132 const JSClassDefinition* JSContactAddressTypeArray::getClassInfo()
134 return &(m_classInfo);
137 JSClassRef JSContactAddressTypeArray::getClassRef()
140 m_jsClassRef = JSClassCreate(&m_classInfo);
145 bool JSContactAddressTypeArray::isObjectOfClass(JSContextRef context, JSValueRef value)
147 return JSValueIsObjectOfClass(context, value, getClassRef());
150 ContactAddressTypeArrayPtr JSContactAddressTypeArray::getContactAddressTypeArray(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 JSContactAddressTypeArrayPriv *priv = static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
161 Throw(WrtDeviceApis::Commons::NullPointerException);
163 return priv->getObject();
166 void JSContactAddressTypeArray::initialize(JSContextRef context,
171 void JSContactAddressTypeArray::finalize(JSObjectRef object)
173 JSContactAddressTypeArrayPriv* priv =
174 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
176 JSObjectSetPrivate(object, NULL);
179 bool JSContactAddressTypeArray::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 JSContactAddressTypeArrayPriv* priv =
189 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
191 Throw(WrtDeviceApis::Commons::NullPointerException);
193 ContactAddressTypeArrayPtr typeArray = priv->getObject();
194 if (index < typeArray->size()) {
198 Catch(WrtDeviceApis::Commons::Exception)
200 //not reporting error is intended
205 JSValueRef JSContactAddressTypeArray::getProperty(JSContextRef context,
207 JSStringRef propertyName,
208 JSValueRef* exception)
210 ContactConverterFactory::ConverterType converter =
211 ContactConverterFactory::getConverter(context);
214 size_t index = converter->toSizeT(propertyName);
215 JSContactAddressTypeArrayPriv* priv =
216 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
218 Throw(WrtDeviceApis::Commons::NullPointerException);
220 ContactAddressTypeArrayPtr typeArray = priv->getObject();
221 if (index < typeArray->size()) {
222 std::string result = converter->toContactAddressTypeStr(typeArray->at(index));
223 if (!result.empty()) {
224 return converter->toJSValueRef(result);
228 Catch(WrtDeviceApis::Commons::Exception)
230 LoggerE("invalid property");
232 return JSValueMakeUndefined(context);
235 bool JSContactAddressTypeArray::setProperty(JSContextRef context,
237 JSStringRef propertyName,
239 JSValueRef* exception)
241 ContactConverterFactory::ConverterType converter =
242 ContactConverterFactory::getConverter(context);
245 size_t index = converter->toSizeT(propertyName);
247 if (!JSValueIsUndefined(context, value)) {
248 str = converter->toString(value);
250 JSContactAddressTypeArrayPriv* priv =
251 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
253 Throw(WrtDeviceApis::Commons::NullPointerException);
255 ContactAddressTypeArrayPtr typeArray = priv->getObject();
257 Throw(WrtDeviceApis::Commons::NullPointerException);
259 if (typeArray->size() <= index) {
260 typeArray->resize(index + 1);
262 (*typeArray)[index] = converter->toContactAddressType(str);
265 Catch(WrtDeviceApis::Commons::Exception)
267 LoggerE("error occured");
268 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
273 bool JSContactAddressTypeArray::deleteProperty(JSContextRef context,
275 JSStringRef propertyName,
276 JSValueRef* exception)
278 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
281 size_t index = converter->toSizeT(propertyName);
282 JSContactAddressTypeArrayPriv* priv =
283 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
285 Throw(WrtDeviceApis::Commons::NullPointerException);
287 ContactAddressTypeArrayPtr typeArray = priv->getObject();
289 Throw(WrtDeviceApis::Commons::NullPointerException);
291 if (typeArray->size() > index) {
292 (*typeArray)[index] = converter->toContactAddressType("");
296 Catch(WrtDeviceApis::Commons::Exception)
298 LoggerE("error occured");
299 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
304 void JSContactAddressTypeArray::getPropertyNames(JSContextRef context,
306 JSPropertyNameAccumulatorRef propertyNames)
308 ContactConverterFactory::ConverterType converter =
309 ContactConverterFactory::getConverter(context);
312 JSContactAddressTypeArrayPriv* priv =
313 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(object));
315 Throw(WrtDeviceApis::Commons::NullPointerException);
317 ContactAddressTypeArrayPtr addressTypes = priv->getObject();
319 int count = addressTypes->size();
321 for(int i=0; i < count; i++)
323 ScopedJSStringRef name(converter->toJSStringRef(converter->toString(i)));
324 JSPropertyNameAccumulatorAddName(propertyNames, name.get());
327 Catch(WrtDeviceApis::Commons::Exception)
329 LoggerE("invalid property");
333 JSValueRef JSContactAddressTypeArray::concat(JSContextRef context,
334 JSObjectRef function,
335 JSObjectRef thisObject,
336 size_t argumentCount,
337 const JSValueRef arguments[],
338 JSValueRef* exception)
342 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
343 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
346 JSValueRef result = JSObjectMake(context,
347 getClassRef(), newPrivateObject);
349 //copy current typeArray
350 JSContactAddressTypeArrayPriv* priv =
351 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
352 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
353 for (size_t i = 0; i < currentTypes->size(); ++i) {
354 typeArray->push_back(currentTypes->at(i));
357 //copy submitted arrays
358 ContactConverterFactory::ConverterType converter =
359 ContactConverterFactory::getConverter(context);
360 for (size_t i = 0; i < argumentCount; ++i) {
361 if (!JSIsArrayValue(context, arguments[i])) {
362 Throw(WrtDeviceApis::Commons::ConversionException);
364 // process array of types
365 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
366 unsigned int len = JSGetArrayLength(context, arrayObj);
367 for (unsigned int e = 0; e < len; ++e) {
368 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
369 typeArray->push_back(converter->toContactAddressType(att));
374 Catch(WrtDeviceApis::Commons::Exception)
376 LoggerE("error occured");
378 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
381 JSValueRef JSContactAddressTypeArray::join(JSContextRef context,
382 JSObjectRef function,
383 JSObjectRef thisObject,
384 size_t argumentCount,
385 const JSValueRef arguments[],
386 JSValueRef* exception)
391 std::string separator(",");
392 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
393 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
394 JSContactAddressTypeArrayPriv* priv =
395 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
396 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
397 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
398 separator = converter->toString(arguments[0]);
400 for (size_t i = 0; i < currentTypes->size(); ++i) {
404 result += currentTypes->at(i);
406 return converter->toJSValueRef(result);
408 Catch(WrtDeviceApis::Commons::Exception)
410 LoggerE("error occured");
412 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
415 JSValueRef JSContactAddressTypeArray::pop(JSContextRef context,
416 JSObjectRef function,
417 JSObjectRef thisObject,
418 size_t argumentCount,
419 const JSValueRef arguments[],
420 JSValueRef* exception)
424 ContactConverterFactory::ConverterType converter =
425 ContactConverterFactory::getConverter(context);
426 JSContactAddressTypeArrayPriv* priv =
427 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
428 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
429 if (currentTypes->size() > 0) {
430 std::string result = converter->toContactAddressTypeStr(currentTypes->at(currentTypes->size()-1));
431 currentTypes->pop_back();
432 return converter->toJSValueRef(result);
435 Catch(WrtDeviceApis::Commons::Exception)
437 LoggerE("error occured");
439 return JSValueMakeUndefined(context);
442 JSValueRef JSContactAddressTypeArray::push(JSContextRef context,
443 JSObjectRef function,
444 JSObjectRef thisObject,
445 size_t argumentCount,
446 const JSValueRef arguments[],
447 JSValueRef* exception)
451 ContactConverterFactory::ConverterType converter =
452 ContactConverterFactory::getConverter(context);
453 JSContactAddressTypeArrayPriv* priv =
454 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
455 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
456 for (size_t i = 0; i < argumentCount; ++i) {
457 currentTypes->push_back(converter->toContactAddressType(arguments[i]));
459 return converter->toJSValueRef(currentTypes->size());
461 Catch(WrtDeviceApis::Commons::Exception)
463 LoggerE("error occured");
465 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
468 JSValueRef JSContactAddressTypeArray::reverse(JSContextRef context,
469 JSObjectRef function,
470 JSObjectRef thisObject,
471 size_t argumentCount,
472 const JSValueRef arguments[],
473 JSValueRef* exception)
477 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
478 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
479 JSContactAddressTypeArrayPriv* priv =
480 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
481 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
482 std::reverse(currentTypes->begin(), currentTypes->end());
485 Catch(WrtDeviceApis::Commons::Exception)
487 LoggerE("error occured");
489 return JSValueMakeUndefined(context);
492 JSValueRef JSContactAddressTypeArray::shift(JSContextRef context,
493 JSObjectRef function,
494 JSObjectRef thisObject,
495 size_t argumentCount,
496 const JSValueRef arguments[],
497 JSValueRef* exception)
501 ContactConverterFactory::ConverterType converter =
502 ContactConverterFactory::getConverter(context);
503 JSContactAddressTypeArrayPriv* priv =
504 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
505 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
506 if (currentTypes->size() > 0) {
507 std::string result = converter->toContactAddressTypeStr(currentTypes->at(0));
508 currentTypes->erase(currentTypes->begin());
509 return converter->toJSValueRef(result);
512 Catch(WrtDeviceApis::Commons::Exception)
514 LoggerE("error occured");
516 return JSValueMakeUndefined(context);
519 JSValueRef JSContactAddressTypeArray::slice(JSContextRef context,
520 JSObjectRef function,
521 JSObjectRef thisObject,
522 size_t argumentCount,
523 const JSValueRef arguments[],
524 JSValueRef* exception)
528 if (argumentCount < 1) {
529 return JSValueMakeUndefined(context);
531 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
532 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
533 ContactAddressTypeArrayPtr typeArray = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
534 JSContactAddressTypeArrayPriv *newPrivateObject = new JSContactAddressTypeArrayPriv(
537 JSValueRef result = JSObjectMake(context,
538 getClassRef(), newPrivateObject);
540 //copy current typeArray
541 JSContactAddressTypeArrayPriv* priv =
542 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
543 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
544 std::size_t first = converter->toSizeT(arguments[0]);
545 std::size_t last = currentTypes->size() - 1;
546 if (argumentCount > 1) {
547 last = converter->toSizeT(arguments[1]);
548 if (last >= currentTypes->size()) {
549 last = currentTypes->size() - 1;
552 for (size_t i = first; i <= last; ++i) {
553 typeArray->push_back(currentTypes->at(i));
558 Catch(WrtDeviceApis::Commons::Exception)
560 LoggerE("error occured");
562 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
565 JSValueRef JSContactAddressTypeArray::sort(JSContextRef context,
566 JSObjectRef function,
567 JSObjectRef thisObject,
568 size_t argumentCount,
569 const JSValueRef arguments[],
570 JSValueRef* exception)
574 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
575 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
576 JSContactAddressTypeArrayPriv* priv =
577 static_cast<JSContactAddressTypeArrayPriv*>(JSObjectGetPrivate(thisObject));
578 ContactAddressTypeArrayPtr currentTypes = priv->getObject();
579 std::sort(currentTypes->begin(), currentTypes->end());
582 Catch(WrtDeviceApis::Commons::Exception)
584 LoggerE("error occured");
586 return JSValueMakeUndefined(context);
589 JSValueRef JSContactAddressTypeArray::splice(JSContextRef context,
590 JSObjectRef function,
591 JSObjectRef thisObject,
592 size_t argumentCount,
593 const JSValueRef arguments[],
594 JSValueRef* exception)
596 return JSValueMakeUndefined(context);
599 JSValueRef JSContactAddressTypeArray::toString(JSContextRef context,
600 JSObjectRef function,
601 JSObjectRef thisObject,
602 size_t argumentCount,
603 const JSValueRef arguments[],
604 JSValueRef* exception)
606 return join(context, function, thisObject, 0, arguments, exception);
609 JSValueRef JSContactAddressTypeArray::unshift(JSContextRef context,
610 JSObjectRef function,
611 JSObjectRef thisObject,
612 size_t argumentCount,
613 const JSValueRef arguments[],
614 JSValueRef* exception)
616 return JSValueMakeUndefined(context);
619 JSValueRef JSContactAddressTypeArray::valueOf(JSContextRef context,
620 JSObjectRef function,
621 JSObjectRef thisObject,
622 size_t argumentCount,
623 const JSValueRef arguments[],
624 JSValueRef* exception)
626 return JSValueMakeUndefined(context);