diff --git a/tests/commonjs_modules_caching.phpt b/tests/commonjs_modules_caching.phpt new file mode 100644 index 0000000..6efec77 --- /dev/null +++ b/tests/commonjs_modules_caching.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test V8Js::setModuleLoader : Returned modules are cached +--SKIPIF-- + +--FILE-- +setModuleLoader(function($module) { + print("setModuleLoader called for ".$module."\n"); + return 'exports.bar = 23;'; +}); + +$v8->executeString($JS, 'module.js'); +?> +===EOF=== +--EXPECT-- +setModuleLoader called for test +setModuleLoader called for test2 +===EOF=== diff --git a/v8js_class.cc b/v8js_class.cc index 924de4d..8a43a1c 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -156,6 +156,7 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ /* Clear persistent handles in module cache */ for (std::map::iterator it = c->modules_loaded.begin(); it != c->modules_loaded.end(); ++it) { + efree(it->first); it->second.Reset(); } c->modules_loaded.~map(); @@ -200,7 +201,7 @@ static zend_object_value v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */ new(&c->modules_stack) std::vector(); new(&c->modules_base) std::vector(); - new(&c->modules_loaded) std::map; + new(&c->modules_loaded) std::map; new(&c->template_cache) std::map(); new(&c->accessor_list) std::vector(); diff --git a/v8js_class.h b/v8js_class.h index 4500f99..aad8e96 100644 --- a/v8js_class.h +++ b/v8js_class.h @@ -23,6 +23,12 @@ typedef v8::Persistent > v8 struct v8js_v8object; struct v8js_accessor_ctx; +struct cmp_str { + bool operator()(char const *a, char const *b) const { + return strcmp(a, b) < 0; + } +}; + /* {{{ Context container */ struct v8js_ctx { zend_object std; @@ -43,7 +49,7 @@ struct v8js_ctx { zval *module_loader; std::vector modules_stack; std::vector modules_base; - std::map modules_loaded; + std::map modules_loaded; std::map template_cache; std::map weak_objects; diff --git a/v8js_methods.cc b/v8js_methods.cc index 48249af..371a01a 100644 --- a/v8js_methods.cc +++ b/v8js_methods.cc @@ -398,7 +398,6 @@ V8JS_METHOD(require) c->modules_loaded[normalised_module_id].Reset(isolate, newobj); info.GetReturnValue().Set(newobj); - efree(normalised_module_id); } void v8js_register_methods(v8::Handle global, v8js_ctx *c) /* {{{ */