/* ==================================================================== | | 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() {} }; } /* 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>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); } /* 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; } /* 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; }; /* 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; }; } 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(/>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 Smalltalk() {} inherits(Smalltalk, SmalltalkObject); if(this.jQuery) { this.jQuery.allowJavaScriptCalls = true; } 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); var api = new Smalltalk; var brikz = new Brikz(api); brikz.organize = OrganizeBrik; brikz.selectorConversion = SelectorConversionBrik; brikz.smalltalk = SmalltalkFactory; brikz.rebuild(); var smalltalk = global_smalltalk = api; 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; }; /* * Answer the smalltalk representation of o. * Used in message sends */ global__st = function(o) { if(o == null) {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-Methods", SmalltalkMethod, smalltalk.Object, false); smalltalk.wrapClassName("Organizer", "Kernel-Objects", brikz.organize.Organizer, smalltalk.Object, false); smalltalk.wrapClassName("PackageOrganizer", "Kernel-Objects", brikz.organize.PackageOrganizer, smalltalk.Organizer, false); smalltalk.wrapClassName("ClassOrganizer", "Kernel-Objects", brikz.organize.ClassOrganizer, smalltalk.Organizer, false); smalltalk.wrapClassName("Number", "Kernel-Objects", Number, smalltalk.Object); smalltalk.wrapClassName("BlockClosure", "Kernel-Methods", Function, smalltalk.Object); smalltalk.wrapClassName("Boolean", "Kernel-Objects", Boolean, smalltalk.Object); smalltalk.wrapClassName("Date", "Kernel-Objects", Date, smalltalk.Object); smalltalk.wrapClassName("UndefinedObject", "Kernel-Objects", SmalltalkNil, smalltalk.Object, false); smalltalk.addClass("Collection", smalltalk.Object, null, "Kernel-Collections"); smalltalk.addClass("IndexableCollection", smalltalk.Collection, null, "Kernel-Collections"); smalltalk.addClass("SequenceableCollection", smalltalk.IndexableCollection, null, "Kernel-Collections"); smalltalk.addClass("CharacterArray", smalltalk.SequenceableCollection, null, "Kernel-Collections"); smalltalk.wrapClassName("String", "Kernel-Collections", String, smalltalk.CharacterArray); smalltalk.wrapClassName("Array", "Kernel-Collections", Array, smalltalk.SequenceableCollection); smalltalk.wrapClassName("RegularExpression", "Kernel-Collections", RegExp, smalltalk.Object); smalltalk.wrapClassName("Error", "Kernel-Exceptions", Error, smalltalk.Object); smalltalk.wrapClassName("MethodContext", "Kernel-Methods", SmalltalkMethodContext, smalltalk.Object, false); /* Alias definitions */ smalltalk.alias(smalltalk.Array, "OrderedCollection"); smalltalk.alias(smalltalk.Date, "Time"); })();