GithubHelp home page GithubHelp logo

mamod / javascript-duktape Goto Github PK

View Code? Open in Web Editor NEW
8.0 8.0 4.0 3.29 MB

Perl interface to Duktape embeddable javascript engine

License: MIT License

Perl 2.36% C 44.76% HTML 15.62% JavaScript 35.83% C++ 1.41% Makefile 0.01% Perl 6 0.02%

javascript-duktape's People

Contributors

mamod avatar rodrigolive avatar vasslabs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javascript-duktape's Issues

Something got broken...

I just tried to install JavsScript::Duktape again in some new machine, using plenv with perl 5.24.0-RC1. The JavsScript::Duktape is got installed OK without any errors nor warnings.

But when tried the example code, from the Synopsys: e.g.

use strict;
use warnings;
use JavaScript::Duktape;

##create new js context
my $js = JavaScript::Duktape->new();

#set function to be used from javascript land
$js->set('write' => sub {
    my $duk = shift;
    print $_[0], "\n";
});

$js->eval(qq~
        for (var i = 0; i < 100; i++){
            write(i);
        }
~);

It prints only 100 empty lines and got100 warnings:

Use of uninitialized value in print at d line 11.

so the print $_[0], "\n"; isn't works.

Could be this somewhat perl-version specific, or something is changed in the JavaScript::Duktape? (In such case probably need change the example code in the Synopsys).

Any idea how to make the above code work again with perl 5.24.0-RC1?

alert and print are undocumented

I discovered by accident that JavaScript::Duktape installs ‘alert’ and ‘print’ functions into the JavaScript environment without documenting them at all.

This should be documented, so that someone trying to run JavaScript code in a restricted environment will not be providing JS code with unintended capabilities.

An option to disable these bindings would also be nice.

The underscore function is automatically global

All Perl symbols that consist of punctuation only are automatically global. That means that you do not have to export _().

I think that this behavior should at least be documented. And maybe you can think of a better function name so that the global namespace does not get polluted.

If you are searching for another function name, please keep in mind that the double underscore __() is already exported by https://metacpan.org/pod/Locale::TextDomain and in relatively wide use.

t/dll.t may segfault

On some of my smoker systems t/dll.t just fails:

t/dll.t ........................... 
No subtests run 

The test summary suggests that a segmenation fault happened here:

t/dll.t                         (Wstat: 11 Tests: 0 Failed: 0)
  Non-zero wait status: 11

I don't see a pattern in the affected perl versions or operating systems --- I see fails for some combinations of perl 5.22.1 .. 5.26.0 and debian jessie, debian stretch, CentOS7, Ubuntu 16.04.

Installation fails on OS X/perlbrew

Hello, this is THE MODULE for what I looking for long time. Would be nice to get it work on OS X. ;)
The "make test" failed. Attached the output from the prove -bv and the perl information.

$ prove -bv
t/perl-data.t .. 
{"f":false,"num":9,"n":null,"t":true,"str":"Hello"}
ok 1 - success from javascript land
ok 2 - success from javascript land
ok 3 - success from javascript land
ok 4 - success from javascript land
ok 5 - success from javascript land
ok 6 - success from javascript land
ok 7
ok 8
ok 9
ok 10
ok 11
ok 12
ok 13
ok 14 - success from javascript land
ok 15 - success from javascript land
ok 16 - success from javascript land
Duktape (top=1): undefined
1..16
Destroying 0x7fe3ba0163a0
ok
t/prop.t ....... 
ok 1 - prop value
ok 2 - return value
1..2
Destroying 0x7fcf404a2720
ok
t/safe-call.t .. 
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6 - Error: From Perl at t/safe-call.t line 26.
# 
ok 7
ok 8
ok 9
Assertion failed: (PL_top_env == &cur_env), function perl_duk_safe_call, file Vm_6e63f.xs, line 144.
All 9 subtests passed 
t/trycatch.t ... 
ok 1
ok 2
ok 3
ok 4 - last stack element should be an error
Assertion failed: (PL_top_env == &cur_env), function perl_duk_safe_call, file Vm_6e63f.xs, line 144.
All 4 subtests passed 
t/uncaught.t ... 
ok 1 - duktape uncaught error at t/uncaught.t line 26.
# 
ok 2 - called once
ok 3 - Error On Top
1 (top=0):
ok 4 - duktape uncaught error at t/uncaught.t line 46.
# 
ok 5 - called once
ok 6 - Error on Top
2 (top=0):
ok 7 - 
ok 8 - called once
ok 9 - Error is on top
ok 10 - 
ok 11 - called once
ok 12 - Error is on top
Duktape (top=1): Error: Died at t/uncaught.t line 18.

