blib/lib/JavaScript/QuickJS.pm

filter: Statementstmt 100% 15 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 20 / 20 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« quickjs/quickjs.h ↑ Summary easyxs/easyxs_structref.h »
1 package JavaScript::QuickJS;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 JavaScript::QuickJS - Run JavaScript via L<QuickJS|https://bellard.org/quickjs> in Perl
11  
12 =head1 SYNOPSIS
13  
14 Quick and dirty …
15  
16 my $val = JavaScript::QuickJS->new()->eval( q<
17 console.log("Hello, Perl!");
18 [ "The", "last", "value", "is", "returned." ];
19 > );
20  
21 =head1 DESCRIPTION
22  
23 This library embeds Fabrice Bellard’s L<QuickJS|https://bellard.org/quickjs>
24 engine into a Perl XS module. You can thus run JavaScript
25 (L<ES2020|https://tc39.github.io/ecma262/>) specification) directly in your
26 Perl programs.
27  
28 This distribution includes all needed C code; unlike with most XS modules
29 that interface with C libraries, you don’t need QuickJS pre-installed on
30 your system.
31  
32 =cut
33  
34 # ----------------------------------------------------------------------
35  
36 4 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 our $VERSION = '0.01';
39  
40 4 XSLoader::load( __PACKAGE__, $VERSION );
41  
42 # ----------------------------------------------------------------------
43  
44 =head1 METHODS
45  
46 For now only a static interface is provided:
47  
48 =head2 $obj = I<CLASS>->new()
49  
50 Instantiates I<CLASS>.
51  
52 =head2 $obj = I<OBJ>->set_globals( NAME1 => VALUE1, .. )
53  
54 Sets 1 or more globals in I<OBJ>. See below for details on type conversions
55 from Perl to JavaScript.
56  
57 =head2 $obj = I<OBJ>->helpers()
58  
59 Defines QuickJS’s “helpers”, e.g., C<console.log>.
60  
61 =head2 $obj = I<OBJ>->std()
62  
63 Enables (but does I<not> import) QuickJS’s C<std> module.
64  
65 =head2 $obj = I<OBJ>->os()
66  
67 Like C<std()> but for QuickJS’s C<os> module.
68  
69 =head2 $VALUE = eval( $JS_CODE )
70  
71 Comparable to running C<qjs -e '...'>. Returns the last value from $JS_CODE;
72 see below for details on type conversions from JavaScript to Perl.
73  
74 Untrapped exceptions in JavaScript will be rethrown as Perl exceptions.
75  
76 =head2 eval_module( $JS_CODE )
77  
78 Runs $JS_CODE as a module, which enables ES6 module syntax.
79 Note that no values can be returned directly in this mode of execution.
80  
81 =cut
82  
83 # ----------------------------------------------------------------------
84  
85 =head1 TYPE CONVERSION: JAVASCRIPT → PERL
86  
87 This module converts returned values from JavaScript thus:
88  
89 =over
90  
91 =item * JS string primitives become I<character> strings in Perl.
92  
93 =item * JS number & boolean primitives become corresponding Perl values.
94  
95 =item * JS null & undefined become Perl undef.
96  
97 =item * JS objects …
98  
99 =over
100  
101 =item * Arrays become Perl array references.
102  
103 =item * “Plain” objects become Perl hash references.
104  
105 =item * Functions become Perl code references.
106  
107 =item * Behaviour is B<UNDEFINED> for other object types.
108  
109 =back
110  
111 =back
112  
113 =head1 TYPE CONVERSION: PERL → JAVASCRIPT
114  
115 Generally speaking, it’s the inverse of JS → Perl, though since Perl doesn’t
116 differentiate “numeric strings” from “numbers” there’s occasional ambiguity.
117 In such cases, behavior is undefined; be sure to typecast in JavaScript
118 accordingly.
119  
120 =over
121  
122 =item * Perl strings, numbers, & booleans become corresponding JavaScript
123 primitives.
124  
125 =item * Perl undef becomes JS null.
126  
127 =item * Unblessed array & hash references become JavaScript arrays and
128 “plain” objects.
129  
130 =item * Perl code references become JavaScript functions.
131  
132 =item * Anything else triggers an exception.
133  
134 =back
135  
136 =head1 PLATFORM NOTES
137  
138 Due to QuickJS limitations, Linux & macOS are the only platforms known
139 to work “out-of-the-box”. Other POSIX OSes I<should> work with some small
140 tweaks to quickjs; see the compiler errors and F<quickjs.c> for more
141 details.
142  
143 Pull requests to improve portability are welcome!
144  
145 =head1 SEE ALSO
146  
147 Other JavaScript modules on CPAN include:
148  
149 =over
150  
151 =item * L<JavaScript::Duktape::XS> and L<JavaScript::Duktape> make the
152 L<Duktape|https://duktape.org> library available to Perl. They’re similar to
153 this library, but Duktape itself (as of this writing) lacks support for
154 several JavaScript constructs that QuickJS supports. (It’s also slower.)
155  
156 =item * L<JavaScript::V8> and L<JavaScript::V8::XS> expose Google’s
157 L<V8|https://v8.dev> library to Perl. Neither seems to support current
158 V8 versions.
159  
160 =item * L<JE> is a pure-Perl (!) JavaScript engine.
161  
162 =item * L<JavaScript> and L<JavaScript::Lite> expose Mozilla’s
163 L<SpiderMonkey|https://spidermonkey.dev/> engine to Perl.
164  
165 =back
166  
167 =head1 LICENSE & COPYRIGHT
168  
169 Copyright 2022 Gasper Software Consulting.
170  
171 This library is licensed under the same terms as Perl itself.
172  
173 =cut
174  
175 #----------------------------------------------------------------------
176  
177 sub _wrap_jsfunc {
178 1 ▶ my $jsfunc_obj = $_[0];
Subroutine _wrap_jsfunc: called CC=1 SCAR=0.0
179 1 ▶ return sub { $jsfunc_obj->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
180 }
181  
182 4 1;
« quickjs/quickjs.h ↑ Summary easyxs/easyxs_structref.h »