Skip to content

Commit

Permalink
renderer: put var declarations at right places
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Sep 27, 2012
1 parent 30f1654 commit 75636c2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/spoon/block.js
Expand Up @@ -24,6 +24,7 @@ function Block(cfg) {
this.instructions = [];
this.root = null;
this.parentRoot = null;
this.fn = cfg.currentRoot;
this.loop = false;
this.exits = null;

Expand Down
8 changes: 5 additions & 3 deletions lib/spoon/cfg.js
Expand Up @@ -7,12 +7,12 @@ function Cfg() {
this.blockId = 0;

this.root = null;
this.currentRoot = null;
this.exits = null;
this.blocks = [];
this.roots = [];
this.rootQueue = [];
this.current = null;
this.currentRoot = null;
this.asyncifyState = null;

this.breakInfo = null;
Expand Down Expand Up @@ -78,9 +78,11 @@ Cfg.prototype.translate = function translate(ast) {
var root = this.rootQueue.shift(),
block = this.createBlock();

if (!this.root) this.root = block;
if (!this.root) {
this.root = block;
}
this.currentRoot = block;

block.fn = block;
this.exits = block.exits = [];

this.roots.push(block);
Expand Down
2 changes: 2 additions & 0 deletions lib/spoon/instruction.js
Expand Up @@ -3,6 +3,7 @@ var instruction = exports,

function Instruction(block, type, args) {
this.block = block;
this.fn = block.fn;
this.cfg = block.cfg;
this.id = block.cfg.instructionId++;
this.until = null;
Expand All @@ -16,6 +17,7 @@ function Instruction(block, type, args) {
if (!(arg instanceof Instruction)) return;
arg.uses.push(this);
}, this);

};
instruction.Instruction = Instruction;
instruction.create = function create(block, type, args) {
Expand Down
24 changes: 22 additions & 2 deletions lib/spoon/renderer.js
Expand Up @@ -9,6 +9,7 @@ function Renderer(cfg) {
// Queue of blocks to visit
this.queue = null;
this.slots = null;
this.fns = null;
this.defaultSlots = null;

// Track block visits to perfrom preorder traversal
Expand Down Expand Up @@ -71,6 +72,7 @@ Renderer.prototype.render = function render() {

this.queue = [ this.cfg.root ];
this.slots = {};
this.fns = {};
this.defaultSlots = [ null, result[1] ];

while (this.queue.length > 0) {
Expand Down Expand Up @@ -208,10 +210,27 @@ Renderer.prototype.renderInstruction = function renderInstruction(instr) {
}

var ast = fn.call(this, args, instr);
if (external) {
if (!instr.fn || instr.fn.id === 0) {
if (ast === null) {
ast = ['var', [[name[1]]]];
} else {
ast = ['var', [[name[1], ast]]];
}
} else {
if (ast !== null) {
// Wrap instructions with external use into variable declaration.
// Insert declaration on the level accessible for both instruction
// and it's every use.
ast = ['assign', true, name, ast];
}

var decl = ['var', [[name[1]]]];
this.fns[instr.fn.id].unshift(decl);
}
}
this.instructions[instr.id] = ast;

// Wrap instructions with external use into variable declaration
if (external) ast = ['var', [[name[1], ast]]];
return ast;
};

Expand Down Expand Up @@ -266,6 +285,7 @@ Renderer.prototype.renderFn = function renderFn(args, instr) {

this.queue.unshift(args[0]);
this.defaultSlots.push(slot);
this.fns[instr.args[0].id] = slot;

return [prefix, name, inputs, slot];
};
Expand Down

0 comments on commit 75636c2

Please sign in to comment.