forked from mikeckennedy/talk-python-transcripts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path032_PyPy_js_and_Python_in_the_browser.vtt
2366 lines (1577 loc) · 78.6 KB
/
032_PyPy_js_and_Python_in_the_browser.vtt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
WEBVTT
00:00:00.001 --> 00:00:03.660
Imagine a future where you're building a rich client-side web app.
00:00:03.660 --> 00:00:07.140
You start by creating some backend services, maybe in Flask or Node,
00:00:07.140 --> 00:00:10.960
create an HTML page, you throw in a few divs and ULs,
00:00:10.960 --> 00:00:15.840
and then you type script source equals main.py language equals Python.
00:00:15.840 --> 00:00:20.260
That future might just be possible for the right type of applications
00:00:20.260 --> 00:00:22.840
with Ryan Kelly's PyPy.js project.
00:00:22.840 --> 00:00:26.480
This is Talk Python to Me with guest Ryan Kelly.
00:00:27.060 --> 00:00:31.380
Show number 32 recorded Wednesday, September 30th, 2015.
00:00:54.120 --> 00:00:57.820
I have many interests, sometimes conflates my creativity.
00:00:57.820 --> 00:01:01.580
Welcome to Talk Python to Me, a weekly podcast on Python,
00:01:01.580 --> 00:01:04.620
the language, the libraries, the ecosystem, and the personalities.
00:01:04.620 --> 00:01:06.760
This is your host, Michael Kennedy.
00:01:06.760 --> 00:01:08.760
Follow me on Twitter where I'm @mkennedy.
00:01:08.760 --> 00:01:12.640
Keep up with the show and listen to past episodes at talkpython.fm
00:01:12.640 --> 00:01:15.220
and follow the show on Twitter via at Talk Python.
00:01:16.220 --> 00:01:19.340
This episode is brought to you by Hired and Codeship.
00:01:19.340 --> 00:01:23.680
Thank them for supporting the show on Twitter via at Hired underscore HQ
00:01:23.680 --> 00:01:24.700
and at Codeship.
00:01:24.700 --> 00:01:26.940
Hey, everyone.
00:01:26.940 --> 00:01:28.300
Thanks so much for listening in.
00:01:28.300 --> 00:01:31.820
No news today, so let me introduce Ryan and we'll get right to the interview.
00:01:31.820 --> 00:01:36.200
Ryan is a software developer based in Melbourne, Australia.
00:01:36.200 --> 00:01:39.320
Most of his days are spent coding in Python and JavaScript,
00:01:39.320 --> 00:01:41.480
commercially as an engineer at Mozilla,
00:01:41.600 --> 00:01:44.000
as well as for a variety of open source projects.
00:01:44.000 --> 00:01:47.360
He also maintains a strong interest in logic programming,
00:01:47.360 --> 00:01:49.640
mainly as a result of his doctoral thesis.
00:01:49.640 --> 00:01:52.420
Ryan, welcome to the show.
00:01:52.420 --> 00:01:53.420
Thanks for having me.
00:01:53.420 --> 00:01:55.560
I'm super excited that you're here today.
00:01:55.560 --> 00:01:57.460
We're going to talk about Python,
00:01:57.460 --> 00:01:59.860
but in a place that you typically don't find it,
00:01:59.860 --> 00:02:00.700
in the browser, right?
00:02:00.700 --> 00:02:01.600
Yep.
00:02:02.400 --> 00:02:04.160
Yeah, there's a lot of cool projects out there,
00:02:04.160 --> 00:02:07.020
and I'm a huge fan of the one that you've been working on.
00:02:07.020 --> 00:02:08.700
Before we get to that, though,
00:02:08.700 --> 00:02:11.260
let's talk about how you got into programming.
00:02:11.260 --> 00:02:11.920
What's your story?
00:02:11.920 --> 00:02:12.880
Sure.
00:02:12.880 --> 00:02:16.000
So I think it was quite a while ago now,
00:02:16.000 --> 00:02:19.880
and I actually started programming in JavaScript in high school way back in the day
00:02:19.880 --> 00:02:21.160
for a class, right?
00:02:21.160 --> 00:02:24.460
I was never the kid that sort of always grew up with computers.
00:02:24.460 --> 00:02:27.760
I very much just got into it for a school assignment
00:02:27.760 --> 00:02:29.680
and pretty much fell in love straight away.
00:02:30.480 --> 00:02:33.960
You know, I just fell in love with the ability to have a thought
00:02:33.960 --> 00:02:37.260
and kind of capture that in some concrete form
00:02:37.260 --> 00:02:39.440
and have it executed and have effects on the world.
00:02:39.440 --> 00:02:40.280
It's pretty amazing.
00:02:40.280 --> 00:02:44.160
And actually, that kind of subject changed my entire career path.
00:02:44.160 --> 00:02:45.700
I'd always planned to go into the sciences,
00:02:45.700 --> 00:02:51.420
and I had sort of a half a semester of computer programming in high school
00:02:51.420 --> 00:02:54.780
and changed my whole career trajectory into engineering and computer science.
00:02:54.780 --> 00:02:57.500
I think a lot of us who are in programming had that experience
00:02:57.500 --> 00:03:02.500
where maybe we didn't think that we were programmers per se, right?
00:03:02.500 --> 00:03:04.640
But then you get some experience where then you're like,
00:03:04.640 --> 00:03:06.420
wow, this is really amazing.
00:03:06.420 --> 00:03:08.940
This is really way more rewarding than a lot of other stuff.
00:03:08.940 --> 00:03:10.320
I had that experience in math.
00:03:10.320 --> 00:03:13.200
I studied lots of math, and I'm like, but we don't build anything here.
00:03:13.200 --> 00:03:17.740
Programming is so much more concrete but creative.
00:03:17.740 --> 00:03:18.400
I love it.
00:03:18.740 --> 00:03:24.680
Yeah, it's that beautiful combination of kind of creativity in the mix, right?
00:03:24.680 --> 00:03:27.240
The discovering and creating is pretty cool.
00:03:27.240 --> 00:03:28.320
Yeah, sure.
00:03:28.320 --> 00:03:29.240
So how did you get to Python?
00:03:29.240 --> 00:03:36.000
I was handed Python as a requirement for an internship I was doing in university.
00:03:37.000 --> 00:03:40.100
I went to work for a company that was doing engineering simulations.
00:03:40.100 --> 00:03:46.700
So I think like a lot of people, I sort of come to Python because it has really strong tools for doing data analysis and visualizations.
00:03:46.700 --> 00:03:52.240
So that was in a kind of typical intern project kind of way.
00:03:52.240 --> 00:03:56.520
It's kind of like, here is a relatively well-scoped project, and you're using this language, and off you go.
00:03:57.460 --> 00:04:04.220
But I just found that it really fit my head in a way that a lot of other languages that I've been dabbling around with didn't.
00:04:04.220 --> 00:04:11.280
It was just kind of super easy to get up and running and dive in, and I found that the more time I spent with it,
00:04:11.280 --> 00:04:16.180
and the more I came to understand the language and find all of these more and more interesting corners of it,
00:04:16.180 --> 00:04:17.840
it just kind of grew from there.
00:04:17.840 --> 00:04:29.280
And sort of since from that point, it's always been my kind of go-to language for side projects or projects where I'm not kind of locked into a language for other reasons.
00:04:29.280 --> 00:04:36.320
Yeah, I think Python has this rare combination of being very capable and yet very simple.
00:04:36.320 --> 00:04:41.400
There's a lot of capable languages, and there's a lot of simple languages, but they don't often intersect.
00:04:41.400 --> 00:04:42.180
So that's great.
00:04:43.140 --> 00:04:49.700
I've worked in some interesting languages like Prologue, and there's a language called Mozart, which is like a logic programming language.
00:04:49.700 --> 00:04:55.600
And they're incredibly powerful, but yeah, they don't have that just clarity that Python has.
00:04:55.600 --> 00:05:00.340
So before we get to talk about your project, you work at Mozilla, right?
00:05:00.340 --> 00:05:01.680
That's right.
00:05:01.680 --> 00:05:03.760
I'm a huge fan of Mozilla.
00:05:03.760 --> 00:05:06.080
Firefox is definitely my browser.
00:05:06.080 --> 00:05:07.520
Cool. Good to hear.
00:05:07.520 --> 00:05:09.300
Yeah, I try to spread the word.
00:05:09.300 --> 00:05:09.820
What do you do there?
00:05:09.820 --> 00:05:12.160
I used to work with the cloud services team.
00:05:12.460 --> 00:05:13.840
We're not an independent team anymore.
00:05:13.840 --> 00:05:18.900
So when I first started out, I was working on the web servers behind Firefox Sync.
00:05:18.900 --> 00:05:23.780
So doing sort of data storage and RESTful JSON APIs.
00:05:23.780 --> 00:05:27.440
They're all Python-based servers.
00:05:27.440 --> 00:05:32.160
So it's pretty standard kind of Python backend web gig.
00:05:32.160 --> 00:05:39.400
Through a series of kind of mergers and reorgs and so forth, I actually wound up on a project that's operating in Node.js.
00:05:40.200 --> 00:05:49.660
So my time these days is spent on Firefox accounts, which is the login system for Sync and for all of the new kind of cloud-based services that Mozilla is doing.
00:05:49.660 --> 00:05:53.800
And it's a 100% JavaScript shop, that team.
00:05:53.800 --> 00:05:55.820
So it's an interesting change of pace.
00:05:56.320 --> 00:06:00.620
But it does leave me kind of itching to get more involved in Python stuff on the side.
00:06:00.620 --> 00:06:02.820
Yeah, that's really interesting.
00:06:02.820 --> 00:06:10.120
I think that's a good lead into your topic because it's like Python and JavaScript, those two worlds colliding, right?
00:06:10.120 --> 00:06:11.120
Right.
00:06:12.120 --> 00:06:20.740
So maybe at a really high level, we could just talk about your project, PyPyJS, and then we could talk a little more specifically about it.
00:06:20.740 --> 00:06:21.740
Sure.
00:06:22.580 --> 00:06:31.000
So at a super high level, I like to describe it as an experiment in building a fast and compliant Python environment for the web.
00:06:31.000 --> 00:06:36.580
So concretely, it means like I want a Python interpreter that will run on top of JavaScript.
00:06:37.040 --> 00:06:45.380
So anywhere you would go that you would want to do something in JavaScript, you should be able to take Python to that platform as a first-class citizen.
00:06:45.380 --> 00:06:49.900
And obviously, a big place where JavaScript shows up is in the browser.
00:06:49.900 --> 00:06:52.580
And the browser is just dominated by JavaScript.
00:06:52.580 --> 00:06:56.280
There's really not a lot of other games in town if you want to do scripting in the browser.
00:06:56.840 --> 00:07:02.800
But I'm also kind of interested in how it might fit on the server side in Node.js or other JavaScript frameworks as well.
00:07:02.800 --> 00:07:10.100
But basically, being able to take other languages such as Python and put them anywhere where JavaScript is currently claiming as its home turf.
00:07:10.100 --> 00:07:11.300
Yeah, that's great.
00:07:11.300 --> 00:07:16.640
I mean, there's definitely been people talking how JavaScript is becoming the new assembly language of the web.
00:07:16.640 --> 00:07:17.660
And we'll get into that.
00:07:17.660 --> 00:07:24.720
So the way that I heard about your project was you gave a really excellent presentation at PyCon 2015.
00:07:24.720 --> 00:07:25.900
Oh, thank you.
00:07:26.260 --> 00:07:27.120
Yeah, yeah, definitely.
00:07:27.120 --> 00:07:28.800
What was the title of it?
00:07:28.800 --> 00:07:29.220
I forgot.
00:07:29.220 --> 00:07:31.340
PyPy.js.
00:07:31.340 --> 00:07:32.560
What, how, why?
00:07:32.560 --> 00:07:33.580
I think from memory.
00:07:33.580 --> 00:07:35.000
Yeah, that's perfect.
00:07:35.000 --> 00:07:35.660
Yeah, I think you're right.
00:07:35.660 --> 00:07:39.940
And it's on the YouTube channel for PyCon US 2015.
00:07:39.940 --> 00:07:42.320
So I'll be sure to link to that in the show notes.
00:07:42.320 --> 00:07:47.720
One of the ways you opened your talk was you talked about how Mozilla loves the web.
00:07:47.720 --> 00:07:51.800
And not just the technology of it, but sort of the whole idea and the culture of it.
00:07:51.800 --> 00:07:53.620
Can you maybe talk about that a bit?
00:07:53.620 --> 00:07:54.300
Sure.
00:07:54.300 --> 00:07:54.340
Sure.
00:07:54.340 --> 00:07:56.740
So we have this saying at Mozilla.
00:07:57.100 --> 00:08:00.960
And this was sort of one of the things that really inspired me to get started on this project.
00:08:00.960 --> 00:08:03.520
We like to say like the web is the platform.
00:08:03.520 --> 00:08:07.840
Well, this is kind of the world we want to live in is where the web is the platform.
00:08:08.780 --> 00:08:16.880
And so the web, not necessarily from a technology standpoint, but from a kind of computing platform standpoint, is pretty unique and pretty amazing.
00:08:17.580 --> 00:08:23.560
It's a platform that doesn't have any kind of all-powerful gatekeepers.
00:08:23.560 --> 00:08:28.000
It's a platform where anyone can kind of show up and start participating.
00:08:28.000 --> 00:08:34.800
You can publish stuff on the web without having to be locked into a particular hardware platform or a particular set of tools.
00:08:34.960 --> 00:08:43.140
You can show up and start consuming stuff from the web without any of the similar sets of restrictions or requirements.
00:08:43.640 --> 00:08:54.840
And so at Mozilla, we kind of see the web as a really powerful tool for empowerment and participation in the global kind of information economy.
00:08:55.920 --> 00:09:01.580
Of course, the web, you know, from a social standpoint, that's all very well.
00:09:01.580 --> 00:09:07.040
But when you get right down to the technology of the web, it doesn't necessarily tick all of those boxes, right?
00:09:07.040 --> 00:09:11.480
In a sense, the technology stack of the web is a little bit kind of restrictive.
00:09:11.480 --> 00:09:13.940
It's all kind of JavaScript and HTML in the browser.
00:09:13.940 --> 00:09:21.280
There's kind of a lot of nice sociological aspects of the web that are really unique and really valuable.
00:09:21.280 --> 00:09:25.680
And there are some technical aspects of the web that are not always to everyone's liking.
00:09:25.680 --> 00:09:27.960
So that's an interesting platform in that respect.
00:09:27.960 --> 00:09:29.360
It really is.
00:09:29.360 --> 00:09:37.080
You know, you could have sold me on this idea with that statement in 2005, no problem.
00:09:37.080 --> 00:09:48.380
But I think it's even more relevant now because we all have these app platforms and these proprietary APIs, you know, iOS, Android, OS X.
00:09:48.380 --> 00:09:53.200
There's just more and more walled gardens and proprietary stuff.
00:09:53.200 --> 00:09:54.860
Walled gardens and app stores.
00:09:55.220 --> 00:09:56.200
Yes, absolutely.
00:09:56.200 --> 00:09:58.600
And there's, you know, good financial reasons for that, right?
00:09:58.600 --> 00:09:59.040
Absolutely.
00:09:59.040 --> 00:10:01.040
And not just financial reasons.
00:10:01.040 --> 00:10:06.000
Like curated content has a lot of user value and security value and stuff like that.
00:10:06.000 --> 00:10:14.140
So the web has not completely undeserved reputation as a little bit of a security badlands because you don't know whose code you're running half the time.
00:10:14.140 --> 00:10:17.200
You just could have got to trust that the browser is going to do the right thing for you.
00:10:17.820 --> 00:10:18.160
That's right.
00:10:18.160 --> 00:10:33.380
And that's, I think, partly why the JavaScript component of the web is so crucial because if it involves Flash or ActiveX or some other, you know, high privileged C-based plugin, it's not the web.
00:10:33.380 --> 00:10:33.820
Not really.
00:10:34.000 --> 00:10:34.560
Absolutely.
00:10:34.560 --> 00:10:35.280
And you can't really trust it, right?
00:10:35.280 --> 00:10:35.840
Absolutely.
00:10:35.840 --> 00:10:41.720
And so your mission to bring things like Python to the web have to involve JavaScript somehow, right?
00:10:41.720 --> 00:10:42.940
Right.
00:10:43.140 --> 00:10:52.880
And I think there's been a long history and pretty much since the web has been a thing of trying to bring more kind of native platform functionality to the web.
00:10:53.320 --> 00:10:54.660
You know, you mentioned Flash.
00:10:54.660 --> 00:10:59.760
They've had, you know, ActiveX or Java applets, you know, way back in the day.
00:10:59.760 --> 00:11:14.120
And the web has a really long history of trying to compete with native platforms by bringing native platform technologies kind of into the web by expanding the scope of the technology stack of the web.
00:11:14.120 --> 00:11:18.960
As, you know, one after another have been kind of failures.
00:11:18.960 --> 00:11:22.480
They fail to stick for any significant length of time.
00:11:23.040 --> 00:11:25.720
And a lot of the time it's because of aspects like that security, right?
00:11:25.720 --> 00:11:29.760
You're just kind of opening up too many holes, too many opportunities for things to go wrong.
00:11:29.760 --> 00:11:42.100
Whereas if you take kind of JavaScript as your base level, then you can build on this sandbox that's had 20-odd years of, you know, figuring out how to get that right and build that model correctly.
00:11:42.100 --> 00:11:48.140
So if you take that as your foundation, you kind of get a lot of the good security stuff and the lessons learned from the web for free.
00:11:48.140 --> 00:11:50.000
Yeah, I totally agree.
00:11:50.140 --> 00:11:54.720
And it's also worth noting the birthplace of JavaScript is Mozilla, right?
00:11:54.720 --> 00:11:56.500
Well, it wasn't Mozilla back then.
00:11:56.500 --> 00:12:02.400
But yeah, sort of the heritage of JavaScript sort of comes from the same place as Mozilla.
00:12:02.400 --> 00:12:03.080
Yeah, absolutely.
00:12:03.080 --> 00:12:03.700
Yeah, absolutely.
00:12:03.820 --> 00:12:20.280
So when I think about JavaScript frameworks, there's the kind of more lightweight ones where it's, I'm writing, I'm taking some JavaScript API and I'm writing some more JavaScript code to write maybe a single page application.
00:12:20.540 --> 00:12:26.660
So I'm thinking of like Angular, you know, those types of things, jQuery, so on.
00:12:26.660 --> 00:12:38.100
And then at the other end, there's an idea of bringing more stuff into the JavaScript world using the concept of something called asm.js.
00:12:38.600 --> 00:12:40.040
Can you speak to that a little bit?
00:12:40.040 --> 00:12:45.300
So asm.js is a funny beast.
00:12:45.300 --> 00:13:00.280
It's sort of this tiny little subset of JavaScript, which if you look at it in the right light and you kind of suspend disbelief for long enough,
00:13:00.280 --> 00:13:09.040
inside of JavaScript are the semantics of a 32-bit CPU, essentially.
00:13:09.040 --> 00:13:17.520
So JavaScript has, for what is a high-level language, JavaScript has a surprising number of kind of really low-level features.
00:13:17.520 --> 00:13:23.160
Like it's got 32-bit bitwise integer operators and things like this.
00:13:24.460 --> 00:13:33.720
Asm.js basically came out of an attempt by a very smart fellow named Alonza Kai to compile C code to the web.
00:13:33.720 --> 00:13:38.320
And through a series of kind of experimenting with different ways of doing that,
00:13:38.320 --> 00:13:46.560
came to realize that there was this subset of JavaScript that you could target that implemented the semantics of the machine more or less directly,
00:13:46.560 --> 00:13:51.380
that you could then take C code and transliterate into this subset called asm.js.
00:13:52.380 --> 00:13:59.200
And because of the kind of browser wars and the JavaScript performance wars that had been going on up to that time,
00:13:59.200 --> 00:14:02.100
it also happened that that stuff would run pretty fast.
00:14:02.100 --> 00:14:10.540
So he likes to describe it as discovering a kind of low-level 32-bit assembly language hidden inside JavaScript,
00:14:10.540 --> 00:14:11.720
and I think that's pretty apt.
00:14:11.720 --> 00:14:12.860
Very cool.
00:14:12.860 --> 00:14:18.960
Have you seen the video or the PyCon presentation, Birth and Death of JavaScript by Gary Bernhardt?
00:14:19.080 --> 00:14:22.080
I was present in the audience for that talk, and I loved it.
00:14:22.080 --> 00:14:24.100
That was an amazing talk.
00:14:24.100 --> 00:14:28.160
Those of you guys out there listening who haven't seen this, I'll put it in the links.
00:14:28.160 --> 00:14:30.500
And I've referenced it before on the show.
00:14:30.500 --> 00:14:39.080
But he did a really good job of showing off Asm.js and basically bringing anything that can be written in C to the web.
00:14:39.900 --> 00:14:46.120
And one of those was like a pretty high-end video game like Quake or something like this, right?
00:14:46.120 --> 00:14:53.540
They're doing sort of more – within half the speed of Native, I think they usually get these things running.
00:14:53.540 --> 00:14:54.380
It's pretty amazing.
00:14:54.380 --> 00:14:56.060
Yeah, very, very amazing.
00:14:56.440 --> 00:15:01.320
And if you say high-end game like Quake, but I sort of sense that presentation has gone past,
00:15:01.320 --> 00:15:05.020
I think they actually have been launching with even more kind of –
00:15:05.020 --> 00:15:12.680
the very latest and greatest gaming frameworks are starting to ship support for generating Asm.js JavaScript natively.
00:15:12.680 --> 00:15:13.740
That's really amazing.
00:15:14.420 --> 00:15:19.840
Okay, so there's this idea of we can take C code and we can compile it not to machine instructions,
00:15:19.840 --> 00:15:28.500
but we can compile it to a special subset of JavaScript that then can be translated by the browsers to execute very fast.
00:15:28.500 --> 00:15:28.940
Correct.
00:15:28.940 --> 00:15:31.400
So how does this relate to your project, PyPy.js?
00:15:31.400 --> 00:15:38.780
The PyPy.js is sort of a coming together of what to me are two amazing feats of engineering, right?
00:15:38.780 --> 00:15:46.440
And one of them was Asm.js and Emscripten and this whole idea that you can treat the web more or less like a compile target,
00:15:46.440 --> 00:15:48.840
as if it were a native CPU.
00:15:48.840 --> 00:15:52.900
The other half of that, of course, is the PyPy Python interpreter,
00:15:52.900 --> 00:15:55.960
which I think you had an episode on not too long ago.
00:15:55.960 --> 00:15:56.860
That's right.
00:15:56.860 --> 00:15:58.020
That was show 21.
00:15:58.020 --> 00:16:01.660
And PyPy, the interpreter, does some really amazing stuff.
00:16:01.660 --> 00:16:02.560
It's incredible.
00:16:02.560 --> 00:16:08.560
So one of the things that I think actually was talked about a little bit in that episode,
00:16:08.560 --> 00:16:15.100
but one of the things I really like about PyPy is they weren't just focused on writing a really fast Python interpreter.
00:16:15.100 --> 00:16:21.020
They were focused on creating a platform for experimenting with interpreters.
00:16:21.020 --> 00:16:29.040
So it seemed to me the perfect kind of opportunity to learn more about both of these projects.
00:16:29.040 --> 00:16:30.320
I'll learn more about PyPy.
00:16:30.320 --> 00:16:32.820
I'll learn more about Emscripten and Asm.js.
00:16:33.940 --> 00:16:40.380
Because PyPy is structured in such a way to make it really easy to dive in and kind of change bits in and out and play around with it,
00:16:40.380 --> 00:16:48.620
I had this idea that I could get in there and like, well, I'll just find the just-in-time compiler backend inside PyPy,
00:16:48.620 --> 00:16:54.080
and we'll just change it to emit Asm.js instead of, you know, x86 or ARM assembly,
00:16:54.500 --> 00:16:58.860
which in a lot of projects would be kind of a terrifying proposition.
00:16:58.860 --> 00:17:04.660
But because of the amount of kind of just good abstractions and so forth that are inside PyPy,
00:17:04.660 --> 00:17:08.500
it actually was quite approachable as a problem.
00:17:08.500 --> 00:17:18.480
So the coming together basically looks like the PyPy interpreter toolchain run through the Emscripten compiler toolchain
00:17:18.480 --> 00:17:22.560
so that you get a version of it that is compiled to JavaScript.
00:17:22.560 --> 00:17:28.820
And then at runtime, it uses the PyPy just-in-time compiler infrastructure
00:17:28.820 --> 00:17:34.100
to take your Python code that you're running and spit out Asm.js at runtime,
00:17:34.100 --> 00:17:39.400
in the same way that a native PyPy interpreter would spit out kind of specialized native code for your application.
00:17:39.400 --> 00:17:45.900
So very much treating Asm.js as if it were just another dialect of some assembly language for a machine.
00:17:46.300 --> 00:17:46.980
That's so amazing.
00:17:46.980 --> 00:17:51.240
How long did it take you to get a proof of concept that was actually doing something?
00:17:51.240 --> 00:17:58.820
I think my very first proof of concept without the just-in-time compiler part came together pretty quickly.
00:17:58.820 --> 00:18:07.320
I lost maybe two weeks trying to figure out that Emscripten was actually assigning file descriptors
00:18:07.320 --> 00:18:09.700
at different numbers than they usually are in Unix.
00:18:09.700 --> 00:18:15.700
So like standard in is usually file descriptor zero, standard out is usually file descriptor one.
00:18:16.000 --> 00:18:18.760
And it was giving them different numbers because it had never mattered before.
00:18:18.760 --> 00:18:20.360
But PyPy really didn't like it.
00:18:20.360 --> 00:18:24.860
So there are a few kind of just like toolchain compatibility problems like that.
00:18:24.860 --> 00:18:32.140
But sort of once I got that figured out, right, is sort of just compile it as if you were running it through a cross compiler
00:18:32.140 --> 00:18:34.160
and you get it up and running pretty quickly.
00:18:34.680 --> 00:18:39.640
The JIT backend, I probably worked on, on and off for the better part of a year.
00:18:39.640 --> 00:18:45.580
I conceived this scheme at the end of PyCon Australia probably three years ago.
00:18:45.580 --> 00:18:51.020
And then I presented the kind of final first version of it at the following PyCon Australia the next year.
00:18:52.160 --> 00:18:58.420
In your presentation, you said your goal was really to bring support to the web for Python.
00:18:58.420 --> 00:19:04.560
And in your mind, that had to sort of meet three criteria, compatibility, performance, and webbishness.
00:19:04.560 --> 00:19:06.660
Can you talk about that?
00:19:06.660 --> 00:19:07.980
I wish I'd found a better word for that.
00:19:07.980 --> 00:19:10.860
I think that's the official word, yeah.
00:19:10.860 --> 00:19:11.700
Yeah.
00:19:12.860 --> 00:19:17.800
So this is not the first time people have tried to do Python-y stuff on the web.
00:19:17.800 --> 00:19:21.220
I think I even said in the talk it probably won't be the last.
00:19:21.220 --> 00:19:31.780
But it's one of those classic cases that you get in software a lot of a triangle where the three goals are kind of more or less opposed to each other.
00:19:31.780 --> 00:19:35.120
So you've had languages that compile to JavaScript.
00:19:35.120 --> 00:19:36.860
That's a thing that we know how to do.
00:19:36.860 --> 00:19:40.120
And we know how to do that relatively fast.
00:19:40.560 --> 00:19:46.080
As long as your language semantically maps to JavaScript pretty well.
00:19:46.080 --> 00:19:47.660
Python doesn't.
00:19:47.660 --> 00:19:51.960
So in Python, you have big ints by default.
00:19:51.960 --> 00:19:58.280
You have a variety of checks built into your arithmetic operators and a few things like that.
00:19:58.280 --> 00:20:05.300
So the semantics of Python are actually quite different to the semantics of JavaScript, even though they look very similar.
00:20:05.300 --> 00:20:08.780
On the one hand, it's easy to make something go fast in JavaScript.
00:20:08.780 --> 00:20:11.120
If you're not too far from the semantics of JavaScript.
00:20:11.120 --> 00:20:16.780
On the other hand, we know how to compile languages from one to the other, right?
00:20:16.780 --> 00:20:18.940
It's sort of a Turing completeness argument.
00:20:18.940 --> 00:20:34.740
So if we wanted to get a really compliant Python interpreter, you know, one that implements all of the edge cases of CPython up and running on a different platform, you know, it's not implausible that we can get that up and running.
00:20:34.820 --> 00:20:36.780
So it's interesting to see how fast we can do it.
00:20:36.780 --> 00:20:41.760
So there's kind of two relatively opposed goals there, right?
00:20:41.760 --> 00:20:46.200
Being fast and being – I like to say being Python, right?
00:20:46.240 --> 00:20:53.080
But doing all of the things that Python does, not kind of looking a bit like Python, but having JavaScript's number model and things like that.
00:20:53.080 --> 00:20:53.460
Right.
00:20:53.460 --> 00:20:53.880
Exactly.
00:20:53.880 --> 00:21:00.680
There's all these – you know, when you talk to people about JavaScript, they're like, oh, there's all these JavaScript gotchas you have to be on the lookout for.
00:21:01.060 --> 00:21:06.320
I don't typically hear people go, look out for all the Python gotchas because they're lurking everywhere, right?
00:21:06.320 --> 00:21:06.760
Right.
00:21:06.760 --> 00:21:08.700
Python's usually got your back.
00:21:19.700 --> 00:21:22.320
This episode is brought to you by Hired.
00:21:22.320 --> 00:21:28.780
Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
00:21:28.780 --> 00:21:37.940
Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
00:21:37.940 --> 00:21:44.300
Typically, candidates receive five or more offers in just the first week, and there are no obligations ever.
00:21:44.300 --> 00:21:46.400
Sounds pretty awesome, doesn't it?
00:21:46.660 --> 00:21:48.460
Well, did I mention there's a signing bonus?
00:21:48.460 --> 00:21:56.900
Everyone who accepts a job from Hired gets a $2,000 signing bonus, and as Talk Python listeners, it gets way sweeter.
00:21:56.900 --> 00:22:04.460
Use the link Hired.com slash Talk Python To Me, and Hired will double the signing bonus to $4,000.
00:22:04.460 --> 00:22:06.180
Opportunity's knocking.
00:22:06.180 --> 00:22:09.780
Visit Hired.com slash Talk Python To Me and answer the call.
00:22:09.780 --> 00:22:20.380
It's usually got your back.
00:22:20.380 --> 00:22:20.760
That's right.
00:22:20.760 --> 00:22:33.860
And so you can't just, for example, translate floats in Python directly over necessarily to just run as JavaScript or numbers, especially integers, like you say, over to JavaScript numbers.
00:22:33.860 --> 00:22:34.640
So there's all that.
00:22:34.640 --> 00:22:36.320
That's the compatibility bit, right?
00:22:36.320 --> 00:22:36.680
Right.
00:22:37.480 --> 00:22:39.520
So, yeah, that's the compatibility aspect.
00:22:39.520 --> 00:22:42.120
And things like parts of the standard library as well.
00:22:42.120 --> 00:22:51.400
You know, do you have all of a full implementation of the sys module, including get frame and kind of the debugger and all of these things?
00:22:52.740 --> 00:22:54.280
And it's interesting.
00:22:54.280 --> 00:22:57.700
It's sort of like, well, I don't really use those things that often.
00:22:57.700 --> 00:23:05.200
You know, I don't really depend on integer overflows escalating to big ints by default that often.
00:23:05.200 --> 00:23:07.700
Or, you know, I don't use sys get frame that often.
00:23:07.960 --> 00:23:08.960
But it's interesting.
00:23:08.960 --> 00:23:09.140
But it's interesting.
00:23:09.140 --> 00:23:22.400
As soon as you start trying to run, like, whole programs on Python, like, you'll find that, oh, you use some library somewhere that uses some library that happens to use one of those things, you know, in some corner of its API.
00:23:23.400 --> 00:23:28.720
So even though they seem like edge cases, they're often not at a whole program level.
00:23:28.720 --> 00:23:29.300
Right.
00:23:29.300 --> 00:23:32.040
They're not necessarily edge cases for small, simple things.
00:23:32.040 --> 00:23:37.620
But if you're going to really write major apps there, then all of a sudden those become things you have to worry about, right?
00:23:37.620 --> 00:23:38.340
Right.
00:23:38.340 --> 00:23:43.840
And something I'm very interested in is less about kind of writing Greenfield's Python code for the web.
00:23:43.840 --> 00:23:47.940
Because, you know, personally, I am not that unhappy writing JavaScript.
00:23:47.940 --> 00:23:54.120
I'm interested in taking Python code that you've written and putting it on the web without having to do a whole lot of work.
00:23:54.120 --> 00:23:57.960
And the compatibility aspect really is really important for things like that.
00:23:57.960 --> 00:23:59.100
Right.
00:23:59.100 --> 00:24:02.000
Like script source equals some file dot py.
00:24:02.000 --> 00:24:03.240
Something like this, right?