[][src]Struct napi::Env

pub struct Env(_);

Env is used to represent a context that the underlying N-API implementation can use to persist VM-specific state. This structure is passed to native functions when they're invoked, and it must be passed back when making N-API calls. Specifically, the same Env that was passed in when the initial native function was called must be passed to any subsequent nested N-API calls. Caching the Env for the purpose of general reuse, and passing the Env between instances of the same addon running on different Worker threads is not allowed. The Env becomes invalid when an instance of a native addon is unloaded. Notification of this event is delivered through the callbacks given to Env::add_env_cleanup_hook and Env::set_instance_data.

Implementations

impl Env[src]

pub unsafe fn from_raw(env: napi_env) -> Self[src]

pub fn get_undefined(&self) -> Result<JsUndefined>[src]

pub fn get_null(&self) -> Result<JsNull>[src]

pub fn get_boolean(&self, value: bool) -> Result<JsBoolean>[src]

pub fn create_int32(&self, int: i32) -> Result<JsNumber>[src]

pub fn create_int64(&self, int: i64) -> Result<JsNumber>[src]

pub fn create_uint32(&self, number: u32) -> Result<JsNumber>[src]

pub fn create_double(&self, double: f64) -> Result<JsNumber>[src]

pub fn create_bigint_from_i64(&self, value: i64) -> Result<JsBigint>[src]

pub fn create_bigint_from_u64(&self, value: u64) -> Result<JsBigint>[src]

pub fn create_bigint_from_i128(&self, value: i128) -> Result<JsBigint>[src]

pub fn create_bigint_from_u128(&self, value: u128) -> Result<JsBigint>[src]

pub fn create_bigint_from_words(
    &self,
    sign_bit: bool,
    words: Vec<u64>
) -> Result<JsBigint>
[src]

n_api_napi_create_bigint_words The resulting BigInt will be negative when sign_bit is true.

pub fn create_string(&self, s: &str) -> Result<JsString>[src]

pub fn create_string_from_std(&self, s: String) -> Result<JsString>[src]

pub fn create_string_from_vec_u8(&self, bytes: Vec<u8>) -> Result<JsString>[src]

pub fn create_string_from_vec_i8(&self, bytes: Vec<i8>) -> Result<JsString>[src]

pub fn create_string_utf16(&self, chars: &[u16]) -> Result<JsString>[src]

pub fn create_string_latin1(&self, chars: &[u8]) -> Result<JsString>[src]

pub fn create_symbol_from_js_string(
    &self,
    description: JsString
) -> Result<JsSymbol>
[src]

pub fn create_symbol(&self, description: Option<&str>) -> Result<JsSymbol>[src]

pub fn create_object(&self) -> Result<JsObject>[src]

pub fn create_array(&self) -> Result<JsObject>[src]

pub fn create_array_with_length(&self, length: usize) -> Result<JsObject>[src]

pub fn create_buffer(&self, length: usize) -> Result<JsBufferValue>[src]

This API allocates a node::Buffer object. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice.

pub fn create_buffer_with_data(&self, data: Vec<u8>) -> Result<JsBufferValue>[src]

This API allocates a node::Buffer object and initializes it with data backed by the passed in buffer. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice.

pub fn create_buffer_copy<D>(&self, data_to_copy: D) -> Result<JsBufferValue> where
    D: AsRef<[u8]>, 
[src]

This API allocates a node::Buffer object and initializes it with data copied from the passed-in buffer. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice.

pub fn create_arraybuffer(&self, length: usize) -> Result<JsArrayBufferValue>[src]

pub fn create_arraybuffer_with_data(
    &self,
    data: Vec<u8>
) -> Result<JsArrayBufferValue>
[src]

pub fn create_function(
    &self,
    name: &str,
    callback: Callback
) -> Result<JsFunction>
[src]

This API allows an add-on author to create a function object in native code. This is the primary mechanism to allow calling into the add-on's native code from JavaScript. The newly created function is not automatically visible from script after this call. Instead, a property must be explicitly set on any object that is visible to JavaScript, in order for the function to be accessible from script.

pub fn get_last_error_info(&self) -> Result<ExtendedErrorInfo>[src]

This API retrieves a napi_extended_error_info structure with information about the last error that occurred. The content of the napi_extended_error_info returned is only valid up until an n-api function is called on the same env. Do not rely on the content or format of any of the extended information as it is not subject to SemVer and may change at any time. It is intended only for logging purposes. This API can be called even if there is a pending JavaScript exception.

pub fn throw_error(&self, msg: &str, code: Option<&str>) -> Result<()>[src]

This API throws a JavaScript Error with the text provided.

pub fn throw_range_error(&self, msg: &str, code: Option<&str>) -> Result<()>[src]

