1 /*!
  2  * 
  3  * JavaLikeScript
  4  * 
  5  */
  6 
  7 // Add default option variable
  8 if (typeof jlsOptions == 'undefined') {
  9     jlsOptions = {};
 10 }
 11 
 12 // Native namespace
 13 var _native = {};
 14 
 15 /*
 16  * Library boot
 17  */
 18 _native.boot = {};
 19 _native.boot.emptyFunction = function() {};
 20 _native.boot.unsupportedFunction = function() {
 21     throw 'Unsupported native operation';
 22 };
 23 _native.boot.unsupportedClass = function() {
 24     throw 'Unsupported native class';
 25 };
 26 _native.boot.logLevel = 0;
 27 _native.boot.log = _native.boot.emptyFunction;
 28 if ((typeof console != 'undefined') && ('log' in console)) {
 29     _native.boot.log = console.log;
 30 }
 31 // Find the default script path
 32 (function() {
 33     var bootstrap = 'bootstrap.js';
 34     var path = '.'; // default
 35     var scriptTags = document.getElementsByTagName('script');
 36     for ( var i = 0; i < scriptTags.length; i++) {
 37         var src = scriptTags[i].getAttribute('src');
 38         if (!src) {
 39             continue;
 40         }
 41         if (src.endsWith('/' + bootstrap)) {
 42             path = src.substring(0, src.length - bootstrap.length - 1);
 43             break;
 44         } else if (src == bootstrap) {
 45             path = '.';
 46             break;
 47         }
 48     }
 49     _native.boot.defaultScriptPath = path;
 50 })();
 51 
 52 
 53 
 54 /*
 55  * Library core
 56  * From "jls generateNativeLibraryAPI.js"
 57  */
 58 _native.core = {};
 59 _native.core.log = function(level, msg) {
 60     if (level >= _native.boot.logLevel) {
 61         _native.boot.log(msg);
 62     }
 63 };
 64 _native.core.logLevel = _native.boot.emptyFunction;
 65 _native.core.exceptionHandler = _native.boot.emptyFunction;
 66 _native.core.sleep = _native.boot.emptyFunction;
 67 _native.core._scriptPath = [];
 68 _native.core.getResourceAsString = function(path) {
 69     _native.core.log(null, '_native.core.getResourceAsString("' + path + '")');
 70     var text = null;
 71     for (var i = 0; i < _native.core._scriptPath.length; i++) {
 72         var url = _native.core._scriptPath[i] + '/' + path;
 73         _native.core.log(null, 'try url "' + url + '"');
 74         var exception = null;
 75         var status = 0;
 76         new Ajax.Request(url, {
 77             method :'get',
 78             asynchronous :false,
 79             evalJS :false,
 80             evalJSON :false,
 81             onException : function(transport, e) {
 82                 exception = e;
 83             },
 84             onFailure : function(transport) {
 85                 status = transport.status;
 86             },
 87             onSuccess : function(transport) {
 88                 text = transport.responseText;
 89             }
 90         });
 91         if (exception != null) {
 92             throw exception;
 93         } else if (status == 0) {
 94             break;
 95         } else if (status != 404) {
 96             throw 'Unsupported status ' + status + ' for script path "' + _native.core._scriptPath[i] + '"';
 97         }
 98     }
 99     if (text == null) {
100         throw 'Script not found "' + path + '"';
101     }
102     return text;
103 };
104 _native.core.getResourceAsByteArray = _native.boot.emptyFunction;
105 _native.core.evalScript = function(path) {
106     _native.core.log(null, '_native.core.evalScript("' + path + '")');
107     eval(_native.core.getResourceAsString(path));
108 };
109 _native.core.loadLibrary = _native.boot.unsupportedFunction;
110 _native.core.visitPaths = _native.boot.emptyFunction;
111 _native.core.gc = _native.boot.unsupportedFunction;
112 _native.core.dumpHead = _native.boot.unsupportedFunction;
113 _native.core.exit = _native.boot.unsupportedFunction;
114 _native.core.halt = _native.boot.unsupportedFunction;
115 _native.core.system = _native.boot.unsupportedFunction;
116 _native.core.signal = _native.boot.unsupportedFunction;
117 _native.core.raise = _native.boot.unsupportedFunction;
118 _native.core.getSignalCounters = _native.boot.unsupportedFunction;
119 _native.core.registerSignalMonitor = _native.boot.unsupportedFunction;
120 _native.core.handleSignal = _native.boot.unsupportedFunction;
121 _native.core.getEnv = _native.boot.unsupportedFunction;
122 _native.core.boot = _native.boot.unsupportedFunction;
123 _native.core.addPath = function(path) {
124     // Check for duplicate
125     var i = _native.core._scriptPath.length;
126     while (--i >= 0) {
127         if (_native.core._scriptPath[i] == path) {
128             break;
129         }
130     }
131     if (i < 0) {
132         _native.core._scriptPath.push(path);
133     }
134 };
135 _native.core.arguments = null;
136 _native.core.properties = { // From "jls systemProperties.js json"
137   'cpu.endian' : 'little',
138   'cpu.pointer.size' : '4',
139   'file.encoding' : 'UTF-8',
140   'file.separator' : '/',
141   'javascript.engine' : '?',
142   'javascript.version' : '180',
143   'jls.bootstrap.filename' : 'bootstrap.js',
144   'jls.extension.path' : '',
145   'jls.home' : '.',
146   'jls.library.path' : '.',
147   'jls.logger.logLevel' : 'warn',
148   'jls.programname' : 'jls',
149   'jls.script.path' : _native.boot.defaultScriptPath,
150   'jls.vendor' : 'spyl',
151   'jls.vendor.url' : 'http://javalikescript.free.fr/',
152   'jls.version' : '0.1',
153   'line.separator' : '\n',
154   'path.separator' : ';',
155   'user.dir' : '.',
156   'user.home' : '.',
157   'user.name' : '?'
158 };
159 _native.core.SIGINT = 2;
160 _native.core.SIGILL = 4;
161 _native.core.SIGFPE = 8;
162 _native.core.SIGSEGV = 11;
163 _native.core.SIGTERM = 15;
164 _native.core.SIGBREAK = 21;
165 _native.core.SIGABRT = 22;
166 _native.core.SIG_ERR = -1;
167 _native.core.SIG_DFL = 0;
168 _native.core.SIG_IGN = 1;
169 _native.core.SIG_USR = 2;
170 /*
171  * Class _native.core.ByteArray
172  */
173 _native.core.ByteArray = function(capacity, adoptable) {
174     // capacity could be a string
175     // TODO
176     this._array = [];
177 };
178 _native.core.ByteArray.prototype.free = function() {
179     this._array = [];
180 };
181 _native.core.ByteArray.prototype.size = function() {
182     return this._array.length;
183 };
184 _native.core.ByteArray.prototype.put = function(offset, value) {
185     this._array[offset] = value;
186 };
187 _native.core.ByteArray.prototype.get = function(offset) {
188     return this._array[offset];
189 };
190 _native.core.ByteArray.prototype.putString = _native.boot.unsupportedFunction;
191 _native.core.ByteArray.prototype.getString = _native.boot.unsupportedFunction;
192 _native.core.ByteArray.prototype.putChars = _native.boot.unsupportedFunction;
193 _native.core.ByteArray.prototype.getChars = _native.boot.unsupportedFunction;
194 _native.core.ByteArray.prototype.toNewChars = _native.boot.emptyFunction; // TODO
195 _native.core.ByteArray.prototype.putNumberArray = _native.boot.unsupportedFunction;
196 _native.core.ByteArray.prototype.pointer = _native.boot.unsupportedFunction;
197 _native.core.ByteArray.prototype.memcpy = function(offset, byteArray, at, length) {
198     for (var i = 0; i < length; i++) {
199         this._array[offset + i] = byteArray[at + i];
200     }
201 };
202 _native.core.ByteArray.prototype.setPointer = _native.boot.unsupportedFunction;
203 _native.core.ByteArray.prototype.resetPointer = _native.boot.unsupportedFunction;
204 _native.core.ByteArray.prototype.isReadOnly = _native.boot.emptyFunction;
205 /*
206  * Unsupported classes
207  */
208 _native.core.Thread = _native.boot.unsupportedClass;
209 _native.core.Lock = _native.boot.unsupportedClass;
210 _native.core.Monitor = _native.boot.unsupportedClass;
211 _native.core.Process = _native.boot.unsupportedClass;
212 _native.core.ProcessAttr = _native.boot.unsupportedClass;
213 
214 // Update script path
215 if ('arguments' in jlsOptions) {
216     _native.core.arguments = jlsOptions.arguments;
217 }
218 // Update properties
219 if ('properties' in jlsOptions) {
220     Object.extend(_native.core.properties, jlsOptions.properties);
221 }
222 // Update script path
223 _native.core._scriptPath = _native.core.properties['jls.script.path'].split(/[;:]/);
224 
225 
226 /**
227  * jls Base framework.
228  * All the framework objects are created under the jls namespace.
229  * The default objects are extended using prototype like behaviors.
230  * 
231  * @namespace
232  * @see jls.lang
233  */
234 var jls = {};
235 
236 /**
237  * @namespace Provides base classes for the jls language.
238  * @see jls.lang.Exception
239  * @see jls.lang.Logger
240  * @see jls.lang.ClassLoader
241  */
242 jls.lang = {};
243 
244 // Check for prototype
245 if (! (('Prototype' in this) && ('Class' in this))) {
246     throw 'The prototype JavaScript framework is not loaded';
247 }
248 jls.lang.Class = Class;
249 
250 
251 // Load base classes
252 _native.core.evalScript('jls/lang/Exception.js');
253 _native.core.evalScript('jls/lang/Logger.js');
254 
255 /**
256  * Logger.
257  * 
258  * @type jls.lang.Logger
259  * @memberOf jls
260  */
261 jls.logger = new jls.lang.Logger(jls.lang.Logger.INFO);
262 if ('jls.logger.logLevel' in _native.core.properties) {
263     jls.logger.setLogLevel(_native.core.properties['jls.logger.logLevel']);
264 }
265 
266 _native.core.evalScript('jls/lang/ClassLoader.js');
267 
268 /**
269  * ClassLoader.
270  * 
271  * @type jls.lang.ClassLoader
272  * @memberOf jls
273  */
274 jls.loader = new jls.lang.ClassLoader();
275 
276 // Register base classes
277 jls.loader.provide('jls.lang.Exception', true);
278 jls.loader.provide('jls.lang.Logger', true);
279 jls.loader.provide('jls.lang.ClassLoader', true);
280 
281