Skip to content

Commit

Permalink
Added with_function_table helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Apr 8, 2024
1 parent 65bd0f6 commit 259864e
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 231 deletions.
58 changes: 25 additions & 33 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use parking_lot::Mutex;
use refuse::Trace;

use crate::symbol::Symbol;
use crate::value::{
CustomType, Dynamic, Rooted, RustType, StaticRustFunctionTable, TypeRef, Value,
};
use crate::value::{CustomType, Dynamic, Rooted, RustFunctionTable, RustType, TypeRef, Value};
use crate::vm::{Fault, Register, VmContext};

pub static LIST_TYPE: RustType<List> = RustType::new("List", |t| {
Expand All @@ -18,36 +16,30 @@ pub static LIST_TYPE: RustType<List> = RustType::new("List", |t| {
Ok(Dynamic::new(list, vm))
}
})
.with_invoke(|_| {
|this, vm, name, arity| {
static FUNCTIONS: StaticRustFunctionTable<List> =
StaticRustFunctionTable::new(|table| {
table
.with_fn(
Symbol::len_symbol(),
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<List>| {
Value::try_from(this.0.lock().len())
},
)
.with_fn(Symbol::set_symbol(), 2, |vm, this| {
let index = vm[Register(0)].take();
let value = vm[Register(1)].take();
this.set(&index, value)?;
Ok(value)
})
.with_fn(
[Symbol::nth_symbol(), Symbol::get_symbol()],
1,
|vm, this| {
let key = vm[Register(0)].take();
this.get(&key)
},
)
});
FUNCTIONS.invoke(vm, name, arity, &this)
}
})
.with_function_table(
RustFunctionTable::new()
.with_fn(
Symbol::len_symbol(),
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<List>| {
Value::try_from(this.0.lock().len())
},
)
.with_fn(Symbol::set_symbol(), 2, |vm, this| {
let index = vm[Register(0)].take();
let value = vm[Register(1)].take();
this.set(&index, value)?;
Ok(value)
})
.with_fn(
[Symbol::nth_symbol(), Symbol::get_symbol()],
1,
|vm, this| {
let key = vm[Register(0)].take();
this.get(&key)
},
),
)
});

#[derive(Debug)]
Expand Down
60 changes: 27 additions & 33 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use refuse::{CollectionGuard, Trace};
use crate::list::List;
use crate::symbol::Symbol;
use crate::value::{
ContextOrGuard, CustomType, Dynamic, RustType, StaticRustFunctionTable, TypeRef, Value,
ContextOrGuard, CustomType, Dynamic, RustFunctionTable, RustType, TypeRef, Value,
};
use crate::vm::{Fault, Register, VmContext};

Expand All @@ -25,38 +25,32 @@ pub static MAP_TYPE: RustType<Map> = RustType::new("Map", |t| {
Ok(Dynamic::new(map, vm))
}
})
.with_invoke(|_| {
|this, vm, name, arity| {
static FUNCTIONS: StaticRustFunctionTable<Map> =
StaticRustFunctionTable::new(|table| {
table
.with_fn(Symbol::set_symbol(), 1, |vm, this| {
let key = vm[Register(0)].take();
this.insert(vm, key, key)?;
Ok(key)
})
.with_fn(Symbol::set_symbol(), 2, |vm, this| {
let key = vm[Register(0)].take();
let value = vm[Register(1)].take();
this.insert(vm, key, value)?;
Ok(value)
})
.with_fn(Symbol::get_symbol(), 1, |vm, this| {
let key = vm[Register(0)].take();
this.get(vm, &key)?.ok_or(Fault::OutOfBounds)
})
.with_fn(Symbol::nth_symbol(), 1, |vm, this| {
let index = vm[Register(0)].take();
this.nth(&index, vm.as_ref())
})
.with_fn(Symbol::len_symbol(), 0, |_vm, this| {
let contents = this.0.lock();
Value::try_from(contents.len())
})
});
FUNCTIONS.invoke(vm, name, arity, &this)
}
})
.with_function_table(
RustFunctionTable::<Map>::new()
.with_fn(Symbol::set_symbol(), 1, |vm, this| {
let key = vm[Register(0)].take();
this.insert(vm, key, key)?;
Ok(key)
})
.with_fn(Symbol::set_symbol(), 2, |vm, this| {
let key = vm[Register(0)].take();
let value = vm[Register(1)].take();
this.insert(vm, key, value)?;
Ok(value)
})
.with_fn(Symbol::get_symbol(), 1, |vm, this| {
let key = vm[Register(0)].take();
this.get(vm, &key)?.ok_or(Fault::OutOfBounds)
})
.with_fn(Symbol::nth_symbol(), 1, |vm, this| {
let index = vm[Register(0)].take();
this.nth(&index, vm.as_ref())
})
.with_fn(Symbol::len_symbol(), 0, |_vm, this| {
let contents = this.0.lock();
Value::try_from(contents.len())
}),
)
});

