diff --git a/core/src/ten_runtime/binding/go/interface/ten/extension.go b/core/src/ten_runtime/binding/go/interface/ten/extension.go index 0342d06c07..a49c801c74 100644 --- a/core/src/ten_runtime/binding/go/interface/ten/extension.go +++ b/core/src/ten_runtime/binding/go/interface/ten/extension.go @@ -18,6 +18,7 @@ import ( ) type Extension interface { + OnConfigure(tenEnv TenEnv) OnInit( tenEnv TenEnv, ) @@ -35,6 +36,10 @@ type DefaultExtension struct{} var _ Extension = new(DefaultExtension) +func (p *DefaultExtension) OnConfigure(tenEnv TenEnv) { + tenEnv.OnConfigureDone() +} + func (p *DefaultExtension) OnInit( tenEnv TenEnv, ) { @@ -132,8 +137,8 @@ func newExtensionWithBridge( } // -//export tenGoExtensionOnInit -func tenGoExtensionOnInit( +//export tenGoExtensionOnConfigure +func tenGoExtensionOnConfigure( extensionID C.uintptr_t, tenEnvID C.uintptr_t, ) { @@ -165,6 +170,41 @@ func tenGoExtensionOnInit( tenEnvInstance.attachToExtension(extensionObj) + extensionObj.OnConfigure(tenEnvObj) +} + +// +//export tenGoExtensionOnInit +func tenGoExtensionOnInit( + extensionID C.uintptr_t, + tenEnvID C.uintptr_t, +) { + extensionObj, ok := loadImmutableHandle(goHandle(extensionID)).(*extension) + if !ok { + panic( + fmt.Sprintf( + "Failed to get extension from handle map, id: %d.", + uintptr(extensionID), + ), + ) + } + + tenEnvObj, ok := handle(tenEnvID).get().(TenEnv) + if !ok { + panic( + fmt.Sprintf( + "Failed to get ten from handle map, id: %d.", + uintptr(tenEnvID), + ), + ) + } + + tenEnvInstance, ok := tenEnvObj.(*tenEnv) + if tenEnvInstance == nil || !ok { + // Should not happen. + panic("Invalid ten object type.") + } + // As the `extension` struct embeds a user-defined extension instance // implements the Extension interface, the `OnInit` method can be called // directly on the `extensionObj` object. The receiver of the `OnInit` diff --git a/core/src/ten_runtime/binding/go/native/extension/extension.c b/core/src/ten_runtime/binding/go/native/extension/extension.c index 70c8727719..88714b19ae 100644 --- a/core/src/ten_runtime/binding/go/native/extension/extension.c +++ b/core/src/ten_runtime/binding/go/native/extension/extension.c @@ -27,6 +27,9 @@ #include "ten_utils/lib/string.h" #include "ten_utils/macro/check.h" +extern void tenGoExtensionOnConfigure(ten_go_handle_t go_extension, + ten_go_handle_t go_ten); + extern void tenGoExtensionOnInit(ten_go_handle_t go_extension, ten_go_handle_t go_ten); @@ -96,7 +99,7 @@ static void ten_go_extension_bridge_destroy(ten_go_extension_t *self) { TEN_FREE(self); } -static void proxy_on_init(ten_extension_t *self, ten_env_t *ten_env) { +static void proxy_on_configure(ten_extension_t *self, ten_env_t *ten_env) { TEN_ASSERT(self && ten_extension_check_integrity(self, true), "Should not happen."); TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true), @@ -111,6 +114,24 @@ static void proxy_on_init(ten_extension_t *self, ten_env_t *ten_env) { ten_go_ten_env_t *ten_bridge = ten_go_ten_env_wrap(ten_env); ten_bridge->c_ten_env_proxy = ten_env_proxy_create(ten_env, 1, NULL); + tenGoExtensionOnConfigure(ten_go_extension_go_handle(extension_bridge), + ten_go_ten_env_go_handle(ten_bridge)); +} + +static void proxy_on_init(ten_extension_t *self, ten_env_t *ten_env) { + TEN_ASSERT(self && ten_extension_check_integrity(self, true), + "Should not happen."); + TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true), + "Should not happen."); + TEN_ASSERT(ten_extension_get_ten(self) == ten_env, "Should not happen."); + + ten_go_extension_t *extension_bridge = + ten_binding_handle_get_me_in_target_lang((ten_binding_handle_t *)self); + TEN_ASSERT(ten_go_extension_check_integrity(extension_bridge), + "Should not happen."); + + ten_go_ten_env_t *ten_bridge = ten_go_ten_env_wrap(ten_env); + tenGoExtensionOnInit(ten_go_extension_go_handle(extension_bridge), ten_go_ten_env_go_handle(ten_bridge)); } @@ -285,9 +306,9 @@ ten_go_extension_t *ten_go_extension_create_internal( extension_bridge->bridge.sp_ref_by_c = NULL; extension_bridge->c_extension = ten_extension_create( - name, NULL, proxy_on_init, proxy_on_start, proxy_on_stop, proxy_on_deinit, - proxy_on_cmd, proxy_on_data, proxy_on_audio_frame, proxy_on_video_frame, - NULL); + name, proxy_on_configure, proxy_on_init, proxy_on_start, proxy_on_stop, + proxy_on_deinit, proxy_on_cmd, proxy_on_data, proxy_on_audio_frame, + proxy_on_video_frame, NULL); ten_binding_handle_set_me_in_target_lang( &extension_bridge->c_extension->binding_handle, extension_bridge); diff --git a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_init_property.c b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_init_property.c index 4b0c364d6e..90b5a2a54f 100644 --- a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_init_property.c +++ b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_init_property.c @@ -7,7 +7,6 @@ #include "include_internal/ten_runtime/binding/go/internal/common.h" #include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h" #include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h" -#include "ten_utils/macro/check.h" #include "ten_runtime/binding/go/interface/ten/common.h" #include "ten_runtime/binding/go/interface/ten/ten_env.h" #include "ten_runtime/common/errno.h" @@ -20,6 +19,7 @@ typedef struct ten_notify_set_init_property_info_t { ten_string_t value; + ten_error_t err; ten_event_t *completed; } ten_env_notify_init_property_info_t; @@ -30,6 +30,7 @@ ten_env_notify_init_property_info_create(const void *value, int value_len) { TEN_ASSERT(info, "Failed to allocate memory."); ten_string_init_formatted(&info->value, "%.*s", value_len, value); + ten_error_init(&info->err); info->completed = ten_event_create(0, 1); return info; @@ -40,6 +41,7 @@ static void ten_env_notify_init_property_info_destroy( TEN_ASSERT(self, "Invalid argument."); ten_string_deinit(&self->value); + ten_error_deinit(&self->err); ten_event_destroy(self->completed); TEN_FREE(self); @@ -79,24 +81,21 @@ ten_go_status_t ten_go_ten_env_init_property_from_json_bytes( return status; }); - ten_error_t err; - ten_error_init(&err); ten_env_notify_init_property_info_t *info = ten_env_notify_init_property_info_create(json_str, json_str_len); TEN_ASSERT(info, "Should not happen."); if (!ten_env_proxy_notify(self->c_ten_env_proxy, ten_env_notify_init_property_from_json, info, false, - &err)) { - ten_go_status_from_error(&status, &err); + &info->err)) { goto done; } ten_event_wait(info->completed, -1); done: + ten_go_status_from_error(&status, &info->err); ten_env_notify_init_property_info_destroy(info); - ten_error_deinit(&err); TEN_GO_TEN_IS_ALIVE_REGION_END(self); ten_is_close: diff --git a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c index f6ca07b56e..cc9e57850c 100644 --- a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c +++ b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c @@ -6,10 +6,10 @@ // #include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h" #include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h" -#include "ten_utils/macro/check.h" #include "ten_runtime/binding/go/interface/ten/ten_env.h" #include "ten_runtime/ten_env_proxy/ten_env_proxy.h" #include "ten_utils/lib/alloc.h" +#include "ten_utils/macro/check.h" typedef struct ten_env_notify_is_cmd_connected_info_t { bool result; @@ -53,7 +53,6 @@ static void ten_notify_is_cmd_connected(ten_env_t *ten_env, void *user_data) { info->result = ten_env_is_cmd_connected( ten_env, ten_string_get_raw_str(&info->name), &err); - TEN_ASSERT(info->result, "Should not happen."); ten_event_set(info->completed); @@ -82,6 +81,7 @@ bool ten_go_ten_env_is_cmd_connected(uintptr_t bridge_addr, const char *name) { } ten_event_wait(info->completed, -1); + result = info->result; done: ten_error_deinit(&err); diff --git a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_set_property.c b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_set_property.c index 23c2019c0f..a64a54dca2 100644 --- a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_set_property.c +++ b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_set_property.c @@ -11,7 +11,6 @@ #include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h" #include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h" #include "include_internal/ten_runtime/binding/go/value/value.h" -#include "ten_utils/macro/check.h" #include "ten_runtime/binding/go/interface/ten/common.h" #include "ten_runtime/binding/go/interface/ten/ten_env.h" #include "ten_runtime/binding/go/interface/ten/value.h" @@ -22,10 +21,12 @@ #include "ten_utils/lib/event.h" #include "ten_utils/lib/json.h" #include "ten_utils/lib/string.h" +#include "ten_utils/macro/check.h" #include "ten_utils/value/value.h" typedef struct ten_env_notify_set_property_info_t { bool result; + ten_error_t err; ten_string_t path; ten_value_t *c_value; ten_event_t *completed; @@ -39,6 +40,7 @@ ten_env_notify_set_property_info_create(const void *path, int path_len, TEN_ASSERT(info, "Failed to allocate memory."); info->result = true; + ten_error_init(&info->err); ten_string_init_formatted(&info->path, "%.*s", path_len, path); info->c_value = value; info->completed = ten_event_create(0, 1); @@ -50,6 +52,7 @@ static void ten_env_notify_set_property_info_destroy( ten_env_notify_set_property_info_t *self) { TEN_ASSERT(self, "Invalid argument."); + ten_error_deinit(&self->err); ten_string_deinit(&self->path); self->c_value = NULL; ten_event_destroy(self->completed); @@ -65,16 +68,10 @@ static void ten_env_notify_set_property(ten_env_t *ten_env, void *user_data) { ten_env_notify_set_property_info_t *info = user_data; TEN_ASSERT(info, "Should not happen."); - ten_error_t err; - ten_error_init(&err); - info->result = ten_env_set_property( - ten_env, ten_string_get_raw_str(&info->path), info->c_value, &err); - TEN_ASSERT(info->result, "Should not happen."); + ten_env, ten_string_get_raw_str(&info->path), info->c_value, &info->err); ten_event_set(info->completed); - - ten_error_deinit(&err); } static void ten_go_ten_env_set_property(ten_go_ten_env_t *self, @@ -90,25 +87,20 @@ static void ten_go_ten_env_set_property(ten_go_ten_env_t *self, ten_go_status_set_errno(status, TEN_ERRNO_TEN_IS_CLOSED); }); - ten_error_t err; - ten_error_init(&err); - ten_env_notify_set_property_info_t *info = ten_env_notify_set_property_info_create(path, path_len, value); if (!ten_env_proxy_notify(self->c_ten_env_proxy, ten_env_notify_set_property, - info, false, &err)) { - ten_go_status_from_error(status, &err); + info, false, &info->err)) { goto done; } ten_event_wait(info->completed, -1); done: + ten_go_status_from_error(status, &info->err); ten_env_notify_set_property_info_destroy(info); - ten_error_deinit(&err); - TEN_GO_TEN_IS_ALIVE_REGION_END(self); ten_is_close: diff --git a/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/default_extension_go/default_extension.go b/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/default_extension_go/default_extension.go index ba53b09e8a..dd363be109 100644 --- a/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/default_extension_go/default_extension.go +++ b/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/default_extension_go/default_extension.go @@ -133,6 +133,11 @@ func (p *extensionB) OnCmd( go func() { fmt.Println("extensionB OnCmd") + connected, err := tenEnv.IsCmdConnected("cmd_not_exist") + if err != nil || connected { + panic("Should not happen.") + } + cmdName, _ := cmd.GetName() if cmdName == "B" { var count uint32 = 0