ok 13 - Error: Died at t/uncaught.t line 18.
# 
Assertion failed: (PL_top_env == &cur_env), function perl_duk_safe_call, file Vm_6e63f.xs, line 144.
All 13 subtests passed 
t/use.t ........ 
ok 1
1..1
Destroying 0x7f8070e51a40
ok

Test Summary Report
-------------------
t/safe-call.t (Wstat: 6 Tests: 9 Failed: 0)
  Non-zero wait status: 6
  Parse errors: No plan found in TAP output
t/trycatch.t (Wstat: 6 Tests: 4 Failed: 0)
  Non-zero wait status: 6
  Parse errors: No plan found in TAP output
t/uncaught.t (Wstat: 6 Tests: 13 Failed: 0)
  Non-zero wait status: 6
  Parse errors: No plan found in TAP output
Files=6, Tests=45, 32 wallclock secs ( 0.07 usr  0.03 sys + 27.41 cusr  1.16 csys = 28.67 CPU)
Result: FAIL

The system:
Darwin tango.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64

Perl:

$ perl -V
Summary of my perl5 (revision 5 version 20 subversion 2) configuration:

  Platform:
    osname=darwin, osvers=14.3.0, archname=darwin-thread-multi-2level
    uname='darwin tango.local 14.3.0 darwin kernel version 14.3.0: mon mar 23 11:59:05 pdt 2015; root:xnu-2782.20.48~5release_x86_64 x86_64 '
    config_args='-de -Dprefix=/Users/clt/perl5/perlbrew/perls/perl-5.20.2 -Dcc=clang -Duse64bitall -Dusethreads -DDEBUGGING -Dusemultiplicity -Accflags=-DPERL_USE_SAFE_PUTENV -Aeval:scriptdir=/Users/clt/perl5/perlbrew/perls/perl-5.20.2/bin'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='clang', ccflags ='-fno-common -DPERL_DARWIN -no-cpp-precomp -arch x86_64 -DPERL_USE_SAFE_PUTENV -DDEBUGGING -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include',
    optimize='-O3 -g',
    cppflags='-no-cpp-precomp -arch x86_64 -fno-common -DPERL_DARWIN -no-cpp-precomp -arch x86_64 -DPERL_USE_SAFE_PUTENV -DDEBUGGING -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include'
    ccversion='', gccversion='4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='env MACOSX_DEPLOYMENT_TARGET=10.3 clang -arch x86_64', ldflags =' -arch x86_64 -fstack-protector -L/usr/local/lib -L/opt/local/lib'
    libpth=/usr/local/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib /usr/lib /opt/local/lib
    libs=-lgdbm -ldbm -ldl -lm -lutil -lc
    perllibs=-ldl -lm -lutil -lc
    libc=, so=dylib, useshrplib=false, libperl=libperl.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup -L/usr/local/lib -L/opt/local/lib -fstack-protector'


Characteristics of this binary (from libperl): 
  Compile-time options: DEBUGGING HAS_TIMES MULTIPLICITY PERLIO_LAYERS
                        PERL_DONT_CREATE_GVSV
                        PERL_HASH_FUNC_ONE_AT_A_TIME_HARD
                        PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP
                        PERL_NEW_COPY_ON_WRITE PERL_PRESERVE_IVUV
                        PERL_TRACK_MEMPOOL PERL_USE_SAFE_PUTENV
                        USE_64_BIT_ALL USE_64_BIT_INT USE_ITHREADS
                        USE_LARGE_FILES USE_LOCALE USE_LOCALE_COLLATE
                        USE_LOCALE_CTYPE USE_LOCALE_NUMERIC USE_PERLIO
                        USE_PERL_ATOF USE_REENTRANT_API
  Built under darwin
  Compiled at May 13 2015 21:55:38
  %ENV:
    PERLBREW_BASHRC_VERSION="0.73"
    PERLBREW_HOME="/Users/clt/.perlbrew"
    PERLBREW_MANPATH="/Users/clt/perl5/perlbrew/perls/perl-5.20.2/man"
    PERLBREW_PATH="/Users/clt/perl5/perlbrew/bin:/Users/clt/perl5/perlbrew/perls/perl-5.20.2/bin"
    PERLBREW_PERL="perl-5.20.2"
    PERLBREW_ROOT="/Users/clt/perl5/perlbrew"
    PERLBREW_VERSION="0.73"
  @INC:
    /Users/clt/perl5/perlbrew/perls/perl-5.20.2/lib/site_perl/5.20.2/darwin-thread-multi-2level
    /Users/clt/perl5/perlbrew/perls/perl-5.20.2/lib/site_perl/5.20.2
    /Users/clt/perl5/perlbrew/perls/perl-5.20.2/lib/5.20.2/darwin-thread-multi-2level
    /Users/clt/perl5/perlbrew/perls/perl-5.20.2/lib/5.20.2
    .

Unicode problems.

Would be nice if you could provide an example how to use unicode with JavaScript::Duktape. Here is an example code in compare with the JE.

use 5.018;
use warnings;
use utf8;
binmode STDOUT, ':utf8';

my $evalstr = <<'//JSEND';
        function uc(s) { return s.toUpperCase(); }
        write( uc("abc αβγ ß") ) // ascii: abc,  greek:alpha beta gamma, german:sharp-s
//JSEND

#JE version
#----------------
use JE;
my $je = JE->new();
$je->new_function( write =>
    sub {
        say "JE : résúlt-", @_;
    }
);
$je->eval($evalstr);

# Duktape version
#----------------
use JavaScript::Duktape;
my $jd = JavaScript::Duktape->new();
$jd->set(write =>
    sub {
        my $duk = shift;
        say "DUK: résúlt-", @_;
    }
);
$jd->eval($evalstr);

it prints:

JE : résúlt-ABC ΑΒΓ SS
DUK: résúlt-ABC Î�Î�Î� SS

e.g the JE prints what I want. What is the correct way using unicode with JavaScript::Duktape?

Please consider switching away from Inline::C

The problem with using Inline::C, rather than compiling the C code at installation time, is that multiple users all end up with their own copies of the compiled C code in their home folders. (And Inline does not have the courtesy to use a hidden file name beginning with a dot!) Also, if I happen to upgrade Inline::C, then JavaScript::Duktape recompiles its C code all over again, which can be quite annoying if I am actually trying to get some work done. :-)

Compilation error on FreeBSD systems (related to memcpy?)

The test suite fails on FreeBSD systems (versions 13 .. 15):

