diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-05-18 11:40:05 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-05-18 11:40:05 -0700 |
commit | f0dd2f8d2da50da0ab87f5e2585e59e7a5f521b7 (patch) | |
tree | 6c22b5ac383495f5c61a2175d50a10f9a63e4c95 | |
parent | 57360800512dc1b23532e1c125c8e0627ccdbcfa (diff) | |
parent | b41adadcf60a25c14f4b139d0ee664521f76a620 (diff) |
Merge pull request #2360 from chadaustin/expose-val-typeof
expose typeof via emscripten::val
-rw-r--r-- | src/embind/emval.js | 5 | ||||
-rw-r--r-- | system/include/emscripten/val.h | 6 | ||||
-rw-r--r-- | tests/embind/embind.test.js | 10 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 8 |
4 files changed, 28 insertions, 1 deletions
diff --git a/src/embind/emval.js b/src/embind/emval.js index 4007701a..ebec3881 100644 --- a/src/embind/emval.js +++ b/src/embind/emval.js @@ -286,3 +286,8 @@ function __emval_has_function(handle, name) { name = getStringOrSymbol(name); return handle[name] instanceof Function; } + +function __emval_typeof(handle) { + handle = requireHandle(handle); + return __emval_register(typeof handle); +} diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index e217c959..bfd8610a 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -62,6 +62,7 @@ namespace emscripten { bool _emval_has_function( EM_VAL value, const char* methodName); + EM_VAL _emval_typeof(EM_VAL value); } template<const char* address> @@ -254,7 +255,6 @@ namespace emscripten { // * delete // * in // * instanceof - // * typeof // * ! ~ - + ++ -- // * * / % // * + - @@ -411,6 +411,10 @@ namespace emscripten { return fromGenericWireType<T>(result); } + val typeof() const { + return val(_emval_typeof(handle)); + } + private: // takes ownership, assumes handle already incref'd explicit val(internal::EM_VAL handle) diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 3ded811a..53d3988a 100644 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -2030,6 +2030,16 @@ module({ assert.equal(65538, instance.c); }); }); + + BaseFixture.extend("typeof", function() { + test("typeof", function() { + assert.equal("object", cm.getTypeOfVal(null)); + assert.equal("object", cm.getTypeOfVal({})); + assert.equal("function", cm.getTypeOfVal(function(){})); + assert.equal("number", cm.getTypeOfVal(1)); + assert.equal("string", cm.getTypeOfVal("hi")); + }); + }); }); /* global run_all_tests */ diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index 5a83903a..52103bbb 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -2368,3 +2368,11 @@ EMSCRIPTEN_BINDINGS(val_new_) { function("construct_with_memory_view", &construct_with_memory_view); function("construct_with_ints_and_float", &construct_with_ints_and_float); } + +std::string getTypeOfVal(const val& v) { + return v.typeof().as<std::string>(); +} + +EMSCRIPTEN_BINDINGS(typeof) { + function("getTypeOfVal", &getTypeOfVal); +} |