lua.js revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1/* 2 * Copyright (c) 2013 The Chromium Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 */ 6 7NaClTerm.prefix = 'lua' 8NaClTerm.nmf = '//storage.googleapis.com/gonacl/demos/publish/234230_dev/lua/lua.nmf' 9 10window.onload = function() { 11 lib.init(function() { 12 document.getElementById('shell').addEventListener('click', runLua, false); 13 document.getElementById('scimark').addEventListener('click', runSciMark, false); 14 document.getElementById('unittests').addEventListener('click', runUnitTests, false); 15 document.getElementById('binarytrees').addEventListener('click', runBinaryTrees, false); 16 document.getElementById('mandlebrot').addEventListener('click', runMandlebrot, false); 17 document.getElementById('donut').addEventListener('click', runDonut, false); 18 document.getElementById('fibonacci').addEventListener('click', runFibonacci, false); 19 document.getElementById('banner').addEventListener('click', runBanner, false); 20 NaClTerm.init(); 21 }); 22}; 23 24function runLua() { 25 NaClTerm.argv = []; 26 term_.command.restartNaCl(); 27} 28 29function runUnitTests() { 30 NaClTerm.argv = ['-e_U=true', 'all.lua'] 31 term_.command.restartNaCl(); 32 return false; 33} 34 35function runSciMark(e) { 36 NaClTerm.argv = ['/mnt/http/scimark.lua'] 37 term_.command.restartNaCl(); 38 return false; 39} 40 41function runBinaryTrees(e) { 42 NaClTerm.argv = ['/mnt/http/binarytrees.lua'] 43 term_.command.restartNaCl(); 44 return false; 45} 46 47function runMandlebrot(e) { 48 if (!term_.command.loaded) 49 runLua(); 50 51 term_.command.sendMessage('for i=-1,1,.08 do for r=-2,1,.04 do I=i R=r n=0 b=I*I while n<26 and R*R+b<4 do I=2*R*I+i R=R*R-b+r b=I*I n=n+1 end io.write(string.char(n+32)) end print() end') 52 53 document.getElementById('terminal').focus(); 54} 55 56function runFibonacci(e) { 57 if (!term_.command.loaded) 58 runLua(); 59 60 term_.command.sendMessage('function fib(n) return n<2 and n or fib(n-1)+fib(n-2) end\nprint(fib(10))\nprint(fib(30))\n') 61 62 document.getElementById('terminal').focus(); 63} 64 65function runBanner(e) { 66 if (!term_.command.loaded) 67 runLua(); 68 69 term_.command.sendMessage( 70't=0 A={64,36,35,42,33,61,59,58,126,45,44,46,32}L={0,254,195,3,224,135,231,127, \ 71124,60,128,255,120,158,207,199,241,252,192,121,30,31,61,227,1,223,243,188,63,251 \ 72,190,193,28,156,62,248,143,7,126}D={2,3,3,4,1,5,6,7,8,9,10,1,11,12,13,14,15,16,4 \ 73,1,10,14,7,17,18,10,1,19,3,20,21,15,15,3,22,21,11,7,17,18,23,2,24,25,13,14,15,26 \ 74,27,13,21,11,7,8,28,29,19,7,25,13,2,3,30,3,8,21,11,7,25,10,29,31,7,32,13,21,19, \ 7527,27,13,33,34,7,25,10,35,15,16,24,20,21,19,24,27,8,36,37,7,25,10,10,31,38,39,13 \ 76}B=bit32 M=math I=io.write T=string.char P=print S=M.sin C=M.cos F=M.floor b={} \ 77for i=1,1200 do b[i]=0 end for i,v in ipairs(D)do w=L[v]for j=1,8 do if B.band(w \ 78,1)==1 then b[160-48+i*8+j]=1 end w=B.rshift(w,1)end end P("\\x1b[2J") for w=1, \ 794720 do P("\\x1b[H")s=1.06*S(t*.07)^2 for i=1,1200 do x=s*((i%60)-34)+34 y=s*(F(i \ 80/60)-15)+15 n=32 if y>=0 and y<20 and x>=0 and x<60 and b[F(y)*60+F(x)]==1 then \ 81n=A[M.min(F(((x-30+C(t)*20)^2+(y-S(t)^2*10)^2)/(195*S(t*.2)^32+5)),12)+1]end if \ 82i%60==0 then n=10 end I(T(n))end t=t+.005 end'); 83 84 document.getElementById('terminal').focus(); 85} 86 87function runDonut(e) { 88 if (!term_.command.loaded) 89 runLua(); 90 91 term_.command.sendMessage( 92' A=0 B=0 z={}b=\n\ 93 {}E={32,46,44,45,126,\n\ 94 58,59,61,33,42,35,36,64}S\n\ 95 =math.sin C=math.cos F=math.\n\ 96 floor I=io.write T=string.char W=60\n\ 97 P=print H=25 P("\\x1b[2J")for w=1,240\n\ 98 do for o=0,W*H do b[o]=1 z[o]=0 end e=\n\ 99 S(A)g=C(A)m=C(B)n=S(B)for j=0,6.28,.09\n\ 100 do d=C(j)f=S(j) for i=0,6.28,.04\n\ 101do c=S(i)h=d+2 D =1/(c*h*e+f*g+5)\n\ 102l=C(i)t=c*h*g- f*e x=F(W/2+W*\n\ 103.3*D*(l*h*m-t*n ))y=F(H/2+H*.6*\n\ 104 D*(l*h*n+t*m))o =x+W*y N=math.\n\ 105 max(0,F(8*((f*e-c*d*g)*m-c*d*e-f*g -l*\n\ 106 d *n)))+2 if H> y and y>0 and x>0 and\n\ 107 W>x and D> z[o] then z[o]=D b[o]=N\n\ 108 end end end P( "\\x1b[H")for k=0\n\ 109 ,W* H do if k%W~=0 then I(T(\n\ 110 E[b[k]]))else I( T( 10))\n\ 111 end end A = A + .04\n\ 112 B=B+.02 end') 113 114 document.getElementById('terminal').focus(); 115} 116