src/denim/nodeapi

Search:
Group by:
Source   Edit  

Types

AsyncActionStatus = enum
  asyncActionSuccess, asyncActionFail
Source   Edit  
Module = ref object
  val*: napi_value ## The `exports` object that will be returned from the module.
                   ## This is where you will register your functions and properties.
  env*: napi_env ## The NAPI environment variable, used for making NAPI calls.
                 ## This is maintained by various hooks and should not be modified directly.
Module object used for registering and exporting functions and properties to NodeJS. This is the main interface for users of this library. Source   Edit  
NapiErrorType {.pure.} = enum
  napiError, napiTypeError, napiRangeError, napiCustomError
NAPI error types for throwError and error procs Source   Edit  
PromiseData[T] = object
  status*: AsyncActionStatus
  deferred*: napi_deferred
  work*: napi_async_work
  jsData*: T
Source   Edit  
TypedArg = tuple[str, argName, argType: string, argNapiValue: NapiValueType,
                 isOptional: bool]
A tuple type used for documenting function parameters in addDocBlock. Contains the parameter name, type, corresponding NAPI value type, and whether the parameter is optional. Source   Edit  

Vars

Env: napi_env = nil
Global environment variable; state maintained by various hooks; used internally Source   Edit  

Procs

proc `!`(status: NapiStatus) {....raises: [NapiStatusError], tags: [], forbids: [].}
Asserts that a call returns correctly; Source   Edit  
proc `$`(o: napi_value): string {....raises: [NapiStatusError, KeyError,
    ValueError, NapiStatusError, KeyError, ValueError], tags: [], forbids: [].}
