forked from locutusjs/locutus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added some runkit functions and reimplemented create_function()
- Loading branch information
Showing
11 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |