pub struct Thread { /* private fields */ }
Expand description
Representation of the virtual machine
Implementations
sourceimpl Thread
impl Thread
sourcepub fn new_thread(&self) -> Result<RootedThread>
pub fn new_thread(&self) -> Result<RootedThread>
Spawns a new gluon thread with its own stack and heap but while still sharing the same global environment
sourcepub fn root_thread(&self) -> RootedThread
pub fn root_thread(&self) -> RootedThread
Roots self
, extending the lifetime of this thread until at least the returned
RootedThread
is droppped
sourcepub fn define_global<'vm, T>(&'vm self, name: &str, value: T) -> Result<()>where
T: Pushable<'vm> + VmType,
👎Deprecated since 0.7.0: Use gluon::import::add_extern_module
instead
pub fn define_global<'vm, T>(&'vm self, name: &str, value: T) -> Result<()>where
T: Pushable<'vm> + VmType,
gluon::import::add_extern_module
insteadCreates a new global value at name
.
Fails if a global called name
already exists.
Examples
Load the factorial
rust function into gluon and evaluate factorial 5
fn factorial(x: i32) -> i32 {
if x <= 1 { 1 } else { x * factorial(x - 1) }
}
let vm = new_vm();
vm.define_global("factorial", primitive!(1, factorial)).unwrap();
let result = Compiler::new()
.run_expr::<i32>(&vm, "example", "factorial 5")
.unwrap_or_else(|err| panic!("{}", err));
let expected = (120, Type::int());
assert_eq!(result, expected);
sourcepub fn get_global<'vm, T>(&'vm self, name: &str) -> Result<T>where
T: for<'value> Getable<'vm, 'value> + VmType,
pub fn get_global<'vm, T>(&'vm self, name: &str) -> Result<T>where
T: for<'value> Getable<'vm, 'value> + VmType,
Retrieves the global called name
.
Examples
Bind the (+)
function in gluon’s prelude standard library
to an add
function in rust
let vm = new_vm();
Compiler::new()
.run_expr::<OpaqueValue<&Thread, Hole>>(&vm, "example",
r#" import! std.int "#)
.unwrap_or_else(|err| panic!("{}", err));
let mut add: FunctionRef<fn(i32, i32) -> i32> =
vm.get_global("std.int.num.(+)").unwrap();
let result = add.call(1, 2);
assert_eq!(result, Ok(3));
Errors
if the global does not exist or it does not have the correct type.
pub fn get_global_type(&self, name: &str) -> Result<ArcType>
sourcepub fn find_type_info(&self, name: &str) -> Result<Alias<Symbol, ArcType>>
pub fn find_type_info(&self, name: &str) -> Result<Alias<Symbol, ArcType>>
Retrieves type information about the type name
. Types inside records can be accessed
using dot notation (std.prelude.Option)
sourcepub fn get_type<T: ?Sized + Any>(&self) -> Option<ArcType>
pub fn get_type<T: ?Sized + Any>(&self) -> Option<ArcType>
Returns the gluon type that was bound to T
sourcepub fn register_type<T: ?Sized + Any>(
&self,
name: &str,
args: &[&str]
) -> Result<ArcType>
pub fn register_type<T: ?Sized + Any>(
&self,
name: &str,
args: &[&str]
) -> Result<ArcType>
Registers the type T
as being a gluon type called name
with generic arguments args
pub fn register_type_as(
&self,
name: Symbol,
alias: Alias<Symbol, ArcType>,
id: TypeId
) -> Result<ArcType>
sourcepub fn get_env<'b>(&'b self) -> RwLockReadGuard<'b, VmEnv>
pub fn get_env<'b>(&'b self) -> RwLockReadGuard<'b, VmEnv>
Locks and retrieves the global environment of the vm
sourcepub fn get_macros(&self) -> &MacroEnv
pub fn get_macros(&self) -> &MacroEnv
Retrieves the macros defined for this vm
sourcepub fn push<'vm, T>(&'vm self, v: T) -> Result<()>where
T: Pushable<'vm>,
pub fn push<'vm, T>(&'vm self, v: T) -> Result<()>where
T: Pushable<'vm>,
Pushes a value to the top of the stack
pub fn allocated_memory(&self) -> usize
pub fn set_memory_limit(&self, memory_limit: usize)
pub fn interrupt(&self)
pub fn interrupted(&self) -> bool
pub fn current_context(&self) -> ActiveThread<'_>
Trait Implementations
sourceimpl ThreadInternal for Thread
impl ThreadInternal for Thread
sourcefn root_value<'vm, T>(&'vm self, value: Variants<'_>) -> RootedValue<T>where
T: VmRoot<'vm>,
fn root_value<'vm, T>(&'vm self, value: Variants<'_>) -> RootedValue<T>where
T: VmRoot<'vm>,
Roots a value
sourcefn execute_io<'vm>(
&'vm self,
value: Variants<'_>
) -> FutureValue<Execute<RootedThread>>
fn execute_io<'vm>(
&'vm self,
value: Variants<'_>
) -> FutureValue<Execute<RootedThread>>
Calls a module, allowed to to run IO expressions
sourcefn call_function<'b>(
&'b self,
context: OwnedContext<'b>,
args: VmIndex
) -> Result<Async<Option<OwnedContext<'b>>>>
fn call_function<'b>(
&'b self,
context: OwnedContext<'b>,
args: VmIndex
) -> Result<Async<Option<OwnedContext<'b>>>>
Calls a function on the stack.
When this function is called it is expected that the function exists at
stack.len() - args - 1
and that the arguments are of the correct type
sourcefn context(&self) -> OwnedContext<'_>
fn context(&self) -> OwnedContext<'_>
sourcefn call_thunk<'vm>(
&'vm self,
closure: GcPtr<ClosureData>
) -> FutureValue<Execute<RootedThread>>
fn call_thunk<'vm>(
&'vm self,
closure: GcPtr<ClosureData>
) -> FutureValue<Execute<RootedThread>>
fn resume(&self) -> Result<Async<OwnedContext<'_>>>
fn set_global(
&self,
name: Symbol,
typ: ArcType,
metadata: Metadata,
value: Value
) -> Result<()>
sourcefn deep_clone_value(&self, owner: &Thread, value: Variants<'_>) -> Result<Value>
fn deep_clone_value(&self, owner: &Thread, value: Variants<'_>) -> Result<Value>
owner
is theread that owns value
which is not necessarily the same as self
fn call_thunk_top<'vm>(
&'vm self,
closure: GcPtr<ClosureData>
) -> BoxFuture<'static, RootedValue<RootedThread>, Error>where
Self: Send + Sync,
fn execute_io_top<'vm>(
&'vm self,
value: Variants<'_>
) -> BoxFuture<'static, RootedValue<RootedThread>, Error>where
Self: Send + Sync,
sourceimpl<'a> VmRoot<'a> for &'a Thread
impl<'a> VmRoot<'a> for &'a Thread
fn root(thread: &'a Thread) -> Self
sourcefn root_value_with_self(self, value: Variants<'_>) -> RootedValue<Self>
fn root_value_with_self(self, value: Variants<'_>) -> RootedValue<Self>
sourceimpl VmType for Thread
impl VmType for Thread
fn make_forall_type(vm: &Thread) -> ArcType
sourcefn extra_args() -> VmIndex
fn extra_args() -> VmIndex
Auto Trait Implementations
impl RefUnwindSafe for Thread
impl Send for Thread
impl Sync for Thread
impl Unpin for Thread
impl UnwindSafe for Thread
Blanket Implementations
sourceimpl<'vm, T> AsyncPushable<'vm> for Twhere
T: Pushable<'vm>,
impl<'vm, T> AsyncPushable<'vm> for Twhere
T: Pushable<'vm>,
sourcefn async_push(
self,
context: &mut ActiveThread<'vm>,
_: u32
) -> Result<Async<()>, Error>
fn async_push(
self,
context: &mut ActiveThread<'vm>,
_: u32
) -> Result<Async<()>, Error>
self
to stack
. If the call is successful a single element should have been added
to the stack and Ok(())
should be returned. If the call is unsuccessful Status:Error
should be returned and the stack should be left intact. Read morefn async_status_push(
self,
context: &mut ActiveThread<'vm>,
frame_index: VmIndex
) -> Statuswhere
Self: Sized,
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<Choices> CoproductSubsetter<CNil, HNil> for Choices
impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
sourceimpl<'vm, T> Pushable<'vm> for Twhere
T: Userdata,
impl<'vm, T> Pushable<'vm> for Twhere
T: Userdata,
sourcefn push(self, context: &mut ActiveThread<'vm>) -> Result<(), Error>
fn push(self, context: &mut ActiveThread<'vm>) -> Result<(), Error>
self
to stack
. If the call is successful a single element should have been added
to the stack and Ok(())
should be returned. If the call is unsuccessful Status:Error
should be returned and the stack should be left intact Read more