dylib | get c types | build dylib | Search

The typeToDataType function takes in parameters returnLength, returnType, and dataType and returns an object compatible with the ffi-rs library, handling different data types through switch statements.

Run example

npm run import -- "c type to data type"

c type to data type

const { open, load, close, DataType, arrayConstructor } = require('ffi-rs')

function typeToDataType(returnLength, returnType, dataType) {
  if(returnLength > 1) {
    switch(returnType) {
      case 'void':
        return arrayConstructor({ type: DataType.StackArray, length: returnLength })
      case 'double':
        return arrayConstructor({ type: DataType.DoubleArray, length: returnLength })
      case 'float':
        return arrayConstructor({ type: DataType.DoubleArray, length: returnLength })
      case 'int':
        return arrayConstructor({ type: DataType.I32Array, length: returnLength })
      case 'char':
        return arrayConstructor({ type: DataType.StringArray, length: returnLength })
      default:
        return arrayConstructor({ type: dataType, length: returnLength })
    }

  } else {
    switch(returnType) {
      case 'void':
        return DataType.Void
      case 'double':
        return DataType.Double
      case 'float':
        return DataType.Double
      case 'int':
        return DataType.I32
      case 'char':
        return DataType.String
      default:
        return dataType
    }
  }
}

module.exports = typeToDataType

What the code could have been:

/**
 * Maps JavaScript types to DataType objects.
 *
 * @param {number} returnLength - The length of the return value.
 * @param {string} returnType - The type of the return value (e.g., 'void', 'double', 'float', etc.).
 * @param {string} dataType - The data type to use for the return value.
 * @returns {DataType | { type: DataType, length: number }} The DataType object or array constructor.
 */
const { DataType, arrayConstructor } = require('ffi-rs');

function typeToDataType(returnLength, returnType, dataType) {
  // Use a lookup table to improve performance
  const typeLookup = {
    'void': { dataType: DataType.Void, arrayType: DataType.StackArray },
    'double': { dataType: DataType.Double, arrayType: DataType.DoubleArray },
    'float': { dataType: DataType.Double, arrayType: DataType.DoubleArray },
    'int': { dataType: DataType.I32, arrayType: DataType.I32Array },
    'char': { dataType: DataType.String, arrayType: DataType.StringArray },
  };

  // Get the type from the lookup table or use a default value
  const typeInfo = typeLookup[returnType] || { dataType, arrayType: dataType };

  // If the return length is greater than 1, use the array type
  if (returnLength > 1) {
    return arrayConstructor({ type: typeInfo.arrayType, length: returnLength });
  }

  // Otherwise, use the single value type
  return typeInfo.dataType;
}

module.exports = typeToDataType;

Breakdown of the Code

Overview

The code exports a function typeToDataType which takes three parameters: returnLength, returnType, and dataType. It is designed to convert these parameters into an object compatible with the ffi-rs library.

Parameters

Return Value

The function returns an object compatible with the ffi-rs library, which can be used to make foreign function calls.

Logic

Switch Statements

The code uses switch statements to handle different data types. The return value depends on the returnType and dataType. If the returnType is a primitive type (e.g., 'void', 'double', 'int', etc.), the corresponding DataType is returned. Otherwise, the dataType is returned as is.

Example Usage

const { typeToDataType } = require('./typeToDataType');

const dataType = typeToDataType(2, 'double', DataType.Double);
console.log(dataType); // Output: { type: 1, length: 2 }

In this example, typeToDataType is called with returnLength set to 2, returnType set to 'double', and dataType set to DataType.Double. The function returns an object with type set to 1 (representing DataType.Double) and length set to 2.