Skip to content

Commit

Permalink
feat: 添加守护器生成工具
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperH-0630 committed Oct 12, 2021
1 parent f2289d0 commit f67f1ec
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 43 deletions.
52 changes: 36 additions & 16 deletions include/runtime/runtime.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#ifndef AFUN_RUNTIME_H
#define AFUN_RUNTIME_H

typedef struct APIFunc APIFunc;
struct APIFunc {
typedef struct APIFuncList APIFuncList;
struct APIFuncList {
char *name;
void *func; // objectAPIFunc
DlcHandle *dlc; // func 的 来源
DLC_SYMBOL(objectAPIFunc) func_; // func_和func二选一, func_时dlc无效
bool free_func_; // func_是否需要释放
};

typedef struct InheritDefine InheritDefine;
struct InheritDefine {
typedef struct InheritDefineList InheritDefineList;
struct InheritDefineList {
af_Object *obj;
};

typedef struct ObjectDefine ObjectDefine;
struct ObjectDefine {
typedef struct ObjectDefineList ObjectDefineList;
struct ObjectDefineList {
char *id;

bool free_api;
af_ObjectAPI *api;
struct APIFunc *api_list; // api和api_list只能二选一
struct APIFuncList *api_list; // api和api_list只能二选一

bool allow_inherit;
af_Object *belong;
Expand All @@ -33,29 +33,49 @@ struct ObjectDefine {
af_Object **save; // obj保存位置
};

typedef struct LiteralFunc LiteralFunc;
struct LiteralFunc {
typedef struct LiteralFuncList LiteralFuncList;
struct LiteralFuncList {
char *pattern;
char *func;
bool in_protect;
};

typedef struct TopMsgFunc TopMsgFunc;
struct TopMsgFunc {
typedef struct TopMsgFuncList TopMsgFuncList;
struct TopMsgFuncList {
char *type;
TopMsgProcessFunc *func;
DlcHandle *dlc; // func 的 来源
DLC_SYMBOL(TopMsgProcessFunc) func_; // func_和func二选一, func_时dlc无效
bool free_func_; // func_是否需要释放
};

typedef struct GuardianFuncList GuardianFuncList;
struct GuardianFuncList {
char *type;
bool always;
size_t size;

DlcHandle *dlc; // func/destruct 的 来源
GuardianFunc *func;
DLC_SYMBOL(GuardianFunc) func_;
bool free_func_; // func_是否需要释放

GuardianDestruct *destruct;
DLC_SYMBOL(GuardianDestruct) destruct_;
bool free_destruct_; // func_是否需要释放

void **data;
void (*initData)(void *data, af_Environment *env); // data初始化的函数
};

AFUN_LANG_EXPORT int runtimeTool(char *name, af_Code **code, af_Object *visitor, af_VarSpace *vs, af_Environment *env);
AFUN_LANG_EXPORT int runtimeToolImport(char *name, af_Object **obj, af_Code **code, af_Environment *env);

AFUN_LANG_EXPORT af_ObjectAPI *makeAPIFromList(const APIFunc api_list[]);
AFUN_LANG_EXPORT void makeObjectFromList(const ObjectDefine obj_def[], af_Object *visitor, af_VarSpace *vs, af_Environment *env);
AFUN_LANG_EXPORT void makeLiteralRegexFromList(const LiteralFunc literal_list[], af_Environment *env);
AFUN_LANG_EXPORT void makeTopMsgProcessFromList(const TopMsgFunc top_msg_list[], af_Environment *env);
AFUN_LANG_EXPORT af_Inherit *makeInheritFromList(const InheritDefine inherit_list[], bool is_reverse);
AFUN_LANG_EXPORT af_ObjectAPI *makeAPIFromList(const APIFuncList api_list[]);
AFUN_LANG_EXPORT void makeObjectFromList(const ObjectDefineList obj_def[], af_Object *visitor, af_VarSpace *vs, af_Environment *env);
AFUN_LANG_EXPORT void makeLiteralRegexFromList(const LiteralFuncList literal_list[], af_Environment *env);
AFUN_LANG_EXPORT void makeTopMsgProcessFromList(const TopMsgFuncList top_msg_list[], af_Environment *env);
AFUN_LANG_EXPORT af_Inherit *makeInheritFromList(const InheritDefineList inherit_list[], bool is_reverse);
AFUN_LANG_EXPORT void makeGuardianFromList(const GuardianFuncList gd_list[], af_Environment *env);

#endif //AFUN_RUNTIME_H
2 changes: 1 addition & 1 deletion src/runtime/base/__base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "__env.h"
#include "__var.h"

static const LiteralFunc literal[] = {
static const LiteralFuncList literal[] = {
/* 字符串匹配:\"[\s\S]*\" */
{.pattern="\\\"[\\s\\S]*\\\"", .func="str", .in_protect=true},
{.pattern=NULL}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/base/quit.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void funcDestruct(char *id, af_Object *obj, QuitFunc *data, af_Environmen
}

void makeQuitFunc(af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
static APIFunc api_list[] = {
static APIFuncList api_list[] = {
{.name="obj_getDataSize", .func=funcGetSize, .dlc=NULL},
{.name="obj_initData", .func=funcInit, .dlc=NULL},
{.name="obj_destructData", .func=funcDestruct, .dlc=NULL},
Expand All @@ -65,7 +65,7 @@ void makeQuitFunc(af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
{.name=NULL}
};

static ObjectDefine obj_def[] = {
static ObjectDefineList obj_def[] = {
{.id=func_id, .free_api=true, .api_list=api_list, .allow_inherit=false,
.var_name="quit", .p_self=3, .p_posterity=3, .p_external=3},
{.id=NULL}
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/base/str_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static size_t strFuncGetSize(char *id, af_Object *obj) {
}

static void strFuncInit(char *id, af_Object *obj, ObjectStrFunc *data, af_Environment *env) {
static const APIFunc api_list[] = {
static const APIFuncList api_list[] = {
{.name="obj_getDataSize", .func=strGetSize, .dlc=NULL},
{.name="obj_initData", .func=strInit, .dlc=NULL},
{.name="obj_destructData", .func=strDestruct, .dlc=NULL},
Expand Down Expand Up @@ -92,7 +92,7 @@ static void strFuncDestruct(char *id, af_Object *obj, ObjectStrFunc *data, af_En
}

void makeStrFunc(af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
static APIFunc api_list[] = {
static APIFuncList api_list[] = {
{.name="obj_getDataSize", .func=strFuncGetSize, .dlc=NULL},
{.name="obj_initData", .func=strFuncInit, .dlc=NULL},
{.name="obj_destructData", .func=strFuncDestruct, .dlc=NULL},
Expand All @@ -103,7 +103,7 @@ void makeStrFunc(af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
{.name=NULL}
};

static ObjectDefine obj_def[] = {
static ObjectDefineList obj_def[] = {
{.id=string_func_id, .free_api=true, .api_list=api_list, .allow_inherit=true,
.var_name="str", .p_self=3, .p_posterity=3, .p_external=3},
{.id=NULL}
Expand Down
83 changes: 62 additions & 21 deletions src/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ int runtimeToolImport(char *name, af_Object **obj, af_Code **code, af_Environmen

/*
* 函数名: makeAPIFromList
* 目标: 根据APIFunc生成api表并写入数据
* 目标: 根据APIFuncList生成api表并写入数据
*/
af_ObjectAPI *makeAPIFromList(const APIFunc api_list[]) {
af_ObjectAPI *makeAPIFromList(const APIFuncList api_list[]) {
af_ObjectAPI *api = makeObjectAPI();

for (const APIFunc *af = api_list; af->name != NULL; af++) {
for (const APIFuncList *af = api_list; af->name != NULL; af++) {
if (af->func != NULL) {
DLC_SYMBOL(objectAPIFunc) func = MAKE_SYMBOL_FROM_HANDLE(af->func, af->dlc, objectAPIFunc);
addAPI(func, af->name, api);
Expand All @@ -66,10 +66,10 @@ af_ObjectAPI *makeAPIFromList(const APIFunc api_list[]) {

/*
* 函数名: makeObjectFromList
* 目标: 根据ObjectDefine生成Object, 并保存到对应位置和变量空间中
* 目标: 根据ObjectDefineList生成Object, 并保存到对应位置和变量空间中
*/
void makeObjectFromList(const ObjectDefine obj_def[], af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
for (const ObjectDefine *od = obj_def; od->id != NULL; od++) {
void makeObjectFromList(const ObjectDefineList obj_def[], af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
for (const ObjectDefineList *od = obj_def; od->id != NULL; od++) {
af_ObjectAPI *api = od->api;
if (api == NULL)
api = makeAPIFromList(od->api_list);
Expand All @@ -86,21 +86,21 @@ void makeObjectFromList(const ObjectDefine obj_def[], af_Object *visitor, af_Var

/*
* 函数名: makeLiteralRegexFromList
* 目标: 根据LiteralFunc压入新的字面量处理器
* 目标: 根据LiteralFuncList压入新的字面量处理器
*/
void makeLiteralRegexFromList(const LiteralFunc literal_list[], af_Environment *env) {
for (const LiteralFunc *lt = literal_list; lt->pattern != NULL; lt++)
void makeLiteralRegexFromList(const LiteralFuncList literal_list[], af_Environment *env) {
for (const LiteralFuncList *lt = literal_list; lt->pattern != NULL; lt++)
pushLiteralRegex(lt->pattern, lt->func, lt->in_protect, env);
}

/*
* 函数名: makeTopMsgProcessFromList
* 目标: 根据TopMsgFunc压入新的字面量处理器
* 目标: 根据TopMsgFuncList压入新的字面量处理器
*/
void makeTopMsgProcessFromList(const TopMsgFunc top_msg_list[], af_Environment *env) {
for (const TopMsgFunc *tml = top_msg_list; tml->type != NULL; tml++) {
void makeTopMsgProcessFromList(const TopMsgFuncList top_msg_list[], af_Environment *env) {
for (const TopMsgFuncList *tml = top_msg_list; tml->type != NULL; tml++) {
if (tml->func != NULL) {
DLC_SYMBOL(TopMsgProcessFunc) func = MAKE_SYMBOL(tml->func, TopMsgProcessFunc);
DLC_SYMBOL(TopMsgProcessFunc) func = MAKE_SYMBOL_FROM_HANDLE(tml->func, tml->dlc, TopMsgProcessFunc);
addTopMsgProcess(tml->type, func, env);
FREE_SYMBOL(func);
continue;
Expand All @@ -115,13 +115,13 @@ void makeTopMsgProcessFromList(const TopMsgFunc top_msg_list[], af_Environment *

/*
* 函数名: makeInheritFromListReverse
* 目标: 以InheritDefine的顺序反向压入Inherit
* 目标: 以InheritDefineList的顺序反向压入Inherit
* 注意: pushInherit是反向压入, 因此InheritDefine得正向读取, 最后Inherit反向压入
*/
static af_Inherit *makeInheritFromListReverse(const InheritDefine inherit_list[]) {
static af_Inherit *makeInheritFromListReverse(const InheritDefineList inherit_list[]) {
af_Inherit *inherit = NULL;
af_Inherit **pinherit = &inherit;
for (const InheritDefine *ind = inherit_list; ind->obj != NULL; ind++) {
for (const InheritDefineList *ind = inherit_list; ind->obj != NULL; ind++) {
af_Inherit *ih = makeInherit(ind->obj);
pinherit = pushInherit(pinherit, ih);
}
Expand All @@ -131,13 +131,13 @@ static af_Inherit *makeInheritFromListReverse(const InheritDefine inherit_list[]

/*
* 函数名: makeInheritFromListForward
* 目标: 以InheritDefine的顺序压入Inherit
* 目标: 以InheritDefineList的顺序压入Inherit
* 注意: pushInherit是反向压入, 因此InheritDefine也得反向读取, 最后Inherit正向压入
*/
static af_Inherit *makeInheritFromListForward(const InheritDefine inherit_list[]) {
static af_Inherit *makeInheritFromListForward(const InheritDefineList inherit_list[]) {
af_Inherit *inherit = NULL;
af_Inherit **pinherit = &inherit;
const InheritDefine *ind = inherit_list;
const InheritDefineList *ind = inherit_list;

/* 找到最后一个元素 */
while (ind->obj != NULL)
Expand All @@ -154,11 +154,52 @@ static af_Inherit *makeInheritFromListForward(const InheritDefine inherit_list[]

/*
* 函数名: makeInheritFromList
* 目标: 根据InheritDefine生成新的Inherit
* 目标: 根据InheritDefineList生成新的Inherit
*/
af_Inherit *makeInheritFromList(const InheritDefine inherit_list[], bool is_reverse) {
af_Inherit *makeInheritFromList(const InheritDefineList inherit_list[], bool is_reverse) {
if (is_reverse)
return makeInheritFromListReverse(inherit_list);
else
return makeInheritFromListForward(inherit_list);
}

/*
* 函数名: makeGuardianFromList
* 目标: 根据GuardianFuncList压入新的字面量处理器
*/
void makeGuardianFromList(const GuardianFuncList gd_list[], af_Environment *env) {
for (const GuardianFuncList *gdl = gd_list; gdl->type != NULL; gdl++) {
DLC_SYMBOL(GuardianFunc) func = gdl->func_;
bool free_func_ = gdl->free_func_;

DLC_SYMBOL(GuardianDestruct) destruct = gdl->destruct_;
bool free_destruct_ = gdl->free_destruct_;

if (func == NULL) {
if (gdl->func == NULL)
continue; // 遇到错误
func = MAKE_SYMBOL_FROM_HANDLE(gdl->func, gdl->dlc, GuardianFunc);
free_func_ = true;
}

if (destruct == NULL) {
if (gdl->destruct == NULL)
continue; // 遇到错误
destruct = MAKE_SYMBOL_FROM_HANDLE(gdl->destruct, gdl->dlc, GuardianDestruct);
free_destruct_ = true;
}

void *tmp = NULL;
void **pdata = gdl->data;
if (pdata == NULL)
pdata = &tmp;

if (addGuardian(gdl->type, gdl->always, gdl->size, func, destruct, pdata, env))
gdl->initData(*pdata, env);

if (free_func_)
FREE_SYMBOL(func);
if (free_destruct_)
FREE_SYMBOL(destruct);
}
}

0 comments on commit f67f1ec

Please sign in to comment.