From b41adadcf60a25c14f4b139d0ee664521f76a620 Mon Sep 17 00:00:00 2001 From: Chad Austin Date: Fri, 16 May 2014 23:34:47 -0700 Subject: expose typeof via emscripten::val --- src/embind/emval.js | 5 +++++ system/include/emscripten/val.h | 6 +++++- tests/embind/embind.test.js | 10 ++++++++++ tests/embind/embind_test.cpp | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) 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 @@ -254,7 +255,6 @@ namespace emscripten { // * delete // * in // * instanceof - // * typeof // * ! ~ - + ++ -- // * * / % // * + - @@ -411,6 +411,10 @@ namespace emscripten { return fromGenericWireType(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(); +} + +EMSCRIPTEN_BINDINGS(typeof) { + function("getTypeOfVal", &getTypeOfVal); +} -- cgit v1.2.3-18-g5258