Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 656693f6712b02080d4e961c3bc02151903c85a6 @deakster committed Apr 9, 2012
Showing with 257 additions and 0 deletions.
  1. +22 −0 example/lib/server/person.js
  2. +8 −0 example/test.js
  3. +215 −0 lib/impact-emu.js
  4. +12 −0 package.json
22 example/lib/server/person.js
@@ -0,0 +1,22 @@
+ig.module(
+ 'server.test'
+).requires(
+ // Enter modules that this one depends on here
+).defines(function () {
+
+
+Person = ig.Class.extend({
+ name: "bob",
+ age: 0,
+
+ init: function () {
+ this.age = 25;
+ },
+
+ speak: function () {
+ return "Hi, my name is " + this.name + " and I am " + this.age + " years old";
+ }
+});
+
+
+});
8 example/test.js
@@ -0,0 +1,8 @@
+require("../../node-impact-emu").useAll(__dirname + "/lib");
+
+ig.requires(
+ 'server.person'
+);
+
+var person = new Person();
+console.log(person.speak());
215 lib/impact-emu.js
@@ -0,0 +1,215 @@
+var path = require("path");
+
+// Global ig object
+var window = global;
+global.ig = {};
+
+// Shortcut to enable all systems
+exports.useAll = function (base) {
+ exports.useNative();
+ exports.useClass();
+ exports.useModule(base);
+};
+
+var moduleBase = null;
+
+// Get the expected path for the module name
+function getExpectedPath(mname) {
+ var modParts = mname.split(".");
+ modParts.unshift(moduleBase);
+ return path.join.apply(null, modParts) + ".js";
+}
+
+// Module system
+// -----------------------------------------------------------------------------
+exports.useModule = function (base) {
+ base = path.normalize(base);
+
+ if (!path.existsSync(base)) {
+ throw "Cannot enable modules, path doesn't exist: " + base;
+ }
+
+ moduleBase = base;
+
+ window.ig.module = function (mname) {
+ return window.ig;
+ };
+
+ window.ig.requires = function () {
+ for (var i = 0; i < arguments.length; i++) {
+ require(getExpectedPath(arguments[i]));
+ }
+ return window.ig;
+ };
+
+ window.ig.defines = function (body) {
+ body.call(window);
+ return window.ig;
+ }
+}
+
+
+// Native type extensions (taken straight out of impact)
+// -----------------------------------------------------------------------------
+exports.useNative = function () {
+ // -----------------------------------------------------------------------------
+ // Native Object extensions
+
+ Number.prototype.map = function(istart, istop, ostart, ostop) {
+ return ostart + (ostop - ostart) * ((this - istart) / (istop - istart));
+ };
+
+ Number.prototype.limit = function(min, max) {
+ return Math.min(max, Math.max(min, this));
+ };
+
+ Number.prototype.round = function(precision) {
+ precision = Math.pow(10, precision || 0);
+ return Math.round(this * precision) / precision;
+ };
+
+ Number.prototype.floor = function() {
+ return Math.floor(this);
+ };
+
+ Number.prototype.ceil = function() {
+ return Math.ceil(this);
+ };
+
+ Number.prototype.toInt = function() {
+ return (this | 0);
+ };
+
+ Number.prototype.toRad = function() {
+ return (this / 180) * Math.PI;
+ };
+
+ Number.prototype.toDeg = function() {
+ return (this * 180) / Math.PI;
+ };
+
+ Array.prototype.erase = function(item) {
+ for( var i = this.length; i--; ) {
+ if( this[i] === item ) {
+ this.splice(i, 1);
+ }
+ }
+ return this;
+ };
+
+ Array.prototype.random = function() {
+ return this[ Math.floor(Math.random() * this.length) ];
+ };
+
+ Function.prototype.bind = function(bind) {
+ var self = this;
+ return function(){
+ var args = Array.prototype.slice.call(arguments);
+ return self.apply(bind || null, args);
+ };
+ };
+
+ return exports;
+};
+
+
+// Class system (taken straight out of impact)
+// -----------------------------------------------------------------------------
+exports.useClass = function () {
+
+ // -----------------------------------------------------------------------------
+ // Class object based on John Resigs code; inspired by base2 and Prototype
+ // http://ejohn.org/blog/simple-javascript-inheritance/
+
+ var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\bparent\b/ : /.*/;
+
+ window.ig.Class = function(){};
+ var inject = function(prop) {
+ var proto = this.prototype;
+ var parent = {};
+ for( var name in prop ) {
+ if(
+ typeof(prop[name]) == "function" &&
+ typeof(proto[name]) == "function" &&
+ fnTest.test(prop[name])
+ ) {
+ parent[name] = proto[name]; // save original function
+ proto[name] = (function(name, fn){
+ return function() {
+ var tmp = this.parent;
+ this.parent = parent[name];
+ var ret = fn.apply(this, arguments);
+ this.parent = tmp;
+ return ret;
+ };
+ })( name, prop[name] );
+ }
+ else {
+ proto[name] = prop[name];
+ }
+ }
+ };
+
+ window.ig.Class.extend = function(prop) {
+ var parent = this.prototype;
+
+ initializing = true;
+ var prototype = new this();
+ initializing = false;
+
+ for( var name in prop ) {
+ if(
+ typeof(prop[name]) == "function" &&
+ typeof(parent[name]) == "function" &&
+ fnTest.test(prop[name])
+ ) {
+ prototype[name] = (function(name, fn){
+ return function() {
+ var tmp = this.parent;
+ this.parent = parent[name];
+ var ret = fn.apply(this, arguments);
+ this.parent = tmp;
+ return ret;
+ };
+ })( name, prop[name] );
+ }
+ else {
+ prototype[name] = prop[name];
+ }
+ }
+
+ function Class() {
+ if( !initializing ) {
+
+ // If this class has a staticInstantiate method, invoke it
+ // and check if we got something back. If not, the normal
+ // constructor (init) is called.
+ if( this.staticInstantiate ) {
+ var obj = this.staticInstantiate.apply(this, arguments);
+ if( obj ) {
+ return obj;
+ }
+ }
+ for( var p in this ) {
+ if( typeof(this[p]) == 'object' ) {
+ this[p] = ig.copy(this[p]); // deep copy!
+ }
+ }
+ if( this.init ) {
+ this.init.apply(this, arguments);
+ }
+ }
+ return this;
+ }
+
+ Class.prototype = prototype;
+ Class.constructor = Class;
+ Class.extend = arguments.callee;
+ Class.inject = inject;
+
+ return Class;
+ };
+
+ return exports; // For chaining
+};
+
12 package.json
@@ -0,0 +1,12 @@
+{
+ "author": "Deakster",
+ "name": "node-impact-emu",
+ "description": "ImpactJS emulation layer for Node.js, to enable the class system, module system and native type extensions from ImpactJS on the server.",
+ "version": "0.1.0",
+ "homepage": "https://github.com/deakster/node-impact-emu",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/deakster/node-impact-emu.git"
+ },
+ "main": "lib/impact-emu.js"
+}

0 comments on commit 656693f

Please sign in to comment.