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
[[cheerp::jsexport]] int gcd(int a, int b) | |
{ | |
//Euclidean algorithm to compute Greatest Commond Divisor | |
while (b) | |
{ | |
int temp = a % b; | |
a = b; | |
b = temp; | |
} | |
return a; |
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
"use strict"; | |
/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/ | |
var __imul=Math.imul; | |
var __fround=Math.fround; | |
var oSlot=0;var nullArray=[null];var nullObj={d:nullArray,o:0}; | |
function __Z3gcdii(La,Lb){ | |
var Lb$paddr$p02=0,La$paddr$p03=0,tmp2=0; | |
if((Lb|0)===0)return La|0; | |
Lb$paddr$p02=Lb; | |
La$paddr$p03=La; |
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
#include <algorithm> | |
#include <random> | |
#include <cheerp/types.h> | |
[[cheerp::jsexport]] void shuffle(client::Array& array) | |
{ | |
std::random_device rd; | |
std::default_random_engine rng (rd()); | |
std::shuffle (&array[0], &array[array.get_length()], rng); | |
} |
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
"use strict";/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/var Q=Math.imul;var R=Math.fround;var oSlot=0;var nullArray=[null];var nullObj={d:nullArray,o:0};function P(e){var d=null,a=null,c=0,b=0;a={i0:0,i1:0,a2:nullArray};d=new Uint8Array(16);a.a2=d;a.i0=17;a.i1=12;b=0;c=0;while(1){d[b]=I[0+c|0]|0;b=b+1|0;if((b|0)===12){d[12]=0;s(a);a=[0];c=(~~( +Math.random()*4294967296)>>>0)%2147483647|0;a[0]=(c|0)!==0?c|0:1|0;F(e,0,e,0+(e.length)|0,a);return;}c=c+1|0;continue;}}function F(h,i,f,g,e){var b=null,d=0,j=null,a=0,c=0;a=(Math.imul(g,4))-(Math.imul(i,4))|0;if((a|0)>4)if((g+ -1|0)>i){b={i0:0,i1:0};a>>>=2;c=0;while(1){b.i0=0;a=a-1|0;b.i1=a;d=D(e,b)|0;if((d|0)!==0){j=h[i+c|0];h[i+c|0]=h[(i+c|0)+d|0];h[(i+c|0)+d|0]=j;}c=c+1|0;if((i+c|0)<(g+ -1|0))continue;break;}}}function D(e,d){var a=null,c=0,b=0;c=d.i0|0;b=(d.i1|0)-c|0;a=new M();switch(b|0){case 0:return c|0;case -1:a.a0=e;a.i1=32;a.i3=2;a.i2=16;a.i5=2147418112;a.i4=2;a.i6=2147352576;a.i7=65535;a.i8=131071;return p(a)|0|0;default:c=b+1|0;b=Math.clz32(c |
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
#include <random> | |
auto getSeed() | |
{ | |
//I am not JSExported! | |
std::random_device rd; | |
return rd(); | |
} | |
class [[cheerp::jsexport]] NormalDistribution { |
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
"use strict";/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/var L=Math.imul;var M=Math.fround;var oSlot=0;var nullArray=[null];var nullObj={d:nullArray,o:0};function K(b){var a=null;a=b.a2;return +y(a,b,a);}function y(f,e,d){var c=-0.,b=-0.,a=-0.;if((f.i3&1)!==0){f.i3=0;b=+f.d2;}else{while(1){b=+k(e);b=b*2+ -1;c= +k(e)*2+ -1;a=b*b+c*c;if(a>1)continue;if(a===0)continue;break;}a=+Math.sqrt( +Math.log(a)* -2/a);f.d2=c*a;f.i3=1;b*=a;}return b* +d.d1+ +d.d0;}function k(a){var b=0;b=j(a)|0;return ((+((j(a)|0)>>>0))*4294967296+(+(b>>>0)))*M(5.42101086E-20);}function j(c){var b=null,e=0,d=0,a=0;a=c.i1|0;b=c.a0;e=(a+1>>>0)%624|0;d=b[e]|0;b[a]=-(d&1)& -1727483681^(b[(a+397>>>0)%624|0]|0)^(d&2147483646|b[a]& -2147483648)>>>1;a=b[c.i1|0]|0;a^=(a>>>11);c.i1=e;a^=(a<<7& -1658038656);a^=(a<<15& -272236544);return a>>>18^a|0;}function J(b){var a=null;w(b,x()|0);a=b.a2;a.d0=0;a.d1=1;a.i3=0;}function w(e,d){var c=null,b=0,a=0;c=e.a0;c[0]=d;a=1;b=d;while(1){b=(L(b>>>30^b,1812433253)|0)+a|0;c[a]=b;a=a+1|0;if((a|0)!==62 |
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
///////////////////////////////////////////////////////////////////////////////// | |
//---------------------- BOILERPLATE TO PUT IN A SEPARATE HEADER -----/// | |
///////////////////////////////////////////////////////////////////////////////// | |
#include <memory> | |
class [[cheerp::genericjs]][[cheerp::jsexport]] CounterAlive { | |
public: | |
CounterAlive() | |
{ | |
++counter; | |
} |
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
class [[cheerp::jsexport]] CppClass | |
{ | |
public: | |
CppClass(int n, bool flag) | |
{ /*initialization*/ } | |
void processNum(int k) | |
{/* ... */} | |
bool isValid() | |
{/* ... */ return true;} | |
static void staticMethod() |
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
/opt/cheerp/bin/clang++ example.cpp -o example.js -O3 -cheerp-pretty-code | |
#WebAssembly is the default target | |
#-cheerp-pretty-code is not needed, but the resulting code is more readable for this example purposes |
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
namespace [[cheerp::genericjs]] client { | |
//forward declare a bunch of JavaScript APIs existing on the browser | |
namespace WebAssembly { | |
public: | |
Promise* instantiate(ArrayBuffer* code, ImportObject* imports); | |
}; | |
} | |
[[cheerp::genericjs]] void onSuccess(ModuleAndInstance* res) { | |
auto start_function = res->get_instance()->get_start_function(); |
OlderNewer