[Tizen] Common xwalk utilities module for shared JavaScript code.
authorSakari Poussa <sakari.poussa@intel.com>
Fri, 23 May 2014 07:53:19 +0000 (10:53 +0300)
committerSakari Poussa <sakari.poussa@intel.com>
Fri, 30 May 2014 07:21:25 +0000 (10:21 +0300)
Initial method contains validation for mandatory and optional arguments.

tizen-wrt.gyp
utils/utils.gyp [new file with mode: 0644]
utils/utils_api.js [new file with mode: 0644]
utils/utils_extension.cc [new file with mode: 0644]
utils/utils_extension.h [new file with mode: 0644]

index 7682b14..e221116 100644 (file)
@@ -18,6 +18,7 @@
         'system_setting/system_setting.gyp:*',
         'time/time.gyp:*',
         'tizen/tizen.gyp:*',
+        'utils/utils.gyp:*',
       ],
       'conditions': [
         [ 'tizen == 1', {
diff --git a/utils/utils.gyp b/utils/utils.gyp
new file mode 100644 (file)
index 0000000..0133bf5
--- /dev/null
@@ -0,0 +1,18 @@
+{
+  'includes':[
+    '../common/common.gypi',
+  ],
+  'targets': [
+    {
+      'target_name': 'tizen_utils',
+      'type': 'loadable_module',
+      'sources': [
+        'utils_api.js',
+        'utils_extension.cc',
+        'utils_extension.h',
+        '../common/extension.cc',
+        '../common/extension.h',
+      ],
+    },
+  ],
+}
diff --git a/utils/utils_api.js b/utils/utils_api.js
new file mode 100644 (file)
index 0000000..b97c8b5
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright (c) 2014 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+var signature_to_type = { 'n': 'number',
+                          'f': 'function',
+                          'b': 'boolean',
+                          's': 'string',
+                          'o': 'object'
+                        };
+
+function Utils() {
+}
+
+Utils.prototype.validateArguments = function(signature, args) {
+  var full_args = Array.prototype.slice.call(args);
+
+  // After '?' everything is optional.
+  var mandatory_len = signature.indexOf('?') === -1 ? signature.length : signature.indexOf('?');
+
+  if (full_args.length < mandatory_len)
+    return false;
+
+  // Mandatory arguments.
+  for (var i = 0; i < mandatory_len; i++) {
+    if (typeof full_args[i] !== signature_to_type[signature[i]] || full_args[i] === null)
+      return false;
+  }
+
+  // Optional args may be null.
+  for (var i = mandatory_len; i < full_args.length && i < signature.length - 1; i++) {
+    if (full_args[i] !== null && typeof full_args[i] !== signature_to_type[signature[i + 1]])
+      return false;
+  }
+
+  return true;
+};
+
+Utils.prototype.validateObject = function(object, signature, attributes) {
+  for (var i = 0; i < signature.length; i++) {
+    if (object.hasOwnProperty(attributes[i]) &&
+        typeof object[attributes[i]] !== signature_to_type[signature[i]]) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+exports = new Utils();
diff --git a/utils/utils_extension.cc b/utils/utils_extension.cc
new file mode 100644 (file)
index 0000000..d62345e
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright (c) 2014 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "utils/utils_extension.h"
+
+// This will be generated from tizen_api.js.
+extern const char kSource_utils_api[];
+
+common::Extension* CreateExtension() {
+  return new UtilsExtension;
+}
+
+UtilsExtension::UtilsExtension() {
+  SetExtensionName("xwalk.utils");
+  SetJavaScriptAPI(kSource_utils_api);
+}
+
+UtilsExtension::~UtilsExtension() {}
diff --git a/utils/utils_extension.h b/utils/utils_extension.h
new file mode 100644 (file)
index 0000000..484e4bf
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright (c) 2014 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UTILS_UTILS_EXTENSION_H_
+#define UTILS_UTILS_EXTENSION_H_
+
+#include "common/extension.h"
+
+class UtilsExtension : public common::Extension {
+ public:
+  UtilsExtension();
+  virtual ~UtilsExtension();
+};
+
+#endif  // UTILS_UTILS_EXTENSION_H_