Skip to content

Commit

Permalink
add name property to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Oct 29, 2024
1 parent 971ec12 commit bc61769
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 43 deletions.
6 changes: 3 additions & 3 deletions crates/dash_vm/src/js_std/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::mem;

use crate::dispatch::HandleResult;
use crate::frame::Frame;
use dash_middle::interner::sym;
use crate::localscope::LocalScope;
use crate::throw;
use crate::value::function::generator::{GeneratorIterator, GeneratorState};
Expand All @@ -11,6 +10,7 @@ use crate::value::function::{Function, FunctionKind};
use crate::value::object::{NamedObject, Object, PropertyValue};
use crate::value::root_ext::RootErrExt;
use crate::value::{Root, Unpack, Value, ValueContext};
use dash_middle::interner::sym;

pub fn next(cx: CallContext) -> Result<Value, Value> {
let generator = cx.this.unpack();
Expand All @@ -36,8 +36,8 @@ pub fn next(cx: CallContext) -> Result<Value, Value> {
.downcast_ref::<Function>()
.map(|fun| fun.kind())
{
Some(FunctionKind::Generator(gen)) => gen.function(),
Some(FunctionKind::Async(fun)) => fun.inner().function(),
Some(FunctionKind::Generator(gen)) => &gen.function,
Some(FunctionKind::Async(fun)) => &fun.inner.function,
_ => throw!(cx.scope, TypeError, "Incompatible generator function"),
};

Expand Down
6 changes: 1 addition & 5 deletions crates/dash_vm/src/value/function/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::user::UserFunction;
#[derive(Debug, Trace)]
pub struct AsyncFunction {
/// The properties of generator functions are very similar to async functions, so we can build upon generators
inner: GeneratorFunction,
pub inner: GeneratorFunction,
}

impl AsyncFunction {
Expand Down Expand Up @@ -92,10 +92,6 @@ impl AsyncFunction {
}
}
}

pub fn inner(&self) -> &GeneratorFunction {
&self.inner
}
}

/// A callable object that is passed to `.then()` on awaited promises.
Expand Down
6 changes: 1 addition & 5 deletions crates/dash_vm/src/value/function/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ use super::user::UserFunction;

#[derive(Debug, Trace)]
pub struct GeneratorFunction {
function: UserFunction,
pub function: UserFunction,
}

impl GeneratorFunction {
pub fn new(function: UserFunction) -> Self {
Self { function }
}

pub fn function(&self) -> &UserFunction {
&self.function
}

pub(crate) fn handle_function_call(
&self,
scope: &mut LocalScope,
Expand Down
48 changes: 18 additions & 30 deletions crates/dash_vm/src/value/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,6 @@ unsafe impl Trace for FunctionKind {
}
}

impl FunctionKind {
pub fn as_native(&self) -> Option<&NativeFunction> {
match self {
Self::Native(f) => Some(f),
_ => None,
}
}

pub fn as_user(&self) -> Option<&UserFunction> {
match self {
Self::User(f) => Some(f),
_ => None,
}
}

pub fn as_generator(&self) -> Option<&GeneratorFunction> {
match self {
Self::Generator(f) => Some(f),
_ => None,
}
}

pub fn as_async(&self) -> Option<&AsyncFunction> {
match self {
Self::Async(f) => Some(f),
_ => None,
}
}
}

impl fmt::Debug for FunctionKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down Expand Up @@ -154,6 +124,16 @@ impl Function {
let this = scope.register(NamedObject::with_prototype_and_constructor(prototype, this_handle));
Ok(this)
}

pub fn inner_user_function(&self) -> Option<&UserFunction> {
match &self.kind {
FunctionKind::User(function) => Some(function),
FunctionKind::Generator(generator) => Some(&generator.function),
FunctionKind::Async(function) => Some(&function.inner.function),
FunctionKind::Closure(closure) => Some(&closure.fun),
FunctionKind::Native(_) => None,
}
}
}

fn handle_call(
Expand Down Expand Up @@ -207,6 +187,14 @@ impl Object for Function {
descriptor: PropertyDataDescriptor::CONFIGURABLE,
}));
}
sym::length => {
if let Some(function) = self.inner_user_function() {
return Ok(Some(PropertyValue {
kind: PropertyValueKind::Static(Value::number(function.inner().params as f64)),
descriptor: PropertyDataDescriptor::CONFIGURABLE,
}));
}
}
sym::prototype => {
let prototype = self.get_or_set_prototype(sc);
return Ok(Some(PropertyValue::static_empty(Value::object(prototype))));
Expand Down

0 comments on commit bc61769

Please sign in to comment.