source_hash

This commit is contained in:
Andrew Morris
2023-08-14 08:57:19 +10:00
parent cd81f79e4b
commit b0fa02a7cf
6 changed files with 39 additions and 98 deletions

16
Cargo.lock generated
View File

@@ -296,6 +296,12 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "crypto-common"
version = "0.1.3"
@@ -2098,6 +2104,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "tinyvec"
version = "1.5.1"
@@ -2242,6 +2257,7 @@ dependencies = [
"swc_common",
"swc_ecma_ast",
"swc_ecma_parser",
"tiny-keccak",
"valuescript_common",
"valuescript_vm",
]

View File

@@ -16,6 +16,7 @@ swc_ecma_parser = "0.102.2"
swc = "0.168.3"
swc_ecma_ast = "0.76.0"
queues = "1.0.2"
tiny-keccak = { version = "2.0", features = ["keccak"] }
valuescript_common = { path = "../valuescript_common" }
valuescript_vm = { path = "../valuescript_vm" }

View File

@@ -11,13 +11,13 @@ mod ident;
mod import_pattern;
mod instruction;
mod link_module;
mod minify;
mod module_compiler;
mod name_allocator;
mod optimization;
mod resolve_path;
mod scope;
mod scope_analysis;
mod source_hash;
mod static_expression_compiler;
mod target_accessor;
mod visit_pointers;

View File

@@ -1,94 +0,0 @@
use std::{
iter::{Peekable, Skip, Take},
str::Chars,
};
use swc_common::BytePos;
pub fn minify(source: &str, span: swc_common::Span) -> String {
let mut minifier = Minifier::new(source, span);
minifier.run();
minifier.res
}
struct Minifier<'a> {
chars: Peekable<Take<Skip<Chars<'a>>>>,
res: String,
}
impl<'a> Minifier<'a> {
fn new(source: &'a str, span: swc_common::Span) -> Self {
let BytePos(start) = span.lo;
let BytePos(end) = span.hi;
let chars = source
.chars()
.skip(start as usize)
.take((end - start) as usize)
.peekable();
Minifier {
chars,
res: String::new(),
}
}
fn run(&mut self) {
let mut punctuation_last = true;
while let Some(c) = self.chars.peek().cloned() {
if c.is_ascii_whitespace() {
self.skip_ws(punctuation_last);
continue;
}
self.res.push(c);
self.chars.next();
punctuation_last = is_js_punctuation(c);
match c {
'\'' | '"' => self.simple_string(c),
'`' => self.template_string(),
_ => {}
}
}
}
fn skip_ws(&mut self, punctuation_last: bool) {
while let Some(c) = self.chars.peek().cloned() {
if !c.is_ascii_whitespace() {
if !punctuation_last && !is_js_punctuation(c) {
self.res.push(' ');
}
break;
}
self.chars.next();
}
}
fn simple_string(&mut self, quote_c: char) {
let mut escaping = false;
for c in &mut self.chars {
self.res.push(c);
if !escaping && c == quote_c {
break;
}
escaping = c == '\\';
}
}
fn template_string(&mut self) {
todo!()
}
}
fn is_js_punctuation(c: char) -> bool {
c.is_ascii_punctuation() && !matches!(c, '$' | '_')
}

View File

@@ -15,7 +15,6 @@ use crate::diagnostic::{Diagnostic, DiagnosticContainer, DiagnosticReporter};
use crate::expression_compiler::{CompiledExpression, ExpressionCompiler};
use crate::function_compiler::{FunctionCompiler, Functionish};
use crate::ident::Ident;
use crate::minify::minify;
use crate::name_allocator::{ident_from_str, NameAllocator};
use crate::scope::OwnerId;
use crate::scope_analysis::{class_to_owner_id, ScopeAnalysis};
@@ -156,8 +155,6 @@ impl ModuleCompiler {
}
fn compile_module(&mut self, module: &swc_ecma_ast::Module) {
println!("Min: {}", minify(&self.source, module.span));
for module_item in &module.body {
self.compile_module_item(module_item);
}

View File

@@ -0,0 +1,21 @@
use swc_common::BytePos;
use tiny_keccak::{Hasher, Keccak};
#[allow(dead_code)]
pub fn source_hash(source: &str, span: swc_common::Span) -> [u8; 32] {
let BytePos(start) = span.lo;
let BytePos(end) = span.hi;
let chars = source
.chars()
.skip(start as usize)
.take((end - start) as usize);
let mut k = Keccak::v256();
k.update(chars.collect::<String>().as_bytes());
let mut output = [0u8; 32];
k.finalize(&mut output);
output
}