123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- function SmalltalkObject(){};
- function SmalltalkBehavior(){};
- function SmalltalkClass(){};
- function SmalltalkMetaclass(){
- this.meta = true;
- };
- function SmalltalkMethod(){};
- function SmalltalkNil(){};
- function Smalltalk(){
- var st = this;
- st.debugMode = true;
-
- function klass(spec) {
- var spec = spec || {};
- var that;
- if(spec.meta) {
- that = new SmalltalkMetaclass();
- } else {
- that = new (klass({meta: true})).fn;
- that.klass.instanceClass = that;
- that.className = spec.className;
- that.klass.className = that.className + ' class';
- }
-
- that.fn = spec.fn || function(){};
- that.superclass = spec.superclass;
- that.iVarNames = spec.iVarNames || [];
- if(that.superclass) {
- that.klass.superclass = that.superclass.klass;
- }
- that.category = spec.category || "";
- that.fn.prototype.methods = {};
- that.fn.prototype.inheritedMethods = {};
- that.fn.prototype.klass = that;
- return that;
- };
-
- st.method = function(spec) {
- var that = new SmalltalkMethod();
- that.selector = spec.selector;
- that.jsSelector = spec.jsSelector;
- that.category = spec.category;
- that.source = spec.source;
- that.messageSends = spec.messageSends || [];
- that.referencedClasses = spec.referencedClasses || [];
- that.fn = spec.fn;
- return that
- };
-
- st.init = function(klass) {
- var subclasses = st.subclasses(klass);
- var methods;
-
- for(var i=0;i<klass.iVarNames.length;i++) {
- klass.fn.prototype["@"+klass.iVarNames[i]] = nil;
- }
- if(klass.superclass && klass.superclass !== nil) {
- methods = st.methods(klass.superclass);
-
- for(var i in methods) {
- if(!klass.fn.prototype.methods[i]) {
- klass.fn.prototype.inheritedMethods[i] = methods[i];
- klass.fn.prototype[methods[i].jsSelector] = methods[i].fn;
- }
- }
-
- for(var i=0;i<klass.superclass.iVarNames.length;i++) {
- if(!klass["@"+klass.superclass.iVarNames[i]]) {
- klass.fn.prototype["@"+klass.superclass.iVarNames[i]] = nil;
- }
- }
- }
- for(var i=0;i<subclasses.length;i++) {
- st.init(subclasses[i]);
- }
- if(klass.klass && !klass.meta) {
- st.init(klass.klass);
- }
- };
-
- st.classes = function() {
- var classes = [];
- for(var i in st) {
- if(i.search(/^[A-Z]/g) != -1) {
- classes.push(st[i]);
- }
- }
- return classes
- };
-
- st.methods = function(klass) {
- var methods = {};
- for(var i in klass.fn.prototype.methods) {
- methods[i] = klass.fn.prototype.methods[i]
- }
- for(var i in klass.fn.prototype.inheritedMethods) {
- methods[i] = klass.fn.prototype.inheritedMethods[i]
- }
- return methods;
- }
-
- st.subclasses = function(klass) {
- var subclasses = [];
- var classes = st.classes();
- for(var i in classes) {
- if(classes[i].fn) {
-
- if(classes[i].klass && classes[i].klass.superclass === klass) {
- subclasses.push(classes[i].klass);
- }
-
- if(classes[i].superclass === klass) {
- subclasses.push(classes[i]);
- }
- }
- }
- return subclasses;
- };
-
- st.mapClassName = function(className, category, fn, superclass) {
- st[className] = klass({
- className: className,
- category: category,
- superclass: superclass,
- fn: fn
- });
- };
-
- st.addClass = function(className, superclass, iVarNames, category) {
- if(st[className]) {
- st[className].superclass = superclass;
- st[className].iVarNames = iVarNames;
- st[className].category = category || st[className].category;
- } else {
- st[className] = klass({
- className: className,
- iVarNames: iVarNames,
- superclass: superclass
- });
- st[className].category = category || '';
- }
- };
-
- st.addMethod = function(jsSelector, method, klass) {
- klass.fn.prototype[jsSelector] = method.fn;
- klass.fn.prototype.methods[method.selector] = method;
- method.methodClass = klass;
- method.jsSelector = jsSelector;
- };
-
- st.send = function(receiver, selector, args, klass) {
- if(typeof receiver === "undefined") {
- receiver = nil;
- }
- if(!klass && receiver.klass && receiver[selector]) {
- return receiver[selector].apply(receiver, args);
- } else if(klass && klass.fn.prototype[selector]) {
- return klass.fn.prototype[selector].apply(receiver, args)
- }
- return messageNotUnderstood(receiver, selector, args);
- };
-
- function messageNotUnderstood(receiver, selector, args) {
-
- if(receiver.klass === undefined) {
- return callJavaScriptMethod(receiver, selector, args);
- }
-
-
- return receiver._doesNotUnderstand_(
- st.Message._new()
- ._selector_(convertSelector(selector))
- ._arguments_(args)
- );
- };
- function callJavaScriptMethod(receiver, selector, args) {
-
- var jsSelector = selector
- .replace(/^_/, '')
- .replace(/_.*/g, '');
- var jsProperty = receiver[jsSelector];
- if(typeof jsProperty === "function") {
- return jsProperty.apply(receiver, args);
- } else if(jsProperty !== undefined) {
- return jsProperty
- }
- smalltalk.Error._signal_(receiver + ' is not a Jtalk object and "' + jsSelector + '" is undefined')
- }
-
-
- function convertSelector(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(/_plus/, '+')
- .replace(/_minus/, '-')
- .replace(/_star/, '*')
- .replace(/_slash/, '/')
- .replace(/_gt/, '>')
- .replace(/_lt/, '<')
- .replace(/_eq/, '=')
- .replace(/_comma/, ',')
- .replace(/_at/, '@')
- };
-
- st.readJSObject = function(js) {
- var object = js;
- var readObject = (js.constructor === Object);
- var readArray = (js.constructor === Array);
-
- if(readObject) {
- object = smalltalk.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;
- };
- }
- function SmalltalkMethodContext() {
- this.stack = [];
- this.push = function(context) {
- stack.push(context);
- };
-
- this.pop = function() {
- stack.pop();
- };
- }
- var nil = new SmalltalkNil();
- var smalltalk = new Smalltalk();
- var thisContext = nil;
- smalltalk.mapClassName("Object", "Kernel", SmalltalkObject);
- smalltalk.mapClassName("Smalltalk", "Kernel", Smalltalk, smalltalk.Object);
- smalltalk.mapClassName("Behavior", "Kernel", SmalltalkBehavior, smalltalk.Object);
- smalltalk.mapClassName("Class", "Kernel", SmalltalkClass, smalltalk.Behavior);
- smalltalk.mapClassName("Metaclass", "Kernel", SmalltalkMetaclass, smalltalk.Behavior);
- smalltalk.mapClassName("CompiledMethod", "Kernel", SmalltalkMethod, smalltalk.Object);
- smalltalk.Object.klass.superclass = smalltalk.Class
- smalltalk.mapClassName("Number", "Kernel", Number, smalltalk.Object);
- smalltalk.mapClassName("BlockClosure", "Kernel", Function, smalltalk.Object);
- smalltalk.mapClassName("Boolean", "Kernel", Boolean, smalltalk.Object);
- smalltalk.mapClassName("Date", "Kernel", Date, smalltalk.Object);
- smalltalk.mapClassName("UndefinedObject", "Kernel", SmalltalkNil, smalltalk.Object);
- smalltalk.mapClassName("Collection", "Kernel", null, smalltalk.Object);
- smalltalk.mapClassName("SequenceableCollection", "Kernel", null, smalltalk.Collection);
- smalltalk.mapClassName("String", "Kernel", String, smalltalk.SequenceableCollection);
- smalltalk.mapClassName("Array", "Kernel", Array, smalltalk.SequenceableCollection);
- smalltalk.mapClassName("RegularExpression", "Kernel", RegExp, smalltalk.String);
- smalltalk.mapClassName("Error", "Kernel", Error, smalltalk.Object);
- if(this.CanvasRenderingContext2D) {
- smalltalk.mapClassName("CanvasRenderingContext", "Canvas", CanvasRenderingContext2D, smalltalk.Object);
- }
|