Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何实施;JS';“我们需要”;函数在perl';那是什么发动机?_Javascript_Node.js_Perl - Fatal编程技术网

Javascript 如何实施;JS';“我们需要”;函数在perl';那是什么发动机?

Javascript 如何实施;JS';“我们需要”;函数在perl';那是什么发动机?,javascript,node.js,perl,Javascript,Node.js,Perl,这是一个很好的JavaScript引擎,用纯perl编写。完美地执行常见的JS代码。他将perl子例程绑定到JS函数的能力非常棒 缺少require函数的是什么,在“node.js”中实现了什么,并执行与perl所需类似的任务 很高兴知道如何在perl中实现该函数并将其绑定到JE,例如在简单的框架中: use 5.016; use warnings; use Path::Tiny; use JE::Destroyer; use JE; my $jslib = path("./jslib");

这是一个很好的JavaScript引擎,用纯perl编写。完美地执行常见的JS代码。他将perl子例程绑定到JS函数的能力非常棒

缺少
require
函数的是什么,在“node.js”中实现了什么,并执行与perl所需类似的任务

很高兴知道如何在perl中实现该函数并将其绑定到JE,例如在简单的框架中:

use 5.016;
use warnings;
use Path::Tiny;

use JE::Destroyer;
use JE;

my $jslib = path("./jslib");

my $j = new JE;
$j->new_function("say", sub { say @_ });  # the "say" in the JS
$j->new_function("require", sub {         # the "require"

    my $source = $jslib->child($_[0])->slurp_utf8; #read the source from some file
    #how to implement the require ?
    #e.g. what JE object should be created and what to bind to it?

});
你知道如何在C语言中实现node.js
require
函数,以及如何在perl中实现它吗

编辑 有关更精确的示例,请添加更扩展的骨架:

use 5.016;
use warnings;
use Path::Tiny;

use JE::Destroyer;
use JE;

my $jslib = path("./jslib");

my $j = new JE;
$j->new_function("say", sub { say @_ });  # the "say" in the JS
$j->new_function("require", sub {         # the "require"

    my $source = $jslib->child($_[0])->slurp_utf8; #read the source from some file
    #how to implement the require ?
    #e.g. what JE object should be created and what to bind to it?

});
  • 文件
    /reqtest.js
    (主程序)
  • 文件
    /node\u modules/adder.js
    (在“node\u modules”中-用于
    node.js
    测试)
印刷品

start
5
因此,它正确地需要
adder.js
并返回了对象,但是 使用以下perl/JE脚本运行它:

use 5.016;
use warnings;
use JE::Destroyer;
use JE;
use Scalar::Util qw( weaken );
use Path::Tiny;
use PIR;

my $script = path("reqtest.js")->slurp_utf8;

my $j = new JE;
$j->{console} = {};
$j->{console}->new_function("log", sub { say @_ });
{
    weaken(my $j = $j);
    $j->new_function(require => sub {
        my $source = get_source($_[0]);
        $j->eval($source) if $source;
    });
}

$j->eval($script);#eval the main script

JE::Destroyer::destroy($j);
undef $j;

#extremelly simplyfied
sub get_source {
    my $name = shift;
    return path("node_modules")->child("$name.js")->slurp_utf8;
}
仅打印:

start

因此,
require
的“simple
$j->eval($source)
实现没有遵循
CommonJS
模块规范-它需要更多的东西。

要执行
require.js
,您可以使用

my $require_js = get('http://requirejs.org/docs/release/2.1.18/comments/require.js');

$j->eval($require_js);

经过一点研究,我创建了以下(一个极其简化的)解决方案:

use 5.016;
use warnings;
use JE::Destroyer;
use JE;
use Scalar::Util qw( weaken );
use Path::Tiny;

my $script = path("reqtest.js")->slurp_utf8;

my $j = new JE;
$j->{console} = {};
$j->{console}->new_function("log", sub { say @_ });
{
    weaken(my $j = $j);   #based on ikegami's previous answer
    $j->new_function(require => sub {
        my $source = get_source($_[0]);
        my $code = new JE::Object::Function $j, qw(exports), $source;
        my $exports = $j->{Object};
        $code->($exports);
        return $exports;
    });
}

$j->eval($script);#eval the main script

JE::Destroyer::destroy($j);
undef $j;

# extremelly simplyfied code search
# the real code-search for the requide("some") should try the following
# some.js some/index.js some.json some/index.json
# for the defined search-paths.
sub get_source {
    my $name = shift;
    return path("node_modules")->child("$name.js")->slurp_utf8;
}
上面是CommonJS require的最短可能实现,并且打印的内容与问题示例中的
node.js
相同

start
5

当然,需要做很多类似的额外工作(例如代码缓存),但最简单的框架是可行的。

出于纯粹的好奇,您的用例是什么?我绝不会想象尝试使用Perl编写的JavaScript引擎来完成比“哇!它起作用了!“但我的想象力相当有限:)@Alex Tokarev,废弃使用JS@ikegami:如果这是您的用例,那么您不需要
require()
函数,它只存在于服务器中。网页中的Javascript不能使用
require
函数,因为它没有在浏览器中实现。@Alex Tokarev,你把我和OP混淆了,你说你无法想象任何用例,所以我给出了一个(那不一定是OP的).@AndyRay-JE与任何解释的模板语言处于相同的位置,例如或任何更复杂的简单标记替换,并允许循环、条件等。或者,如果您愿意,它就像是用JavaScript编写的。它只是一种用perl解释的语言,perl是解决此类问题的正确工具这是一个正确的工作。你没有提到想要
require
返回一个对象。你想让它返回什么对象?JS没有
require
函数。你希望你的函数有什么输入和输出?这与你最初要求的完全不同,但是如果你想运行CommonJS的
requiree、 js
,将其传递给
eval
。更新了我的答案。
use 5.016;
use warnings;
use JE::Destroyer;
use JE;
use Scalar::Util qw( weaken );
use Path::Tiny;

my $script = path("reqtest.js")->slurp_utf8;

my $j = new JE;
$j->{console} = {};
$j->{console}->new_function("log", sub { say @_ });
{
    weaken(my $j = $j);   #based on ikegami's previous answer
    $j->new_function(require => sub {
        my $source = get_source($_[0]);
        my $code = new JE::Object::Function $j, qw(exports), $source;
        my $exports = $j->{Object};
        $code->($exports);
        return $exports;
    });
}

$j->eval($script);#eval the main script

JE::Destroyer::destroy($j);
undef $j;

# extremelly simplyfied code search
# the real code-search for the requide("some") should try the following
# some.js some/index.js some.json some/index.json
# for the defined search-paths.
sub get_source {
    my $name = shift;
    return path("node_modules")->child("$name.js")->slurp_utf8;
}
start
5