Skip to content

Instantly share code, notes, and snippets.

@norcalli
Last active January 1, 2020 03:50
Show Gist options
  • Save norcalli/d4f4a3eba791da32dd5a6f82000895d7 to your computer and use it in GitHub Desktop.
Save norcalli/d4f4a3eba791da32dd5a6f82000895d7 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
extern void* main_loop;
extern const char* Version;
extern int nvim_get_current_buf(void);
#define false ((_Bool)0)
typedef _Bool bool;
typedef bool Boolean;
typedef int64_t Integer;
typedef double Float;
typedef struct {
char *data;
size_t size;
} String;
typedef int handle_T;
typedef handle_T Buffer;
typedef handle_T Window;
typedef handle_T Tabpage;
typedef struct object Object;
typedef struct {
Object *items;
size_t size, capacity;
} Array;
// Basic types
typedef enum {
kErrorTypeNone = -1,
kErrorTypeException,
kErrorTypeValidation
} ErrorType;
typedef struct {
ErrorType type;
char *msg;
} Error;
typedef enum {
kObjectTypeNil = 0,
kObjectTypeBoolean,
kObjectTypeInteger,
kObjectTypeFloat,
kObjectTypeString,
kObjectTypeArray,
kObjectTypeDictionary,
kObjectTypeLuaRef,
// EXT types, cannot be split or reordered, see #EXT_OBJECT_TYPE_SHIFT
kObjectTypeBuffer,
kObjectTypeWindow,
kObjectTypeTabpage,
} ObjectType;
struct object {
ObjectType type;
union {
Boolean boolean;
Integer integer;
Float floating;
String string;
Array array;
// Dictionary dictionary;
// LuaRef luaref;
} data;
};
extern void nvim_buf_set_lines(uint64_t channel_id,
Buffer buffer,
Integer start,
Integer end,
Boolean strict_indexing,
Array replacement,
Error *err);
int main(int argc, char** argv) {
printf("\n");
printf("Version: %s\n", Version);
printf("MAIN LOOP: %llx\n", main_loop);
int buf = nvim_get_current_buf();
printf("Current buf: %x\n", buf);
String line_str = {
data: "butts",
size: 5,
};
Object line = {
.type = kObjectTypeString,
};
line.data.string = line_str;
Array lines = {
items: &line,
size: 1,
capacity: 1,
};
Error error;
nvim_buf_set_lines(0, buf, 0, -1, false, lines, &error);
}
local ffi = require'ffi'
local tcc = tcc or require'tcc'.load()
_G.tcc = tcc
ffi.cdef [[
void* main_loop;
]]
local start = os.clock()
local state = tcc.new()
state:set_output_type(tcc.OUTPUT.MEMORY)
-- state:set_options("-rdynamic")
-- state:add_file("./tcc-test-nvim.c")
state:compile_string(io.open("./tcc-test-nvim.c"):read("*a"))
print(ffi.C.main_loop)
-- state:add_symbol("main_loop", ffi.C.main_loop)
-- state:add_symbol("main_loop", ffi.C.main_loop)
state:run(tcc.args())
print((os.clock() - start) * 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment