/* ==================================================================== | | 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() {} }; } /* 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) { 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() { return st.thisContext ? st.thisContext.copy() : nil; }; function pushContext(receiver, selector, locals, lookupClass) { var c = st.oldContext, tc = st.thisContext; if(!c) { return st.thisContext = new SmalltalkMethodContext(receiver, selector, locals, tc, lookupClass); } st.oldContext = null; c.homeContext = tc; c.pc = 1; c.receiver = receiver; c.selector = selector || ""; c.locals = locals || {}; return st.thisContext = c; } function popContext(context) { st.thisContext = context.homeContext; context.homeContext = undefined; st.oldContext = context; } /* 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(/>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, '@') } /* 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(boolean) { if ((undefined !== boolean) && (boolean.klass === smalltalk.Boolean)) { return boolean; } else { st.NonBooleanReceiver._new()._object_(boolean)._signal(); } }; /* Smalltalk initialization. Called on page load */ st.initialize = function() { if(initialized) { return; } classes.forEach(function(klass) { st.init(klass); }); classes.forEach(function(klass) { klass._initialize(); }); initialized = true; }; } inherits(Smalltalk, SmalltalkObject); function SmalltalkMethodContext(receiver, selector, locals, home, lookupClass) { this.receiver = receiver; this.selector = selector; this.locals = locals || {}; this.homeContext = home; this.lookupClass = lookupClass; } inherits(SmalltalkMethodContext, SmalltalkObject); SmalltalkMethodContext.prototype.copy = function() { var home = this.homeContext; if(home) {home = home.copy()} return new SmalltalkMethodContext( this.receiver, this.selector, this.temps, home ); }; 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; }; 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; } /* * Answer the smalltalk representation of o. * Used in message sends */ var _st = function(o) { if(typeof o === 'undefined') {return nil} if(null === o) {return nil} if(o.klass) {return o} return smalltalk.JSObjectProxy._on_(o); }; /***************************************** BOOTSTRAP ******************************************/ smalltalk.wrapClassName("Object", "Kernel-Objects", SmalltalkObject, undefined, false); smalltalk.wrapClassName("Behavior", "Kernel-Classes", SmalltalkBehavior, smalltalk.Object, false); smalltalk.wrapClassName("Metaclass", "Kernel-Classes", SmalltalkMetaclass, smalltalk.Behavior, false); smalltalk.wrapClassName("Class", "Kernel-Classes", SmalltalkClass, smalltalk.Behavior, false); smalltalk.Object.klass.superclass = smalltalk.Class; smalltalk.wrapClassName("Smalltalk", "Kernel-Objects", Smalltalk, smalltalk.Object, false); smalltalk.wrapClassName("Package", "Kernel-Objects", SmalltalkPackage, smalltalk.Object, false); smalltalk.wrapClassName("CompiledMethod", "Kernel-Objects", SmalltalkMethod, smalltalk.Object, false); smalltalk.wrapClassName("Organizer", "Kernel-Objects", SmalltalkOrganizer, smalltalk.Object, false); 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, false); smalltalk.wrapClassName("Collection", "Kernel", null, smalltalk.Object, false); smalltalk.wrapClassName("SequenceableCollection", "Kernel", null, smalltalk.Collection, false); smalltalk.wrapClassName("CharacterArray", "Kernel", null, smalltalk.SequenceableCollection, false); smalltalk.wrapClassName("String", "Kernel", String, smalltalk.CharacterArray); smalltalk.wrapClassName("Symbol", "Kernel", SmalltalkSymbol, smalltalk.CharacterArray, false); 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, false); /* Alias definitions */ smalltalk.alias(smalltalk.Array, "OrderedCollection"); smalltalk.alias(smalltalk.Date, "Time");