Skip to content

Instantly share code, notes, and snippets.

@holyshared
Last active May 13, 2018 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyshared/3233eb6e5474a0e226ce4216dff0fcd1 to your computer and use it in GitHub Desktop.
Save holyshared/3233eb6e5474a0e226ce4216dff0fcd1 to your computer and use it in GitHub Desktop.
*.byte
*.cmi
*.cmo
<!doctype>
<html>
<head>
<title>example</title>
<script src="num_op.js"></script>
<script>
console.log(num_op.increment([1, 2]));
</script>
</head>
<body>
</body>
</html>
all:
ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg \
-o num_op.byte num_op.mli num_op.ml
js_of_ocaml num_op.byte
clean:
rm *.byte *.cmi *.cmo *.js
// Generated by js_of_ocaml 3.1.0
(function(b){"use strict";var
af=" : flags Open_text and Open_binary are not compatible",U="Sys_error",ae=224,ad="Invalid_argument",s=1024,A="jsError",T=57343,ac=512,ab="End_of_file",aa="Failure",R="Undefined_recursive_module",S=" : flags Open_rdonly and Open_wronly are not compatible",Q="([^/]*)",$="Stack_overflow",g=128,c="",z="^",M=" : file already exists",N=2048,O=56320,P=240,f=248,_="Not_found",L="Assert_failure",m="/",Y="Sys_blocked_io",Z="fd ",K="Out_of_memory",X="Match_failure",V="Division_by_zero",W="static/",aH=0;function
u(){return aH++}function
az(){return typeof
module!=="undefined"&&module&&module.exports?module.exports:b}function
ak(a){if(a.length<24){for(var
b=0;b<a.length;b++)if(a.charCodeAt(b)>127)return false;return true}else
return!/[^\x00-\x7f]/.test(a)}function
aT(f){for(var
h=c,b=h,a,j,d=0,i=f.length;d<i;d++){a=f.charCodeAt(d);if(a<g){for(var
e=d+1;e<i&&(a=f.charCodeAt(e))<g;e++);if(e-d>ac){b.substr(0,1);h+=b;b=c;h+=f.slice(d,e)}else
b+=f.slice(d,e);if(e==i)break;d=e}if(a<N){b+=String.fromCharCode(192|a>>6);b+=String.fromCharCode(g|a&63)}else
if(a<55296||a>=T)b+=String.fromCharCode(ae|a>>12,g|a>>6&63,g|a&63);else
if(a>=56319||d+1==i||(j=f.charCodeAt(d+1))<O||j>T)b+="\xef\xbf\xbd";else{d++;a=(a<<10)+j-56613888;b+=String.fromCharCode(P|a>>18,g|a>>12&63,g|a>>6&63,g|a&63)}if(b.length>s){b.substr(0,1);h+=b;b=c}}return h+b}function
aL(b,a){if(a.repeat)return a.repeat(b);var
d=c,e=0;if(b==0)return d;for(;;){if(b&1)d+=a;b>>=1;if(b==0)return d;a+=a;e++;if(e==9)a.slice(0,1)}}function
F(d,e,c){var
b=new
Array(c);for(var
a=0;a<c;a++)b[a]=d[e+a];return b}function
E(b,d,a){var
e=String.fromCharCode;if(d==0&&a<=4096&&a==b.length)return e.apply(null,b);var
f=c;for(;0<a;d+=s,a-=s)f+=e.apply(null,F(b,d,Math.min(a,s)));return f}function
ai(a){if(a.t==2)a.c+=aL(a.l-a.c.length,"\0");else
a.c=E(a.c,0,a.c.length);a.t=0}function
aS(f){for(var
l=c,d=c,i,h,j,a,b=0,k=f.length;b<k;b++){h=f.charCodeAt(b);if(h<g){for(var
e=b+1;e<k&&(h=f.charCodeAt(e))<g;e++);if(e-b>ac){d.substr(0,1);l+=d;d=c;l+=f.slice(b,e)}else
d+=f.slice(b,e);if(e==k)break;b=e}a=1;if(++b<k&&((j=f.charCodeAt(b))&-64)==g){i=j+(h<<6);if(h<ae){a=i-12416;if(a<g)a=1}else{a=2;if(++b<k&&((j=f.charCodeAt(b))&-64)==g){i=j+(i<<6);if(h<P){a=i-925824;if(a<N||a>=55295&&a<57344)a=2}else{a=3;if(++b<k&&((j=f.charCodeAt(b))&-64)==g&&h<245){a=j-63447168+(i<<6);if(a<65536||a>1114111)a=3}}}}}if(a<4){b-=a;d+="\ufffd"}else
if(a>65535)d+=String.fromCharCode(55232+(a>>10),O+(a&1023));else
d+=String.fromCharCode(a);if(d.length>s){d.substr(0,1);l+=d;d=c}}return l+d}function
aR(a){switch(a.t){case
9:return a.c;default:ai(a);case
0:if(ak(a.c)){a.t=9;return a.c}a.t=8;case
8:return aS(a.c)}}function
n(c,a,b){this.t=c;this.c=a;this.l=b}n.prototype.toString=function(){return aR(this)};function
D(a){var
b=9;if(!ak(a))b=8,a=aT(a);return new
n(b,a,a.length)}function
aX(c,e){var
d=c.length,b=new
Array(d+1),a=0;for(;a<d;a++)b[a]=c[a];b[a]=e;return b}function
p(b,a){if(b.fun)return p(b.fun,a);var
c=b.length,d=a.length,e=c-d;if(e==0)return b.apply(null,a);else
if(e<0)return p(b.apply(null,F(a,0,c)),F(a,c,d-c));else
return function(c){return p(b,aX(a,c))}}function
aA(a){return function(){return arguments.length>0?p(a,arguments):p(a,[undefined])}}function
aY(c,e){var
d=c.length,b=new
Array(d+1);b[0]=e;for(var
a=1;a<=d;a++)b[a]=c[a-1];return b}function
aB(a){return function(){return p(a,aY(arguments,this))}}function
aJ(b,a){throw[0,b,a]}function
d(a){return new
n(0,a,a.length)}function
ap(b,a){aJ(b,d(a))}var
a=[0];function
j(b){ap(a.Sys_error,b)}var
o=new
Array();function
aD(e){var
b=o[e];if(!b.opened)j("Cannot flush a closed channel");if(!b.buffer||b.buffer==c)return 0;if(b.fd&&a.fds[b.fd]&&a.fds[b.fd].output){var
d=a.fds[b.fd].output;switch(d.length){case
2:d(e,b.buffer);break;default:d(b.buffer)}}b.buffer=c;return 0}if(b.process&&b.process.cwd)var
t=b.process.cwd().replace(/\\/g,m);else
var
t="/static";if(t.slice(-1)!==m)t+=m;function
aC(a){a=a
instanceof
n?a.toString():a;if(a.charCodeAt(0)!=47)a=t+a;var
e=a.split(m),b=[];for(var
d=0;d<e.length;d++)switch(e[d]){case"..":if(b.length>1)b.pop();break;case".":break;case"":if(b.length==0)b.push(c);break;default:b.push(e[d]);break}b.orig=a;return b}function
aI(a){a=a
instanceof
n?a.toString():a;j(a+": No such file or directory")}function
aN(a){return new
n(4,a,a.length)}function
aj(b){ap(a.Invalid_argument,b)}function
as(){aj("index out of bounds")}function
aP(a,b){switch(a.t&6){default:if(b>=a.c.length)return 0;case
0:return a.c.charCodeAt(b);case
4:return a.c[b]}}function
aM(b,a){if(a>>>0>=b.l)as();return aP(b,a)}function
q(a){if(a<0)aj("String.create");return new
n(a?2:9,c,a)}function
am(a){return a.l}function
C(c){if(b.Uint8Array)var
d=new(b.Uint8Array)(c.l);else
var
d=new
Array(c.l);var
f=c.c,e=f.length,a=0;for(;a<e;a++)d[a]=f.charCodeAt(a);for(e=c.l;a<e;a++)d[a]=0;c.c=d;c.t=4;return d}function
w(d,e,b,f,c){if(c==0)return 0;if(f==0&&(c>=b.l||b.t==2&&c>=b.c.length)){b.c=d.t==4?E(d.c,e,c):e==0&&d.c.length==c?d.c:d.c.substr(e,c);b.t=b.c.length==b.l?0:2}else
if(b.t==2&&f==b.c.length){b.c+=d.t==4?E(d.c,e,c):e==0&&d.c.length==c?d.c:d.c.substr(e,c);b.t=b.c.length==b.l?0:2}else{if(b.t!=4)C(b);var
g=d.c,h=b.c;if(d.t==4)if(f<=e)for(var
a=0;a<c;a++)h[f+a]=g[e+a];else
for(var
a=c-1;a>=0;a--)h[f+a]=g[e+a];else{var
i=Math.min(c,g.length-e);for(var
a=0;a<i;a++)h[f+a]=g.charCodeAt(e+a);for(;a<c;a++)h[f+a]=0}}return 0}function
ag(){}function
e(a){this.data=a}e.prototype=new
ag();e.prototype.truncate=function(a){var
b=this.data;this.data=q(a|0);w(b,0,this.data,0,a)};e.prototype.length=function(){return am(this.data)};e.prototype.write=function(b,d,g,a){var
c=this.length();if(b+a>=c){var
e=q(b+a),f=this.data;this.data=e;w(f,0,this.data,0,c)}w(d,g,this.data,b,a);return 0};e.prototype.read=function(c,a,d,b){var
e=this.length();w(this.data,c,a,d,b);return 0};e.prototype.read_one=function(a){return aM(this.data,a)};e.prototype.close=function(){};e.prototype.constructor=e;function
h(b,a){this.content={};this.root=b;this.lookupFun=a}h.prototype.nm=function(a){return this.root+a};h.prototype.lookup=function(a){if(!this.content[a]&&this.lookupFun){var
b=this.lookupFun(d(this.root),d(a));if(b!=0)this.content[a]=new
e(b[1])}};h.prototype.exists=function(a){if(a==c)return 1;var
d=a+m,e=new
RegExp(z+d);for(var
b
in
this.content)if(b.match(e))return 1;this.lookup(a);return this.content[a]?1:0};h.prototype.readdir=function(d){var
g=d==c?c:d+m,h=new
RegExp(z+g+Q),e={},b=[];for(var
f
in
this.content){var
a=f.match(h);if(a&&!e[a[1]]){e[a[1]]=true;b.push(a[1])}}return b};h.prototype.is_dir=function(a){var
e=a==c?c:a+m,f=new
RegExp(z+e+Q),g=[];for(var
d
in
this.content){var
b=d.match(f);if(b)return 1}return 0};h.prototype.unlink=function(a){var
b=this.content[a]?true:false;delete
this.content[a];return b};h.prototype.open=function(a,b){if(b.rdonly&&b.wronly)j(this.nm(a)+S);if(b.text&&b.binary)j(this.nm(a)+af);this.lookup(a);if(this.content[a]){if(this.is_dir(a))j(this.nm(a)+" : is a directory");if(b.create&&b.excl)j(this.nm(a)+M);var
c=this.content[a];if(b.truncate)c.truncate();return c}else
if(b.create){this.content[a]=new
e(q(0));return this.content[a]}else
aI(this.nm(a))};h.prototype.register=function(b,a){if(this.content[b])j(this.nm(b)+M);if(a
instanceof
n)this.content[b]=new
e(a);else
if(a
instanceof
Array)this.content[b]=new
e(aN(a));else
if(a.toString){var
c=d(a.toString());this.content[b]=new
e(c)}};h.prototype.constructor=h;function
ah(a){if(a.t!=4)C(a);return a.c}function
aQ(a,c,b){b&=255;if(a.t!=4){if(c==a.c.length){a.c+=String.fromCharCode(b);if(c+1==a.l)a.t=0;return 0}C(a)}a.c[c]=b;return 0}function
aO(b,a,c){if(a>>>0>=b.l)as();return aQ(b,a,c)}var
B=b.Buffer;function
l(a){this.fs=require("fs");this.fd=a}l.prototype=new
ag();l.prototype.truncate=function(a){this.fs.ftruncateSync(this.fd,a|0)};l.prototype.length=function(){return this.fs.fstatSync(this.fd).size};l.prototype.write=function(g,c,d,f){var
a=ah(c);if(!(a
instanceof
b.Uint8Array))a=new(b.Uint8Array)(a);var
e=new
B(a);this.fs.writeSync(this.fd,e,d,f,g);return 0};l.prototype.read=function(h,e,d,g){var
a=ah(e);if(!(a
instanceof
b.Uint8Array))a=new(b.Uint8Array)(a);var
f=new
B(a);this.fs.readSync(this.fd,f,d,g,h);for(var
c=0;c<g;c++)aO(e,d+c,f[d+c]);return 0};l.prototype.read_one=function(d){var
c=new(b.Uint8Array)(1),a=new
B(c);this.fs.readSync(this.fd,a,0,1,d);return a[0]};l.prototype.close=function(){this.fs.closeSync(this.fd)};l.prototype.constructor=l;function
k(a){this.fs=require("fs");this.root=a}k.prototype.nm=function(a){return this.root+a};k.prototype.exists=function(a){return this.fs.existsSync(this.nm(a))?1:0};k.prototype.readdir=function(a){return this.fs.readdirSync(this.nm(a))};k.prototype.is_dir=function(a){return this.fs.statSync(this.nm(a)).isDirectory()?1:0};k.prototype.unlink=function(a){var
b=this.fs.existsSync(this.nm(a))?1:0;this.fs.unlinkSync(this.nm(a));return b};k.prototype.open=function(f,c){var
a=require("constants"),b=0;for(var
e
in
c)switch(e){case"rdonly":b|=a.O_RDONLY;break;case"wronly":b|=a.O_WRONLY;break;case"append":b|=a.O_WRONLY|a.O_APPEND;break;case"create":b|=a.O_CREAT;break;case"truncate":b|=a.O_TRUNC;break;case"excl":b|=a.O_EXCL;break;case"binary":b|=a.O_BINARY;break;case"text":b|=a.O_TEXT;break;case"nonblock":b|=a.O_NONBLOCK;break}var
d=this.fs.openSync(this.nm(f),b);return new
l(d)};k.prototype.rename=function(b,a){this.fs.renameSync(this.nm(b),this.nm(a))};k.prototype.constructor=k;var
r=t.match(/[^\/]*\//)[0],v=[];if(typeof
module!=="undefined"&&module.exports&&typeof
require!=="undefined")v.push({path:r,device:new
k(r)});else
v.push({path:r,device:new
h(r)});v.push({path:r+W,device:new
h(r+W)});function
aZ(b){var
f=aC(b),b=f.join(m),e=b+m,c;for(var
d=0;d<v.length;d++){var
a=v[d];if(e.search(a.path)==0&&(!c||c.path.length<a.path.length))c={path:a.path,device:a.device,rest:b.substring(a.path.length,b.length)}}return c}function
ar(e,f){var
a=o[e],c=d(f),b=am(c);a.file.write(a.offset,c,0,b);a.offset+=b;return 0}function
aV(a){var
c=b;if(c.process&&c.process.stdout&&c.process.stdout.write)c.process.stderr.write(a);else{if(a.charCodeAt(a.length-1)==10)a=a.substr(0,a.length-1);var
d=c.console;d&&d.error&&d.error(a)}}function
aW(a){var
c=b;if(c.process&&c.process.stdout&&c.process.stdout.write)c.process.stdout.write(a);else{if(a.charCodeAt(a.length-1)==10)a=a.substr(0,a.length-1);var
d=c.console;d&&d.log&&d.log(a)}}function
x(d,f,e,b){if(a.fds===undefined)a.fds=new
Array();b=b?b:{};var
c={};c.file=e;c.offset=b.append?e.length():0;c.flags=b;c.output=f;a.fds[d]=c;if(!a.fd_last_idx||d>a.fd_last_idx)a.fd_last_idx=d;return d}function
a0(d,c,h){var
b={};while(c){switch(c[1]){case
0:b.rdonly=1;break;case
1:b.wronly=1;break;case
2:b.append=1;break;case
3:b.create=1;break;case
4:b.truncate=1;break;case
5:b.excl=1;break;case
6:b.binary=1;break;case
7:b.text=1;break;case
8:b.nonblock=1;break}c=c[2]}if(b.rdonly&&b.wronly)j(d.toString()+S);if(b.text&&b.binary)j(d.toString()+af);var
e=aZ(d),f=e.device.open(e.rest,b),g=a.fd_last_idx?a.fd_last_idx:0;return x(g+1,ar,f,b)}x(0,ar,new
e(q(0)));x(1,aW,new
e(q(0)));x(2,aV,new
e(q(0)));function
aE(d){var
c=a.fds[d];if(c.flags.wronly)j(Z+d+" is writeonly");var
b={file:c.file,offset:c.offset,fd:d,opened:true,out:false,refill:null};o[b.fd]=b;return b.fd}function
al(e){var
d=a.fds[e];if(d.flags.rdonly)j(Z+e+" is readonly");var
b={file:d.file,offset:d.offset,fd:e,opened:true,out:true,buffer:c};o[b.fd]=b;return b.fd}function
aF(){var
b=0;for(var
a=0;a<o.length;a++)if(o[a]&&o[a].opened&&o[a].out)b=[0,o[a].fd,b];return b}function
aG(a){return a
instanceof
Array?a[0]:a
instanceof
n?252:1e3}function
i(d,c,b){a[d+1]=c;if(b)a[b]=c}var
ao={};function
ay(a){if((a.t&6)!=0)ai(a);return a.c}function
aK(a,b){ao[ay(a)]=b;return 0}function
aq(a){return a}function
an(a){return ao[a]}function
aU(c){if(c
instanceof
Array)return c;if(b.RangeError&&c
instanceof
b.RangeError&&c.message&&c.message.match(/maximum call stack/i))return aq(a.Stack_overflow);if(b.InternalError&&c
instanceof
b.InternalError&&c.message&&c.message.match(/too much recursion/i))return aq(a.Stack_overflow);if(c
instanceof
b.Error&&an(A))return[0,an(A),c];return[0,a.Failure,D(String(c))]}var
G=[f,d(U),-2];i(11,[f,d(R),-12],R);i(10,[f,d(L),-11],L);i(9,[f,d(Y),-10],Y);i(8,[f,d($),-9],$);i(7,[f,d(X),-8],X);i(6,[f,d(_),-7],_);i(5,[f,d(V),-6],V);i(4,[f,d(ab),-5],ab);i(3,[f,d(ad),-4],ad);i(2,[f,d(aa),-3],aa);i(1,G,U);i(0,[f,d(K),-1],K);u(0);aE(0);al(1);al(2);u(0);u(0);u(0);var
H=[0,0],aw=d("Js.Error");function
I(a){H[1]=[0,a,H[1]];return 0}var
av=b.Array,J=[f,aw,u(0)],y=[0,J,{}],au=aG(y)===f?y:y[1];aK(d(A),au);(function(a){throw a});I(function(a){return a[1]===J?[0,D(a[2].toString())]:0});I(function(a){return a
instanceof
av?0:[0,D(a.toString())]});var
ax={"increment":aB(function(b,a){return a.map(aA(function(a,c,b){return a+1}))})};az(0).num_op=ax;function
at(b){var
a=b;for(;;){if(a){var
c=a[2],d=a[1];try{aD(d)}catch(a){a=aU(a);if(a[1]!==G)throw a}var
a=c;continue}return 0}}at(aF(0));return}(function(){return this}()));
open Js_of_ocaml
let num_op = (object%js
method increment nums =
let add_one n = (Js.float_of_number n) +. 1.0
|> Js.number_of_float in
Js.array_map add_one nums
end)
let _ =
Js.export "num_op" num_op
open Js_of_ocaml
val num_op: <
increment : ((Js.number Js.t) Js.js_array) Js.t
-> (((Js.number Js.t) Js.js_array) Js.t) Js.meth;
> Js.t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment