-
Notifications
You must be signed in to change notification settings - Fork 0
/
felineffi.h
124 lines (100 loc) · 4.65 KB
/
felineffi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef _WIN32
// Stops MSVC warnings / errors on functions such as 'fopen'
#include <Windows.h>
#ifdef __cplusplus
#define FELINE_EXPORT extern "C" __declspec(dllexport)
#define FELINE_IMPORT extern "C" __declspec(dllimport)
#else
#define FELINE_EXPORT __declspec(dllexport)
#define FELINE_IMPORT __declspec(dllimport)
#endif
#endif
typedef struct InstanceData {
void(*freeData)(struct InstanceData*);
} InstanceData;
typedef struct VM VM;
typedef struct Obj Obj;
typedef struct ObjClass ObjClass;
typedef struct ObjInstance ObjInstance;
typedef struct ObjString ObjString;
typedef enum ValueType {
VAL_BOOL,
VAL_NULL,
VAL_NUMBER,
VAL_OBJ
} ValueType;
typedef struct Value {
ValueType type;
union {
bool boolean;
double number;
Obj* obj;
} as;
} Value;
typedef enum InternalExceptionType {
INTERNAL_EXCEPTION_BASE,
INTERNAL_EXCEPTION_TYPE,
INTERNAL_EXCEPTION_ARITY,
INTERNAL_EXCEPTION_PROPERTY,
INTERNAL_EXCEPTION_INDEX_RANGE,
INTERNAL_EXCEPTION_UNDEFINED_VARIABLE,
INTERNAL_EXCEPTION_STACK_OVERFLOW,
INTERNAL_EXCEPTION_LINK_FAILURE,
INTERNAL_EXCEPTION_VALUE,
INTERNAL_EXCEPTION__COUNT
} InternalExceptionType;
typedef Value(*NativeFunction)(VM* vm, uint8_t argCount, Value* value);
#define BOOL_VAL(value) ((Value){VAL_BOOL, { .boolean = value }})
#define NULL_VAL ((Value){VAL_NULL, { .number = 0 }})
#define NUMBER_VAL(value) ((Value){VAL_NUMBER, { .number = value }})
#define OBJ_VAL(object) ((Value){VAL_OBJ, { .obj = (Obj*)object }})
#define AS_BOOL(value) ((value).as.boolean)
#define AS_NUMBER(value) ((value).as.number)
#define AS_OBJ(value) ((value).as.obj)
#define IS_BOOL(value) ((value).type == VAL_BOOL)
#define IS_NULL(value) ((value).type == VAL_NULL)
#define IS_NUMBER(value) ((value).type == VAL_NUMBER)
#define IS_OBJ(value) ((value).type == VAL_OBJ)
ObjClass* (*feline_getInternalException)(VM* vm, InternalExceptionType type);
void (*feline_throwException)(VM* vm, ObjClass* exceptionType, const char* format, ...);
bool (*feline_isInstance)(Value value);
ObjInstance* (*feline_asInstance)(Value value);
bool (*feline_isString)(Value value);
ObjString* (*feline_asString)(Value value);
bool (*feline_getInstanceField)(VM* vm, ObjInstance* instance, const char* name, Value* value);
bool (*feline_setInstanceField)(VM* vm, ObjInstance* instance, const char* name, Value value);
char* (*feline_getStringCharacters)(ObjString* string);
size_t(*feline_getStringLength)(ObjString* string);
void (*feline_setInstanceNativeData)(ObjInstance* instance, InstanceData* data);
InstanceData* (*feline_getInstanceNativeData)(ObjInstance* instance);
ObjString* (*feline_takeString)(VM* vm, char* str, size_t length);
ObjString* (*feline_copyString)(VM* vm, const char* str, size_t length);
#ifdef _WIN32
HINSTANCE hostFelineApp;
#ifndef FELINE_IMPLEMENT_ONCE
#define FELINE_IMPLEMENT_ONCE
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
if (fdwReason == DLL_PROCESS_ATTACH) {
hostFelineApp = GetModuleHandle(NULL);
feline_getInternalException = (ObjClass * (*)(VM*, InternalExceptionType))GetProcAddress(hostFelineApp, "export_getInternalException");
feline_throwException = (void (*)(VM*, ObjClass*, const char*, ...))GetProcAddress(hostFelineApp, "throwException");
feline_isInstance = (bool (*)(Value))GetProcAddress(hostFelineApp, "export_isInstance");
feline_asInstance = (ObjInstance * (*)(Value))GetProcAddress(hostFelineApp, "export_asInstance");
feline_isString = (bool (*)(Value))GetProcAddress(hostFelineApp, "export_isString");
feline_asString = (ObjString * (*)(Value))GetProcAddress(hostFelineApp, "export_asString");
feline_getInstanceField = (bool (*)(VM*, ObjInstance*, const char*, Value*))GetProcAddress(hostFelineApp, "export_getInstanceField");
feline_setInstanceField = (bool (*)(VM*, ObjInstance*, const char*, Value))GetProcAddress(hostFelineApp, "export_setInstanceField");
feline_getStringCharacters = (char* (*)(ObjString * string))GetProcAddress(hostFelineApp, "export_getStringCharacters");
feline_getStringLength = (size_t(*)(ObjString * string))GetProcAddress(hostFelineApp, "export_getStringLength");
feline_setInstanceNativeData = (void (*)(ObjInstance * instance, InstanceData * data))GetProcAddress(hostFelineApp, "export_setInstanceNativeData");
feline_getInstanceNativeData = (InstanceData * (*)(ObjInstance * instance))GetProcAddress(hostFelineApp, "export_getInstanceNativeData");
feline_takeString = (ObjString * (*)(VM * vm, char* str, size_t length))GetProcAddress(hostFelineApp, "takeString");
feline_copyString = (ObjString * (*)(VM * vm, const char* str, size_t length))GetProcAddress(hostFelineApp, "copyString");
}
return TRUE;
}
#endif
#endif