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 --stdinstalls at startup, holding aContext— that is what every host function value is created through;import * as std from "std"installs inside a running interpreter, becausemodule::ModuleLoader::load_nativeis handed&mut Interpand nothing else. AContextcannot be conjured from anInterp: theContextowns 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§
Sourcefn new_host_function_value(
&mut self,
name: &str,
length: u32,
f: HostFn,
) -> Result<Value>
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.
Sourcefn new_string(&mut self, text: &str) -> Value
fn new_string(&mut self, text: &str) -> Value
A string value.
Sourcefn new_plain_object(&mut self) -> Value
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.
Sourcefn new_object_with_prototype(&mut self, proto: &Value) -> Result<Value>
fn new_object_with_prototype(&mut self, proto: &Value) -> Result<Value>
A fresh empty object whose prototype is proto.
Sourcefn global(&mut self) -> Value
fn global(&mut self) -> Value
The global object — where a host module parks what its closures cannot capture.
Define name on object as a non-enumerable data property.
Sourcefn set_property(
&mut self,
object: &Value,
name: &str,
value: Value,
) -> Result<()>
fn set_property( &mut self, object: &Value, name: &str, value: Value, ) -> Result<()>
Define name on object as an enumerable data property.
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".