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) const{
49 JSValueRef value = NULL;
51 value = JSValueMakeNull(mContext);
53 value = JSValueMakeUndefined(mContext);
57 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue) const{
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) const{
71 return static_cast<long>(toLongLong(index,nullable,defaultvalue));
74 unsigned long ArgumentValidator::toULong(int index, bool nullable, long defaultvalue) const{
75 return static_cast<unsigned long>(toULongLong(index,nullable,defaultvalue));
78 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue) const{
79 return static_cast<long long>(toNumber(index,nullable,defaultvalue));
82 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, long long defaultvalue) const{
83 return static_cast<unsigned long long>(toNumber(index,nullable,defaultvalue));
87 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue) const{
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) const{
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) const{
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) const{
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) const{
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) const{
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) const{
163 return getArgument(index, nullable);
166 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable) const{
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) const{
178 JSValueRef value = getArgument(index, nullable);
179 if( JSValueIsNull(mContext, value) && nullable){
183 if( !JSIsArrayValue(mContext, value)){
184 throw TypeMismatchException("Type is not Array");
187 JSValueRef exception = NULL;
188 JSObjectRef obj = JSValueToObject(mContext, value, &exception);
189 if( exception != NULL )
190 throw TypeMismatchException(mContext, exception);
195 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable) const{
196 JSObjectRef value = toArrayObject(index, nullable);
197 if( value == NULL || JSValueIsNull(mContext, value) ){
198 return std::vector<std::string>();
200 return JSUtil::JSArrayToStringVector(mContext, value);
203 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable) const{
204 JSObjectRef value = toArrayObject(index, nullable);
205 if( value == NULL || JSValueIsNull(mContext, value) ){
206 return std::vector<long>();
208 return JSUtil::JSArrayToLongVector(mContext, value);
212 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable) const{
213 JSObjectRef value = toArrayObject(index, nullable);
214 if( value == NULL || JSValueIsNull(mContext, value) ){
215 return std::vector<double>();
217 return JSUtil::JSArrayToDoubleVector(mContext, value);
220 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable) const{
221 JSObjectRef value = toArrayObject(index, nullable);
222 if( value == NULL || JSValueIsNull(mContext, value) ){
223 return std::vector<time_t>();
225 return JSUtil::JSArrayToTimeTVector(mContext, value);
228 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable) const{
229 JSObjectRef value = toArrayObject(index, nullable);
230 if( value == NULL || JSValueIsNull(mContext, value) ){
231 return std::vector<bool>();
233 return JSUtil::JSArrayToBoolVector(mContext, value);
236 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable) const{
237 JSObjectRef value = toArrayObject(index, nullable);
238 if( value == NULL || JSValueIsNull(mContext, value) ){
239 return std::vector<JSValueRef>();
242 std::vector<JSValueRef> result;
243 for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
244 JSValueRef element = JSGetArrayElement(mContext, value, i);
245 result.push_back(element);
250 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable) const{
251 JSObjectRef value = toObject(index, nullable);
252 if( value == NULL || JSValueIsNull(mContext, value) ){
253 return std::map<std::string, std::string>();
256 return JSUtil::JSValueToStringMap(mContext, value);