This API throws a JavaScript RangeError with the text provided.

pub fn throw_type_error(&self, msg: &str, code: Option<&str>) -> Result<()>[src]

This API throws a JavaScript TypeError with the text provided.

pub fn fatal_error(self, location: &str, message: &str)[src]

In the event of an unrecoverable error in a native module A fatal error can be thrown to immediately terminate the process.

pub fn fatal_exception(&self, err: Error)[src]

Trigger an 'uncaughtException' in JavaScript. Useful if an async callback throws an exception with no way to recover.

pub fn define_class(
    &self,
    name: &str,
    constructor_cb: Callback,
    properties: &[Property<'_>]
) -> Result<JsFunction>
[src]

pub fn wrap<T: 'static>(
    &self,
    js_object: &mut JsObject,
    native_object: T
) -> Result<()>
[src]

pub fn unwrap<T: 'static>(&self, js_object: &JsObject) -> Result<&mut T>[src]

pub fn drop_wrapped<T: 'static>(&self, js_object: JsObject) -> Result<()>[src]

pub fn create_external<T: 'static>(
    &self,
    native_object: T
) -> Result<JsExternal>
[src]

pub fn get_value_external<T: 'static>(
    &self,
    js_external: &JsExternal
) -> Result<&mut T>
[src]

pub fn create_error(&self, e: Error) -> Result<JsObject>[src]

pub fn spawn<T: 'static + Task>(&self, task: T) -> Result<AsyncWorkPromise<'_>>[src]

pub fn run_in_scope<T, F>(&self, executor: F) -> Result<T> where
    F: FnOnce() -> Result<T>, 
[src]

pub fn get_global(&self) -> Result<JsGlobal>[src]

pub fn get_napi_version(&self) -> Result<u32>[src]

pub fn get_uv_event_loop(&self) -> Result<*mut uv_loop_s>[src]

pub fn add_env_cleanup_hook<T, F>(
    &mut self,
    cleanup_data: T,
    cleanup_fn: F
) -> Result<CleanupEnvHook<T>> where
    T: 'static,
    F: 'static + FnOnce(T), 
[src]

pub fn remove_env_cleanup_hook<T>(
    &mut self,
    hook: CleanupEnvHook<T>
) -> Result<()> where
    T: 'static, 
[src]

pub fn create_threadsafe_function<T: Send, V: NapiValue, R: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>>(
    &self,
    func: &JsFunction,
    max_queue_size: usize,
    callback: R
) -> Result<ThreadsafeFunction<T>>
[src]

pub fn execute_tokio_future<T: 'static + Send, V: 'static + NapiValue, F: 'static + Send + Future<Output = Result<T>>, R: 'static + Send + Sync + FnOnce(&mut Env, T) -> Result<V>>(
    &self,
    fut: F,
    resolver: R
) -> Result<JsObject>
[src]

pub fn create_date(&self, time: f64) -> Result<JsDate>[src]

pub fn to_js_value<T>(&self, node: &T) -> Result<JsUnknown> where
    T: Serialize
[src]

Serialize Rust Struct into JavaScript Value

#[derive(Serialize, Debug, Deserialize)]
struct AnObject {
  a: u32,
  b: Vec<f64>,
  c: String,
}

#[js_function]
fn serialize(ctx: CallContext) -> Result<JsUnknown> {
  let value = AnyObject { a: 1, b: vec![0.1, 2.22], c: "hello" };
  ctx.env.to_js_value(&value)
}

pub fn from_js_value<T: ?Sized, V>(&self, value: V) -> Result<T> where
    T: DeserializeOwned,
    V: NapiValue
[src]

Deserialize data from JsValue

#[derive(Serialize, Debug, Deserialize)]
struct AnObject {
 a: u32,
 b: Vec<f64>,
 c: String,
}

#[js_function(1)]
fn deserialize_from_js(ctx: CallContext) -> Result<JsUndefined> {
  let arg0 = ctx.get::<JsUnknown>(0)?;
  let de_serialized: AnObject = ctx.env.from_js_value(arg0)?;
  ...
}

pub fn strict_equals<A: NapiValue, B: NapiValue>(
    &self,
    a: A,
    b: B
) -> Result<bool>
[src]

This API represents the invocation of the Strict Equality algorithm as defined in Section 7.2.14 of the ECMAScript Language Specification.

pub fn get_node_version(&self) -> Result<NodeVersion>[src]

pub fn raw(&self) -> napi_env[src]

get raw env ptr

Trait Implementations

impl Clone for Env[src]

impl Copy for Env[src]

Auto Trait Implementations

impl RefUnwindSafe for Env

impl !Send for Env

impl !Sync for Env

impl Unpin for Env

impl UnwindSafe for Env

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.