impl Trace for Map {
Expand Down
181 changes: 79 additions & 102 deletions src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use regex::{Captures, Regex};
use crate::string::MuseString;
use crate::symbol::{Symbol, SymbolRef};
use crate::syntax::token::RegexLiteral;
use crate::value::{
AnyDynamic, CustomType, Rooted, RustType, StaticRustFunctionTable, TypeRef, Value,
};
use crate::value::{AnyDynamic, CustomType, Rooted, RustFunctionTable, RustType, TypeRef, Value};
use crate::vm::{Fault, Register, VmContext};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -61,45 +59,39 @@ impl Deref for MuseRegex {
impl CustomType for MuseRegex {
fn muse_type(&self) -> &TypeRef {
static TYPE: RustType<MuseRegex> = RustType::new("Regex", |t| {
t.with_invoke(|_| {
|this, vm, name, arity| {
static FUNCTIONS: StaticRustFunctionTable<MuseRegex> =
StaticRustFunctionTable::new(|table| {
table
.with_fn(
"total_captures",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
Value::try_from(this.captures_len())
},
)
.with_fn(
"total_static_captures",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
Ok(this
.static_captures_len()
.map(Value::try_from)
.transpose()?
.unwrap_or_default())
},
)
.with_fn(
Symbol::captures_symbol(),
1,
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
let haystack = vm[Register(0)].take();
haystack.map_str(vm, |vm, haystack| {
this.captures(haystack, vm.as_ref())
.map(|value| Value::dynamic(value, vm))
.unwrap_or_default()
})
},
)
});
FUNCTIONS.invoke(vm, name, arity, &this)
}
})
t.with_function_table(
RustFunctionTable::new()
.with_fn(
"total_captures",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
Value::try_from(this.captures_len())
},
)
.with_fn(
"total_static_captures",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
Ok(this
.static_captures_len()
.map(Value::try_from)
.transpose()?
.unwrap_or_default())
},
)
.with_fn(
Symbol::captures_symbol(),
1,
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseRegex>| {
let haystack = vm[Register(0)].take();
haystack.map_str(vm, |vm, haystack| {
this.captures(haystack, vm.as_ref())
.map(|value| Value::dynamic(value, vm))
.unwrap_or_default()
})
},
),
)
.with_hash(|_| {
|this, _vm, hasher| {
this.expr.as_str().hash(hasher);
Expand Down Expand Up @@ -202,43 +194,34 @@ impl MuseCaptures {
impl CustomType for MuseCaptures {
fn muse_type(&self) -> &TypeRef {
static TYPE: RustType<MuseCaptures> = RustType::new("RegexCaptures", |t| {
t.with_invoke(|_| {
|this, vm, name, arity| {
static FUNCTIONS: StaticRustFunctionTable<MuseCaptures> =
StaticRustFunctionTable::new(|table| {
table
.with_fn(
Symbol::get_symbol(),
1,
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
let index = vm[Register(0)].take();
let index = if let Some(index) = index.as_usize() {
index
} else {
let name =
index.to_string(vm)?.try_upgrade(vm.guard())?;
let Some(index) = this.by_name.get(&name).copied()
else {
return Ok(Value::Nil);
};
index
};
this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
},
)
.with_fn(
Symbol::nth_symbol(),
1,
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
let index =
vm[Register(0)].as_usize().ok_or(Fault::OutOfBounds)?;
this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
},
)
});
FUNCTIONS.invoke(vm, name, arity, &this)
}
})
t.with_function_table(
RustFunctionTable::new()
.with_fn(
Symbol::get_symbol(),
1,
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
let index = vm[Register(0)].take();
let index = if let Some(index) = index.as_usize() {
index
} else {
let name = index.to_string(vm)?.try_upgrade(vm.guard())?;
let Some(index) = this.by_name.get(&name).copied() else {
return Ok(Value::Nil);
};
index
};
this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
},
)
.with_fn(
Symbol::nth_symbol(),
1,
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
let index = vm[Register(0)].as_usize().ok_or(Fault::OutOfBounds)?;
this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
},
),
)
});
&TYPE
}
Expand All @@ -253,29 +236,23 @@ pub struct MuseMatch {
impl CustomType for MuseMatch {
fn muse_type(&self) -> &TypeRef {
static TYPE: RustType<MuseMatch> = RustType::new("RegexMatch", |t| {
t.with_invoke(|_| {
|this, vm, name, arity| {
static FUNCTIONS: StaticRustFunctionTable<MuseMatch> =
StaticRustFunctionTable::new(|table| {
table
.with_fn(
"content",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseMatch>| {
Ok(Value::Dynamic(this.content))
},
)
.with_fn(
"start",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseMatch>| {
Value::try_from(this.start)
},
)
});
FUNCTIONS.invoke(vm, name, arity, &this)
}
})
t.with_function_table(
RustFunctionTable::new()
.with_fn(
"content",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseMatch>| {
Ok(Value::Dynamic(this.content))
},
)
.with_fn(
"start",
0,
|_vm: &mut VmContext<'_, '_>, this: &Rooted<MuseMatch>| {
Value::try_from(this.start)
},
),
)
.with_to_string(|_| |this, vm| this.content.to_string(vm))
});
&TYPE
Expand Down
Loading

0 comments on commit 259864e

Please sign in to comment.