From d161fccc9b662f073fd53d9de1d46f706de5ae10 Mon Sep 17 00:00:00 2001 From: Todd Lee Date: Wed, 28 Nov 2012 13:05:37 -0800 Subject: [PATCH] added two utility functions to emval. - check if a given key exists. - return length of an array. --- src/embind/emval.js | 13 +++++++++++++ system/include/emscripten/val.h | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/embind/emval.js b/src/embind/emval.js index f74c0fd54..a3157d2a7 100644 --- a/src/embind/emval.js +++ b/src/embind/emval.js @@ -53,6 +53,10 @@ function __emval_new_cstring(str) { return __emval_register(Pointer_stringify(str)); } +function __emval_has_property(handle, k) { + return _emval_handle_array[handle].value.hasOwnProperty(k); +} + function __emval_get_property(handle, k) { k = Pointer_stringify(k); return __emval_register(_emval_handle_array[handle].value[k]); @@ -66,6 +70,15 @@ function __emval_get_property_by_unsigned_long(handle, k) { return __emval_register(_emval_handle_array[handle].value[k]); } +function __emval_get_length(handle) { + var val = _emval_handle_array[handle].value; + if (Object.prototype.toString.call(val) === "[object Array]") { + return val.length; + } + + return 0; +} + function __emval_eval_global_method(handle, objectName, methodName) { var objectNameStr = Pointer_stringify(objectName); var methodNameStr = Pointer_stringify(methodName); diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index 68f5b63e3..945b0d8a6 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -14,12 +14,14 @@ namespace emscripten { EM_VAL _emval_new_object(); EM_VAL _emval_new_long(long value); EM_VAL _emval_new_cstring(const char* str); + bool _emval_has_property(EM_VAL object, const char* key); EM_VAL _emval_get_property(EM_VAL object, const char* key); EM_VAL _emval_get_property_by_long(EM_VAL object, long key); EM_VAL _emval_get_property_by_unsigned_long(EM_VAL object, unsigned long key); EM_VAL _emval_eval_global_method(EM_VAL object, const char* objectName, const char* methodName); void _emval_set_property(EM_VAL object, const char* key, EM_VAL value); void _emval_set_property_by_int(EM_VAL object, long key, EM_VAL value); + unsigned int _emval_get_length(EM_VAL object); void _emval_as(EM_VAL value, TYPEID returnType); EM_VAL _emval_call( EM_VAL value, @@ -78,6 +80,10 @@ namespace emscripten { return *this; } + bool exist(const char* key) const { + return internal::_emval_has_property(handle, key); + } + val get(const char* key) const { return val(internal::_emval_get_property(handle, key)); } @@ -111,6 +117,10 @@ namespace emscripten { internal::_emval_set_property_by_int(handle, key, v.handle); } + unsigned int length() { + return internal::_emval_get_length(handle); + } + template val operator()(Args... args) { using namespace internal;