Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalising bug #349

Closed
chenos opened this issue Jan 10, 2018 · 1 comment
Closed

Normalising bug #349

chenos opened this issue Jan 10, 2018 · 1 comment

Comments

@chenos
Copy link
Contributor

chenos commented Jan 10, 2018

--TEST--
Test V8Js::setModuleNormaliser : Custom normalisation #005
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php

$v8 = new V8Js();

$v8->setModuleNormaliser(function ($base, $moduleName) {
    var_dump($base, $moduleName);
    if ($base == '' && $moduleName == './tags') {
        return ['./tags', 'index.js'];
    }
    if ($base == './tags' && $moduleName == './if.js') {
        return ['./tags', 'if.js'];
    }
    return [$base, $moduleName];
});

$v8->setModuleLoader(function ($moduleName) {
    print("setModuleLoader called for ".$moduleName."\n");
    switch ($moduleName) {
        case './app.js':
            return "require('./tags')";
        case './tags/index.js':
            return "require('./if.js')";
    }
});

$v8->executeString("require('./app.js')");

echo "------------------------------------------------\n";

$v8 = new V8Js();

$v8->setModuleNormaliser(function ($base, $moduleName) {
    var_dump($base, $moduleName);
    if ($base == '' && $moduleName == './tags') {
        return ['./tags', 'index.js'];
    }
    if ($base == './tags' && $moduleName == './if.js') {
        return ['./tags', 'if.js'];
    }
    return [$base, $moduleName];
});

$v8->setModuleLoader(function ($moduleName) {
    print("setModuleLoader called for ".$moduleName."\n");
    switch ($moduleName) {
        case './app.js':
            return "require('./tags')()"; // different
        case './tags/index.js':
            return "module.exports = function() {require('./if.js')}"; // different
    }
});

$v8->executeString("require('./app.js')");

?>
===EOF===
--EXPECT--
string(0) ""
string(8) "./app.js"
setModuleLoader called for ./app.js
string(0) ""
string(6) "./tags"
setModuleLoader called for ./tags/index.js
string(6) "./tags"
string(7) "./if.js"
setModuleLoader called for ./tags/if.js
------------------------------------------------
string(0) ""
string(8) "./app.js"
setModuleLoader called for ./app.js
string(0) ""
string(6) "./tags"
setModuleLoader called for ./tags/index.js
string(6) "./tags"
string(7) "./if.js"
setModuleLoader called for ./tags/if.js
===EOF===

Two tests should be the same. But the second result is:

string(0) ""
string(7) "./if.js"
setModuleLoader called for ./if.js

./app.js

require('./tags')()

./tags/index.js require inside functions

module.exports = function() {
  require('./if.js')
}
@stesie
Copy link
Member

stesie commented Feb 1, 2018

Nice catch, that's there for ages :-)

Thanks for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants