Skip to content

Commit 972defd

Browse files
committed
Merge pull request #7 from mamod/v1.0.0_5
V1.0.0 cleanup
2 parents 78031b3 + 7c78cd9 commit 972defd

File tree

19 files changed

+175
-134
lines changed

19 files changed

+175
-134
lines changed

Dev/bench.pl

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,109 @@
1313

1414

1515

16-
{ ##settung global functions
16+
# { ##setting global functions
1717

18-
my $t = timeit($count, sub{
19-
$js->set('test', sub{});
20-
});
21-
print_results("set global functions", $t);
22-
}
18+
# my $t = timeit($count, sub{
19+
# $js->set('test', sub{});
20+
# });
21+
# print_results("set global functions", $t);
22+
# }
2323

24-
{ ##getting javascript object
24+
# { ##getting javascript object
2525

26-
my $t = timeit($count, sub{
27-
$duk->eval_string(qq~
28-
function javascriptObject1 (){}
29-
javascriptObject1;
30-
~);
26+
# my $t = timeit($count, sub{
27+
# $duk->eval_string(qq~
28+
# function javascriptObject1 (){}
29+
# javascriptObject1;
30+
# ~);
3131

32-
my $obj = $duk->to_perl_object(-1);
33-
$duk->pop();
34-
});
35-
print_results("getting objects", $t);
36-
}
32+
# my $obj = $duk->to_perl_object(-1);
33+
# $duk->pop();
34+
# });
35+
# print_results("getting objects", $t);
36+
# }
3737

