2018-12-21 10:54:36 +03:00
|
|
|
#![feature(box_syntax)]
|
|
|
|
#![feature(test)]
|
|
|
|
|
2019-12-14 12:51:08 +03:00
|
|
|
extern crate test;
|
|
|
|
|
2018-12-21 10:54:36 +03:00
|
|
|
use sourcemap::SourceMapBuilder;
|
|
|
|
use swc_common::FileName;
|
2019-12-02 11:10:06 +03:00
|
|
|
use swc_ecma_codegen::{self, Emitter};
|
2018-12-30 05:57:27 +03:00
|
|
|
use swc_ecma_parser::{Parser, Session, SourceFileInput, Syntax};
|
2018-12-21 10:54:36 +03:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
const SOURCE: &str = r#"
|
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* Extract red color out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0x00
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function red( color )
|
|
|
|
{
|
|
|
|
let foo = 3.14;
|
|
|
|
return color >> 16;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Extract green out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0xDE
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function green( color )
|
|
|
|
{
|
|
|
|
return ( color >> 8 ) & 0xFF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Extract blue color out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0xAD
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function blue( color )
|
|
|
|
{
|
|
|
|
return color & 0xFF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts an integer containing a color such as 0x00DEAD to a hex
|
|
|
|
* string, such as '#00DEAD';
|
|
|
|
*
|
|
|
|
* @param {Number} int
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function intToHex( int )
|
|
|
|
{
|
|
|
|
const mask = '#000000';
|
|
|
|
const hex = int.toString( 16 );
|
|
|
|
return mask.substring( 0, 7 - hex.length ) + hex;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts a hex string containing a color such as '#00DEAD' to
|
|
|
|
* an integer, such as 0x00DEAD;
|
|
|
|
*
|
|
|
|
* @param {Number} num
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function hexToInt( hex )
|
|
|
|
{
|
|
|
|
return parseInt( hex.substring( 1 ), 16 );
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
red,
|
|
|
|
green,
|
|
|
|
blue,
|
|
|
|
intToHex,
|
|
|
|
hexToInt,
|
|
|
|
};
|
|
|
|
"#;
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn emit_colors(b: &mut Bencher) {
|
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(true, |cm, handler| {
|
|
|
|
let session = Session { handler: &handler };
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2019-02-08 07:00:58 +03:00
|
|
|
let mut parser = Parser::new(
|
|
|
|
session,
|
|
|
|
Syntax::default(),
|
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-12-21 10:54:36 +03:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let buf = vec![];
|
|
|
|
let mut src_map_builder = SourceMapBuilder::new(None);
|
|
|
|
{
|
|
|
|
let handlers = box MyHandlers;
|
|
|
|
let mut emitter = Emitter {
|
|
|
|
cfg: swc_ecma_codegen::Config {
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-02-26 07:56:58 +03:00
|
|
|
comments: None,
|
2018-12-21 10:54:36 +03:00
|
|
|
cm: cm.clone(),
|
|
|
|
wr: box swc_ecma_codegen::text_writer::JsWriter::new(
|
|
|
|
cm.clone(),
|
|
|
|
"\n",
|
|
|
|
buf,
|
2019-02-17 17:25:35 +03:00
|
|
|
Some(&mut src_map_builder),
|
2018-12-21 10:54:36 +03:00
|
|
|
),
|
|
|
|
handlers,
|
|
|
|
pos_of_leading_comments: Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(&module)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyHandlers;
|
|
|
|
|
|
|
|
impl swc_ecma_codegen::Handlers for MyHandlers {}
|