pub struct Thread { /* private fields */ }
Expand description
Representation of the virtual machine
Implementations
sourceimpl Thread
impl Thread
sourcepub fn new_thread(&self) -> Result<RootedThread, Error>
pub fn new_thread(&self) -> Result<RootedThread, Error>
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<(), Error>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<(), Error>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, Error>where
T: for<'value> Getable<'vm, 'value> + VmType,
pub fn get_global<'vm, T>(&'vm self, name: &str) -> Result<T, Error>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<Symbol>, Error>
sourcepub fn find_type_info(
&self,
name: &str
) -> Result<Alias<Symbol, ArcType<Symbol>>, Error>
pub fn find_type_info(
&self,
name: &str
) -> Result<Alias<Symbol, ArcType<Symbol>>, Error>
Retrieves type information about the type name
. Types inside records can be accessed
using dot notation (std.prelude.Option)
sourcepub fn get_type<T>(&self) -> Option<ArcType<Symbol>>where
T: Any + ?Sized,
pub fn get_type<T>(&self) -> Option<ArcType<Symbol>>where
T: Any + ?Sized,
Returns the gluon type that was bound to T
sourcepub fn register_type<T>(
&self,
name: &str,
args: &[&str]
) -> Result<ArcType<Symbol>, Error>where
T: Any + ?Sized,
pub fn register_type<T>(
&self,
name: &str,
args: &[&str]
) -> Result<ArcType<Symbol>, Error>where
T: Any + ?Sized,
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<Symbol>>,
id: TypeId
) -> Result<ArcType<Symbol>, Error>
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<(), Error>where
T: Pushable<'vm>,
pub fn push<'vm, T>(&'vm self, v: T) -> Result<(), Error>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<'de> DeserializeState<'de, DeSeed> for Thread
impl<'de> DeserializeState<'de, DeSeed> for Thread
sourcefn deserialize_state<__D>(
__seed: &mut DeSeed,
__deserializer: __D
) -> Result<Thread, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize_state<__D>(
__seed: &mut DeSeed,
__deserializer: __D
) -> Result<Thread, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Self
using seed
and the deserializer
sourceimpl SerializeState<SeSeed> for Thread
impl SerializeState<SeSeed> for Thread
sourcefn serialize_state<__S>(
&self,
__serializer: __S,
__seed: &SeSeed
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize_state<__S>(
&self,
__serializer: __S,
__seed: &SeSeed
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
self
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<'_>
) -> Either<FutureResult<<Execute<RootedThread> as Future>::Item, <Execute<RootedThread> as Future>::Error>, Execute<RootedThread>>
fn execute_io<'vm>(
&'vm self,
value: Variants<'_>
) -> Either<FutureResult<<Execute<RootedThread> as Future>::Item, <Execute<RootedThread> as Future>::Error>, Execute<RootedThread>>
Calls a module, allowed to to run IO expressions
sourcefn call_function<'b>(
&'b self,
context: OwnedContext<'b>,
args: u32
) -> Result<Async<Option<OwnedContext<'b>>>, Error>
fn call_function<'b>(
&'b self,
context: OwnedContext<'b>,
args: u32
) -> Result<Async<Option<OwnedContext<'b>>>, Error>
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>
) -> Either<FutureResult<<Execute<RootedThread> as Future>::Item, <Execute<RootedThread> as Future>::Error>, Execute<RootedThread>>
fn call_thunk<'vm>(
&'vm self,
closure: GcPtr<ClosureData>
) -> Either<FutureResult<<Execute<RootedThread> as Future>::Item, <Execute<RootedThread> as Future>::Error>, Execute<RootedThread>>
fn resume(&self) -> Result<Async<OwnedContext<'_>>, Error>
fn set_global(
&self,
name: Symbol,
typ: ArcType<Symbol>,
metadata: Metadata,
value: Value
) -> Result<(), Error>
sourcefn deep_clone_value(
&self,
owner: &Thread,
value: Variants<'_>
) -> Result<Value, Error>
fn deep_clone_value(
&self,
owner: &Thread,
value: Variants<'_>
) -> Result<Value, Error>
owner
is theread that owns value
which is not necessarily the same as self
fn call_thunk_top<'vm>(
&'vm self,
closure: GcPtr<ClosureData>
) -> Box<dyn Future<Error = Error, Item = RootedValue<RootedThread>> + Send + 'static, Global>where
Self: Send + Sync,
fn execute_io_top<'vm>(
&'vm self,
value: Variants<'_>
) -> Box<dyn Future<Error = Error, Item = RootedValue<RootedThread>> + Send + 'static, Global>where
Self: Send + Sync,
sourceimpl<'a> VmRoot<'a> for &'a Thread
impl<'a> VmRoot<'a> for &'a Thread
fn root(thread: &'a Thread) -> &'a Thread
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<Symbol>
sourcefn extra_args() -> u32
fn extra_args() -> u32
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
impl<T> Any for Twhere
T: Any,
impl<T> Any for Twhere
T: Any,
fn get_type_id(&self) -> TypeId
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: u32
) -> 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
impl<T, U, I> LiftInto<U, I> for Twhere
U: LiftFrom<T, I>,
impl<T, U, I> LiftInto<U, I> for Twhere
U: LiftFrom<T, I>,
fn lift_into(self) -> U
fn lift_into(self) -> U
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