scoper
modify nested scope at runtime
example
modify
Given this simple source:
var x = 3;;
we can modify the values of the variable x
and the constant 100
at runtime:
var fs = ;var scoper = ;var src = fs;var c = Function'return ' + ;c;;;
$ node example/simple.js
300
330
360
390
1540
1650
1760
1870
1980
2090
^C
attributes
Given some source:
var x = 5;{var y = n + x + 100;return {var z = 6;var {var q = y * 10;var x = z + 2;return q + x;};return ;};};
the attribute object looks like:
$ node example/attr
{ scope:
{ '': {},
'body.1': {},
'body.1.callee': {},
'body.1.callee.init': {},
'body.1.callee.right': {},
'arguments.0': {} },
function:
{ 'body.1.callee.right': [Function],
'body.1.callee': [Function: bar],
'body.1': [Function: foo],
'arguments.0': [Function] },
literal:
{ '': [ 5, 500 ],
'body.1': [ 100 ],
'body.1.callee': [ 6 ],
'body.1.callee.init': [ 10, 2 ],
'arguments.0': [ 32 ] },
patch: [Function],
run: [Function] }
methods
var scoper =
var newSrc = scoper(src, opts)
Return a string that rewrites src
to add scoping instrumentation hooks.
Optionally:
opts.names
- an object mapping scope
, function
, and literal
keys to
internal ids to use for names in the rewritten source. By default, these names
are randomly generated.
var c = Function('return ' + newSrc)()
Create a new scoper context c
from the javascript source string newSrc
returned by scoper()
.
c.run()
Run the source. You can run the source as many times as you like. You can modify the scope attributes before, after, or during a run.
c.patch(ctx)
Patch the current context with a new context ctx
.
ctx
should have one or more attributes ctx.scope
, ctx.function
and
ctx.literal
to replace the internal attributes of the current object.
attributes
c.scope
All the variables are kept in this object.
c.function
Copies of all functions are kept here so you can modify the functions at runtime.
c.literal
The literal number, string, boolean, and regex values are stored in this object.
install
With npm do:
npm install scoper
license
MIT