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>
31 #define LOG_TAG "TIZEN_DEVICEAPI"
34 using namespace WrtDeviceApis::CommonsJavaScript;
35 using namespace WrtDeviceApis::Commons;
41 ArgumentValidator::ArgumentValidator(JSContextRef ctx, int argc, const JSValueRef* argv):mContext(ctx),mArgc(argc), mArgv(argv){
44 ArgumentValidator::~ArgumentValidator(){
47 JSValueRef ArgumentValidator::getArgument(int index, bool nullable) const{
52 JSValueRef value = NULL;
54 value = JSValueMakeNull(mContext);
56 value = JSValueMakeUndefined(mContext);
60 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue) const{
61 JSValueRef value = getArgument(index, nullable);
62 if( JSValueIsNull(mContext, value) && nullable){
65 return JSUtil::JSValueToNumber(mContext, value);
68 long ArgumentValidator::toLong(int index, bool nullable, long defaultvalue) const{
69 return static_cast<long>(toLongLong(index,nullable,defaultvalue));
72 signed char ArgumentValidator::toByte(int index, bool nullable, signed char defaultvalue) const{
73 return static_cast<signed char>(toLong(index,nullable,defaultvalue));
76 unsigned char ArgumentValidator::toOctet(int index, bool nullable, unsigned char defaultvalue) const{
77 return static_cast<unsigned char>(toULong(index,nullable,defaultvalue));
80 unsigned long ArgumentValidator::toULong(int index, bool nullable, unsigned long defaultvalue) const{
81 double number = toNumber(index,nullable,defaultvalue);
83 return static_cast<unsigned long>(static_cast<long>(number));
84 return static_cast<unsigned long>(number);
87 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue) const{
88 return static_cast<long long>(toNumber(index,nullable,defaultvalue));
91 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, unsigned long long defaultvalue) const{
92 double number = toNumber(index,nullable,defaultvalue);
94 return static_cast<unsigned long long>(static_cast<long long>(number));
95 return static_cast<unsigned long long>(number);
99 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue) const{
100 JSValueRef value = getArgument(index, nullable);
101 if( JSValueIsNull(mContext, value) && nullable){
104 return JSUtil::JSValueToDouble(mContext, value);
107 std::string ArgumentValidator::toString(int index, bool nullable, const string & defaultvalue) const{
108 JSValueRef value = getArgument(index, nullable);
109 if( JSValueIsNull(mContext, value) && nullable){
115 JSValueRef exception = NULL;
116 JSStringRef str = JSValueToStringCopy(mContext, value, &exception);
117 if (exception != NULL) {
118 throw TypeMismatchException(mContext, exception);
120 size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
123 JSStringGetUTF8CString(str, buffer, jsSize);
126 JSStringRelease(str);
130 bool ArgumentValidator::toBool(int index, bool nullable, bool defaultvalue) const{
131 JSValueRef value = getArgument(index, nullable);
132 if( JSValueIsNull(mContext, value) && nullable){
135 bool boolvalue = JSValueToBoolean(mContext, value);
139 time_t ArgumentValidator::toTimeT(int index, bool nullable, time_t defaultvalue) const{
140 JSValueRef value = getArgument(index, nullable);
141 if( JSValueIsNull(mContext, value) && nullable){
144 return JSUtil::JSValueToTimeT(mContext, value);
147 JSObjectRef ArgumentValidator::toObject(int index, bool nullable) const{
148 JSValueRef value = getArgument(index, nullable);
149 if( JSValueIsNull(mContext, value) && nullable){
152 if( !JSValueIsObject(mContext, value) ){
153 throw TypeMismatchException("Value is not Object");
156 JSValueRef exception = NULL;
157 JSObjectRef object = JSValueToObject(mContext, value, &exception);
159 throw TypeMismatchException(mContext, exception);
164 JSObjectRef ArgumentValidator::toObject(int index, JSClassRef info, bool nullable) const{
165 JSObjectRef obj = toObject(index, nullable);
168 if( !JSValueIsObjectOfClass( mContext, obj, info) ){
169 throw TypeMismatchException("Value is not correct type");
174 JSValueRef ArgumentValidator::toJSValueRef(int index, bool nullable) const{
175 return getArgument(index, nullable);
178 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable) const{
179 JSObjectRef obj = toObject(index, nullable);
180 if( obj == NULL && nullable){
183 if( !JSObjectIsFunction( mContext, obj )){
184 throw TypeMismatchException("Value is not function");
189 JSObjectRef ArgumentValidator::toArrayObject(int index, bool nullable) const{
190 JSValueRef value = getArgument(index, nullable);
191 if( JSValueIsNull(mContext, value) && nullable){
195 if( !JSIsArrayValue(mContext, value)){
196 throw TypeMismatchException("Type is not Array");
199 JSValueRef exception = NULL;
200 JSObjectRef obj = JSValueToObject(mContext, value, &exception);
201 if( exception != NULL )
202 throw TypeMismatchException(mContext, exception);
207 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable) const{
208 JSObjectRef value = toArrayObject(index, nullable);
209 if( value == NULL || JSValueIsNull(mContext, value) ){
210 return std::vector<std::string>();
212 return JSUtil::JSArrayToStringVector(mContext, value);
215 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable) const{
216 JSObjectRef value = toArrayObject(index, nullable);
217 if( value == NULL || JSValueIsNull(mContext, value) ){
218 return std::vector<long>();
220 return JSUtil::JSArrayToLongVector(mContext, value);
224 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable) const{
225 JSObjectRef value = toArrayObject(index, nullable);
226 if( value == NULL || JSValueIsNull(mContext, value) ){
227 return std::vector<double>();
229 return JSUtil::JSArrayToDoubleVector(mContext, value);
232 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable) const{
233 JSObjectRef value = toArrayObject(index, nullable);
234 if( value == NULL || JSValueIsNull(mContext, value) ){
235 return std::vector<time_t>();
237 return JSUtil::JSArrayToTimeTVector(mContext, value);
240 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable) const{
241 JSObjectRef value = toArrayObject(index, nullable);
242 if( value == NULL || JSValueIsNull(mContext, value) ){
243 return std::vector<bool>();
245 return JSUtil::JSArrayToBoolVector(mContext, value);
248 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable) const{
249 JSObjectRef value = toArrayObject(index, nullable);
250 if( value == NULL || JSValueIsNull(mContext, value) ){
251 return std::vector<JSValueRef>();
254 std::vector<JSValueRef> result;
255 for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
256 JSValueRef element = JSGetArrayElement(mContext, value, i);
257 result.push_back(element);
262 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable) const{
263 JSObjectRef value = toObject(index, nullable);
264 if( value == NULL || JSValueIsNull(mContext, value) ){
265 return std::map<std::string, std::string>();
268 return JSUtil::JSValueToStringMap(mContext, value);
271 JSObjectRef ArgumentValidator::toCallbackObject(int index, bool nullable, const char *callback, ...) const{
272 JSObjectRef obj = toObject(index, nullable);
273 if( obj == NULL && nullable){
277 va_start (var_args, callback);
278 const char * check = callback;
279 while( check != NULL ){
280 JSStringRef propertyName = JSStringCreateWithUTF8CString(check);
281 bool has = JSObjectHasProperty(mContext, obj, propertyName);
282 JSStringRelease(propertyName);
284 JSObjectRef o = JSUtil::JSValueToObject(mContext, JSUtil::getProperty(mContext, obj, check));
285 if( !JSObjectIsFunction(mContext, o) ){
287 throw TypeMismatchException("Property is not function object");
290 check = static_cast<const char *>(va_arg(var_args, const char *));