forsyth20121205-174063a80cc13 years agoHistory
1480 lines · 38.3 KB · Brainfuck
RawDownload
## utility functions#biinst(o: ref Obj, bi: Builtin, p: ref Obj, h: ESHostobj): ref Obj{	bo := mkobj(p, "Function");	bo.call = mkcall(nil, bi.params);	bo.val = strval(bi.val);	bo.host = h;	varinstant(bo, DontEnum|DontDelete|ReadOnly, "length", ref RefVal(numval(real bi.length)));	varinstant(o, DontEnum, bi.name, ref RefVal(objval(bo)));	return bo;} biminst(o: ref Obj, bis: array of Builtin, p: ref Obj, h: ESHostobj){	for(i := 0; i < len bis; i++)		biinst(o, bis[i], p, h);} biarg(args: array of ref Val, i: int): ref Val{	if(i < len args)		return args[i];	return undefined;} ## interface to builtin objects#get(ex: ref Ecmascript->Exec, o: ref Ecmascript->Obj, property: string): ref Ecmascript->Val{	return esget(ex, o, property, 1);} put(ex: ref Ecmascript->Exec, o: ref Ecmascript->Obj, property: string, val: ref Ecmascript->Val){	return esput(ex, o, property, val, 1);} canput(ex: ref Ecmascript->Exec, o: ref Ecmascript->Obj, property: string): ref Ecmascript->Val{	return escanput(ex, o, property, 1);} hasproperty(ex: ref Ecmascript->Exec, o: ref Ecmascript->Obj, property: string): ref Ecmascript->Val{	return eshasproperty(ex, o, property, 1);} delete(ex: ref Ecmascript->Exec, o: ref Ecmascript->Obj, property: string){	return esdelete(ex, o, property, 1);} defaultval(ex: ref Ecmascript->Exec, o: ref Ecmascript->Obj, tyhint: int): ref Ecmascript->Val{	return esdefaultval(ex, o, tyhint, 1);} call(ex: ref Ecmascript->Exec, f, this: ref Ecmascript->Obj, args: array of ref Ecmascript->Val, eval: int): ref Ecmascript->Ref{	x, y: real;	v: ref Val; 	if(this == nil)		this = ex.global;	if(f.host != me)		return escall(ex, f, this, args, eval);	case f.val.str{	"eval" =>		v = ceval(ex, f, this, args);	"parseInt" =>		v = cparseInt(ex, f, this, args);	"parseFloat" =>		v = cparseFloat(ex, f, this, args);	"escape" =>		v = cescape(ex, f, this, args);	"unescape" =>		v = cunescape(ex, f, this, args);	"isNaN" =>		v = cisNaN(ex, f, this, args);	"isFinite" =>		v = cisFinite(ex, f, this, args);	"decodeURI" =>		v = cdecodeuri(ex, f, this, args);	"encodeURI" =>		v = cencodeuri(ex, f, this, args);	"decodeURIComponent" =>		v = cdecodeuric(ex, f, this, args);	"encodeURIComponent" =>		v = cencodeuric(ex, f, this, args);	"Object" =>		v = cobj(ex, f, this, args);	"Object.prototype.toString" or	"Object.prototype.toLocaleString" =>		v = cobjprototoString(ex, f, this, args);	"Object.prototype.valueOf" =>		v = cobjprotovalueOf(ex, f, this, args);	"Object.prototype.hasOwnProperty" =>		v = cobjprotohasownprop(ex, f, this, args);	"Object.prototype.isPrototypeOf" =>		v = cobjprotoisprotoof(ex, f, this, args);	"Object.prototype.propertyisEnumerable" =>		v = cobjprotopropisenum(ex, f, this, args);	"Function" =>		v = objval(nfunc(ex, f, args));	"Function.Prototype" =>		v = undefined;	"Function.prototype.toString" =>		v = cfuncprototoString(ex, f, this, args);	"Function.prototype.apply" =>		v = cfuncprotoapply(ex, f, this, args);	"Function.prototype.call" =>		v = cfuncprotocall(ex, f, this, args);	"Error" =>		v = objval(nerr(ex, f, args, ex.errproto));	"Error.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"EvalError" =>		v = objval(nerr(ex, f, args, ex.evlerrproto));	"EvalError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"RangeError" =>		v = objval(nerr(ex, f, args, ex.ranerrproto));	"RangeError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"ReferenceError" =>		v = objval(nerr(ex, f, args, ex.referrproto));	"ReferenceError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"SyntaxError" =>		v = objval(nerr(ex, f, args, ex.synerrproto));	"SyntaxError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"TypeError" =>		v = objval(nerr(ex, f, args, ex.typerrproto));	"TypeError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"URIError" =>		v = objval(nerr(ex, f, args, ex.urierrproto));	"URIError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"InternalError" =>		v = objval(nerr(ex, f, args, ex.interrproto));	"InternalError.prototype.toString" =>		v = cerrprototoString(ex, f, this, args);	"Array" =>		v = objval(narray(ex, f, args));	"Array.prototype.toString" or "Array.prototype.toLocaleString" =>		v = carrayprototoString(ex, f, this, args);	"Array.prototype.concat" =>		v = carrayprotoconcat(ex, f, this, args);	"Array.prototype.join" =>		v = carrayprotojoin(ex, f, this, args);	"Array.prototype.pop" =>		v = carrayprotopop(ex, f, this, args);	"Array.prototype.push" =>		v = carrayprotopush(ex, f, this, args);	"Array.prototype.reverse" =>		v = carrayprotoreverse(ex, f, this, args);	"Array.prototype.shift" =>		v = carrayprotoshift(ex, f, this, args);	"Array.prototype.slice" =>		v = carrayprotoslice(ex, f, this, args);	"Array.prototype.splice" =>		v = carrayprotosplice(ex, f, this, args);	"Array.prototype.sort" =>		v = carrayprotosort(ex, f, this, args);	"Array.prototype.unshift" =>		v = carrayprotounshift(ex, f, this, args);	"String" =>		v = cstr(ex, f, this, args);	"String.fromCharCode" =>		v = cstrfromCharCode(ex, f, this, args);	"String.prototype.toString" =>		v = cstrprototoString(ex, f, this, args);	"String.prototype.valueOf" =>		v = cstrprototoString(ex, f, this, args);	"String.prototype.charAt" =>		v = cstrprotocharAt(ex, f, this, args);	"String.prototype.charCodeAt" =>		v = cstrprotocharCodeAt(ex, f, this, args);	"String.prototype.concat" =>		v = cstrprotoconcat(ex, f, this, args);	"String.prototype.indexOf" =>		v = cstrprotoindexOf(ex, f, this, args);	"String.prototype.lastIndexOf" =>		v = cstrprotolastindexOf(ex, f, this, args);	"String.prototype.localeCompare" =>		v = cstrprotocmp(ex, f, this, args);	"String.prototype.slice" =>		v = cstrprotoslice(ex, f, this, args);	"String.prototype.split" =>		v = cstrprotosplit(ex, f, this, args);	"String.prototype.substr" =>		v = cstrprotosubstr(ex, f, this, args);	"String.prototype.substring" =>		v = cstrprotosubstring(ex, f, this, args);	"String.prototype.toLowerCase" or "String.prototype.toLocaleLowerCase" =>		v = cstrprototoLowerCase(ex, f, this, args);	"String.prototype.toUpperCase" or "String.prototype.toLocaleUpperCase" =>		v = cstrprototoUpperCase(ex, f, this, args);	"String.prototype.match" =>		v = cstrprotomatch(ex, f, this, args);	"String.prototype.replace" =>		v = cstrprotoreplace(ex, f, this, args);	"String.prototype.search" =>		v = cstrprotosearch(ex, f, this, args);# JavaScript 1.0	"String.prototype.anchor" or	"String.prototype.big" or	"String.prototype.blink" or	"String.prototype.bold" or	"String.prototype.fixed" or	"String.prototype.fontcolor" or	"String.prototype.fontsize" or	"String.prototype.italics" or	"String.prototype.link" or	"String.prototype.small" or	"String.prototype.strike" or	"String.prototype.sub" or	"String.prototype.sup" =>		s := toString(ex, objval(this));		arg := toString(ex, biarg(args, 0));		tag, endtag: string;		case f.val.str{		"String.prototype.anchor" =>			tag = "<A NAME=\"" + arg + "\">";			endtag = "</A>";		"String.prototype.big" =>			tag = "<BIG>";			endtag = "</BIG>";		"String.prototype.blink" =>			tag = "<BLINK>";			endtag = "</BLINK>";		"String.prototype.bold" =>			tag = "<B>";			endtag = "</B>";		"String.prototype.fixed" =>			tag = "<TT>";			endtag = "</TT>";		"String.prototype.fontcolor" =>			tag = "<FONT COLOR=\"" + arg + "\">";			endtag = "</FONT>";		"String.prototype.fontsize" =>			tag = "<FONT SIZE=\"" + arg + "\">";			endtag = "</FONT>";		"String.prototype.italics" =>			tag = "<I>";			endtag = "</I>";		"String.prototype.link" =>			tag = "<A HREF=\"" + arg + "\">";			endtag = "</A>";		"String.prototype.small" =>			tag = "<SMALL>";			endtag = "</SMALL>";		"String.prototype.strike" =>			tag = "<STRIKE>";			endtag = "</STRIKE>";		"String.prototype.sub" =>			tag = "<SUB>";			endtag = "</SUB>";		"String.prototype.sup" =>			tag = "<SUP>";			endtag = "</SUP>";		}		v = strval(tag + s + endtag);	"Boolean" =>		v = cbool(ex, f, this, args);	"Boolean.prototype.toString" =>		v = cboolprototoString(ex, f, this, args);	"Boolean.prototype.valueOf" =>		v = cboolprotovalueOf(ex, f, this, args);	"Number" =>		v = cnum(ex, f, this, args);	"Number.prototype.toString" or "Number.prototype.toLocaleString" =>		v = cnumprototoString(ex, f, this, args);	"Number.prototype.valueOf" =>		v = cnumprotovalueOf(ex, f, this, args);	"Number.prototype.toFixed" =>		v = cnumprotofix(ex, f, this, args);	"Number.prototype.toExponential" =>		v = cnumprotoexp(ex, f, this, args);	"Number.prototype.toPrecision" =>		v = cnumprotoprec(ex, f, this, args);	"RegExp" =>		v = cregexp(ex, f, this, args);	"RegExp.prototype.exec" =>		v = cregexpprotoexec(ex, f, this, args);	"RegExp.prototype.test" =>		v = cregexpprototest(ex, f, this, args);	"RegExp.prototype.toString" =>		v = cregexpprototoString(ex, f, this, args);	"Math.abs" or	"Math.acos" or	"Math.asin" or	"Math.atan" or	"Math.ceil" or	"Math.cos" or	"Math.exp" or	"Math.floor" or	"Math.log" or	"Math.round" or	"Math.sin" or	"Math.sqrt" or	"Math.tan" =>		x = toNumber(ex, biarg(args, 0));		case f.val.str{		"Math.abs" =>			if(x < 0.)				x = -x;			else if(x == 0.)				x = 0.;		"Math.acos" =>		x = math->acos(x);		"Math.asin" =>		x = math->asin(x);		"Math.atan" =>		x = math->atan(x);		"Math.ceil" =>		x = math->ceil(x);		"Math.cos" =>		x = math->cos(x);		"Math.exp" =>		x = math->exp(x);		"Math.floor" =>		x = math->floor(x);		"Math.log" =>		x = math->log(x);		"Math.round" =>		if((x == .0 && copysign(1., x) == -1.)					|| (x < .0 && x >= -0.5))						x = -0.;					else						x = math->floor(x+.5);		"Math.sin" =>		x = math->sin(x);		"Math.sqrt" =>		x = math->sqrt(x);		"Math.tan" =>		x = math->tan(x);		}		v = numval(x);	"Math.random" =>#		range := big 16r7fffffffffffffff;		range := big 1000000000;		v = numval(real bigrand(range)/ real range);	"Math.atan2" or	"Math.max" or	"Math.min" or	"Math.pow" =>		x = toNumber(ex, biarg(args, 0));		y = toNumber(ex, biarg(args, 1));		case f.val.str{		"Math.atan2" =>			x = math->atan2(x, y);		"Math.max" =>			if(x > y)				;			else if(x < y)				x = y;			else if(x == y){				if(x == 0. && copysign(1., x) == -1. && copysign(1., y) == 1.)					x = y;			}else				x = Math->NaN;		"Math.min" =>			if(x < y)				;			else if(x > y)				x = y;			else if(x == y){				if(x == 0. && copysign(1., x) == 1. && copysign(1., y) == -1.)					x = y;			}else				x = Math->NaN;		"Math.pow" =>			x = math->pow(x, y);		}		v = numval(x);	"Date" =>		v = cdate(ex, f, this, args);	"Date.parse" =>		v = cdateparse(ex, f, this, args);	"Date.UTC" =>		v = cdateUTC(ex, f, this, args);	"Date.prototype.toString" or	"Date.prototype.toLocaleString" =>		v = cdateprototoString(ex, f, this, args);	"Date.prototype.toDateString" or	"Date.prototype.toLocaleDateString" =>		v = cdateprototoDateString(ex, f, this, args);	"Date.prototype.toTimeString" or	"Date.prototype.toLocaleTimeString" =>		v = cdateprototoTimeString(ex, f, this, args);	"Date.prototype.valueOf" or	"Date.prototype.getTime" =>		v = cdateprotovalueOf(ex, f, this, args);	"Date.prototype.getYear" or	"Date.prototype.getFullYear" or	"Date.prototype.getMonth" or	"Date.prototype.getDate" or	"Date.prototype.getDay" or	"Date.prototype.getHours" or	"Date.prototype.getMinutes" or	"Date.prototype.getSeconds" =>		v = cdateprotoget(ex, f, this, args, !UTC);	"Date.prototype.getUTCFullYear" or	"Date.prototype.getUTCMonth" or	"Date.prototype.getUTCDate" or	"Date.prototype.getUTCDay" or	"Date.prototype.getUTCHours" or	"Date.prototype.getUTCMinutes" or	"Date.prototype.getUTCSeconds" =>		v = cdateprotoget(ex, f, this, args, UTC);	"Date.prototype.getMilliseconds" or	"Date.prototype.getUTCMilliseconds" =>		v = cdateprotogetMilliseconds(ex, f, this, args);	"Date.prototype.getTimezoneOffset" =>		v = cdateprotogetTimezoneOffset(ex, f, this, args);	"Date.prototype.setTime" =>		v = cdateprotosetTime(ex, f, this, args);	"Date.prototype.setMilliseconds" =>		v = cdateprotosetMilliseconds(ex, f, this, args, !UTC);	"Date.prototype.setUTCMilliseconds" =>		v = cdateprotosetMilliseconds(ex, f, this, args, UTC);	"Date.prototype.setSeconds" =>		v = cdateprotosetSeconds(ex, f, this, args, !UTC);	"Date.prototype.setUTCSeconds" =>		v = cdateprotosetSeconds(ex, f, this, args, UTC);	"Date.prototype.setMinutes" =>		v = cdateprotosetMinutes(ex, f, this, args, !UTC);	"Date.prototype.setUTCMinutes" =>		v = cdateprotosetMinutes(ex, f, this, args, UTC);	"Date.prototype.setHours" =>		v = cdateprotosetHours(ex, f, this, args, !UTC);	"Date.prototype.setUTCHours" =>		v = cdateprotosetHours(ex, f, this, args, UTC);	"Date.prototype.setDate" =>		v = cdateprotosetDate(ex, f, this, args, !UTC);	"Date.prototype.setUTCDate" =>		v = cdateprotosetDate(ex, f, this, args, UTC);	"Date.prototype.setMonth" =>		v = cdateprotosetMonth(ex, f, this, args, !UTC);	"Date.prototype.setUTCMonth" =>		v = cdateprotosetMonth(ex, f, this, args, UTC);	"Date.prototype.setFullYear" =>		v = cdateprotosetFullYear(ex, f, this, args, !UTC);	"Date.prototype.setUTCFullYear" =>		v = cdateprotosetFullYear(ex, f, this, args, UTC);	"Date.prototype.setYear" =>		v = cdateprotosetYear(ex, f, this, args);	"Date.prototype.toUTCString" or	"Date.prototype.toGMTString" =>		v = cdateprototoUTCString(ex, f, this, args);	* =>		v = nil;	}	if(v == nil)		runtime(ex, ReferenceError, "unknown function "+f.val.str+" in builtin call");	return valref(v);} rsalt := big 12345678; randinit(seed: big){	rsalt = big seed;	bigrand(big 1);	bigrand(big 1);} RANDMASK: con (big 1<<63)-(big 1); bigrand(modulus: big): big{	rsalt = rsalt * big 1103515245 + big 12345;	if(modulus <= big 0)		return big 0;	return ((rsalt&RANDMASK)>>10) % modulus;} construct(ex: ref Ecmascript->Exec, f: ref Ecmascript->Obj, args: array of ref Ecmascript->Val): ref Ecmascript->Obj{	if(f.host != me)		runtime(ex, TypeError, "ecmascript builtin called incorrectly");	case f.val.str{	"Object" =>		return nobj(ex, f, args);	"Function" =>		return nfunc(ex, f, args);	"Array" =>		return narray(ex, f, args);	"Error" =>		return nerr(ex, f, args, ex.errproto);	"EvalError" =>		return nerr(ex, f, args, ex.evlerrproto);	"RangeError" =>		return nerr(ex, f, args, ex.ranerrproto);	"ReferenceError" =>		return nerr(ex, f, args, ex.referrproto);	"SyntaxError" =>		return nerr(ex, f, args, ex.synerrproto);	"TypeError" =>		return nerr(ex, f, args, ex.typerrproto);	"URIError" =>		return nerr(ex, f, args, ex.urierrproto);	"InternalError" =>		return nerr(ex, f, args, ex.interrproto);	"String" or	"Boolean" or

Lines 1–500 of 1480.