2 // Tizen Web Device API
3 // Copyright (c) 2013 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 #include "ArgumentValidator.h"
20 #include "PlatformException.h"
22 #include <CommonsJavaScript/Converter.h>
28 #define LOG_TAG "TIZEN_DEVICEAPI"
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace WrtDeviceApis::Commons;
38 ArgumentValidator::ArgumentValidator(JSContextRef ctx, int argc, const JSValueRef* argv):mContext(ctx),mArgc(argc), mArgv(argv){
41 ArgumentValidator::~ArgumentValidator(){
44 JSValueRef ArgumentValidator::getArgument(int index, bool nullable){
49 JSValueRef value = NULL;
51 value = JSValueMakeNull(mContext);
53 value = JSValueMakeUndefined(mContext);
57 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue){
58 JSValueRef value = getArgument(index, nullable);
59 JSValueRef exception = NULL;
60 if( JSValueIsNull(mContext, value) && nullable){
63 double numberValue = JSValueToNumber(mContext, value, &exception);
64 if(exception != NULL){
65 throw TypeMismatchException(mContext, exception);
70 long ArgumentValidator::toLong(int index, bool nullable, long defaultvalue){
71 return static_cast<long>(toLongLong(index,nullable,defaultvalue));
74 unsigned long ArgumentValidator::toULong(int index, bool nullable, long defaultvalue){
75 return static_cast<unsigned long>(toULongLong(index,nullable,defaultvalue));
78 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue){
79 return static_cast<long long>(toNumber(index,nullable,defaultvalue));
82 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, long long defaultvalue){
83 return static_cast<unsigned long long>(toNumber(index,nullable,defaultvalue));
87 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue){
88 double doublevalue = toNumber(index,nullable,defaultvalue);
89 if( isnan(doublevalue)){
90 throw TypeMismatchException("Value is not number");
95 std::string ArgumentValidator::toString(int index, bool nullable, const string & defaultvalue){
96 JSValueRef value = getArgument(index, nullable);
97 if( JSValueIsNull(mContext, value) && nullable){
103 JSValueRef exception = NULL;
104 JSStringRef str = JSValueToStringCopy(mContext, value, &exception);
105 if (exception != NULL) {
106 throw TypeMismatchException(mContext, exception);
108 size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
111 JSStringGetUTF8CString(str, buffer, jsSize);
114 JSStringRelease(str);
118 bool ArgumentValidator::toBool(int index, bool nullable, bool defaultvalue){
119 JSValueRef value = getArgument(index, nullable);
120 if( JSValueIsNull(mContext, value) && nullable){
123 bool boolvalue = JSValueToBoolean(mContext, value);
127 time_t ArgumentValidator::toTimeT(int index, bool nullable, time_t defaultvalue){
128 JSValueRef value = getArgument(index, nullable);
129 if( JSValueIsNull(mContext, value) && nullable){
132 return JSUtil::JSValueToTimeT(mContext, value);
135 JSObjectRef ArgumentValidator::toObject(int index, bool nullable){
136 JSValueRef value = getArgument(index, nullable);
137 if( JSValueIsNull(mContext, value) && nullable){
140 if( !JSValueIsObject(mContext, value) ){
141 throw TypeMismatchException("Value is not Object");
144 JSValueRef exception = NULL;
145 JSObjectRef object = JSValueToObject(mContext, value, &exception);
147 throw TypeMismatchException(mContext, exception);
152 JSObjectRef ArgumentValidator::toObject(int index, JSClassRef info, bool nullable){
153 JSObjectRef obj = toObject(index, nullable);
156 if( !JSValueIsObjectOfClass( mContext, obj, info) ){
157 throw TypeMismatchException("Value is not correct type");
162 JSValueRef ArgumentValidator::toJSValueRef(int index, bool nullable){
163 return getArgument(index, nullable);
166 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable){
167 JSObjectRef obj = toObject(index, nullable);
168 if( obj == NULL && nullable){
171 if( !JSObjectIsFunction( mContext, obj )){
172 throw TypeMismatchException("Value is not function");
177 JSObjectRef ArgumentValidator::toArrayObject(int index, bool nullable){
178 JSValueRef value = getArgument(index, nullable);
179 if( JSValueIsNull(mContext, value) && nullable){
183 if( !JSIsArrayValue(mContext, value)){
184 JSObjectRef jsArray = JSCreateArrayObject(mContext, 0, NULL);
185 if( !JSValueIsNull(mContext,value) && !JSValueIsUndefined(mContext,value) )
186 JSSetArrayElement(mContext, jsArray, 0, value);
189 JSValueRef exception = NULL;
190 JSObjectRef obj = JSValueToObject(mContext, value, &exception);
191 if( exception != NULL )
192 throw TypeMismatchException(mContext, exception);
197 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable){
198 JSObjectRef value = toArrayObject(index, nullable);
199 if( value == NULL || JSValueIsNull(mContext, value) ){
200 return std::vector<std::string>();
202 return JSUtil::JSArrayToStringVector(mContext, value);
205 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable){
206 JSObjectRef value = toArrayObject(index, nullable);
207 if( value == NULL || JSValueIsNull(mContext, value) ){
208 return std::vector<long>();
210 return JSUtil::JSArrayToLongVector(mContext, value);
214 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable){
215 JSObjectRef value = toArrayObject(index, nullable);
216 if( value == NULL || JSValueIsNull(mContext, value) ){
217 return std::vector<double>();
219 return JSUtil::JSArrayToDoubleVector(mContext, value);
222 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable){
223 JSObjectRef value = toArrayObject(index, nullable);
224 if( value == NULL || JSValueIsNull(mContext, value) ){
225 return std::vector<time_t>();
227 return JSUtil::JSArrayToTimeTVector(mContext, value);
230 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable){
231 JSObjectRef value = toArrayObject(index, nullable);
232 if( value == NULL || JSValueIsNull(mContext, value) ){
233 return std::vector<bool>();
235 return JSUtil::JSArrayToBoolVector(mContext, value);
238 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable){
239 JSObjectRef value = toArrayObject(index, nullable);
240 if( value == NULL || JSValueIsNull(mContext, value) ){
241 return std::vector<JSValueRef>();
244 std::vector<JSValueRef> result;
245 for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
246 JSValueRef element = JSGetArrayElement(mContext, value, i);
247 result.push_back(element);
252 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable){
253 JSObjectRef value = toObject(index, nullable);
254 if( value == NULL || JSValueIsNull(mContext, value) ){
255 return std::map<std::string, std::string>();
258 return JSUtil::JSValueToStringMap(mContext, value);