Skip to content

Commit

Permalink
Make possible to implement protocol for type if already implemented b…
Browse files Browse the repository at this point in the history
…y ancestor.
  • Loading branch information
Gozala committed Mar 21, 2012
1 parent 10f28a3 commit 5004e70
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core.js
Expand Up @@ -6,10 +6,14 @@

var unbind = Function.call.bind(Function.bind, Function.call)
var slice = unbind(Array.prototype.slice)
var owns = unbind(Object.prototype.hasOwnProperty)
var args = (function() { return arguments })()
var Arguments = Object.getPrototypeOf(args)
var stringify = unbind(Object.prototype.toString)

var ERROR_IMPLEMENTS = 'Type already implements this protocol: '
var ERROR_DOES_NOT_IMPLEMENTS = 'Protocol is not implemented: '

function Name(name) {
return ':' + (name || '') + ':' + Math.round(Math.random() * 1000000000000000)
}
Expand Down Expand Up @@ -105,7 +109,7 @@ function define(signature) {
(isFunction(target) && builtins.Function[name]) ||
(isObject(target) && builtins.Object[name]))

if (!f) throw TypeError('Protocol is not implemented: ' + key)
if (!f) throw TypeError(ERROR_DOES_NOT_IMPLEMENTS + key)
return f.apply(f, arguments)
}
method[':this-index'] = signature[key].indexOf(define)
Expand All @@ -128,7 +132,7 @@ function extend(protocol, type, implementation) {
Object.keys(implementation).forEach(function(key, name) {
if (key in protocol) {
name = protocol[key][':name']
if (name in type) throw Error('Type already implements protocol: ' + key)
if (owns(type, name)) throw Error(ERROR_IMPLEMENTS + key)
descriptor[name] = {
value: implementation[key],
enumerable: false,
Expand Down

0 comments on commit 5004e70

Please sign in to comment.