Skip to content

Commit

Permalink
Fix get_span_text unicode bug
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Mar 1, 2024
1 parent 131ebcc commit 6d83ec2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
13 changes: 1 addition & 12 deletions valuescript_compiler/src/expression_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1882,15 +1883,3 @@ pub fn value_from_literal(lit: &swc_ecma_ast::Lit) -> Result<Value, &'static str
JSXText(_) => 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::<String>()
}
13 changes: 13 additions & 0 deletions valuescript_compiler/src/get_span_text.rs
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions valuescript_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 2 additions & 11 deletions valuescript_compiler/src/src_hash.rs
Original file line number Diff line number Diff line change
@@ -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::<String>().as_bytes());
k.update(get_span_text(span, source).as_bytes());

let mut output = [0u8; 32];
k.finalize(&mut output);
Expand Down

0 comments on commit 6d83ec2

Please sign in to comment.