Skip to content

Commit

Permalink
indentation
Browse files Browse the repository at this point in the history
unify all indentations in files to 4 speces
  • Loading branch information
mamod committed Jan 24, 2017
1 parent 15cbee3 commit 0ace21c
Show file tree
Hide file tree
Showing 28 changed files with 965 additions and 965 deletions.
260 changes: 130 additions & 130 deletions examples/advanced-array.pl
Original file line number Diff line number Diff line change
Expand Up @@ -13,134 +13,134 @@
## from perl

{
## set some javascript objects
$js->eval(q{
function Users (name, age, isAdmin){
this.name = name;
this.age = age;
this.role = isAdmin;
}
function Roles (role){
this.admin = role.admin;
this.developer = role.developer;
}
});

## getting both Users & Roles objects from
## javascript land as perl objects
my $users = $js->get_object('Users');
my $role = $js->get_object('Roles');

## add userList javascript array
## but this time from perl
$js->set('usersList', [
$users->new('Joe The Admin', 36, $role->new({ admin => true, developer => false })),
$users->new('Doe The Developer', 28, $role->new({ admin => false, developer => true }))
]);

## did we get this right ?
$js->eval(q{
print(usersList[0].name); // => Joe
print(usersList[1].age); // => 28
});

## get userList from perl again!!
## not sure if you ever need to do this :)
my $usersList = $js->get_object('usersList');

## set a map function, we are going to use it
## from javascript to map our userList
## and set admins
$duk->push_perl_function( sub {

# javascript array map prototype pass
# three arguments, (current element, element index, whole array)
# so getting argument at index 0 for each array element

# we need to extract it as javascript object
# because we need to do some stuff with it
# so instead of using to_perl method
# we use to_perl_object
my $user = $duk->to_perl_object(0);

# if his role is admin add isAdmin prop
if ($user->role->admin){
$duk->push_perl($user);
$duk->push_true();
$duk->put_prop_string(-2, "isAdmin");
$duk->pop();
}

# return array element
$duk->push_perl($user);

# tell duktape stack that we are pushing/return something
return 1;
});

## push the above created function to duktape stack
## as a global function with "maps" name
$duk->put_global_string('maps');

## use our maps function from javascript
my $admins = $duk->eval_string(q{ usersList.map(maps); });

## get
$admins = $duk->to_perl_object(-1);

## don't forget to pop eval results
## we already have it mapped to perl
$duk->pop();

## admins should be a javascript array object
## let's check if forEach works
$admins->forEach( sub {
my $user = $duk->to_perl_object(0);
if ($user->isAdmin) {
print "==================================\n";
print "found an admin\n";
print "his name is : ";
print $user->name, "\n";
print "==================================\n";
}
});

## $admin is a javascript array prototype
## so instead of using map from javascript we can
## also use it from perl land too, and we will
## assign new created array to $dev array
my $dev = $admins->map( sub {
my $user = $duk->to_perl_object(0);
if (!$user->isAdmin){
return $user;
}
return false;
});

## again since javascript map function
## creates new Array, $dev is an object to
## javascript Array
$dev->forEach( sub {
my $user = shift;
if ($user == false){
print "** only devs allowed\n";
} else {
print "welcome home ", $user->{name}, "\n";
}
});

print "==================================\n";
## now back to perl again, let's check who's admin
## we can also get list as a perl array
## eval function will not convert results as objects
## it will return as perl data so ..
$admins = $js->eval(q{ usersList.map(maps); });
for (@{$admins}){
if (!$_->{isAdmin}) {
print "found a Developer\n";
print "his name is : ";
print $_->{name}, "\n";
}
}
## set some javascript objects
$js->eval(q{
function Users (name, age, isAdmin){
this.name = name;
this.age = age;
this.role = isAdmin;
}
function Roles (role){
this.admin = role.admin;
this.developer = role.developer;
}
});

## getting both Users & Roles objects from
## javascript land as perl objects
my $users = $js->get_object('Users');
my $role = $js->get_object('Roles');

## add userList javascript array
## but this time from perl
$js->set('usersList', [
$users->new('Joe The Admin', 36, $role->new({ admin => true, developer => false })),
$users->new('Doe The Developer', 28, $role->new({ admin => false, developer => true }))
]);

## did we get this right ?
$js->eval(q{
print(usersList[0].name); // => Joe
print(usersList[1].age); // => 28
});

## get userList from perl again!!
## not sure if you ever need to do this :)
my $usersList = $js->get_object('usersList');

## set a map function, we are going to use it
## from javascript to map our userList
## and set admins
$duk->push_perl_function( sub {

# javascript array map prototype pass
# three arguments, (current element, element index, whole array)
# so getting argument at index 0 for each array element

# we need to extract it as javascript object
# because we need to do some stuff with it
# so instead of using to_perl method
# we use to_perl_object
my $user = $duk->to_perl_object(0);

# if his role is admin add isAdmin prop
if ($user->role->admin){
$duk->push_perl($user);
$duk->push_true();
$duk->put_prop_string(-2, "isAdmin");
$duk->pop();
}

# return array element
$duk->push_perl($user);

# tell duktape stack that we are pushing/return something
return 1;
});

## push the above created function to duktape stack
## as a global function with "maps" name
$duk->put_global_string('maps');

## use our maps function from javascript
my $admins = $duk->eval_string(q{ usersList.map(maps); });

## get
$admins = $duk->to_perl_object(-1);

## don't forget to pop eval results
## we already have it mapped to perl
$duk->pop();

## admins should be a javascript array object
## let's check if forEach works
$admins->forEach( sub {
my $user = $duk->to_perl_object(0);
if ($user->isAdmin) {
print "==================================\n";
print "found an admin\n";
print "his name is : ";
print $user->name, "\n";
print "==================================\n";
}
});

## $admin is a javascript array prototype
## so instead of using map from javascript we can
## also use it from perl land too, and we will
## assign new created array to $dev array
my $dev = $admins->map( sub {
my $user = $duk->to_perl_object(0);
if (!$user->isAdmin){
return $user;
}
return false;
});

## again since javascript map function
## creates new Array, $dev is an object to
## javascript Array
$dev->forEach( sub {
my $user = shift;
if ($user == false){
print "** only devs allowed\n";
} else {
print "welcome home ", $user->{name}, "\n";
}
});

print "==================================\n";
## now back to perl again, let's check who's admin
## we can also get list as a perl array
## eval function will not convert results as objects
## it will return as perl data so ..
$admins = $js->eval(q{ usersList.map(maps); });
for (@{$admins}){
if (!$_->{isAdmin}) {
print "found a Developer\n";
print "his name is : ";
print $_->{name}, "\n";
}
}
}
12 changes: 6 additions & 6 deletions examples/array.pl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

