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 JSStringArray.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 "JSStringArray.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;
53 JSClassDefinition JSStringArray::m_classInfo = {
55 kJSClassAttributeNone,
65 NULL, //deleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //callAsFunction,
68 NULL, //callAsConstructor,
70 NULL, //convertToType,
73 JSStaticValue JSStringArray::m_property[] = {
74 { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
78 JSStaticFunction JSStringArray::m_function[] = {
79 { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
80 { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
81 { FUNCTION_POP, pop, kJSPropertyAttributeNone },
82 { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
83 { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
84 { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
85 { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
86 { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
87 { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
88 { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
89 { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
90 { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
94 JSClassRef JSStringArray::m_jsClassRef = JSClassCreate(
95 JSStringArray::getClassInfo());
97 JSValueRef JSStringArray::getLength(JSContextRef context,
99 JSStringRef propertyName,
100 JSValueRef* exception)
105 JSStringArrayPriv* priv =
106 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 StringArrayPtr strs = priv->getObject();
112 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
113 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
114 return converter->toJSValueRef(strs->size());
117 Catch(WrtDeviceApis::Commons::Exception)
119 LogError("invalid conversion");
121 return JSValueMakeUndefined(context);
124 JSObjectRef JSStringArray::createArray(JSContextRef context,
125 const StringArrayPtr &strs)
127 JSStringArrayPriv *priv = new JSStringArrayPriv(context, strs);
128 return JSObjectMake(context, getClassRef(), priv);
131 const JSClassDefinition* JSStringArray::getClassInfo()
133 return &(m_classInfo);
136 JSClassRef JSStringArray::getClassRef()
139 m_jsClassRef = JSClassCreate(&m_classInfo);
144 bool JSStringArray::isObjectOfClass(JSContextRef context, JSValueRef value)
146 return JSValueIsObjectOfClass(context, value, getClassRef());
149 StringArrayPtr JSStringArray::getStringArray(JSContextRef context, JSValueRef value)
151 if (!isObjectOfClass(context, value)) {
152 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
154 JSObjectRef object = JSValueToObject(context, value, NULL);
156 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
158 JSStringArrayPriv* priv = static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(object));
160 Throw(WrtDeviceApis::Commons::NullPointerException);
162 return priv->getObject();
165 void JSStringArray::initialize(JSContextRef context,
171 void JSStringArray::finalize(JSObjectRef object)
174 JSStringArrayPriv* priv =
175 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(object));
177 JSObjectSetPrivate(object, NULL);
180 bool JSStringArray::hasProperty(JSContextRef context,
182 JSStringRef propertyName)
185 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
186 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
189 size_t index = converter->toSizeT(propertyName);
190 JSStringArrayPriv* priv =
191 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(object));
193 Throw(WrtDeviceApis::Commons::NullPointerException);
195 StringArrayPtr strs = priv->getObject();
196 if (index < strs->size()) {
200 Catch(WrtDeviceApis::Commons::Exception)
202 //not reporting error is intended
207 JSValueRef JSStringArray::getProperty(JSContextRef context,
209 JSStringRef propertyName,
210 JSValueRef* exception)
213 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
214 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
217 size_t index = converter->toSizeT(propertyName);
218 JSStringArrayPriv* priv =
219 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(object));
221 Throw(WrtDeviceApis::Commons::NullPointerException);
223 StringArrayPtr strs = priv->getObject();
224 if (index < strs->size()) {
225 std::string result = strs->at(index);
226 if (!result.empty()) {
227 return converter->toJSValueRef(result);
231 Catch(WrtDeviceApis::Commons::Exception)
233 LogError("invalid property");
235 return JSValueMakeUndefined(context);
238 bool JSStringArray::setProperty(JSContextRef context,
240 JSStringRef propertyName,
242 JSValueRef* exception)
245 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
246 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
249 size_t index = converter->toSizeT(propertyName);
251 if (!JSValueIsUndefined(context, value)) {
252 str = converter->toString(value);
254 JSStringArrayPriv* priv =
255 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(object));
257 Throw(WrtDeviceApis::Commons::NullPointerException);
259 StringArrayPtr strs = priv->getObject();
261 Throw(WrtDeviceApis::Commons::NullPointerException);
263 if (strs->size() <= index) {
264 strs->resize(index + 1);
266 (*strs)[index] = str;
269 Catch(WrtDeviceApis::Commons::Exception)
271 LogError("error occured");
272 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
277 JSValueRef JSStringArray::concat(JSContextRef context,
278 JSObjectRef function,
279 JSObjectRef thisObject,
280 size_t argumentCount,
281 const JSValueRef arguments[],
282 JSValueRef* exception)
287 StringArrayPtr strs = StringArrayPtr(new StringArray());
288 JSStringArrayPriv *newPrivateObject = new JSStringArrayPriv(
291 JSValueRef result = JSObjectMake(context,
292 getClassRef(), newPrivateObject);
295 JSStringArrayPriv* priv =
296 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
297 StringArrayPtr currentStrings = priv->getObject();
298 for (size_t i = 0; i < currentStrings->size(); ++i) {
299 strs->push_back(currentStrings->at(i));
302 //copy submitted arrays
303 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
304 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
305 for (size_t i = 0; i < argumentCount; ++i) {
306 if (!JSIsArrayValue(context, arguments[i])) {
307 Throw(WrtDeviceApis::Commons::ConversionException);
309 // process array of strings
310 JSObjectRef arrayObj = converter->toJSObjectRef(arguments[i]);
311 unsigned int len = JSGetArrayLength(context, arrayObj);
312 for (unsigned int e = 0; e < len; ++e) {
313 JSValueRef att = JSGetArrayElement(context, arrayObj, e);
314 strs->push_back(converter->toString(att));
319 Catch(WrtDeviceApis::Commons::Exception)
321 LogError("error occured");
323 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
326 JSValueRef JSStringArray::join(JSContextRef context,
327 JSObjectRef function,
328 JSObjectRef thisObject,
329 size_t argumentCount,
330 const JSValueRef arguments[],
331 JSValueRef* exception)
333 //LogDebug("entered");
337 std::string separator(",");
338 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
339 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
340 JSStringArrayPriv* priv =
341 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
342 StringArrayPtr currentStrings = priv->getObject();
343 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
344 separator = converter->toString(arguments[0]);
346 for (size_t i = 0; i < currentStrings->size(); ++i) {
350 result += currentStrings->at(i);
352 return converter->toJSValueRef(result);
354 Catch(WrtDeviceApis::Commons::Exception)
356 LogError("error occured");
358 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
361 JSValueRef JSStringArray::pop(JSContextRef context,
362 JSObjectRef function,
363 JSObjectRef thisObject,
364 size_t argumentCount,
365 const JSValueRef arguments[],
366 JSValueRef* exception)
368 //LogDebug("entered");
371 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
372 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
373 JSStringArrayPriv* priv =
374 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
375 StringArrayPtr currentStrings = priv->getObject();
376 if (currentStrings->size() > 0) {
377 std::string result = currentStrings->at(
378 currentStrings->size() - 1);
379 currentStrings->pop_back();
380 return converter->toJSValueRef(result);
383 Catch(WrtDeviceApis::Commons::Exception)
385 LogError("error occured");
387 return JSValueMakeUndefined(context);
390 JSValueRef JSStringArray::push(JSContextRef context,
391 JSObjectRef function,
392 JSObjectRef thisObject,
393 size_t argumentCount,
394 const JSValueRef arguments[],
395 JSValueRef* exception)
397 //LogDebug("entered");
400 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
401 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
402 JSStringArrayPriv* priv =
403 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
404 StringArrayPtr currentStrings = priv->getObject();
405 for (size_t i = 0; i < argumentCount; ++i) {
406 currentStrings->push_back(converter->toString(arguments[i]));
408 return converter->toJSValueRef(currentStrings->size());
410 Catch(WrtDeviceApis::Commons::Exception)
412 LogError("error occured");
414 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
417 JSValueRef JSStringArray::reverse(JSContextRef context,
418 JSObjectRef function,
419 JSObjectRef thisObject,
420 size_t argumentCount,
421 const JSValueRef arguments[],
422 JSValueRef* exception)
424 //LogDebug("entered");
427 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
428 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
429 JSStringArrayPriv* priv =
430 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
431 StringArrayPtr currentStrings = priv->getObject();
432 std::reverse(currentStrings->begin(), currentStrings->end());
435 Catch(WrtDeviceApis::Commons::Exception)
437 LogError("error occured");
439 return JSValueMakeUndefined(context);
442 JSValueRef JSStringArray::shift(JSContextRef context,
443 JSObjectRef function,
444 JSObjectRef thisObject,
445 size_t argumentCount,
446 const JSValueRef arguments[],
447 JSValueRef* exception)
449 //LogDebug("entered");
452 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
453 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
454 JSStringArrayPriv* priv =
455 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
456 StringArrayPtr currentStrings = priv->getObject();
457 if (currentStrings->size() > 0) {
458 std::string result = currentStrings->at(0);
459 currentStrings->erase(currentStrings->begin());
460 return converter->toJSValueRef(result);
463 Catch(WrtDeviceApis::Commons::Exception)
465 LogError("error occured");
467 return JSValueMakeUndefined(context);
470 JSValueRef JSStringArray::slice(JSContextRef context,
471 JSObjectRef function,
472 JSObjectRef thisObject,
473 size_t argumentCount,
474 const JSValueRef arguments[],
475 JSValueRef* exception)
480 if (argumentCount < 1) {
481 return JSValueMakeUndefined(context);
483 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
484 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
485 StringArrayPtr strs = StringArrayPtr(new StringArray());
486 JSStringArrayPriv *newPrivateObject = new JSStringArrayPriv(
489 JSValueRef result = JSObjectMake(context,
490 getClassRef(), newPrivateObject);
493 JSStringArrayPriv* priv =
494 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
495 StringArrayPtr currentStrings = priv->getObject();
496 std::size_t first = converter->toSizeT(arguments[0]);
497 std::size_t last = currentStrings->size() - 1;
498 if (argumentCount > 1) {
499 last = converter->toSizeT(arguments[1]);
500 if (last >= currentStrings->size()) {
501 last = currentStrings->size() - 1;
507 for (size_t i = first; i <= last; ++i) {
508 strs->push_back(currentStrings->at(i));
513 Catch(WrtDeviceApis::Commons::Exception)
515 LogError("error occured");
517 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
520 JSValueRef JSStringArray::sort(JSContextRef context,
521 JSObjectRef function,
522 JSObjectRef thisObject,
523 size_t argumentCount,
524 const JSValueRef arguments[],
525 JSValueRef* exception)
527 //LogDebug("entered");
530 WrtDeviceApis::CommonsJavaScript::BasicConverter converter =
531 WrtDeviceApis::CommonsJavaScript::BasicConverterFactory::getConverter(context);
532 JSStringArrayPriv* priv =
533 static_cast<JSStringArrayPriv*>(JSObjectGetPrivate(thisObject));
534 StringArrayPtr currentStrings = priv->getObject();
535 std::sort(currentStrings->begin(), currentStrings->end());
538 Catch(WrtDeviceApis::Commons::Exception)
540 LogError("error occured");
542 return JSValueMakeUndefined(context);
545 JSValueRef JSStringArray::splice(JSContextRef context,
546 JSObjectRef function,
547 JSObjectRef thisObject,
548 size_t argumentCount,
549 const JSValueRef arguments[],
550 JSValueRef* exception)
552 //LogDebug("entered");
553 return JSValueMakeUndefined(context);
556 JSValueRef JSStringArray::toString(JSContextRef context,
557 JSObjectRef function,
558 JSObjectRef thisObject,
559 size_t argumentCount,
560 const JSValueRef arguments[],
561 JSValueRef* exception)
563 //LogDebug("entered");
564 return join(context, function, thisObject, 0, arguments, exception);
567 JSValueRef JSStringArray::unshift(JSContextRef context,
568 JSObjectRef function,
569 JSObjectRef thisObject,
570 size_t argumentCount,
571 const JSValueRef arguments[],
572 JSValueRef* exception)
574 //LogDebug("entered");
575 return JSValueMakeUndefined(context);
578 JSValueRef JSStringArray::valueOf(JSContextRef context,
579 JSObjectRef function,
580 JSObjectRef thisObject,
581 size_t argumentCount,
582 const JSValueRef arguments[],
583 JSValueRef* exception)
585 //LogDebug("entered");
586 return JSValueMakeUndefined(context);