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 bool ArgumentValidator::isOmitted(int index){
65 bool ArgumentValidator::isNull(int index){
66 if( !isOmitted(index) && JSValueIsNull(mContext, mArgv[index]) ){
71 bool ArgumentValidator::isUndefined(int index){
72 if( !isOmitted(index) && JSValueIsUndefined(mContext, mArgv[index]) ){
78 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue) const{
79 JSValueRef value = getArgument(index, nullable);
80 if( JSValueIsNull(mContext, value) && nullable){
83 return JSUtil::JSValueToNumber(mContext, value);
86 long ArgumentValidator::toLong(int index, bool nullable, long defaultvalue) const{
87 return static_cast<long>(toLongLong(index,nullable,defaultvalue));
90 signed char ArgumentValidator::toByte(int index, bool nullable, signed char defaultvalue) const{
91 return static_cast<signed char>(toLong(index,nullable,defaultvalue));
94 unsigned char ArgumentValidator::toOctet(int index, bool nullable, unsigned char defaultvalue) const{
95 return static_cast<unsigned char>(toULong(index,nullable,defaultvalue));
98 unsigned long ArgumentValidator::toULong(int index, bool nullable, unsigned long defaultvalue) const{
99 double number = toNumber(index,nullable,defaultvalue);
101 return static_cast<unsigned long>(static_cast<long>(number));
102 return static_cast<unsigned long>(number);
105 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue) const{
106 return static_cast<long long>(toNumber(index,nullable,defaultvalue));
109 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, unsigned long long defaultvalue) const{
110 double number = toNumber(index,nullable,defaultvalue);
112 return static_cast<unsigned long long>(static_cast<long long>(number));
113 return static_cast<unsigned long long>(number);
117 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue) const{
118 JSValueRef value = getArgument(index, nullable);
119 if( JSValueIsNull(mContext, value) && nullable){
122 return JSUtil::JSValueToDouble(mContext, value);
125 std::string ArgumentValidator::toString(int index, bool nullable, const string & defaultvalue) const{
126 JSValueRef value = getArgument(index, nullable);
127 if( JSValueIsNull(mContext, value) && nullable){
133 JSValueRef exception = NULL;
134 JSStringRef str = JSValueToStringCopy(mContext, value, &exception);
135 if (exception != NULL) {
136 throw TypeMismatchException(mContext, exception);
138 size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
141 JSStringGetUTF8CString(str, buffer, jsSize);
144 JSStringRelease(str);
148 bool ArgumentValidator::toBool(int index, bool nullable, bool defaultvalue) const{
149 JSValueRef value = getArgument(index, nullable);
150 if( JSValueIsNull(mContext, value) && nullable){
153 bool boolvalue = JSValueToBoolean(mContext, value);
157 time_t ArgumentValidator::toTimeT(int index, bool nullable, time_t defaultvalue) const{
158 JSValueRef value = getArgument(index, nullable);
159 if( JSValueIsNull(mContext, value) && nullable){
162 return JSUtil::JSValueToTimeT(mContext, value);
165 JSObjectRef ArgumentValidator::toObject(int index, bool nullable) const{
166 JSValueRef value = getArgument(index, nullable);
167 if( JSValueIsNull(mContext, value) && nullable){
170 if( !JSValueIsObject(mContext, value) ){
171 throw TypeMismatchException("Value is not Object");
174 JSValueRef exception = NULL;
175 JSObjectRef object = JSValueToObject(mContext, value, &exception);
177 throw TypeMismatchException(mContext, exception);
182 JSObjectRef ArgumentValidator::toObject(int index, JSClassRef info, bool nullable) const{
183 JSObjectRef obj = toObject(index, nullable);
186 if( !JSValueIsObjectOfClass( mContext, obj, info) ){
187 throw TypeMismatchException("Value is not correct type");
192 JSValueRef ArgumentValidator::toJSValueRef(int index, bool nullable) const{
193 return getArgument(index, nullable);
196 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable) const{
197 JSObjectRef obj = toObject(index, nullable);
198 if( obj == NULL && nullable){
201 if( !JSObjectIsFunction( mContext, obj )){
202 throw TypeMismatchException("Value is not function");
207 JSObjectRef ArgumentValidator::toArrayObject(int index, bool nullable) const{
208 JSValueRef value = getArgument(index, nullable);
209 if( JSValueIsNull(mContext, value) && nullable){
213 if( !JSIsArrayValue(mContext, value)){
214 throw TypeMismatchException("Type is not Array");
217 JSValueRef exception = NULL;
218 JSObjectRef obj = JSValueToObject(mContext, value, &exception);
219 if( exception != NULL )
220 throw TypeMismatchException(mContext, exception);
225 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable) const{
226 JSObjectRef value = toArrayObject(index, nullable);
227 if( value == NULL || JSValueIsNull(mContext, value) ){
228 return std::vector<std::string>();
230 return JSUtil::JSArrayToStringVector(mContext, value);
233 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable) const{
234 JSObjectRef value = toArrayObject(index, nullable);
235 if( value == NULL || JSValueIsNull(mContext, value) ){
236 return std::vector<long>();
238 return JSUtil::JSArrayToLongVector(mContext, value);
242 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable) const{
243 JSObjectRef value = toArrayObject(index, nullable);
244 if( value == NULL || JSValueIsNull(mContext, value) ){
245 return std::vector<double>();
247 return JSUtil::JSArrayToDoubleVector(mContext, value);
250 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable) const{
251 JSObjectRef value = toArrayObject(index, nullable);
252 if( value == NULL || JSValueIsNull(mContext, value) ){
253 return std::vector<time_t>();
255 return JSUtil::JSArrayToTimeTVector(mContext, value);
258 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable) const{
259 JSObjectRef value = toArrayObject(index, nullable);
260 if( value == NULL || JSValueIsNull(mContext, value) ){
261 return std::vector<bool>();
263 return JSUtil::JSArrayToBoolVector(mContext, value);
266 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable) const{
267 JSObjectRef value = toArrayObject(index, nullable);
268 if( value == NULL || JSValueIsNull(mContext, value) ){
269 return std::vector<JSValueRef>();
272 std::vector<JSValueRef> result;
273 for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
274 JSValueRef element = JSGetArrayElement(mContext, value, i);
275 result.push_back(element);
280 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable) const{
281 JSObjectRef value = toObject(index, nullable);
282 if( value == NULL || JSValueIsNull(mContext, value) ){
283 return std::map<std::string, std::string>();
286 return JSUtil::JSValueToStringMap(mContext, value);
289 JSObjectRef ArgumentValidator::toCallbackObject(int index, bool nullable, const char *callback, ...) const{
290 JSObjectRef obj = toObject(index, nullable);
291 if( obj == NULL && nullable){
295 va_start (var_args, callback);
296 const char * check = callback;
297 while( check != NULL ){
298 JSStringRef propertyName = JSStringCreateWithUTF8CString(check);
299 bool has = JSObjectHasProperty(mContext, obj, propertyName);
300 JSStringRelease(propertyName);
302 JSObjectRef o = JSUtil::JSValueToObject(mContext, JSUtil::getProperty(mContext, obj, check));
303 if( !JSObjectIsFunction(mContext, o) ){
305 throw TypeMismatchException("Property is not function object");
308 check = static_cast<const char *>(va_arg(var_args, const char *));