Skip to content

Instantly share code, notes, and snippets.

Avatar
🦍
Generating legacy code

Hector Zarco zzarcon

🦍
Generating legacy code
View GitHub Profile
@zzarcon
zzarcon / foo.js
Created Sep 10, 2017
Bind call to class property
View foo.js
// FROM:
class Foo {
constructor() {
this.bar = this.bar.bind(this);
}
bar() {
return 1;
}
}
View fibonacci.c
int fibonacci(int n) {
int a = 1;
int b = 1;
while (n-- > 1) {
int t = a;
a = b;
b += t;
}
View fibo.js
function fiboJs(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
@zzarcon
zzarcon / loader.js
Created Jan 9, 2017
WebAssembly loader
View loader.js
module.exports = (filename) => {
return fetch(filename)
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.compile(buffer))
.then(module => {
const imports = {
env: {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({
@zzarcon
zzarcon / benchmark.js
Last active Jan 10, 2017
Fibo benchmark
View benchmark.js
const Benchmark = require('benchmark');
const loadModule = require('./loader');
const {fiboJs, fiboJsRec, fiboJsMemo} = require('./fibo.js');
const suite = new Benchmark.Suite;
const numToFibo = 40;
window.Benchmark = Benchmark; //Benchmark.js uses the global object internally
console.info('Benchmark started');
View wrapped.js
(function($0) {var stack = Runtime.stackSave();var str = $0;var ret = 0;
if (str !== null && str !== undefined && str !== 0) { // null string
// at most 4 bytes per UTF-8 code point, +1 for the trailing '\0'
var len = (str.length << 2) + 1;
ret = Runtime.stackAlloc(len);
stringToUTF8(str, ret, len);
};$0=(ret);var ret = cfunc($0);if (typeof EmterpreterAsync === 'object') { assert(!EmterpreterAsync.state, 'cannot start async op with normal JS calling cwrap') }Runtime.stackRestore(stack);return ret})
View cwrap.js
cwrap = function cwrap(ident, returnType, argTypes) {
argTypes = argTypes || [];
var cfunc = getCFunc(ident);
// When the function takes numbers and returns a number, we can just return
// the original function
var numericArgs = argTypes.every(function(type){ return type === 'number'});
var numericRet = (returnType !== 'string');
if ( numericRet && numericArgs) {
return cfunc;
}
View strlen.c
#include <string.h>
#include <stdint.h>
#include <limits.h>
#define ALIGN (sizeof(size_t))
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
size_t strlen(const char *s)
@zzarcon
zzarcon / foo.sh
Created Jan 7, 2017
webassembly paths
View foo.sh
source /Users/zzarcon/emsdk_portable/emsdk_env.sh
export LLVM_ROOT=/Users/zzarcon/emsdk_portable/clang/fastcomp/build_incoming_64/bin
export NODE_JS=/Users/zzarcon/.nvm/versions/node/v7.4.0/bin/node
export EMSCRIPTEN_ROOT=/Users/zzarcon/emsdk_portable/emscripten/incoming
export EMSCRIPTEN_NATIVE_OPTIMIZER=/Users/zzarcon/emsdk_portable/emscripten/incoming_64bit_optimizer/optimizer
@zzarcon
zzarcon / index.jsx
Last active Jan 4, 2017
Webcomponent custom styles example
View index.jsx
/** @jsx h */
/*
Component A passing custom styles to sk-tags
*/
import { Component, h } from 'skatejs';
class A extends Component {
renderCallback() {
const tagStyles = `