diff --git a/src/embind/emval.js b/src/embind/emval.js index c60c62e06..eb31777b5 100755 --- a/src/embind/emval.js +++ b/src/embind/emval.js @@ -73,8 +73,22 @@ function __emval_take_value(type, v) { return __emval_register(v); } -function __emval_new(handle) { - return __emval_register(new (_emval_handle_array[handle].value)); +function __emval_new(handle, argCount, argTypes) { + var args = parseParameters( + argCount, + argTypes, + Array.prototype.slice.call(arguments, 3)); + + // implement what amounts to operator new + var constructor = _emval_handle_array[handle].value; + function dummy(){} + dummy.prototype = constructor.prototype; + var obj = new constructor; + var rv = constructor.apply(obj, args); + if (typeof rv === 'object') { + obj = rv; + } + return __emval_register(obj); } // appease jshint (technically this code uses eval) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index 3952e008d..48f8ef694 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -20,7 +20,11 @@ namespace emscripten { EM_VAL _emval_new_cstring(const char*); void _emval_take_value(TYPEID type/*, ...*/); - EM_VAL _emval_new(EM_VAL value); + EM_VAL _emval_new( + EM_VAL value, + unsigned argCount, + internal::TYPEID argTypes[] + /*, ... */); EM_VAL _emval_get_global(const char* name); EM_VAL _emval_get_property(EM_VAL object, EM_VAL key); @@ -122,8 +126,25 @@ namespace emscripten { return val::global("Object")["prototype"]["hasOwnProperty"].call("call", *this, val(key)).as(); } - val new_() const { - return val(internal::_emval_new(handle)); + template + val new_(Args... args) const { + using namespace internal; + + WithPolicies<>::ArgTypeList argList; + // todo: this is awfully similar to operator(), can we + // merge them somehow? + typedef EM_VAL (*TypedNew)( + EM_VAL, + unsigned, + TYPEID argTypes[], + typename BindingType::WireType...); + TypedNew typedNew = reinterpret_cast(&_emval_new); + return val( + typedNew( + handle, + argList.count, + argList.types, + toWireType(args)...)); } template @@ -136,7 +157,7 @@ namespace emscripten { internal::_emval_set_property(handle, val(key).handle, v.handle); } - template + template val operator()(Args... args) { using namespace internal;