Skip to content

Instantly share code, notes, and snippets.

View mraleph's full-sized avatar
🐌

Slava Egorov mraleph

🐌
View GitHub Profile
function Modulo(value, remainder) {
var mod = value % remainder;
// Guard against returning -0.
if (mod == 0) return 0;
return mod >= 0 ? mod : mod + remainder;
}
function WeekDay(time) {
return Modulo(Math.floor(time / 86400000) + 4, 7);
@mraleph
mraleph / CountLeadingZeros.patch
Created October 20, 2011 19:27
CountLeadingZeros.patch
Index: src/arm/macro-assembler-arm.cc
===================================================================
--- src/arm/macro-assembler-arm.cc (revision 9710)
+++ src/arm/macro-assembler-arm.cc (working copy)
@@ -2907,8 +2907,8 @@
#ifdef CAN_USE_ARMV5_INSTRUCTIONS
clz(zeros, source); // This instruction is only supported after ARM5.
#else
+ Move(scratch, source);
mov(zeros, Operand(0, RelocInfo::NONE));
@mraleph
mraleph / gist:1352731
Created November 9, 2011 19:49
catching memory corruption
static Handle<Value> Stat(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1 || !args[0]->IsString()) {
return THROW_BAD_ARGS;
}
String::Utf8Value path(args[0]->ToString());
if (args[1]->IsFunction()) {
@mraleph
mraleph / coroutine-example.js
Created November 14, 2011 16:35 — forked from creationix/coroutine-example.js
Shallow coroutines based on Harmony generators
// Variant of Tim Caswell's "fibers" proposal based on Harmony generators.
// Make generator into a shallow coroutine.
function GF(g) {
var f = g(resume);
function resume (value) { f.send(value); }
f.next();
}
function readfile(filename, next) {
@mraleph
mraleph / gist:1518569
Created December 25, 2011 00:41
typeof null on v8
mraleph@whitestar|~/src/v8/% ./d8 [svn|bleeding_edge-10247]
V8 version 3.7.4 (candidate) [console: dumb]
d8> typeof null
object
d8> ^C
mraleph@whitestar|~/src/v8/% ./d8 --harmony [svn|bleeding_edge-10247]
V8 version 3.7.4 (candidate) [console: dumb]
d8> typeof null
null
d8> ^C
// To make the benchmark results predictable, we replace Math.random
// with a 100% deterministic alternative.
Math.random = (function() {
var seed = 49734321;
return function() {
// Robert Jenkins' 32 bit integer hash function.
seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
function MakePoint(x, y)
local point = {}
point.x = x
point.y = y
return point
end
function MakeArrayOfPoints(N)
local array = {}
local m = -1
0 0 v13 BlockEntry
0 0 v14 Simulate id=3
0 0 v15 StackCheck changes[NewSpacePromotion]
0 4 i137 Change t5 t to i range[-2147483648,2147483647,m0=0]
0 3 i146 Constant 0 range[0,0,m0=0]
0 1 i18 Add i137 i146 range[-2147483648,2147483647,m0=0]
0 4 i141 Change t7 t to i range[-2147483648,2147483647,m0=0]
0 1 i20 Add i141 i146 range[-2147483648,2147483647,m0=0]
0 0 t21 CheckNonSmi t6
0 0 t22 CheckMaps t6 [0x4e80ede1]
// Sending futures across isolate boundaries.
#import('dart:isolate', prefix: 'isolate');
// FutureSendPort/FutureReceivePorts are ports for sending and receiving futures
// When connecting futures across isolates boundaries they do it by
// building the following structure:
//
// ISOLATE A
// [Future] --events--> [Completer SendPort]
@mraleph
mraleph / gist:3104414
Created July 13, 2012 11:29
stupid GC stressing code
// To make the benchmark results predictable, we replace Math.random
// with a 100% deterministic alternative.
Math.random = (function() {
var seed = 49734321;
return function() {
// Robert Jenkins' 32 bit integer hash function.
seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;