Convenient procedure to convert napi_value to string representation Source   Edit  
proc `[]`(obj: napi_value; index: int): napi_value {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Alias for getElement; raises exception Source   Edit  
proc `[]`(obj: napi_value; key: string): napi_value {.
    ...raises: [KeyError, ValueError, NapiStatusError], tags: [], forbids: [].}
Alias for getProperty Source   Edit  
proc `[]=`(obj: napi_value; index: int; value: napi_value) {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Alias for setElement; raises exception Source   Edit  
proc `[]=`(obj: napi_value; key: string; value: napi_value) {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Alias for setProperty, raises exception Source   Edit  
proc addDocBlock(fnName: string;
                 args: openArray[(string, string, NapiValueType, bool)]) {.
    ...raises: [ValueError, KeyError], tags: [], forbids: [].}
Source   Edit  
proc assert(status: NapiStatus) {....raises: [NapiStatusError], tags: [],
                                  forbids: [].}
Asserts that a call returns correctly; Source   Edit  
proc callFunction(fn: napi_value; args: openArray[napi_value] = [];
                  this: napi_value = %*emptyArr): napi_value {.discardable,
    ...raises: [NapiStatusError], tags: [], forbids: [].}
Call a function by napi_value with given args and this context. Source   Edit  
proc callMethod(instance: napi_value; methd: string;
                args: openArray[napi_value] = []): napi_value {.discardable,
    ...raises: [NapiStatusError, KeyError, ValueError], tags: [], forbids: [].}
Call a method from instance by name with given args Source   Edit  
proc create[T](n: Module; t: T): napi_value
Create a new napi_value of T using the Module's environment variable Source   Edit  
proc createFn(env: napi_env; fname: string; cb: napi_callback): napi_value {.
    ...raises: [NapiStatusError], tags: [], forbids: [].}
Create a new function napi_value from a napi_callback. This is used for procs like register where we need to create a napi_value from a callback function. Source   Edit  
proc defineProperties(obj: Module) {....raises: [NapiStatusError], tags: [],
                                     forbids: [].}
Source   Edit  
proc expect(env: napi_env; n: napi_value; expectKind: NapiValueType): bool {.
    ...raises: [NapiStatusError], tags: [], forbids: [].}
Source   Edit  
proc expect(env: napi_env; v: seq[napi_value]; errorName, fnIdent: string): bool {.
    ...raises: [KeyError, NapiStatusError, Exception, ValueError],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc get(key: string): napi_value {....raises: [NapiStatusError, KeyError,
    ValueError], tags: [], forbids: [].}
Alias of getProperty for accessing global properties. This proc supports dot annotations: get("JSON.parse") Source   Edit  
proc get(key: string; default: napi_value): napi_value {.
    ...raises: [NapiStatusError], tags: [], forbids: [].}
Alias for getProperty with default support Source   Edit  
proc get(obj: napi_value; key: string): napi_value {.
    ...raises: [KeyError, ValueError, NapiStatusError], tags: [], forbids: [].}
Alias of getProperty for accessing a property from a specific object Source   Edit  
proc getBool(n: napi_value): bool {....raises: [NapiStatusError], tags: [],
                                    forbids: [].}
Return a bool value from n Source   Edit  
proc getBool(n: napi_value; default: bool): bool {....raises: [], tags: [],
    forbids: [].}
Return a bool from n. Returns default on failure Source   Edit  
proc getElement(obj: napi_value; index: int): napi_value {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Retrieves value from index in obj; raises exception if obj is not an array or index is out of bounds Source   Edit  
proc getElement(obj: napi_value; index: int; default: napi_value): napi_value {.
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc getGlobal(): napi_value {....raises: [NapiStatusError], tags: [], forbids: [].}
Returns NodeJS global variable Source   Edit  
proc getInt32(n: napi_value): int32 {....raises: [NapiStatusError], tags: [],
                                      forbids: [].}
Retrieves value from node; raises exception on failure Source   Edit  
proc getInt32(n: napi_value; default: int32): int32 {....raises: [], tags: [],
    forbids: [].}
Retrieves value from node; returns default on failure Source   Edit  
proc getInt64(n: napi_value): int64 {....raises: [NapiStatusError], tags: [],
                                      forbids: [].}
Retrieves value from node; raises exception on failure Source   Edit  
proc getInt64(n: napi_value; default: int64): int64 {....raises: [], tags: [],
    forbids: [].}
Retrieves value from node; returns default on failure Source   Edit  
proc getNull(): napi_value {....raises: [NapiStatusError], tags: [], forbids: [].}
Returns JavaScript null value Source   Edit  
proc getProperty(obj: napi_value; key: string): napi_value {.
    ...raises: [KeyError, ValueError, NapiStatusError], tags: [], forbids: [].}
Retrieves property key from obj; Panics if obj is not an object Source   Edit  
proc getProperty(obj: napi_value; key: string; default: napi_value): napi_value {.
    ...raises: [], tags: [], forbids: [].}
Retrieves property key from obj; returns default if obj is not an object or does not contain key Source   Edit  
proc getPropertyNames(obj: napi_value): napi_value {.
    ...raises: [ValueError, NapiStatusError], tags: [], forbids: [].}
Checks whether or not obj has a property key; Panics if obj is not an object Source   Edit  
proc getPropertyNamesArr(obj: napi_value): seq[napi_value] {.
    ...raises: [ValueError, NapiStatusError], tags: [], forbids: [].}
Checks whether or not obj has a property key; Panics if obj is not an object Source   Edit  
proc getPropertyNamesArrStr(obj: napi_value): seq[string] {.
    ...raises: [ValueError, NapiStatusError], tags: [], forbids: [].}
Checks whether or not obj has a property key; Panics if obj is not an object Source   Edit  
proc getStr(n: napi_value): string {....raises: [NapiStatusError], tags: [],
                                     forbids: [].}
Return a string from n Source   Edit  
proc getStr(n: napi_value; default: string; bufsize: int = 40): string {.
    ...raises: [], tags: [], forbids: [].}
Retrieves utf8 encoded value from node; returns default on failure Maximum return string length is equal to bufsize Source   Edit  
proc getUndefined(): napi_value {....raises: [NapiStatusError], tags: [],
                                  forbids: [].}
Returns JavaScript undefined value Source   Edit  
proc hasElement(obj: napi_value; index: int): bool {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Checks whether element is contained in obj; raises exception if obj is not an array Source   Edit  
proc hasOwnProperty(obj: napi_value; key: string): bool {.
    ...raises: [ValueError, NapiStatusError], tags: [], forbids: [].}
Checks whether or not obj has a property key; Panics if obj is not an object Source   Edit  
proc hasProperty(obj: napi_value; key: string): bool {.
    ...raises: [ValueError, NapiStatusError], tags: [], forbids: [].}
Checks whether or not obj has a property key; Panics if obj is not an object Source   Edit  
proc isArray(obj: napi_value): bool {....raises: [NapiStatusError], tags: [],
                                      forbids: [].}
Source   Edit  
proc kind(val: napi_value): NapiValueType {....raises: [NapiStatusError], tags: [],
    forbids: [].}
Source   Edit  
proc len(arr: napi_value): int {....raises: [NapiStatusError, ValueError, KeyError],
                                 tags: [], forbids: [].}
Source   Edit  
proc napiCall(fname: string; args: openArray[napi_value] = []): napi_value {.
    discardable, ...raises: [NapiStatusError, KeyError, ValueError], tags: [],
    forbids: [].}
A short-hand procedure for calling global Napi functions by string. You can use dot annotation for accessing and calling from object properties. For example napiCall("JSON.parse", "{}") Source   Edit  
proc napiCreate[T](t: T): napi_value
Create a new napi_value of T Source   Edit  
proc newNodeValue(val: napi_value; env: napi_env): Module {....raises: [],
    tags: [], forbids: [].}
Used internally, disregard Source   Edit  
proc register(obj: Module; name: string; cb: napi_callback;
              attr: NapiPropertyAttributes = napi_default) {.
    ...raises: [NapiStatusError], tags: [], forbids: [].}
Register a new property field to obj Module. Source   Edit  
proc register[T: int | uint | string | napi_value](obj: Module; name: string;
    value: T; attr: NapiPropertyAttributes = napi_default)
Adds field to exports object obj Source   Edit  
proc register[T: int | uint | string | napi_value](obj: Module; name: string;
    values: openArray[(string, T)]; attr: NapiPropertyAttributes = napi_default)
Register a new property field to obj Module. Source   Edit  
proc register[T: int | uint | string | napi_value](obj: Module; name: string;
    values: openArray[T]; attr: NapiPropertyAttributes = napi_default)
Adds field to exports object obj Source   Edit  
proc setElement(obj: napi_value; index: int; value: napi_value) {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Sets value at index; raises exception if obj is not an array Source   Edit  
proc setProperty(obj: napi_value; key: string; value: napi_value) {.
    ...raises: [NapiStatusError, ValueError], tags: [], forbids: [].}
Sets property key in obj to value; raises exception if obj is not an object Source   Edit  
proc throwError(env: napi_env; msg: string; code = "";
                errorType: NapiErrorType = napiError;
                customError: napi_value = nil): NapiStatus {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc toBool(n: napi_value): napi_value {....raises: [NapiStatusError], tags: [],
    forbids: [].}
Source   Edit  
proc toNumber(n: napi_value): napi_value {....raises: [NapiStatusError], tags: [],
    forbids: [].}
Source   Edit  
proc toSeq(n: napi_value): seq[napi_value] {.
    ...raises: [NapiStatusError, ValueError, KeyError], tags: [], forbids: [].}
Return unpacked Array object to seq[napi_value] Source   Edit  
proc toString(n: napi_value): napi_value {....raises: [NapiStatusError], tags: [],
    forbids: [].}
Source   Edit  
proc tryGetJson(n: napi_value): JsonNode {....raises: [NapiStatusError, KeyError,
    ValueError, IOError, OSError, JsonParsingError],
    tags: [ReadIOEffect, WriteIOEffect], forbids: [].}
Source   Edit  
proc undefined(): napi_value {....raises: [NapiStatusError], tags: [], forbids: [].}
Returns an undefined napi_value Source   Edit  

Iterators

iterator items(n: napi_value): napi_value {.
    ...raises: [NapiStatusError, ValueError, KeyError], tags: [], forbids: [].}
Source   Edit  
iterator pairs(n: napi_value): (string, napi_value) {.
    ...raises: [NapiStatusError, ValueError, KeyError], tags: [], forbids: [].}
Yields every key and value Source   Edit  

Macros

macro `%*`(x: typed): untyped
An elegant way to convert Nim types to napi_value. Source   Edit  
macro `.`(val: napi_value; field: untyped): untyped
Source   Edit  
macro `.()`(val: napi_value; field: untyped; args: varargs[typed]): untyped
Source   Edit  
macro `.=`(val: napi_value; field: untyped; value: napi_value): untyped
Source   Edit  
macro `.=`(val: napi_value; field: untyped; value: typed): untyped
Source   Edit  
macro export_napi(fn: untyped; withModule: untyped = nil)

A fancy compile-time macro to export NAPI functions

proc hello(name: string): string {.export_napi.} =
  return %* args.get("name")

If you want use it within module than use withModule arg:

proc hello(name: string): string {.export_napi: false.} =
  return %* args.get("name")

Source   Edit  
macro export_napi(vName, vType: untyped; vVal: typed)

A fancy compile-time macro to export object properties

var name {.export_napi.} = "Denim is Awesome!"

Source   Edit  
macro init(initHook: proc (exports: Module)): void

Bootstraps module; use by calling register to add properties to exports

 init proc(module: Module) =
   module.register("hello", "hello world")

Source   Edit  
macro promise(fn: untyped)
Source   Edit  
macro toJsObject(x: typed): untyped
An elegant way to convert Nim types to napi_value. Source   Edit  
macro toObject(x: untyped): untyped
Convert {"a": "val", "b": "val"} to JavaScript Object Source   Edit  

Templates

template error(msg: string; code = ""; errorType: NapiErrorType = napiError;
               customError: napi_value = nil): untyped
Throw an exception using napi_throw_error Source   Edit  
template exportFn(exports: Module; paramCt: int; name: string; handle: untyped): untyped {.
    dirty.}
Register and export a function Source   Edit  
template getIdentStr(n: untyped): string
Source   Edit  
template getInt(n: napi_value): int
Return int value from n Source   Edit  
template getInt(n: napi_value; default: int): int
Return int value from n. Returns default on failure Source   Edit  
template registerFn(paramCt: int; fnName, fnNameJS, handle: untyped): untyped {.
    dirty.}
Register a function Source   Edit  

Exports

napi_env, napi_set_property, napi_get_and_clear_last_exception, napi_get_element, napi_deferred, napi_delete_property, napi_create_error, napi_get_buffer_info, napi_get_arraybuffer_info, napi_queue_async_work, napi_callback_scope, napi_call_function, napi_get_value_double, napi_undefined, napi_throw_type_error, napi_define_class, napi_get_null, napi_callback_info, napi_is_array, napi_create_object, NapiThreadSafeFunction, napiModule, napi_create_uint64, napi_async_execute_callback, napi_has_own_property, napi_is_dataview, napi_get_node_version, napi_value, napi_is_promise, napi_get_value_string_latin1, napi_get_boolean, napi_async_context, napi_get_cb_info, napi_coerce_to_number, napi_wrap, napi_get_value_string_utf8, napi_throw_range_error, napi_has_element, napi_get_value_external, napi_module_register, napi_get_value_int32, napi_threadsafe_function_call_js, napi_symbol, napi_new_instance, napi_get_reference_value, napi_create_int32, napi_is_typedarray, napi_is_buffer, napi_coerce_to_bool, napi_get_prototype, napi_fatal_error, NapiValueType, napi_async_complete_callback, napi_create_external, napi_get_dataview_info, napi_create_array, NapiPropertyAttributes, napi_is_arraybuffer, napi_async_init, NapiExtendedErrorInfo, napi_threadsafe_function, napi_addon_register_func, napi_cancel_async_work, napi_delete_element, napi_get_undefined, napi_get_property_names, napi_create_uint32, napi_create_date, napi_create_type_error, napi_create_string_latin1, NapiModule, napi_get_date_value, napi_instanceof, napi_number, napi_create_arraybuffer, napi_is_date, napi_define_properties, napi_unrwap, napi_make_callback, napi_create_range_error, napi_typeof, napi_ref, napi_create_external_arraybuffer, napi_external, napi_get_array_length, napi_callback, napi_create_array_with_length, napi_coerce_to_string, napi_get_version, napi_finalize, napi_has_property, napi_get_value_int64, napi_boolean, napi_bigint, napi_create_promise, napi_remove_wrap, napi_create_dataview, napi_throw_error, napi_escapable_handle_scope, NapiStatus, NapiNodeVersion, napi_create_typedarray, napi_async_destroy, napi_null, napi_add_finalizer, napi_set_named_property, napi_get_last_error_info, napi_is_error, napi_get_property, napi_async_work, napi_throw, napi_is_exception_pending, napi_get_typedarray_info, napi_object, napi_create_symbol, napi_function, napi_has_named_property, napi_get_value_bool, napi_get_global, napi_run_script, napi_create_async_work, napi_adjust_external_memory, napi_strict_equals, NApiTypedArrayType, napi_coerce_to_object, napi_create_string_utf8, napi_create_function, napi_create_buffer, napi_delete_async_work, napi_create_reference, NapiPropertyDescriptor, napi_create_external_buffer, napi_resolve_deferred, napi_get_value_uint32, napi_string, napi_open_handle_scope, napi_create_double, napi_set_element, napi_handle_scope, napi_get_new_target, napi_create_buffer_copy, napi_reject_deferred, napi_get_named_property, napi_create_int64, napi_env, napi_set_property, napi_get_and_clear_last_exception, napi_get_element, napi_deferred, napi_unrwap, napi_create_range_error, napi_create_error, napi_typeof, napi_ref, napi_create_external_arraybuffer, napi_external, napi_get_array_length, napi_callback, napi_coerce_to_number, napi_get_arraybuffer_info, napi_coerce_to_string, napi_create_reference, napi_define_class, napi_get_version, napi_call_function, napi_get_value_double, napi_throw_type_error, napi_coerce_to_object, napi_get_value_int64, napi_undefined, napi_get_null, napi_is_exception_pending, napi_bigint, napi_create_promise, napi_remove_wrap, napi_callback_info, napi_create_dataview, NapiExtendedErrorInfo, napi_is_array, napi_create_object, napi_get_value_bool, napi_get_new_target, napi_escapable_handle_scope, napi_create_uint64, NapiStatus, napi_has_own_property, napi_coerce_to_bool, napi_throw, napi_null, napi_add_finalizer, napi_set_named_property, napi_define_properties, napi_value, napi_is_promise, napi_get_value_string_latin1, napi_get_boolean, napi_get_cb_info, napi_get_last_error_info, napi_get_value_string_utf8, napi_is_error, napi_get_property, napi_boolean, napi_throw_range_error, napi_object, napi_create_symbol, napi_has_element, napi_set_element, napi_get_global, napi_run_script, napi_is_arraybuffer, napi_get_value_int32, napi_threadsafe_function_call_js, napi_strict_equals, napi_symbol, napi_new_instance, napi_get_value_uint32, NApiTypedArrayType, napi_create_string_latin1, napi_get_typedarray_info, napi_create_int32, napi_create_string_utf8, napi_create_function, napi_is_typedarray, napi_is_date, NapiPropertyDescriptor, NapiValueType, napi_get_reference_value, napi_is_dataview, napi_create_external, napi_create_typedarray, napi_resolve_deferred, napi_get_dataview_info, napi_create_array, NapiPropertyAttributes, napi_function, napi_adjust_external_memory, napi_create_array_with_length, napi_wrap, napi_has_named_property, napi_finalize, napi_string, NapiThreadSafeFunction, napi_get_value_external, napi_create_double, napi_delete_element, napi_get_undefined, napi_get_property_names, napi_create_uint32, napi_create_date, napi_delete_property, napi_create_type_error, napi_number, napi_has_property, napi_throw_error, napi_reject_deferred, napi_open_handle_scope, napi_create_arraybuffer, napi_get_date_value, napi_instanceof, napi_handle_scope, napi_get_prototype, napi_get_named_property, napi_create_int64, enumerate, toUnchecked