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.
18 #include "ArgumentValidationChecker.h"
19 #include "JSTizenException.h"
26 #define LOG_TAG "TIZEN_DEVICEAPI"
34 void ArgumentValidationChecker::check(JSContextRef ctx, int argumentCount, const JSValueRef arguments [ ], int convertedArgumentCount, JSValueRef convertedArguments [ ], Option option, ...){
36 va_start (var_args, option);
40 Type type = static_cast<Type>(va_arg(var_args, int));
41 JSObjectRef testObject = NULL;
42 bool nullable = ((opt & NULLABLE) == NULLABLE );
43 JSValueRef jsException = NULL;
45 if( index >= convertedArgumentCount ){
46 LOGE("invalid argument check code");
51 if( argumentCount <= index ){
53 convertedArguments[index] = JSValueMakeUndefined(ctx);
55 convertedArguments[index] = JSValueMakeNull(ctx);
58 convertedArguments[index] = arguments[index];
59 if( JSValueIsObject(ctx, arguments[index]) )
60 testObject = JSValueToObject(ctx, arguments[index], NULL);
65 if( JSValueIsNull(ctx, convertedArguments[index]) && nullable){
66 if( type == ArgumentValidationChecker::UserDefine)
67 va_arg(var_args, JSClassRef);
68 if( type == ArgumentValidationChecker::SelfCheck)
69 va_arg(var_args, SelfChecker);
71 opt = static_cast<Option>(va_arg (var_args, int));
78 case ArgumentValidationChecker::Float:
79 case ArgumentValidationChecker::Long:
81 double testValue = JSValueToNumber(ctx, convertedArguments[index], &jsException );
82 if( jsException != NULL )
83 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "Could not converting to number", index);
84 if( ArgumentValidationChecker::Float == type && testValue != testValue )
85 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "Converted value is nan", index);
88 case ArgumentValidationChecker::String:
90 JSStringRef convertedstr = JSValueToStringCopy(ctx, convertedArguments[index], &jsException);
91 if( jsException != NULL){
92 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "Could not converting to string", index);
94 JSStringRelease(convertedstr);
97 case ArgumentValidationChecker::Boolean:
98 //Boolean can convert from any types.
100 case ArgumentValidationChecker::Function:
101 if( testObject == NULL || !JSObjectIsFunction(ctx, testObject) )
102 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "argument is not function", index);
104 case ArgumentValidationChecker::Object:
105 if( testObject == NULL )
106 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "argument is not object", index);
108 case ArgumentValidationChecker::Array:
109 if( !JSIsArrayValue(ctx, convertedArguments[index])){
110 JSObjectRef jsArray = JSCreateArrayObject(ctx, 0, NULL);
111 JSSetArrayElement(ctx, jsArray, 0, convertedArguments[index]);
112 convertedArguments[index] = jsArray;
115 case ArgumentValidationChecker::UserDefine:
117 JSClassRef classType = va_arg(var_args, JSClassRef);
118 if( !JSValueIsObjectOfClass(ctx, convertedArguments[index],classType) )
119 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "argument is not wanted object", index);
122 case ArgumentValidationChecker::SelfCheck:
124 SelfChecker checker = va_arg(var_args, SelfChecker);
125 if( !checker(convertedArguments[index]) )
126 throw ArgumentValidationException(JSTizenException::TYPE_MISMATCH_ERROR, "argument is not wanted object", index);
131 opt = static_cast<Option>(va_arg (var_args, int));