소스 검색

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

Stefan Siegl 9 년 전
부모
커밋
ea3ec4bd65
6개의 변경된 파일140개의 추가작업 그리고 1개의 파일을 삭제
  1. 23 0
      tests/commonjs_normalise_001.phpt
  2. 23 0
      tests/commonjs_normalise_002.phpt
  3. 25 0
      tests/commonjs_normalise_003.phpt
  4. 30 0
      tests/commonjs_normalise_004.phpt
  5. 30 0
      tests/commonjs_normalise_005.phpt
  6. 9 1
      v8js_commonjs.cc

+ 23 - 0
tests/commonjs_normalise_001.phpt

@@ -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 - 0
tests/commonjs_normalise_002.phpt

@@ -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 - 0
tests/commonjs_normalise_003.phpt

@@ -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 - 0
tests/commonjs_normalise_004.phpt

@@ -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 - 0
tests/commonjs_normalise_005.phpt

@@ -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===

+ 9 - 1
v8js_commonjs.cc

@@ -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);
     }
 }