Documentation
¶
Index ¶
- func As[T ValueType](input ValueType) T
- func StrictEqual(env EnvType, lhs, rhs ValueType) (bool, error)
- func ThrowError(env EnvType, code, err string) error
- func ValueFrom(napiValue ValueType, v any) error
- type Array
- func (arr *Array) Append(values ...ValueType) error
- func (arr *Array) Delete(index int) (bool, error)
- func (arr *Array) From(from iter.Seq[any]) (err error)
- func (arr *Array) Get(index int) (ValueType, error)
- func (arr *Array) Length() (int, error)
- func (arr *Array) Seq() iter.Seq[ValueType]
- func (arr *Array) Set(index int, value ValueType) error
- type ArrayBuffer
- type AsyncWorker
- type Bigint
- type Boolean
- type Buffer
- type Callback
- type CallbackAsyncWorkerDone
- type CallbackAsyncWorkerExec
- type DataView
- type Date
- type EnvType
- type Error
- type External
- type Function
- type NapiType
- type NodeVersion
- type Number
- type Object
- func (obj *Object) Delete(key string) (bool, error)
- func (obj *Object) DeleteWithValue(key ValueType) (bool, error)
- func (obj *Object) Freeze() error
- func (obj *Object) From(from iter.Seq2[any, any]) (err error)
- func (obj *Object) Get(key string) (ValueType, error)
- func (obj *Object) GetPropertyNames() (*Array, error)
- func (obj *Object) GetWithValue(key ValueType) (ValueType, error)
- func (obj *Object) Has(name string) (bool, error)
- func (obj *Object) HasOwnProperty(key ValueType) (bool, error)
- func (obj *Object) HasOwnPropertyString(keyString string) (bool, error)
- func (obj *Object) InstanceOf(value ValueType) (bool, error)
- func (obj *Object) Seal() error
- func (obj *Object) Seq() iter.Seq2[string, ValueType]
- func (obj *Object) Set(key string, value ValueType) error
- func (obj *Object) SetWithValue(key, value ValueType) error
- type Promise
- type String
- type TypedArray
- type TypedArrayType
- type ValueType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v0.1.2
Casts to another type of ValueType, when the actual type is known or assumed.
This conversion does NOT coerce the type. Calling any methods inappropriate for the actual value type.
func StrictEqual ¶ added in v0.1.2
This API represents the invocation of the Strict Equality algorithm as defined in https://tc39.github.io/ecma262/#sec-strict-equality-comparison of the ECMAScript Language Specification.
func ThrowError ¶
ThrowError throws a JavaScript error in the given N-API environment with the specified code and error message. If the code is an empty string, it captures the current Go stack trace and uses it as the error code. Returns an error if the underlying N-API call fails.
Parameters:
- env: The N-API environment in which to throw the error.
- code: The error code to associate with the thrown error. If empty, the current stack trace is used.
- err: The error message to be thrown.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
func (*Array) Append ¶
Append adds one or more values to the end of the array. It accepts a variadic number of ValueType arguments and appends each to the array, starting from the current length. If an error occurs during the append operation, it returns the error; otherwise, it returns nil.
func (*Array) From ¶ added in v0.1.2
Populates the Array with elements from the provided iterator sequence. For each element in the sequence, it converts the value to ValueType if necessary, and appends it to the end of the Array. If an error occurs during conversion or insertion, the operation stops and the error is returned. Returns error if an error if any occurs during value conversion or insertion; otherwise, nil.
type ArrayBuffer ¶ added in v0.1.2
type ArrayBuffer struct {
// contains filtered or unexported fields
}
ArrayBuffer represents a JavaScript ArrayBuffer.
func CreateArrayBuffer ¶ added in v0.1.2
func CreateArrayBuffer(env EnvType, length int) (*ArrayBuffer, []byte, error)
CreateArrayBuffer creates a new JavaScript ArrayBuffer instance. It also returns a pointer to the underlying byte buffer.
func CreateExternalArrayBuffer ¶ added in v0.1.2
func CreateExternalArrayBuffer(env EnvType, data []byte, finalize napi.Finalize, finalizeHint unsafe.Pointer) (*ArrayBuffer, error)
CreateExternalArrayBuffer creates a JavaScript ArrayBuffer instance over an existing external data buffer. The caller is responsible for managing the lifetime of the data buffer. The finalize callback will be invoked when the ArrayBuffer is garbage collected.
func ToArrayBuffer ¶ added in v0.1.2
func ToArrayBuffer(o ValueType) *ArrayBuffer
ToArrayBuffer converts a ValueType to *ArrayBuffer. It assumes the underlying N-API value is an ArrayBuffer. Use IsArrayBuffer to check before converting.
func (*ArrayBuffer) ByteLength ¶ added in v0.1.2
func (ab *ArrayBuffer) ByteLength() (int, error)
ByteLength retrieves the length (in bytes) of the ArrayBuffer. This is a convenience wrapper around Info().
func (*ArrayBuffer) Data ¶ added in v0.1.2
func (ab *ArrayBuffer) Data() ([]byte, error)
Data retrieves the underlying byte data of the ArrayBuffer as a Go slice. This is a convenience wrapper around Info().
func (*ArrayBuffer) Detach ¶ added in v0.1.2
func (ab *ArrayBuffer) Detach() error
Detach detaches the ArrayBuffer, making its contents inaccessible from JavaScript. This is used for transferring ownership of the underlying buffer.
func (*ArrayBuffer) Info ¶ added in v0.1.2
func (ab *ArrayBuffer) Info() ([]byte, int, error)
Info retrieves information about the ArrayBuffer, including its underlying data buffer and length.
func (*ArrayBuffer) IsDetached ¶ added in v0.1.2
func (ab *ArrayBuffer) IsDetached() (bool, error)
IsDetached checks if the ArrayBuffer has been detached.
type AsyncWorker ¶ added in v0.1.1
type AsyncWorker struct {
// contains filtered or unexported fields
}
AsyncWorker encapsulates an asynchronous worker for executing tasks in a separate thread using Node.js N-API. It embeds a value type and manages the lifecycle of an async work operation, including its associated promise for JavaScript interoperability.
func CreateAsyncWorker ¶ added in v0.1.2
func CreateAsyncWorker(env EnvType, exec CallbackAsyncWorkerExec, done CallbackAsyncWorkerDone) (*AsyncWorker, error)
CreateAsyncWorker creates an asynchronous worker that executes the provided exec function in a separate thread, and calls the done callback upon completion. It returns an AsyncWorker instance and a promise that can be used to track the asynchronous operation from JavaScript.
The promise is resolved or rejected based on the outcome of the async operation. If an error or panic occurs during execution, the promise is rejected with the corresponding error. If the async worker is cancelled, the promise is also rejected.
func (*AsyncWorker) Cancel ¶ added in v0.1.1
func (async *AsyncWorker) Cancel() error
Cancel attempts to cancel the asynchronous work associated with the AsyncWorker. It returns an error if the cancellation fails or if the async work cannot be cancelled.
type Bigint ¶
type Bigint struct {
// contains filtered or unexported fields
}
func CreateBigint ¶
CreateBigint creates a new Bigint value in the given N-API environment from the provided int64 or uint64 value. The function is generic and accepts either int64 or uint64 as the input type. It returns a pointer to a Bigint and an error if the creation fails.
func (*Bigint) Int64 ¶
Int64 returns the value of the Bigint as an int64 along with an error if the conversion fails. It retrieves the int64 representation of the underlying N-API BigInt value. If the value cannot be represented as an int64, an error is returned.
func (*Bigint) Uint64 ¶
Uint64 returns the value of the Bigint as a uint64 along with an error if the conversion fails. It retrieves the underlying BigInt value from the N-API environment and attempts to convert it to a uint64. If the value cannot be represented as a uint64 or if an error occurs during retrieval, an error is returned.
type Boolean ¶
type Boolean struct {
// contains filtered or unexported fields
}
func CreateBoolean ¶
CreateBoolean creates a new Boolean value in the given N-API environment. It takes an EnvType representing the N-API environment and a Go bool value. Returns a pointer to a Boolean object representing the value in the N-API environment, or an error if the creation fails.
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
func CopyBuffer ¶
Copy []byte to Node::Buffer struct
func CreateBuffer ¶
Create new Buffer with length
type CallbackAsyncWorkerDone ¶ added in v0.1.2
Funtion to run after exec code
type CallbackAsyncWorkerExec ¶ added in v0.1.2
type CallbackAsyncWorkerExec func(env EnvType)
function to run code in background without locker Loop event
type DataView ¶ added in v0.1.2
type DataView struct {
// contains filtered or unexported fields
}
DataView represents a JavaScript DataView object.
func CreateDataView ¶ added in v0.1.2
func CreateDataView(env EnvType, buffer *ArrayBuffer, byteOffset, byteLength int) (*DataView, error)
CreateDataView creates a new JavaScript DataView instance over an existing ArrayBuffer.
func ToDataView ¶ added in v0.1.2
ToDataView converts a ValueType to *DataView. It assumes the underlying N-API value is a DataView. Use IsDataView to check before converting.
func (*DataView) Buffer ¶ added in v0.1.2
func (dv *DataView) Buffer() (*ArrayBuffer, error)
Buffer returns the underlying ArrayBuffer referenced by the DataView. This is a convenience wrapper around Info().
func (*DataView) ByteLength ¶ added in v0.1.2
ByteLength returns the length (in bytes) of the DataView. This is a convenience wrapper around Info().
func (*DataView) ByteOffset ¶ added in v0.1.2
ByteOffset returns the offset (in bytes) of the DataView within its underlying ArrayBuffer. This is a convenience wrapper around Info().
func (*DataView) Info ¶ added in v0.1.2
func (dv *DataView) Info() (buffer *ArrayBuffer, byteLength int, byteOffset int, dataPtr unsafe.Pointer, err error)
Info retrieves information about the DataView, including the underlying ArrayBuffer, its byte length, and the byte offset within the buffer. It also returns a pointer to the start of the DataView's section within the buffer's data.
type Date ¶
type Date struct {
// contains filtered or unexported fields
}
func CreateDate ¶
CreateDate creates a new JavaScript Date object in the given N-API environment using the provided Go time.Time value. It returns a pointer to a Date wrapper or an error if the creation fails.
func (Date) Time ¶
Time returns the Go time.Time representation of the Date value. It retrieves the date value from the underlying N-API environment, converts it to a Unix millisecond timestamp, and constructs a time.Time object. If an error occurs during value retrieval or conversion, it is returned.
type EnvType ¶
type EnvType interface {
NapiValue() napi.Env // Primitive value to NAPI call
Global() (*Object, error)
Undefined() (ValueType, error)
Null() (ValueType, error)
}
EnvType defines an interface for interacting with a NAPI environment. It provides methods to retrieve the underlying NAPI environment, access the global object, and obtain the JavaScript 'undefined' and 'null' values as Go types.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func CreateError ¶
CreateError creates a new JavaScript Error object in the given N-API environment with the specified message. It returns a pointer to the Error object and an error if the creation fails at any step.
func (*Error) ThrowAsJavaScriptException ¶
ThrowAsJavaScriptException throws the current Error as a JavaScript exception in the associated N-API environment. It returns an error if the operation fails.
type External ¶ added in v0.1.2
type External struct {
// contains filtered or unexported fields
}
func CreateExternal ¶ added in v0.1.2
func CreateExternal(env EnvType, data unsafe.Pointer, finalize napi.Finalize, finalizeHint unsafe.Pointer) (*External, error)
CreateExternal creates a new JavaScript external value in the given N-API environment. It associates the provided data pointer with the external, and allows specifying a finalize callback and a finalize hint, which will be called when the external is garbage collected.
func ToExternal ¶ added in v0.1.2
type Function ¶
type Function struct {
// contains filtered or unexported fields
}
func CreateFunction ¶
func CreateFunctionNapi ¶
Create function from internal napi.Callback
type NapiType ¶
type NapiType int
Typeof of Value
type NodeVersion ¶ added in v0.1.2
type NodeVersion = napi.NodeVersion
func GetNodeVersion ¶ added in v0.1.2
func GetNodeVersion(env EnvType) (*NodeVersion, error)
GetNodeVersion retrieves the Node.js version information for the given environment. It takes an EnvType as input and returns a pointer to a NodeVersion struct and an error. If the version retrieval fails, it returns a non-nil error.
type Number ¶
type Number struct {
// contains filtered or unexported fields
}
func CreateNumber ¶
func CreateNumber[T ~int | ~uint | ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64 | ~float32 | ~float64](env EnvType, n T) (*Number, error)
CreateNumber creates a new JavaScript Number object from a Go numeric value of type T. The function supports various integer and floating-point types, including int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, and float64. It converts the provided Go value to the appropriate JavaScript number representation using the N-API environment.
func (*Number) Float ¶
Float returns the float64 representation of the Number. It retrieves the underlying value from the N-API environment and value handle. If the conversion fails, an error is returned.
func (*Number) Int ¶
Int returns the int64 representation of the Number. It calls napi.GetValueInt64 using the underlying NapiEnv and NapiValue. If the conversion fails, an error is returned.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
func (*Object) DeleteWithValue ¶
Delete property.
func (*Object) Freeze ¶
This method freezes a given object.
This prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed.
It also prevents the object's prototype from being changed.
func (*Object) GetPropertyNames ¶
Get all property names.
func (*Object) GetWithValue ¶
Gets a property.
func (*Object) HasOwnProperty ¶
Checks whether a own property is present.
func (*Object) HasOwnPropertyString ¶
Checks whether a own property is present.
func (*Object) InstanceOf ¶
Checks if an object is an instance created by a constructor function, this is equivalent to the JavaScript `instanceof` operator.
func (*Object) Seal ¶
This method seals a given object.
This prevents new properties from being added to it, as well as marking all existing properties as non-configurable.
func (*Object) Seq ¶
Seq returns an iterator (Seq2) over the object's property names and their corresponding values. It retrieves all property names of the object, and for each property, yields the property's name as a string and its associated ValueType. If an error occurs while retrieving property names or values, the function panics. The iteration stops if the yield function returns false.
func (*Object) SetWithValue ¶
Sets a property.
type Promise ¶ added in v0.1.1
type Promise struct {
// contains filtered or unexported fields
}
func CreatePromise ¶ added in v0.1.1
CreatePromise creates a new JavaScript Promise object in the given N-API environment. It returns a pointer to the created Promise and an error if the operation fails. The function internally calls napi.CreatePromise to obtain the promise value and deferred handle. If an error occurs during promise creation, it is converted and returned.
type String ¶
type String struct {
// contains filtered or unexported fields
}
func CreateString ¶
Create *String from go string
func CreateStringUtf16 ¶
Create string to utf16
func (*String) Utf16Value ¶
Converts a String value to a UTF-16 encoded in rune.
type TypedArray ¶ added in v0.1.2
type TypedArray struct {
// contains filtered or unexported fields
}
func CreateTypedArray ¶ added in v0.1.2
func CreateTypedArray(env EnvType, Type TypedArrayType, length, byteOffset int, arrayValue *ArrayBuffer) (*TypedArray, error)
CreateTypedArray creates a new typed array of the specified type and length, backed by the provided ArrayBuffer. The function takes the environment (env), the typed array type (Type), the desired length, the byte offset within the buffer, and a pointer to the ArrayBuffer. It returns a pointer to the created TypedArray of type T, or an error if the operation fails.
Parameters:
- env: The environment in which to create the typed array.
- Type: The type of the typed array to create (e.g., Int8Array, Float32Array).
- length: The number of elements in the typed array.
- byteOffset: The offset in bytes from the start of the ArrayBuffer.
- arrayValue: Pointer to the ArrayBuffer to use as the backing store.
func ToTypedArray ¶ added in v0.1.2
func ToTypedArray(value ValueType) *TypedArray
Return Generic Value to TypedArray
func (TypedArray) Get ¶ added in v0.1.2
func (typed TypedArray) Get() (data []byte, arr *ArrayBuffer, err error)
Get retrieves the underlying byte slice and associated ArrayBuffer from the TypedArray. It returns the data as a byte slice, a pointer to the ArrayBuffer, and an error if the operation fails. The function extracts the necessary information using napi.GetTypedArrayInfo and handles any status errors accordingly.
func (TypedArray) Type ¶ added in v0.1.2
func (typed TypedArray) Type() TypedArrayType
Type returns the type of the TypedArray as a TypedArrayType. This method is used to retrieve the specific type of the TypedArray instance. It returns the type as a TypedArrayType constant, which can be used to identify the kind of typed array (e.g., Int8Array, Float32Array, etc.). The function is a method of the TypedArray struct and does not take any parameters. It is a simple getter method that returns the type of the TypedArray.
type TypedArrayType ¶ added in v0.1.2
type TypedArrayType = napi.TypedArrayType
const ( TypedArrayInt8Array TypedArrayType = napi.TypedArrayInt8Array TypedArrayUint8Array TypedArrayType = napi.TypedArrayUint8Array TypedArrayUint8ClampedArray TypedArrayType = napi.TypedArrayUint8ClampedArray TypedArrayInt16Array TypedArrayType = napi.TypedArrayInt16Array TypedArrayUint16Array TypedArrayType = napi.TypedArrayUint16Array TypedArrayInt32Array TypedArrayType = napi.TypedArrayInt32Array TypedArrayUint32Array TypedArrayType = napi.TypedArrayUint32Array TypedArrayFloat32Array TypedArrayType = napi.TypedArrayFloat32Array TypedArrayFloat64Array TypedArrayType = napi.TypedArrayFloat64Array TypedArrayBigInt64Array TypedArrayType = napi.TypedArrayBigInt64Array TypedArrayBigUint64Array TypedArrayType = napi.TypedArrayBigUint64Array )
type ValueType ¶
type ValueType interface {
Env() EnvType // NAPI Env to NAPI call
Type() (NapiType, error) // NAPI Type of value
NapiValue() napi.Value // Primitive value to NAPI call
NapiEnv() napi.Env // NAPI Env to NAPI call
}
ValueType defines an interface for types that can be represented as N-API values. It provides methods to retrieve the underlying napi.Value and napi.Env, as well as methods to access the environment and determine the N-API type of the value.
Implementations of ValueType are expected to bridge Go values with their corresponding N-API representations, facilitating interoperability with Node.js.
Methods:
- NapiValue(): Returns the underlying primitive napi.Value for N-API calls.
- NapiEnv(): Returns the associated napi.Env for N-API calls.
- Env(): Returns the environment as an EnvType.
- Type(): Returns the N-API type of the value, or an error if it cannot be determined.
func GoFuncOf ¶ added in v0.1.2
GoFuncOf wraps a Go function as a JavaScript-compatible function for use with the given environment. It takes an EnvType representing the JavaScript environment and a Go function (of any type). Returns a ValueType representing the JavaScript function and an error if the wrapping fails.
func N_APIValue ¶
Return ValueType from napi.Value
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
examples/asyncWorker
command
|
|
|
examples/binding/js
command
|
|
|
examples/goFuncConv
command
|
|
|
examples/js_conversion
command
|
|
|
examples/threadsafe
command
|
|
|
napi
N-API used by sirherobrine23.com.br/Sirherobrine23/napi-go, only use in internal, don't export
|
N-API used by sirherobrine23.com.br/Sirherobrine23/napi-go, only use in internal, don't export |
|
Package module provides the entry point and initialization logic for N-API modules written in Go.
|
Package module provides the entry point and initialization logic for N-API modules written in Go. |
|
binding
Export values to Javascript
|
Export values to Javascript |