mirror of
https://github.com/phpv8/v8js.git
synced 2024-12-22 03:01:51 +00:00
Fix commonjs memory leaks (and increase test coverage to 100%)
This commit is contained in:
parent
bfc6b29989
commit
ea3ec4bd65
23
tests/commonjs_normalise_001.phpt
Normal file
23
tests/commonjs_normalise_001.phpt
Normal 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===
|
23
tests/commonjs_normalise_002.phpt
Normal file
23
tests/commonjs_normalise_002.phpt
Normal 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===
|
25
tests/commonjs_normalise_003.phpt
Normal file
25
tests/commonjs_normalise_003.phpt
Normal 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===
|
30
tests/commonjs_normalise_004.phpt
Normal file
30
tests/commonjs_normalise_004.phpt
Normal 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===
|
30
tests/commonjs_normalise_005.phpt
Normal file
30
tests/commonjs_normalise_005.phpt
Normal 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===
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user