Skip to content

Commit

Permalink
Merge pull request #42 from mohayonao/dev
Browse files Browse the repository at this point in the history
use Object.defineProperty
  • Loading branch information
mohayonao committed May 17, 2014
2 parents 2feafff + 303433b commit 8c05e2c
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 48 deletions.
29 changes: 18 additions & 11 deletions build/scscript-classlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,18 @@ SCScript.install(function(sc) {
// TODO: implements equals
// TODO: implements compareObject
// TODO: implements instVarHash
// TODO: implements basicHash
// TODO: implements hash
// TODO: implements identityHash

spec.basicHash = function() {
return $SC.Integer(this._hash);
};

spec.hash = function() {
return $SC.Integer(this._hash);
};

spec.identityHash = function() {
return $SC.Integer(this._hash);
};

spec["->"] = function($obj) {
return $SC("Association").new(this, $obj);
Expand Down Expand Up @@ -1577,10 +1586,6 @@ SCScript.install(function(sc) {
return $SC.Boolean(this.valueOf() !== $aMagnitude.valueOf());
};

spec.hash = function() {
return this._subclassResponsibility("hash");
};

spec["<"] = function($aMagnitude) {
return $SC.Boolean(this < $aMagnitude);
};
Expand Down Expand Up @@ -2145,8 +2150,6 @@ SCScript.install(function(sc) {
return this.absdif($that) ["<"] ($precision);
}, "that; precision=0.0001");

// TODO: implements hash

spec.asInteger = function() {
return $SC.Integer(this._);
};
Expand Down Expand Up @@ -2535,7 +2538,9 @@ SCScript.install(function(sc) {

spec.isInteger = utils.alwaysReturn$true;

// TODO: implements hash
spec.hash = function() {
return $SC.Float(this._).hash();
};

[
[ "+", $SC.Integer, $SC.Float ],
Expand Down Expand Up @@ -8055,7 +8060,9 @@ SCScript.install(function(sc) {
return $false;
};

// TODO: implements hash
spec.hash = function() {
return this._key.hash();
};

spec["<"] = function($anAssociation) {
return this._key ["<"] ($anAssociation.key());
Expand Down
18 changes: 13 additions & 5 deletions build/scscript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(global) {
"use strict";

var sc = { VERSION: "0.0.32" };
var sc = { VERSION: "0.0.33" };

// src/sc/sc.js
(function(sc) {
Expand Down Expand Up @@ -639,6 +639,7 @@ var sc = { VERSION: "0.0.32" };
var klass = {};
var metaClasses = {};
var classes = klass.classes = {};
var hash = 0x100000;

var createClassInstance = function(MetaSpec) {
var instance = new SCClass();
Expand Down Expand Up @@ -675,7 +676,9 @@ var sc = { VERSION: "0.0.32" };
className + bond + methodName + " is already defined."
);
}
methods[methodName] = func;
Object.defineProperty(methods, methodName, {
value: func, writable: true
});
};

if (typeof fn === "function") {
Expand Down Expand Up @@ -742,9 +745,11 @@ var sc = { VERSION: "0.0.32" };
newClass = new MetaClass._MetaSpec();
newClass._name = className;
newClass._Spec = constructor;
constructor.prototype.__class = newClass;
constructor.prototype.__Spec = constructor;
constructor.prototype.__className = className;
Object.defineProperties(constructor.prototype, {
__class: { value: newClass, writable: true },
__Spec : { value: constructor },
__className: { value: className }
});
classes[className] = newClass;

return newClass;
Expand Down Expand Up @@ -825,6 +830,9 @@ var sc = { VERSION: "0.0.32" };
Object.defineProperties(this, {
_immutable: {
value: false, writable: true
},
_hash: {
value: hash++
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scscript",
"version": "0.0.32",
"version": "0.0.33",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
4 changes: 3 additions & 1 deletion src/sc/classlib/Collections/Association.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ SCScript.install(function(sc) {
return $false;
};

// TODO: implements hash
spec.hash = function() {
return this._key.hash();
};

spec["<"] = function($anAssociation) {
return this._key ["<"] ($anAssociation.key());
Expand Down
8 changes: 7 additions & 1 deletion src/sc/classlib/Collections/Association_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@
test = instance ["=="] ($anAssociation);
expect(test).to.be.a("SCBoolean").that.is.true;
});
it.skip("#hash", function() {
it("#hash", function() {
var instance, test;

instance = this.createInstance();

test = instance.hash();
expect(test).to.be.a("SCInteger");
});
it("#<", function() {
var instance, test;
Expand Down
15 changes: 12 additions & 3 deletions src/sc/classlib/Core/Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,18 @@ SCScript.install(function(sc) {
// TODO: implements equals
// TODO: implements compareObject
// TODO: implements instVarHash
// TODO: implements basicHash
// TODO: implements hash
// TODO: implements identityHash

spec.basicHash = function() {
return $SC.Integer(this._hash);
};

spec.hash = function() {
return $SC.Integer(this._hash);
};

spec.identityHash = function() {
return $SC.Integer(this._hash);
};

spec["->"] = function($obj) {
return $SC("Association").new(this, $obj);
Expand Down
24 changes: 21 additions & 3 deletions src/sc/classlib/Core/Object_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,29 @@
});
it.skip("#instVarHash", function() {
});
it.skip("#basicHash", function() {
it("#basicHash", function() {
var instance, test;

instance = this.createInstance();

test = instance.basicHash();
expect(test).to.be.a("SCInteger");
});
it.skip("#hash", function() {
it("#hash", function() {
var instance, test;

instance = this.createInstance();

test = instance.hash();
expect(test).to.be.a("SCInteger");
});
it.skip("#identityHash", function() {
it("#identityHash", function() {
var instance, test;

instance = this.createInstance();

test = instance.identityHash();
expect(test).to.be.a("SCInteger");
});
it("#->", sinon.test(function() {
var instance, test, spy;
Expand Down
4 changes: 3 additions & 1 deletion src/sc/classlib/Math/Integer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ SCScript.install(function(sc) {

spec.isInteger = utils.alwaysReturn$true;

// TODO: implements hash
spec.hash = function() {
return $SC.Float(this._).hash();
};

[
[ "+", $SC.Integer, $SC.Float ],
Expand Down
8 changes: 7 additions & 1 deletion src/sc/classlib/Math/Integer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@
[ Infinity, [], false ],
]);
});
it.skip("#hash", function() {
it("#hash", function() {
var instance, test;

instance = this.createInstance();

test = instance.hash();
expect(test).to.be.a("SCInteger");
});
it("#+", function() {
testCase(this, [
Expand Down
4 changes: 0 additions & 4 deletions src/sc/classlib/Math/Magnitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ SCScript.install(function(sc) {
return $SC.Boolean(this.valueOf() !== $aMagnitude.valueOf());
};

spec.hash = function() {
return this._subclassResponsibility("hash");
};

spec["<"] = function($aMagnitude) {
return $SC.Boolean(this < $aMagnitude);
};
Expand Down
6 changes: 0 additions & 6 deletions src/sc/classlib/Math/Magnitude_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
[ 3, [ 2 ], true ],
]);
});
it("#hash", function() {
var instance = this.createInstance();
expect(function() {
instance.hash();
}).to.throw(Error, "should have been implemented by subclass");
});
it("#<", function() {
testCase(this, [
[ 1, [ 2 ], true ],
Expand Down
2 changes: 0 additions & 2 deletions src/sc/classlib/Math/SimpleNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,6 @@ SCScript.install(function(sc) {
return this.absdif($that) ["<"] ($precision);
}, "that; precision=0.0001");

// TODO: implements hash

spec.asInteger = function() {
return $SC.Integer(this._);
};
Expand Down
2 changes: 0 additions & 2 deletions src/sc/classlib/Math/SimpleNumber_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,6 @@
[ 10, [ 10.01, 0.02 ], true ],
]);
});
it.skip("#hash", function() {
});
it("#asInteger", function() {
testCase(this, [
[ $SC.Integer(10), [], $SC.Integer(10) ],
Expand Down
16 changes: 12 additions & 4 deletions src/sc/lang/klass/klass.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var klass = {};
var metaClasses = {};
var classes = klass.classes = {};
var hash = 0x100000;

var createClassInstance = function(MetaSpec) {
var instance = new SCClass();
Expand Down Expand Up @@ -46,7 +47,9 @@
className + bond + methodName + " is already defined."
);
}
methods[methodName] = func;
Object.defineProperty(methods, methodName, {
value: func, writable: true
});
};

if (typeof fn === "function") {
Expand Down Expand Up @@ -113,9 +116,11 @@
newClass = new MetaClass._MetaSpec();
newClass._name = className;
newClass._Spec = constructor;
constructor.prototype.__class = newClass;
constructor.prototype.__Spec = constructor;
constructor.prototype.__className = className;
Object.defineProperties(constructor.prototype, {
__class: { value: newClass, writable: true },
__Spec : { value: constructor },
__className: { value: className }
});
classes[className] = newClass;

return newClass;
Expand Down Expand Up @@ -196,6 +201,9 @@
Object.defineProperties(this, {
_immutable: {
value: false, writable: true
},
_hash: {
value: hash++
}
});
}
Expand Down
4 changes: 1 addition & 3 deletions src/sc/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@
return instance;
};

var __testid = Date.now();

sc.test.object = function(properties) {
var instance = sc.lang.klass.classes.Object.new();

Expand All @@ -284,7 +282,7 @@
});
});
}
instance.__testid = __testid++;
instance.__testid = instance._hash;

return instance;
};
Expand Down

0 comments on commit 8c05e2c

Please sign in to comment.