+1
| module.exports = require('./lib/scope'); |
+27
-12
@@ -60,6 +60,6 @@ (function() { | ||
| } else { | ||
| return x.literal(); | ||
| return x.literalize(); | ||
| } | ||
| }; | ||
| Scope.rootValues = { | ||
| Scope.rootLocals = { | ||
| "__slice": Array.prototype.slice, | ||
@@ -107,3 +107,3 @@ "__bind": function(fn, me) { | ||
| return function() { | ||
| return this._eval(name); | ||
| return this.get(name); | ||
| }; | ||
@@ -113,5 +113,3 @@ }; | ||
| return function(val) { | ||
| return this._eval.call({ | ||
| val: val | ||
| }, "" + name + " = this.val"); | ||
| return this.set(name, val, false); | ||
| }; | ||
@@ -122,3 +120,3 @@ }; | ||
| return this.root = this.global.extend({ | ||
| values: this.rootValues | ||
| locals: this.rootLocals | ||
| }); | ||
@@ -132,3 +130,3 @@ }; | ||
| this.options = options != null ? options : {}; | ||
| varTypes = ['values', 'literals']; | ||
| varTypes = ['locals', 'literals']; | ||
| for (_i = 0, _len = varTypes.length; _i < _len; _i++) { | ||
@@ -140,3 +138,3 @@ k = varTypes[_i]; | ||
| } | ||
| this.options.values.__scope = this; | ||
| this.options.locals.__scope = this; | ||
| this.parent = this.options.parent; | ||
@@ -166,5 +164,3 @@ names = []; | ||
| return _results; | ||
| }).call(this)).join(''), this.parent.eval({ | ||
| locals: this.options.values | ||
| }, literals + EVAL_LITERAL)); | ||
| }).call(this)).join(''), this.parent.eval(this.options, literals + EVAL_LITERAL)); | ||
| exports = (function() { | ||
@@ -224,2 +220,21 @@ var _l, _len4, _ref3, _results; | ||
| }; | ||
| Scope.prototype.set = function(name, val, isLiteral) { | ||
| var obj, _len, _ref, _results; | ||
| if (isString(name)) { | ||
| return this._eval.call({ | ||
| val: val | ||
| }, ("" + name + " = ") + (isLiteral ? literalize(val) : "this.val")); | ||
| } else { | ||
| _ref = [name, val], obj = _ref[0], isLiteral = _ref[1]; | ||
| _results = []; | ||
| for (val = 0, _len = obj.length; val < _len; val++) { | ||
| name = obj[val]; | ||
| _results.push(this.set(name, val, isLiteral)); | ||
| } | ||
| return _results; | ||
| } | ||
| }; | ||
| Scope.prototype.get = function(name) { | ||
| return this._eval(name); | ||
| }; | ||
| Scope.prototype.extend = function(options) { | ||
@@ -226,0 +241,0 @@ if (options == null) { |
+12
-107
@@ -7,3 +7,2 @@ /*--------------------- Layout and Typography ----------------------------*/ | ||
| color: #252519; | ||
| margin: 0; padding: 0; | ||
| } | ||
@@ -13,111 +12,17 @@ a { | ||
| } | ||
| a:visited { | ||
| color: #261a3b; | ||
| } | ||
| p { | ||
| margin: 0 0 15px 0; | ||
| a:visited { | ||
| color: #261a3b; | ||
| } | ||
| h1, h2, h3, h4, h5, h6 { | ||
| margin: 0px 0 15px 0; | ||
| pre, code { | ||
| background: #eeeeee; | ||
| border: 1px solid #e3e3e3; | ||
| padding: 0 5px; | ||
| border-radius: 3px; | ||
| } | ||
| h1 { | ||
| margin-top: 40px; | ||
| } | ||
| #container { | ||
| position: relative; | ||
| pre code { | ||
| border: 0px; | ||
| padding: 0; | ||
| } | ||
| #background { | ||
| position: fixed; | ||
| top: 0; left: 525px; right: 0; bottom: 0; | ||
| background: #f5f5ff; | ||
| border-left: 1px solid #e5e5ee; | ||
| z-index: -1; | ||
| } | ||
| #jump_to, #jump_page { | ||
| background: white; | ||
| -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777; | ||
| -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px; | ||
| font: 10px Arial; | ||
| text-transform: uppercase; | ||
| cursor: pointer; | ||
| text-align: right; | ||
| } | ||
| #jump_to, #jump_wrapper { | ||
| position: fixed; | ||
| right: 0; top: 0; | ||
| padding: 5px 10px; | ||
| } | ||
| #jump_wrapper { | ||
| padding: 0; | ||
| display: none; | ||
| } | ||
| #jump_to:hover #jump_wrapper { | ||
| display: block; | ||
| } | ||
| #jump_page { | ||
| padding: 5px 0 3px; | ||
| margin: 0 0 25px 25px; | ||
| } | ||
| #jump_page .source { | ||
| display: block; | ||
| padding: 5px 10px; | ||
| text-decoration: none; | ||
| border-top: 1px solid #eee; | ||
| } | ||
| #jump_page .source:hover { | ||
| background: #f5f5ff; | ||
| } | ||
| #jump_page .source:first-child { | ||
| } | ||
| table td { | ||
| border: 0; | ||
| outline: 0; | ||
| } | ||
| td.docs, th.docs { | ||
| max-width: 450px; | ||
| min-width: 450px; | ||
| min-height: 5px; | ||
| padding: 10px 25px 1px 50px; | ||
| overflow-x: hidden; | ||
| vertical-align: top; | ||
| text-align: left; | ||
| } | ||
| .docs pre { | ||
| margin: 15px 0 15px; | ||
| padding-left: 15px; | ||
| } | ||
| .docs p tt, .docs p code { | ||
| background: #f8f8ff; | ||
| border: 1px solid #dedede; | ||
| font-size: 12px; | ||
| padding: 0 0.2em; | ||
| } | ||
| .pilwrap { | ||
| position: relative; | ||
| } | ||
| .pilcrow { | ||
| font: 12px Arial; | ||
| text-decoration: none; | ||
| color: #454545; | ||
| position: absolute; | ||
| top: 3px; left: -20px; | ||
| padding: 1px 2px; | ||
| opacity: 0; | ||
| -webkit-transition: opacity 0.2s linear; | ||
| } | ||
| td.docs:hover .pilcrow { | ||
| opacity: 1; | ||
| } | ||
| td.code, th.code { | ||
| padding: 14px 15px 16px 25px; | ||
| width: 100%; | ||
| vertical-align: top; | ||
| background: #f5f5ff; | ||
| border-left: 1px solid #e5e5ee; | ||
| } | ||
| pre, tt, code { | ||
| font-size: 12px; line-height: 18px; | ||
| font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace; | ||
| margin: 0; padding: 0; | ||
| } | ||
@@ -26,7 +26,7 @@ <!DOCTYPE html> <html> <head> <title>scope.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> scope.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p>File header.</p> | ||
| <p><code>Scope.eval</code> provides the outer scope for all scopes in Scopejs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@eval = </span><span class="o">`</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">eval</span><span class="p">)(</span><span class="nx">EVAL_LITERAL</span><span class="p">)</span><span class="o">`</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <h4>Scope.literalize(x)</h4> | ||
| <p><code>Scope.eval</code> provides the outer scope for all scoped evals.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@eval = </span><span class="o">`</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">eval</span><span class="p">)(</span><span class="nx">EVAL_LITERAL</span><span class="p">)</span><span class="o">`</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <h4>Scope.literalize(x)</h4> | ||
| <p>Return a literal expression for <code>x</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@literalize = </span><span class="nv">literalize = </span><span class="nf">(x) -></span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>A string is a literal expression of some other value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">isString</span> <span class="nx">x</span> <span class="k">then</span> <span class="nx">x</span> | ||
| <p>Return a literal expression for <code>x</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@literalize = </span><span class="nv">literalize = </span><span class="nf">(x) -></span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>A string must already be an expression.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">isString</span> <span class="nx">x</span> <span class="k">then</span> <span class="nx">x</span> | ||
| <span class="k">else</span> <span class="k">if</span> <span class="nx">isFn</span> <span class="nx">x</span> </pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>'Decompile' a function using <code>Function::toString</code> (where supported)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">x = </span><span class="nx">x</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>Remove name from named function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">x = </span><span class="nx">x</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/function[^\(]*\(/</span><span class="p">,</span> <span class="s2">"function ("</span> | ||
| <span class="s2">"(#{x})"</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Otherwise, <code>x</code> must literalize itself.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">else</span> <span class="nx">x</span><span class="p">.</span><span class="nx">literal</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <h4>Class properties intended to be overridden.</h4> </td> <td class="code"> <div class="highlight"><pre> </pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>These values are defined in the root scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@rootValues =</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>CoffeeScript runtime helpers</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">__slice: </span><span class="nb">Array</span><span class="o">::</span><span class="nx">slice</span> | ||
| <span class="s2">"(#{x})"</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Otherwise, <code>x</code> must literalize itself.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">else</span> <span class="nx">x</span><span class="p">.</span><span class="nx">literalize</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <h4>Class properties intended to be overridden.</h4> </td> <td class="code"> <div class="highlight"><pre> </pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>These locals are defined in the root scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@rootLocals =</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>CoffeeScript runtime helpers</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">__slice: </span><span class="nb">Array</span><span class="o">::</span><span class="nx">slice</span> | ||
| <span class="nv">__bind: </span><span class="nf">(fn, me) -></span> <span class="o">-></span> <span class="nx">fn</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">me</span><span class="p">,</span> <span class="nx">arguments</span> | ||
@@ -45,6 +45,4 @@ <span class="nv">__indexOf: </span><span class="nb">Array</span><span class="o">::</span><span class="nx">indexOf</span> <span class="o">or</span> <span class="nf">(item) -></span> | ||
| </pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>List of reserved variable names.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@reserved = </span><span class="p">[</span><span class="s1">'__expr'</span><span class="p">]</span> | ||
| </pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Create a getters / setters for the given name.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@makeGetter = </span><span class="nf">(name) -></span> | ||
| <span class="o">-></span> <span class="nx">@_eval</span> <span class="nx">name</span> | ||
| <span class="vi">@makeSetter = </span><span class="nf">(name) -></span> | ||
| <span class="nf">(val) -></span> <span class="nx">@_eval</span><span class="p">.</span><span class="nx">call</span> <span class="p">{</span><span class="nx">val</span><span class="p">},</span> <span class="s2">"#{name} = this.val"</span> | ||
| </pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Create a getters / setters for the given name.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@makeGetter = </span><span class="nf">(name) -></span> <span class="o">-></span> <span class="nx">@get</span> <span class="nx">name</span> | ||
| <span class="vi">@makeSetter = </span><span class="nf">(name) -></span> <span class="nf">(val) -></span> <span class="nx">@set</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="kc">false</span> | ||
| </pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <h4>Class initializer</h4> | ||
@@ -57,3 +55,3 @@ | ||
| All scopes may extend this scope. | ||
| It contains the rootValues.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@root = </span><span class="nx">@global</span><span class="p">.</span><span class="nx">extend</span> <span class="nv">values: </span><span class="nx">@rootValues</span> | ||
| It contains the rootLocals.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@root = </span><span class="nx">@global</span><span class="p">.</span><span class="nx">extend</span> <span class="nv">locals: </span><span class="nx">@rootLocals</span> | ||
| </pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <h4>Scope.create(options)</h4> | ||
@@ -64,3 +62,3 @@ | ||
| <p>See constructor for description of options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@create = </span><span class="nf">(options) -></span> <span class="nx">@root</span><span class="p">.</span><span class="nx">extend</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <h4>constructor(options)</h4> | ||
| <p>See <code>constructor</code> for description of options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@create = </span><span class="nf">(options) -></span> <span class="nx">@root</span><span class="p">.</span><span class="nx">extend</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <h4>constructor(options)</h4> | ||
@@ -70,6 +68,6 @@ <p>Called with no arguments: creates a global scope.</p> | ||
| <p>There are two ways to define | ||
| local variables in the new scope: using <em>values</em> | ||
| local variables in the new scope: using <em>locals</em> | ||
| and <em>literals</em>.</p> | ||
| <h5><em>param</em>: <code>options.values</code></h5> | ||
| <h5><em>param</em>: <code>options.locals</code></h5> | ||
@@ -82,3 +80,3 @@ <p>Describes local variable names and values to be declared and set | ||
| var scope = Scope.create({ | ||
| values: {foo: foo} | ||
| locals: {foo: foo} | ||
| }); | ||
@@ -95,3 +93,3 @@ | ||
| <pre><code>scope = Scope.create({ | ||
| values: {foo: 3}, | ||
| locals: {foo: 3}, | ||
| literals: { | ||
@@ -103,18 +101,18 @@ getFoo: "function(){return foo}" | ||
| log(scope.eval('foo')); // 3 | ||
| </code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(@options = {}) -></span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>The types of variables in options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">varTypes = </span><span class="p">[</span><span class="s1">'values'</span><span class="p">,</span> <span class="s1">'literals'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Normalize options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@options</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">?=</span> <span class="p">{}</span> <span class="k">for</span> <span class="nx">k</span> <span class="k">in</span> <span class="nx">varTypes</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <h5>The <code>__scope</code> local variable</h5> | ||
| </code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(@options = {}) -></span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>The types of variables in options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">varTypes = </span><span class="p">[</span><span class="s1">'locals'</span><span class="p">,</span> <span class="s1">'literals'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Normalize options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@options</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">?=</span> <span class="p">{}</span> <span class="k">for</span> <span class="nx">k</span> <span class="k">in</span> <span class="nx">varTypes</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <h5>The <code>__scope</code> local variable</h5> | ||
| <p>Within the target scope, <code>__scope</code> refers to the Scope object for that | ||
| target scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@options.values.__scope = </span><span class="err">@</span> | ||
| target scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@options.locals.__scope = </span><span class="err">@</span> | ||
| <span class="p">{</span><span class="nx">@parent</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@options</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <p>Register variable names declared in options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">names = </span><span class="p">[]</span> | ||
| <span class="nx">names</span><span class="p">.</span><span class="nx">push</span> <span class="nx">name</span> <span class="k">for</span> <span class="nx">name</span> <span class="k">of</span> <span class="nx">@options</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="k">for</span> <span class="nx">k</span> <span class="k">in</span> <span class="nx">varTypes</span> | ||
| <span class="k">throw</span> <span class="s1">'Reserved'</span> <span class="k">for</span> <span class="nx">n</span> <span class="k">in</span> <span class="nx">names</span> <span class="k">when</span> <span class="nx">n</span> <span class="k">in</span> <span class="nx">@constructor</span><span class="p">.</span><span class="nx">reserved</span> | ||
| <span class="vi">@names = </span><span class="nx">names</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">@parent</span><span class="o">?</span><span class="p">.</span><span class="nx">names</span> <span class="o">or</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <p>Create this scope's 'scoped eval'</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@_eval = </span> | ||
| <span class="vi">@names = </span><span class="nx">names</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">@parent</span><span class="o">?</span><span class="p">.</span><span class="nx">names</span> <span class="o">or</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <p>Compile the 'scoped eval'</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@_eval = </span> | ||
| <span class="nx">unless</span> <span class="nx">@parent</span><span class="o">?</span> <span class="k">then</span> <span class="nx">Scope</span><span class="p">.</span><span class="nb">eval</span> | ||
| <span class="k">else</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>Concatenate and assign all local literals</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">literals = </span><span class="p">(</span><span class="k">for</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">@options</span><span class="p">.</span><span class="nx">literals</span> | ||
| <span class="s2">"var #{name} = #{literalize val};\n"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>Eval <code>expr</code> in the parent scope with local values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@parent</span><span class="p">.</span><span class="nb">eval</span> <span class="p">{</span><span class="nv">locals: </span><span class="nx">@options</span><span class="p">.</span><span class="nx">values</span><span class="p">},</span> <span class="nx">literals</span> <span class="o">+</span> <span class="nx">EVAL_LITERAL</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <h5>Exports</h5> | ||
| <span class="s2">"var #{name} = #{literalize val};\n"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>Eval <code>literals</code> in the parent scope with <code>@options.locals</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@parent</span><span class="p">.</span><span class="nb">eval</span> <span class="nx">@options</span><span class="p">,</span> <span class="nx">literals</span> <span class="o">+</span> <span class="nx">EVAL_LITERAL</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <h5>Exports</h5> | ||
| <p>Exports allow direct access to values inside the scope via | ||
| <p>Exports allow direct access to locals inside the scope via | ||
| getters and setters (where support exists):</p> | ||
| <pre><code>var scope = Scope.create({values: {foo: 0}}); | ||
| <pre><code>var scope = Scope.create({locals: {foo: 0}}); | ||
@@ -147,3 +145,3 @@ // Set a local variable inside the target scope (setters are by reference). | ||
| <p><code>ctx.locals</code> may define additional local values visible only to <code>expr</code>.</p> | ||
| <p><code>ctx.locals</code> may define additional locals visible only to <code>expr</code>.</p> | ||
@@ -153,7 +151,8 @@ <h5>Argument Decompilation</h5> | ||
| <p>The <code>expr</code> argument need not be a string: it can also be a function or | ||
| an object that defines a <code>literal</code> method. See <code>Scope.literalize</code>. | ||
| an object that defines a <code>literalize</code> method. See <code>Scope.literalize</code>. | ||
| This means that you can recompile a function in another scope like this:</p> | ||
| <p>var getXFromScope = scope.eval(function(){return x}); | ||
| log(getXFromScope()); // Prints the current value of x in the scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">eval</span><span class="o">:</span> <span class="nf">(ctx, expr) -></span> | ||
| <pre><code>var getXFromScope = scope.eval(function(){return x}); | ||
| log(getXFromScope()); // Prints the current value of x in the scope. | ||
| </code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">eval</span><span class="o">:</span> <span class="nf">(ctx, expr) -></span> | ||
| <span class="p">[</span><span class="nx">ctx</span><span class="p">,</span> <span class="nx">expr</span><span class="p">]</span> <span class="o">=</span> <span class="p">[{},</span> <span class="nx">ctx</span><span class="p">]</span> <span class="nx">unless</span> <span class="nx">expr</span> | ||
@@ -171,11 +170,25 @@ <span class="nv">locals = </span> | ||
| <p>log(scope.run(function(){return x})); // Prints the current value of x in the scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">run: </span><span class="nf">(ctx, fn) -></span> | ||
| <pre><code>log(scope.run(function(){return x})); // Prints the current value of x in the scope. | ||
| </code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">run: </span><span class="nf">(ctx, fn) -></span> | ||
| <span class="p">[</span><span class="nx">ctx</span><span class="p">,</span> <span class="nx">fn</span><span class="p">]</span> <span class="o">=</span> <span class="p">[{},</span> <span class="nx">ctx</span><span class="p">]</span> <span class="nx">unless</span> <span class="nx">fn</span> | ||
| <span class="nx">@eval</span> <span class="nx">ctx</span><span class="p">,</span> <span class="s2">"#{literalize fn}.call(this)"</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</a> </div> <h4>Scope::extend(options)</h4> | ||
| <span class="nx">@eval</span> <span class="nx">ctx</span><span class="p">,</span> <span class="s2">"#{literalize fn}.call(this)"</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</a> </div> <h4>Scope::set(name, val, isLiteral)</h4> | ||
| <p>Set local variable to value. | ||
| <code>if isLiteral then literalize val</code>.</p> | ||
| <p>To set multiple values: <code>Scope.set(obj, isLiteral)</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">set: </span><span class="nf">(name, val, isLiteral) -></span> | ||
| <span class="k">if</span> <span class="nx">isString</span> <span class="nx">name</span> <span class="k">then</span> <span class="nx">@_eval</span><span class="p">.</span><span class="nx">call</span> <span class="p">{</span><span class="nx">val</span><span class="p">},</span> <span class="s2">"#{name} = "</span> <span class="o">+</span> | ||
| <span class="p">(</span><span class="k">if</span> <span class="nx">isLiteral</span> <span class="k">then</span> <span class="nx">literalize</span> <span class="nx">val</span> <span class="k">else</span> <span class="s2">"this.val"</span><span class="p">)</span> | ||
| <span class="k">else</span> | ||
| <span class="p">[</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">isLiteral</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">name</span><span class="p">,</span> <span class="nx">val</span><span class="p">]</span> | ||
| <span class="nx">@set</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">isLiteral</span> <span class="k">for</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">val</span> <span class="k">in</span> <span class="nx">obj</span> | ||
| </pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</a> </div> <h4>Scope::get(name)</h4> | ||
| <p>Get a local value from this scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">get: </span><span class="nf">(name) -></span> <span class="nx">@_eval</span> <span class="nx">name</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">¶</a> </div> <h4>Scope::extend(options)</h4> | ||
| <p>Create a new scope that extends this one, with the given options. | ||
| See <code>constructor</code> for a list </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">extend: </span><span class="nf">(options = {}) -></span> <span class="k">new</span> <span class="nx">@constructor</span> <span class="nx">extend</span> <span class="nx">options</span><span class="p">,</span> <span class="nv">parent: </span><span class="err">@</span> | ||
| </pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</a> </div> <p>Initialize this class</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@initialize</span><span class="p">()</span> | ||
| See <code>constructor</code> for a list of options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">extend: </span><span class="nf">(options = {}) -></span> <span class="k">new</span> <span class="nx">@constructor</span> <span class="nx">extend</span> <span class="nx">options</span><span class="p">,</span> <span class="nv">parent: </span><span class="err">@</span> | ||
| </pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">¶</a> </div> <p>Initialize this class</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@initialize</span><span class="p">()</span> | ||
| </pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |
+34
-35
| # ScopeJS | ||
| A library for defining and working with lexical scopes. | ||
| _Pimp your Lexical Scopes._ | ||
| ## Contents | ||
| - [Installation and Useage](#useage) | ||
| A JavaScript library for defining and working with lexical scopes. | ||
| [ScopeJS is hosted on github](http://github.com/dbrans/scope). | ||
| ## Table of Contents | ||
| - [Installation and Usage](#usage) | ||
| - [Theory](#theory) | ||
@@ -15,11 +19,7 @@ - [API](#api) | ||
| var scope = Scope.create({ | ||
| values: { | ||
| foo: 3 | ||
| }, | ||
| literals: { | ||
| multiplyFoo: function(val) { | ||
| foo = foo * val; | ||
| } | ||
| } | ||
| }); | ||
| locals: {foo: 3}, | ||
| literals: { | ||
| multiplyFoo: function(val) {foo *= val;} | ||
| } | ||
| }); | ||
@@ -37,7 +37,5 @@ // Call a local function. | ||
| var inner = scope.extend({ | ||
| values: { | ||
| bar: 'bar' | ||
| } | ||
| }); | ||
| locals: {bar: 'bar'} | ||
| }); | ||
| // Inner scope has access to the outer scope: | ||
@@ -50,14 +48,14 @@ inner.foo = 6; | ||
| [Scopejs is hosted on github](http://github.com/dbrans/scopejs). | ||
| ~anchor:useage | ||
| ~anchor:usage | ||
| ## Installation | ||
| There are two ways to install scopejs: via npm or via git. | ||
| There are two ways to install ScopeJS: via npm or via git. | ||
| ### NPM | ||
| 1. If you haven't already, [Install npm](http://howtonode.org/introduction-to-npm). | ||
| 2. `npm install scopejs` | ||
| 1. If you haven't already, [install npm](http://howtonode.org/introduction-to-npm). | ||
| 2. `npm install -g scope` | ||
| (Leave off the -g if you don't wish to install globally.) | ||
| ### Git | ||
@@ -68,4 +66,4 @@ To install from source, you should have [CoffeeScript](http://coffeescript.org) installed. | ||
| git clone git://github.com/dbrans/scopejs.git | ||
| cd scopejs | ||
| git clone git://github.com/dbrans/scope.git | ||
| cd scope | ||
| cake install | ||
@@ -77,3 +75,3 @@ | ||
| var Scope = require('scopejs').Scope; | ||
| var Scope = require('scope').Scope; | ||
| var scope = Scope.create(...); | ||
@@ -84,3 +82,3 @@ | ||
| <script type="text/javascript" src="PATH_TO_SCOPEJS/browser/scope.js"></script> | ||
| <script type="text/javascript" src="PATH_TO_SCOPE/browser/scope.js"></script> | ||
@@ -93,3 +91,3 @@ `browser/scope.js` defines `Scope` as a global variable. So you can just go | ||
| ## Community | ||
| Please use the [github page for ScopeJS](http://github.com/dbrans/scopejs) | ||
| Please use the [github page for ScopeJS](http://github.com/dbrans/scope) | ||
| to discuss and raise issues about ScopeJS. | ||
@@ -183,3 +181,3 @@ | ||
| // Here newvar exists: | ||
| // Here 'newvar' exists: | ||
| log(scopeEval('var newvar = 3; newvar')); // 3 | ||
@@ -227,7 +225,4 @@ | ||
| ## API | ||
| I think we have covered enough concepts to make it easy to talk about what | ||
| ScopeJS does. | ||
| For API documentation, please refer to the [annotated source](documentation/docs/scope.html) | ||
| For API documentation, have a look at the [annotated source](documentation/docs/scope.html) | ||
| ~anchor:coffee | ||
@@ -245,3 +240,7 @@ ## CoffeeScript support | ||
| If you have a string of CoffeeScript code that you want to compile or run inside a target scope, | ||
| use `CoffeeScript.compile code, bare:true` to compile `code` first. 'Native' CoffeeScript eval/compile | ||
| use | ||
| CoffeeScript.compile code, bare:true | ||
| to compile `code` first. 'Native' CoffeeScript eval/compile | ||
| support could be envisioned for the future. | ||
@@ -259,3 +258,3 @@ | ||
| foo: 3 | ||
| compile: | ||
| literals: | ||
| # Won't have the desired effect. | ||
@@ -262,0 +261,0 @@ setFoo: (val) -> foo = val |
+34
-35
@@ -5,3 +5,3 @@ <!DOCTYPE html> | ||
| <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | ||
| <title>ScopeJS</title> | ||
| <title>ScopeJS: Pimp your lexical scopes.</title> | ||
| <link rel="stylesheet" type="text/css" href="documentation/css/doc.css" /> | ||
@@ -13,8 +13,12 @@ <link rel="shortcut icon" href="documentation/images/favicon.ico" /> | ||
| <p>A library for defining and working with lexical scopes.</p> | ||
| <p><em>Pimp your Lexical Scopes.</em></p> | ||
| <h2>Contents</h2> | ||
| <p>A JavaScript library for defining and working with lexical scopes.</p> | ||
| <p><a href="http://github.com/dbrans/scope">ScopeJS is hosted on github</a>.</p> | ||
| <h2>Table of Contents</h2> | ||
| <ul> | ||
| <li><a href="#useage">Installation and Useage</a></li> | ||
| <li><a href="#usage">Installation and Usage</a></li> | ||
| <li><a href="#theory">Theory</a></li> | ||
@@ -30,11 +34,7 @@ <li><a href="#api">API</a></li> | ||
| var scope = Scope.create({ | ||
| values: { | ||
| foo: 3 | ||
| }, | ||
| literals: { | ||
| multiplyFoo: function(val) { | ||
| foo = foo * val; | ||
| } | ||
| } | ||
| }); | ||
| locals: {foo: 3}, | ||
| literals: { | ||
| multiplyFoo: function(val) {foo *= val;} | ||
| } | ||
| }); | ||
@@ -52,6 +52,4 @@ // Call a local function. | ||
| var inner = scope.extend({ | ||
| values: { | ||
| bar: 'bar' | ||
| } | ||
| }); | ||
| locals: {bar: 'bar'} | ||
| }); | ||
@@ -66,9 +64,7 @@ // Inner scope has access to the outer scope: | ||
| <p><a href="http://github.com/dbrans/scopejs">Scopejs is hosted on github</a>.</p> | ||
| <p><div id="usage"></div></p> | ||
| <p><div id="useage"></div></p> | ||
| <h2>Installation</h2> | ||
| <p>There are two ways to install scopejs: via npm or via git.</p> | ||
| <p>There are two ways to install ScopeJS: via npm or via git.</p> | ||
@@ -78,4 +74,5 @@ <h3>NPM</h3> | ||
| <ol> | ||
| <li>If you haven't already, <a href="http://howtonode.org/introduction-to-npm">Install npm</a>. </li> | ||
| <li><code>npm install scopejs</code></li> | ||
| <li>If you haven't already, <a href="http://howtonode.org/introduction-to-npm">install npm</a>. </li> | ||
| <li><code>npm install -g scope</code> | ||
| (Leave off the -g if you don't wish to install globally.)</li> | ||
| </ol> | ||
@@ -89,4 +86,4 @@ | ||
| <pre><code>git clone git://github.com/dbrans/scopejs.git | ||
| cd scopejs | ||
| <pre><code>git clone git://github.com/dbrans/scope.git | ||
| cd scope | ||
| cake install | ||
@@ -99,3 +96,3 @@ </code></pre> | ||
| <pre><code>var Scope = require('scopejs').Scope; | ||
| <pre><code>var Scope = require('scope').Scope; | ||
| var scope = Scope.create(...); | ||
@@ -108,3 +105,3 @@ </code></pre> | ||
| <pre><code><script type="text/javascript" src="PATH_TO_SCOPEJS/browser/scope.js"></script> | ||
| <pre><code><script type="text/javascript" src="PATH_TO_SCOPE/browser/scope.js"></script> | ||
| </code></pre> | ||
@@ -120,3 +117,3 @@ | ||
| <p>Please use the <a href="http://github.com/dbrans/scopejs">github page for ScopeJS</a> | ||
| <p>Please use the <a href="http://github.com/dbrans/scope">github page for ScopeJS</a> | ||
| to discuss and raise issues about ScopeJS.</p> | ||
@@ -222,3 +219,3 @@ | ||
| <pre><code>// Here newvar exists: | ||
| <pre><code>// Here 'newvar' exists: | ||
| log(scopeEval('var newvar = 3; newvar')); // 3 | ||
@@ -271,7 +268,4 @@ | ||
| <p>I think we have covered enough concepts to make it easy to talk about what | ||
| ScopeJS does.</p> | ||
| <p>For API documentation, please refer to the <a href="documentation/docs/scope.html">annotated source</a></p> | ||
| <p>For API documentation, have a look at the <a href="documentation/docs/scope.html">annotated source</a></p> | ||
| <p><div id="coffee"></div></p> | ||
@@ -292,3 +286,8 @@ | ||
| <p>If you have a string of CoffeeScript code that you want to compile or run inside a target scope, | ||
| use <code>CoffeeScript.compile code, bare:true</code> to compile <code>code</code> first. 'Native' CoffeeScript eval/compile | ||
| use </p> | ||
| <pre><code>CoffeeScript.compile code, bare:true | ||
| </code></pre> | ||
| <p>to compile <code>code</code> first. 'Native' CoffeeScript eval/compile | ||
| support could be envisioned for the future.</p> | ||
@@ -306,3 +305,3 @@ | ||
| foo: 3 | ||
| compile: | ||
| literals: | ||
| # Won't have the desired effect. | ||
@@ -309,0 +308,0 @@ setFoo: (val) -> foo = val |
+27
-12
@@ -46,6 +46,6 @@ (function() { | ||
| } else { | ||
| return x.literal(); | ||
| return x.literalize(); | ||
| } | ||
| }; | ||
| Scope.rootValues = { | ||
| Scope.rootLocals = { | ||
| "__slice": Array.prototype.slice, | ||
@@ -93,3 +93,3 @@ "__bind": function(fn, me) { | ||
| return function() { | ||
| return this._eval(name); | ||
| return this.get(name); | ||
| }; | ||
@@ -99,5 +99,3 @@ }; | ||
| return function(val) { | ||
| return this._eval.call({ | ||
| val: val | ||
| }, "" + name + " = this.val"); | ||
| return this.set(name, val, false); | ||
| }; | ||
@@ -108,3 +106,3 @@ }; | ||
| return this.root = this.global.extend({ | ||
| values: this.rootValues | ||
| locals: this.rootLocals | ||
| }); | ||
@@ -118,3 +116,3 @@ }; | ||
| this.options = options != null ? options : {}; | ||
| varTypes = ['values', 'literals']; | ||
| varTypes = ['locals', 'literals']; | ||
| for (_i = 0, _len = varTypes.length; _i < _len; _i++) { | ||
@@ -126,3 +124,3 @@ k = varTypes[_i]; | ||
| } | ||
| this.options.values.__scope = this; | ||
| this.options.locals.__scope = this; | ||
| this.parent = this.options.parent; | ||
@@ -152,5 +150,3 @@ names = []; | ||
| return _results; | ||
| }).call(this)).join(''), this.parent.eval({ | ||
| locals: this.options.values | ||
| }, literals + EVAL_LITERAL)); | ||
| }).call(this)).join(''), this.parent.eval(this.options, literals + EVAL_LITERAL)); | ||
| exports = (function() { | ||
@@ -210,2 +206,21 @@ var _l, _len4, _ref3, _results; | ||
| }; | ||
| Scope.prototype.set = function(name, val, isLiteral) { | ||
| var obj, _len, _ref, _results; | ||
| if (isString(name)) { | ||
| return this._eval.call({ | ||
| val: val | ||
| }, ("" + name + " = ") + (isLiteral ? literalize(val) : "this.val")); | ||
| } else { | ||
| _ref = [name, val], obj = _ref[0], isLiteral = _ref[1]; | ||
| _results = []; | ||
| for (val = 0, _len = obj.length; val < _len; val++) { | ||
| name = obj[val]; | ||
| _results.push(this.set(name, val, isLiteral)); | ||
| } | ||
| return _results; | ||
| } | ||
| }; | ||
| Scope.prototype.get = function(name) { | ||
| return this._eval(name); | ||
| }; | ||
| Scope.prototype.extend = function(options) { | ||
@@ -212,0 +227,0 @@ if (options == null) { |
+8
-6
| { | ||
| "name": "scope", | ||
| "description": "A library for defining and working with lexical scopes in JavaScript.", | ||
| "keywords": ["coffeescript", "javascript", "DSL", "scope"], | ||
| "description": "Define and work with lexical scopes.", | ||
| "keywords": ["scope", "lexical", "DSL", "coffeescript"], | ||
| "author": "Derek Brans <dbrans@gmail.com>", | ||
| "version": "0.8.0", | ||
| "version": "0.8.1", | ||
| "homepage": "http://scopejs.org", | ||
| "bugs": { "web" : "github.com/dbrans/scope/issues"}, | ||
| "licenses": [{ | ||
| "type": "MIT", | ||
| "url": "http://github.com/dbrans/scopejs/raw/master/LICENSE" | ||
| "url": "github.com/dbrans/scope/raw/master/LICENSE" | ||
| }], | ||
@@ -14,3 +16,3 @@ "engines": { | ||
| }, | ||
| "scripts": { "test": "cake test" }, | ||
| "main": "./index", | ||
| "directories" : { | ||
@@ -21,4 +23,4 @@ "lib" : "./lib" | ||
| "type": "git", | ||
| "url": "git://github.com/dbrans/scopejs.git" | ||
| "url": "git://github.com/dbrans/scope.git" | ||
| } | ||
| } |
+44
-30
@@ -34,3 +34,3 @@ # File header. | ||
| # in the global scope. | ||
| # | ||
| # | ||
| # `Scope.eval(expr)` has these properties: | ||
@@ -43,3 +43,3 @@ # | ||
| # | ||
| # `Scope.eval` provides the outer scope for all scopes in Scopejs. | ||
| # `Scope.eval` provides the outer scope for all scoped evals. | ||
| @eval = `(1, eval)(EVAL_LITERAL)` | ||
@@ -50,3 +50,3 @@ | ||
| @literalize = literalize = (x) -> | ||
| # A string is a literal expression of some other value. | ||
| # A string must already be an expression. | ||
| if isString x then x | ||
@@ -60,8 +60,8 @@ else if isFn x | ||
| # Otherwise, `x` must literalize itself. | ||
| else x.literal() | ||
| else x.literalize() | ||
| # #### Class properties intended to be overridden. | ||
| # These values are defined in the root scope. | ||
| @rootValues = | ||
| # These locals are defined in the root scope. | ||
| @rootLocals = | ||
| # CoffeeScript runtime helpers | ||
@@ -86,6 +86,4 @@ __slice: Array::slice | ||
| # Create a getters / setters for the given name. | ||
| @makeGetter = (name) -> | ||
| -> @_eval name | ||
| @makeSetter = (name) -> | ||
| (val) -> @_eval.call {val}, "#{name} = this.val" | ||
| @makeGetter = (name) -> -> @get name | ||
| @makeSetter = (name) -> (val) -> @set name, val, false | ||
@@ -102,4 +100,4 @@ # #### Class initializer | ||
| # All scopes may extend this scope. | ||
| # It contains the rootValues. | ||
| @root = @global.extend values: @rootValues | ||
| # It contains the rootLocals. | ||
| @root = @global.extend locals: @rootLocals | ||
@@ -110,3 +108,3 @@ # #### Scope.create(options) | ||
| # | ||
| # See constructor for description of options. | ||
| # See `constructor` for description of options. | ||
| @create = (options) -> @root.extend options | ||
@@ -118,6 +116,6 @@ | ||
| # There are two ways to define | ||
| # local variables in the new scope: using _values_ | ||
| # local variables in the new scope: using _locals_ | ||
| # and _literals_. | ||
| # | ||
| # ##### *param*: `options.values` | ||
| # ##### *param*: `options.locals` | ||
| # Describes local variable names and values to be declared and set | ||
@@ -129,3 +127,3 @@ # in the target scope. | ||
| # var scope = Scope.create({ | ||
| # values: {foo: foo} | ||
| # locals: {foo: foo} | ||
| # }); | ||
@@ -140,3 +138,3 @@ # | ||
| # scope = Scope.create({ | ||
| # values: {foo: 3}, | ||
| # locals: {foo: 3}, | ||
| # literals: { | ||
@@ -150,3 +148,3 @@ # getFoo: "function(){return foo}" | ||
| # The types of variables in options. | ||
| varTypes = ['values', 'literals'] | ||
| varTypes = ['locals', 'literals'] | ||
| # Normalize options. | ||
@@ -157,3 +155,3 @@ @options[k] ?= {} for k in varTypes | ||
| # target scope. | ||
| @options.values.__scope = @ | ||
| @options.locals.__scope = @ | ||
| {@parent} = @options | ||
@@ -165,3 +163,3 @@ # Register variable names declared in options. | ||
| @names = names.concat @parent?.names or [] | ||
| # Create this scope's 'scoped eval' | ||
| # Compile the 'scoped eval' | ||
| @_eval = | ||
@@ -173,9 +171,9 @@ unless @parent? then Scope.eval | ||
| "var #{name} = #{literalize val};\n").join('') | ||
| # Eval `expr` in the parent scope with local values. | ||
| @parent.eval {locals: @options.values}, literals + EVAL_LITERAL | ||
| # Eval `literals` in the parent scope with `@options.locals`. | ||
| @parent.eval @options, literals + EVAL_LITERAL | ||
| # #####Exports | ||
| # Exports allow direct access to values inside the scope via | ||
| # Exports allow direct access to locals inside the scope via | ||
| # getters and setters (where support exists): | ||
| # | ||
| # var scope = Scope.create({values: {foo: 0}}); | ||
| # var scope = Scope.create({locals: {foo: 0}}); | ||
| # | ||
@@ -210,11 +208,11 @@ # // Set a local variable inside the target scope (setters are by reference). | ||
| # | ||
| # `ctx.locals` may define additional local values visible only to `expr`. | ||
| # `ctx.locals` may define additional locals visible only to `expr`. | ||
| # | ||
| # ##### Argument Decompilation | ||
| # The `expr` argument need not be a string: it can also be a function or | ||
| # an object that defines a `literal` method. See `Scope.literalize`. | ||
| # an object that defines a `literalize` method. See `Scope.literalize`. | ||
| # This means that you can recompile a function in another scope like this: | ||
| # | ||
| # var getXFromScope = scope.eval(function(){return x}); | ||
| # log(getXFromScope()); // Prints the current value of x in the scope. | ||
| # var getXFromScope = scope.eval(function(){return x}); | ||
| # log(getXFromScope()); // Prints the current value of x in the scope. | ||
| # | ||
@@ -234,3 +232,3 @@ eval: (ctx, expr) -> | ||
| # | ||
| # log(scope.run(function(){return x})); // Prints the current value of x in the scope. | ||
| # log(scope.run(function(){return x})); // Prints the current value of x in the scope. | ||
| # | ||
@@ -241,5 +239,21 @@ run: (ctx, fn) -> | ||
| # #### Scope::set(name, val, isLiteral) | ||
| # Set local variable to value. | ||
| # `if isLiteral then literalize val`. | ||
| # | ||
| # To set multiple values: `Scope.set(obj, isLiteral)` | ||
| set: (name, val, isLiteral) -> | ||
| if isString name then @_eval.call {val}, "#{name} = " + | ||
| (if isLiteral then literalize val else "this.val") | ||
| else | ||
| [obj, isLiteral] = [name, val] | ||
| @set name, val, isLiteral for name, val in obj | ||
| # #### Scope::get(name) | ||
| # Get a local value from this scope. | ||
| get: (name) -> @_eval name | ||
| # #### Scope::extend(options) | ||
| # Create a new scope that extends this one, with the given options. | ||
| # See `constructor` for a list | ||
| # See `constructor` for a list of options. | ||
| extend: (options = {}) -> new @constructor extend options, parent: @ | ||
@@ -246,0 +260,0 @@ |
+3
-3
@@ -17,3 +17,3 @@ <!DOCTYPE html> | ||
| scope = Scope.create({ | ||
| values: { | ||
| locals: { | ||
| foo: foo | ||
@@ -29,3 +29,3 @@ }, | ||
| scope = Scope.create({ | ||
| values: { | ||
| locals: { | ||
| foo: foo | ||
@@ -42,3 +42,3 @@ } | ||
| scope = Scope.create({ | ||
| values: { | ||
| locals: { | ||
| foo: foo | ||
@@ -45,0 +45,0 @@ } |
+17
-17
@@ -7,3 +7,3 @@ # Tests for scopejs | ||
| test 'Get a value from scope', -> | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
| eq scope.eval('foo'), foo | ||
@@ -13,3 +13,3 @@ | ||
| newfoo = 4 | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
@@ -26,3 +26,3 @@ scope.eval "foo = #{newfoo}" | ||
| newfoo = -> bar | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
@@ -38,3 +38,3 @@ scope.eval {newfoo}, "foo = this.newfoo" | ||
| test 'Get a value from scope - function version', -> | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
| eq foo, scope.run -> foo | ||
@@ -44,3 +44,3 @@ | ||
| newfoo = 4 | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
| # CoffeeScript, careful with assignments! | ||
@@ -57,3 +57,3 @@ # foo is already declared here. | ||
| newfoo = -> bar | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
@@ -68,5 +68,5 @@ scope.run {newfoo}, -> foo = @newfoo | ||
| test 'values and closures', -> | ||
| test 'locals and closures', -> | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| literals: | ||
@@ -78,3 +78,3 @@ f: -> foo | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| literals: | ||
@@ -87,3 +87,3 @@ f: -> @foo | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| literals: {f} | ||
@@ -96,3 +96,3 @@ eq scope.eval('f')(), foo | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| eq foo, scope.run f | ||
@@ -103,3 +103,3 @@ | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| literals: | ||
@@ -112,3 +112,3 @@ f: "function () { return foo;}" | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| literals: | ||
@@ -120,3 +120,3 @@ f: "function named() { return foo;}" | ||
| scope = Scope.create | ||
| values:{foo} | ||
| locals:{foo} | ||
| literals: | ||
@@ -128,3 +128,3 @@ f: (CoffeeScript.compile "-> foo", bare: true) | ||
| test 'coffeescript helpers', -> | ||
| scope = Scope.create values: {foo} | ||
| scope = Scope.create locals: {foo} | ||
| eq foo, scope.run -> | ||
@@ -139,3 +139,3 @@ class FooClass | ||
| scope = Scope.create | ||
| values: {foo} | ||
| locals: {foo} | ||
@@ -161,3 +161,3 @@ # get | ||
| okThrows -> | ||
| Scope.create values: __eval: null | ||
| Scope.create locals: __eval: null | ||
@@ -164,0 +164,0 @@ test 'Set undeclared name throws', -> |
| .git_fubar |
Sorry, the diff of this file is not supported yet
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
129964
2.11%1749
-3.69%