Browse Source

Merge branch 'requirejs-wip' into requirejs-with-folder-restructure

Conflicts:
bin/amberc.js
Herbert Vojčík 10 years ago
parent
commit
d230525035
10 changed files with 108 additions and 72 deletions
  1. 53 29
      bin/amberc.js
  2. 1 1
      grunt/tasks/grunt-peg.js
  3. 42 35
      package.json
  4. 1 1
      support/_st.js
  5. 1 1
      support/boot.js
  6. 5 0
      support/browser-compatibility.js
  7. 2 2
      support/nil.js
  8. 1 1
      support/parser.js
  9. 1 1
      support/smalltalk.js
  10. 1 1
      test/Test.js

+ 53 - 29
bin/amberc.js

@@ -8,6 +8,8 @@
  * Execute 'node compiler.js' without arguments or with -h / --help for help.
  */
 
+var amdefine = require("amdefine");
+
 /**
  * Map the async filter function onto array and evaluate callback, once all have finished.
  * Taken from: http://howtonode.org/control-flow-part-iii
@@ -54,19 +56,19 @@ function Combo(callback) {
 
 Combo.prototype = {
   add: function () {
-    var self = this,
-        id = this.items;
-    this.items++;
-    return function () {
-      self.check(id, arguments);
-    };
+	var self = this,
+		id = this.items;
+	this.items++;
+	return function () {
+	  self.check(id, arguments);
+	};
   },
   check: function (id, arguments) {
-    this.results[id] = Array.prototype.slice.call(arguments);
-    this.items--;
-    if (this.items == 0) {
-      this.callback.apply(this, this.results);
-    }
+	this.results[id] = Array.prototype.slice.call(arguments);
+	this.items--;
+	if (this.items == 0) {
+	  this.callback.apply(this, this.results);
+	}
   }
 };
 
@@ -83,11 +85,11 @@ var path = require('path'),
 function AmberC(amber_dir, closure_jar) {
 	this.amber_dir = amber_dir;
 	this.closure_jar = closure_jar || '';
-	this.kernel_libraries = ['@boot', 'Kernel-Objects', 'Kernel-Classes', 'Kernel-Methods',
-	                         'Kernel-Collections', 'Kernel-Exceptions', 'Kernel-Transcript',
-	                         'Kernel-Announcements'];
-	this.compiler_libraries = this.kernel_libraries.concat(['@parser', 'Importer-Exporter', 'Compiler-Exceptions',
-	                          'Compiler-Core', 'Compiler-AST', 'Compiler-IR', 'Compiler-Inlining', 'Compiler-Semantic']);
+	this.kernel_libraries = ['@boot', '@smalltalk', '@nil', '@_st', 'Kernel-Objects', 'Kernel-Classes', 'Kernel-Methods',
+							'Kernel-Collections', 'Kernel-Exceptions', 'Kernel-Transcript',
+							'Kernel-Announcements'];
+	this.compiler_libraries = this.kernel_libraries.concat(['@parser', 'Importer-Exporter',
+							'Compiler-Core', 'Compiler-AST', 'Compiler-Exceptions', 'Compiler-IR', 'Compiler-Inlining', 'Compiler-Semantic']);
 }
 
 
@@ -101,7 +103,7 @@ var createDefaults = function(amber_dir, finished_callback){
 
 	return {
 		'load': [],
-		'init': path.join(amber_dir, 'support', 'init.js'),
+//		'init': path.join(amber_dir, 'support', 'init.js'),
 		'main': undefined,
 		'mainfile': undefined,
 		'stFiles': [],
@@ -166,7 +168,7 @@ AmberC.prototype.check_configuration_ok = function(configuration) {
 		throw new Error('AmberC.check_configuration_ok(): missing configuration object');
 	}
 	if (undefined === configuration.init) {
-		throw new Error('AmberC.check_configuration_ok(): init value missing in configuration object');
+//		throw new Error('AmberC.check_configuration_ok(): init value missing in configuration object');
 	}
 
 	if (0 === configuration.jsFiles.length && 0 === configuration.stFiles.lenght) {
@@ -224,8 +226,8 @@ AmberC.prototype.check_for_closure_compiler = function(callback) {
  * @param callback gets called on success with path to .js file as parameter
  */
 AmberC.prototype.resolve_js = function(filename, callback) {
-    var special = filename[0] == "@";
-    if (special) { filename = filename.slice(1); }
+	var special = filename[0] == "@";
+	if (special) { filename = filename.slice(1); }
 	var baseName = path.basename(filename, '.js');
 	var jsFile = baseName + this.defaults.loadsuffix + '.js';
 	var amberJsFile = path.join(this.amber_dir, special?'support':'js', jsFile);
@@ -396,12 +398,12 @@ AmberC.prototype.resolve_compiler = function(callback) {
  */
 AmberC.prototype.resolve_init = function(compilerFiles) {
 	// check and add init.js
-	var initFile = this.defaults.init;
-	if ('.js' !== path.extname(initFile)) {
-		initFile = this.resolve_js(initFile);
-		this.defaults.init = initFile;
-	}
-	compilerFiles.push(initFile);
+//	var initFile = this.defaults.init;
+//	if ('.js' !== path.extname(initFile)) {
+//		initFile = this.resolve_js(initFile);
+//		this.defaults.init = initFile;
+//	}
+//	compilerFiles.push(initFile);
 
 	this.create_compiler(compilerFiles);
 };
@@ -415,13 +417,19 @@ AmberC.prototype.resolve_init = function(compilerFiles) {
 AmberC.prototype.create_compiler = function(compilerFilesArray) {
 	var self = this;
 	var compiler_files = new Combo(function() {
+		var define = amdefine(module), requirejs = define.require;
+		define("amber_vm/browser-compatibility", [], {});
+
 		var content = '(function() {';
 		Array.prototype.slice.call(arguments).forEach(function(data) {
 			// data is an array where index 0 is the error code and index 1 contains the data
 			content += data[1];
+			var match = (""+data[1]).match(/^define\("([^"]*)"/);
+			if (match) content += 'requirejs("'+match[1]+'");\n';
 		});
-		content = content + 'return smalltalk;})();';
+		content = content + 'return requirejs("amber_vm/smalltalk");})();';
 		self.defaults.smalltalk = eval(content);
+		self.defaults.smalltalk.initialize();
 		console.log('Compiler loaded');
 		self.defaults.smalltalk.ErrorHandler._setCurrent_(self.defaults.smalltalk.RethrowErrorHandler._new());
 
@@ -575,8 +583,8 @@ AmberC.prototype.compose_js_files = function() {
 	}
 
 	if (undefined !== defaults.init) {
-		console.log('Adding initializer ' + defaults.init);
-		program_files.push(defaults.init);
+//		console.log('Adding initializer ' + defaults.init);
+//		program_files.push(defaults.init);
 	}
 
 	console.ambercLog('Writing program file: %s.js', programFile);
@@ -591,6 +599,21 @@ AmberC.prototype.compose_js_files = function() {
 		self.optimize();
 	});
 
+	var defineDefine = function () {
+		var path = require('path');
+		var amdefine = $SRC$;
+		var define = amdefine(module);
+		var result = function () {
+			var id = arguments[0];
+			setTimeout(function () { define.require(id); }, 0);
+			return define.apply(this, arguments);
+		};
+		result.amd = {};
+		return result;
+	};
+
+	fileStream.write('var define = ('+(''+defineDefine).replace('$SRC$', ""+amdefine)+')();\n'
+		+ 'define("amber_vm/browser-compatibility", [], {});\n');
 	program_files.forEach(function(file) {
 		if(fs.existsSync(file)) {
 			console.log('Adding : ' + file);
@@ -600,6 +623,7 @@ AmberC.prototype.compose_js_files = function() {
 			throw(new Error('Can not find file ' + file));
 		}
 	});
+	fileStream.write('define("amber_vm/_init", ["amber_vm/smalltalk"], function (st) { st.initialize(); });\n');
 	if (undefined !== defaults.main) {
 		console.log('Adding call to: %s>>main', defaults.main);
 		fileStream.write('smalltalk.' + defaults.main + '._main()');

+ 1 - 1
grunt/tasks/grunt-peg.js

@@ -23,7 +23,7 @@ module.exports = function(grunt) {
       export_var: 'module.exports'
     });
     var parser = PEG.buildParser(grunt.file.read(this.data.src), options);
-    var content = 'define(["smalltalk","nil"],function(smalltalk,nil){\n'+options.export_var + ' = ' + parser.toSource() + ';\n});';
+    var content = 'define("amber_vm/parser", ["./smalltalk","./nil"],function(smalltalk,nil){\n'+options.export_var + ' = ' + parser.toSource() + ';\n});';
     grunt.file.write(this.data.dest, content);
   });
 };

+ 42 - 35
package.json

@@ -1,37 +1,44 @@
 {
-    "name": "amber",
-    "version": "0.11.0",
-    "description": "An implementation of the Smalltalk language that runs on top of the JS runtime.",
-    "homepage": "http://amber-lang.net",
-    "keywords": [ "javascript", "smalltalk", "language", "compiler", "web" ],
-    "author": {
-        "name": "Nicolas Petton",
-        "email": "petton.nicolas@gmail.com",
-        "url": "http://www.nicolas-petton.fr"
-    },
-    "license": {
-        "type": "MIT"
-    },
-    "repository": {
-        "type": "git",
-        "url": "git://github.com/amber-smalltalk/amber.git#0.11.0"
-    },
-    "engines": {
-        "node": "0.8.x"
-    },
-    "bin": {
-        "amber": "./bin/amber",
-        "amberc": "./bin/amberc",
-        "ambers": "./bin/server"
-    },
-    "scripts": {
-        "test": "sh -c 'grunt amberc:amber_test_runner' && node ./test/amber_test_runner.js"
-    },
-    "devDependencies": {
-        "pegjs": "~0.7.0",
-        "grunt": "~0.4.0",
-        "grunt-contrib-jshint": "~0.3.0",
-        "grunt-image-embed": "~0.1.3",
-        "grunt-contrib-mincss": "~0.3.2"
-    }
+  "name": "amber",
+  "version": "0.11.0",
+  "description": "An implementation of the Smalltalk language that runs on top of the JS runtime.",
+  "homepage": "http://amber-lang.net",
+  "keywords": [
+    "javascript",
+    "smalltalk",
+    "language",
+    "compiler",
+    "web"
+  ],
+  "author": {
+    "name": "Nicolas Petton",
+    "email": "petton.nicolas@gmail.com",
+    "url": "http://www.nicolas-petton.fr"
+  },
+  "license": {
+    "type": "MIT"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/amber-smalltalk/amber.git#0.11.0"
+  },
+  "engines": {
+    "node": "0.8.x"
+  },
+  "bin": {
+    "amber": "./bin/amber",
+    "amberc": "./bin/amberc",
+    "ambers": "./bin/server"
+  },
+  "scripts": {
+    "test": "sh -c 'grunt amberc:amber_test_runner' && node ./test/amber_test_runner.js"
+  },
+  "devDependencies": {
+    "pegjs": "~0.7.0",
+    "grunt": "~0.4.0",
+    "grunt-contrib-jshint": "~0.3.0",
+    "grunt-image-embed": "~0.1.3",
+    "grunt-contrib-mincss": "~0.3.2",
+    "amdefine": "0.0.5"
+  }
 }

+ 1 - 1
support/_st.js

@@ -1,3 +1,3 @@
-define(['./boot'], function (boot) {
+define("amber_vm/_st", ["./boot"], function (boot) {
     return boot._st;
 });

+ 1 - 1
support/boot.js

@@ -34,7 +34,7 @@
    ==================================================================== */
 
 
-define([ './ensure-console', './es5-shim-2.0.2/es5-shim.min', './es5-shim-2.0.2/es5-sham.min' ], function () {
+define("amber_vm/boot", [ './browser-compatibility' ], function () {
 
 /* Array extensions */
 

+ 5 - 0
support/browser-compatibility.js

@@ -0,0 +1,5 @@
+define([
+    './ensure-console',
+    './es5-shim-2.0.2/es5-shim.min',
+    './es5-shim-2.0.2/es5-sham.min'
+], {});

+ 2 - 2
support/nil.js

@@ -1,3 +1,3 @@
-define(['./boot'], function (boot) {
+define("amber_vm/nil", ["./boot"], function (boot) {
     return boot.nil;
-});
+});

+ 1 - 1
support/parser.js

@@ -1,4 +1,4 @@
-define(["./smalltalk","./nil"],function(smalltalk,nil){
+define("amber_vm/parser", ["./smalltalk","./nil"],function(smalltalk,nil){
 smalltalk.parser = (function(){
   /*
    * Generated by PEG.js 0.7.0.

+ 1 - 1
support/smalltalk.js

@@ -1,4 +1,4 @@
-define(['./boot'], function (boot) {
+define("amber_vm/smalltalk", ["./boot"], function (boot) {
     return boot.smalltalk;
 });
 

+ 1 - 1
test/Test.js

@@ -1,4 +1,4 @@
-define(["smalltalk","nil","_st"], function(smalltalk,nil,_st){
+define("amber/Test", ["amber_vm/smalltalk","amber_vm/nil","amber_vm/_st"], function(smalltalk,nil,_st){
 smalltalk.addPackage('Test');
 smalltalk.addClass('NodeTestRunner', smalltalk.Object, [], 'Test');