/* ==================================================================== | | Jtalk Smalltalk | http://jtalk-project.org | ====================================================================== ====================================================================== | | Copyright (c) 2010-2011 | Nicolas Petton | | Jtalk 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. | ==================================================================== */ /* Smalltalk constructors definition */ function SmalltalkObject(){}; function SmalltalkBehavior(){}; function SmalltalkClass(){}; function SmalltalkMetaclass(){ this.meta = true; }; function SmalltalkMethod(){}; function SmalltalkNil(){}; function Smalltalk(){ var st = this; /* 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) { 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; }; /* 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.category = spec.category; that.source = spec.source; that.messageSends = spec.messageSends || []; that.referencedClasses = spec.referencedClasses || []; that.fn = spec.fn; return that }; /* Initialize a class in its class hierarchy. Handle both class and metaclasses. */ st.init = function(klass) { var subclasses = st.subclasses(klass); var methods; if(klass.superclass && klass.superclass !== nil) { methods = st.methods(klass.superclass); //Methods linking 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>doesNotUnderstand: */ return receiver._doesNotUnderstand_( st.Message._new() ._selector_(st.convertSelector(selector)) ._arguments_(args) ); }; function callJavaScriptMethod(receiver, selector, args) { /* Call a method of a JS object, or answer a property. 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)" */ var jsSelector = selector .replace(/^_/, '') .replace(/_.*/g, ''); var jsProperty = receiver[jsSelector]; if(typeof jsProperty === "function") { return jsProperty.apply(receiver, args); } else if(jsProperty !== undefined) { if(args[0]) { receiver[jsSelector] = args[0]; return nil; } else { return jsProperty } } smalltalk.Error._signal_(receiver + ' is not a Jtalk object and "' + jsSelector + '" is undefined') }; /* Handle thisContext pseudo variable */ pushContext = function(receiver, selector, temps) { if(thisContext) { return thisContext = thisContext.newContext({receiver: receiver, selector: selector, temps: temps}); } else { return thisContext = new SmalltalkMethodContext({receiver: receiver, selector: selector, temps: temps}); } }; popContext = function(context) { if(context) { context.removeYourself(); } }; /* 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(/_plus/, '+') .replace(/_minus/, '-') .replace(/_star/, '*') .replace(/_slash/, '/') .replace(/_gt/, '>') .replace(/_lt/, '<') .replace(/_eq/, '=') .replace(/_comma/, ',') .replace(/_at/, '@') }; /* 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 = 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; }; /* Toggle deployment mode (no context will be handled during message send */ st.setDeploymentMode = function() { st.send = sendWithoutContext; }; st.setDevelopmentMode = function() { st.send = sendWithContext; } /* Set development mode by default */ st.setDevelopmentMode(); } function SmalltalkMethodContext(spec) { var that = this; spec = spec || {}; that.homeContext = spec.homeContext; that.selector = spec.selector; that.receiver = spec.receiver; that.temps = spec.temps || {}; that.newContext = function(spec) { spec = spec || {}; return new SmalltalkMethodContext({homeContext: that, receiver: spec.receiver, selector: spec.selector, temps: spec.temps}); } that.removeYourself = function() { thisContext = that.homeContext; } } /* Global Smalltalk objects. nil and thisContext shouldn't be globals. */ var nil = new SmalltalkNil(); var smalltalk = new Smalltalk(); var thisContext = undefined; /* Utilities */ Array.prototype.remove = function(s){ var index = this.indexOf(s); if(this.indexOf(s) != -1)this.splice(index, 1); } if(this.jQuery) { this.jQuery.allowJavaScriptCalls = true; } /****************************************************************************************/ /* Base classes mapping. If you edit this part, do not forget to set the superclass of the object metaclass to Class after the definition of Object */ 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); smalltalk.mapClassName("MethodContext", "Kernel", SmalltalkMethodContext, smalltalk.Object); if(this.CanvasRenderingContext2D) { smalltalk.mapClassName("CanvasRenderingContext", "Canvas", CanvasRenderingContext2D, smalltalk.Object); }