diff --git a/build/scscript.js b/build/scscript.js index 267ce86..285eaa3 100644 --- a/build/scscript.js +++ b/build/scscript.js @@ -91,26 +91,42 @@ var sc = { VERSION: "0.0.7" }; } }; - var def = function(className, spec, classMethods, instanceMethods) { + var def = function(className, spec, classMethods, instanceMethods, opts) { + + var setMethod = function(methods, methodName, func) { + var dot; + + if (methods.hasOwnProperty(methodName) && !(opts && opts.force)) { + dot = methods === classMethods ? "." : "#"; + throw new Error(className + dot + methodName + " is already defined."); + } + + methods[methodName] = func; + }; + Object.keys(spec).forEach(function(methodName) { var thrower; + if (methodName === "constructor") { return; } + if (throwError.hasOwnProperty(methodName)) { thrower = throwError[methodName]; spec[methodName].forEach(function(methodName) { if (methodName.charCodeAt(0) === 0x24) { // u+0024 is '$' methodName = methodName.substr(1); - classMethods[methodName] = thrower(className + "." + methodName); + setMethod(classMethods, methodName, thrower(className + "." + methodName)); } else { - instanceMethods[methodName] = thrower(className + "#" + methodName); + setMethod(instanceMethods, methodName, thrower(className + "#" + methodName)); } }); - } else if (methodName.charCodeAt(0) === 0x24) { // u+0024 is '$' - classMethods[methodName.substr(1)] = spec[methodName]; } else { - instanceMethods[methodName] = spec[methodName]; + if (methodName.charCodeAt(0) === 0x24) { // u+0024 is '$' + setMethod(classMethods, methodName.substr(1), spec[methodName]); + } else { + setMethod(instanceMethods, methodName, spec[methodName]); + } } }); }; @@ -147,7 +163,7 @@ var sc = { VERSION: "0.0.7" }; metaClass = constructor.metaClass; - def(className, spec, metaClass._MetaSpec.prototype, constructor.prototype); + def(className, spec, metaClass._MetaSpec.prototype, constructor.prototype, opts); metaClass._Spec = constructor; metaClass._isMetaClass = true; @@ -156,7 +172,7 @@ var sc = { VERSION: "0.0.7" }; classes[className] = null; }; - sc.lang.klass.refine = function(className, spec) { + sc.lang.klass.refine = function(className, spec, opts) { var metaClass; if (!metaClasses.hasOwnProperty(className)) { @@ -165,7 +181,7 @@ var sc = { VERSION: "0.0.7" }; ); } metaClass = metaClasses[className]; - def(className, spec, metaClass._MetaSpec.prototype, metaClass._Spec.prototype); + def(className, spec, metaClass._MetaSpec.prototype, metaClass._Spec.prototype, opts); }; sc.lang.klass.get = function(name) { @@ -266,6 +282,8 @@ var sc = { VERSION: "0.0.7" }; // src/sc/lang/classlib/Core/Object.js (function(sc) { + // var $SC = sc.lang.$SC; + sc.lang.klass.refine("Object", { js: function() { return this._raw; @@ -422,7 +440,6 @@ var sc = { VERSION: "0.0.7" }; "alwaysYield", "yieldAndReset", "idle", - "while", "dependants", "changed", "addDependant", @@ -490,7 +507,6 @@ var sc = { VERSION: "0.0.7" }; "archiveAsObject", "checkCanArchive", "writeTextArchive", - "protect", "$readTextArchive", "asTextArchive", "getContainedObjects", @@ -664,16 +680,12 @@ var sc = { VERSION: "0.0.7" }; "putN", "putAll", "do", - "while", "subSample", "loop", "generate", - "while", "collect", "reject", - "while", "select", - "while", "dot", "interlace", "appendStream", @@ -917,8 +929,6 @@ var sc = { VERSION: "0.0.7" }; "writeInputSpec", "series", "seriesIter", - "while", - "while", "degreeToKey", "keyToDegree", "nearestInList", @@ -1478,10 +1488,7 @@ var sc = { VERSION: "0.0.7" }; "numVars", "varArgs", "loop", - "loop", - "block", "block", - "try", "asRoutine", "dup", "sum", @@ -1765,7 +1772,6 @@ var sc = { VERSION: "0.0.7" }; "flopDict", "histo", "printAll", - "printAll", "printcsAll", "dumpAll", "printOn", @@ -1789,7 +1795,6 @@ var sc = { VERSION: "0.0.7" }; "$series", "$geom", "$fib", - "while", "$rand", "$exprand", "$rand2", @@ -1808,7 +1813,6 @@ var sc = { VERSION: "0.0.7" }; "indicesOfEqual", "find", "findAll", - "while", "indexOfGreaterThan", "indexIn", "indexInBetween", @@ -2024,19 +2028,11 @@ var sc = { VERSION: "0.0.7" }; "mergeSort", "mergeSortTemp", "mergeTemp", - "while", - "while", - "while", "insertionSort", "insertionSortRange", - "while", - "while", "hoareMedian", "hoareFind", - "while", "hoarePartition", - "while", - "while", "$streamContents", "$streamContentsLimit", "wrapAt", @@ -2075,9 +2071,7 @@ var sc = { VERSION: "0.0.7" }; "indexOf", "indexOfGreaterThan", "takeThese", - "while", "replace", - "while", "slotSize", "slotAt", "slotPut", @@ -2226,13 +2220,11 @@ var sc = { VERSION: "0.0.7" }; "containsi", "findRegexp", "findAllRegexp", - "while", "find", "findBackwards", "endsWith", "beginsWith", "findAll", - "while", "replace", "escapeChar", "shellQuote", @@ -2430,9 +2422,7 @@ var sc = { VERSION: "0.0.7" }; "$make", "$use", "make", - "protect", "use", - "protect", "eventAt", "composeEvents", "$pop", diff --git a/src/sc/lang/classlib/Collections/ArrayedCollection.js b/src/sc/lang/classlib/Collections/ArrayedCollection.js index 043a2e2..b1e03bc 100644 --- a/src/sc/lang/classlib/Collections/ArrayedCollection.js +++ b/src/sc/lang/classlib/Collections/ArrayedCollection.js @@ -23,9 +23,7 @@ "indexOf", "indexOfGreaterThan", "takeThese", - "while", "replace", - "while", "slotSize", "slotAt", "slotPut", diff --git a/src/sc/lang/classlib/Collections/Collection.js b/src/sc/lang/classlib/Collections/Collection.js index e626acd..6f92a54 100644 --- a/src/sc/lang/classlib/Collections/Collection.js +++ b/src/sc/lang/classlib/Collections/Collection.js @@ -85,7 +85,6 @@ "flopDict", "histo", "printAll", - "printAll", "printcsAll", "dumpAll", "printOn", diff --git a/src/sc/lang/classlib/Collections/Environment.js b/src/sc/lang/classlib/Collections/Environment.js index ab09bdf..e2c9d90 100644 --- a/src/sc/lang/classlib/Collections/Environment.js +++ b/src/sc/lang/classlib/Collections/Environment.js @@ -8,9 +8,7 @@ "$make", "$use", "make", - "protect", "use", - "protect", "eventAt", "composeEvents", "$pop", diff --git a/src/sc/lang/classlib/Collections/SequenceableCollection.js b/src/sc/lang/classlib/Collections/SequenceableCollection.js index 1e860a8..52b3e94 100644 --- a/src/sc/lang/classlib/Collections/SequenceableCollection.js +++ b/src/sc/lang/classlib/Collections/SequenceableCollection.js @@ -8,7 +8,6 @@ "$series", "$geom", "$fib", - "while", "$rand", "$exprand", "$rand2", @@ -27,7 +26,6 @@ "indicesOfEqual", "find", "findAll", - "while", "indexOfGreaterThan", "indexIn", "indexInBetween", @@ -243,19 +241,11 @@ "mergeSort", "mergeSortTemp", "mergeTemp", - "while", - "while", - "while", "insertionSort", "insertionSortRange", - "while", - "while", "hoareMedian", "hoareFind", - "while", "hoarePartition", - "while", - "while", "$streamContents", "$streamContentsLimit", "wrapAt", diff --git a/src/sc/lang/classlib/Collections/String.js b/src/sc/lang/classlib/Collections/String.js index e41f025..f319e42 100644 --- a/src/sc/lang/classlib/Collections/String.js +++ b/src/sc/lang/classlib/Collections/String.js @@ -77,13 +77,11 @@ "containsi", "findRegexp", "findAllRegexp", - "while", "find", "findBackwards", "endsWith", "beginsWith", "findAll", - "while", "replace", "escapeChar", "shellQuote", diff --git a/src/sc/lang/classlib/Core/Function.js b/src/sc/lang/classlib/Core/Function.js index 72c5bd8..97bb05f 100644 --- a/src/sc/lang/classlib/Core/Function.js +++ b/src/sc/lang/classlib/Core/Function.js @@ -38,10 +38,7 @@ "numVars", "varArgs", "loop", - "loop", - "block", "block", - "try", "asRoutine", "dup", "sum", diff --git a/src/sc/lang/classlib/Core/Object.js b/src/sc/lang/classlib/Core/Object.js index b6c1ba0..b1071e3 100644 --- a/src/sc/lang/classlib/Core/Object.js +++ b/src/sc/lang/classlib/Core/Object.js @@ -4,6 +4,8 @@ require("../../klass"); require("../../dollarSC"); + // var $SC = sc.lang.$SC; + sc.lang.klass.refine("Object", { js: function() { return this._raw; @@ -160,7 +162,6 @@ "alwaysYield", "yieldAndReset", "idle", - "while", "dependants", "changed", "addDependant", @@ -228,7 +229,6 @@ "archiveAsObject", "checkCanArchive", "writeTextArchive", - "protect", "$readTextArchive", "asTextArchive", "getContainedObjects", diff --git a/src/sc/lang/classlib/Math/SimpleNumber.js b/src/sc/lang/classlib/Math/SimpleNumber.js index 70e75c9..68cdb1c 100644 --- a/src/sc/lang/classlib/Math/SimpleNumber.js +++ b/src/sc/lang/classlib/Math/SimpleNumber.js @@ -152,8 +152,6 @@ "writeInputSpec", "series", "seriesIter", - "while", - "while", "degreeToKey", "keyToDegree", "nearestInList", diff --git a/src/sc/lang/classlib/Streams/Stream.js b/src/sc/lang/classlib/Streams/Stream.js index 470d386..a77c244 100644 --- a/src/sc/lang/classlib/Streams/Stream.js +++ b/src/sc/lang/classlib/Streams/Stream.js @@ -16,16 +16,12 @@ "putN", "putAll", "do", - "while", "subSample", "loop", "generate", - "while", "collect", "reject", - "while", "select", - "while", "dot", "interlace", "appendStream", diff --git a/src/sc/lang/klass.js b/src/sc/lang/klass.js index c4e3f6d..e953338 100644 --- a/src/sc/lang/klass.js +++ b/src/sc/lang/klass.js @@ -32,26 +32,42 @@ } }; - var def = function(className, spec, classMethods, instanceMethods) { + var def = function(className, spec, classMethods, instanceMethods, opts) { + + var setMethod = function(methods, methodName, func) { + var dot; + + if (methods.hasOwnProperty(methodName) && !(opts && opts.force)) { + dot = methods === classMethods ? "." : "#"; + throw new Error(className + dot + methodName + " is already defined."); + } + + methods[methodName] = func; + }; + Object.keys(spec).forEach(function(methodName) { var thrower; + if (methodName === "constructor") { return; } + if (throwError.hasOwnProperty(methodName)) { thrower = throwError[methodName]; spec[methodName].forEach(function(methodName) { if (methodName.charCodeAt(0) === 0x24) { // u+0024 is '$' methodName = methodName.substr(1); - classMethods[methodName] = thrower(className + "." + methodName); + setMethod(classMethods, methodName, thrower(className + "." + methodName)); } else { - instanceMethods[methodName] = thrower(className + "#" + methodName); + setMethod(instanceMethods, methodName, thrower(className + "#" + methodName)); } }); - } else if (methodName.charCodeAt(0) === 0x24) { // u+0024 is '$' - classMethods[methodName.substr(1)] = spec[methodName]; } else { - instanceMethods[methodName] = spec[methodName]; + if (methodName.charCodeAt(0) === 0x24) { // u+0024 is '$' + setMethod(classMethods, methodName.substr(1), spec[methodName]); + } else { + setMethod(instanceMethods, methodName, spec[methodName]); + } } }); }; @@ -88,7 +104,7 @@ metaClass = constructor.metaClass; - def(className, spec, metaClass._MetaSpec.prototype, constructor.prototype); + def(className, spec, metaClass._MetaSpec.prototype, constructor.prototype, opts); metaClass._Spec = constructor; metaClass._isMetaClass = true; @@ -97,7 +113,7 @@ classes[className] = null; }; - sc.lang.klass.refine = function(className, spec) { + sc.lang.klass.refine = function(className, spec, opts) { var metaClass; if (!metaClasses.hasOwnProperty(className)) { @@ -106,7 +122,7 @@ ); } metaClass = metaClasses[className]; - def(className, spec, metaClass._MetaSpec.prototype, metaClass._Spec.prototype); + def(className, spec, metaClass._MetaSpec.prototype, metaClass._Spec.prototype, opts); }; sc.lang.klass.get = function(name) { diff --git a/src/sc/lang/klass_test.js b/src/sc/lang/klass_test.js index 97ba256..b47717c 100644 --- a/src/sc/lang/klass_test.js +++ b/src/sc/lang/klass_test.js @@ -76,6 +76,31 @@ describe("sc.lang.klass", function() { sc.lang.klass.refine("NotRegisteredClass", {}); }).to.throw("class 'NotRegisteredClass' is not registered."); }); + it("should error when re-define method", function() { + expect(function() { + sc.lang.klass.refine("SubClass", { + $methodOverriddenInSubClass: function() { + return "SubClass.methodOverriddenInSubClass"; + } + }); + }).to.throw("SubClass.methodOverriddenInSubClass is already defined."); + expect(function() { + sc.lang.klass.refine("SubClass", { + methodOverriddenInSubClass: function() { + return "SubClass#methodOverriddenInSubClass"; + } + }); + }).to.throw("SubClass#methodOverriddenInSubClass is already defined."); + }); + it("should not throw error when re-defin method with option.force is true", function() { + expect(function() { + sc.lang.klass.refine("SubClass", { + methodOverriddenInSubClass: function() { + return "SubClass#methodOverriddenInSubClass"; + } + }, { force: true }); + }).to.not.throw("SubClass#methodOverriddenInSubClass is already defined."); + }); }); describe("Class", function() { before(function() {