Skip to content

Commit

Permalink
Actually stop modifying built-ins.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Apr 10, 2012
1 parent b22a6ff commit 8a27b10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 8 additions & 8 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function typeOf(value) {
/**
Normalized version of `typeof`.
**/
return stringify(value).split(' ')[1].split(']')[0]
var type = stringify(value).split(' ')[1].split(']')[0]
return type === 'Object' && Object.getPrototypeOf(value) ? 'Type' : type
}

var types = {
Expand Down Expand Up @@ -86,10 +87,11 @@ function protocol(signature) {
var index = method[':this-index']
var name = method[':name']
var target = arguments[index]
var type = typeOf(target)
var f = (
(target && target[name]) || // By instance
(types[typeOf(target)][name]) || // By type
(types.Object[name])) // Default
(target && target[name]) || // By instance
(type in types && types[type][name]) || // By type
(types.Object[name])) // Default

if (!f) throw TypeError(ERROR_DOES_NOT_IMPLEMENTS + key)
return f.apply(f, arguments)
Expand All @@ -105,10 +107,8 @@ exports.protocol = protocol

function extend(protocol, type, implementation) {
var descriptor = {}
if (typeof(type) === 'function' && typeof(type.prototype) !== 'function')
type = type.prototype
else
type = types[typeOf(type)] || type
if (typeof(type) === 'function') type = type.prototype
type = types[typeOf(type)] || type

Object.keys(implementation).forEach(function(key, name) {
if (key in protocol) {
Expand Down
15 changes: 15 additions & 0 deletions tests/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ exports['test inline types'] = function(assert) {
assert.equal(Foo.isFoo(bar), true, 'bar is foo')
}

exports['test that globals are not mutated'] = function(assert) {
var prototypeNames = Object.getOwnPropertyNames(Object.prototype)
var functionNames = Object.getOwnPropertyNames(Object)

var Foo = protocol({
isFoo: [ protocol ]
})
Foo(Object, { isFoo: function(object) { return false } })

assert.deepEqual(Object.getOwnPropertyNames(Object),
functionNames, 'no properties were added to function')
assert.deepEqual(Object.getOwnPropertyNames(Object.prototype),
prototypeNames, 'no properties were added to prototype')
}

if (module == require.main)
require('test').run(exports);

Expand Down

0 comments on commit 8a27b10

Please sign in to comment.