2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file JSAttributesOfInterestArray.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSAttributesOfInterestArray.h"
32 #define FUNCTION_CONCAT "concat"
33 #define FUNCTION_JOIN "join"
34 #define FUNCTION_POP "pop"
35 #define FUNCTION_PUSH "push"
36 #define FUNCTION_REVERSE "reverse"
37 #define FUNCTION_SHIFT "shift"
38 #define FUNCTION_SLICE "slice"
39 #define FUNCTION_SORT "sort"
40 #define FUNCTION_SPLICE "splice"
41 #define FUNCTION_TOSTRING "toString"
42 #define FUNCTION_UNSHIFT "unshift"
43 #define FUNCTION_VALUEOF "valueOf"
45 #define ATTRIBUTE_LENGTH "length"
51 using namespace TizenApis::Commons;
52 using namespace TizenApis::Api::Contact;
54 JSClassDefinition JSAttributesOfInterestArray::m_classInfo = {
56 kJSClassAttributeNone,
66 NULL, //deleteProperty,
67 NULL, //getPropertyNames,
68 NULL, //callAsFunction,
69 NULL, //callAsConstructor,
71 NULL, //convertToType,
74 JSStaticValue JSAttributesOfInterestArray::m_property[] = {
75 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
79 JSStaticFunction JSAttributesOfInterestArray::m_function[] = {
80 { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
81 { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
82 { FUNCTION_POP, pop, kJSPropertyAttributeNone },
83 { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
84 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
85 { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
86 { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
87 { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
88 { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
89 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
90 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
91 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
95 JSClassRef JSAttributesOfInterestArray::m_jsClassRef = JSClassCreate(
96 JSAttributesOfInterestArray::getClassInfo());
98 JSValueRef JSAttributesOfInterestArray::getLength(JSContextRef context,
100 JSStringRef propertyName,
101 JSValueRef* exception)
106 JSAttributesOfInterestArrayPriv* priv =
107 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
109 Throw(WrtDeviceApis::Commons::NullPointerException);
111 AttributesOfInterestArrayPtr 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 LogError("invalid conversion");
122 return JSValueMakeUndefined(context);
125 JSObjectRef JSAttributesOfInterestArray::createArray(JSContextRef context,
126 const AttributesOfInterestArrayPtr &typeArray)
128 JSAttributesOfInterestArrayPriv *priv = new JSAttributesOfInterestArrayPriv(context, typeArray);
129 return JSObjectMake(context, getClassRef(), priv);
132 const JSClassDefinition* JSAttributesOfInterestArray::getClassInfo()
134 return &(m_classInfo);
137 JSClassRef JSAttributesOfInterestArray::getClassRef()
140 m_jsClassRef = JSClassCreate(&m_classInfo);
145 bool JSAttributesOfInterestArray::isObjectOfClass(JSContextRef context, JSValueRef value)
147 return JSValueIsObjectOfClass(context, value, getClassRef());
150 AttributesOfInterestArrayPtr JSAttributesOfInterestArray::getAttributesOfInterestArray(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 JSAttributesOfInterestArrayPriv *priv = static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
161 Throw(WrtDeviceApis::Commons::NullPointerException);
163 return priv->getObject();
166 void JSAttributesOfInterestArray::initialize(JSContextRef context,
172 void JSAttributesOfInterestArray::finalize(JSObjectRef object)
175 JSAttributesOfInterestArrayPriv* priv =
176 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
178 JSObjectSetPrivate(object, NULL);
181 bool JSAttributesOfInterestArray::hasProperty(JSContextRef context,
183 JSStringRef propertyName)
186 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
187 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
190 size_t index = converter->toSizeT(propertyName);
191 JSAttributesOfInterestArrayPriv* priv =
192 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
194 Throw(WrtDeviceApis::Commons::NullPointerException);
196 AttributesOfInterestArrayPtr typeArray = priv->getObject();
197 if (index < typeArray->size()) {
201 Catch(WrtDeviceApis::Commons::Exception)
203 //not reporting error is intended
208 JSValueRef JSAttributesOfInterestArray::getProperty(JSContextRef context,
210 JSStringRef propertyName,
211 JSValueRef* exception)
214 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
215 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
218 size_t index = converter->toSizeT(propertyName);
219 JSAttributesOfInterestArrayPriv* priv =
220 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
222 Throw(WrtDeviceApis::Commons::NullPointerException);
224 AttributesOfInterestArrayPtr typeArray = priv->getObject();
225 if (index < typeArray->size()) {
226 std::string result = typeArray->at(index);
227 if (!result.empty()) {
228 return converter->toJSValueRef(result);
232 Catch(WrtDeviceApis::Commons::Exception)
234 LogError("invalid property");
236 return JSValueMakeUndefined(context);
239 bool JSAttributesOfInterestArray::setProperty(JSContextRef context,
241 JSStringRef propertyName,
243 JSValueRef* exception)
246 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
247 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
250 size_t index = converter->toSizeT(propertyName);
252 if (!JSValueIsUndefined(context, value)) {
253 str = converter->toString(value);
255 JSAttributesOfInterestArrayPriv* priv =
256 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(object));
258 Throw(WrtDeviceApis::Commons::NullPointerException);
260 AttributesOfInterestArrayPtr typeArray = priv->getObject();
262 Throw(WrtDeviceApis::Commons::NullPointerException);
264 if (typeArray->size() <= index) {
265 typeArray->resize(index + 1);
267 (*typeArray)[index] = str;
270 Catch(WrtDeviceApis::Commons::Exception)
272 LogError("error occured");
273 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
278 JSValueRef JSAttributesOfInterestArray::concat(JSContextRef context,
279 JSObjectRef function,
280 JSObjectRef thisObject,
281 size_t argumentCount,
282 const JSValueRef arguments[],
283 JSValueRef* exception)
288 AttributesOfInterestArrayPtr typeArray =
289 AttributesOfInterestArrayPtr(new AttributesOfInterestArray());
290 JSAttributesOfInterestArrayPriv *newPrivateObject =
291 new JSAttributesOfInterestArrayPriv(context, typeArray);
292 JSValueRef result = JSObjectMake(context,
293 getClassRef(), newPrivateObject);
295 //copy current typeArray
296 JSAttributesOfInterestArrayPriv* priv =
297 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
298 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
299 for (size_t i = 0; i < currentStrings->size(); ++i) {
300 typeArray->push_back(currentStrings->at(i));
303 //copy submitted arrays
304 ContactConverterFactory::ConverterType converter =
305 ContactConverterFactory::getConverter(context);
306 for (size_t i = 0; i < argumentCount; ++i) {
307 if (!JSIsArrayValue(context, arguments[i])) {
308 Throw(WrtDeviceApis::Commons::ConversionException);
310 // process array of strings
311 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
312 unsigned int len = JSGetArrayLength(context, arrayObj);
313 for (unsigned int e = 0; e < len; ++e) {
314 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
315 typeArray->push_back(converter->toString(att));
320 Catch(WrtDeviceApis::Commons::Exception)
322 LogError("error occured");
324 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
327 JSValueRef JSAttributesOfInterestArray::join(JSContextRef context,
328 JSObjectRef function,
329 JSObjectRef thisObject,
330 size_t argumentCount,
331 const JSValueRef arguments[],
332 JSValueRef* exception)
334 //LogDebug("entered");
338 std::string separator(",");
339 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
340 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
341 JSAttributesOfInterestArrayPriv* priv =
342 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
343 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
344 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
345 separator = converter->toString(arguments[0]);
347 for (size_t i = 0; i < currentStrings->size(); ++i) {
351 result += currentStrings->at(i);
353 return converter->toJSValueRef(result);
355 Catch(WrtDeviceApis::Commons::Exception)
357 LogError("error occured");
359 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
362 JSValueRef JSAttributesOfInterestArray::pop(JSContextRef context,
363 JSObjectRef function,
364 JSObjectRef thisObject,
365 size_t argumentCount,
366 const JSValueRef arguments[],
367 JSValueRef* exception)
369 //LogDebug("entered");
372 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
373 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
374 JSAttributesOfInterestArrayPriv* priv =
375 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
376 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
377 if (currentStrings->size() > 0) {
378 std::string result = currentStrings->at(
379 currentStrings->size() - 1);
380 currentStrings->pop_back();
381 return converter->toJSValueRef(result);
384 Catch(WrtDeviceApis::Commons::Exception)
386 LogError("error occured");
388 return JSValueMakeUndefined(context);
391 JSValueRef JSAttributesOfInterestArray::push(JSContextRef context,
392 JSObjectRef function,
393 JSObjectRef thisObject,
394 size_t argumentCount,
395 const JSValueRef arguments[],
396 JSValueRef* exception)
398 //LogDebug("entered");
401 ContactConverterFactory::ConverterType converter =
402 ContactConverterFactory::getConverter(context);
403 JSAttributesOfInterestArrayPriv* priv =
404 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
405 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
406 for (size_t i = 0; i < argumentCount; ++i) {
407 currentStrings->push_back(converter->toString(arguments[i]));
409 return converter->toJSValueRef(currentStrings->size());
411 Catch(WrtDeviceApis::Commons::Exception)
413 LogError("error occured");
415 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
418 JSValueRef JSAttributesOfInterestArray::reverse(JSContextRef context,
419 JSObjectRef function,
420 JSObjectRef thisObject,
421 size_t argumentCount,
422 const JSValueRef arguments[],
423 JSValueRef* exception)
425 //LogDebug("entered");
428 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
429 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
430 JSAttributesOfInterestArrayPriv* priv =
431 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
432 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
433 std::reverse(currentStrings->begin(), currentStrings->end());
436 Catch(WrtDeviceApis::Commons::Exception)
438 LogError("error occured");
440 return JSValueMakeUndefined(context);
443 JSValueRef JSAttributesOfInterestArray::shift(JSContextRef context,
444 JSObjectRef function,
445 JSObjectRef thisObject,
446 size_t argumentCount,
447 const JSValueRef arguments[],
448 JSValueRef* exception)
450 //LogDebug("entered");
453 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
454 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
455 JSAttributesOfInterestArrayPriv* priv =
456 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
457 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
458 if (currentStrings->size() > 0) {
459 std::string result = currentStrings->at(0);
460 currentStrings->erase(currentStrings->begin());
461 return converter->toJSValueRef(result);
464 Catch(WrtDeviceApis::Commons::Exception)
466 LogError("error occured");
468 return JSValueMakeUndefined(context);
471 JSValueRef JSAttributesOfInterestArray::slice(JSContextRef context,
472 JSObjectRef function,
473 JSObjectRef thisObject,
474 size_t argumentCount,
475 const JSValueRef arguments[],
476 JSValueRef* exception)
481 if (argumentCount < 1) {
482 return JSValueMakeUndefined(context);
484 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
485 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
486 AttributesOfInterestArrayPtr typeArray = AttributesOfInterestArrayPtr(new StringArray());
487 JSAttributesOfInterestArrayPriv *newPrivateObject = new JSAttributesOfInterestArrayPriv(
490 JSValueRef result = JSObjectMake(context,
491 getClassRef(), newPrivateObject);
493 //copy current typeArray
494 JSAttributesOfInterestArrayPriv* priv =
495 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
496 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
497 std::size_t first = converter->toSizeT(arguments[0]);
498 std::size_t last = currentStrings->size() - 1;
499 if (argumentCount > 1) {
500 last = converter->toSizeT(arguments[1]);
501 if (last >= currentStrings->size()) {
502 last = currentStrings->size() - 1;
508 for (size_t i = first; i <= last; ++i) {
509 typeArray->push_back(currentStrings->at(i));
514 Catch(WrtDeviceApis::Commons::Exception)
516 LogError("error occured");
518 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
521 JSValueRef JSAttributesOfInterestArray::sort(JSContextRef context,
522 JSObjectRef function,
523 JSObjectRef thisObject,
524 size_t argumentCount,
525 const JSValueRef arguments[],
526 JSValueRef* exception)
528 //LogDebug("entered");
531 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
532 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
533 JSAttributesOfInterestArrayPriv* priv =
534 static_cast<JSAttributesOfInterestArrayPriv*>(JSObjectGetPrivate(thisObject));
535 AttributesOfInterestArrayPtr currentStrings = priv->getObject();
536 std::sort(currentStrings->begin(), currentStrings->end());
539 Catch(WrtDeviceApis::Commons::Exception)
541 LogError("error occured");
543 return JSValueMakeUndefined(context);
546 JSValueRef JSAttributesOfInterestArray::splice(JSContextRef context,
547 JSObjectRef function,
548 JSObjectRef thisObject,
549 size_t argumentCount,
550 const JSValueRef arguments[],
551 JSValueRef* exception)
553 //LogDebug("entered");
554 return JSValueMakeUndefined(context);
557 JSValueRef JSAttributesOfInterestArray::toString(JSContextRef context,
558 JSObjectRef function,
559 JSObjectRef thisObject,
560 size_t argumentCount,
561 const JSValueRef arguments[],
562 JSValueRef* exception)
564 //LogDebug("entered");
565 return join(context, function, thisObject, 0, arguments, exception);
568 JSValueRef JSAttributesOfInterestArray::unshift(JSContextRef context,
569 JSObjectRef function,
570 JSObjectRef thisObject,
571 size_t argumentCount,
572 const JSValueRef arguments[],
573 JSValueRef* exception)
575 //LogDebug("entered");
576 return JSValueMakeUndefined(context);
579 JSValueRef JSAttributesOfInterestArray::valueOf(JSContextRef context,
580 JSObjectRef function,
581 JSObjectRef thisObject,
582 size_t argumentCount,
583 const JSValueRef arguments[],
584 JSValueRef* exception)
586 //LogDebug("entered");
587 return JSValueMakeUndefined(context);