From aff8d9e716a1953ed1526b3f8ce8455850f780da Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 11 Feb 2013 17:30:04 +0100 Subject: [PATCH] node: don't malloc in FromConstructorTemplate * allocate space for argv on the stack * move the declaration to node_internals.h --- src/node.cc | 20 ++++++-------------- src/node.h | 12 ------------ src/node_internals.h | 4 ++++ 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/node.cc b/src/node.cc index b88639f706..21d778abce 100644 --- a/src/node.cc +++ b/src/node.cc @@ -863,22 +863,14 @@ Local WinapiErrnoException(int errorno, #endif -Handle FromConstructorTemplate(Persistent& t, +Handle FromConstructorTemplate(Persistent t, const Arguments& args) { HandleScope scope; - - const int argc = args.Length(); - Local* argv = new Local[argc]; - - for (int i = 0; i < argc; ++i) { - argv[i] = args[i]; - } - - Local instance = t->GetFunction()->NewInstance(argc, argv); - - delete[] argv; - - return scope.Close(instance); + Local argv[32]; + unsigned argc = args.Length(); + if (argc > ARRAY_SIZE(argv)) argc = ARRAY_SIZE(argv); + for (unsigned i = 0; i < argc; ++i) argv[i] = args[i]; + return scope.Close(t->GetFunction()->NewInstance(argc, argv)); } diff --git a/src/node.h b/src/node.h index 682b505832..5769a7b9ce 100644 --- a/src/node.h +++ b/src/node.h @@ -150,18 +150,6 @@ NODE_EXTERN ssize_t DecodeWrite(char *buf, v8::Local BuildStatsObject(const uv_statbuf_t* s); -/** - * Call this when your constructor is invoked as a regular function, e.g. - * Buffer(10) instead of new Buffer(10). - * @param constructorTemplate Constructor template to instantiate from. - * @param args The arguments object passed to your constructor. - * @see v8::Arguments::IsConstructCall - */ -v8::Handle FromConstructorTemplate( - v8::Persistent& constructorTemplate, - const v8::Arguments& args); - - static inline v8::Persistent* cb_persist( const v8::Local &v) { v8::Persistent *fn = new v8::Persistent(); diff --git a/src/node_internals.h b/src/node_internals.h index f57f226cc1..5c568c0cd0 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -97,6 +97,10 @@ inline static v8::Handle ThrowRangeError(const char* errmsg) { abort(); \ } +v8::Handle FromConstructorTemplate( + v8::Persistent t, + const v8::Arguments& args); + } // namespace node #endif // SRC_NODE_INTERNALS_H_