Permalink
Browse files

generalized and simplified type resolving solution based on ECMA reco…

…mmendation, corrected tests
  • Loading branch information...
rotaryden committed May 27, 2012
1 parent 09fb160 commit d34f1f0acc80f04efde5ae7436b52d37a6567d41
Showing with 31 additions and 27 deletions.
  1. +1 −1 jassino.min.js
  2. +23 −17 src/jassino.js
  3. +7 −9 test/test.js
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -48,12 +48,19 @@ var Jassino = (function() {
InstantiationError: _make_exc("Instantiation"),
MembersError: _make_exc("Members"),
- is_array: is_array
+ use_global_scope: function(){
+ this.NS = window
+ window.Class = this.Class
+ window.Trait = this.Trait
+ }
+
},
- UNDEF = "undefined",
- FUN = "function",
- STR = "string",
- OBJ = 'object'
+
+ T_UNDEF = "Undefined",
+ T_FUN = "Function",
+ T_STR = "String",
+ T_OBJ = "Object",
+ T_ARRAY = 'Array'
//==============================================================================================
@@ -76,7 +83,7 @@ var Jassino = (function() {
function slice(arr, begin, end){return Array.prototype.slice.call(arr, begin, end)}
- function is_array(a){return Object.prototype.toString.call(a) === '[object Array]';} //ECMAScript recommendation
+ function is(tp, a){return Object.prototype.toString.call(a) === '[object ' + tp + ']';} //ECMAScript recommendation
function _inner_extend(destination, source_field_val, field_name){
@@ -150,12 +157,11 @@ var Jassino = (function() {
if (len < 2) throw new AE(args, "Specify at least name and body")
//if first parameter an object => it should be namespace
- if (args[0] !== null && //null is object
- typeof args[0] === OBJ && ! is_array(args[0])
+ if (is(T_OBJ, args[0])
){
name_pos++
ns = args[0]
- }else if(args[0] && typeof args[0] === STR){
+ }else if(args[0] && is(T_STR, args[0])){
ns = J.NS
}else{
throw new AE(args, "First argument should be namespace or name")
@@ -169,24 +175,24 @@ var Jassino = (function() {
//number of parameters between name and body
var_par_num = len - (name_pos + 1) - 1
- if (! data.name || typeof data.name !== STR) throw new AE(data, "Invalid name")
+ if (! data.name || ! is(T_STR, data.name)) throw new AE(data, "Invalid name")
if (var_par_num > 0){
var errmsg = "Parameters between name and body: 1st - class, 2nd - traits array 1..n || one of them"
var par_after_name = args[name_pos + 1]
if (var_par_num == 2){ //superclass and traits
var traits = args[name_pos + 2]
- if (typeof par_after_name == FUN && is_array(traits) ) {
+ if (is(T_FUN, par_after_name) && is(T_ARRAY, traits) ) {
data.sclass = par_after_name
data.straits = traits
}else{
throw new AE(data, errmsg)
}
}else if(var_par_num == 1){ //super class OR traits
- if (typeof par_after_name === FUN) {
+ if (is(T_FUN, par_after_name)) {
data.sclass = par_after_name
- }else if (is_array(par_after_name)){
+ }else if (is(T_ARRAY, par_after_name)){
data.straits = par_after_name
}else{
throw new AE(data, errmsg)
@@ -198,7 +204,7 @@ var Jassino = (function() {
}
function _nsadd(data, obj){
- if (typeof data.ns[data.name] !== UNDEF)
+ if (! is(T_UNDEF, data.ns[data.name]))
throw new J.DuplicationError(data)
data.ns[data.name] = obj
}
@@ -253,7 +259,7 @@ var Jassino = (function() {
:
function(){if (! this[_VALID_INSTANCE_MARKER]) _inst_err()}
- else if (typeof saved_ctor === FUN)
+ else if (is(T_FUN, saved_ctor))
//---- full explicit constructor
// super constructor call (if needed) must be done as this.SuperClassName()
klass = function(){
@@ -265,12 +271,12 @@ var Jassino = (function() {
}
}
- else if (is_array(saved_ctor)){
+ else if (is(T_ARRAY, saved_ctor)){
//---- Shortcut form SPEC: <[[<'$super_arg', ....>],> ['constructor_arg', ....]<]>
//saved_ctor non-empty here due to very first condition
//saved_ctor[0] - super agrs, [1] - constructor args
- if (! is_array(saved_ctor[0])){
+ if (! is(T_ARRAY, saved_ctor[0])){
if (SuperClass) throw new J.ConstructorError(saved_ctor,
"_: [arg,...] assumes NO SuperClass")
klass = function(){
View
@@ -692,9 +692,7 @@ test("Bees Simplified [not finished]", function() {
*************************************************************************************/
//convenient variables
- var Class = Jassino.Class,
- Trait = Jassino.Trait,
- NS = Jassino.NS
+ Jassino.use_global_scope()
//Namespace. It may contain members as well
var Bees = {}
@@ -711,7 +709,7 @@ test("Bees Simplified [not finished]", function() {
}
})
- Trait(Bees, 'Flyable', [Bees.Moveable], { //explicit namespace
+ Trait(Bees, 'Flyable', [Movable], { //explicit namespace
flight_hours: 0,
fly_to: function(loc){
this.move_to(loc)
@@ -728,14 +726,14 @@ test("Bees Simplified [not finished]", function() {
_:['name', 'lifespan']
})
- Class(Bees, 'FemaleBee', NS.Bee, {
+ Class(Bees, 'FemaleBee', Bee, {
_: function(name){ //Explicit constructor
this.Bee('F') //super constructor call
this.name=name
} //constructor
})
- Class(Bees, 'MaleBee', NS.Bee, [Bees.Flyable], {
+ Class(Bees, 'MaleBee', Bee, [Bees.Flyable], {
_: [['name','lifespan'], []]
})
@@ -746,7 +744,7 @@ test("Bees Simplified [not finished]", function() {
} //constructor
})
- Class(Bees, 'Worker', Bees.FemaleBee, [NS.Workable, Bees.Flyable], {
+ Class(Bees, 'Worker', Bees.FemaleBee, [Workable, Bees.Flyable], {
//Sort Constructor form, means:
// On declaration time, generate constructor accepting 2 parameters,
// first parameter pass to super constructor
@@ -801,13 +799,13 @@ test("Bees Simplified [not finished]", function() {
})
//----------------------------------------------------------------------------------------------
- Class('BeeKeeper', [NS.Workable], {
+ Class('BeeKeeper', [Workable], {
})
//---------------------------------------------------------------
- Class(Places, 'Hive', Place, [NS.Moveable], {
+ Class(Places, 'Hive', Place, [Movable], {
$NAME: 'Place',
//Constructor shortcut (SuperClass-less form)
//means: take first argument from constructor and place it into this.bees

0 comments on commit d34f1f0

Please sign in to comment.