Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

19 changes: 13 additions & 6 deletions crates/clazel-js-builtins/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,15 @@ fn define_properties(vm: &mut Vm, obj: Value, props: Value) -> Result<(), Abrupt
/// A property descriptor for a data property (§6.2.6†): each field present or
/// absent.
#[derive(Clone, Copy, Debug)]
struct Descriptor {
value: Option<Value>,
writable: Option<bool>,
enumerable: Option<bool>,
configurable: Option<bool>,
pub struct Descriptor {
/// `[[Value]]`.
pub value: Option<Value>,
/// `[[Writable]]`.
pub writable: Option<bool>,
/// `[[Enumerable]]`.
pub enumerable: Option<bool>,
/// `[[Configurable]]`.
pub configurable: Option<bool>,
}

/// ToPropertyDescriptor ( Obj ), §6.2.6.5†:
Expand Down Expand Up @@ -272,7 +276,10 @@ fn define_property(vm: &mut Vm, _this: Value, args: &[Value]) -> Result<Value, A
/// return false; if Desc has [[Value]] and not SameValue, return false.
/// 6. The fields present in Desc replace those of the property.
/// A false result is a TypeError.
fn define_property_or_throw(
///
/// # Errors
/// A `TypeError` when the definition is refused, [`Abrupt::OutOfMemory`].
pub fn define_property_or_throw(
vm: &mut Vm,
obj: Value,
k: clazel_js_heap::string::Key,
Expand Down
27 changes: 27 additions & 0 deletions crates/clazel-js-vm/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use crate::numeric::{self, Num, num};
use clazel_js_heap::shape::Attrs;
use clazel_js_heap::string::{JsString, Key};
use clazel_js_heap::value::Value;
use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -283,6 +284,8 @@ pub struct Vm {
pub(crate) terminator: Terminator,
pub(crate) budget: u64,
pub(crate) atoms: Atoms,
/// The embedder's state (`clazel-js`'s runtime), see [`Vm::set_embedder`].
pub(crate) embedder: Option<Box<dyn Any + Send>>,
}

impl Vm {
Expand Down Expand Up @@ -349,6 +352,7 @@ impl Vm {
terminator: Terminator::new(),
budget: u64::MAX,
atoms,
embedder: None,
})
}

Expand All @@ -359,6 +363,29 @@ impl Vm {
self.terminator.clone()
}

/// Stores the embedder's own state in the VM, replacing what was there.
///
/// A [`NativeFn`] is a plain function pointer that gets only `&mut Vm`, so
/// an embedder whose natives are closures (`clazel-js`) keeps the table of
/// closures here and finds it again from inside the native. The VM never
/// looks at it. Dropping the VM drops it.
pub fn set_embedder<T: Any + Send>(&mut self, data: T) {
self.embedder = Some(Box::new(data));
}

/// The embedder's state, if it is a `T`.
#[must_use]
pub fn embedder<T: Any>(&self) -> Option<&T> {
self.embedder.as_deref().and_then(|e| e.downcast_ref::<T>())
}

/// The embedder's state, mutably, if it is a `T`.
pub fn embedder_mut<T: Any>(&mut self) -> Option<&mut T> {
self.embedder
.as_deref_mut()
.and_then(|e| e.downcast_mut::<T>())
}

/// Bounds the back-edges and calls the VM may still make; when spent, a
/// run ends as [`Abrupt::Terminated`].
pub fn set_budget(&mut self, steps: u64) {
Expand Down
25 changes: 25 additions & 0 deletions crates/clazel-js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "clazel-js"
version = "0.0.0"
edition.workspace = true
license.workspace = true
publish.workspace = true
description = "The JavaScript engine's embedding API for Clazel (phase-7.md item 7.8): Runtime, realms, scripts and modules, native functions, host objects, rooted handles, the Host trait, limits and the Terminator. This is the contract only; nothing here is implemented yet."

# Engine crates only (`cargo xtask check` enforces that no engine crate reaches
# a browser crate; see ENGINE in xtask/src/status.rs): the parser and compiler
# for scripts, the interpreter, the built-ins, and the heap's value type.
[dependencies]
clazel-js-builtins = { path = "../clazel-js-builtins" }
clazel-js-heap = { path = "../clazel-js-heap" }
clazel-js-syntax = { path = "../clazel-js-syntax" }
clazel-js-vm = { path = "../clazel-js-vm" }

# Verification tier (docs/VERIFICATION.md). Tier 0: this crate holds the
# contract and its stubs, and nothing is proven about it.
[package.metadata.clazel]
tier = 0
target-tier = 0

[lints]
workspace = true
Loading
Loading