diff --git a/build/scscript-classlib.js b/build/scscript-classlib.js index ce22e18..f285a62 100644 --- a/build/scscript-classlib.js +++ b/build/scscript-classlib.js @@ -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); @@ -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); }; @@ -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._); }; @@ -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 ], @@ -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()); diff --git a/build/scscript.js b/build/scscript.js index 6adfbbd..e9ca31b 100644 --- a/build/scscript.js +++ b/build/scscript.js @@ -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) { @@ -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(); @@ -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") { @@ -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; @@ -825,6 +830,9 @@ var sc = { VERSION: "0.0.32" }; Object.defineProperties(this, { _immutable: { value: false, writable: true + }, + _hash: { + value: hash++ } }); } diff --git a/package.json b/package.json index f2c160f..8e01e56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scscript", - "version": "0.0.32", + "version": "0.0.33", "author": "Nao Yonamine ", "homepage": "http://mohayonao.github.io/SCScript/", "bugs": "https://github.com/mohayonao/SCScript/issues", diff --git a/src/sc/classlib/Collections/Association.js b/src/sc/classlib/Collections/Association.js index 1e0feff..dc43637 100644 --- a/src/sc/classlib/Collections/Association.js +++ b/src/sc/classlib/Collections/Association.js @@ -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()); diff --git a/src/sc/classlib/Collections/Association_test.js b/src/sc/classlib/Collections/Association_test.js index 124a648..88d876c 100644 --- a/src/sc/classlib/Collections/Association_test.js +++ b/src/sc/classlib/Collections/Association_test.js @@ -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; diff --git a/src/sc/classlib/Core/Object.js b/src/sc/classlib/Core/Object.js index a59c90f..6a461cc 100644 --- a/src/sc/classlib/Core/Object.js +++ b/src/sc/classlib/Core/Object.js @@ -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); diff --git a/src/sc/classlib/Core/Object_test.js b/src/sc/classlib/Core/Object_test.js index 1537464..589adfd 100644 --- a/src/sc/classlib/Core/Object_test.js +++ b/src/sc/classlib/Core/Object_test.js @@ -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; diff --git a/src/sc/classlib/Math/Integer.js b/src/sc/classlib/Math/Integer.js index c9da6f2..adf007c 100644 --- a/src/sc/classlib/Math/Integer.js +++ b/src/sc/classlib/Math/Integer.js @@ -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 ], diff --git a/src/sc/classlib/Math/Integer_test.js b/src/sc/classlib/Math/Integer_test.js index e284f13..a78db7d 100644 --- a/src/sc/classlib/Math/Integer_test.js +++ b/src/sc/classlib/Math/Integer_test.js @@ -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, [ diff --git a/src/sc/classlib/Math/Magnitude.js b/src/sc/classlib/Math/Magnitude.js index f395ba1..9acda53 100644 --- a/src/sc/classlib/Math/Magnitude.js +++ b/src/sc/classlib/Math/Magnitude.js @@ -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); }; diff --git a/src/sc/classlib/Math/Magnitude_test.js b/src/sc/classlib/Math/Magnitude_test.js index c4f6ba9..7593999 100644 --- a/src/sc/classlib/Math/Magnitude_test.js +++ b/src/sc/classlib/Math/Magnitude_test.js @@ -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 ], diff --git a/src/sc/classlib/Math/SimpleNumber.js b/src/sc/classlib/Math/SimpleNumber.js index 045eb6a..a43603f 100644 --- a/src/sc/classlib/Math/SimpleNumber.js +++ b/src/sc/classlib/Math/SimpleNumber.js @@ -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._); }; diff --git a/src/sc/classlib/Math/SimpleNumber_test.js b/src/sc/classlib/Math/SimpleNumber_test.js index 9b344dd..5cea111 100644 --- a/src/sc/classlib/Math/SimpleNumber_test.js +++ b/src/sc/classlib/Math/SimpleNumber_test.js @@ -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) ], diff --git a/src/sc/lang/klass/klass.js b/src/sc/lang/klass/klass.js index 71ec008..64e6a95 100644 --- a/src/sc/lang/klass/klass.js +++ b/src/sc/lang/klass/klass.js @@ -10,6 +10,7 @@ var klass = {}; var metaClasses = {}; var classes = klass.classes = {}; + var hash = 0x100000; var createClassInstance = function(MetaSpec) { var instance = new SCClass(); @@ -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") { @@ -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; @@ -196,6 +201,9 @@ Object.defineProperties(this, { _immutable: { value: false, writable: true + }, + _hash: { + value: hash++ } }); } diff --git a/src/sc/test/utils.js b/src/sc/test/utils.js index 6c67738..9466cdd 100644 --- a/src/sc/test/utils.js +++ b/src/sc/test/utils.js @@ -272,8 +272,6 @@ return instance; }; - var __testid = Date.now(); - sc.test.object = function(properties) { var instance = sc.lang.klass.classes.Object.new(); @@ -284,7 +282,7 @@ }); }); } - instance.__testid = __testid++; + instance.__testid = instance._hash; return instance; };