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>
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace WrtDeviceApis::Commons;
37 ArgumentValidator::ArgumentValidator(JSContextRef ctx, int argc, const JSValueRef* argv):mContext(ctx),mArgc(argc), mArgv(argv){
40 ArgumentValidator::~ArgumentValidator(){
43 JSValueRef ArgumentValidator::getArgument(int index, bool nullable) const{
48 JSValueRef value = NULL;
50 value = JSValueMakeNull(mContext);
52 value = JSValueMakeUndefined(mContext);
56 bool ArgumentValidator::isOmitted(int index){
61 bool ArgumentValidator::isNull(int index){
62 if( !isOmitted(index) && JSValueIsNull(mContext, mArgv[index]) ){
67 bool ArgumentValidator::isUndefined(int index){
68 if( !isOmitted(index) && JSValueIsUndefined(mContext, mArgv[index]) ){
74 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue) const{
75 JSValueRef value = getArgument(index, nullable);
76 if( JSValueIsNull(mContext, value) && nullable){
79 return JSUtil::JSValueToNumber(mContext, value);
82 long ArgumentValidator::toLong(int index, bool nullable, long defaultvalue) const{
83 return static_cast<long>(toLongLong(index,nullable,defaultvalue));
86 signed char ArgumentValidator::toByte(int index, bool nullable, signed char defaultvalue) const{
87 return static_cast<signed char>(toLong(index,nullable,defaultvalue));
90 unsigned char ArgumentValidator::toOctet(int index, bool nullable, unsigned char defaultvalue) const{
91 return static_cast<unsigned char>(toULong(index,nullable,defaultvalue));
94 unsigned long ArgumentValidator::toULong(int index, bool nullable, unsigned long defaultvalue) const{
95 double number = toNumber(index,nullable,defaultvalue);
97 return static_cast<unsigned long>(static_cast<long>(number));
98 return static_cast<unsigned long>(number);
101 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue) const{
102 return static_cast<long long>(toNumber(index,nullable,defaultvalue));
105 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, unsigned long long defaultvalue) const{
106 double number = toNumber(index,nullable,defaultvalue);
108 return static_cast<unsigned long long>(static_cast<long long>(number));
109 return static_cast<unsigned long long>(number);
113 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue) const{
114 JSValueRef value = getArgument(index, nullable);
115 if( JSValueIsNull(mContext, value) && nullable){
118 return JSUtil::JSValueToDouble(mContext, value);
121 std::string ArgumentValidator::toString(int index, bool nullable, const string & defaultvalue) const{
122 JSValueRef value = getArgument(index, nullable);
123 if( JSValueIsNull(mContext, value) && nullable){
129 JSValueRef exception = NULL;
130 JSStringRef str = JSValueToStringCopy(mContext, value, &exception);
131 if (exception != NULL) {
132 throw TypeMismatchException(mContext, exception);
134 size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
137 JSStringGetUTF8CString(str, buffer, jsSize);
140 JSStringRelease(str);
144 bool ArgumentValidator::toBool(int index, bool nullable, bool defaultvalue) const{
145 JSValueRef value = getArgument(index, nullable);
146 if( JSValueIsNull(mContext, value) && nullable){
149 bool boolvalue = JSValueToBoolean(mContext, value);
153 time_t ArgumentValidator::toTimeT(int index, bool nullable, time_t defaultvalue) const{
154 JSValueRef value = getArgument(index, nullable);
155 if( JSValueIsNull(mContext, value) && nullable){
158 return JSUtil::JSValueToTimeT(mContext, value);
161 JSObjectRef ArgumentValidator::toObject(int index, bool nullable) const{
162 JSValueRef value = getArgument(index, nullable);
163 if( JSValueIsNull(mContext, value) && nullable){
166 if( !JSValueIsObject(mContext, value) ){
167 throw TypeMismatchException("Value is not Object");
170 JSValueRef exception = NULL;
171 JSObjectRef object = JSValueToObject(mContext, value, &exception);
173 throw TypeMismatchException(mContext, exception);
178 JSObjectRef ArgumentValidator::toObject(int index, JSClassRef info, bool nullable) const{
179 JSObjectRef obj = toObject(index, nullable);
182 if( !JSValueIsObjectOfClass( mContext, obj, info) ){
183 throw TypeMismatchException("Value is not correct type");
188 JSValueRef ArgumentValidator::toJSValueRef(int index, bool nullable) const{
189 return getArgument(index, nullable);
192 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable) const{
193 JSObjectRef obj = toObject(index, nullable);
194 if( obj == NULL && nullable){
197 if( !JSObjectIsFunction( mContext, obj )){
198 throw TypeMismatchException("Value is not function");
203 JSObjectRef ArgumentValidator::toArrayObject(int index, bool nullable) const{
204 JSValueRef value = getArgument(index, nullable);
205 if( JSValueIsNull(mContext, value) && nullable){
209 if( !JSIsArrayValue(mContext, value)){
210 throw TypeMismatchException("Type is not Array");
213 JSValueRef exception = NULL;
214 JSObjectRef obj = JSValueToObject(mContext, value, &exception);
215 if( exception != NULL )
216 throw TypeMismatchException(mContext, exception);
221 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable) const{
222 JSObjectRef value = toArrayObject(index, nullable);
223 if( value == NULL || JSValueIsNull(mContext, value) ){
224 return std::vector<std::string>();
226 return JSUtil::JSArrayToStringVector(mContext, value);
229 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable) const{
230 JSObjectRef value = toArrayObject(index, nullable);
231 if( value == NULL || JSValueIsNull(mContext, value) ){
232 return std::vector<long>();
234 return JSUtil::JSArrayToLongVector(mContext, value);
238 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable) const{
239 JSObjectRef value = toArrayObject(index, nullable);
240 if( value == NULL || JSValueIsNull(mContext, value) ){
241 return std::vector<double>();
243 return JSUtil::JSArrayToDoubleVector(mContext, value);
246 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable) const{
247 JSObjectRef value = toArrayObject(index, nullable);
248 if( value == NULL || JSValueIsNull(mContext, value) ){
249 return std::vector<time_t>();
251 return JSUtil::JSArrayToTimeTVector(mContext, value);
254 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable) const{
255 JSObjectRef value = toArrayObject(index, nullable);
256 if( value == NULL || JSValueIsNull(mContext, value) ){
257 return std::vector<bool>();
259 return JSUtil::JSArrayToBoolVector(mContext, value);
262 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable) const{
263 JSObjectRef value = toArrayObject(index, nullable);
264 if( value == NULL || JSValueIsNull(mContext, value) ){
265 return std::vector<JSValueRef>();
268 std::vector<JSValueRef> result;
269 for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
270 JSValueRef element = JSGetArrayElement(mContext, value, i);
271 result.push_back(element);
276 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable) const{
277 JSObjectRef value = toObject(index, nullable);
278 if( value == NULL || JSValueIsNull(mContext, value) ){
279 return std::map<std::string, std::string>();
282 return JSUtil::JSValueToStringMap(mContext, value);
285 JSObjectRef ArgumentValidator::toCallbackObject(int index, bool nullable, const char *callback, ...) const{
286 JSObjectRef obj = toObject(index, nullable);
287 if( obj == NULL && nullable){
291 va_start (var_args, callback);
292 const char * check = callback;
293 while( check != NULL ){
294 JSStringRef propertyName = JSStringCreateWithUTF8CString(check);
295 bool has = JSObjectHasProperty(mContext, obj, propertyName);
296 JSStringRelease(propertyName);
298 JSObjectRef o = JSUtil::JSValueToObject(mContext, JSUtil::getProperty(mContext, obj, check));
299 if( !JSObjectIsFunction(mContext, o) ){
301 throw TypeMismatchException("Property is not function object");
304 check = static_cast<const char *>(va_arg(var_args, const char *));