diff --git a/valuescript_compiler/src/expression_compiler.rs b/valuescript_compiler/src/expression_compiler.rs index 12b2d02..35461a7 100644 --- a/valuescript_compiler/src/expression_compiler.rs +++ b/valuescript_compiler/src/expression_compiler.rs @@ -6,6 +6,7 @@ use swc_common::Spanned; use crate::asm::{Array, Instruction, Label, Number, Object, Register, Structured, Value}; use crate::diagnostic::{Diagnostic, DiagnosticContainer, DiagnosticReporter}; use crate::function_compiler::{FunctionCompiler, Functionish}; +use crate::get_span_text::get_span_text; use crate::ident::Ident as CrateIdent; use crate::scope::{NameId, OwnerId}; use crate::scope_analysis::{fn_to_owner_id, NameType}; @@ -1882,15 +1883,3 @@ pub fn value_from_literal(lit: &swc_ecma_ast::Lit) -> Result return Err("JSXText literals"), }) } - -fn get_span_text(span: swc_common::Span, source: &str) -> String { - let swc_common::BytePos(start) = span.lo; - let swc_common::BytePos(end) = span.hi; - - let chars = source - .chars() - .skip(start as usize) - .take((end - start) as usize); - - chars.collect::() -} diff --git a/valuescript_compiler/src/get_span_text.rs b/valuescript_compiler/src/get_span_text.rs new file mode 100644 index 0000000..b9edad7 --- /dev/null +++ b/valuescript_compiler/src/get_span_text.rs @@ -0,0 +1,13 @@ +use valuescript_vm::unicode_at; + +pub fn get_span_text(span: swc_common::Span, source: &str) -> String { + let mut res = String::new(); + + for i in span.lo().0..span.hi().0 { + if let Some(c) = unicode_at(source.as_bytes(), source.len(), i as usize) { + res.push(c); + } + } + + res +} diff --git a/valuescript_compiler/src/lib.rs b/valuescript_compiler/src/lib.rs index 3b56811..b69edee 100644 --- a/valuescript_compiler/src/lib.rs +++ b/valuescript_compiler/src/lib.rs @@ -7,6 +7,7 @@ mod diagnostic; mod expression_compiler; mod function_compiler; mod gather_modules; +mod get_span_text; mod ident; mod import_pattern; mod inline_valuescript; diff --git a/valuescript_compiler/src/src_hash.rs b/valuescript_compiler/src/src_hash.rs index 12a7a74..235dce7 100644 --- a/valuescript_compiler/src/src_hash.rs +++ b/valuescript_compiler/src/src_hash.rs @@ -1,19 +1,10 @@ -use swc_common::BytePos; use tiny_keccak::{Hasher, Keccak}; -use crate::asm::Hash; +use crate::{asm::Hash, get_span_text::get_span_text}; pub fn src_hash(source: &str, span: swc_common::Span) -> Hash { - 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::().as_bytes()); + k.update(get_span_text(span, source).as_bytes()); let mut output = [0u8; 32]; k.finalize(&mut output);