0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-12-22 09:21:52 +00:00

Fix commonjs memory leaks (and increase test coverage to 100%)

This commit is contained in:
Stefan Siegl 2015-08-01 19:30:55 +02:00
parent bfc6b29989
commit ea3ec4bd65
6 changed files with 140 additions and 1 deletions

View File

@ -0,0 +1,23 @@
--TEST--
Test V8Js::setModuleLoader : Path normalisation #001
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$JS = <<< EOT
var foo = require("./test");
EOT;
$v8 = new V8Js();
$v8->setModuleLoader(function($module) {
print("setModuleLoader called for ".$module."\n");
return 'exports.bar = 23;';
});
$v8->executeString($JS, 'module.js');
?>
===EOF===
--EXPECT--
setModuleLoader called for test
===EOF===

View File

@ -0,0 +1,23 @@
--TEST--
Test V8Js::setModuleLoader : Path normalisation #002
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$JS = <<< EOT
var foo = require("../../../test");
EOT;
$v8 = new V8Js();
$v8->setModuleLoader(function($module) {
print("setModuleLoader called for ".$module."\n");
return 'exports.bar = 23;';
});
$v8->executeString($JS, 'module.js');
?>
===EOF===
--EXPECT--
setModuleLoader called for test
===EOF===

View File

@ -0,0 +1,25 @@
--TEST--
Test V8Js::setModuleLoader : Path normalisation #003
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$JS = <<< EOT
var foo = require("foo/test");
var foo = require("foo/bar/baz/test");
EOT;
$v8 = new V8Js();
$v8->setModuleLoader(function($module) {
print("setModuleLoader called for ".$module."\n");
return 'exports.bar = 23;';
});
$v8->executeString($JS, 'module.js');
?>
===EOF===
--EXPECT--
setModuleLoader called for foo/test
setModuleLoader called for foo/bar/baz/test
===EOF===

View File

@ -0,0 +1,30 @@
--TEST--
Test V8Js::setModuleLoader : Path normalisation #004
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$JS = <<< EOT
var foo = require("foo/test");
EOT;
$v8 = new V8Js();
$v8->setModuleLoader(function($module) {
print("setModuleLoader called for ".$module."\n");
switch($module) {
case 'foo/test':
return 'require("./blar");';
case 'foo/blar':
return 'exports.bar = 23;';
}
});
$v8->executeString($JS, 'module.js');
?>
===EOF===
--EXPECT--
setModuleLoader called for foo/test
setModuleLoader called for foo/blar
===EOF===

View File

@ -0,0 +1,30 @@
--TEST--
Test V8Js::setModuleLoader : Path normalisation #005
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$JS = <<< EOT
var foo = require("foo/test");
EOT;
$v8 = new V8Js();
$v8->setModuleLoader(function($module) {
print("setModuleLoader called for ".$module."\n");
switch($module) {
case 'foo/test':
return 'require("../blar");';
case 'blar':
return 'exports.bar = 23;';
}
});
$v8->executeString($JS, 'module.js');
?>
===EOF===
--EXPECT--
setModuleLoader called for foo/test
setModuleLoader called for blar
===EOF===

View File

@ -76,12 +76,19 @@ void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *norm
if (!strcmp(term, "..")) {
// Ignore parent term (..) if it's the first normalised term
if (normalised_terms.size() > 0) {
// Remove the parent normalized term
// Remove the parent normalized term (and free it)
efree(normalised_terms.back());
normalised_terms.pop_back();
}
// free the ".." term
efree(term);
} else if (strcmp(term, ".")) {
// Add the term if it's not the current term (.)
normalised_terms.push_back(term);
} else {
// Discard "." term
efree(term);
}
}
@ -102,5 +109,6 @@ void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *norm
}
strcat(normalised_path, term);
efree(term);
}
}