import Exception from "../exception"; import {isArray} from "../utils"; import AST from "./ast"; var slice = [].slice; export function Compiler() {} // the foundHelper register will disambiguate helper lookup from finding a // function in a context. This is necessary for mustache compatibility, which // requires that context functions in blocks are evaluated by blockHelperMissing, // and then proceed as if the resulting value was provided to blockHelperMissing. Compiler.prototype = { compiler: Compiler, equals: function(other) { var len = this.opcodes.length; if (other.opcodes.length !== len) { return false; } for (var i = 0; i < len; i++) { var opcode = this.opcodes[i], otherOpcode = other.opcodes[i]; if (opcode.opcode !== otherOpcode.opcode || !argEquals(opcode.args, otherOpcode.args)) { return false; } } // We know that length is the same between the two arrays because they are directly tied // to the opcode behavior above. len = this.children.length; for (i = 0; i < len; i++) { if (!this.children[i].equals(other.children[i])) { return false; } } return true; }, guid: 0, compile: function(program, options) { this.sourceNode = []; this.opcodes = []; this.children = []; this.options = options; this.stringParams = options.stringParams; this.trackIds = options.trackIds; options.blockParams = options.blockParams || []; // These changes will propagate to the other compiler components var knownHelpers = options.knownHelpers; options.knownHelpers = { 'helperMissing': true, 'blockHelperMissing': true, 'each': true, 'if': true, 'unless': true, 'with': true, 'log': true, 'lookup': true }; if (knownHelpers) { for (var name in knownHelpers) { options.knownHelpers[name] = knownHelpers[name]; } } return this.accept(program); }, compileProgram: function(program) { var result = new this.compiler().compile(program, this.options); var guid = this.guid++; this.usePartial = this.usePartial || result.usePartial; this.children[guid] = result; this.useDepths = this.useDepths || result.useDepths; return guid; }, accept: function(node) { this.sourceNode.unshift(node); var ret = this[node.type](node); this.sourceNode.shift(); return ret; }, Program: function(program) { this.options.blockParams.unshift(program.blockParams); var body = program.body; for(var i=0, l=body.length; i 1) { throw new Exception('Unsupported number of partial arguments: ' + params.length, partial); } else if (!params.length) { params.push({type: 'PathExpression', parts: [], depth: 0}); } this.setupFullMustacheParams(partial.sexpr, undefined, undefined, true); var indent = partial.indent || ''; if (this.options.preventIndent && indent) { this.opcode('appendContent', indent); indent = ''; } this.opcode('invokePartial', partialName, indent); this.opcode('append'); }, MustacheStatement: function(mustache) { this.accept(mustache.sexpr); if(mustache.escaped && !this.options.noEscape) { this.opcode('appendEscaped'); } else { this.opcode('append'); } }, ContentStatement: function(content) { if (content.value) { this.opcode('appendContent', content.value); } }, CommentStatement: function() {}, SubExpression: function(sexpr) { var type = this.classifySexpr(sexpr); if (type === 'simple') { this.simpleSexpr(sexpr); } else if (type === 'helper') { this.helperSexpr(sexpr); } else { this.ambiguousSexpr(sexpr); } }, ambiguousSexpr: function(sexpr, program, inverse) { var path = sexpr.path, name = path.parts[0], isBlock = program != null || inverse != null; this.opcode('getContext', path.depth); this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); this.accept(path); this.opcode('invokeAmbiguous', name, isBlock); }, simpleSexpr: function(sexpr) { this.accept(sexpr.path); this.opcode('resolvePossibleLambda'); }, helperSexpr: function(sexpr, program, inverse) { var params = this.setupFullMustacheParams(sexpr, program, inverse), path = sexpr.path, name = path.parts[0]; if (this.options.knownHelpers[name]) { this.opcode('invokeKnownHelper', params.length, name); } else if (this.options.knownHelpersOnly) { throw new Exception("You specified knownHelpersOnly, but used the unknown helper " + name, sexpr); } else { path.falsy = true; this.accept(path); this.opcode('invokeHelper', params.length, path.original, AST.helpers.simpleId(path)); } }, PathExpression: function(path) { this.addDepth(path.depth); this.opcode('getContext', path.depth); var name = path.parts[0], scoped = AST.helpers.scopedId(path), blockParamId = !path.depth && !scoped && this.blockParamIndex(name); if (blockParamId) { this.opcode('lookupBlockParam', blockParamId, path.parts); } else if (!name) { // Context reference, i.e. `{{foo .}}` or `{{foo ..}}` this.opcode('pushContext'); } else if (path.data) { this.options.data = true; this.opcode('lookupData', path.depth, path.parts); } else { this.opcode('lookupOnContext', path.parts, path.falsy, scoped); } }, StringLiteral: function(string) { this.opcode('pushString', string.value); }, NumberLiteral: function(number) { this.opcode('pushLiteral', number.value); }, BooleanLiteral: function(bool) { this.opcode('pushLiteral', bool.value); }, Hash: function(hash) { var pairs = hash.pairs, i, l; this.opcode('pushHash'); for (i=0, l=pairs.length; i= 0) { return [depth, param]; } } } }; export function precompile(input, options, env) { if (input == null || (typeof input !== 'string' && input.type !== 'Program')) { throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input); } options = options || {}; if (!('data' in options)) { options.data = true; } if (options.compat) { options.useDepths = true; } var ast = env.parse(input, options); var environment = new env.Compiler().compile(ast, options); return new env.JavaScriptCompiler().compile(environment, options); } export function compile(input, options, env) { if (input == null || (typeof input !== 'string' && input.type !== 'Program')) { throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input); } options = options || {}; if (!('data' in options)) { options.data = true; } if (options.compat) { options.useDepths = true; } var compiled; function compileInput() { var ast = env.parse(input, options); var environment = new env.Compiler().compile(ast, options); var templateSpec = new env.JavaScriptCompiler().compile(environment, options, undefined, true); return env.template(templateSpec); } // Template is only compiled on first use and cached after that point. var ret = function(context, options) { if (!compiled) { compiled = compileInput(); } return compiled.call(this, context, options); }; ret._setup = function(options) { if (!compiled) { compiled = compileInput(); } return compiled._setup(options); }; ret._child = function(i, data, blockParams, depths) { if (!compiled) { compiled = compileInput(); } return compiled._child(i, data, blockParams, depths); }; return ret; } function argEquals(a, b) { if (a === b) { return true; } if (isArray(a) && isArray(b) && a.length === b.length) { for (var i = 0; i < a.length; i++) { if (!argEquals(a[i], b[i])) { return false; } } return true; } }