diff options
author | Chad Austin <chad@imvu.com> | 2014-05-16 23:34:47 -0700 |
---|---|---|
committer | Chad Austin <chad@chadaustin.me> | 2014-05-16 23:41:36 -0700 |
commit | b41adadcf60a25c14f4b139d0ee664521f76a620 (patch) | |
tree | bfa375fa57f071b4eb0aeb49d1630fa41171b80e | |
parent | 398cded47b7e08fbb753fb7b4788da29a22195bb (diff) |
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); +} |