/* ==================================================================== | | Amber Smalltalk | http://amber-lang.net | ====================================================================== ====================================================================== | | Copyright (c) 2010-2011 | Nicolas Petton | | 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. | ==================================================================== */ /* Make sure that console is defined */ if (typeof console === "undefined") { this.console = { log: function() {}, warn: function() {}, info: function() {}, debug: function() {}, error: function() {} }; } /* Smalltalk constructors definition */ function SmalltalkObject(){}; function SmalltalkBehavior(){}; function SmalltalkClass(){}; function SmalltalkPackage(){}; function SmalltalkMetaclass(){ this.meta = true; }; function SmalltalkMethod(){}; function SmalltalkNil(){}; function SmalltalkSymbol(string){ this.value = string; }; function SmalltalkOrganizer() { this.elements = []; }; SmalltalkOrganizer.prototype.addElement = function(el) { if(typeof el === 'undefined' || el === nil) { return false; } if(this.elements.indexOf(el) == -1) { this.elements.push(el); } }; SmalltalkOrganizer.prototype.removeElement = function(el) { for(var i=0; i>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); }; /* Reuse one old context stored in oldContext */ st.oldContext = null; /* Handle thisContext pseudo variable */ st.getThisContext = function() { if(st.thisContext) { return st.thisContext.copy(); } }; function pushContext(receiver, selector, method, temps) { var c = st.oldContext, tc = st.thisContext; if (!c) { return st.thisContext = new SmalltalkMethodContext(receiver, selector, method, temps, tc); } st.oldContext = null; c.homeContext = tc; c.pc = 1; c.receiver = receiver; c.selector = selector; c.method = method; c.temps = temps || {}; return st.thisContext = c; }; function popContext(context) { st.thisContext = context.homeContext; context.homeContext = undefined; st.oldContext = context; }; /* 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; }; /* Boolean assertion */ st.assert = function(shouldBeBoolean) { if ((undefined !== shouldBeBoolean) && (shouldBeBoolean.klass === smalltalk.Boolean)) { return shouldBeBoolean == true; } else { smalltalk.NonBooleanReceiver._new()._object_(shouldBeBoolean)._signal(); } } }; function SmalltalkMethodContext(receiver, selector, method, temps, home) { this.receiver = receiver; this.selector = selector; this.method = method; this.temps = temps || {}; this.homeContext = home; }; SmalltalkMethodContext.prototype.copy = function() { var home = this.homeContext; if(home) {home = home.copy()} return new SmalltalkMethodContext( this.receiver, this.selector, this.method, this.temps, home ); }; SmalltalkMethodContext.prototype.resume = function() { //Brutally set the receiver as thisContext, then re-enter the function smalltalk.thisContext = this; return this.method.apply(receiver, temps); }; /* Global Smalltalk objects. */ var nil = new SmalltalkNil(); var smalltalk = new Smalltalk(); if(this.jQuery) { this.jQuery.allowJavaScriptCalls = true; } /****************************************************************************************/ /* Base classes wrapping. If you edit this part, do not forget to set the superclass of the object metaclass to Class after the definition of Object */ smalltalk.wrapClassName("Object", "Kernel", SmalltalkObject); smalltalk.wrapClassName("Smalltalk", "Kernel", Smalltalk, smalltalk.Object); smalltalk.wrapClassName("Package", "Kernel", SmalltalkPackage, smalltalk.Object); smalltalk.wrapClassName("Behavior", "Kernel", SmalltalkBehavior, smalltalk.Object); smalltalk.wrapClassName("Class", "Kernel", SmalltalkClass, smalltalk.Behavior); smalltalk.wrapClassName("Metaclass", "Kernel", SmalltalkMetaclass, smalltalk.Behavior); smalltalk.wrapClassName("CompiledMethod", "Kernel", SmalltalkMethod, smalltalk.Object); smalltalk.wrapClassName("Organizer", "Kernel-Objects", SmalltalkOrganizer, smalltalk.Object); smalltalk.Object.klass.superclass = smalltalk.Class; smalltalk.wrapClassName("Number", "Kernel", Number, smalltalk.Object); smalltalk.wrapClassName("BlockClosure", "Kernel", Function, smalltalk.Object); smalltalk.wrapClassName("Boolean", "Kernel", Boolean, smalltalk.Object); smalltalk.wrapClassName("Date", "Kernel", Date, smalltalk.Object); smalltalk.wrapClassName("UndefinedObject", "Kernel", SmalltalkNil, smalltalk.Object); smalltalk.wrapClassName("Collection", "Kernel", null, smalltalk.Object); smalltalk.wrapClassName("SequenceableCollection", "Kernel", null, smalltalk.Collection); smalltalk.wrapClassName("CharacterArray", "Kernel", null, smalltalk.SequenceableCollection); smalltalk.wrapClassName("String", "Kernel", String, smalltalk.CharacterArray); smalltalk.wrapClassName("Symbol", "Kernel", SmalltalkSymbol, smalltalk.CharacterArray); smalltalk.wrapClassName("Array", "Kernel", Array, smalltalk.SequenceableCollection); smalltalk.wrapClassName("RegularExpression", "Kernel", RegExp, smalltalk.String); smalltalk.wrapClassName("Error", "Kernel", Error, smalltalk.Object); smalltalk.wrapClassName("MethodContext", "Kernel", SmalltalkMethodContext, smalltalk.Object); /* Alias definitions */ smalltalk.alias(smalltalk.Array, "OrderedCollection"); smalltalk.alias(smalltalk.Date, "Time");