Last active
September 9, 2015 18:32
-
-
Save 5HT/512ccd71847538600627 to your computer and use it in GitHub Desktop.
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
dvp = DataView.prototype; | |
dvp.gu=dvp.getUint8;dvp.gu2=dvp.getUint16; | |
dvp.gi4=dvp.getInt32;dvp.gu4=dvp.getUint32; | |
function itoa(x) { return String.fromCharCode(x); } | |
function flat(a) { return a.reduce(function(f,t){return f.concat(Array.isArray(t)?flat(t):t);},[]); } | |
// BERT Encoder | |
function atom(o) { return {t:100,v:utf8_toByteArray(o)}; } | |
function bin(o) { return {t:109,v:utf8_toByteArray(o)}; } | |
function tuple() { return {t:104,v:arguments}; } | |
function enc(o) { console.log(o); return new Blob(flat([131,ein(o)])); } | |
function ein(o) { return eval('en_'+o.t)(o); } | |
function en_98(o) { return [98,o.v>>>24,o.v>>>16&255,o.v>>>8&255,o.v&255]; } | |
function en_100(o) { return [100,o.v.length>>>8,o.v.length&255,o.v];} | |
function en_104(o) { return [104,o.v.length,o.v.map(function(x){return ein(x);})]; } | |
function en_109(o) { var l=o.v.length;return[108,l>>>24,l>>>16&255,l>>>8&255,l&255,o.v]; } | |
function en_108(o) { var l=o.v.length;return[108,l>>>24,l>>>16&255,l>>>8&255,l&255, | |
o.v.map(function(x){return ein(x);})]; } | |
// BERT Decoder | |
function nop(b) { return []; }; | |
function int(b) { return b==1?sx.gu(ix++):sx.gi4((a=ix,ix+=4,a)); }; | |
function dec(d) { sx=new DataView(d);ix=0; if(sx.gu(ix++)!==131)throw("BERT?"); return din();}; | |
function str(b) { var sz=b==2?sx.gu2(ix):sx.gi4(ix);ix+=b; | |
return utf8_dec(new DataView(sx.buffer.slice(ix,ix+=sz))); }; | |
function run(b) { var i=sz=b==1?sx.gu(ix):sx.gu4(ix),r=[]; ix+=b; | |
while(i--) r[sz-i-1]=din(); if(b==4)ix++; return r; }; | |
function din() { var c=sx.gu(ix++),x; switch(c) { case 97: x=[int,1];break; | |
case 98: x=[int,4]; break; case 100: x=[str,2]; break; | |
case 104: x=[run,1]; break; case 107: x=[str,2]; break; | |
case 108: x=[run,4]; break; case 109: x=[str,4]; break; | |
default: x=[nop,0]; } return {t:c,v:x[0](x[1])};}; | |
/// BERT Protocol | |
function isTUPLE(x,num,name) { return (x.v.length === num && x.v[0].v === name); } | |
var $io = {}; $io.on = function onio(x, cb) { if (isTUPLE(x,3,'io')) { | |
try { eval(x.v[1].v); if (typeof cb == 'function') cb(x); } catch (e) { console.log(e); return { status: '' }; } | |
return { status: "ok" }; } else return { status: '' }; } | |
var $file = {}; $file.on = function onfile(x, cb) { if (isTUPLE(x,12,'ftp')) { | |
if (typeof cb == 'function') cb(x); return { status: "ok" }; } else return { status: ''}; } | |
var $bin = {}; $bin.on = function onbin(x, cb) { if (isTUPLE(x,2,'bin')) { | |
if (typeof cb == 'function') cb(x); return { status: "ok" }; } else return { status: '' }; } | |
var $bert = {}; $bert.protos = [$io,$bin,$file]; | |
$bert.on = function onbert(evt, cb) { | |
if (Blob.prototype.isPrototypeOf(evt.data) && (evt.data.length > 0 || evt.data.size > 0)) { | |
var r = new FileReader(); | |
r.addEventListener("loadend", function() { | |
try { erlang = dec(r.result); | |
if (debug) console.log(JSON.stringify(erlang.v)); | |
if (typeof cb == 'function') cb(erlang); | |
for (var i=0;i<$bert.protos.length;i++) { | |
p = $bert.protos[i]; if (p.on(erlang, p.do).status == "ok") return; } | |
} catch (e) { console.log(e); } }); | |
r.readAsArrayBuffer(evt.data); | |
return { status: "ok" }; } else return { status: "error", desc: "data" }; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment