|
13 | 13 | ## from perl |
14 | 14 |
|
15 | 15 | { |
16 | | - ## set some javascript objects |
17 | | - $js->eval(q{ |
18 | | - function Users (name, age, isAdmin){ |
19 | | - this.name = name; |
20 | | - this.age = age; |
21 | | - this.role = isAdmin; |
22 | | - } |
23 | | -
|
24 | | - function Roles (role){ |
25 | | - this.admin = role.admin; |
26 | | - this.developer = role.developer; |
27 | | - } |
28 | | - }); |
29 | | - |
30 | | - ## getting both Users & Roles objects from |
31 | | - ## javascript land as perl objects |
32 | | - my $users = $js->get_object('Users'); |
33 | | - my $role = $js->get_object('Roles'); |
34 | | - |
35 | | - ## add userList javascript array |
36 | | - ## but this time from perl |
37 | | - $js->set('usersList', [ |
38 | | - $users->new('Joe The Admin', 36, $role->new({ admin => true, developer => false })), |
39 | | - $users->new('Doe The Developer', 28, $role->new({ admin => false, developer => true })) |
40 | | - ]); |
41 | | - |
42 | | - ## did we get this right ? |
43 | | - $js->eval(q{ |
44 | | - print(usersList[0].name); // => Joe |
45 | | - print(usersList[1].age); // => 28 |
46 | | - }); |
47 | | - |
48 | | - ## get userList from perl again!! |
49 | | - ## not sure if you ever need to do this :) |
50 | | - my $usersList = $js->get_object('usersList'); |
51 | | - |
52 | | - ## set a map function, we are going to use it |
53 | | - ## from javascript to map our userList |
54 | | - ## and set admins |
55 | | - $duk->push_perl_function( sub { |
56 | | - |
57 | | - # javascript array map prototype pass |
58 | | - # three arguments, (current element, element index, whole array) |
59 | | - # so getting argument at index 0 for each array element |
60 | | - |
61 | | - # we need to extract it as javascript object |
62 | | - # because we need to do some stuff with it |
63 | | - # so instead of using to_perl method |
64 | | - # we use to_perl_object |
65 | | - my $user = $duk->to_perl_object(0); |
66 | | - |
67 | | - # if his role is admin add isAdmin prop |
68 | | - if ($user->role->admin){ |
69 | | - $duk->push_perl($user); |
70 | | - $duk->push_true(); |
71 | | - $duk->put_prop_string(-2, "isAdmin"); |
72 | | - $duk->pop(); |
73 | | - } |
74 | | - |
75 | | - # return array element |
76 | | - $duk->push_perl($user); |
77 | | - |
78 | | - # tell duktape stack that we are pushing/return something |
79 | | - return 1; |
80 | | - }); |
81 | | - |
82 | | - ## push the above created function to duktape stack |
83 | | - ## as a global function with "maps" name |
84 | | - $duk->put_global_string('maps'); |
85 | | - |
86 | | - ## use our maps function from javascript |
87 | | - my $admins = $duk->eval_string(q{ usersList.map(maps); }); |
88 | | - |
89 | | - ## get |
90 | | - $admins = $duk->to_perl_object(-1); |
91 | | - |
92 | | - ## don't forget to pop eval results |
93 | | - ## we already have it mapped to perl |
94 | | - $duk->pop(); |
95 | | - |
96 | | - ## admins should be a javascript array object |
97 | | - ## let's check if forEach works |
98 | | - $admins->forEach( sub { |
99 | | - my $user = $duk->to_perl_object(0); |
100 | | - if ($user->isAdmin) { |
101 | | - print "==================================\n"; |
102 | | - print "found an admin\n"; |
103 | | - print "his name is : "; |
104 | | - print $user->name, "\n"; |
105 | | - print "==================================\n"; |
106 | | - } |
107 | | - }); |
108 | | - |
109 | | - ## $admin is a javascript array prototype |
110 | | - ## so instead of using map from javascript we can |
111 | | - ## also use it from perl land too, and we will |
112 | | - ## assign new created array to $dev array |
113 | | - my $dev = $admins->map( sub { |
114 | | - my $user = $duk->to_perl_object(0); |
115 | | - if (!$user->isAdmin){ |
116 | | - return $user; |
117 | | - } |
118 | | - return false; |
119 | | - }); |
120 | | - |
121 | | - ## again since javascript map function |
122 | | - ## creates new Array, $dev is an object to |
123 | | - ## javascript Array |
124 | | - $dev->forEach( sub { |
125 | | - my $user = shift; |
126 | | - if ($user == false){ |
127 | | - print "** only devs allowed\n"; |
128 | | - } else { |
129 | | - print "welcome home ", $user->{name}, "\n"; |
130 | | - } |
131 | | - }); |
132 | | - |
133 | | - print "==================================\n"; |
134 | | - ## now back to perl again, let's check who's admin |
135 | | - ## we can also get list as a perl array |
136 | | - ## eval function will not convert results as objects |
137 | | - ## it will return as perl data so .. |
138 | | - $admins = $js->eval(q{ usersList.map(maps); }); |
139 | | - for (@{$admins}){ |
140 | | - if (!$_->{isAdmin}) { |
141 | | - print "found a Developer\n"; |
142 | | - print "his name is : "; |
143 | | - print $_->{name}, "\n"; |
144 | | - } |
145 | | - } |
| 16 | + ## set some javascript objects |
| 17 | + $js->eval(q{ |
| 18 | + function Users (name, age, isAdmin){ |
| 19 | + this.name = name; |
| 20 | + this.age = age; |
| 21 | + this.role = isAdmin; |
| 22 | + } |
| 23 | +
|
| 24 | + function Roles (role){ |
| 25 | + this.admin = role.admin; |
| 26 | + this.developer = role.developer; |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + ## getting both Users & Roles objects from |
| 31 | + ## javascript land as perl objects |
| 32 | + my $users = $js->get_object('Users'); |
| 33 | + my $role = $js->get_object('Roles'); |
| 34 | + |
| 35 | + ## add userList javascript array |
| 36 | + ## but this time from perl |
| 37 | + $js->set('usersList', [ |
| 38 | + $users->new('Joe The Admin', 36, $role->new({ admin => true, developer => false })), |
| 39 | + $users->new('Doe The Developer', 28, $role->new({ admin => false, developer => true })) |
| 40 | + ]); |
| 41 | + |
| 42 | + ## did we get this right ? |
| 43 | + $js->eval(q{ |
| 44 | + print(usersList[0].name); // => Joe |
| 45 | + print(usersList[1].age); // => 28 |
| 46 | + }); |
| 47 | + |
| 48 | + ## get userList from perl again!! |
| 49 | + ## not sure if you ever need to do this :) |
| 50 | + my $usersList = $js->get_object('usersList'); |
| 51 | + |
| 52 | + ## set a map function, we are going to use it |
| 53 | + ## from javascript to map our userList |
| 54 | + ## and set admins |
| 55 | + $duk->push_perl_function( sub { |
| 56 | + |
| 57 | + # javascript array map prototype pass |
| 58 | + # three arguments, (current element, element index, whole array) |
| 59 | + # so getting argument at index 0 for each array element |
| 60 | + |
| 61 | + # we need to extract it as javascript object |
| 62 | + # because we need to do some stuff with it |
| 63 | + # so instead of using to_perl method |
| 64 | + # we use to_perl_object |
| 65 | + my $user = $duk->to_perl_object(0); |
| 66 | + |
| 67 | + # if his role is admin add isAdmin prop |
| 68 | + if ($user->role->admin){ |
| 69 | + $duk->push_perl($user); |
| 70 | + $duk->push_true(); |
| 71 | + $duk->put_prop_string(-2, "isAdmin"); |
| 72 | + $duk->pop(); |
| 73 | + } |
| 74 | + |
| 75 | + # return array element |
| 76 | + $duk->push_perl($user); |
| 77 | + |
| 78 | + # tell duktape stack that we are pushing/return something |
| 79 | + return 1; |
| 80 | + }); |
| 81 | + |
| 82 | + ## push the above created function to duktape stack |
| 83 | + ## as a global function with "maps" name |
| 84 | + $duk->put_global_string('maps'); |
| 85 | + |
| 86 | + ## use our maps function from javascript |
| 87 | + my $admins = $duk->eval_string(q{ usersList.map(maps); }); |
| 88 | + |
| 89 | + ## get |
| 90 | + $admins = $duk->to_perl_object(-1); |
| 91 | + |
| 92 | + ## don't forget to pop eval results |
| 93 | + ## we already have it mapped to perl |
| 94 | + $duk->pop(); |
| 95 | + |
| 96 | + ## admins should be a javascript array object |
| 97 | + ## let's check if forEach works |
| 98 | + $admins->forEach( sub { |
| 99 | + my $user = $duk->to_perl_object(0); |
| 100 | + if ($user->isAdmin) { |
| 101 | + print "==================================\n"; |
| 102 | + print "found an admin\n"; |
| 103 | + print "his name is : "; |
| 104 | + print $user->name, "\n"; |
| 105 | + print "==================================\n"; |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + ## $admin is a javascript array prototype |
| 110 | + ## so instead of using map from javascript we can |
| 111 | + ## also use it from perl land too, and we will |
| 112 | + ## assign new created array to $dev array |
| 113 | + my $dev = $admins->map( sub { |
| 114 | + my $user = $duk->to_perl_object(0); |
| 115 | + if (!$user->isAdmin){ |
| 116 | + return $user; |
| 117 | + } |
| 118 | + return false; |
| 119 | + }); |
| 120 | + |
| 121 | + ## again since javascript map function |
| 122 | + ## creates new Array, $dev is an object to |
| 123 | + ## javascript Array |
| 124 | + $dev->forEach( sub { |
| 125 | + my $user = shift; |
| 126 | + if ($user == false){ |
| 127 | + print "** only devs allowed\n"; |
| 128 | + } else { |
| 129 | + print "welcome home ", $user->{name}, "\n"; |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + print "==================================\n"; |
| 134 | + ## now back to perl again, let's check who's admin |
| 135 | + ## we can also get list as a perl array |
| 136 | + ## eval function will not convert results as objects |
| 137 | + ## it will return as perl data so .. |
| 138 | + $admins = $js->eval(q{ usersList.map(maps); }); |
| 139 | + for (@{$admins}){ |
| 140 | + if (!$_->{isAdmin}) { |
| 141 | + print "found a Developer\n"; |
| 142 | + print "his name is : "; |
| 143 | + print $_->{name}, "\n"; |
| 144 | + } |
| 145 | + } |
146 | 146 | } |
0 commit comments