Skip to content

Instantly share code, notes, and snippets.

@tkihira
tkihira / gist:2161538
Created March 22, 2012 18:43
goto implementation on JavaScript
var gotoLabel;
do {
switch(gotoLabel) {
default:
/* code here... */
gotoLabel = "label1"; break; /*goto lable1*/
case "label1":
/* code here... */
gotoLable = null;
}
@tkihira
tkihira / hack version.c
Created March 22, 2012 19:09
switch hack on C
void switch_func(int n) {
char *s;
switch(n) {
case 0:
s = "Sun";
if(0)
case 1:
{ s = "Mon"; }
if(0)
case 2:
@tkihira
tkihira / profiler.js
Created March 24, 2012 16:00
JavaScript General Profiler
window.Profiler = {
stackFrame: [],
counters: {},
working: false,
listup: function(name, obj, parentName, parentObj, level) {
if(level > 10) {
return;
}
var fullName = parentName? parentName + "." + name: name;
@tkihira
tkihira / gist:2232412
Created March 29, 2012 01:57
test for cc
/**
* @constructor
*/
var f1 = function() {
};
/**
*
*/
f1.prototype.foo = function() {
@tkihira
tkihira / gist:2250405
Created March 30, 2012 09:46
for..in hack
var o = {a: 0, b: 1, c: 2, d: 3};
var a = []; var i = 0;
for(a[i++] in o);
@tkihira
tkihira / gist:2292457
Created April 3, 2012 14:32
Chrome's bug code
var canvas = document.createElement("canvas");
canvas.width = canvas.height = 300;
document.body.appendChild(canvas);
var ctx = canvas.getContext("2d");
var m = document.createElement("canvas");
m.width = m.height = 100;
var mctx = m.getContext("2d");
mctx.fillStyle = "#f00";
mctx.fillRect(0, 0, 100, 100);
@tkihira
tkihira / gist:2367067
Created April 12, 2012 13:02
rmdir recursively in node.js
var fs = require("fs");
var path = require("path");
var rmdir = function(dir) {
var list = fs.readdirSync(dir);
for(var i = 0; i < list.length; i++) {
var filename = path.join(dir, list[i]);
var stat = fs.statSync(filename);
if(filename == "." || filename == "..") {
@tkihira
tkihira / gist:2779981
Created May 24, 2012 07:08
VM sample
while(offset >= 0) {
switch(offset) {
case 0: // "PUSH 10"
stack.push(10);
case 2: // "PUSH 8"
stack.push(8);
case 4: // "LESS"
var a = stack.pop();
var b = stack.pop();
stack.push((a < b)? 1: 0);
@tkihira
tkihira / gist:3014700
Created June 28, 2012 23:28
mkdir, rmdir and copyDir(deep-copy a directory) in node.js
var mkdir = function(dir) {
// making directory without exception if exists
try {
fs.mkdirSync(dir, 0755);
} catch(e) {
if(e.code != "EEXIST") {
throw e;
}
}
};
@tkihira
tkihira / gist:3035490
Created July 2, 2012 20:26
convert SJIS to UTF-8 using AJAX and ecl.js
<!doctype html>
<html><head><title>escape sample</title></head>
// http://www.vector.co.jp/soft/other/java/se342855.html
<script src="ecl.js"></script>
<script>
onload = function() {
var xhr = new XMLHttpRequest();
xhr.overrideMimeType('text/plain; charset=x-user-defined'); // binary
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { // DONE