Skip to content

Instantly share code, notes, and snippets.

/*
Shows that strongly typed vars is possible in js! And do not worry about speed. :) Robust type system is more important than yet another benchmark :P. And i'll plan to make deploy system which checks all types and removes all 'ugly' `with`. This code definitely should be complexer, but i keep it simple.
P.S. `with` rulezzz
P.P.S. As you see its too easy to make field of class strongly typed too.
*/
(function($G){
if(Object.defineProperty && !{}.__defineGetter__)
{
Object.prototype.__defineGetter__ = function(name, _fn)
// yield in es3
// original code
(function()
{
var _gen = function(a)
{
var d = '';
for(;;) {
@bga
bga / Function bind.js
Created February 16, 2011 07:43
Function bind.js
/*
uses compilation to optimize `Function#bind`, but not real `bind` which in fact - `carry`
inlines consts.
*/
var defaultThis = (function(){ return this })();
this._fastBind = (function()
{
var _quoteString = function(s)
var _addProps = function(a, name)
{
a[name + '.a'] = name + '.a';
a['d'] = name + '.d';
};
var _create = function(proto)
{
var C = function(){};
C.prototype = proto;
var _copyFile = function(
iName,
oName,
_ret
)
{
var CHUNK_SIZE = 65536;
var _copy = function(iF, oF, size, _ret)
/* Reordered binary search and cache-misses. Thanks for idea, http://www.linux.org.ru/people/ckotinko/profile. */
var n = 800000;
var ENABLE_TYPED_ARRAYS = 1;
var MAKE_SOLID = 1;
var $G = this;
var MyArray =
ENABLE_TYPED_ARRAYS && (
/*
`String.prototype._matchWithPoss(re, p)` (c) New BSD License
@param re {RegExp}
@param p ?= 0 {Number} start position to match
Matches regExp with subgroups and back references as build-in `String.prototype.match` but adds info about subgroups positions in string.
Example:
console.log(' 123abcABC123abc'._matchWithPoss(/(?:\d+)([a-z]+)[A-Z]+(\d+\1)/, 1));
[
{ // 0 - whole matched substring with matched position
"s": "123abcABC123abc",
/*
Very simple. Uses [gs]etters and caller. Precache '_method in Class' relations using unique `Class.jbId_` and `_method.jbOwnerClassIdMap_`.
*/
(function()
{
var nextClassId = 1;
var _methodInClass = function(_fn, Class)
{
var classId = Class.jbId_;
@bga
bga / declarative style evaluation.js
Created September 22, 2011 13:09
declarative style evaluation.js
/*
Declarative style evaluation.
Advantages
order of evaluation is not important
evaluation is lazy i.e. its evaluated only if required
auto memorization of evaluation (only for pure fns)
expressions are writed as in math, no mutable values
*/
Object.prototype._let = #(name, _calc) {
@bga
bga / ltml.js
Created November 1, 2011 23:53
ltml.js
String.prototype._quoteCString = #{
-> @replace(/\\/g, '\\\\').replace(/\"/g, '\\"')
}
fix _ltmlToHtml = #(s) {
fix _skipSpaces = #(p) {
-> p + s.slice(p).match(/^\s*/)[0].length // skip
}
fix _passAttr = #(p) {
switch(s.charAt(p))