| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045 | /* ====================================================================   |   |   Amber Smalltalk   |   http://amber-lang.net   |   ======================================================================   ======================================================================   |   | Copyright (c) 2010-2011   | Nicolas Petton <petton.nicolas@gmail.com>   |   | Amber is released under the MIT license   |   | Permission is hereby granted, free of charge, to any person obtaining   | a copy of this software and associated documentation files (the   | 'Software'), to deal in the Software without restriction, including   | without limitation the rights to use, copy, modify, merge, publish,   | distribute, sublicense, and/or sell copies of the Software, and to   | permit persons to whom the Software is furnished to do so, subject to   | the following conditions:   |   | The above copyright notice and this permission notice shall be   | included in all copies or substantial portions of the Software.   |   | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,   | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF   | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE   | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.   |   ==================================================================== *//* Global Smalltalk objects. */// The globals below all begin with `global_' prefix.// This prefix is to advice developers to avoid their usage,// instead using local versions smalltalk, nil, _st that are// provided by appropriate wrappers to each package.// The plan is to use different module loader (and slightly change the wrappers)// so that these globals are hidden completely inside the exports/imports of the module loader.// DO NOT USE DIRECTLY! CAN DISAPPEAR AT ANY TIME.var global_smalltalk, global_nil, global__st;(function () {/* Reconfigurable micro composition system, https://github.com/herby/brikz */function Brikz(api) {	var brikz = this, backup = {};	function mixin(s, t, k) {		for (k=k||Object.keys(s), l=k.length, i=0; i<l; ++i) t[k[i]]=s[k[i]];		return t;	}	Object.defineProperties(this, {		ensure: { value: null,			enumerable: false, configurable: true, writable: true},		rebuild: { value: function () {			var oapi = mixin(api, {}), obrikz = mixin(backup, {});			mixin({}, api, Object.keys(api)); backup = {};			brikz.ensure = function (key) {				var b = brikz[key], bak = [];				while (typeof b === "function") b = new b(brikz, api, bak);				if (b === bak) { b = obrikz[key]; mixin(oapi, api, bak); }				return brikz[key] = backup[key] = b;			}			Object.keys(brikz).forEach(function (key) { brikz.ensure(key); });			brikz.ensure = null;		}, enumerable: false, configurable: true, writable: false }});}/* Brikz end */function inherits(child, parent) {	child.prototype = Object.create(parent.prototype, {		constructor: { value: child,			enumerable: false, configurable: true, writable: true }	});}/* Smalltalk foundational objects */function SmalltalkObject() {}function SmalltalkNil() {}inherits(SmalltalkNil, SmalltalkObject);function Smalltalk() {}inherits(Smalltalk, SmalltalkObject);var nil = new SmalltalkNil();var api = new Smalltalk;var brikz = new Brikz(api);function OrganizeBrik(brikz, st) {	brikz.ensure("augments");	var org = this;	org.Organizer = function () {};	inherits(org.Organizer, SmalltalkObject);	org.PackageOrganizer = function () {		this.elements = [];	};	inherits(org.PackageOrganizer, org.Organizer);	org.ClassOrganizer = function () {		this.elements = [];	};	inherits(org.ClassOrganizer, org.Organizer);	org.setupClassOrganization = function (klass) {		klass.organization = new org.ClassOrganizer;		klass.organization.theClass = klass;	};	org.setupPackageOrganization = function (pkg) {		pkg.organization = new org.PackageOrganizer;	};	org.addOrganizationElement = function (owner, element) {		owner.organization.elements.addElement(element);	};	org.removeOrganizationElement = function (owner, element) {		owner.organization.elements.removeElement(element);	};}function DNUBrik(brikz, st) {	/* Method not implemented handlers */	brikz.ensure("selectorConversion");	brikz.ensure("messageSend");	this.methods = [];	this.selectors = [];	this.get = function (string) {		var index = this.selectors.indexOf(string);		if(index !== -1) {			return this.methods[index];		}		this.selectors.push(string);		var selector = st.selector(string);		var method = {jsSelector: selector, fn: this.createHandler(selector)};		this.methods.push(method);		return method;	};	/* Dnu handler method */	this.createHandler = function (selector) {		var handler = function() {			var args = Array.prototype.slice.call(arguments);			return brikz.messageSend.messageNotUnderstood(this, selector, args);		};		return handler;	}}function ClassInitBrik(brikz, st) {	var dnu = brikz.ensure("dnu");	var manip = brikz.ensure("manipulation");	/* Initialize a class in its class hierarchy. Handle both classes and	 metaclasses. */	st.init = function(klass) {		st.initClass(klass);		if(klass.klass && !klass.meta) {			st.initClass(klass.klass);		}	};	st.initClass = function(klass) {		if(klass.wrapped) {			klass.inheritedMethods = {};			copySuperclass(klass);		} else {			installSuperclass(klass);		}		if(klass === st.Object || klass.wrapped) {			installDnuHandlers(klass);		}	};	function installSuperclass(klass) {		// only if the klass has not been initialized yet.		if(klass.fn.prototype._yourself) { return; }		if(klass.superclass && klass.superclass !== nil) {			inherits(klass.fn, klass.superclass.fn);			manip.wireKlass(klass);			manip.reinstallMethods(klass);		}	}	function copySuperclass(klass, superclass) {		for (superclass = superclass || klass.superclass;			 superclass && superclass !== nil;			 superclass = superclass.superclass) {			for (var keys = Object.keys(superclass.methods), i = 0; i < keys.length; i++) {				inheritMethodIfAbsent(superclass.methods[keys[i]], klass);			}		}	}	function inheritMethodIfAbsent(method, klass) {		var selector = method.selector;		if(klass.methods.hasOwnProperty(selector) || klass.inheritedMethods.hasOwnProperty(selector)) {			return;		}		manip.installMethod(method, klass);		klass.inheritedMethods[method.selector] = true;	}	function installDnuHandlers(klass) {		var m = dnu.methods;		for(var i=0; i<m.length; i++) {			manip.installMethodIfAbsent(m[i], klass);		}	}}function ManipulationBrik(brikz, st) {	var manip = this;	manip.installMethodIfAbsent = function (handler, klass) {		var jsFunction = klass.fn.prototype[handler.jsSelector];		if(!jsFunction) {			manip.installMethod(handler, klass);		}	};	manip.installMethod = function (method, klass) {		Object.defineProperty(klass.fn.prototype, method.jsSelector, {			value: method.fn,			enumerable: false, configurable: true, writable: true		});	};	manip.wireKlass = function (klass) {		Object.defineProperty(klass.fn.prototype, "klass", {			value: klass,			enumerable: false, configurable: true, writable: true		});	};	manip.reinstallMethods = function (klass) {		for(var keys = Object.keys(klass.methods), i=0; i<keys.length; i++) {			manip.installMethod(klass.methods[keys[i]], klass);		}	};}function ClassesBrik(brikz, st) {	var org = brikz.ensure("organize");	var manip = brikz.ensure("manipulation");	function SmalltalkPackage() {}	function SmalltalkBehavior() {}	function SmalltalkClass() {}	function SmalltalkMetaclass() {		this.meta = true;	}	inherits(SmalltalkPackage, SmalltalkObject);	inherits(SmalltalkBehavior, SmalltalkObject);	inherits(SmalltalkClass, SmalltalkBehavior);	inherits(SmalltalkMetaclass, SmalltalkBehavior);	this.Package = SmalltalkPackage;	this.Behavior = SmalltalkBehavior;	this.Class = SmalltalkClass;	this.Metaclass = SmalltalkMetaclass;	/* Smalltalk classes */	var classes = [];	var wrappedClasses = [];	/* We hold all Packages in a separate Object */	st.packages = {};	/* Smalltalk package creation. To add a Package, use smalltalk.addPackage() */	function pkg(spec) {		var that = new SmalltalkPackage();		that.pkgName = spec.pkgName;		org.setupPackageOrganization(that);		that.properties = spec.properties || {};		return that;	}	/* Smalltalk class creation. A class is an instance of an automatically		created metaclass object. Newly created classes (not their metaclass)		should be added to the smalltalk object, see smalltalk.addClass().		Superclass linking is *not* handled here, see smalltalk.init()  */	function klass(spec) {		spec = spec || {};		var meta = metaclass(spec);		var that = meta.instanceClass;		that.fn = spec.fn || function() {};		setupClass(that, spec);		that.className = spec.className;		that.wrapped   = spec.wrapped || false;		meta.className = spec.className + ' class';		if(spec.superclass) {			that.superclass = spec.superclass;			meta.superclass = spec.superclass.klass;		}		return that;	}	function metaclass(spec) {		spec = spec || {};		var that = new SmalltalkMetaclass();		inherits(			that.fn = function() {},			spec.superclass ? spec.superclass.klass.fn : SmalltalkClass		);		that.instanceClass = new that.fn();		setupClass(that);		return that;	}	function setupClass(klass, spec) {		spec = spec || {};		klass.iVarNames = spec.iVarNames || [];		klass.pkg = spec.pkg;		Object.defineProperty(klass, "toString", {			value: function() { return 'Smalltalk ' + this.className; },			enumerable:false, configurable: true, writable: false		});		org.setupClassOrganization(klass);		Object.defineProperty(klass, "methods", {			value: {},			enumerable: false, configurable: true, writable: true		});		manip.wireKlass(klass);	}	/* Add a package to the smalltalk.packages object, creating a new one if needed.	 If pkgName is null or empty we return nil, which is an allowed package for a class.	 If package already exists we still update the properties of it. */	st.addPackage = function(pkgName, properties) {		if(!pkgName) {return nil;}		if(!(st.packages[pkgName])) {			st.packages[pkgName] = pkg({				pkgName: pkgName,				properties: properties			});		} else {			if(properties) {				st.packages[pkgName].properties = properties;			}		}		return st.packages[pkgName];	};	/* Add a class to the smalltalk object, creating a new one if needed.	 A Package is lazily created if it does not exist with given name. */	st.addClass = function(className, superclass, iVarNames, pkgName) {		var pkg = st.addPackage(pkgName);		if (superclass == nil) { superclass = null; }		if(st[className] && st[className].superclass == superclass) {			st[className].superclass = superclass;			st[className].iVarNames = iVarNames;			st[className].pkg = pkg || st[className].pkg;		} else {			if(st[className]) {				st.removeClass(st[className]);			}			st[className] = klass({				className: className,				superclass: superclass,				pkg: pkg,				iVarNames: iVarNames			});		}		classes.addElement(st[className]);		org.addOrganizationElement(pkg, st[className]);	};	st.removeClass = function(klass) {		org.removeOrganizationElement(klass.pkg, klass);		classes.removeElement(klass);		delete st[klass.className];	};	/* Create a new class wrapping a JavaScript constructor, and add it to the	 global smalltalk object. Package is lazily created if it does not exist with given name. */	st.wrapClassName = function(className, pkgName, fn, superclass, wrapped) {		if(wrapped !== false) {			wrapped = true;		}		var pkg = st.addPackage(pkgName);		st[className] = klass({			className:  className,			superclass: superclass,			pkg:        pkg,			fn:         fn,			wrapped:    wrapped		});		classes.addElement(st[className]);		if(wrapped) {			wrappedClasses.addElement(st[className]);		}		org.addOrganizationElement(pkg, st[className]);	};	/* Create an alias for an existing class */	st.alias = function(klass, alias) {		st[alias] = klass;	};	/* Answer all registered Smalltalk classes */	//TODO: remove the function and make smalltalk.classes an array	st.classes = function() {		return classes;	};	st.wrappedClasses = function() {		return wrappedClasses;	};	/* Answer all registered Packages as Array */	// TODO: Remove this hack	st.packages.all = function() {		var packages = [];		for(var i in st.packages) {			if(!st.packages.hasOwnProperty(i) || typeof(st.packages[i]) === "function") continue;			packages.push(st.packages[i]);		}		return packages;	};	/* Answer the direct subclasses of klass. */	st.subclasses = function(klass) {		var subclasses = [];		var classes = st.classes();		for(var i=0; i < classes.length; i++) {			var c = classes[i];			if(c.fn) {				//Classes				if(c.superclass === klass) {					subclasses.push(c);				}				c = c.klass;				//Metaclasses				if(c && c.superclass === klass) {					subclasses.push(c);				}			}		}		return subclasses;	};	st.allSubclasses = function(klass) {		var result, subclasses;		result = subclasses = st.subclasses(klass);		subclasses.forEach(function(subclass) {			result.push.apply(result, st.allSubclasses(subclass));		});		return result;	};}function MethodsBrik(brikz, st) {	var manip = brikz.ensure("manipulation");	var org = brikz.ensure("organize");	var instance = brikz.ensure("instance");	var dnu = brikz.ensure("dnu");	brikz.ensure("selectorConversion");	brikz.ensure("augments");	function SmalltalkMethod() {}	inherits(SmalltalkMethod, SmalltalkObject);	this.Method = SmalltalkMethod;	/* Smalltalk method object. To add a method to a class,	 use smalltalk.addMethod() */	st.method = function(spec) {		var that = new SmalltalkMethod();		that.selector          = spec.selector;		that.jsSelector        = spec.jsSelector;		that.args              = spec.args || {};		that.category          = spec.category;		that.source            = spec.source;		that.messageSends      = spec.messageSends || [];		that.referencedClasses = spec.referencedClasses || [];		that.fn                = spec.fn;		return that;	};	function installNewDnuHandler(newHandler) {		manip.installMethodIfAbsent(newHandler, st.Object);		var wrappedClasses = st.wrappedClasses();		for(var i = 0; i < wrappedClasses.length; i++) {			manip.installMethodIfAbsent(newHandler, wrappedClasses[i]);		}	}	/* Add/remove a method to/from a class */	/* This is a temporary version of addMethod() for backward compatibility */	st.addMethod = function(method_exJsSelector, klass_exMethod, exKlass) {		if (typeof method_exJsSelector === "string") { //legacy			if (method_exJsSelector !== st.selector(klass_exMethod.selector)) {				console.log("DISCREPANCY: arg, in_method");				console.log(method_exJsSelector);				console.log(st.selector(klass_exMethod.selector));				klass_exMethod.jsSelector = method_exJsSelector;			}			return new_addMethod(klass_exMethod, exKlass);		}		return new_addMethod(method_exJsSelector, klass_exMethod);	};	// later, st.addMethod can be this:	function new_addMethod(method, klass) {		if (!(method.jsSelector)) {			method.jsSelector = st.selector(method.selector);		}		manip.installMethod(method, klass);		klass.methods[method.selector] = method;		method.methodClass = klass;		// During the bootstrap, #addCompiledMethod is not used.		// Therefore we populate the organizer here too		org.addOrganizationElement(klass, method.category);		// If already initialized (else it will be done later anyway),		// re-initialize all subclasses to ensure the new method		// propagation (for wrapped classes, not using the prototype		// chain.		if(instance.initialized()) {			st.allSubclasses(klass).forEach(function(subclass) {				st.initClass(subclass);			});		}		for(var i=0; i<method.messageSends.length; i++) {			var dnuHandler = dnu.get(method.messageSends[i]);			if(instance.initialized()) {				installNewDnuHandler(dnuHandler);			}		}	}	st.removeMethod = function(method) {		var klass = method.methodClass;		delete klass.fn.prototype[st.selector(method.selector)];		delete klass.methods[method.selector];		// Do *not* delete protocols from here.		// This is handled by #removeCompiledMethod	};	/* Answer all method selectors based on dnu handlers */	st.allSelectors = function() {		return dnu.selectors;	};}function AugmentsBrik(brikz, st) {	/* Make sure that console is defined */	if(typeof console === "undefined") {		this.console = {			log: function() {},			warn: function() {},			info: function() {},			debug: function() {},			error: function() {}		};	}	/* Array extensions */	Array.prototype.addElement = function(el) {		if(typeof el === 'undefined') { return; }		if(this.indexOf(el) == -1) {			this.push(el);		}	};	Array.prototype.removeElement = function(el) {		var i = this.indexOf(el);		if (i !== -1) { this.splice(i, 1); }	};}function InstanceBrik(brikz, st) {	brikz.ensure("classInit");	var initialized = false;	/* Smalltalk initialization. Called on page load */	st.initialize = function() {		if(initialized) { return; }		st.classes().forEach(function(klass) {			st.init(klass);		});		st.classes().forEach(function(klass) {			klass._initialize();		});		initialized = true;	};	this.initialized = function () {		return initialized;	};	/* List of all reserved words in JavaScript. They may not be used as variables		in Smalltalk. */	// list of reserved JavaScript keywords as of	//   http://es5.github.com/#x7.6.1.1	// and	//   http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.6.1	st.reservedWords = ['break', 'case', 'catch', 'continue', 'debugger',		'default', 'delete', 'do', 'else', 'finally', 'for', 'function',		'if', 'in', 'instanceof', 'new', 'return', 'switch', 'this', 'throw',		'try', 'typeof', 'var', 'void', 'while', 'with',		// ES5: future use: http://es5.github.com/#x7.6.1.2		'class', 'const', 'enum', 'export', 'extends', 'import', 'super',		// ES5: future use in strict mode		'implements', 'interface', 'let', 'package', 'private', 'protected',		'public', 'static', 'yield'];	st.globalJsVariables = ['jQuery', 'window', 'document', 'process', 'global'];	if(this.jQuery) {		this.jQuery.allowJavaScriptCalls = true;	}}function PrimitivesBrik(brikz, st) {	/* Unique ID number generator */	var oid = 0;	st.nextId = function() {		oid += 1;		return oid;	};	/* Converts a JavaScript object to valid Smalltalk Object */	st.readJSObject = function(js) {		var object = js;		var readObject = (js.constructor === Object);		var readArray = (js.constructor === Array);		if(readObject) {			object = st.Dictionary._new();		}		for(var i in js) {			if(readObject) {				object._at_put_(i, st.readJSObject(js[i]));			}			if(readArray) {				object[i] = st.readJSObject(js[i]);			}		}		return object;	};	/* Boolean assertion */	st.assert = function(shouldBeBoolean) {		if ((undefined !== shouldBeBoolean) && (shouldBeBoolean.klass === st.Boolean)) {			return (shouldBeBoolean == true);		} else {			st.NonBooleanReceiver._new()._object_(shouldBeBoolean)._signal();		}	};	/* Backward compatibility with Amber 0.9.1 */	st.symbolFor = function(aString) { return aString; };}function RuntimeBrik(brikz, st) {	function SmalltalkMethodContext(home, setup) {		this.homeContext = home;		this.setup       = setup || function() {};		this.pc          = 0;	}// Fallbacks	SmalltalkMethodContext.prototype.locals = {};	SmalltalkMethodContext.prototype.receiver = null;	SmalltalkMethodContext.prototype.selector = null;	SmalltalkMethodContext.prototype.lookupClass = null;	inherits(SmalltalkMethodContext, SmalltalkObject);	this.MethodContext = SmalltalkMethodContext;	SmalltalkMethodContext.prototype.fill = function(receiver, selector, locals, lookupClass) {		this.receiver    = receiver;		this.selector    = selector;		this.locals      = locals || {};		this.lookupClass = lookupClass;	};	SmalltalkMethodContext.prototype.fillBlock = function(locals, ctx) {		this.locals        = locals || {};		this.outerContext  = ctx;	};	SmalltalkMethodContext.prototype.init = function() {		var home = this.homeContext;		if(home) {			home = home.init();		}		this.setup(this);	};	SmalltalkMethodContext.prototype.method = function() {		var method;		var lookup = this.lookupClass || this.receiver.klass;		while(!method && lookup) {			method = lookup.methods[smalltalk.convertSelector(this.selector)];			lookup = lookup.superclass;		}		return method;	};	/* This is the current call context object. While it is publicly available,	 Use smalltalk.getThisContext() instead which will answer a safe copy of	 the current context */	st.thisContext = undefined;	st.withContext = function(worker, setup) {		if(st.thisContext) {			st.thisContext.pc++;			return inContext(worker, setup);		} else {			try {				return inContext(worker, setup);			} catch(error) {				if(error.smalltalkError) {					handleError(error);				} else {					var errorWrapper = st.JavaScriptException._on_(error);					try {errorWrapper._signal();} catch(ex) {}					errorWrapper._context_(st.getThisContext());					handleError(errorWrapper);				}				// Reset the context stack in any case				st.thisContext = undefined;				// Throw the exception anyway, as we want to stop				// the execution to avoid infinite loops				// Update: do not throw the exception. It's really annoying.				// throw error;			}		}	};	function inContext(worker, setup) {		var context = pushContext(setup);		var result = worker(context);		popContext(context);		return result;	}	/* Handles Smalltalk errors. Triggers the registered ErrorHandler		(See the Smalltalk class ErrorHandler and its subclasses */	function handleError(error) {		st.ErrorHandler._current()._handleError_(error);	}	/* Handle thisContext pseudo variable */	st.getThisContext = function() {		if(st.thisContext) {			st.thisContext.init();			return st.thisContext;		} else {			return nil;		}	};	function pushContext(setup) {		return st.thisContext = new SmalltalkMethodContext(st.thisContext, setup);	}	function popContext(context) {		st.thisContext = context.homeContext;	}	/*	 * Answer the smalltalk representation of o.	 * Used in message sends	 */	this._st = function (o) {		if(o == null) {return nil;}		if(o.klass) {return o;}		return st.JSObjectProxy._on_(o);	};}function MessageSendBrik(brikz, st) {	brikz.ensure("selectorConversion");	/* Handles unhandled errors during message sends */	// simply send the message and handle #dnu:	st.send = function(receiver, selector, args, klass) {		var method;		if(receiver === null) {			receiver = nil;		}		method = klass ? klass.fn.prototype[selector] : receiver.klass && receiver[selector];		if(method) {			return method.apply(receiver, args);		} else {			return messageNotUnderstood(receiver, selector, args);		}	};	/* Handles #dnu: *and* JavaScript method calls.	 if the receiver has no klass, we consider it a JS object (outside of the	 Amber system). Else assume that the receiver understands #doesNotUnderstand: */	function messageNotUnderstood(receiver, selector, args) {		/* Handles JS method calls. */		if(receiver.klass === undefined || receiver.allowJavaScriptCalls) {			return callJavaScriptMethod(receiver, selector, args);		}		/* Handles not understood messages. Also see the Amber counter-part		 Object>>doesNotUnderstand: */		return receiver._doesNotUnderstand_(			st.Message._new()				._selector_(st.convertSelector(selector))				._arguments_(args)		);	}	/* Call a method of a JS object, or answer a property if it exists.	 Else try wrapping a JSObjectProxy around the receiver.	 If the object property is a function, then call it, except if it starts with	 an uppercase character (we probably want to answer the function itself in this	 case and send it #new from Amber).	 Converts keyword-based selectors by using the first	 keyword only, but keeping all message arguments.	 Example:	 "self do: aBlock with: anObject" -> "self.do(aBlock, anObject)" */	function callJavaScriptMethod(receiver, selector, args) {		var jsSelector = selector._asJavaScriptSelector();		var jsProperty = receiver[jsSelector];		if(typeof jsProperty === "function" && !/^[A-Z]/.test(jsSelector)) {			return jsProperty.apply(receiver, args);		} else if(jsProperty !== undefined) {			if(args[0]) {				receiver[jsSelector] = args[0];				return nil;			} else {				return jsProperty;			}		}		return st.send(st.JSObjectProxy._on_(receiver), selector, args);	}	this.messageNotUnderstood = messageNotUnderstood;}function SelectorConversionBrik(brikz, st) {	/* Convert a Smalltalk selector into a JS selector */	st.selector = function(string) {		var selector = '_' + string;		selector = selector.replace(/:/g, '_');		selector = selector.replace(/[\&]/g, '_and');		selector = selector.replace(/[\|]/g, '_or');		selector = selector.replace(/[+]/g, '_plus');		selector = selector.replace(/-/g, '_minus');		selector = selector.replace(/[*]/g ,'_star');		selector = selector.replace(/[\/]/g ,'_slash');		selector = selector.replace(/[\\]/g ,'_backslash');		selector = selector.replace(/[\~]/g ,'_tild');		selector = selector.replace(/>/g ,'_gt');		selector = selector.replace(/</g ,'_lt');		selector = selector.replace(/=/g ,'_eq');		selector = selector.replace(/,/g ,'_comma');		selector = selector.replace(/[@]/g ,'_at');		return selector;	};	/* Convert a string to a valid smalltalk selector.		if you modify the following functions, also change String>>asSelector		accordingly */	st.convertSelector = function(selector) {		if(selector.match(/__/)) {			return convertBinarySelector(selector);		} else {			return convertKeywordSelector(selector);		}	};	function convertKeywordSelector(selector) {		return selector.replace(/^_/, '').replace(/_/g, ':');	}	function convertBinarySelector(selector) {		return selector			.replace(/^_/, '')			.replace(/_and/g, '&')			.replace(/_or/g, '|')			.replace(/_plus/g, '+')			.replace(/_minus/g, '-')			.replace(/_star/g, '*')			.replace(/_slash/g, '/')			.replace(/_backslash/g, '\\')			.replace(/_tild/g, '~')			.replace(/_gt/g, '>')			.replace(/_lt/g, '<')			.replace(/_eq/g, '=')			.replace(/_comma/g, ',')			.replace(/_at/g, '@');	}}function BootstrapBrik(brikz, st) {	brikz.ensure("classes");	brikz.ensure("methods");	brikz.ensure("organize");	brikz.ensure("runtime");	/***************************************** BOOTSTRAP ******************************************/	st.wrapClassName("Object", "Kernel-Objects", SmalltalkObject, undefined, false);	st.wrapClassName("Behavior", "Kernel-Classes", brikz.classes.Behavior, st.Object, false);	st.wrapClassName("Metaclass", "Kernel-Classes", brikz.classes.Metaclass, st.Behavior, false);	st.wrapClassName("Class", "Kernel-Classes", brikz.classes.Class, st.Behavior, false);	st.Object.klass.superclass = st.Class;	st.wrapClassName("Smalltalk", "Kernel-Objects", Smalltalk, st.Object, false);	st.wrapClassName("Package", "Kernel-Objects", brikz.classes.Package, st.Object, false);	st.wrapClassName("CompiledMethod", "Kernel-Methods", brikz.methods.Method, st.Object, false);	st.wrapClassName("Organizer", "Kernel-Objects", brikz.organize.Organizer, st.Object, false);	st.wrapClassName("PackageOrganizer", "Kernel-Objects", brikz.organize.PackageOrganizer, st.Organizer, false);	st.wrapClassName("ClassOrganizer", "Kernel-Objects", brikz.organize.ClassOrganizer, st.Organizer, false);	st.wrapClassName("Number", "Kernel-Objects", Number, st.Object);	st.wrapClassName("BlockClosure", "Kernel-Methods", Function, st.Object);	st.wrapClassName("Boolean", "Kernel-Objects", Boolean, st.Object);	st.wrapClassName("Date", "Kernel-Objects", Date, st.Object);	st.wrapClassName("UndefinedObject", "Kernel-Objects", SmalltalkNil, st.Object, false);	st.addClass("Collection", st.Object, null, "Kernel-Collections");	st.addClass("IndexableCollection", st.Collection, null, "Kernel-Collections");	st.addClass("SequenceableCollection", st.IndexableCollection, null, "Kernel-Collections");	st.addClass("CharacterArray", st.SequenceableCollection, null, "Kernel-Collections");	st.wrapClassName("String", "Kernel-Collections", String, st.CharacterArray);	st.wrapClassName("Array", "Kernel-Collections", Array, st.SequenceableCollection);	st.wrapClassName("RegularExpression", "Kernel-Collections", RegExp, st.Object);	st.wrapClassName("Error", "Kernel-Exceptions", Error, st.Object);	st.wrapClassName("MethodContext", "Kernel-Methods", brikz.runtime.MethodContext, st.Object, false);	/* Alias definitions */	st.alias(st.Array, "OrderedCollection");	st.alias(st.Date, "Time");}brikz.dnu = DNUBrik;brikz.messageSend = MessageSendBrik;brikz.organize = OrganizeBrik;brikz.selectorConversion = SelectorConversionBrik;brikz.runtime = RuntimeBrik;brikz.primitives = PrimitivesBrik;brikz.classInit = ClassInitBrik;brikz.manipulation = ManipulationBrik;brikz.classes = ClassesBrik;brikz.methods = MethodsBrik;brikz.instance = InstanceBrik;brikz.augments = AugmentsBrik;brikz.bootstrap = BootstrapBrik;brikz.rebuild();global_smalltalk = api;global_nil = nil;global__st = brikz.runtime._st;})();
 |