# check if it's really set in JavaScript
$js->eval(q{
print(numbers[0]); // => 1
print(numbers[4]); // => 5
print(numbers[0]); // => 1
print(numbers[4]); // => 5
});


Expand All @@ -34,8 +34,8 @@
# 1 => 3
# 2 => 4
$numbers->forEach(sub {
my ($value, $index, $ar) = @_;
print $index, " => ", $value, "\n";
my ($value, $index, $ar) = @_;
print $index, " => ", $value, "\n";
});

print "We ar now reversed \n";
Expand All @@ -46,6 +46,6 @@
# 1 => 3
# 2 => 2
$reversed->forEach(sub {
my ($value, $index, $ar) = @_;
print $index, " => ", $value, "\n";
my ($value, $index, $ar) = @_;
print $index, " => ", $value, "\n";
});
8 changes: 4 additions & 4 deletions examples/date.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
my $duk = $js->duk;

$js->eval(q{
var today = new Date();
print(today.getMinutes());
var unixTimestamp = Date.now();
print(unixTimestamp);
var today = new Date();
print(today.getMinutes());
var unixTimestamp = Date.now();
print(unixTimestamp);
});

my $date = $js->get_object('Date');
Expand Down
12 changes: 6 additions & 6 deletions examples/this.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
my $duk = $js->duk;

$js->eval(q{
function Person (fname, lname) {
this.firstName = fname;
this.lastName = lname;
this.getName = getName;
}
function Person (fname, lname) {
this.firstName = fname;
this.lastName = lname;
this.getName = getName;
}
});

$js->set('getName', sub {
return this->firstName . ' ' . this->lastName;
return this->firstName . ' ' . this->lastName;
});


Expand Down
32 changes: 16 additions & 16 deletions t/api/c-constructor.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ my $duk = $js->duk;
SET_PRINT_METHOD($duk);

sub my_constructor {
return 0;
return 0;
}

sub test1 {
$duk->push_global_object();
$duk->push_function(\&my_constructor, 0); # constructor (function) */
$duk->push_object(); # prototype object -> [ global cons proto ] */
$duk->push_string("inherited value");
$duk->put_prop_string(-2, "inherited"); # set proto.inherited = "inherited value" */
$duk->put_prop_string(-2, "prototype"); # set cons.prototype = proto; stack -> [ global cons ] */
$duk->put_prop_string(-2, "MyConstructor"); # set global.MyConstructor = cons; stack -> [ global ] */
$duk->pop();

$duk->eval_string("var obj = new MyConstructor(); print(obj.inherited);");
# $duk->dump();
$duk->pop();

printf("top at end: %ld\n", $duk->get_top());
return 0;
$duk->push_global_object();
$duk->push_function(\&my_constructor, 0); # constructor (function) */
$duk->push_object(); # prototype object -> [ global cons proto ] */
$duk->push_string("inherited value");
$duk->put_prop_string(-2, "inherited"); # set proto.inherited = "inherited value" */
$duk->put_prop_string(-2, "prototype"); # set cons.prototype = proto; stack -> [ global cons ] */
$duk->put_prop_string(-2, "MyConstructor"); # set global.MyConstructor = cons; stack -> [ global ] */
$duk->pop();

$duk->eval_string("var obj = new MyConstructor(); print(obj.inherited);");
# $duk->dump();
$duk->pop();

printf("top at end: %ld\n", $duk->get_top());
return 0;
}

TEST_SAFE_CALL($duk, \&test1, 'test1');
Expand Down
6 changes: 3 additions & 3 deletions t/api/check-type.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ my $i = 0;

my $n = $duk->get_top();
for ($i = 0; $i < $n + 1; $i++) { # end on invalid index on purpose
printf("stack[%ld] --> DUK_TYPE_NUMBER=%ld DUK_TYPE_NONE=%ld\n",
$i, $duk->check_type($i, DUK_TYPE_NUMBER),
$duk->check_type($i, DUK_TYPE_NONE));
printf("stack[%ld] --> DUK_TYPE_NUMBER=%ld DUK_TYPE_NONE=%ld\n",
$i, $duk->check_type($i, DUK_TYPE_NUMBER),
$duk->check_type($i, DUK_TYPE_NONE));
}

test_stdout();
Expand Down
Loading

0 comments on commit 0ace21c

Please sign in to comment.