-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from mamod/objects
Objects
- Loading branch information
Showing
4 changed files
with
175 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use lib './lib'; | ||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
|
||
use JavaScript::Duktape; | ||
|
||
my $js = JavaScript::Duktape->new; | ||
|
||
my $buffer = $js->get_object( q{ | ||
Buffer.prototype.get = function(n){ | ||
return this[n] || ''; | ||
}; | ||
Buffer.prototype.set = function set(n, v){ | ||
this[n] = v; | ||
}; | ||
// will be implemented in perl | ||
// var buf = new Buffer(7); | ||
// buf.fill(97); | ||
// print(buf.toString()); | ||
// var n = buf.slice(0, 2).fill('b'); | ||
// print(buf.toString()); | ||
Buffer; | ||
}); | ||
|
||
my $buf = $buffer->new(7); | ||
|
||
$buf->fill(97); | ||
print $buf->byteLength, "\n"; #7 | ||
print $buf->toString('utf8'), "\n"; #aaaaaaa; | ||
|
||
my $n = $buf->slice(0, 2)->fill('b'); | ||
|
||
print $n->byteLength, "\n"; #2 | ||
print $n->toString('utf8'), "\n"; #bb | ||
|
||
print $n->get(1), "\n"; #98; | ||
print $buf->get(6), "\n"; #97; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use lib './lib'; | ||
use strict; | ||
use warnings; | ||
use utf8; | ||
use Encode; | ||
use Data::Dumper; | ||
use Test::More; | ||
use JavaScript::Duktape; | ||
|
||
my $js = JavaScript::Duktape->new; | ||
my $duk = $js->duk; | ||
my $ret = $duk->eval_string( q{ | ||
Buffer.prototype.get = function(n){ | ||
return this[n]; | ||
}; | ||
Buffer.prototype.set = function set(n, v){ | ||
this[n] = v; | ||
}; | ||
Buffer; | ||
}); | ||
|
||
my $buffer = $duk->to_perl_object(-1); | ||
$duk->pop(); | ||
|
||
for (0 .. 10){ | ||
is $duk->get_top(), 0; | ||
my $gg = $buffer->new(7); | ||
|
||
$gg->fill('V'); | ||
$gg->fill('V'); | ||
my $t = $buffer->new(17); | ||
$t->fill("3"); | ||
|
||
is $t->toString('utf8'), Encode::encode('UTF-8', ("\3" x 17) ); | ||
is $t->byteLength, 17; | ||
|
||
# $t->setx(0, 66); | ||
# # | ||
is $t->get(0), 3; | ||
$t->set(1, 97); | ||
is $t->get(1), 97; | ||
|
||
my $n = $t->slice(0, 2)->fill('b'); | ||
$n->fill('a'); | ||
|
||
is $n->byteLength, 2; | ||
is $n->toString('utf8'), 'aa'; | ||
|
||
is $gg->toString('utf8'), "V" x 7; | ||
is $gg->byteLength, 7; | ||
} | ||
|
||
is $duk->get_top(), 0; | ||
done_testing(100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use lib './lib'; | ||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use JavaScript::Duktape; | ||
use Test::More; | ||
|
||
my $js = JavaScript::Duktape->new; | ||
my $duk = $js->duk; | ||
my $ret = $duk->eval_string( q{ | ||
String; | ||
}); | ||
|
||
my $string = $duk->to_perl_object(-1); | ||
$duk->pop(); | ||
|
||
my $str = $string->new("Hi there"); | ||
my $str2 = $str->slice(0, 2); | ||
is $str2, 'Hi'; | ||
|
||
is $str->charAt(10000), ''; | ||
is $str->charAt(0), 'H'; | ||
|
||
is $str->toUpperCase(_), 'HI THERE'; | ||
is $str->slice(3, $str->length), 'there'; | ||
|
||
|
||
|
||
is $string->new('Blue Whale')->indexOf('Blue'), 0; #// returns 0 | ||
is $string->new('Blue Whale')->indexOf('Blute'), -1; #// returns -1 | ||
is $string->new('Blue Whale')->indexOf('Whale', 0), 5; #// returns 5 | ||
is $string->new('Blue Whale')->indexOf('Whale', 5), 5; #// returns 5 | ||
is $string->new('Blue Whale')->indexOf('', 9), 9; #// returns 9 | ||
is $string->new('Blue Whale')->indexOf('', 10), 10; #// returns 10 | ||
is $string->new('Blue Whale')->indexOf('', 11), 10; #// returns 10 | ||
|
||
|
||
{ | ||
my $str = $string->new('To be, or not to be, that is the question.'); | ||
my $count = 0; | ||
my $pos = $str->indexOf('e'); | ||
|
||
while ($pos != -1) { | ||
$count++; | ||
$pos = $str->indexOf('e', $pos + 1); | ||
} | ||
is $count, 4; #// displays 4 | ||
is $str->concat->(' EOF'), 'To be, or not to be, that is the question. EOF'; | ||
} | ||
|
||
|
||
my $orig = $string->new(' foo '); | ||
is $orig->trim(_), 'foo'; #// 'foo' | ||
|
||
is $duk->get_top(), 0; | ||
|
||
done_testing(16); |