commonjs_cust_normalise_003.phpt 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. --TEST--
  2. Test V8Js::setModuleNormaliser : Custom normalisation #003
  3. --SKIPIF--
  4. <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
  5. --FILE--
  6. <?php
  7. $JS = <<< EOT
  8. var foo = require("./test");
  9. var bar = require("test");
  10. EOT;
  11. $v8 = new V8Js();
  12. // Caching is done based on the identifiers passed back
  13. // by the module normaliser. If it returns the same id
  14. // for multiple require calls, the module loader callback
  15. // will be called only once (as the others are cached)
  16. $v8->setModuleNormaliser(function($base, $module) {
  17. var_dump($base, $module);
  18. return [ "path/to", "test-foo" ];
  19. });
  20. $v8->setModuleLoader(function($module) {
  21. print("setModuleLoader called for ".$module."\n");
  22. if($module != "path/to/test-foo") {
  23. throw new \Exception("module caching fails");
  24. }
  25. return 'exports.bar = 23;';
  26. });
  27. $v8->executeString($JS, 'module.js');
  28. ?>
  29. ===EOF===
  30. --EXPECT--
  31. string(0) ""
  32. string(6) "./test"
  33. setModuleLoader called for path/to/test-foo
  34. string(0) ""
  35. string(4) "test"
  36. ===EOF===