Function libraryImport

  • Declares that the function is an external imported library.

    Parameters

    • libraryName: string

      The name of the dynamic library to be imported.

    • signature: string

      The imported function signature. Specifies the return type and parameter types the C-function takes. Must exactly match how it was exported in the dynamic library or the engine may crash.

    • OptionalsymbolName: string

      The symbol to call on the imported library. This symbol must be exported in the library (see DLL export) and must be compatible with the C-ABI.

    Returns ((target: any, property: string, descriptor: PropertyDescriptor) => void)

    The return type of the function, according to its signature.

      • (target, property, descriptor): void
      • Parameters

        • target: any
        • property: string
        • descriptor: PropertyDescriptor

        Returns void

    This decorator will override the implementation of the function with another that calls the imported library function.

    If the library was not found or the specified symbol was not exported on the given library, the implementation will call the original implementation of this function.

    Library import support the following C types to be proxied seamlessly between JavaScript and the native calling convention:

    • void: Represents a no type. Usually only used by functions that have no return value.
    • char: unsigned 8-bit character. Will map to a JavaScript String.
    • uint8: unsigned 8-bit integer. Will map to a JavaScript Number.
    • uint16: unsigned 16-bit integer. Will map to a JavaScript Number.
    • uint32: unsigned 32-bit integer. Will map to a JavaScript Number.
    • uint64: unsigned 64-bit integer. Will map to a JavaScript Number.
    • int8: signed 8-bit integer. Will map to a JavaScript Number.
    • int16: signed 16-bit integer. Will map to a JavaScript Number.
    • int32: signed 32-bit integer. Will map to a JavaScript Number.
    • int64: signed 64-bit integer. Will map to a JavaScript Number.
    • float: a C 32-bit floating point number. Will map to a JavaScript Number.
    • double: a C 64-bit floating point number. Will map to a JavaScript Number.
    • boolean: a C true or false true. Maps to a 8-bit unsigned integer on C and a Boolean on JavaScript.
    • string: mapped as a const char* on C and seamlessly translated to/from a JavaScript String.
    • ptr: Mapped as a void* on C and as a IntPtr on JavaScript.

    The function signature must be in the form of returnType([arguments, ...]) where either returnTypeorarguments` are one of the supported data type specified above.

     class PlatformFile {
    @importLibrary("c", "fopen", "ptr(string,string)")
    public static fopen(path: string, mode: string): IntPtr {}

    @importLibrary("c", "fgetc", "int32(ptr)")
    public static fgetc(handle: IntPtr): number {}

    @importLibrary("c", "fclose", "int32(ptr)")
    public static fclose(handle: IntPtr): number {}
    }

    const handle: BigInt = PlatformFile.fopen("/my/path", "r");
    const bytes: number[] = [
    PlatformFile.fgetc(handle),
    PlatformFile.fgetc(handle),
    PlatformFile.fgetc(handle),
    PlatformFile.fgetc(handle)
    ];
    PlatformFile.fclose(handle);