Skip to content

Commit

Permalink
added some runkit functions and reimplemented create_function()
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Apr 6, 2009
1 parent 09ff5fe commit 2c68581
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 18 deletions.
3 changes: 0 additions & 3 deletions _unported/runkit/runkit_function_add.js

This file was deleted.

3 changes: 0 additions & 3 deletions _unported/runkit/runkit_function_copy.js

This file was deleted.

3 changes: 0 additions & 3 deletions _unported/runkit/runkit_function_redefine.js

This file was deleted.

3 changes: 0 additions & 3 deletions _unported/runkit/runkit_function_remove.js

This file was deleted.

3 changes: 0 additions & 3 deletions _unported/runkit/runkit_function_rename.js

This file was deleted.

10 changes: 7 additions & 3 deletions functions/funchand/create_function.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
function create_function (args, code) {
// http://kevin.vanzonneveld.net
// + original by: Johnny Mast (http://www.phpvrouwen.nl)
// + reimplemented by: Brett Zamir (http://brettz9.blogspot.com)
// * example 1: f = create_function('a, b', "return (a + b);");
// * example 1: f(1, 2);
// * returns 1: 3

eval('var _oFunctionObject = function (' + args + ') { ' + code + '}');
return _oFunctionObject;
try {
return Function.apply(null, args.split(',').concat(code));
}
catch (e) {
return false;
}
}
19 changes: 19 additions & 0 deletions functions/runkit/runkit_function_add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function runkit_function_add (funcname, arglist, code) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brettz9.blogspot.com)
// % note 1: Function can only be added to the global context; use create_function() for an anonymous function
// * example 1: runkit_function_add('add', 'a, b', "return (a + b);");
// * returns 1: true

if (window[funcname] !== undefined) { // Presumably disallows adding where exists, since there is also redefine function
return false;
}

try {
window[funcname] = Function.apply(null, arglist.split(',').concat(code));
return true;
}
catch (e) {
return false;
}
}
14 changes: 14 additions & 0 deletions functions/runkit/runkit_function_copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function runkit_function_copy (funcname, targetname) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brettz9.blogspot.com)
// % note 1: Function can only be copied to and from the global context
// * example 1: function plus (a, b) { return (a + b); }
// * example 1: runkit_function_copy('plus', 'add');
// * returns 1: true

if (typeof window[funcname] !== 'function' || window[targetname] !== undefined) { // (presumably disallow overwriting existing variables)
return false;
}
window[targetname] = window[funcname];
return true;
}
20 changes: 20 additions & 0 deletions functions/runkit/runkit_function_redefine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function runkit_function_redefine (funcname, arglist, code) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brettz9.blogspot.com)
// % note 1: Function can only be added to the global context; use create_function() for an anonymous function
// * example 1: function add (a, b, c) {return a+b+c;}
// * example 1: runkit_function_redefine('add', 'a, b', "return (a + b);");
// * returns 1: true

if (window[funcname] === undefined) { // Requires existing function?
return false;
}

try {
window[funcname] = Function.apply(null, arglist.split(',').concat(code));
return true;
}
catch (e) {
return false;
}
}
20 changes: 20 additions & 0 deletions functions/runkit/runkit_function_remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function runkit_function_remove (funcname) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brettz9.blogspot.com)
// % note 1: Function can only remove from the global context
// * example 1: function add (a, b, c) {return a+b+c;}
// * example 1: runkit_function_remove('add');
// * returns 1: true

if (window[funcname] === undefined) { // Requires existing function?
return false;
}

try {
delete window[funcname];
return true;
}
catch (e) {
return false;
}
}
15 changes: 15 additions & 0 deletions functions/runkit/runkit_function_rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function runkit_function_rename (funcname, newname) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brettz9.blogspot.com)
// % note 1: Function can only be copied to and from the global context
// * example 1: function plus (a, b) { return (a + b); }
// * example 1: runkit_function_rename('plus', 'add');
// * returns 1: true

if (typeof window[newname] !== 'function' || window[funcname] !== undefined) { // (presumably disallow overwriting existing variables)
return false;
}
window[newname] = window[funcname];
delete window[funcname];
return true;
}

0 comments on commit 2c68581

Please sign in to comment.