Skip to content

Commit 71f4b7b

Browse files
author
Stefan Siegl
committed
Cache pseudo-Array FunctionTemplate as well
1 parent 31d1feb commit 71f4b7b

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

v8js_class.cc

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */
108108
c->object_name.~Persistent();
109109
c->global_template.Reset();
110110
c->global_template.~Persistent();
111+
c->array_tmpl.Reset();
112+
c->array_tmpl.~Persistent();
111113

112114
/* Clear persistent call_impl & method_tmpls templates */
113115
for (std::map<v8js_tmpl_t *, v8js_tmpl_t>::iterator it = c->call_impls.begin();
@@ -221,6 +223,7 @@ static zend_object_value v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
221223
new(&c->object_name) v8::Persistent<v8::String>();
222224
new(&c->context) v8::Persistent<v8::Context>();
223225
new(&c->global_template) v8::Persistent<v8::FunctionTemplate>();
226+
new(&c->array_tmpl) v8::Persistent<v8::FunctionTemplate>();
224227

225228
new(&c->modules_stack) std::vector<char*>();
226229
new(&c->modules_base) std::vector<char*>();

v8js_class.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ struct v8js_ctx {
4545
long memory_limit;
4646
bool memory_limit_hit;
4747

48-
v8::Persistent<v8::FunctionTemplate> global_template;
48+
v8js_tmpl_t global_template;
49+
v8js_tmpl_t array_tmpl;
4950

5051
zval *module_loader;
5152
std::vector<char *> modules_stack;

v8js_object_export.cc

+15-5
Original file line numberDiff line numberDiff line change
@@ -901,12 +901,22 @@ static v8::Handle<v8::Object> v8js_wrap_array_to_object(v8::Isolate *isolate, zv
901901
uint key_len;
902902
ulong index;
903903

904-
// @todo re-use template likewise
905-
v8::Local<v8::FunctionTemplate> new_tpl = v8::FunctionTemplate::New(isolate, 0);
904+
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
905+
v8::Local<v8::FunctionTemplate> new_tpl;
906+
907+
if(ctx->array_tmpl.IsEmpty()) {
908+
new_tpl = v8::FunctionTemplate::New(isolate, 0);
906909

907-
/* Call it Array, but it is not a native array, especially it doesn't have
908-
* have the typical Array.prototype functions. */
909-
new_tpl->SetClassName(V8JS_SYM("Array"));
910+
/* Call it Array, but it is not a native array, especially it doesn't have
911+
* have the typical Array.prototype functions. */
912+
new_tpl->SetClassName(V8JS_SYM("Array"));
913+
914+
/* Store for later re-use */
915+
ctx->array_tmpl.Reset(isolate, new_tpl);
916+
}
917+
else {
918+
new_tpl = v8::Local<v8::FunctionTemplate>::New(isolate, ctx->array_tmpl);
919+
}
910920

911921
v8::Handle<v8::Object> newobj = new_tpl->InstanceTemplate()->NewInstance();
912922

0 commit comments

Comments
 (0)