Skip to content

Commit

Permalink
Escape HTML correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Feb 25, 2025
1 parent 6aa74cf commit 83151bf
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
components: clippy
profile: minimal
toolchain: 1.78.0
toolchain: 1.85.0
- name: Install Rust (nightly)
uses: actions-rs/toolchain@v1
with:
Expand Down
27 changes: 15 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [
]
categories = ["development-tools"]
description = "A compiler, VM, language server, and online playground for the Lox programming language"
edition = "2021"
edition = "2024"
keywords = [
"cli",
"compiler",
Expand All @@ -24,7 +24,7 @@ keywords = [
license = "MIT"
name = "loxcraft"
repository = "https://github.com/ajeetdsouza/loxcraft"
rust-version = "1.70.0"
rust-version = "1.85.0"
version = "0.1.1"

[badges]
Expand Down Expand Up @@ -75,7 +75,7 @@ warp-embed = { version = "0.5.0", optional = true }
webbrowser = { version = "1.0.2", optional = true }

[target.'cfg(target_family = "wasm")'.dependencies]
wasm-bindgen = "0.2.67"
wasm-bindgen = "0.2.100"

[target.'cfg(not(any(miri, target_family = "wasm")))'.dependencies]
mimalloc = { version = "0.1.27", default-features = false }
Expand Down
36 changes: 20 additions & 16 deletions playground/rust/lox-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion playground/rust/lox-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "lox-wasm"
version = "0.1.0"
edition = "2021"
edition = "2024"

[lib]
crate-type = ["cdylib"]
Expand Down
6 changes: 3 additions & 3 deletions playground/rust/lox-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[allow(non_snake_case)]
pub fn loxRun(source: &str) {
let output = &mut Output::new();
match VM::default().run(source, output) {
let writer = Output::new();
let mut writer = HtmlWriter::new(writer);
match VM::default().run(source, &mut writer) {
Ok(()) => postMessage(&Message::ExitSuccess.to_string()),
Err(errors) => {
let mut writer = HtmlWriter::new(output);
for e in errors.iter() {
report_error(&mut writer, source, e);
}
Expand Down
8 changes: 4 additions & 4 deletions src/vm/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ impl<T> Allocator<T> {
unsafe impl<T: GlobalAlloc> GlobalAlloc for Allocator<T> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.allocated_bytes.fetch_add(layout.size(), Ordering::Relaxed);
self.inner.alloc(layout)
unsafe { self.inner.alloc(layout) }
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.allocated_bytes.fetch_sub(layout.size(), Ordering::Relaxed);
self.inner.dealloc(ptr, layout)
unsafe { self.inner.dealloc(ptr, layout) }
}

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
self.allocated_bytes.fetch_add(layout.size(), Ordering::Relaxed);
self.inner.alloc_zeroed(layout)
unsafe { self.inner.alloc_zeroed(layout) }
}

unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
self.allocated_bytes.fetch_add(new_size.wrapping_sub(layout.size()), Ordering::Relaxed);
self.inner.realloc(ptr, layout, new_size)
unsafe { self.inner.realloc(ptr, layout, new_size) }
}
}
1 change: 1 addition & 0 deletions src/vm/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn now() -> f64 {
.as_secs_f64()
}

#[inline(always)]
pub const fn unreachable() -> ! {
if cfg!(debug_assertions) { unreachable!() } else { unsafe { hint::unreachable_unchecked() } }
}
39 changes: 7 additions & 32 deletions src/vm/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,13 @@ impl Debug for Value {

impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.is_nil() {
write!(f, "nil")
} else if self.is_true() {
write!(f, "true")
} else if self.is_false() {
write!(f, "false")
} else if self.is_number() {
write!(f, "{}", self.as_number())
} else if self.is_object() {
write!(f, "{}", self.as_object())
} else {
util::unreachable()
match *self {
Self::NIL => write!(f, "nil"),
Self::TRUE => write!(f, "true"),
Self::FALSE => write!(f, "false"),
_ if self.is_number() => write!(f, "{}", self.as_number()),
_ if self.is_object() => write!(f, "{}", self.as_object()),
_ => util::unreachable(),
}
}
}
Expand Down Expand Up @@ -104,20 +99,6 @@ impl Value {
self.0 & (Self::QNAN | Self::SIGN_BIT) == (Self::QNAN | Self::SIGN_BIT)
}

pub fn is_false(self) -> bool {
Self(self.0) == Self::FALSE
}

pub fn is_true(self) -> bool {
Self(self.0) == Self::TRUE
}

/// # Safety
/// This is undefined behavior if the [`Value`] is not of type [`ValueType::Bool`].
pub fn as_bool(self) -> bool {
self == Self::TRUE
}

/// # Safety
/// This is undefined behavior if the [`Value`] is not of type [`ValueType::Number`].
pub fn as_number(self) -> f64 {
Expand Down Expand Up @@ -169,12 +150,6 @@ mod tests {

#[test]
fn convert_to_and_from_values() {
let value = false;
assert_eq!(Value::from(value).as_bool(), value);

let value = true;
assert_eq!(Value::from(value).as_bool(), value);

let value = 0.0;
assert_eq!(Value::from(value).as_number(), value);

Expand Down

0 comments on commit 83151bf

Please sign in to comment.