metacall/core

BUG: Error while using `metacall_load_from_package_export`

Opened this issue · 2 comments

🐛 Bug Report

metacall_load_from_package_export errors while using it with a C library file.

The following code should work without any error:

const { metacall_execution_path, metacall_load_from_package_export } = require('metacall');

metacall_execution_path('c', '/usr/include');
metacall_execution_path('c', '/usr/lib/x86_64-linux-gnu')

const { sqlite3_libversion } = metacall_load_from_package_export('c', 'sqlite3');

console.log(sqlite3_libversion());

Given that sqlite3.h is present in /usr/include and libsqlite3.so is present in /usr/lib/x86_64-linux-gnu

Expected Behavior

It should output the current version of sqlite.

Current Behavior

Throws the following error:

Error: Failed to parse type sqlite3_uint64
Error: Function closure from parameter 'xProfile' has failed to be prepared
Error: Failed to parse type unsigned int
Error: Function closure from parameter 'xCallback' has failed to be prepared
Error: Failed to parse type sqlite3_int64
Error: Function closure from parameter '' has failed to be prepared
Error: Failed to parse type unsigned int
Error: Function closure from parameter '' has failed to be prepared
Error: Failed to parse type sqlite3_int64
Error: Function closure from parameter '' has failed to be prepared
m ��

Steps to Reproduce

  1. Build metacall from source along with the cli
  2. Write the above given code
  3. Run it using metacallcli main.js

Development environment

I'm using Debian (WSL2)

@inclinedadarsh I started to make an example with this, most of the issues are already solved but there are some, I will publish it in a separate repo maybe or also as a test in this one.

@inclinedadarsh I have solved most of the issues you reported. I created a PR because I want to go further, I want to reproduce this example "as it is" in JS or Python:

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

int main(void) {
    sqlite3 *db;
    char *err_msg = 0;
    int rc;

    // 1. Open (or create) the database
    rc = sqlite3_open("test.db", &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }
    printf("Opened database successfully.\n");

    // 2. Create SQL statement for creating table
    const char *sql_create =
        "CREATE TABLE IF NOT EXISTS Users("
        "ID INTEGER PRIMARY KEY AUTOINCREMENT, "
        "Name TEXT NOT NULL, "
        "Age INT NOT NULL);";

    rc = sqlite3_exec(db, sql_create, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error (create table): %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(db);
        return 1;
    }
    printf("Table created successfully.\n");

    // 3. Insert values
    const char *sql_insert =
        "INSERT INTO Users (Name, Age) VALUES ('Alice', 30);"
        "INSERT INTO Users (Name, Age) VALUES ('Bob', 25);"
        "INSERT INTO Users (Name, Age) VALUES ('Charlie', 35);";

    rc = sqlite3_exec(db, sql_insert, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error (insert): %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(db);
        return 1;
    }
    printf("Records inserted successfully.\n");

    // 4. Close the database
    sqlite3_close(db);
    printf("Database closed.\n");

    return 0;
}

I created a PR and a branch with the work in progress I have been doing, this includes the fixes of your program but still I want to merge once I can reproduce this code in JS o Py.

PR: #568
Branch: https://github.com/metacall/core/tree/feature/c-loader-inout-pointers