hbs 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.gitmodules +3 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/README.mdown +20 -0
- data/Rakefile +5 -0
- data/hbs.gemspec +28 -0
- data/js/.gitignore +5 -0
- data/js/Gemfile +4 -0
- data/js/LICENSE +20 -0
- data/js/README.markdown +224 -0
- data/js/Rakefile +107 -0
- data/js/bench/benchwarmer.js +147 -0
- data/js/bench/handlebars.js +166 -0
- data/js/lib/handlebars.js +15 -0
- data/js/lib/handlebars/ast.js +103 -0
- data/js/lib/handlebars/base.js +114 -0
- data/js/lib/handlebars/compiler.js +739 -0
- data/js/lib/handlebars/debug.js +29 -0
- data/js/lib/handlebars/printer.js +138 -0
- data/js/lib/handlebars/utils.js +66 -0
- data/js/lib/handlebars/visitor.js +13 -0
- data/js/spec/acceptance_spec.rb +100 -0
- data/js/spec/parser_spec.rb +259 -0
- data/js/spec/qunit_spec.js +836 -0
- data/js/spec/spec_helper.rb +106 -0
- data/js/spec/tokenizer_spec.rb +227 -0
- data/js/src/handlebars.l +34 -0
- data/js/src/handlebars.yy +99 -0
- data/lib/handlebars.rb +23 -0
- data/lib/handlebars/loader.rb +29 -0
- data/lib/handlebars/version.rb +3 -0
- data/spec/handlebars_spec.rb +32 -0
- metadata +112 -0
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/README.mdown
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
## Handlebars.rb
|
3
|
+
|
4
|
+
This uses [therubyracer](http://github.com/cowboyd/therubyracer) to bind to the _actual_ JavaScript implementation of
|
5
|
+
[Handlebars.js](http://github.com/wycats/handlebars.js) so that you can use it from ruby.
|
6
|
+
|
7
|
+
## Hack
|
8
|
+
|
9
|
+
git clone git@github.com:cowboyd/handlebars.rb.git #git it
|
10
|
+
cd handlebars.rb #go to it
|
11
|
+
git submodule update --init #pull down handlebars.js
|
12
|
+
rspec spec/ #test it
|
13
|
+
|
14
|
+
## Use
|
15
|
+
|
16
|
+
require 'handlebars'
|
17
|
+
template = Handlebars.compile("{{say}}{{what}}")
|
18
|
+
template.call(:say => "Hey", :what => "Yuh!") #=> "Hey Yuh!"
|
19
|
+
|
20
|
+
|
data/Rakefile
ADDED
data/hbs.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "handlebars/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hbs"
|
7
|
+
s.version = Handlebars::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Charles Lowell"]
|
10
|
+
s.email = ["cowboyd@thefrontside.net"]
|
11
|
+
s.homepage = "http://github.com/cowboyd/handlebars.rb"
|
12
|
+
s.summary = %q{Ruby bindings for the handlebars.js templating library}
|
13
|
+
s.description = %q{Uses the rubyracer bind in rails}
|
14
|
+
|
15
|
+
s.rubyforge_project = "handlebars"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
Dir.chdir("js") do
|
22
|
+
s.files += `git ls-files`.split("\n").map {|f| "js/#{f}"}
|
23
|
+
s.files += ['js/lib/handlebars/parser.js']
|
24
|
+
end
|
25
|
+
|
26
|
+
s.add_dependency "therubyracer", "~> 0.9.3beta1"
|
27
|
+
s.add_development_dependency "rspec", "~> 2.0.0"
|
28
|
+
end
|
data/js/.gitignore
ADDED
data/js/Gemfile
ADDED
data/js/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (C) 2011 by Yehuda Katz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
data/js/README.markdown
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
Handlebars.js
|
2
|
+
=============
|
3
|
+
|
4
|
+
Handlebars.js is an extension to the [Mustache templating language](http://mustache.github.com/) created by Chris Wanstrath. Handlebars.js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be.
|
5
|
+
|
6
|
+
Checkout the official Handlebars docs site at [http://www.handlebarsjs.com](http://www.handlebarsjs.com).
|
7
|
+
|
8
|
+
|
9
|
+
Installing
|
10
|
+
----------
|
11
|
+
Installing Handlebars is easy. Simply [download the package from GitHub](https://github.com/wycats/handlebars.js/archives/master) and add it to your web pages (you should usually use the most recent version).
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
In general, the syntax of Handlebars.js templates is a superset of Mustache templates. For basic syntax, check out the [Mustache manpage](http://mustache.github.com/mustache.5.html).
|
16
|
+
|
17
|
+
Once you have a template, use the Handlebars.compile method to compile the template into a function. The generated function takes a context argument, which will be used to render the template.
|
18
|
+
|
19
|
+
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
|
20
|
+
"{{kids/length}} kids:</p>" +
|
21
|
+
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
|
22
|
+
var template = Handlebars.compile(source);
|
23
|
+
|
24
|
+
var data = { "name": "Alan", "hometown": "Somewhere, TX",
|
25
|
+
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
|
26
|
+
var result = template(data);
|
27
|
+
|
28
|
+
// Would render:
|
29
|
+
// <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>
|
30
|
+
// <ul>
|
31
|
+
// <li>Jimmy is 12</li>
|
32
|
+
// <li>Sally is 4</li>
|
33
|
+
// </ul>
|
34
|
+
|
35
|
+
|
36
|
+
Registering Helpers
|
37
|
+
-------------------
|
38
|
+
|
39
|
+
You can register helpers that Handlebars will use when evaluating your
|
40
|
+
template. Here's an example, which assumes that your objects have a URL
|
41
|
+
embedded in them, as well as the text for a link:
|
42
|
+
|
43
|
+
Handlebars.registerHelper('link_to', function(context) {
|
44
|
+
return "<a href='" + context.url + "'>" + context.body + "</a>";
|
45
|
+
});
|
46
|
+
|
47
|
+
var context = { posts: [{url: "/hello-world", body: "Hello World!"}] };
|
48
|
+
var source = "<ul>{{#posts}}<li>{{{link_to this}}}</li>{{/posts}}</ul>"
|
49
|
+
|
50
|
+
var template = Handlebars.compile(source);
|
51
|
+
template(context);
|
52
|
+
|
53
|
+
// Would render:
|
54
|
+
//
|
55
|
+
// <ul>
|
56
|
+
// <li><a href='/hello-world'>Hello World!</a></li>
|
57
|
+
// </ul>
|
58
|
+
|
59
|
+
|
60
|
+
Escaping
|
61
|
+
--------
|
62
|
+
|
63
|
+
By default, the `{{expression}}` syntax will escape its contents. This
|
64
|
+
helps to protect you against accidental XSS problems caused by malicious
|
65
|
+
data passed from the server as JSON.
|
66
|
+
|
67
|
+
To explicitly *not* escape the contents, use the triple-mustache
|
68
|
+
(`{{{}}}`). You have seen this used in the above example.
|
69
|
+
|
70
|
+
|
71
|
+
Differences Between Handlebars.js and Mustache
|
72
|
+
----------------------------------------------
|
73
|
+
Handlebars.js adds a couple of additional features to make writing templates easier and also changes a tiny detail of how partials work.
|
74
|
+
|
75
|
+
### Paths
|
76
|
+
|
77
|
+
Handlebars.js supports an extended expression syntax that we call paths. Paths are made up of typical expressions and / characters. Expressions allow you to not only display data from the current context, but to display data from contexts that are descendents and ancestors of the current context.
|
78
|
+
|
79
|
+
To display data from descendent contexts, use the `/` character. So, for example, if your data were structured like:
|
80
|
+
|
81
|
+
var data = {"person": { "name": "Alan" }, company: {"name": "Rad, Inc." } };
|
82
|
+
|
83
|
+
you could display the person's name from the top-level context with the following expression:
|
84
|
+
|
85
|
+
{{person/name}}
|
86
|
+
|
87
|
+
Similarly, if already traversed into the person object you could still display the company's name with an expression like `{{../company/name}}`, so:
|
88
|
+
|
89
|
+
{{#person}}{{name}} - {{../company/name}}{{/person}}
|
90
|
+
|
91
|
+
would render:
|
92
|
+
|
93
|
+
Alan - Rad, Inc.
|
94
|
+
|
95
|
+
### Strings
|
96
|
+
|
97
|
+
When calling a helper, you can pass paths or Strings as parameters. For
|
98
|
+
instance:
|
99
|
+
|
100
|
+
Handlebars.registerHelper('link_to', function(title, context) {
|
101
|
+
return "<a href='/posts" + context.id + "'>" + title + "</a>"
|
102
|
+
});
|
103
|
+
|
104
|
+
var context = { posts: [{url: "/hello-world", body: "Hello World!"}] };
|
105
|
+
var source = '<ul>{{#posts}}<li>{{{link_to "Post" this}}}</li>{{/posts}}</ul>'
|
106
|
+
|
107
|
+
var template = Handlebars.compile(source);
|
108
|
+
template(context);
|
109
|
+
|
110
|
+
// Would render:
|
111
|
+
//
|
112
|
+
// <ul>
|
113
|
+
// <li><a href='/hello-world'>Post!</a></li>
|
114
|
+
// </ul>
|
115
|
+
|
116
|
+
When you pass a String as a parameter to a helper, the literal String
|
117
|
+
gets passed to the helper function.
|
118
|
+
|
119
|
+
|
120
|
+
### Block Helpers
|
121
|
+
|
122
|
+
Handlebars.js also adds the ability to define block helpers. Block helpers are functions that can be called from anywhere in the template. Here's an example:
|
123
|
+
|
124
|
+
var source = "<ul>{{#people}}<li>{{{#link}}}{{name}}{{/link}}</li>{{/people}}</ul>";
|
125
|
+
Handlebars.registerHelper('link', function(context, fn) {
|
126
|
+
return '<a href="/people/' + this.__get__("id") + '">' + fn(this) + '</a>';
|
127
|
+
});
|
128
|
+
var template = Handlebars.compile(source);
|
129
|
+
|
130
|
+
var data = { "people": [
|
131
|
+
{ "name": "Alan", "id": 1 },
|
132
|
+
{ "name": "Yehuda", "id": 2 }
|
133
|
+
]};
|
134
|
+
template(data);
|
135
|
+
|
136
|
+
// Should render:
|
137
|
+
// <ul>
|
138
|
+
// <li><a href="/people/1">Alan</a></li>
|
139
|
+
// <li><a href="/people/2">Yehuda</a></li>
|
140
|
+
// </ul>
|
141
|
+
|
142
|
+
Whenever the block helper is called it is given two parameters, the argument that is passed to the helper, or the current context if no argument is passed and the compiled contents of the block. Inside of the block helper the value of `this` is the current context, wrapped to include a method named `__get__` that helps translate paths into values within the helpers.
|
143
|
+
|
144
|
+
### Partials
|
145
|
+
|
146
|
+
You can register additional templates as partials, which will be used by
|
147
|
+
Handlebars when it encounters a partial (`{{> partialName}}`). Partials
|
148
|
+
can either be String templates or compiled template functions. Here's an
|
149
|
+
example:
|
150
|
+
|
151
|
+
var source = "<ul>{{#people}}<li>{{> link}}</li>{{/people}}</ul>";
|
152
|
+
|
153
|
+
Handlebars.registerPartial('link', '<a href="/people/{{id}}">{{name}}</a>')
|
154
|
+
var template = Handlebars.compile(source);
|
155
|
+
|
156
|
+
var data = { "people": [
|
157
|
+
{ "name": "Alan", "id": 1 },
|
158
|
+
{ "name": "Yehuda", "id": 2 }
|
159
|
+
]};
|
160
|
+
|
161
|
+
template(data);
|
162
|
+
|
163
|
+
// Should render:
|
164
|
+
// <ul>
|
165
|
+
// <li><a href="/people/1">Alan</a></li>
|
166
|
+
// <li><a href="/people/2">Yehuda</a></li>
|
167
|
+
// </ul>
|
168
|
+
|
169
|
+
Precompiling Templates
|
170
|
+
----------------------
|
171
|
+
|
172
|
+
TODO in the rewrite. This will use RubyRacer and not node.
|
173
|
+
|
174
|
+
Performance
|
175
|
+
-----------
|
176
|
+
|
177
|
+
In a rough performance test, precompiled Handlebars.js templates (in the original version of Handlebars.js) rendered in about half the time of Mustache templates. It would be a shame if it were any other way, since they were precompiled, but the difference in architecture does have some big performance advantages. Justin Marney, a.k.a. [gotascii](http://github.com/gotascii), confirmed that with an [independent test](http://sorescode.com/2010/09/12/benchmarks.html). The rewritten Handlebars (current version) is faster than the old version, and we will have some benchmarks in the near future.
|
178
|
+
|
179
|
+
|
180
|
+
Building
|
181
|
+
--------
|
182
|
+
|
183
|
+
To build handlebars, just run `rake release`, and you will get two files
|
184
|
+
in the `dist` directory. The debug version comes with stack trace
|
185
|
+
annotations for webkit browsers, but will slightly increase startup
|
186
|
+
time.
|
187
|
+
|
188
|
+
|
189
|
+
Upgrading
|
190
|
+
---------
|
191
|
+
|
192
|
+
When upgrading from the Handlebars 0.9 series, be aware that the
|
193
|
+
signature for passing custom helpers or partials to templates has
|
194
|
+
changed.
|
195
|
+
|
196
|
+
Instead of:
|
197
|
+
|
198
|
+
template(context, helpers, partials, [data])
|
199
|
+
|
200
|
+
Use:
|
201
|
+
|
202
|
+
template(context, {helpers: helpers, partials: partials, data: data})
|
203
|
+
|
204
|
+
Known Issues
|
205
|
+
------------
|
206
|
+
* Handlebars.js can be cryptic when there's an error while rendering.
|
207
|
+
|
208
|
+
Handlebars Contrib
|
209
|
+
------------------
|
210
|
+
Alan Johnson, a.k.a. commondream, keeps a repository of useful helpers in the [handlebars-contrib](http://github.com/commondream/handlebars-contrib) repository. Feel free to copy those, or add some of your own.
|
211
|
+
|
212
|
+
Handlebars in the Wild
|
213
|
+
-----------------
|
214
|
+
* Don Park wrote an Express.js view engine adapter for Handlebars.js called [hbs](http://github.com/donpark/hbs)
|
215
|
+
* [sammy.js](http://github.com/quirkey/sammy) by Aaron Quint, a.k.a. quirkey, supports Handlebars.js as one of its template plugins.
|
216
|
+
* [SproutCore](http://www.sproutcore.com) uses Handlebars.js as its main templating engine, extending it with automatic data binding support.
|
217
|
+
|
218
|
+
Helping Out
|
219
|
+
-----------
|
220
|
+
If you notice any problems, please report them to the GitHub issue tracker at [http://github.com/wycats/handlebars.js/issues](http://github.com/wycats/handlebars.js/issues). Feel free to contact commondream or wycats through GitHub with any other questions or feature requests. To submit changes fork the project and send a pull request.
|
221
|
+
|
222
|
+
License
|
223
|
+
-------
|
224
|
+
Handlebars.js is released under the MIT license.
|
data/js/Rakefile
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
file "lib/handlebars/parser.js" => ["src/handlebars.yy","src/handlebars.l"] do
|
5
|
+
if ENV['PATH'].split(':').any? {|folder| File.exists?(folder+'/jison')}
|
6
|
+
system "jison src/handlebars.yy src/handlebars.l"
|
7
|
+
File.open("lib/handlebars/parser.js", "w") do |file|
|
8
|
+
file.puts File.read("handlebars.js") + ";"
|
9
|
+
end
|
10
|
+
|
11
|
+
sh "rm handlebars.js"
|
12
|
+
else
|
13
|
+
puts "Jison is not installed. Try running `npm install jison`."
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
task :compile => "lib/handlebars/parser.js"
|
18
|
+
|
19
|
+
desc "run the spec suite"
|
20
|
+
task :spec => [:release] do
|
21
|
+
system "rspec -cfs spec"
|
22
|
+
end
|
23
|
+
|
24
|
+
task :default => [:compile, :spec]
|
25
|
+
|
26
|
+
def remove_exports(string)
|
27
|
+
match = string.match(%r{^// BEGIN\(BROWSER\)\n(.*)\n^// END\(BROWSER\)}m)
|
28
|
+
match ? match[1] : string
|
29
|
+
end
|
30
|
+
|
31
|
+
minimal_deps = %w(parser base ast visitor utils compiler).map do |file|
|
32
|
+
"lib/handlebars/#{file}.js"
|
33
|
+
end
|
34
|
+
|
35
|
+
debug_deps = %w(parser base ast visitor printer utils compiler debug).map do |file|
|
36
|
+
"lib/handlebars/#{file}.js"
|
37
|
+
end
|
38
|
+
|
39
|
+
directory "dist"
|
40
|
+
|
41
|
+
minimal_deps.unshift "dist"
|
42
|
+
debug_deps.unshift "dist"
|
43
|
+
|
44
|
+
def build_for_task(task)
|
45
|
+
FileUtils.rm_rf("dist/*") if File.directory?("dist")
|
46
|
+
FileUtils.mkdir_p("dist")
|
47
|
+
|
48
|
+
contents = []
|
49
|
+
task.prerequisites.each do |filename|
|
50
|
+
next if filename == "dist"
|
51
|
+
|
52
|
+
contents << "// #{filename}\n" + remove_exports(File.read(filename)) + ";"
|
53
|
+
end
|
54
|
+
|
55
|
+
File.open(task.name, "w") do |file|
|
56
|
+
file.puts contents.join("\n")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
file "dist/handlebars.js" => minimal_deps do |task|
|
61
|
+
build_for_task(task)
|
62
|
+
end
|
63
|
+
|
64
|
+
file "dist/handlebars.debug.js" => debug_deps do |task|
|
65
|
+
build_for_task(task)
|
66
|
+
end
|
67
|
+
|
68
|
+
task :build => [:compile, "dist/handlebars.js"]
|
69
|
+
task :debug => [:compile, "dist/handlebars.debug.js"]
|
70
|
+
|
71
|
+
desc "build the build and debug versions of handlebars"
|
72
|
+
task :release => [:build, :debug]
|
73
|
+
|
74
|
+
directory "vendor"
|
75
|
+
|
76
|
+
desc "benchmark against dust.js and mustache.js"
|
77
|
+
task :bench => "vendor" do
|
78
|
+
require "open-uri"
|
79
|
+
File.open("vendor/mustache.js", "w") do |file|
|
80
|
+
file.puts open("https://github.com/janl/mustache.js/raw/master/mustache.js").read
|
81
|
+
file.puts "module.exports = Mustache;"
|
82
|
+
end
|
83
|
+
|
84
|
+
File.open("vendor/benchmark.js", "w") do |file|
|
85
|
+
file.puts open("https://github.com/mathiasbynens/benchmark.js/raw/master/benchmark.js").read
|
86
|
+
end
|
87
|
+
|
88
|
+
if File.directory?("vendor/dustjs")
|
89
|
+
system "cd vendor/dustjs && git pull"
|
90
|
+
else
|
91
|
+
system "git clone git://github.com/akdubya/dustjs.git vendor/dustjs"
|
92
|
+
end
|
93
|
+
|
94
|
+
if File.directory?("vendor/coffee")
|
95
|
+
system "cd vendor/coffee && git pull"
|
96
|
+
else
|
97
|
+
system "git clone git://github.com/jashkenas/coffee-script.git vendor/coffee"
|
98
|
+
end
|
99
|
+
|
100
|
+
if File.directory?("vendor/eco")
|
101
|
+
system "cd vendor/eco && git pull"
|
102
|
+
else
|
103
|
+
system "git clone git://github.com/sstephenson/eco.git vendor/eco"
|
104
|
+
end
|
105
|
+
|
106
|
+
system "node bench/handlebars.js"
|
107
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
|
2
|
+
var Benchmark = require("benchmark");
|
3
|
+
|
4
|
+
var BenchWarmer = function(names) {
|
5
|
+
this.benchmarks = [];
|
6
|
+
this.currentBenches = [];
|
7
|
+
this.names = [];
|
8
|
+
this.errors = {};
|
9
|
+
};
|
10
|
+
|
11
|
+
var print = require("sys").print;
|
12
|
+
|
13
|
+
BenchWarmer.prototype = {
|
14
|
+
winners: function(benches) {
|
15
|
+
var result = Benchmark.filter(benches, function(bench) { return bench.cycles; });
|
16
|
+
|
17
|
+
if (result.length > 1) {
|
18
|
+
result.sort(function(a, b) { return b.compare(a); });
|
19
|
+
first = result[0];
|
20
|
+
last = result[result.length - 1];
|
21
|
+
|
22
|
+
var winners = [];
|
23
|
+
|
24
|
+
Benchmark.each(result, function(bench) {
|
25
|
+
if (bench.compare(first) === 0) {
|
26
|
+
winners.push(bench);
|
27
|
+
}
|
28
|
+
});
|
29
|
+
|
30
|
+
return winners;
|
31
|
+
} else {
|
32
|
+
return result;
|
33
|
+
}
|
34
|
+
},
|
35
|
+
suite: function(suite, fn) {
|
36
|
+
this.suiteName = suite;
|
37
|
+
this.first = true;
|
38
|
+
|
39
|
+
var self = this;
|
40
|
+
|
41
|
+
fn(function(name, benchFn) {
|
42
|
+
self.push(name, benchFn);
|
43
|
+
});
|
44
|
+
},
|
45
|
+
push: function(name, fn) {
|
46
|
+
if(this.names.indexOf(name) == -1) {
|
47
|
+
this.names.push(name);
|
48
|
+
}
|
49
|
+
|
50
|
+
var first = this.first, suiteName = this.suiteName, self = this;
|
51
|
+
this.first = false;
|
52
|
+
|
53
|
+
var bench = new Benchmark(function() {
|
54
|
+
fn();
|
55
|
+
}, {
|
56
|
+
name: this.suiteName + ": " + name,
|
57
|
+
onComplete: function() {
|
58
|
+
if(first) { self.startLine(suiteName); }
|
59
|
+
self.writeBench(bench);
|
60
|
+
self.currentBenches.push(bench);
|
61
|
+
}, onError: function() {
|
62
|
+
self.errors[this.name] = this;
|
63
|
+
}
|
64
|
+
});
|
65
|
+
|
66
|
+
this.benchmarks.push(bench);
|
67
|
+
},
|
68
|
+
bench: function() {
|
69
|
+
var benchSize = 0, names = this.names, self = this, i, l;
|
70
|
+
|
71
|
+
for(i=0, l=names.length; i<l; i++) {
|
72
|
+
var name = names[i];
|
73
|
+
|
74
|
+
if(benchSize < name.length) { benchSize = name.length; }
|
75
|
+
}
|
76
|
+
|
77
|
+
this.nameSize = benchSize + 2;
|
78
|
+
this.benchSize = 20;
|
79
|
+
var horSize = 0;
|
80
|
+
|
81
|
+
this.startLine("ops/msec");
|
82
|
+
horSize = horSize + "ops/msec ".length;
|
83
|
+
for(i=0, l=names.length; i<l; i++) {
|
84
|
+
print(names[i] + new Array(this.benchSize - names[i].length + 1).join(" "));
|
85
|
+
horSize = horSize + this.benchSize;
|
86
|
+
}
|
87
|
+
|
88
|
+
print("WINNER(S)");
|
89
|
+
horSize = horSize + "WINNER(S)".length;
|
90
|
+
|
91
|
+
print("\n" + new Array(horSize + 1).join("-"));
|
92
|
+
|
93
|
+
Benchmark.invoke(this.benchmarks, {
|
94
|
+
name: "run",
|
95
|
+
onComplete: function() {
|
96
|
+
var errors = false, prop, bench;
|
97
|
+
for(prop in self.errors) { if(self.errors.hasOwnProperty(prop)) { errors = true; break; } }
|
98
|
+
|
99
|
+
if(errors) {
|
100
|
+
print("\n\nErrors:\n");
|
101
|
+
for(prop in self.errors) {
|
102
|
+
if(self.errors.hasOwnProperty(prop)) {
|
103
|
+
bench = self.errors[prop];
|
104
|
+
print("\n" + bench.name + ":\n");
|
105
|
+
print(bench.error.message);
|
106
|
+
if(bench.error.stack) {
|
107
|
+
print(bench.error.stack.join("\n"));
|
108
|
+
}
|
109
|
+
print("\n");
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
});
|
115
|
+
},
|
116
|
+
startLine: function(name) {
|
117
|
+
var winners = Benchmark.map(this.winners(this.currentBenches), function(bench) {
|
118
|
+
return bench.name.split(": ")[1];
|
119
|
+
});
|
120
|
+
|
121
|
+
this.currentBenches = [];
|
122
|
+
|
123
|
+
print(winners.join(", "));
|
124
|
+
print("\n");
|
125
|
+
var padding = this.nameSize - name.length + 1;
|
126
|
+
name = name + new Array(padding).join(" ");
|
127
|
+
print(name);
|
128
|
+
},
|
129
|
+
writeBench: function(bench) {
|
130
|
+
var out;
|
131
|
+
|
132
|
+
if(!bench.error) {
|
133
|
+
var count = bench.hz,
|
134
|
+
moe = count * bench.stats.RME / 100;
|
135
|
+
|
136
|
+
out = Math.round(count / 1000) + " ±" + Math.round(moe / 1000) + " (" + bench.cycles + ")";
|
137
|
+
} else {
|
138
|
+
out = "E";
|
139
|
+
}
|
140
|
+
|
141
|
+
var padding = this.benchSize - out.length + 1;
|
142
|
+
out = out + new Array(padding).join(" ");
|
143
|
+
print(out);
|
144
|
+
}
|
145
|
+
};
|
146
|
+
|
147
|
+
module.exports = BenchWarmer;
|