Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

docs

  • Loading branch information...
commit c3333030be09db8965cb77bb33703def2c8f38fc 1 parent 3bf5bd4
James Halliday authored November 06, 2012
6  example/attr.js
... ...
@@ -0,0 +1,6 @@
  1
+var fs = require('fs');
  2
+var scoper = require('../');
  3
+
  4
+var src = fs.readFileSync(__dirname + '/source/nested.js', 'utf8');
  5
+var c = Function('return ' + scoper(src))();
  6
+console.dir(c);
150  readme.markdown
Source Rendered
... ...
@@ -0,0 +1,150 @@
  1
+# scoper
  2
+
  3
+modify nested scope at runtime
  4
+
  5
+# example
  6
+
  7
+## modify
  8
+
  9
+Given this simple source:
  10
+
  11
+``` js
  12
+var x = 3;
  13
+
  14
+setInterval(function () {
  15
+    console.log(100 * x);
  16
+}, 500);
  17
+```
  18
+
  19
+we can modify the values of the variable `x` and the constant `100` at runtime:
  20
+
  21
+``` js
  22
+var fs = require('fs');
  23
+var scoper = require('scoper');
  24
+
  25
+var src = fs.readFileSync(__dirname + '/source/simple.js', 'utf8');
  26
+var c = Function('return ' + scoper(src))();
  27
+c.run();
  28
+
  29
+setInterval(function () {
  30
+    c.literal['arguments.0'][0] += 10;
  31
+}, 500);
  32
+
  33
+setTimeout(function () {
  34
+    c.scope[''].x = 11;
  35
+}, 2000);
  36
+```
  37
+
  38
+***
  39
+
  40
+```
  41
+$ node example/simple.js
  42
+300
  43
+330
  44
+360
  45
+390
  46
+1540
  47
+1650
  48
+1760
  49
+1870
  50
+1980
  51
+2090
  52
+^C
  53
+```
  54
+
  55
+## attributes
  56
+
  57
+Given some source:
  58
+
  59
+``` js
  60
+var x = 5;
  61
+
  62
+function foo (n) {
  63
+    var y = n + x + 100;
  64
+    
  65
+    return (function bar () {
  66
+        var z = 6;
  67
+        var f = function () {
  68
+            var q = y * 10;
  69
+            var x = z + 2;
  70
+            return q + x;
  71
+        };
  72
+        return f(z);
  73
+    })();
  74
+}
  75
+
  76
+setInterval(function () {
  77
+    console.log(foo(32));
  78
+}, 500);
  79
+```
  80
+
  81
+the attribute object looks like:
  82
+
  83
+```
  84
+$ node example/attr
  85
+{ scope: 
  86
+   { '': {},
  87
+     'body.1': {},
  88
+     'body.1.callee': {},
  89
+     'body.1.callee.init': {},
  90
+     'body.1.callee.right': {},
  91
+     'arguments.0': {} },
  92
+  function: 
  93
+   { 'body.1.callee.right': [Function],
  94
+     'body.1.callee': [Function: bar],
  95
+     'body.1': [Function: foo],
  96
+     'arguments.0': [Function] },
  97
+  literal: 
  98
+   { '': [ 5, 500 ],
  99
+     'body.1': [ 100 ],
  100
+     'body.1.callee': [ 6 ],
  101
+     'body.1.callee.init': [ 10, 2 ],
  102
+     'arguments.0': [ 32 ] },
  103
+  run: [Function] }
  104
+```
  105
+
  106
+# methods
  107
+
  108
+``` js
  109
+var scoper = require('scoper')
  110
+```
  111
+
  112
+## var newSrc = scoper(src)
  113
+
  114
+Return a string that rewrites `src` to add scoping instrumentation hooks.
  115
+
  116
+## var c = Function('return ' + newSrc)()
  117
+
  118
+Create a new scoper context `c` from the javascript source string `newSrc`
  119
+returned by `scoper()`.
  120
+
  121
+## c.run()
  122
+
  123
+Run the source. You can run the source as many times as you like. You can modify
  124
+the scope attributes before, after, or during a run.
  125
+
  126
+# attributes
  127
+
  128
+## c.scope
  129
+
  130
+All the variables are kept in this object.
  131
+
  132
+## c.function
  133
+
  134
+Copies of all functions are kept here so you can modify the functions at runtime.
  135
+
  136
+## c.literal
  137
+
  138
+The literal number, string, boolean, and regex values are stored in this object.
  139
+
  140
+# install
  141
+
  142
+With [npm](https://npmjs.org) do:
  143
+
  144
+```
  145
+npm install scoper
  146
+```
  147
+
  148
+# license
  149
+
  150
+MIT

0 notes on commit c333303

Please sign in to comment.
Something went wrong with that request. Please try again.