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 #include "JSCategoryArray.h"
21 #include <Tizen/Common/JSTizenException.h>
22 #include <Tizen/Common/JSTizenExceptionFactory.h>
23 #include <CommonsJavaScript/Converter.h>
25 using namespace TizenApis::Api::Calendar;
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
29 #define FUNCTION_CONCAT "concat"
30 #define FUNCTION_JOIN "join"
31 #define FUNCTION_POP "pop"
32 #define FUNCTION_PUSH "push"
33 #define FUNCTION_REVERSE "reverse"
34 #define FUNCTION_SHIFT "shift"
35 #define FUNCTION_SLICE "slice"
36 #define FUNCTION_SORT "sort"
37 #define FUNCTION_SPLICE "splice"
38 #define FUNCTION_TOSTRING "toString"
39 #define FUNCTION_UNSHIFT "unshift"
40 #define FUNCTION_VALUEOF "valueOf"
42 #define ATTRIBUTE_LENGTH "length"
48 JSClassDefinition JSCategoryArray::m_classInfo = {
50 kJSClassAttributeNone,
60 NULL, //deleteProperty,
61 NULL, //getPropertyNames,
62 NULL, //callAsFunction,
63 NULL, //callAsConstructor,
65 NULL, //convertToType,
68 JSStaticValue JSCategoryArray::m_property[] = {
69 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
73 JSStaticFunction JSCategoryArray::m_function[] = {
74 { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
75 { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
76 { FUNCTION_POP, pop, kJSPropertyAttributeNone },
77 { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
78 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
79 { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
80 { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
81 { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
82 { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
83 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
84 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
85 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
89 JSClassRef JSCategoryArray::m_jsClassRef = JSClassCreate(
90 JSCategoryArray::getClassInfo());
92 JSValueRef JSCategoryArray::getLength(JSContextRef context,
94 JSStringRef propertyName,
95 JSValueRef* exception)
100 JSCategoryArrayPrivate* priv =
101 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(object));
103 Throw(NullPointerException);
105 CategoryListPtr categories = priv->getObject();
107 Converter converter(context);
108 return converter.toJSValueRef(categories->size());
113 LogError("invalid conversion");
115 return JSValueMakeUndefined(context);
118 CategoryListPtr JSCategoryArray::getCategories(JSContextRef context,
122 JSCategoryArrayPrivate* priv =
123 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(object));
125 Throw(NullPointerException);
127 return priv->getObject();
130 JSObjectRef JSCategoryArray::createArray(JSContextRef context,
131 const CategoryListPtr &categories)
133 JSCategoryArrayPrivate *priv = new JSCategoryArrayPrivate(context,
135 return JSObjectMake(context, getClassRef(), priv);
138 const JSClassDefinition* JSCategoryArray::getClassInfo()
140 return &(m_classInfo);
143 JSClassRef JSCategoryArray::getClassRef()
146 m_jsClassRef = JSClassCreate(&m_classInfo);
151 void JSCategoryArray::initialize(JSContextRef context,
157 void JSCategoryArray::finalize(JSObjectRef object)
160 JSCategoryArrayPrivate* priv =
161 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(object));
163 JSObjectSetPrivate(object, NULL);
166 bool JSCategoryArray::hasProperty(JSContextRef context,
168 JSStringRef propertyName)
171 Converter converter(context);
174 size_t index = converter.toSizeT(propertyName);
175 JSCategoryArrayPrivate* priv =
176 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(object));
178 Throw(NullPointerException);
180 CategoryListPtr categories = priv->getObject();
181 if (index < categories->size()) {
187 //not reporting error is intended
192 JSValueRef JSCategoryArray::getProperty(JSContextRef context,
194 JSStringRef propertyName,
195 JSValueRef* exception)
198 Converter converter(context);
201 size_t index = converter.toSizeT(propertyName);
202 JSCategoryArrayPrivate* priv =
203 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(object));
205 Throw(NullPointerException);
207 CategoryListPtr categories = priv->getObject();
208 if (index < categories->size()) {
209 std::string result = categories->at(index);
210 if (!result.empty()) {
211 return converter.toJSValueRef(result);
217 LogError("invalid property");
219 return JSValueMakeUndefined(context);
222 bool JSCategoryArray::setProperty(JSContextRef context,
224 JSStringRef propertyName,
226 JSValueRef* exception)
229 Converter converter(context);
232 size_t index = converter.toSizeT(propertyName);
233 std::string category;
234 if (!JSValueIsUndefined(context, value)) {
235 category = converter.toString(value);
237 JSCategoryArrayPrivate* priv =
238 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(object));
240 Throw(NullPointerException);
242 CategoryListPtr categories = priv->getObject();
244 Throw(NullPointerException);
246 if (categories->size() <= index) {
247 categories->resize(index + 1);
249 (*categories)[index] = category;
254 LogError("error occured");
255 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
260 JSValueRef JSCategoryArray::concat(JSContextRef context,
261 JSObjectRef function,
262 JSObjectRef thisObject,
263 size_t argumentCount,
264 const JSValueRef arguments[],
265 JSValueRef* exception)
270 CategoryListPtr categories = CategoryListPtr(new CategoryList());
271 JSCategoryArrayPrivate *newPrivateObject = new JSCategoryArrayPrivate(
274 JSValueRef result = JSObjectMake(context,
275 getClassRef(), newPrivateObject);
277 //copy current categories
278 JSCategoryArrayPrivate* priv =
279 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
280 CategoryListPtr currentCategories = priv->getObject();
281 for (size_t i = 0; i < currentCategories->size(); ++i) {
282 categories->push_back(currentCategories->at(i));
285 //copy submitted arrays
286 Converter converter(context);
287 for (size_t i = 0; i < argumentCount; ++i) {
288 if (!JSIsArrayValue(context, arguments[i])) {
289 Throw(ConversionException);
291 // process array of strings
292 JSObjectRef arrayObj = converter.toJSObjectRef(arguments[i]);
293 unsigned int len = JSGetArrayLength(context, arrayObj);
294 for (unsigned int e = 0; e < len; ++e) {
295 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
296 categories->push_back(converter.toString(att));
303 LogError("error occured");
305 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
308 JSValueRef JSCategoryArray::join(JSContextRef context,
309 JSObjectRef function,
310 JSObjectRef thisObject,
311 size_t argumentCount,
312 const JSValueRef arguments[],
313 JSValueRef* exception)
319 std::string separator(",");
320 Converter converter(context);
321 JSCategoryArrayPrivate* priv =
322 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
323 CategoryListPtr currentCategories = priv->getObject();
324 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
325 separator = converter.toString(arguments[0]);
327 for (size_t i = 0; i < currentCategories->size(); ++i) {
331 result += currentCategories->at(i);
333 return converter.toJSValueRef(result);
337 LogError("error occured");
339 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
342 JSValueRef JSCategoryArray::pop(JSContextRef context,
343 JSObjectRef function,
344 JSObjectRef thisObject,
345 size_t argumentCount,
346 const JSValueRef arguments[],
347 JSValueRef* exception)
352 Converter converter(context);
353 JSCategoryArrayPrivate* priv =
354 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
355 CategoryListPtr currentCategories = priv->getObject();
356 if (currentCategories->size() > 0) {
357 std::string result = currentCategories->at(
358 currentCategories->size() - 1);
359 currentCategories->pop_back();
360 return converter.toJSValueRef(result);
365 LogError("error occured");
367 return JSValueMakeUndefined(context);
370 JSValueRef JSCategoryArray::push(JSContextRef context,
371 JSObjectRef function,
372 JSObjectRef thisObject,
373 size_t argumentCount,
374 const JSValueRef arguments[],
375 JSValueRef* exception)
380 Converter converter(context);
381 JSCategoryArrayPrivate* priv =
382 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
383 CategoryListPtr currentCategories = priv->getObject();
384 for (size_t i = 0; i < argumentCount; ++i) {
385 currentCategories->push_back(converter.toString(arguments[i]));
387 return converter.toJSValueRef(currentCategories->size());
391 LogError("error occured");
393 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
396 JSValueRef JSCategoryArray::reverse(JSContextRef context,
397 JSObjectRef function,
398 JSObjectRef thisObject,
399 size_t argumentCount,
400 const JSValueRef arguments[],
401 JSValueRef* exception)
406 Converter converter(context);
407 JSCategoryArrayPrivate* priv =
408 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
409 CategoryListPtr currentCategories = priv->getObject();
410 std::reverse(currentCategories->begin(), currentCategories->end());
415 LogError("error occured");
417 return JSValueMakeUndefined(context);
420 JSValueRef JSCategoryArray::shift(JSContextRef context,
421 JSObjectRef function,
422 JSObjectRef thisObject,
423 size_t argumentCount,
424 const JSValueRef arguments[],
425 JSValueRef* exception)
430 Converter converter(context);
431 JSCategoryArrayPrivate* priv =
432 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
433 CategoryListPtr currentCategories = priv->getObject();
434 if (currentCategories->size() > 0) {
435 std::string result = currentCategories->at(0);
436 currentCategories->erase(currentCategories->begin());
437 return converter.toJSValueRef(result);
442 LogError("error occured");
444 return JSValueMakeUndefined(context);
447 JSValueRef JSCategoryArray::slice(JSContextRef context,
448 JSObjectRef function,
449 JSObjectRef thisObject,
450 size_t argumentCount,
451 const JSValueRef arguments[],
452 JSValueRef* exception)
457 if (argumentCount < 1) {
458 return JSValueMakeUndefined(context);
460 Converter converter(context);
461 CategoryListPtr categories = CategoryListPtr(new CategoryList());
462 JSCategoryArrayPrivate *newPrivateObject = new JSCategoryArrayPrivate(
465 JSValueRef result = JSObjectMake(context,
466 getClassRef(), newPrivateObject);
468 //copy current categories
469 JSCategoryArrayPrivate* priv =
470 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
471 CategoryListPtr currentCategories = priv->getObject();
472 std::size_t first = converter.toSizeT(arguments[0]);
473 std::size_t last = currentCategories->size() - 1;
474 if (argumentCount > 1) {
475 last = converter.toSizeT(arguments[1]);
476 if (last >= currentCategories->size()) {
477 last = currentCategories->size() - 1;
483 for (size_t i = first; i <= last; ++i) {
484 categories->push_back(currentCategories->at(i));
491 LogError("error occured");
493 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
496 JSValueRef JSCategoryArray::sort(JSContextRef context,
497 JSObjectRef function,
498 JSObjectRef thisObject,
499 size_t argumentCount,
500 const JSValueRef arguments[],
501 JSValueRef* exception)
506 Converter converter(context);
507 JSCategoryArrayPrivate* priv =
508 static_cast<JSCategoryArrayPrivate*>(JSObjectGetPrivate(thisObject));
509 CategoryListPtr currentCategories = priv->getObject();
510 std::sort(currentCategories->begin(), currentCategories->end());
515 LogError("error occured");
517 return JSValueMakeUndefined(context);
520 JSValueRef JSCategoryArray::splice(JSContextRef context,
521 JSObjectRef function,
522 JSObjectRef thisObject,
523 size_t argumentCount,
524 const JSValueRef arguments[],
525 JSValueRef* exception)
528 return JSValueMakeUndefined(context);
531 JSValueRef JSCategoryArray::toString(JSContextRef context,
532 JSObjectRef function,
533 JSObjectRef thisObject,
534 size_t argumentCount,
535 const JSValueRef arguments[],
536 JSValueRef* exception)
539 return join(context, function, thisObject, 0, arguments, exception);
542 JSValueRef JSCategoryArray::unshift(JSContextRef context,
543 JSObjectRef function,
544 JSObjectRef thisObject,
545 size_t argumentCount,
546 const JSValueRef arguments[],
547 JSValueRef* exception)
550 return JSValueMakeUndefined(context);
553 JSValueRef JSCategoryArray::valueOf(JSContextRef context,
554 JSObjectRef function,
555 JSObjectRef thisObject,
556 size_t argumentCount,
557 const JSValueRef arguments[],
558 JSValueRef* exception)
561 return JSValueMakeUndefined(context);