Allow multiple arguments to emscripten::val::new_()

This commit is contained in:
Chad Austin 2013-03-26 12:39:57 -07:00 коммит произвёл Jukka Jylänki
Родитель 7973873abf
Коммит dbd1a3bfe2
2 изменённых файлов: 41 добавлений и 6 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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;