aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2013-03-26 12:39:57 -0700
committerJukka Jylänki <jujjyl@gmail.com>2013-04-12 14:26:41 +0300
commitdbd1a3bfe253ac1df0200427de10f60dc520cfb1 (patch)
tree56b1cbb492baaf9ce3843a31b67dd52b1e1b0fe8
parent7973873abff495fab9f9193e057a6c6c52871d05 (diff)
Allow multiple arguments to emscripten::val::new_()
-rwxr-xr-xsrc/embind/emval.js18
-rw-r--r--system/include/emscripten/val.h29
2 files changed, 41 insertions, 6 deletions
diff --git a/src/embind/emval.js b/src/embind/emval.js
index c60c62e0..eb31777b 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 3952e008..48f8ef69 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<bool>();
}
- val new_() const {
- return val(internal::_emval_new(handle));
+ template<typename... Args>
+ val new_(Args... args) const {
+ using namespace internal;
+
+ WithPolicies<>::ArgTypeList<Args...> argList;
+ // todo: this is awfully similar to operator(), can we
+ // merge them somehow?
+ typedef EM_VAL (*TypedNew)(
+ EM_VAL,
+ unsigned,
+ TYPEID argTypes[],
+ typename BindingType<Args>::WireType...);
+ TypedNew typedNew = reinterpret_cast<TypedNew>(&_emval_new);
+ return val(
+ typedNew(
+ handle,
+ argList.count,
+ argList.types,
+ toWireType(args)...));
}
template<typename T>
@@ -136,7 +157,7 @@ namespace emscripten {
internal::_emval_set_property(handle, val(key).handle, v.handle);
}
- template<typename ...Args>
+ template<typename... Args>
val operator()(Args... args) {
using namespace internal;