Skip to content

Commit

Permalink
Move some things into utils.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jul 17, 2024
1 parent 7fb6e8c commit a35fd06
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 62 deletions.
2 changes: 2 additions & 0 deletions valuescript_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod builtins;
mod instruction_byte;
mod utils;

pub use builtins::*;
pub use instruction_byte::*;
pub use utils::*;
56 changes: 56 additions & 0 deletions valuescript_common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pub fn to_i32(x: f64) -> i32 {
if x == f64::INFINITY {
return 0;
}

let int1 = (x.trunc() as i64) & 0xffffffff;

int1 as i32
}

pub fn to_u32(x: f64) -> u32 {
if x == f64::INFINITY {
return 0;
}

let int1 = (x.trunc() as i64) & 0xffffffff;

int1 as u32
}

pub fn unicode_at(bytes: &[u8], len: usize, index: usize) -> Option<char> {
code_point_at(bytes, len, index)
.map(|code_point| std::char::from_u32(code_point).expect("Invalid code point"))
}

pub fn code_point_at(bytes: &[u8], len: usize, index: usize) -> Option<u32> {
if index >= len {
return None;
}

let byte = bytes[index];

let leading_ones = byte.leading_ones() as usize;

if leading_ones == 0 {
return Some(byte as u32);
}

if leading_ones == 1 || leading_ones > 4 || index + leading_ones > len {
return None;
}

let mut value = (byte & (0x7F >> leading_ones)) as u32;

for i in 1..leading_ones {
let next_byte = bytes[index + i];

if next_byte.leading_ones() != 1 {
return None;
}

value = (value << 6) | (next_byte & 0x3F) as u32;
}

Some(value)
}
2 changes: 1 addition & 1 deletion valuescript_compiler/src/get_span_text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use valuescript_vm::unicode_at;
use valuescript_common::unicode_at;

pub fn get_span_text(span: swc_common::Span, source: &str) -> String {
let mut res = String::new();
Expand Down
3 changes: 2 additions & 1 deletion valuescript_compiler/src/optimization/kal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use num_bigint::BigInt;
use valuescript_common::unicode_at;
use valuescript_vm::{
operations, unicode_at,
operations,
vs_class::VsClass,
vs_object::VsObject,
vs_value::{number_to_index, ToVal, Val},
Expand Down
2 changes: 1 addition & 1 deletion valuescript_compiler/src/static_expression_compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::RefCell;

use swc_common::Spanned;
use valuescript_vm::operations::to_i32;
use valuescript_common::to_i32;

use crate::{
asm::{Array, Builtin, Number, Object, Value},
Expand Down
3 changes: 2 additions & 1 deletion valuescript_vm/src/builtins/math_builtin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::fmt;
use std::rc::Rc;

use valuescript_common::to_u32;

use crate::native_function::{native_fn, NativeFunction};
use crate::operations::to_u32;
use crate::vs_class::VsClass;
use crate::vs_value::{LoadFunctionResult, ToVal, Val, ValTrait};

Expand Down
1 change: 0 additions & 1 deletion valuescript_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub use builtins::error_builtin;
pub use builtins::type_error_builtin;
pub use bytecode::{Bytecode, DecoderMaker};
pub use jsx_element::is_jsx_element;
pub use string_methods::unicode_at;
pub use virtual_machine::VirtualMachine;
pub use vs_symbol::VsSymbol;
pub use vs_value::{LoadFunctionResult, ValTrait};
22 changes: 2 additions & 20 deletions valuescript_vm/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use num_bigint::BigInt;
use num_bigint::Sign;
use num_traits::FromPrimitive;
use num_traits::ToPrimitive;
use valuescript_common::to_i32;
use valuescript_common::to_u32;

use crate::array_methods::op_sub_array;
use crate::bigint_methods::op_sub_bigint;
Expand Down Expand Up @@ -541,26 +543,6 @@ pub fn op_optional_chain(left: &mut Val, right: &Val) -> Result<Val, Val> {
}
}

pub fn to_i32(x: f64) -> i32 {
if x == f64::INFINITY {
return 0;
}

let int1 = (x.trunc() as i64) & 0xffffffff;

int1 as i32
}

pub fn to_u32(x: f64) -> u32 {
if x == f64::INFINITY {
return 0;
}

let int1 = (x.trunc() as i64) & 0xffffffff;

int1 as u32
}

pub fn op_bit_and(left: &Val, right: &Val) -> Result<Val, Val> {
if let Some(res) = try_binary_override(BinaryOp::BitAnd, left, right) {
return res;
Expand Down
39 changes: 2 additions & 37 deletions valuescript_vm/src/string_methods.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{rc::Rc, str::Chars};

use valuescript_common::{code_point_at, unicode_at};

use crate::{
builtins::internal_error_builtin::ToInternalError,
helpers::{to_wrapping_index, to_wrapping_index_clamped},
Expand Down Expand Up @@ -741,40 +743,3 @@ fn last_index_of(string_bytes: &[u8], search_bytes: &[u8], at_least_pos: usize)

None
}

pub fn unicode_at(bytes: &[u8], len: usize, index: usize) -> Option<char> {
code_point_at(bytes, len, index)
.map(|code_point| std::char::from_u32(code_point).expect("Invalid code point"))
}

fn code_point_at(bytes: &[u8], len: usize, index: usize) -> Option<u32> {
if index >= len {
return None;
}

let byte = bytes[index];

let leading_ones = byte.leading_ones() as usize;

if leading_ones == 0 {
return Some(byte as u32);
}

if leading_ones == 1 || leading_ones > 4 || index + leading_ones > len {
return None;
}

let mut value = (byte & (0x7F >> leading_ones)) as u32;

for i in 1..leading_ones {
let next_byte = bytes[index + i];

if next_byte.leading_ones() != 1 {
return None;
}

value = (value << 6) | (next_byte & 0x3F) as u32;
}

Some(value)
}

0 comments on commit a35fd06

Please sign in to comment.