3838
{ ##object operations
3939
$duk->eval_string(qq~
4040
function javascriptObject (){
41+
this.test = 9;
4142
this.callme = function(n){
4243
this.test = n;
4344
}
4445
}
46+
47+
javascriptObject.prototype.setfunc = function(fn){
48+
this.fn = fn;
49+
print(this.fn(9,8));
50+
};
51+
52+
javascriptObject.prototype.testcall = function(a,b){
53+
this.fn(a,9);
54+
return a+b;
55+
};
56+
4557
javascriptObject;
4658
~);
4759
my $obj = $duk->to_perl_object(-1);
4860
$duk->pop();
4961

50-
{ #calling new on objects
51-
my $t = timeit($count, sub{
52-
my $o = $obj->new();
53-
});
54-
print_results("calling new", $t);
55-
}
62+
# { #calling new on objects
63+
# my $t = timeit($count, sub{
64+
# my $o = $obj->new();
65+
# });
66+
# print_results("calling new", $t);
67+
# }
5668

57-
{ #calling function from objects
69+
# { #calling function from objects
70+
# my $o = $obj->new();
71+
# my $i = 0;
72+
# my $t = timeit($count, sub{
73+
# $o->callme($i++);
74+
# # print $o->test, "\n";
75+
# });
76+
# print_results("calling function", $t);
77+
# }
78+
79+
{ #setting functions
5880
my $o = $obj->new();
5981
my $i = 0;
82+
83+
$o->setfunc($duk->cache( sub {
84+
# print Dumper \@_;
85+
# print "cool\n";
86+
87+
# print "cool\n";
88+
return 88;
89+
}));
90+
6091
my $t = timeit($count, sub{
61-
$o->callme($i++);
62-
# print $o->test, "\n";
92+
# my $n = $o->fn($i++, 7);
93+
my $result = $o->testcall(1,3);
94+
# print $result, "\n";
6395
});
64-
print_results("calling function", $t);
96+
print_results("calling cached function", $t);
6597
}
6698
}
6799

100+
my @results;
101+
68102
sub print_results {
69103
my $name = shift;
70104
my $t = shift;
71105

72106
my @result = split('@', timestr($t));
73107

74-
print "===================================\n";
75-
print "$count $name\n";
76-
print "===================================\n";
77-
print " " . $result[0], "\n";
78-
print $result[1], "\n";
79-
print "\n\n";
108+
my $r = "";
109+
$r .= "===================================\n";
110+
$r .= "$count $name\n";
111+
$r .= "===================================\n";
112+
$r .= " " . $result[0] . "\n";
113+
$r .= $result[1] . "\n";
114+
$r .= "\n\n";
115+
116+
push @results, $r;
117+
}
118+
119+
for (@results){
120+
print $_;
80121
}

Dev/jQuery.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
if ($text =~ s/\{(\s?)$/;/){
2323
print $text;
2424
}
25-
25+
2626
$text .= "\n";
2727
##disable debug methods
2828
if ($text !~ /duk_debug/){

Dev/parse.pl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
my @code;
1111

1212
while (<$file>){
13-
13+
1414
if ($_ =~ /^DUK_EXTERNAL_DECL/){
1515
$_ =~ s/^DUK_EXTERNAL_DECL(\s+?)//;
1616
$_ =~ s/;(\n)?$//;
17-
17+
1818
if ($_ !~ /duk_context \*ctx/g){
1919
next;
2020
}
@@ -73,7 +73,7 @@
7373
# $perl_function =~s/duk_context \*ctx/SV \*Obj/;
7474
$perl_function = 'aperl_' . $perl_function;
7575

76-
76+
7777
$code .= $code_type . "$perl_function {\n";
7878
$code .= "\tduk_size_t sz;\n" if $lstring;
7979

README.pod

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ JavaScript::Duktape - Perl interface to Duktape embeddable javascript engine
66
<a href="https://travis-ci.org/mamod/JavaScript-Duktape"><img src="https://travis-ci.org/mamod/JavaScript-Duktape.svg?branch=master"></a>
77

88
=head1 SYNOPSIS
9-
9+
1010
use JavaScript::Duktape;
1111

1212
##create new js context
@@ -28,7 +28,7 @@ JavaScript::Duktape - Perl interface to Duktape embeddable javascript engine
2828

2929
=head1 DESCRIPTION
3030

31-
JavaScript::Duktape implements almost all duktape javascript engine api, the c code is just
31+
JavaScript::Duktape implements almost all duktape javascript engine api, the c code is just
3232
a thin layer that maps duktape api to perl, and all other functions implemented in perl
3333
it self, so maintaing and contributing to the base code should be easy.
3434

@@ -58,7 +58,7 @@ To access vm create new context then call C<vm>
5858
my $js = JavaScript::Duktape->new();
5959
my $duk = $js->vm;
6060

61-
#now you can call Duktape API from perl
61+
#now you can call Duktape API from perl
6262

6363
$duk->push_string('print');
6464
$duk->eval();
@@ -76,15 +76,15 @@ above but with using C<dump> function to get a glance of stack top
7676
#push "print" string
7777
$duk->push_string('print');
7878
$duk->dump(); #-> [ Duktape (top=1): print ]
79-
79+
8080
#since print is a native function we need to evaluate it
8181
$duk->eval();
8282
$duk->dump(); #-> [ Duktape (top=1): function print() {/* native */} ]
8383

8484
#push one argument to print function
8585
$duk->push_string('hi');
8686
$duk->dump(); #-> [ Duktape (top=2): function print() {/* native */} hi ]
87-
87+
8888
#now call print function and pass "hi" as one argument
8989
$duk->call(1);
9090

@@ -99,7 +99,7 @@ above but with using C<dump> function to get a glance of stack top
9999

100100
=head1 VM methods
101101

102-
As a general rule all duktape api supported, but I haven't had the chance to test them all,
102+
As a general rule all duktape api supported, but I haven't had the chance to test them all,
103103
so please report any missing or failure api call and I'll try to fix
104104

105105
For the list of duktape engine API please see L<http://duktape.org/api.html>, and here is how
@@ -117,8 +117,8 @@ you can translate duktape api to perl
117117
# duk_pop(ctx);
118118

119119
#and here is how we can implement it in JavaScript::Duktape
120-
121-
$duk->push_c_function(sub {
120+
121+
$duk->push_c_function(sub {
122122
my $duk = shift;
123123
my $num1 = $duk->get_int(0);
124124
my $num2 = $duk->get_int(1);
@@ -137,12 +137,12 @@ you can translate duktape api to perl
137137
As you can see all you need to do is replacing C<duk_> with C<$duk->> and remove C<ctx> from the function call,
138138
this may sounds crazy but api tests have been generated by copying duktape tests and using search and replace tool :)
139139

140-
Besides duktape api, C<JavaScript::Duktape::Vm> implements the following methods
140+
Besides duktape api, C<JavaScript::Duktape::Vm> implements the following methods
141141

142142
=over 4
143-
143+
144144
=item push_function ( code_ref, num_of_args );
145-
145+
146146
an alias to push_c_function
147147

148148
=item push_perl( ... );
@@ -160,7 +160,7 @@ resets duktape stack top
160160
=back
161161

162162
=head1 AUTHOR
163-
163+
164164
Mamod Mehyar C<< <[email protected]> >>
165165

166166
=head1 LICENSE

0 commit comments

Comments
 (0)