Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

readme: docs

  • Loading branch information...
commit 69380daa017fbf00671fde968a5fcd3182f49a6b 1 parent e4e3484
Fedor Indutny authored October 01, 2012
94  README.md
Source Rendered
@@ -2,6 +2,100 @@
2 2
 
3 3
 Continuation passing style for javascript.
4 4
 
  5
+## It's hot
  6
+
  7
+Spoon is a javascript *transpiler*, it's designed to compile javascript to...
  8
+javascript.
  9
+
  10
+Lets take following code sample:
  11
+```javascript
  12
+var x = 1;
  13
+
  14
+var y = hardMath(x) * 2;
  15
+
  16
+console.log(y);
  17
+```
  18
+
  19
+Suppose you would like to do `hardMath` on some remote machine (or in thread),
  20
+this will surely require you to make this call asynchronous - i.e. pass a
  21
+continuation as a callback for `hardMath` function.
  22
+
  23
+Calling `spoon(code, ['hardMath'])` will find all occurences of `hardMath` and
  24
+replace them with following thing:
  25
+```javascript
  26
+function __$fn1(__$r) {
  27
+    y = __$r * 2;
  28
+    console.log(y);
  29
+    return __$callback.call(this);
  30
+};
  31
+
  32
+var y;
  33
+
  34
+var x;
  35
+
  36
+x = 1;
  37
+
  38
+return hardMath(x, __$fn1);
  39
+```
  40
+
  41
+As you can see `__$callback` function should be available in a context.
  42
+
  43
+## Why spoon is interesting?
  44
+
  45
+Spoon isn't just doing stupid tricks with your code, it compiles javascript to
  46
+well-known form (used by almost every compiler, including v8) HIR (High-Level
  47
+intermediate representation).
  48
+
  49
+You can do it yourself by calling:
  50
+```javascript
  51
+var cfg = spoon.construct(esprima.parse('x = y + 1 * 2'));
  52
+
  53
+console.log(cfg.toString());
  54
+```
  55
+
  56
+Will produce:
  57
+```
  58
+--- CFG ---
  59
+[block 0]
  60
+# predecessors: 
  61
+# parent: null
  62
+# frontier: 
  63
+# cfrontier: 
  64
+i0 = var y
  65
+i1 = get x
  66
+i2 = literal 1
  67
+i3 = literal 2
  68
+i4 = binop *, i2, i3
  69
+i5 = binop +, i1, i4
  70
+i6 = set =, y, i5
  71
+# successors: 
  72
+# children: 
  73
+```
  74
+
  75
+You can manipulate blocks, change order of instruction, do some optimizations,
  76
+and, after that, compile CFG back to JS:
  77
+```javascript
  78
+spoon.render(cfg);
  79
+```
  80
+
  81
+## API
  82
+
  83
+```javascript
  84
+// Transpile code
  85
+spoon(code, [ functions ], {
  86
+  declaration: 'enable spoon', // spoon will touch only code with
  87
+                               // "enable spoon"; declaration
  88
+  uglify: {},                  // uglifyjs options (used for code generation)
  89
+  esprima: {}                  // esprima options (used for parsing)
  90
+})
  91
+
  92
+// Construct CFG
  93
+var cfg = spoon.construct(EsprimaAST);
  94
+
  95
+// Render CFG
  96
+spoon.render(cfg);
  97
+```
  98
+
5 99
 ### License
6 100
 
7 101
 This software is licensed under the MIT License.
8  lib/spoon/cfg.js
@@ -188,9 +188,11 @@ Cfg.prototype.asyncify = function asyncify(asts, options) {
188 188
     if (!hasDeclaration[block.id]) {
189 189
       // Find declaration first
190 190
       var found = block.instructions.some(function(instr) {
191  
-        if (instr.type !== 'literal' ||
192  
-            instr.args[0] !== (options.declaration || 'enable spoon') ||
193  
-            instr.uses.length > 0) {
  191
+        // No declaration? - All blocks counts
  192
+        if (options.declaration &&
  193
+            (instr.type !== 'literal' ||
  194
+            instr.args[0] !== options.declaration ||
  195
+            instr.uses.length > 0)) {
194 196
           return false;
195 197
         }
196 198
 
4  test/asyncify-test.js
@@ -9,7 +9,9 @@ describe('Spoon', function() {
9 9
     var ast = esprima.parse(code.toString()),
10 10
         cfg = spoon.construct(ast);
11 11
 
12  
-    cfg.asyncify([esprima.parse(what || 'async')], {});
  12
+    cfg.asyncify([esprima.parse(what || 'async')], {
  13
+      declaration: 'enable spoon'
  14
+    });
13 15
 
14 16
     var out = spoon.render(cfg);
15 17
     var code = uglify.uglify.gen_code(out, { beautify: true });

0 notes on commit 69380da

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