JavaScript::V8::CommonJS - Modules/1.0 for JavaScript::V8
use JavaScript::V8::CommonJS; my $js = JavaScript::V8::CommonJS->new(paths => ["./modules"]); print $js->eval('require("foo").add(4, 2)'); # prints 6 # modules/foo.js # exports.add = function(a, b) { return a + b }
CommonJS implementation for JavaScript::V8. Currently only Module/1.0 spec is implemented. (Passing all unit tests at https://github.com/commonjs/commonjs/tree/master/tests/modules/1.0)
All arguments are optional.
Arrayref of paths to search for modules. Default: [getcwd()].
Hashref of native modules. Default: {}.
Register native modules. Attempting to register a module twice is a fatal error.
$js->add_module( http => { get => sub { ... }, post => sub { ... }, ... });
Evaluates javascript source code on the global context. JS exceptions are rethrown as JavaScript::V8::CommonJS::Exception instances.
$js->eval('require("program").doSomething()', "main")
The second argument is a source or filename to be reported on error messages.
$js->eval_file("main.js")
Returns the JavaScript::V8::Context instance.
# run v8 garbage collector $js->c->idle_notification
Copyright (C) Carlos Fernando Avila Gratz.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Carlos Fernando Avila Gratz <cafe@kreato.com.br>
To install JavaScript::V8::CommonJS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JavaScript::V8::CommonJS
CPAN shell
perl -MCPAN -e shell install JavaScript::V8::CommonJS
For more information on module installation, please visit the detailed CPAN module installation guide.