wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Common / JSArray.cpp
1 #include "JSArray.h"
2 #include "PlatformException.h"
3
4 namespace DeviceAPI {
5 namespace Common {
6
7 JSArrayBase::JSArrayBase( JSContextRef context, JSObjectRef array ):mContext(context), mArray(array){
8     if(!JSIsArrayValue(context, array)){
9         throw TypeMismatchException("The type is not array");
10     }
11     JSValueProtect(context, array);
12 }
13
14 JSArrayBase::JSArrayBase( JSContextRef context):mContext(context){
15     JSValueRef exception = NULL;
16     mArray = JSObjectMakeArray( context, 0, NULL, &exception);
17     if(exception != NULL){
18         throw UnknownException(context, exception);
19     }
20 }
21
22 JSArrayBase::~JSArrayBase(){
23     JSValueUnprotect(mContext, mArray);
24 }
25
26 size_t JSArrayBase::size() const {
27     return JSGetArrayLength(mContext, mArray);
28 }
29
30 void JSArrayBase::resize(size_t size){
31     JSUtil::setProperty(mContext, mArray, "length", JSUtil::toJSValueRef(mContext, static_cast<double>(size)), kJSPropertyAttributeNone, NULL);
32 }
33
34 JSValueRef JSArrayBase::get(int index) const{
35     return JSGetArrayElement(mContext, mArray, index);
36 }
37
38 bool JSArrayBase::set( int index, JSValueRef value){
39     bool t = JSSetArrayElement(mContext, mArray, index, value);
40     return t;
41 }
42
43 bool JSArrayBase::append( JSValueRef value ){
44     return set( size(), value);
45 }
46
47 }//Common
48 }//DeviceAPI