Skip to content

Commit

Permalink
.ref() shall not pollute the global context Issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
xk committed Apr 14, 2012
1 parent 0076292 commit ed7fc7e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
18 changes: 7 additions & 11 deletions src/ref.js
@@ -1,25 +1,20 @@
(function (ctr) {

return function ref (ƒ) {

if (typeof ƒ !== 'function') {
throw TypeError('.ref( ƒ ) -> ƒ must be a function');
}

var globalReference= ƒ.name;

if (globalReference) {
this.eval(ƒ);
var that= this;
var globalReference= 'thread._refs['+ ctr+ ']';
if (ctr++) {
that.eval(globalReference+ '= '+ ƒ);
}
else {
globalReference= 'thread._refs['+ ctr+ ']';
ctr++;
this.eval('!thread._refs && (thread._refs= []);\n'+ globalReference+ '= '+ ƒ);
that.eval('thread._refs= ['+ ƒ+ ']');
}

var that= this;
function fun () {

var n= arguments.length;
var params= '()';
var cb;
Expand All @@ -36,7 +31,8 @@
}
}

return cb ? that.eval(globalReference+ params, cb) : that.eval(globalReference+ params);
cb ? that.eval(globalReference+ params, cb) : that.eval(globalReference+ params);
return fun;
}

fun._ref= globalReference;
Expand Down
2 changes: 1 addition & 1 deletion src/ref.js.c
@@ -1 +1 @@
static const char* kRef_js= "\n\x28\x66\x75\x6e\x63\x74\x69\x6f\x6e\x28\x29\x7b\x76\x61\x72 \x63\x74\x72\x3d\x30\x3b\x72\x65\x74\x75\x72\x6e \x66\x75\x6e\x63\x74\x69\x6f\x6e \x72\x65\x66\x28\xc6\x92\x29\x7b\x63\x6f\x6e\x73\x6f\x6c\x65\x2e\x6c\x6f\x67\x28\x22\x72\x65\x66 \x28\xc6\x92\x29\x22\x29\x3b\x69\x66\x28\x74\x79\x70\x65\x6f\x66 \xc6\x92\x21\x3d\x3d\x27\x66\x75\x6e\x63\x74\x69\x6f\x6e\x27\x29\x7b\x74\x68\x72\x6f\x77 \x54\x79\x70\x65\x45\x72\x72\x6f\x72\x28\x27\x2e\x72\x65\x66\x28 \xc6\x92 \x29 \x2d\x3e \xc6\x92 \x6d\x75\x73\x74 \x62\x65 \x61 \x66\x75\x6e\x63\x74\x69\x6f\x6e\x27\x29\x3b\x7d\n\x76\x61\x72 \x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x3d\xc6\x92\x2e\x6e\x61\x6d\x65\x3b\x69\x66\x28\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x29\x7b\x74\x68\x69\x73\x2e\x65\x76\x61\x6c\x28\xc6\x92\x29\x3b\x7d\n\x65\x6c\x73\x65\x7b\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x3d\x27\x74\x68\x72\x65\x61\x64\x2e\x5f\x72\x65\x66\x73\x5b\x27\x2b\x63\x74\x72\x2b\x27\x5d\x27\x3b\x63\x74\x72\x2b\x2b\x3b\x74\x68\x69\x73\x2e\x65\x76\x61\x6c\x28\x27\x21\x74\x68\x72\x65\x61\x64\x2e\x5f\x72\x65\x66\x73 \x26\x26 \x28\x74\x68\x72\x65\x61\x64\x2e\x5f\x72\x65\x66\x73\x3d \x5b\x5d\x29\x3b\x5c\x6e\x27\x2b\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x2b\x27\x3d \x27\x2b\xc6\x92\x29\x3b\x7d\n\x76\x61\x72 \x74\x68\x61\x74\x3d\x74\x68\x69\x73\x3b\x66\x75\x6e\x63\x74\x69\x6f\x6e \x66\x75\x6e\x28\x29\x7b\x76\x61\x72 \x6e\x3d\x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x76\x61\x72 \x70\x61\x72\x61\x6d\x73\x3d\x27\x28\x29\x27\x3b\x76\x61\x72 \x63\x62\x3b\x69\x66\x28\x6e\x29\x7b\x28\x74\x79\x70\x65\x6f\x66 \x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x5b\x6e\x2d\x31\x5d\x3d\x3d\x3d\x27\x66\x75\x6e\x63\x74\x69\x6f\x6e\x27\x29\x26\x26\x28\x63\x62\x3d\x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x5b\x2d\x2d\x6e\x5d\x29\x3b\x69\x66\x28\x6e\x29\x7b\x70\x61\x72\x61\x6d\x73\x3d\x5b\x5d\x3b\x66\x6f\x72\x28\x76\x61\x72 \x69\x3d\x30\x3b\x69\x3c\x6e\x3b\x69\x2b\x2b\x29\x7b\x70\x61\x72\x61\x6d\x73\x5b\x69\x5d\x3d\x4a\x53\x4f\x4e\x2e\x73\x74\x72\x69\x6e\x67\x69\x66\x79\x28\x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x5b\x69\x5d\x29\x3b\x7d\n\x70\x61\x72\x61\x6d\x73\x3d\x27\x28\x27\x2b\x70\x61\x72\x61\x6d\x73\x2e\x6a\x6f\x69\x6e\x28\x27\x2c\x27\x29\x2b\x27\x29\x27\x3b\x7d\x7d\n\x72\x65\x74\x75\x72\x6e \x63\x62\x3f\x74\x68\x61\x74\x2e\x65\x76\x61\x6c\x28\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x2b\x70\x61\x72\x61\x6d\x73\x2c\x63\x62\x29\x3a\x74\x68\x61\x74\x2e\x65\x76\x61\x6c\x28\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x2b\x70\x61\x72\x61\x6d\x73\x29\x3b\x7d\n\x66\x75\x6e\x2e\x5f\x72\x65\x66\x3d\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x3b\x72\x65\x74\x75\x72\x6e \x66\x75\x6e\x3b\x7d\x3b\x7d\x29\x28\x29\x3b";
static const char* kRef_js= "\n\x28\x66\x75\x6e\x63\x74\x69\x6f\x6e\x28\x63\x74\x72\x29\x7b\x72\x65\x74\x75\x72\x6e \x66\x75\x6e\x63\x74\x69\x6f\x6e \x72\x65\x66\x28\xc6\x92\x29\x7b\x69\x66\x28\x74\x79\x70\x65\x6f\x66 \xc6\x92\x21\x3d\x3d\x27\x66\x75\x6e\x63\x74\x69\x6f\x6e\x27\x29\x7b\x74\x68\x72\x6f\x77 \x54\x79\x70\x65\x45\x72\x72\x6f\x72\x28\x27\x2e\x72\x65\x66\x28 \xc6\x92 \x29 \x2d\x3e \xc6\x92 \x6d\x75\x73\x74 \x62\x65 \x61 \x66\x75\x6e\x63\x74\x69\x6f\x6e\x27\x29\x3b\x7d\n\x76\x61\x72 \x74\x68\x61\x74\x3d\x74\x68\x69\x73\x3b\x76\x61\x72 \x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x3d\x27\x74\x68\x72\x65\x61\x64\x2e\x5f\x72\x65\x66\x73\x5b\x27\x2b\x63\x74\x72\x2b\x27\x5d\x27\x3b\x69\x66\x28\x63\x74\x72\x2b\x2b\x29\x7b\x74\x68\x61\x74\x2e\x65\x76\x61\x6c\x28\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x2b\x27\x3d \x27\x2b\xc6\x92\x29\x3b\x7d\n\x65\x6c\x73\x65\x7b\x74\x68\x61\x74\x2e\x65\x76\x61\x6c\x28\x27\x74\x68\x72\x65\x61\x64\x2e\x5f\x72\x65\x66\x73\x3d \x5b\x27\x2b\xc6\x92\x2b\x27\x5d\x27\x29\x3b\x7d\n\x66\x75\x6e\x63\x74\x69\x6f\x6e \x66\x75\x6e\x28\x29\x7b\x76\x61\x72 \x6e\x3d\x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x2e\x6c\x65\x6e\x67\x74\x68\x3b\x76\x61\x72 \x70\x61\x72\x61\x6d\x73\x3d\x27\x28\x29\x27\x3b\x76\x61\x72 \x63\x62\x3b\x69\x66\x28\x6e\x29\x7b\x28\x74\x79\x70\x65\x6f\x66 \x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x5b\x6e\x2d\x31\x5d\x3d\x3d\x3d\x27\x66\x75\x6e\x63\x74\x69\x6f\x6e\x27\x29\x26\x26\x28\x63\x62\x3d\x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x5b\x2d\x2d\x6e\x5d\x29\x3b\x69\x66\x28\x6e\x29\x7b\x70\x61\x72\x61\x6d\x73\x3d\x5b\x5d\x3b\x66\x6f\x72\x28\x76\x61\x72 \x69\x3d\x30\x3b\x69\x3c\x6e\x3b\x69\x2b\x2b\x29\x7b\x70\x61\x72\x61\x6d\x73\x5b\x69\x5d\x3d\x4a\x53\x4f\x4e\x2e\x73\x74\x72\x69\x6e\x67\x69\x66\x79\x28\x61\x72\x67\x75\x6d\x65\x6e\x74\x73\x5b\x69\x5d\x29\x3b\x7d\n\x70\x61\x72\x61\x6d\x73\x3d\x27\x28\x27\x2b\x70\x61\x72\x61\x6d\x73\x2e\x6a\x6f\x69\x6e\x28\x27\x2c\x27\x29\x2b\x27\x29\x27\x3b\x7d\x7d\n\x63\x62\x3f\x74\x68\x61\x74\x2e\x65\x76\x61\x6c\x28\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x2b\x70\x61\x72\x61\x6d\x73\x2c\x63\x62\x29\x3a\x74\x68\x61\x74\x2e\x65\x76\x61\x6c\x28\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x2b\x70\x61\x72\x61\x6d\x73\x29\x3b\x72\x65\x74\x75\x72\x6e \x66\x75\x6e\x3b\x7d\n\x66\x75\x6e\x2e\x5f\x72\x65\x66\x3d\x67\x6c\x6f\x62\x61\x6c\x52\x65\x66\x65\x72\x65\x6e\x63\x65\x3b\x72\x65\x74\x75\x72\x6e \x66\x75\x6e\x3b\x7d\x3b\x7d\x29\x28\x30\x29";
4 changes: 2 additions & 2 deletions test/test30_ref_NamedFunction.js
Expand Up @@ -9,12 +9,12 @@ function cb (err, data) {
if (err) throw err;
if (+data !== r) throw Error('FAIL -> RESULT');
console.log('PASS, OK -> RESULT: ['+ [data, r]+ '], NAME: '+ ref._ref);
thread.destroy();
this.destroy();
}

var ref= thread.ref(fibo);

if (ref._ref !== fibo.name) throw Error('FAIL -> NAME');
if (ref._ref !== 'thread._refs[0]') throw Error('FAIL -> NAME');

ref(kN, cb);
var r= fibo(kN);
2 changes: 1 addition & 1 deletion test/test31_ref_anonymousFunction.js
Expand Up @@ -9,7 +9,7 @@ function cb (err, data) {
if (err) throw err;
if (+data !== r) throw Error('FAIL');
console.log('PASS, OK -> RESULT: ['+ [data, r]+ '], NAME: '+ ref._ref);
thread.destroy();
this.destroy();
}

var ref= thread.ref(anonymous);
Expand Down

0 comments on commit ed7fc7e

Please sign in to comment.