Table of Contents generated with DocToc
_R
_R is a reflection library.
Installation
npm install reflect-r
Node.js:
var _R = ;
Browsers:
Running tests
gulp test
Building project
gulp build
Compatibility, requirements
_R requires following ES5/ES6 features:
Required functions (some functions can not work without them)
- Object.defineProperty
- Object.getOwnPropertyDescriptor
- JSON.stringify (can be polyfilled)
Recommended functions (some functions can work incorrectly without them)
- Object.getPrototypeOf
- Object.getOwnPropertyNames
Tested environments
These enviroments pass automatic tests.
- Node 0.8.14
- Firefox 33
- Chrome 36
See section 'Implementation dependant' for futher information.
Test coverage
- Statements : 91.1% (215/236)
- Branches : 70.41% (69/98)
- Functions : 88.68% (47/53)
- Lines : 90.99% (212/233)
_R settings
_R
Changes directive placed in every function.
Available directives
_R.DIRECTIVE_NORMAL // ''; is placed before every new function
_R.DIRECTIVE_STRICT // 'use strict'; is placed before every function; default
_R.DIRECTIVE_ASM // 'use asm'; is placed before every new function
Implementation dependant
_R.isValidVariableName
tests variable name against current implementation rules._R.isBoundOrNativeFunction
is slow in V8 (due to V8's incompatibility with ES6 spec)._R.getObjectPrototype
can fail in IE8 and lower. Internally it prefersObject.getPrototypeOf
over.__proto__
over.constructor.prototype
.
_R methods
Checks and tests
isValidVariableName
_R
Checks if supplied name
is a valid variable identifier in current JS implementation.
isBoundOrNativeFunction
_R
Checks if supplied func
is bound (.bind
) or native code.
Retrieve informations
getFunctionSourceCode
_R
Returns FunctionExpression
. Throws error when called on non-function, bound function or native-code function.
getInternalClass
_R
Returns [[Class]]
's name of what
.
getObjectPrototype
_R
If what
is an object, returns it's prototype. Otherwise, returns null
.
Can return invalid object in IE8 and lower.
getPrototypesChain
_R
If what
is an object, returns array containing what
and objects in it's prototype chain (array ends with null
).
Otherwise, return [what, null]
.
When cyclical reference is detected (possible in IE8 and lower), function returns with current prototypes list.
Modify functions
declosureFunction
_R;
Returns func
redefined in global context. transformer
function is called on source code of func
before code evaluation.
transformer
function transformer(sourceCode, originalFunction) { /* magic */ return transformedSourceCode; }
createNamedFunction
_R
Works like Function
constructor but first argument is the function name (used in recursive calls).
createClosure
_R
Creates closure in given context. Example:
console // undefinedvar showFactorial = _R;;
makeGeneric
_R;
Changes method to function accepting this
as first argument.
Usage example
var slice = _R;;
wrapFunction
_R
Returns new function. New function pseudocode:
{var error = null;var data = null;var newArgs = || arguments;try data = func; catcheerror = e;var afterResult = ;data = afterResult || data;return data;}
before
function before(func, thisArg, funcArguments, dataObject) {}
before
function can return array or modify funcArguments
object. It will be used by wraped function.
after
function after(func, thisArg, funcError, funcResult, funcArguments, dataObject) {}
Usage example
$ = jQuery = _R
Utility
indirectEval
_R;
Works like eval
but:
- code scope and context always follow rules for ECMAScript strict mode
eval
- if preparationCode isn't supplied, code is run with global settings directive (default: 'use strict')
ES6 Reflect methods
construct
_R
This function follows specification of Reflect.construct
from ES6 (26.1.2).
Usage example
_R;
has
_R
This function follows specification of Reflect.has
from ES6 (26.1.9).
apply
_R
This function follows specification of Reflect.apply
from ES6 (26.1.3).
Create and modify objects
Proxy
target getHandler setHandler;
Creates proxy object for target. Proxy objects are sealed (Object.seal
).
createProxy
_R;
Alias for new _R.Proxy
.
defaultSetter
_RdefaultSetter
Transfers assigment to original object.
Usage example
{ 'strict mode'; thisdiameter = undefined; // property have to exist thisarea = undefined; // property have to exist thisradius = r; return _R;}Circle { if propertyName === 'radius' return originalObjectradius; if propertyName === 'diameter' return proxyObjectradius*2; if propertyName === 'area' return proxyObjectradius*proxyObjectradius*MathPI; }; Circle { if propertyName !== 'radius' throw Error'You can not modify anything in circle except radius'; else return originalObjectradius = propertyValue; }; var k = 5;kradius === kdiameter*2; // truekdiameter = 7; // Always throw Error: You can not modify anything in circle except radiuskoh = 'hai'; // Error in strict mode; Does nothing in sloppy modekradius = 11; // worksconsole; // 22
forbidPropertyNames
_R
Forbids writing to properties of object.
Returns modified what
.
...names
can be any combination of strings and array of strings.
Usage example
_R;Objectprototype{}; // throws TypeError
addMagicLengthProperty
_R;
Adds magic length
property to what
object. It works almost as length
property of arrays but it does not support length exceeding maximal index.
If readOnly
argument is false, changes in length
property will remove indexes over specified length.