Skip to content

Instantly share code, notes, and snippets.

Avatar

Carlo Piovesan carlopi

View GitHub Profile
@carlopi
carlopi / Makefile
Created Sep 13, 2021
Wrapping Cheerp ES6 modules
View Makefile
all:
/opt/cheerp/bin/clang++ add.cpp -cheerp-make-module=es6 -o add.js -O3
@carlopi
carlopi / _README.txt
Last active Apr 14, 2021
binaryheap.cpp compiled with Cheerp
View _README.txt
wat2wasm cheerp_wasm.wat > cheerp_wasm.wasm
/path/to/d8 --module --harmony-top-level-await bench_cheerp.js
@carlopi
carlopi / cheerp_jit.cpp
Created Mar 21, 2021
Skeleton of Jit implementation with Cheerp
View cheerp_jit.cpp
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();
@carlopi
carlopi / compile.sh
Last active Dec 1, 2020
Example of calling of JavaScript code from C++ with Cheerp
View compile.sh
/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
View exampleJSExport.cpp
class [[cheerp::jsexport]] CppClass
{
public:
CppClass(int n, bool flag)
{ /*initialization*/ }
void processNum(int k)
{/* ... */}
bool isValid()
{/* ... */ return true;}
static void staticMethod()
@carlopi
carlopi / pimpl_with_deleter.cpp
Created Jun 30, 2020
Wrapper via pimpl_with_deleter of arbitrary Wasm classes
View pimpl_with_deleter.cpp
/////////////////////////////////////////////////////////////////////////////////
//---------------------- BOILERPLATE TO PUT IN A SEPARATE HEADER -----///
/////////////////////////////////////////////////////////////////////////////////
#include <memory>
class [[cheerp::genericjs]][[cheerp::jsexport]] CounterAlive {
public:
CounterAlive()
{
++counter;
}
View normalDistribution.js
"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
@carlopi
carlopi / normalDistribution.cpp
Created Jun 26, 2020
normalDistribution.cpp
View normalDistribution.cpp
#include <random>
auto getSeed()
{
//I am not JSExported!
std::random_device rd;
return rd();
}
class [[cheerp::jsexport]] NormalDistribution {
View shuffle.js
"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
View shuffle.cpp
#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);
}