Skip to main content

HostSurface

Trait HostSurface 

Source
pub trait HostSurface {
    // Required methods
    fn new_host_function_value(
        &mut self,
        name: &str,
        length: u32,
        f: HostFn,
    ) -> Result<Value>;
    fn new_string(&mut self, text: &str) -> Value;
    fn new_plain_object(&mut self) -> Value;
    fn new_object_with_prototype(&mut self, proto: &Value) -> Result<Value>;
    fn global(&mut self) -> Value;
    fn define_hidden(
        &mut self,
        object: &Value,
        name: &str,
        value: Value,
    ) -> Result<()>;
    fn set_property(
        &mut self,
        object: &Value,
        name: &str,
        value: Value,
    ) -> Result<()>;
    fn hidden(&mut self, object: &Value, name: &str) -> Option<Value>;
}
Expand description

What a host module needs from the engine, whichever way the engine was reached.

A host module (std, os) is installed from two different places, and until this trait existed the two could not share one implementation:

  • qjs --std installs at startup, holding a Context — that is what every host function value is created through;
  • import * as std from "std" installs inside a running interpreter, because module::ModuleLoader::load_native is handed &mut Interp and nothing else. A Context cannot be conjured from an Interp: the Context owns the interpreter.

So the handful of operations a host module actually performs are named here, and both types provide them. Keeping the five methods rather than “just call the interpreter” preserves the reason Context’s host surface exists at all: a HostFn closure may not capture a Gc, so host code reaches shared values through the global object, and that is one of the five.

Required Methods§

Source

fn new_host_function_value( &mut self, name: &str, length: u32, f: HostFn, ) -> Result<Value>

A host function as a value, for putting on an object rather than the global.

Source

fn new_string(&mut self, text: &str) -> Value

A string value.

Source

fn new_plain_object(&mut self) -> Value

A fresh empty object whose prototype is Object.prototype.

Named new_plain_object rather than new_object because Interp already has a new_object(Option<Gc<JsObject>>); two methods of the same name on one type would make every call site ambiguous.

Source

fn new_object_with_prototype(&mut self, proto: &Value) -> Result<Value>

A fresh empty object whose prototype is proto.

Source

fn global(&mut self) -> Value

The global object — where a host module parks what its closures cannot capture.

Source

fn define_hidden( &mut self, object: &Value, name: &str, value: Value, ) -> Result<()>

Define name on object as a non-enumerable data property.

Source

fn set_property( &mut self, object: &Value, name: &str, value: Value, ) -> Result<()>

Define name on object as an enumerable data property.

Source

fn hidden(&mut self, object: &Value, name: &str) -> Option<Value>

The own data property name of object, if it has one.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§