diff --git a/v8js_class.cc b/v8js_class.cc index ae032ac..583466a 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -108,6 +108,8 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ c->object_name.~Persistent(); c->global_template.Reset(); c->global_template.~Persistent(); + c->array_tmpl.Reset(); + c->array_tmpl.~Persistent(); /* Clear persistent call_impl & method_tmpls templates */ for (std::map::iterator it = c->call_impls.begin(); @@ -221,6 +223,7 @@ static zend_object_value v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */ new(&c->object_name) v8::Persistent(); new(&c->context) v8::Persistent(); new(&c->global_template) v8::Persistent(); + new(&c->array_tmpl) v8::Persistent(); new(&c->modules_stack) std::vector(); new(&c->modules_base) std::vector(); diff --git a/v8js_class.h b/v8js_class.h index 5880cda..07b7b8a 100644 --- a/v8js_class.h +++ b/v8js_class.h @@ -45,7 +45,8 @@ struct v8js_ctx { long memory_limit; bool memory_limit_hit; - v8::Persistent global_template; + v8js_tmpl_t global_template; + v8js_tmpl_t array_tmpl; zval *module_loader; std::vector modules_stack; diff --git a/v8js_object_export.cc b/v8js_object_export.cc index 21b1637..8d447c6 100644 --- a/v8js_object_export.cc +++ b/v8js_object_export.cc @@ -901,12 +901,22 @@ static v8::Handle v8js_wrap_array_to_object(v8::Isolate *isolate, zv uint key_len; ulong index; - // @todo re-use template likewise - v8::Local new_tpl = v8::FunctionTemplate::New(isolate, 0); + v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0); + v8::Local new_tpl; - /* Call it Array, but it is not a native array, especially it doesn't have - * have the typical Array.prototype functions. */ - new_tpl->SetClassName(V8JS_SYM("Array")); + if(ctx->array_tmpl.IsEmpty()) { + new_tpl = v8::FunctionTemplate::New(isolate, 0); + + /* Call it Array, but it is not a native array, especially it doesn't have + * have the typical Array.prototype functions. */ + new_tpl->SetClassName(V8JS_SYM("Array")); + + /* Store for later re-use */ + ctx->array_tmpl.Reset(isolate, new_tpl); + } + else { + new_tpl = v8::Local::New(isolate, ctx->array_tmpl); + } v8::Handle newobj = new_tpl->InstanceTemplate()->NewInstance();