PERL_DL_NONLAZY=1 "/usr/perl5.38.2/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/bool.t t/buffer.t t/circular-push.t t/circular-to-perl.t t/circular.t t/deep-objects.t t/dll.t t/eval-function.t t/json.t t/nested.t t/number-or-string.t t/perl-data.t t/pointer.t t/prop.t t/safe-call.t t/trycatch.t t/typescript.t t/uncaught.t t/undefined.t t/use.t t/utf8.t t/api/base64.t t/api/c-constructor.t t/api/call-prop.t t/api/charcodeat.t t/api/check-type.t t/api/compile.t t/api/dev-cfunc-name.t t/api/dup.t t/api/eval-string.t t/api/hex.t t/api/internal-key-access.t t/api/internal-property-basics.t t/api/multithread.t t/api/pcall-method.t t/api/pcall-perl-function.t t/api/pcall-prop.t t/api/pcall.t t/api/pcompile-nofile.t t/api/push_this.t t/api/require-lstring.t t/api/safe-call.t t/api/safe-to-string.t t/api/symbol-getownpropertysymbols.t t/objects/01.t t/objects/02.t t/objects/03.t t/objects/buffer.t t/objects/string.t t/objects/this.t t/sandbox/00-memory.t t/sandbox/01-memory.t t/sandbox/02-memory.t t/sandbox/bubble-timeout.t t/sandbox/check.t t/sandbox/timeout.t
Running Mkbootstrap for Vm_104e ()
chmod 644 "Vm_104e.bs"
"/usr/perl5.38.2/bin/perl" -MExtUtils::Command::MM -e 'cp_nonempty' -- Vm_104e.bs blib/arch/auto/JavaScript/Duktape/Vm_104e/Vm_104e.bs 644
"/usr/perl5.38.2/bin/perl" "/usr/perl5.38.2/lib/5.38.2/ExtUtils/xsubpp"  -typemap "/usr/perl5.38.2/lib/5.38.2/ExtUtils/typemap" -typemap "/home/cpansand/.cpan/build/2023122319/JavaScript-Duktape-2.5.0-0/lib/JavaScript/Duktape/C/typemap"  Vm_104e.xs > Vm_104e.xsc
mv Vm_104e.xsc Vm_104e.c
cc -c  -iquote"/home/cpansand/.cpan/build/2023122319/JavaScript-Duktape-2.5.0-0/t/api" -I/home/cpansand/.cpan/build/2023122319/JavaScript-Duktape-2.5.0-0/lib/JavaScript/Duktape/C/../C -I/home/cpansand/.cpan/build/2023122319/JavaScript-Duktape-2.5.0-0/lib/JavaScript/Duktape/C/../C/lib  -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -O2 -pipe    -DVERSION=\"0.00\"  -DXS_VERSION=\"0.00\" -DPIC -fPIC "-I/usr/perl5.38.2/lib/5.38.2/aarch64-freebsd/CORE"   Vm_104e.c
Vm_104e.xs:7:17: error: incompatible integer to pointer conversion passing 'IV' (aka 'long') to parameter of type 'void *' [-Wint-conversion]
        memcpy( to, from, sz );
                ^~
/usr/include/string.h:63:31: note: passing argument to parameter here
void    *memcpy(void * __restrict, const void * __restrict, size_t);
                                 ^
Vm_104e.xs:7:21: error: incompatible integer to pointer conversion passing 'IV' (aka 'long') to parameter of type 'const void *' [-Wint-conversion]
        memcpy( to, from, sz );
                    ^~~~
/usr/include/string.h:63:56: note: passing argument to parameter here
void    *memcpy(void * __restrict, const void * __restrict, size_t);
                                                          ^
2 errors generated.
*** Error code 1
... etc ...

seg fault when nested errors are thrown

If a Perl function called from another Perl function throws an error, the whole perl process aborts with a segmentation fault:

use strict;
use warnings;
use JavaScript::Duktape;

my $js = JavaScript::Duktape->new();

$js->set( inc => sub{
    shift;
    die "oh no!";
});

$js->set( each => sub{
    shift;
    my $arr = shift;
    my $cb = shift;
    for(@$arr) {
        $cb->($_);
    }
});

$js->eval(q{
    var x=0;
    each([11,22], function(i){
        x+=i; x=inc(x);
    });
});

strange try-tiny interaction results in blank $@

The problem here is that a try-catch wrapped $js->eval() will not get the error message rethrown in the catch { die $err }.

This did not occur with v0.3.0, only with the new error correction pushed to the master branch.

Here's the code:

use strict;
use warnings;
use JavaScript::Duktape;
use Try::Tiny;

try {
    eval_js(q{
        throw new Error('oh boy!');
    });
} catch {
    my $err = shift;
    warn "TRY-TINY ERR=$err"; #### $err is blank!?!
};

eval {
    eval_js(q{
        throw new Error('oh boy!');
    });
};
warn "EVAL ERR=$@"; ###### blank again!?!

sub eval_js {
    my $code = shift;

    my $js= JavaScript::Duktape->new;

    try {
        $js->eval( $code );
    } catch {
        my $err = $_;
        warn "CAUGHT ERR: $err";
        die "JS ERR: $err";
    };
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.