diff --git a/docs/changes.md b/docs/changes.md index 498eddb2e..9c1ab26b5 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,4 +1,6 @@ # 最新动态 +2022/02/18 + * 增加函数fscript\_set\_global\_object。 2022/02/16 * 增加函数ui\_loader\_load\_widget\_from\_xml。 diff --git a/src/tkc/fscript.c b/src/tkc/fscript.c index dd6e19b07..4c0be88b1 100644 --- a/src/tkc/fscript.c +++ b/src/tkc/fscript.c @@ -2736,12 +2736,23 @@ tk_object_t* fscript_get_global_object(void) { return s_global_obj; } -ret_t fscript_global_init(void) { - tk_object_t* obj = object_default_create_ex(FALSE); - return_value_if_fail(obj != NULL, RET_BAD_PARAMS); +ret_t fscript_set_global_object(tk_object_t* obj) { + TK_OBJECT_UNREF(s_global_obj); - s_global_obj = object_locker_create(obj); - TK_OBJECT_UNREF(obj); + TK_OBJECT_REF(obj); + s_global_obj = obj; + + return RET_OK; +} + +ret_t fscript_global_init(void) { + if (s_global_obj != NULL) { + tk_object_t* obj = object_default_create_ex(FALSE); + return_value_if_fail(obj != NULL, RET_BAD_PARAMS); + + s_global_obj = object_locker_create(obj); + TK_OBJECT_UNREF(obj); + } return RET_OK; } diff --git a/src/tkc/fscript.h b/src/tkc/fscript.h index 89cc97864..df574c10c 100644 --- a/src/tkc/fscript.h +++ b/src/tkc/fscript.h @@ -330,6 +330,16 @@ ret_t fscript_eval(tk_object_t* obj, const char* script, value_t* result); */ ret_t fscript_global_init(void); +/** + * @method fscript_set_global_object + * 用于替换默认的全局对象。 + * >仅限于在系统初始化时调用。 + * @param {tk_object_t*} obj 全局对象。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t fscript_set_global_object(tk_object_t* obj); + /** * @method fscript_register_func * 注册全局自定义函数。